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
3
4// REQUIRES: pointer-tagging
5
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>
14
15#include <sanitizer/hwasan_interface.h>
16
17const unsigned char kTag = 42;
18const size_t kNumShadowPages = 1024;
19const size_t kNumPages = 16 * kNumShadowPages;
20const size_t kPageSize = 4096;
21const size_t kMapSize = kNumPages * kPageSize;
22
23void sync_rss() {
24 char *page = (char *)mmap(addr: 0, len: kPageSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, fd: 0, offset: 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(addr: page, len: kPageSize, MADV_DONTNEED);
29 }
30 munmap(addr: page, len: kPageSize);
31}
32
33size_t current_rss() {
34 sync_rss();
35 int statm_fd = open(file: "/proc/self/statm", O_RDONLY);
36 assert(statm_fd >= 0);
37
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);
42
43 close(fd: statm_fd);
44 return rss;
45}
46
47int test_rss_difference(void *p) {
48 __hwasan_tag_memory(p, tag: kTag, size: kMapSize);
49 size_t rss_before = current_rss();
50 __hwasan_tag_memory(p, tag: 0, size: kMapSize);
51 size_t rss_after = current_rss();
52 fprintf(stderr, format: "%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, format: "diff %zu\n", diff);
57 // Check that the difference is at least close to kNumShadowPages.
58 return diff > kNumShadowPages / 2;
59}
60
61int main() {
62 fprintf(stderr, format: "starting rss %zu\n", current_rss());
63 fprintf(stderr, format: "shadow pages: %zu\n", kNumShadowPages);
64
65 void *p = mmap(addr: 0, len: kMapSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, fd: 0, offset: 0);
66 fprintf(stderr, format: "p = %p\n", p);
67
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);
72
73 fprintf(stderr, format: "p = %p\n", p);
74 fprintf(stderr, format: "passed %zu out of %zu\n", success_count, total_count);
75 assert(success_count > total_count * 0.8);
76
77 return 0;
78}
79

source code of compiler-rt/test/hwasan/TestCases/Linux/release-shadow.c