1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <signal.h>
18 #include <stdio.h>
19 #include <sys/auxv.h>
20 #include <sys/cdefs.h>
21 #include <sys/mman.h>
22 #include <unistd.h>
23 #include <memory>
24
action(int signo,siginfo_t * info __unused,void *)25 void action(int signo, siginfo_t* info __unused, void*) {
26 #ifdef __ANDROID__
27 if (signo == 11 && info->si_code == SEGV_MTEAERR) {
28 fprintf(stderr, "SEGV_MTEAERR\n");
29 _exit(0);
30 }
31
32 if (signo == 11 && info->si_code == SEGV_MTESERR) {
33 fprintf(stderr, "SEGV_MTESERR\n");
34 _exit(0);
35 }
36 #endif
37
38 fprintf(stderr, "signo %d\n", signo);
39 _exit(0);
40 }
41
action2(int signo,siginfo_t * info __unused,void *)42 void action2(int signo, siginfo_t* info __unused, void*) {
43 fprintf(stderr, "unexpected signal %d\n", signo);
44 _exit(0);
45 }
46
main()47 __attribute__((optnone)) int main() {
48 struct sigaction sa = {};
49 sa.sa_sigaction = action;
50 sa.sa_flags = SA_SIGINFO;
51 sigaction(SIGSEGV, &sa, nullptr);
52 // suppress HWASan crash in logcat / tombstone.
53 struct sigaction dfl_sa = {};
54 dfl_sa.sa_handler = SIG_DFL;
55 sigaction(SIGABRT, &dfl_sa, nullptr);
56
57 std::unique_ptr<int[]> p = std::make_unique<int[]>(4);
58 volatile int oob = p[-1];
59 (void)oob;
60
61 #if defined(__BIONIC__) && defined(__aarch64__)
62 // If we get here, bad access on system heap memory did not trigger a fault.
63 // This suggests that MTE is disabled. Make sure that explicitly tagged PROT_MTE memory does not
64 // trigger a fault either.
65 if (getauxval(AT_HWCAP2) & HWCAP2_MTE) {
66 sa.sa_sigaction = action2;
67 sigaction(SIGSEGV, &sa, nullptr);
68
69 size_t page_size = static_cast<size_t>(sysconf(_SC_PAGESIZE));
70 void* p = mmap(nullptr, page_size, PROT_READ | PROT_WRITE | PROT_MTE,
71 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
72 if (!p) {
73 fprintf(stderr, "mmap failed\n");
74 return 1;
75 }
76
77 void* q = p;
78 __asm__ __volatile__(
79 ".arch_extension memtag\n"
80 "irg %[Ptr], %[Ptr], xzr\n"
81 "stg %[Ptr], [%[Ptr]]\n"
82 "addg %[Ptr], %[Ptr], 0, 1\n"
83 "str xzr, [%[Ptr]]\n"
84 : [Ptr] "+&r"(q)
85 :
86 : "memory");
87
88 munmap(p, page_size);
89 }
90 #endif // __aarch64__
91
92 fprintf(stderr, "normal exit\n");
93 return 0;
94 }
95