1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <stdlib.h>
36#include <stdio.h>
37#include <unistd.h>
38#include <sys/types.h>
39#include <fcntl.h>
40#include <setjmp.h>
41#include <signal.h>
42
43int err;
44int segv_caught;
45
46#define SHOULD_NOT_CHANGE_VAL 5
47int should_not_change = SHOULD_NOT_CHANGE_VAL;
48
49#define OK_TO_CHANGE_VAL 13
50int ok_to_change = OK_TO_CHANGE_VAL;
51
52static void __check(const char *filename, int line, int x, int expect)
53{
54 if (x != expect) {
55 printf("ERROR %s:%d - %d != %d\n",
56 filename, line, x, expect);
57 err++;
58 }
59}
60
61#define check(x, expect) __check(__FILE__, __LINE__, (x), (expect))
62
63static void __chk_error(const char *filename, int line, int ret)
64{
65 if (ret < 0) {
66 printf("ERROR %s:%d - %d\n", filename, line, ret);
67 err++;
68 }
69}
70
71#define chk_error(ret) __chk_error(__FILE__, __LINE__, (ret))
72
73jmp_buf jmp_env;
74
75static void sig_segv(int sig, siginfo_t *info, void *puc)
76{
77 check(sig, SIGSEGV);
78 segv_caught = 1;
79 longjmp(jmp_env, 1);
80}
81
82int main()
83{
84 struct sigaction act;
85 int dummy32;
86 long long dummy64;
87 void *p;
88
89
90 act.sa_sigaction = sig_segv;
91 sigemptyset(&act.sa_mask);
92 act.sa_flags = SA_SIGINFO;
93 chk_error(sigaction(SIGSEGV, &act, NULL));
94 if (setjmp(jmp_env) == 0) {
95 asm volatile("r18 = ##should_not_change\n\t"
96 "r19 = #0\n\t"
97 "{\n\t"
98 " memw(r18) = #7\n\t"
99 " %0 = memw(r19)\n\t"
100 "}:mem_noshuf\n\t"
101 : "=r"(dummy32) : : "r18", "r19", "memory");
102 }
103
104 act.sa_handler = SIG_DFL;
105 sigemptyset(&act.sa_mask);
106 act.sa_flags = 0;
107 chk_error(sigaction(SIGSEGV, &act, NULL));
108
109 check(segv_caught, 1);
110 check(should_not_change, SHOULD_NOT_CHANGE_VAL);
111
112
113
114
115
116 asm volatile("r18 = ##ok_to_change\n\t"
117 "r19 = #0\n\t"
118 "p0 = cmp.gt(r0, r0)\n\t"
119 "{\n\t"
120 " memw(r18) = #7\n\t"
121 " if (p0) %0 = memw(r19)\n\t"
122 "}:mem_noshuf\n\t"
123 : "=r"(dummy32) : : "r18", "r19", "p0", "memory");
124
125 check(ok_to_change, 7);
126
127
128
129
130
131 ok_to_change = OK_TO_CHANGE_VAL;
132 p = NULL;
133 asm volatile("r18 = ##ok_to_change\n\t"
134 "p0 = cmp.gt(r0, r0)\n\t"
135 "{\n\t"
136 " memw(r18) = #9\n\t"
137 " if (p0) %1 = memd(%0 ++ #8)\n\t"
138 "}:mem_noshuf\n\t"
139 : "+r"(p), "=r"(dummy64) : : "r18", "p0", "memory");
140
141 check(ok_to_change, 9);
142 check((int)p, (int)NULL);
143
144 puts(err ? "FAIL" : "PASS");
145 return err ? EXIT_FAILURE : EXIT_SUCCESS;
146}
147