swapcontext_annotation_reset.cpp
1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// RUN: %clangxx_msan -fno-sanitize=memory -c %s -o %t-main.o
// RUN: %clangxx_msan %t-main.o %s -o %t
// RUN: %run %t
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <ucontext.h>
#include <unistd.h>
#include <sanitizer/msan_interface.h>
#if __has_feature(memory_sanitizer)
__attribute__((noinline)) int bar(int a, int b) {
volatile int zero = 0;
return zero;
}
void foo(int x, int y, int expected) {
assert(__msan_test_shadow(&x, sizeof(x)) == expected);
assert(__msan_test_shadow(&y, sizeof(y)) == expected);
// Poisons parameter shadow in TLS so that the next call (to foo) from
// uninstrumented main has params 1 and 2 poisoned no matter what.
int a, b;
(void)bar(a, b);
}
#else
// This code is not instrumented by MemorySanitizer to prevent it from modifying
// MSAN TLS data for this test.
int foo(int, int, int);
int main(int argc, char **argv) {
int x, y;
// The parameters should _not_ be poisoned; this is the first call to foo.
foo(x, y, -1);
// The parameters should be poisoned; the prior call to foo left them so.
foo(x, y, 0);
ucontext_t ctx;
if (getcontext(&ctx) == -1) {
perror("getcontext");
_exit(1);
}
// Simulate a fiber switch occurring from MSAN's perspective (though no switch
// actually occurs).
const void *previous_stack_bottom = nullptr;
size_t previous_stack_size = 0;
__msan_start_switch_fiber(ctx.uc_stack.ss_sp, ctx.uc_stack.ss_size);
__msan_finish_switch_fiber(&previous_stack_bottom, &previous_stack_size);
// The simulated fiber switch will reset the TLS parameter shadow. So even
// though the most recent call to foo left the parameter shadow poisoned, the
// parameters are _not_ expected to be poisoned now.
foo(x, y, -1);
return 0;
}
#endif