2022-03-10 18:58:17 -08:00
|
|
|
// DFSAN_OPTIONS=no_huge_pages_for_shadow=false RUN: %clang_dfsan %s -o %t && %run %t
|
|
|
|
// DFSAN_OPTIONS=no_huge_pages_for_shadow=true RUN: %clang_dfsan %s -o %t && %run %t
|
|
|
|
// DFSAN_OPTIONS=no_huge_pages_for_shadow=false RUN: %clang_dfsan %s -DORIGIN_TRACKING -mllvm -dfsan-track-origins=1 -o %t && %run %t
|
|
|
|
// DFSAN_OPTIONS=no_huge_pages_for_shadow=true RUN: %clang_dfsan %s -DORIGIN_TRACKING -mllvm -dfsan-track-origins=1 -o %t && %run %t
|
2020-10-05 16:56:50 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <sanitizer/dfsan_interface.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
size_t get_rss_kb() {
|
|
|
|
size_t ret = 0;
|
|
|
|
pid_t pid = getpid();
|
|
|
|
|
|
|
|
char fname[256];
|
|
|
|
sprintf(fname, "/proc/%ld/task/%ld/smaps", (long)pid, (long)pid);
|
|
|
|
FILE *f = fopen(fname, "r");
|
|
|
|
assert(f);
|
|
|
|
|
|
|
|
char buf[256];
|
|
|
|
while (fgets(buf, sizeof(buf), f) != NULL) {
|
|
|
|
int64_t rss;
|
2024-06-07 21:10:13 -07:00
|
|
|
// DFSan's sscanf is broken and doesn't check for ordinary characters in
|
|
|
|
// the format string, hence we use strstr as a secondary check
|
|
|
|
// (https://github.com/llvm/llvm-project/issues/94769).
|
|
|
|
if ((sscanf(buf, "Rss: %ld kB", &rss) == 1) &&
|
|
|
|
(strstr(buf, "Rss: ") != NULL))
|
2020-10-05 16:56:50 +00:00
|
|
|
ret += rss;
|
|
|
|
}
|
|
|
|
assert(feof(f));
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
const size_t map_size = 100 << 20;
|
|
|
|
size_t before = get_rss_kb();
|
|
|
|
|
|
|
|
// mmap and touch all addresses. The overhead is 1x.
|
|
|
|
char *p = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
|
|
memset(p, 0xff, map_size);
|
|
|
|
size_t after_mmap = get_rss_kb();
|
|
|
|
|
|
|
|
// store labels to all addresses. The overhead is 2x.
|
2021-06-04 15:34:02 -07:00
|
|
|
const dfsan_label label = 8;
|
2020-10-05 16:56:50 +00:00
|
|
|
char val = 0xff;
|
|
|
|
dfsan_set_label(label, &val, sizeof(val));
|
|
|
|
memset(p, val, map_size);
|
|
|
|
size_t after_mmap_and_set_label = get_rss_kb();
|
|
|
|
|
|
|
|
// fixed-mmap the same address. OS recyles pages and reinitializes data at the
|
|
|
|
// address. This should be the same to calling munmap.
|
|
|
|
p = mmap(p, map_size, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
|
|
|
|
size_t after_fixed_mmap = get_rss_kb();
|
|
|
|
|
|
|
|
// store labels to all addresses.
|
|
|
|
memset(p, val, map_size);
|
|
|
|
size_t after_mmap_and_set_label2 = get_rss_kb();
|
|
|
|
|
|
|
|
// munmap the addresses.
|
|
|
|
munmap(p, map_size);
|
|
|
|
size_t after_munmap = get_rss_kb();
|
|
|
|
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"RSS at start: %zu, after mmap: %zu, after mmap+set label: %zu, after "
|
|
|
|
"fixed map: %zu, after another mmap+set label: %zu, after munmap: %zu\n",
|
|
|
|
before, after_mmap, after_mmap_and_set_label, after_fixed_mmap,
|
|
|
|
after_mmap_and_set_label2, after_munmap);
|
|
|
|
|
2024-06-07 21:10:13 -07:00
|
|
|
// This is orders of magnitude larger than we expect (typically < 10,000KB).
|
|
|
|
// It is a quick check to ensure that the RSS calculation function isn't
|
|
|
|
// egregriously wrong.
|
|
|
|
assert(before < 1000000);
|
|
|
|
|
2020-10-05 16:56:50 +00:00
|
|
|
const size_t mmap_cost_kb = map_size >> 10;
|
[DFSan] Fix flakey release_shadow_space.c accounting for Origin chains.
Test sometimes fails on buildbot (after two non-Origins executions):
/usr/bin/ld: warning: Cannot export local symbol 'dfsan_flush'
RSS at start: 4620, after mmap: 107020, after mmap+set label: 209424, after fixed map: 4624, after another mmap+set label: 209424, after munmap: 4624
/usr/bin/ld: warning: Cannot export local symbol 'dfsan_flush'
RSS at start: 4620, after mmap: 107020, after mmap+set label: 209424, after fixed map: 4624, after another mmap+set label: 209424, after munmap: 4624
/usr/bin/ld: warning: Cannot export local symbol 'dfsan_flush'
RSS at start: 4620, after mmap: 107020, after mmap+set label: 317992, after fixed map: 10792, after another mmap+set label: 317992, after munmap: 10792
release_shadow_space.c.tmp: /b/sanitizer-x86_64-linux/build/llvm-project/compiler-rt/test/dfsan/release_shadow_space.c:91: int main(int, char **): Assertion `after_fixed_mmap <= before + delta' failed.
Reviewed By: vitalybuka
Differential Revision: https://reviews.llvm.org/D111522
2021-10-11 00:28:47 -07:00
|
|
|
// Shadow space (1:1 with application memory)
|
2021-04-15 16:49:54 -07:00
|
|
|
const size_t mmap_shadow_cost_kb = sizeof(dfsan_label) * mmap_cost_kb;
|
2021-05-05 00:53:51 +00:00
|
|
|
#ifdef ORIGIN_TRACKING
|
[DFSan] Fix flakey release_shadow_space.c accounting for Origin chains.
Test sometimes fails on buildbot (after two non-Origins executions):
/usr/bin/ld: warning: Cannot export local symbol 'dfsan_flush'
RSS at start: 4620, after mmap: 107020, after mmap+set label: 209424, after fixed map: 4624, after another mmap+set label: 209424, after munmap: 4624
/usr/bin/ld: warning: Cannot export local symbol 'dfsan_flush'
RSS at start: 4620, after mmap: 107020, after mmap+set label: 209424, after fixed map: 4624, after another mmap+set label: 209424, after munmap: 4624
/usr/bin/ld: warning: Cannot export local symbol 'dfsan_flush'
RSS at start: 4620, after mmap: 107020, after mmap+set label: 317992, after fixed map: 10792, after another mmap+set label: 317992, after munmap: 10792
release_shadow_space.c.tmp: /b/sanitizer-x86_64-linux/build/llvm-project/compiler-rt/test/dfsan/release_shadow_space.c:91: int main(int, char **): Assertion `after_fixed_mmap <= before + delta' failed.
Reviewed By: vitalybuka
Differential Revision: https://reviews.llvm.org/D111522
2021-10-11 00:28:47 -07:00
|
|
|
// Origin space (1:1 with application memory)
|
2021-05-05 00:53:51 +00:00
|
|
|
const size_t mmap_origin_cost_kb = mmap_cost_kb;
|
|
|
|
#else
|
|
|
|
const size_t mmap_origin_cost_kb = 0;
|
|
|
|
#endif
|
2020-10-05 16:56:50 +00:00
|
|
|
assert(after_mmap >= before + mmap_cost_kb);
|
2021-05-05 00:53:51 +00:00
|
|
|
assert(after_mmap_and_set_label >=
|
|
|
|
after_mmap + mmap_shadow_cost_kb + mmap_origin_cost_kb);
|
|
|
|
assert(after_mmap_and_set_label2 >=
|
|
|
|
before + mmap_cost_kb + mmap_shadow_cost_kb + mmap_origin_cost_kb);
|
2020-10-05 16:56:50 +00:00
|
|
|
|
[DFSan] Fix flakey release_shadow_space.c accounting for Origin chains.
Test sometimes fails on buildbot (after two non-Origins executions):
/usr/bin/ld: warning: Cannot export local symbol 'dfsan_flush'
RSS at start: 4620, after mmap: 107020, after mmap+set label: 209424, after fixed map: 4624, after another mmap+set label: 209424, after munmap: 4624
/usr/bin/ld: warning: Cannot export local symbol 'dfsan_flush'
RSS at start: 4620, after mmap: 107020, after mmap+set label: 209424, after fixed map: 4624, after another mmap+set label: 209424, after munmap: 4624
/usr/bin/ld: warning: Cannot export local symbol 'dfsan_flush'
RSS at start: 4620, after mmap: 107020, after mmap+set label: 317992, after fixed map: 10792, after another mmap+set label: 317992, after munmap: 10792
release_shadow_space.c.tmp: /b/sanitizer-x86_64-linux/build/llvm-project/compiler-rt/test/dfsan/release_shadow_space.c:91: int main(int, char **): Assertion `after_fixed_mmap <= before + delta' failed.
Reviewed By: vitalybuka
Differential Revision: https://reviews.llvm.org/D111522
2021-10-11 00:28:47 -07:00
|
|
|
#ifdef ORIGIN_TRACKING
|
|
|
|
// This value is chosen based on observed difference.
|
|
|
|
const size_t mmap_origin_chain_kb = 4000;
|
|
|
|
#else
|
|
|
|
const size_t mmap_origin_chain_kb = 0;
|
|
|
|
#endif
|
|
|
|
|
2020-10-05 16:56:50 +00:00
|
|
|
// RSS may not change memory amount after munmap to the same level as the
|
|
|
|
// start of the program. The assert checks the memory up to a delta.
|
|
|
|
const size_t delta = 5000;
|
[DFSan] Fix flakey release_shadow_space.c accounting for Origin chains.
Test sometimes fails on buildbot (after two non-Origins executions):
/usr/bin/ld: warning: Cannot export local symbol 'dfsan_flush'
RSS at start: 4620, after mmap: 107020, after mmap+set label: 209424, after fixed map: 4624, after another mmap+set label: 209424, after munmap: 4624
/usr/bin/ld: warning: Cannot export local symbol 'dfsan_flush'
RSS at start: 4620, after mmap: 107020, after mmap+set label: 209424, after fixed map: 4624, after another mmap+set label: 209424, after munmap: 4624
/usr/bin/ld: warning: Cannot export local symbol 'dfsan_flush'
RSS at start: 4620, after mmap: 107020, after mmap+set label: 317992, after fixed map: 10792, after another mmap+set label: 317992, after munmap: 10792
release_shadow_space.c.tmp: /b/sanitizer-x86_64-linux/build/llvm-project/compiler-rt/test/dfsan/release_shadow_space.c:91: int main(int, char **): Assertion `after_fixed_mmap <= before + delta' failed.
Reviewed By: vitalybuka
Differential Revision: https://reviews.llvm.org/D111522
2021-10-11 00:28:47 -07:00
|
|
|
// Origin chains are not freed, even when the origin space which refers to
|
|
|
|
// them is freed, so mmap_origin_chain_kb is added to account for this.
|
|
|
|
assert(after_fixed_mmap <= before + delta + mmap_origin_chain_kb);
|
|
|
|
assert(after_munmap <= before + delta + mmap_origin_chain_kb);
|
2020-10-05 16:56:50 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|