blob: 969e112cced33a5cb9925a7fbba96a57a17457e5 [file] [log] [blame]
Christopher Ferris5f45d582013-08-07 13:09:51 -07001/*
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
Elliott Hughes851e68a2014-02-19 16:53:20 -080029#include <private/bionic_asm.h>
Christopher Ferris5f45d582013-08-07 13:09:51 -070030
31 .syntax unified
32
33 .thumb
34 .thumb_func
35
Christopher Ferris866e7b62017-05-24 16:00:37 -070036 // To avoid warning about deprecated instructions, add an explicit
37 // arch. The code generated is exactly the same.
38 .arch armv7-a
39
Christopher Ferris5f45d582013-08-07 13:09:51 -070040// Get the length of the source string first, then do a memcpy of the data
41// instead of a strcpy.
Haibo Huangf1c8d1a2018-11-27 15:38:47 -080042ENTRY(__strcpy_chk_krait)
Christopher Ferris5f45d582013-08-07 13:09:51 -070043 pld [r0, #0]
Christopher Ferris5f45d582013-08-07 13:09:51 -070044 push {r0, lr}
Brent DeGraaf1d0268c2013-10-02 13:47:11 +000045 .cfi_adjust_cfa_offset 8
Christopher Ferris5f45d582013-08-07 13:09:51 -070046 .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]
Christopher Ferris5f45d582013-08-07 13:09:51 -0700154
155 // Add 1 for copy length to get the string terminator.
156 add r2, r3, #1
Christopher Ferris5f45d582013-08-07 13:09:51 -0700157
Elliott Hughesbdd8f892016-05-26 16:38:34 -0700158 cmp r2, lr
159 itt hi
160 movhi r0, r2
161 bhi __strcpy_chk_fail
162
Christopher Ferrisa57c9c02013-08-21 09:41:12 -0700163#include "memcpy_base.S"
164
Haibo Huangf1c8d1a2018-11-27 15:38:47 -0800165END(__strcpy_chk_krait)