1// RUN: %clang_hwasan -g %s -o %t
2// RUN: not %run %t 0 2>&1 | FileCheck %s
3// RUN: not %run %t -33 2>&1 | FileCheck %s
4// REQUIRES: pointer-tagging
5
6#include <assert.h>
7#include <setjmp.h>
8#include <stdio.h>
9#include <stdlib.h>
10
11/* Testing longjmp/setjmp should test that accesses to scopes jmp'd over are
12 caught. */
13int __attribute__((noinline))
14uses_longjmp(int **other_array, int num, jmp_buf env) {
15 int internal_array[100] = {0};
16 *other_array = &internal_array[0];
17 longjmp(env: env, val: num);
18}
19
20int __attribute__((noinline)) uses_setjmp(int num) {
21 int big_array[100];
22 int *other_array = NULL;
23 sigjmp_buf cur_env;
24 int temp = 0;
25 if ((temp = sigsetjmp(cur_env, 1)) != 0) {
26 assert((num == 0 && temp == 1) || (num != 0 && temp == num));
27 // We're testing that our longjmp interceptor untagged the previous stack.
28 // Hence the tag in memory should be zero.
29 if (other_array != NULL)
30 return other_array[0];
31 // CHECK: READ of size 4 at{{.*}}tags: {{..}}/00
32 return 100;
33 } else
34 return uses_longjmp(other_array: &other_array, num, env: cur_env);
35}
36
37int __attribute__((noinline)) main(int argc, char *argv[]) {
38 assert(argc == 2);
39 int longjmp_retval = atoi(nptr: argv[1]);
40 uses_setjmp(num: longjmp_retval);
41 return 0;
42}
43

source code of compiler-rt/test/hwasan/TestCases/longjmp-setjmp-interception.c