Skip to content

Commit 221336c

Browse files
authored
[dfsan] Fix release_shadow_space.c (#94770)
DFSan's sscanf is incorrect (#94769), which results in erroneous matches when scraping RSS from /proc/maps. This patch works around the issue by using strstr as a secondary check. It also adds a loose validity check for the initial RSS measurement, to guard against regressions in get_rss_kb(). Fixes #91287
1 parent c0a1214 commit 221336c

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

compiler-rt/test/dfsan/release_shadow_space.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
// DFSAN_OPTIONS=no_huge_pages_for_shadow=false RUN: %clang_dfsan %s -DORIGIN_TRACKING -mllvm -dfsan-track-origins=1 -o %t && %run %t
44
// DFSAN_OPTIONS=no_huge_pages_for_shadow=true RUN: %clang_dfsan %s -DORIGIN_TRACKING -mllvm -dfsan-track-origins=1 -o %t && %run %t
55

6-
// This test is flaky right now: https://github.com/llvm/llvm-project/issues/91287
7-
// UNSUPPORTED: target={{.*}}
8-
96
#include <assert.h>
107
#include <sanitizer/dfsan_interface.h>
118
#include <stdbool.h>
@@ -26,7 +23,11 @@ size_t get_rss_kb() {
2623
char buf[256];
2724
while (fgets(buf, sizeof(buf), f) != NULL) {
2825
int64_t rss;
29-
if (sscanf(buf, "Rss: %ld kB", &rss) == 1)
26+
// DFSan's sscanf is broken and doesn't check for ordinary characters in
27+
// the format string, hence we use strstr as a secondary check
28+
// (https://github.com/llvm/llvm-project/issues/94769).
29+
if ((sscanf(buf, "Rss: %ld kB", &rss) == 1) &&
30+
(strstr(buf, "Rss: ") != NULL))
3031
ret += rss;
3132
}
3233
assert(feof(f));
@@ -73,6 +74,11 @@ int main(int argc, char **argv) {
7374
before, after_mmap, after_mmap_and_set_label, after_fixed_mmap,
7475
after_mmap_and_set_label2, after_munmap);
7576

77+
// This is orders of magnitude larger than we expect (typically < 10,000KB).
78+
// It is a quick check to ensure that the RSS calculation function isn't
79+
// egregriously wrong.
80+
assert(before < 1000000);
81+
7682
const size_t mmap_cost_kb = map_size >> 10;
7783
// Shadow space (1:1 with application memory)
7884
const size_t mmap_shadow_cost_kb = sizeof(dfsan_label) * mmap_cost_kb;

0 commit comments

Comments
 (0)