xref: /llvm-project/compiler-rt/test/hwasan/TestCases/enable-disable.c (revision 43aa6e6bf3d5ca1dde3e839f4c6ebd0e524055a1)
1 // Test that disabling/enabling tagging does not trigger false reports on
2 // allocations happened in a different state.
3 
4 // RUN: %clang_hwasan -O1 %s -o %t && %run %t 2>&1
5 
6 #include <assert.h>
7 #include <sanitizer/hwasan_interface.h>
8 #include <stdlib.h>
9 
10 enum {
11   COUNT = 5,
12   SZ = 10,
13 };
14 void *x[COUNT];
15 
main()16 int main() {
17   __hwasan_enable_allocator_tagging();
18   for (unsigned i = 0; i < COUNT; ++i) {
19     x[i] = malloc(SZ);
20     assert(__hwasan_test_shadow(x[i], SZ) == -1);
21   }
22   for (unsigned i = 0; i < COUNT; ++i)
23     free(x[i]);
24 
25   __hwasan_disable_allocator_tagging();
26   for (unsigned i = 0; i < COUNT; ++i) {
27     x[i] = malloc(SZ);
28     assert(__hwasan_tag_pointer(x[i], 0) == x[i]);
29     assert(__hwasan_test_shadow(x[i], SZ) == -1);
30   }
31   for (unsigned i = 0; i < COUNT; ++i)
32     free(x[i]);
33 
34   __hwasan_enable_allocator_tagging();
35   for (unsigned i = 0; i < COUNT; ++i) {
36     x[i] = malloc(SZ);
37     assert(__hwasan_test_shadow(x[i], SZ) == -1);
38   }
39   for (unsigned i = 0; i < COUNT; ++i)
40     free(x[i]);
41   return 0;
42 }
43