• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Aucun tag

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

Révisionae1b43635802dccd9fc793474c433b5dca23841f (tree)
l'heure2018-01-16 05:01:44
AuteurMax Filippov <jcmvbkbc@gmai...>
CommiterWaldemar Brodkorb

Message de Log

xtensa: fix strcmp

Loops with 'loop forever' annotation inside strcmp are actually meant to
loop forever. Falling through the end of the first loop may result in
equal strings being compared unequal, e.g.:

#include <string.h>

int main(void)
{
char a[4096] attribute((aligned(4)));
char b[4096] attribute((aligned(4)));

memset(a, ' ', 258 * 8);
memset(b, ' ', 258 * 8);
a[255 * 8] = 0;
a[256 * 8] = 'a';
b[255 * 8] = 0;
b[256 * 8] = 'b';
return !(strcmp(a, b) == 0);
}

Falling through the end of the second loop may result in unequal strings
being compared as equal, e.g.:

#include <string.h>

int main(void)
{
char a[4096] attribute((aligned(4)));
char b[4096] attribute((aligned(4)));

memset(a, ' ', 514 * 6);
memset(b, ' ', 514 * 6);
a[514 * 6 + 0] = 'a';
a[514 * 6 + 1] = 0;
b[514 * 6 + 0] = 'b';
b[514 * 6 + 1] = 0;
return !(strcmp(a, b) != 0);
}

Use 0 as a loop counter to make 232 - 1 iterations which is enough to
cover all addressable memory. While at it drop useless nop at the end of
the first loop and use a11 for all loop counters.

Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>

Change Summary

Modification

--- a/libc/string/xtensa/strcmp.S
+++ b/libc/string/xtensa/strcmp.S
@@ -92,8 +92,8 @@ ENTRY (strcmp)
9292 /* (2 mod 4) alignment for loop instruction */
9393 .Lunaligned:
9494 #if XCHAL_HAVE_LOOPS
95- _movi.n a8, 0 /* set up for the maximum loop count */
96- loop a8, .Lretdiff /* loop forever (almost anyway) */
95+ movi a11, 0 /* set up for the maximum loop count */
96+ loop a11, .Lretdiff /* loop forever (almost anyway) */
9797 #endif
9898 .Lnextbyte:
9999 l8ui a8, a2, 0
@@ -131,11 +131,10 @@ ENTRY (strcmp)
131131 #if XCHAL_HAVE_LOOPS
132132 .Laligned:
133133 .begin no-transform
134+ movi a11, 0
134135 l32r a4, .Lmask0 /* mask for byte 0 */
135136 l32r a7, .Lmask4
136- /* Loop forever. (a4 is more than than the maximum number
137- of iterations) */
138- loop a4, .Laligned_done
137+ loop a11, .Laligned_done /* Loop forever. */
139138
140139 /* First unrolled loop body. */
141140 l32i a8, a2, 0 /* get word from s1 */
@@ -156,8 +155,6 @@ ENTRY (strcmp)
156155 addi a2, a2, 8 /* advance s1 pointer */
157156 addi a3, a3, 8 /* advance s2 pointer */
158157 .Laligned_done:
159- or a1, a1, a1 /* nop */
160-
161158 .Lprobeq2:
162159 /* Adjust pointers to account for the loop unrolling. */
163160 addi a2, a2, 4
@@ -198,7 +195,7 @@ ENTRY (strcmp)
198195 #if XCHAL_HAVE_LOOPS
199196
200197 /* align (1 mod 4) */
201- loop a4, .Leq /* loop forever (a4 is bigger than max iters) */
198+ loop a11, .Leq /* loop forever */
202199 .end no-transform
203200
204201 l32i a8, a2, 0 /* get word from s1 */