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

msan: incorrect origin for 64-bit values

Open KernelAddress opened this issue 8 years ago • 7 comments

Bugzilla Link 37138
Version unspecified
OS Linux
CC @dwblaikie,@ramosian-glider,@kcc,@vitalybuka

Extended Description

clang version 7.0.0 (trunk 326052)

#include <stdlib.h>

attribute((noinline)) void inithalf(void* x) { (int)x = 0; }

attribute((noinline)) void copyhalf(void* x, void* y) { (int)x = (int)y; }

attribute((noinline)) void copy(long long* x, long long* y) { *x = *y; }

int main() { long long x, y, z; copyhalf(&y, &z); inithalf(&y); copy(&x, &y); if (x) exit(1); }

==258685==WARNING: MemorySanitizer: use-of-uninitialized-value #​0 0x499df4 in main /tmp/msan.c:23:6

#&#8203;1 0x7f00b44cb2b0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202b0)
#&#8203;2 0x41d639 in _start (/usr/local/google/home/dvyukov/src/linux2/a.out+0x41d639)

Uninitialized value was stored to memory at #​0 0x499c98 in copy /tmp/msan.c:15:5

Uninitialized value was stored to memory at #​0 0x499c03 in copyhalf /tmp/msan.c:10:11

Uninitialized value was created by an allocation of 'z' in the stack frame of function 'main' #​0 0x499cd0 in main /tmp/msan.c:18

SUMMARY: MemorySanitizer: use-of-uninitialized-value /tmp/msan.c:23:6 in main

Uninit value comes from 'y'.

KernelAddress avatar Apr 16 '18 08:04 KernelAddress

I'd say either is correct. To start with, 4 byte granularity of origins is arbitrary, so it is always OK (but not ideal!) to "coarsen" origin data.

Sure, I agree that it would be great if 8-byte load/store sequence could preserve all origins. As I described in #​4, it would require reshuffling the entire instrumentation pass, which currently assumes that origin for IR temp of any type is always i32.

eugenis avatar Apr 19 '18 18:04 eugenis

I understand that there can be more complex cases (e.g. shifts). But it would be nice if our general algorithm, when applied to the simplest case of just loading and string an uint64, would produce correct results.

I am not sure there is always a correct single part of the double origin. Consider that both parts are initialized and have different origins. Which one is correct?

KernelAddress avatar Apr 19 '18 07:04 KernelAddress

To load 8 bytes of origins and then store 8 bytes of origins, we need to associate 8 bytes of origins with an i64 IR temporary. Probably as an {i32, i32} temp. That's where it gets interesting, because it affects literally everything in MSan. For example, what do we do if the i64 is shifted right by 3 bits? What if it goes though a SIMD shuffle?

Done correctly, MSan would need, for each half of the output (assuming output is i64 as well), find out which uninitialized bits it is affected by and pick any of those bit's origin. That's a big can of worms.

A simpler solution is to dumb down origins into a single i32 for any IR temp. We almost have it right now, we just need to tweak LoadInst to load origin from the correct half.

eugenis avatar Apr 18 '18 22:04 eugenis

For an 8-byte store we don't need to look at shadow values at all. Without chained origins we just load 8 bytes of src origins and store then as-is to dst origins. This is as fast as one can get. There problem here is with chained origins. We pass only first origin for chaining and then store it into both dst origin slots. We should somehow chain both of them, however if they are equal (common case) we just need to check that they are equal and don't chain twice.

KernelAddress avatar Apr 18 '18 18:04 KernelAddress

The problem is in visitLoadInst, which loads origin for the entire 8-byte value from the first half. This is incorrect when the first half is fully initialized. I'm worried that doing this correctly would have a large perf impact - every 8-byte load instruction would need to compare the first 4 bytes of shadow with zero, and conditionally load origin for either the first or the second half.

Actually, it does not sound that bad. Needs to be benchmarked.

Another option is to extend instrumentation to associate more than one origin value with an IR temp. I.e. an 8-byte temp would get 2 origins, and a SIMD vector would get a vector of origins. There has been reports of similar issues with vectors. But this would be a huge change, and definitely a performance hit as well.

eugenis avatar Apr 16 '18 20:04 eugenis

run as: clang test.c -O2 -g -fsanitize=memory -fsanitize-memory-track-origins && ./a.out

KernelAddress avatar Apr 16 '18 08:04 KernelAddress

assigned to @eugenis

KernelAddress avatar Apr 16 '18 08:04 KernelAddress