Skip to content

[TySan] Added tests for methods of ignoring instrumentation #124125

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
Jan 24, 2025

Conversation

gbMattN
Copy link
Contributor

@gbMattN gbMattN commented Jan 23, 2025

TySan supports some preprocessor checks and ignorelists, but they are currently untested. This PR adds some tests to make sure they all work.

@fhahn @AaronBallman, this is based off the discussion in the documentation PR [#123595]

@llvmbot
Copy link
Member

llvmbot commented Jan 23, 2025

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

Author: None (gbMattN)

Changes

TySan supports some preprocessor checks and ignorelists, but they are currently untested. This PR adds some tests to make sure they all work.

@fhahn @AaronBallman, this is based off the discussion in the documentation PR [#123595]


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

3 Files Affected:

  • (added) compiler-rt/test/tysan/ignorelist.c (+35)
  • (added) compiler-rt/test/tysan/ignorelist.h (+7)
  • (added) compiler-rt/test/tysan/preprocessor.c (+30)
diff --git a/compiler-rt/test/tysan/ignorelist.c b/compiler-rt/test/tysan/ignorelist.c
new file mode 100644
index 00000000000000..6f6039c47318cf
--- /dev/null
+++ b/compiler-rt/test/tysan/ignorelist.c
@@ -0,0 +1,35 @@
+// RUN: %clang_tysan %s -o %t && %run %t 10 >%t.out.0 2>&1
+// RUN: FileCheck --check-prefixes=CHECK,CHECK-BOTH %s < %t.out.0
+// RUN: echo "fun:typeViolationignored" > %tmp
+// RUN: echo "src:*ignorelist.h" > %tmp
+// RUN: %clang_tysan -fsanitize-ignorelist=%tmp %s -o %t && %run %t 10 >%t.out 2>&1
+// RUN: FileCheck --check-prefixes=CHECK-IGNORELIST,CHECK-BOTH %s < %t.out
+
+#include "ignorelist.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+void typeViolationIgnored(float *fPtr) { printf("As int: %d\n", *(int *)fPtr); }
+
+void typeViolation(int *fPtr) { printf("As float: %f\n", *(float *)fPtr); }
+
+int main() {
+  float *f = (float *)malloc(sizeof(float));
+  *f = 413.0f;
+  typeViolationIgnored(f);
+  // CHECK: TypeSanitizer: type-aliasing-violation on address 0x{{.*}}
+  // CHECK-NEXT: READ of size 4 at 0x{{.*}} with type int accesses an existing object of type float
+  // CHECK-IGNORELIST-NOT: TypeSanitizer: type-aliasing-violation on address 0x{{.*}}
+
+  int *i = (int *)malloc(sizeof(int));
+  *i = 612;
+  typeViolation(i);
+  // CHECK-BOTH: TypeSanitizer: type-aliasing-violation on address 0x{{.*}}
+  // CHECK-BOTH: READ of size 4 at 0x{{.*}} with type float accesses an existing object of type int
+
+  typeViolationMultiFile((void *)i);
+  // CHECK: TypeSanitizer: type-aliasing-violation on address 0x{{.*}}
+  // CHECK-IGNORELIST-NOT: TypeSanitizer: type-aliasing-violation on address 0x{{.*}}
+
+  return 0;
+}
diff --git a/compiler-rt/test/tysan/ignorelist.h b/compiler-rt/test/tysan/ignorelist.h
new file mode 100644
index 00000000000000..bc0cde711fd76f
--- /dev/null
+++ b/compiler-rt/test/tysan/ignorelist.h
@@ -0,0 +1,7 @@
+// Used as part of the ignorelist.c test
+// tests if the "src:" ignorelist directive works
+#include <stdio.h>
+
+void typeViolationMultiFile(void *value) {
+  printf("As long: %ld\n", *(long *)value);
+}
diff --git a/compiler-rt/test/tysan/preprocessor.c b/compiler-rt/test/tysan/preprocessor.c
new file mode 100644
index 00000000000000..b8b87f4e7ef297
--- /dev/null
+++ b/compiler-rt/test/tysan/preprocessor.c
@@ -0,0 +1,30 @@
+// RUN: %clang_tysan -O0 %s -o %t && %run %t >%t.out 2>&1 && FileCheck --check-prefix=CHECK-SANITIZED %s < %t.out
+// RUN: %clang_tysan -DNOSAN -O0 %s -o %t && %run %t >%t.out 2>&1 && FileCheck --check-prefix=CHECK-NOSAN %s < %t.out
+// RUN: %clang -O0 %s -o %t && %run %t >%t.out 2>&1 && FileCheck --check-prefix=CHECK-SIMPLE %s < %t.out
+
+#include <stdio.h>
+
+#if __has_feature(type_sanitizer)
+
+#  ifdef NOSAN
+__attribute__((no_sanitize("type")))
+#  endif
+int main(){
+
+  int value = 42;
+  printf("As float: %f\n", *(float *)&value);
+  // CHECK-SANITIZED: ERROR: TypeSanitizer
+  // CHECK-NOSAN-NOT: ERROR: TypeSanitizer
+
+  return 0;
+}
+
+#else
+
+int main() {
+  printf("Nothing interesting here\n");
+  return 0;
+}
+// CHECK-SIMPLE: Nothing interesting here
+
+#endif

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you for the improved test coverage!

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for taking care of this!

@goussepi goussepi merged commit 1c0af8d into llvm:main Jan 24, 2025
10 checks passed
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.

5 participants