llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Inline Assembly does not honor the =A output constraint

Open KernelAddress opened this issue 13 years ago • 9 comments

Bugzilla Link 14741
Resolution INVALID
Resolved on Jan 02, 2013 10:50
Version trunk
OS Linux
CC @KernelAddress,@echristo,@efriedma-quic,@kcc

Extended Description

$ cat /tmp/test.cc #include <stdio.h> typedef unsigned long long tick_t; #define RDTICK()
({ tick_t __t; asm volatile ("rdtsc" : "=A" (__t)); __t; }) tick_t read_cpu_ticks() {return RDTICK();} int main() { printf("%llu\n", read_cpu_ticks()); }

$ clang++ -v clang version 3.3 (trunk 171216) Target: x86_64-unknown-linux-gnu Thread model: posix

$ clang++ /tmp/test.cc && ./a.out 16337390

$ clang++ /tmp/test.cc -fsanitize=address && ./a.out ASAN:SIGSEGV

==411== ERROR: AddressSanitizer: SEGV on unknown address 0x000000047d83 (pc 0x000000418226 sp 0x7fffc5fed520 bp 0x7fffc5fed5d0 T0) AddressSanitizer can not provide additional info. #​0 0x418225 (/usr/local/google/home/dvyukov/src/fc/fc_benchmark_15_6_2010/a.out+0x418225) #​1 0x41826c (/usr/local/google/home/dvyukov/src/fc/fc_benchmark_15_6_2010/a.out+0x41826c) #​2 0x7fe48ddf476c (/lib/x86_64-linux-gnu/libc-2.15.so+0x2176c) Stats: 0M malloced (0M for red zones) by 0 calls Stats: 0M realloced by 0 calls Stats: 0M freed by 0 calls Stats: 0M really freed by 0 calls Stats: 0M (0M-0M) mmaped; 0 maps, 0 unmaps mmaps by size class: mallocs by size class: frees by size class: rfrees by size class: Stats: malloc large: 0 small slow: 0 Stats: StackDepot: 0 ids; 0M mapped ==411== ABORTING

KernelAddress avatar Dec 29 '12 11:12 KernelAddress

On x86-64, if the operand to "=A" is a 64-bit integer, it is held entirely in RAX. So your inline asm not only fails to correctly clobber RDX, it won't do what you expect. (Blindly reusing inline assembly written for x86-32 is not a good idea.)

efriedma-quic avatar Jan 02 '13 18:01 efriedma-quic

% clang -O0 ~/tmp/rdx.cc ; ./a.out Res: 3ffe00000000 Expecting: 3ffe00000000 % clang -O1 ~/tmp/rdx.cc ; ./a.out Res: 3efe0001dca2 Expecting: 3ffe00000000

here is a repro w/o asan

we force all registers to be used before rdtsc and reused after it. one of the values gets overwritten by rdtsc and receives lower bits of time stamp instead of zeroes.

kcc avatar Dec 29 '12 15:12 kcc

repro

kcc avatar Dec 29 '12 15:12 kcc

building a test with 'clang -S -o - -emit-llvm ' shows that LLVM transforms the inline assembly constraints as follows:

"=A"(t) ===> "=A,~{dirflag},~{fpsr},~{flags}" "=a"(t) ===> "={ax},~{dirflag},~{fpsr},~{flags}" "=d"(t) ===> "={dx},~{dirflag},~{fpsr},~{flags}"

kcc avatar Dec 29 '12 14:12 kcc

Apparently, =A is not honored:

% cat rdtsc.cc long long rdtsc_low() { long long t; asm volatile ("rdtsc" : "=A" (t)); return t; }

int main() { rdtsc_low(); } % clang -g -fsanitize=address /home/kcc/tmp/rdtsc.cc && ./a.out ASAN:SIGSEGV

4144f4: 0f 31 rdtsc
4144f6: 89 c6 mov %eax,%esi 4144f8: 48 89 d7 mov %rdx,%rdi

right after rdtsc %rdx is used as if it is unchanged.

kcc avatar Dec 29 '12 14:12 kcc

I can't find the official docs, but this page http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html says:

"A" : Specifies the a’ or d’ registers. This is primarily useful for 64-bit integer values intended to be returned with the d’ register holding the most significant bits and the a’ register holding the least significant bits.

KernelAddress avatar Dec 29 '12 13:12 KernelAddress

Or more like this:

long long rdtsc() { unsigned u, l; asm volatile ("rdtsc" : "=a" (l), "=d" (u)); long long r = u; return (r << 32) | l; }

kcc avatar Dec 29 '12 13:12 kcc

The code is invalid. rdtsc changes rax and rdx, but the inline assembly blob doesn't tell it. It should look something like this: asm volatile ("rdtsc" : "=A" (t) : : "rax", "rdx");

kcc avatar Dec 29 '12 13:12 kcc

This only happens at O0:

% clang++ ~/tmp/rdtsc.cc -O1 -fsanitize=address && ./a.out 1419155744 % clang++ ~/tmp/rdtsc.cc -O0 -fsanitize=address && ./a.out ASAN:SIGSEGV

kcc avatar Dec 29 '12 12:12 kcc