1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *    the documentation and/or other materials provided with the
13 *    distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <private/bionic_asm.h>
30
31    .syntax unified
32
33    .thumb
34    .thumb_func
35
36    // To avoid warning about deprecated instructions, add an explicit
37    // arch. The code generated is exactly the same.
38    .arch armv7-a
39
40// Get the length of the source string first, then do a memcpy of the data
41// instead of a strcpy.
42ENTRY(__strcpy_chk)
43    pld     [r0, #0]
44    push    {r0, lr}
45    .cfi_adjust_cfa_offset 8
46    .cfi_rel_offset r0, 0
47    .cfi_rel_offset lr, 4
48
49    mov     lr, r2
50    mov     r0, r1
51
52    ands    r3, r1, #7
53    beq     .L_mainloop
54
55    // Align to a double word (64 bits).
56    rsb     r3, r3, #8
57    lsls    ip, r3, #31
58    beq     .L_align_to_32
59
60    ldrb    r2, [r0], #1
61    cbz     r2, .L_update_count_and_finish
62
63.L_align_to_32:
64    bcc     .L_align_to_64
65    ands    ip, r3, #2
66    beq     .L_align_to_64
67
68    ldrb    r2, [r0], #1
69    cbz     r2, .L_update_count_and_finish
70    ldrb    r2, [r0], #1
71    cbz     r2, .L_update_count_and_finish
72
73.L_align_to_64:
74    tst     r3, #4
75    beq     .L_mainloop
76    ldr     r3, [r0], #4
77
78    sub     ip, r3, #0x01010101
79    bic     ip, ip, r3
80    ands    ip, ip, #0x80808080
81    bne     .L_zero_in_second_register
82
83    .p2align 2
84.L_mainloop:
85    ldrd    r2, r3, [r0], #8
86
87    pld     [r0, #64]
88
89    sub     ip, r2, #0x01010101
90    bic     ip, ip, r2
91    ands    ip, ip, #0x80808080
92    bne     .L_zero_in_first_register
93
94    sub     ip, r3, #0x01010101
95    bic     ip, ip, r3
96    ands    ip, ip, #0x80808080
97    bne     .L_zero_in_second_register
98    b       .L_mainloop
99
100.L_update_count_and_finish:
101    sub     r3, r0, r1
102    sub     r3, r3, #1
103    b       .L_check_size
104
105.L_zero_in_first_register:
106    sub     r3, r0, r1
107    lsls    r2, ip, #17
108    bne     .L_sub8_and_finish
109    bcs     .L_sub7_and_finish
110    lsls    ip, ip, #1
111    bne     .L_sub6_and_finish
112
113    sub     r3, r3, #5
114    b       .L_check_size
115
116.L_sub8_and_finish:
117    sub     r3, r3, #8
118    b       .L_check_size
119
120.L_sub7_and_finish:
121    sub     r3, r3, #7
122    b       .L_check_size
123
124.L_sub6_and_finish:
125    sub     r3, r3, #6
126    b       .L_check_size
127
128.L_zero_in_second_register:
129    sub     r3, r0, r1
130    lsls    r2, ip, #17
131    bne     .L_sub4_and_finish
132    bcs     .L_sub3_and_finish
133    lsls    ip, ip, #1
134    bne     .L_sub2_and_finish
135
136    sub     r3, r3, #1
137    b       .L_check_size
138
139.L_sub4_and_finish:
140    sub     r3, r3, #4
141    b       .L_check_size
142
143.L_sub3_and_finish:
144    sub     r3, r3, #3
145    b       .L_check_size
146
147.L_sub2_and_finish:
148    sub     r3, r3, #2
149
150.L_check_size:
151    pld     [r1, #0]
152    pld     [r1, #64]
153    ldr     r0, [sp]
154
155    // Add 1 for copy length to get the string terminator.
156    add     r2, r3, #1
157
158    cmp     r2, lr
159    itt     hi
160    movhi   r0, r2
161    bhi     __strcpy_chk_fail
162
163#include "memcpy_base.S"
164
165END(__strcpy_chk)
166