From 18b46a81ea3ce4b4fc52ee93ba5edd9abfc309eb Mon Sep 17 00:00:00 2001 From: Thurston Dang Date: Thu, 6 Jun 2024 23:31:01 +0000 Subject: [PATCH] [dfsan] Add test case for sscanf This test case shows a limitation of DFSan's sscanf implementation (introduced in https://reviews.llvm.org/D153775): it simply ignores ordinary characters in the format string, instead of actually comparing them against the input. This may change the semantics of instrumented programs. Importantly, this also means that DFSan's release_shadow_space.c test, which relies on sscanf to scrape the RSS from /proc/maps output, will incorrectly match lines that don't contain RSS information. As a result, it is scraping numbers from irrelevant output (e.g., base addresses), and can therefore result in test flakiness (https://github.com/llvm/llvm-project/issues/91287). --- compiler-rt/test/dfsan/sscanf.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 compiler-rt/test/dfsan/sscanf.c diff --git a/compiler-rt/test/dfsan/sscanf.c b/compiler-rt/test/dfsan/sscanf.c new file mode 100644 index 0000000000000..dbc2de4ba96c1 --- /dev/null +++ b/compiler-rt/test/dfsan/sscanf.c @@ -0,0 +1,19 @@ +// RUN: %clang_dfsan %s -o %t && %run %t +// XFAIL: * + +#include +#include + +int main(int argc, char *argv[]) { + char buf[256] = "10000000000-100000000000 rw-p 00000000 00:00 0"; + long rss = 0; + // This test exposes a bug in DFSan's sscanf, that leads to flakiness + // in release_shadow_space.c (see + // https://github.com/llvm/llvm-project/issues/91287) + if (sscanf(buf, "Garbage text before, %ld, Garbage text after", &rss) == 1) { + printf("Error: matched %ld\n", rss); + return 1; + } + + return 0; +}