[dfsan] Fix release_shadow_space.c (#94770)

DFSan's sscanf is incorrect
(https://github.com/llvm/llvm-project/issues/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 https://github.com/llvm/llvm-project/issues/91287
This commit is contained in:
Thurston Dang
2024-06-07 21:10:13 -07:00
committed by GitHub
parent c0a1214c0c
commit 221336c8e2

View File

@@ -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;