[asan] Avoid instrumenting duplicated memory access in the same basic block
[gcc.git] / gcc / testsuite / c-c++-common / asan / no-redundant-instrumentation-1.c
1 /* This tests that when faced with two references to the same memory
2 location in the same basic block, the second reference should not
3 be instrumented by the Address Sanitizer. */
4
5 /* { dg-options "-fdump-tree-asan0" } */
6 /* { dg-do compile } */
7 /* { dg-skip-if "" { *-*-* } { "*" } { "-O0" } } */
8
9 static char tab[4] = {0};
10
11 static int
12 test0 ()
13 {
14 /* __builtin___asan_report_store1 called 2 times for the two stores
15 below. */
16 tab[0] = 1;
17 tab[1] = 2;
18
19 /* __builtin___asan_report_load1 called 1 time for the store
20 below. */
21 char t0 = tab[1];
22
23 /* This load should not be instrumented because it is to the same
24 memory location as above. */
25 char t1 = tab[1];
26
27 return t0 + t1;
28 }
29
30 static int
31 test1 ()
32 {
33 /*__builtin___asan_report_store1 called 1 time here to instrument
34 the initialization. */
35 char foo[4] = {1};
36
37 /*__builtin___asan_report_store1 called 2 times here to instrument
38 the store to the memory region of tab. */
39 __builtin_memset (tab, 3, sizeof (tab));
40
41 /* There is no instrumentation for the two memset calls below. */
42 __builtin_memset (tab, 4, sizeof (tab));
43 __builtin_memset (tab, 5, sizeof (tab));
44
45 /* There are 2 calls to __builtin___asan_report_store1 and 2 calls
46 to __builtin___asan_report_load1 to instrument the store to
47 (subset of) the memory region of tab. */
48 __builtin_memcpy (&tab[1], foo, sizeof (tab) - 1);
49
50 /* This should not generate a __builtin___asan_report_load1 because
51 the reference to tab[1] has been already instrumented above. */
52 return tab[1];
53
54 /* So for these function, there should be 7 calls to
55 __builtin___asan_report_store1. */
56 }
57
58 int
59 main ()
60 {
61 return test0 () && test1 ();
62 }
63
64 /* { dg-final { scan-tree-dump-times "__builtin___asan_report_store1" 7 "asan0" } } */
65 /* { dg-final { scan-tree-dump-times "__builtin___asan_report_load" 2 "asan0" } } */
66 /* { dg-final { cleanup-tree-dump "asan0" } } */