1 // Test that tagging a large region to 0 reduces RSS.
2 // RUN: %clang_hwasan -mllvm -hwasan-globals=0 -mllvm -hwasan-instrument-stack=0 %s -o %t && %run %t 2>&1
4 // REQUIRES: pointer-tagging
12 #include <sys/types.h>
15 #include <sanitizer/hwasan_interface.h>
17 const unsigned char kTag
= 42;
18 const size_t kNumShadowPages
= 1024;
19 const size_t kNumPages
= 16 * kNumShadowPages
;
20 const size_t kPageSize
= 4096;
21 const size_t kMapSize
= kNumPages
* kPageSize
;
24 char *page
= (char *)mmap(0, kPageSize
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_ANONYMOUS
, 0, 0);
25 // Linux kernel updates RSS counters after a set number of page faults.
26 for (int i
= 0; i
< 100; ++i
) {
28 madvise(page
, kPageSize
, MADV_DONTNEED
);
30 munmap(page
, kPageSize
);
33 size_t current_rss() {
35 int statm_fd
= open("/proc/self/statm", O_RDONLY
);
36 assert(statm_fd
>= 0);
39 assert(read(statm_fd
, &buf
, sizeof(buf
)) > 0);
41 assert(sscanf(buf
, "%zu %zu", &size
, &rss
) == 2);
47 int test_rss_difference(void *p
) {
48 __hwasan_tag_memory(p
, kTag
, kMapSize
);
49 size_t rss_before
= current_rss();
50 __hwasan_tag_memory(p
, 0, kMapSize
);
51 size_t rss_after
= current_rss();
52 fprintf(stderr
, "%zu -> %zu\n", rss_before
, rss_after
);
53 if (rss_before
<= rss_after
)
55 size_t diff
= rss_before
- rss_after
;
56 fprintf(stderr
, "diff %zu\n", diff
);
57 // Check that the difference is at least close to kNumShadowPages.
58 return diff
> kNumShadowPages
/ 2;
62 fprintf(stderr
, "starting rss %zu\n", current_rss());
63 fprintf(stderr
, "shadow pages: %zu\n", kNumShadowPages
);
65 void *p
= mmap(0, kMapSize
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_ANONYMOUS
, 0, 0);
66 fprintf(stderr
, "p = %p\n", p
);
68 size_t total_count
= 10;
69 size_t success_count
= 0;
70 for (size_t i
= 0; i
< total_count
; ++i
)
71 success_count
+= test_rss_difference(p
);
73 fprintf(stderr
, "p = %p\n", p
);
74 fprintf(stderr
, "passed %zu out of %zu\n", success_count
, total_count
);
75 assert(success_count
> total_count
* 0.8);