On Tue, May 05, 2020 at 06:14:53PM -0600, Jason A. Donenfeld wrote:
> clang-10 has a broken optimization stage that doesn't allow the
> compiler to prove at compile time that certain memcpys are within
> bounds, and thus the outline memcpy is always called, resulting in
> horrific performance, and in some cases, excessive stack frame growth.
> Here's a simple reproducer:
>
> typedef unsigned long size_t;
> void *c(void *dest, const void *src, size_t n) __asm__("memcpy");
> extern inline __attribute__((gnu_inline)) void *memcpy(void *dest, const
> void *src, size_t n) { return c(dest, src, n); }
> void blah(char *a)
> {
> unsigned long long b[10], c[10];
> int i;
>
> memcpy(b, a, sizeof(b));
> for (i = 0; i < 10; ++i)
> c[i] = b[i] ^ b[9 - i];
> for (i = 0; i < 10; ++i)
> b[i] = c[i] ^ a[i];
> memcpy(a, b, sizeof(b));
> }
>
> Compile this with clang-9 and clang-10 and observe:
>
> zx2c4@thinkpad /tmp/curve25519-hacl64-stack-frame-size-test $ clang-10
> -Wframe-larger-than=0 -O3 -c b.c -o c10.o
> b.c:5:6: warning: stack frame size of 104 bytes in function 'blah'
> [-Wframe-larger-than=]
> void blah(char *a)
> ^
> 1 warning generated.
> zx2c4@thinkpad /tmp/curve25519-hacl64-stack-frame-size-test $ clang-9
> -Wframe-larger-than=0 -O3 -c b.c -o c9.o
>
> Looking at the disassembly of c10.o and c9.o, one can see that c9.o is
> properly optimized in the obvious way one would expect, while c10.o has
> blown up and includes extern calls to memcpy.
>
> But actually, for versions of clang earlier than 10, fortify source
> mostly does nothing. So, between being broken and doing nothing, it
> probably doesn't make sense to pretend to offer this option. So, this
> commit just disables it entirely when compiling with clang.
>
> Cc: Arnd Bergmann <[email protected]>
> Cc: LKML <[email protected]>
> Cc: clang-built-linux <[email protected]>
> Cc: Kees Cook <[email protected]>
> Cc: George Burgess <[email protected]>
> Cc: Nick Desaulniers <[email protected]>
> Link: https://bugs.llvm.org/show_bug.cgi?id=45802
> Signed-off-by: Jason A. Donenfeld <[email protected]>
Grudgingly,
Reviewed-by: Kees Cook <[email protected]>
--
Kees Cook