2 * Common code for arch-specific MMU_INST_FETCH fault testing.
15 #include <sys/ucontext.h>
17 /* Forward declarations. */
19 static void *arch_mcontext_pc(const mcontext_t *ctx);
20 static int arch_mcontext_arg(const mcontext_t *ctx);
21 static void arch_flush(void *p, int len);
23 /* Testing infrastructure. */
27 const char *test_code;
36 static void *page_base;
38 static const struct noexec_test *current_noexec_test;
40 static void handle_err(const char *syscall)
42 printf("[ FAILED ] %s: %s\n", syscall, strerror(errno));
46 static void handle_segv(int sig, siginfo_t *info, void *ucontext)
48 const struct noexec_test *test = current_noexec_test;
49 const mcontext_t *mc = &((ucontext_t *)ucontext)->uc_mcontext;
56 printf("[ FAILED ] unexpected SEGV\n");
59 current_noexec_test = NULL;
61 expected_si = page_base + test->expected_si_ofs;
62 if (info->si_addr != expected_si) {
63 printf("[ FAILED ] wrong si_addr (%p != %p)\n",
64 info->si_addr, expected_si);
68 pc = arch_mcontext_pc(mc);
69 expected_pc = page_base + test->expected_pc_ofs;
70 if (pc != expected_pc) {
71 printf("[ FAILED ] wrong pc (%p != %p)\n", pc, expected_pc);
75 arg = arch_mcontext_arg(mc);
76 if (arg != test->expected_arg) {
77 printf("[ FAILED ] wrong arg (%d != %d)\n", arg, test->expected_arg);
81 if (mprotect(page_base, page_size,
82 PROT_READ | PROT_WRITE | PROT_EXEC) < 0) {
83 handle_err("mprotect");
87 static void test_noexec_1(const struct noexec_test *test)
89 void *start = page_base + test->page_ofs;
90 void (*fn)(int arg) = page_base + test->entry_ofs;
92 memcpy(start, test->test_code, test->test_len);
93 arch_flush(start, test->test_len);
95 /* Trigger TB creation in order to test invalidation. */
98 if (mprotect(page_base, page_size, PROT_NONE) < 0) {
99 handle_err("mprotect");
102 /* Trigger SEGV and check that handle_segv() ran. */
103 current_noexec_test = test;
105 assert(current_noexec_test == NULL);
108 static int test_noexec(struct noexec_test *tests, size_t n_tests)
110 struct sigaction act;
113 memset(&act, 0, sizeof(act));
114 act.sa_sigaction = handle_segv;
115 act.sa_flags = SA_SIGINFO;
116 if (sigaction(SIGSEGV, &act, NULL) < 0) {
117 handle_err("sigaction");
120 page_size = getpagesize();
121 page_base = mmap(NULL, 2 * page_size,
122 PROT_READ | PROT_WRITE | PROT_EXEC,
123 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
124 if (page_base == MAP_FAILED) {
127 page_base += page_size;
129 for (i = 0; i < n_tests; i++) {
130 struct noexec_test *test = &tests[i];
132 printf("[ RUN ] %s\n", test->name);
137 printf("[ PASSED ]\n");