[hwasan] More realistic setjmp test.
authorFlorian Mayer <fmayer@google.com>
Fri, 9 Jul 2021 15:44:12 +0000 (16:44 +0100)
committerFlorian Mayer <fmayer@google.com>
Fri, 9 Jul 2021 19:27:32 +0000 (20:27 +0100)
The existing one actually failed on the int* p, not on int z (as can be
seen by the fault being 8 bytes rather than 4).

This is also needed to make sure the stack safety analysis does not
classify the alloca as safe.

Reviewed By: hctim

Differential Revision: https://reviews.llvm.org/D105705

compiler-rt/test/hwasan/TestCases/longjmp.c

index 700a1cb..86fff7a 100644 (file)
@@ -3,24 +3,34 @@
 
 // REQUIRES: stable-runtime, pointer-tagging
 
+#include <setjmp.h>
 #include <stdlib.h>
 #include <assert.h>
 #include <sanitizer/hwasan_interface.h>
+#include <unistd.h>
+
+static int *volatile p;
 
 __attribute__((noinline))
-int f(void *caller_frame) {
+int f(jmp_buf buf) {
   int z = 0;
-  int *volatile p = &z;
+  p = &z;
   // Tag of local is never zero.
   assert(__hwasan_tag_pointer(p, 0) != p);
 #ifndef NEGATIVE
-  // This will destroy shadow of "z", and the following load will crash.
-  __hwasan_handle_longjmp(caller_frame);
+  // This will destroy shadow of "z", the p[0] in main will crash.
+  longjmp(buf, 1);
 #endif
   return p[0];
 }
 
 int main() {
-  return f(__builtin_frame_address(0));
-  // CHECK: READ of size 8 at {{.*}} tags: {{.*}}/00 (ptr/mem)
+  jmp_buf buf;
+  if (setjmp(buf)) {
+    return p[0];
+  } else {
+    f(buf);
+  }
+  return 0;
+  // CHECK: READ of size 4 at {{.*}} tags: {{.*}}/00 (ptr/mem)
 }