Skip to content

[dfsan] Fix release_shadow_space.c #94770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2024

Conversation

thurstond
Copy link
Contributor

@thurstond thurstond commented Jun 7, 2024

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

DFSan's sscanf is incorrect (llvm#94769), which
results in erroneous matches when scraping RSS /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, to guard
against future regressions in get_rss_kb().

Fixes llvm#91287
@llvmbot
Copy link
Member

llvmbot commented Jun 7, 2024

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Thurston Dang (thurstond)

Changes

DFSan's sscanf is incorrect (#94769), which results in erroneous matches when scraping RSS /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, to guard against future regressions in get_rss_kb().

Fixes #91287


Full diff: https://github.com/llvm/llvm-project/pull/94770.diff

1 Files Affected:

  • (modified) compiler-rt/test/dfsan/release_shadow_space.c (+10-4)
diff --git a/compiler-rt/test/dfsan/release_shadow_space.c b/compiler-rt/test/dfsan/release_shadow_space.c
index 60dec98ebec49..0f0e1a9c731cb 100644
--- a/compiler-rt/test/dfsan/release_shadow_space.c
+++ b/compiler-rt/test/dfsan/release_shadow_space.c
@@ -3,9 +3,6 @@
 // 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
 
-// This test is flaky right now: https://github.com/llvm/llvm-project/issues/91287
-// UNSUPPORTED:  target={{.*}}
-
 #include <assert.h>
 #include <sanitizer/dfsan_interface.h>
 #include <stdbool.h>
@@ -26,7 +23,11 @@ size_t get_rss_kb() {
   char buf[256];
   while (fgets(buf, sizeof(buf), f) != NULL) {
     int64_t rss;
-    if (sscanf(buf, "Rss: %ld kB", &rss) == 1)
+    // 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))
       ret += rss;
   }
   assert(feof(f));
@@ -73,6 +74,11 @@ int main(int argc, char **argv) {
       before, after_mmap, after_mmap_and_set_label, after_fixed_mmap,
       after_mmap_and_set_label2, after_munmap);
 
+  // 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);
+
   const size_t mmap_cost_kb = map_size >> 10;
   // Shadow space (1:1 with application memory)
   const size_t mmap_shadow_cost_kb = sizeof(dfsan_label) * mmap_cost_kb;

@thurstond thurstond merged commit 221336c into llvm:main Jun 8, 2024
9 checks passed
nekoshirro pushed a commit to nekoshirro/Alchemist-LLVM that referenced this pull request Jun 9, 2024
DFSan's sscanf is incorrect
(llvm#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 llvm#91287

Signed-off-by: Hafidz Muzakky <[email protected]>
@HerrCai0907 HerrCai0907 mentioned this pull request Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DFSAN release_shadow_space.c is flaky
3 participants