blob: 4b125c89cf41f5e472af63d902fc7f366c53bf90 [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
29#include <machine/asm.h>
30#include "libc_events.h"
31
32 .syntax unified
33
34 .thumb
35 .thumb_func
36
37// Get the length of src string, then get the source of the dst string.
38// Check that the two lengths together don't exceed the threshold, then
39// do a memcpy of the data.
40ENTRY(__strcat_chk)
41 .cfi_startproc
42 pld [r0, #0]
Christopher Ferrisbd7fe1d2013-08-20 11:20:48 -070043 .save {r0, lr}
Christopher Ferris5f45d582013-08-07 13:09:51 -070044 push {r0, lr}
45 .cfi_def_cfa_offset 8
46 .cfi_rel_offset r0, 0
47 .cfi_rel_offset lr, 4
Christopher Ferrisbd7fe1d2013-08-20 11:20:48 -070048 .save {r4, r5}
Christopher Ferris5f45d582013-08-07 13:09:51 -070049 push {r4, r5}
50 .cfi_adjust_cfa_offset 8
51 .cfi_rel_offset r4, 0
52 .cfi_rel_offset r5, 4
53
54 mov lr, r2
55
56 // Save the dst register to r5
57 mov r5, r0
58
59 // Zero out r4
60 eor r4, r4, r4
61
62 // r1 contains the address of the string to count.
63.L_strlen_start:
64 mov r0, r1
65 ands r3, r1, #7
66 beq .L_mainloop
67
68 // Align to a double word (64 bits).
69 rsb r3, r3, #8
70 lsls ip, r3, #31
71 beq .L_align_to_32
72
73 ldrb r2, [r1], #1
74 cbz r2, .L_update_count_and_finish
75
76.L_align_to_32:
77 bcc .L_align_to_64
78 ands ip, r3, #2
79 beq .L_align_to_64
80
81 ldrb r2, [r1], #1
82 cbz r2, .L_update_count_and_finish
83 ldrb r2, [r1], #1
84 cbz r2, .L_update_count_and_finish
85
86.L_align_to_64:
87 tst r3, #4
88 beq .L_mainloop
89 ldr r3, [r1], #4
90
91 sub ip, r3, #0x01010101
92 bic ip, ip, r3
93 ands ip, ip, #0x80808080
94 bne .L_zero_in_second_register
95
96 .p2align 2
97.L_mainloop:
98 ldrd r2, r3, [r1], #8
99
100 pld [r1, #64]
101
102 sub ip, r2, #0x01010101
103 bic ip, ip, r2
104 ands ip, ip, #0x80808080
105 bne .L_zero_in_first_register
106
107 sub ip, r3, #0x01010101
108 bic ip, ip, r3
109 ands ip, ip, #0x80808080
110 bne .L_zero_in_second_register
111 b .L_mainloop
112
113.L_update_count_and_finish:
114 sub r3, r1, r0
115 sub r3, r3, #1
116 b .L_finish
117
118.L_zero_in_first_register:
119 sub r3, r1, r0
120 lsls r2, ip, #17
121 bne .L_sub8_and_finish
122 bcs .L_sub7_and_finish
123 lsls ip, ip, #1
124 bne .L_sub6_and_finish
125
126 sub r3, r3, #5
127 b .L_finish
128
129.L_sub8_and_finish:
130 sub r3, r3, #8
131 b .L_finish
132
133.L_sub7_and_finish:
134 sub r3, r3, #7
135 b .L_finish
136
137.L_sub6_and_finish:
138 sub r3, r3, #6
139 b .L_finish
140
141.L_zero_in_second_register:
142 sub r3, r1, r0
143 lsls r2, ip, #17
144 bne .L_sub4_and_finish
145 bcs .L_sub3_and_finish
146 lsls ip, ip, #1
147 bne .L_sub2_and_finish
148
149 sub r3, r3, #1
150 b .L_finish
151
152.L_sub4_and_finish:
153 sub r3, r3, #4
154 b .L_finish
155
156.L_sub3_and_finish:
157 sub r3, r3, #3
158 b .L_finish
159
160.L_sub2_and_finish:
161 sub r3, r3, #2
162
163.L_finish:
164 cmp r4, #0
165 bne .L_strlen_done
166
167 // Time to get the dst string length.
168 mov r1, r5
169
170 // Save the original source address to r5.
171 mov r5, r0
172
173 // Save the current length (adding 1 for the terminator).
174 add r4, r3, #1
175 b .L_strlen_start
176
177 // r0 holds the pointer to the dst string.
178 // r3 holds the dst string length.
179 // r4 holds the src string length + 1.
180.L_strlen_done:
181 add r2, r3, r4
182 cmp r2, lr
183 bgt .L_fortify_check_failed
184
185 // Set up the registers for the memcpy code.
186 mov r1, r5
187 pld [r1, #64]
188 mov r2, r4
189 add r0, r0, r3
Christopher Ferrisbd7fe1d2013-08-20 11:20:48 -0700190 .pad #-8
Christopher Ferris5f45d582013-08-07 13:09:51 -0700191 pop {r4, r5}
192 .cfi_adjust_cfa_offset -8
193 .cfi_restore r4
194 .cfi_restore r5
195
196 #include "memcpy_base.S"
197
198.L_fortify_check_failed:
199 .cfi_adjust_cfa_offset 8
200 .cfi_rel_offset r4, 0
201 .cfi_rel_offset r5, 4
202
203 ldr r0, error_message
204 ldr r1, error_code
2051:
206 add r0, pc
207 bl __fortify_chk_fail
208error_code:
209 .word BIONIC_EVENT_STRCAT_BUFFER_OVERFLOW
210error_message:
211 .word error_string-(1b+4)
212
213 .cfi_endproc
214END(__strcat_chk)
215
216 .data
217error_string:
218 .string "strcat buffer overflow"