[NFC][AArch64] Explicitly define undefined bits for instructions (#122129)
[llvm-project.git] / compiler-rt / test / hwasan / TestCases / Linux / release-shadow.c
blob705f5e6f433cc2637d704d9d697af232e31ee23c
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
6 #include <assert.h>
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <sys/mman.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <unistd.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;
23 void sync_rss() {
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) {
27 page[0] = 42;
28 madvise(page, kPageSize, MADV_DONTNEED);
30 munmap(page, kPageSize);
33 size_t current_rss() {
34 sync_rss();
35 int statm_fd = open("/proc/self/statm", O_RDONLY);
36 assert(statm_fd >= 0);
38 char buf[100];
39 assert(read(statm_fd, &buf, sizeof(buf)) > 0);
40 size_t size, rss;
41 assert(sscanf(buf, "%zu %zu", &size, &rss) == 2);
43 close(statm_fd);
44 return rss;
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)
54 return 0;
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;
61 int main() {
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);
77 return 0;