Skip to content

Commit 1bfbe1f

Browse files
wtarreaupaulmckrcu
authored andcommitted
tools/nolibc: prevent gcc from making memset() loop over itself
When building on ARM in thumb mode with gcc-11.3 at -O2 or -O3, nolibc-test segfaults during the select() tests. It turns out that at this level, gcc recognizes an opportunity for using memset() to zero the fd_set, but it miscompiles it because it also recognizes a memset pattern as well, and decides to call memset() from the memset() code: 000122bc <memset>: 122bc: b510 push {r4, lr} 122be: 0004 movs r4, r0 122c0: 2a00 cmp r2, #0 122c2: d003 beq.n 122cc <memset+0x10> 122c4: 23ff movs r3, #255 ; 0xff 122c6: 4019 ands r1, r3 122c8: f7ff fff8 bl 122bc <memset> 122cc: 0020 movs r0, r4 122ce: bd10 pop {r4, pc} Simply placing an empty asm() statement inside the loop suffices to avoid this. Signed-off-by: Willy Tarreau <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]>
1 parent 55abdd1 commit 1bfbe1f

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

tools/include/nolibc/string.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,11 @@ void *memset(void *dst, int b, size_t len)
8888
{
8989
char *p = dst;
9090

91-
while (len--)
91+
while (len--) {
92+
/* prevent gcc from recognizing memset() here */
93+
asm volatile("");
9294
*(p++) = b;
95+
}
9396
return dst;
9497
}
9598

0 commit comments

Comments
 (0)