1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include <condition_variable>
34 #include <thread>
35
36 #include <dlfcn.h>
37 #include <inttypes.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40
41 #include "../mte_utils.h"
42 #include "CHECK.h"
43
44 #if defined(__BIONIC__) && defined(__aarch64__)
45
46 enum State { kInit, kThreadStarted, kStackRemapped };
47
48 // We can't use pthread_getattr_np because that uses the rlimit rather than the actual mapping
49 // bounds.
find_main_stack_limits(uintptr_t * low,uintptr_t * high)50 static void find_main_stack_limits(uintptr_t* low, uintptr_t* high) {
51 uintptr_t startstack = reinterpret_cast<uintptr_t>(__builtin_frame_address(0));
52
53 // Hunt for the region that contains that address.
54 FILE* fp = fopen("/proc/self/maps", "re");
55 if (fp == nullptr) {
56 abort();
57 }
58 char line[BUFSIZ];
59 while (fgets(line, sizeof(line), fp) != nullptr) {
60 uintptr_t lo, hi;
61 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR, &lo, &hi) == 2) {
62 if (lo <= startstack && startstack <= hi) {
63 *low = lo;
64 *high = hi;
65 fclose(fp);
66 return;
67 }
68 }
69 }
70 abort();
71 }
72
73 template <typename Fn>
fault_new_stack_page(uintptr_t low,Fn f)74 unsigned int fault_new_stack_page(uintptr_t low, Fn f) {
75 uintptr_t new_low;
76 uintptr_t new_high;
77 volatile char buf[4096];
78 buf[4095] = 1;
79 find_main_stack_limits(&new_low, &new_high);
80 if (new_low < low) {
81 f();
82 return new_high;
83 }
84 // Useless, but should defeat TCO.
85 return new_low + fault_new_stack_page(low, f);
86 }
main(int argc,char ** argv)87 extern "C" int main(int argc, char** argv) {
88 if (argc < 2) {
89 return 1;
90 }
91 const char* path = argv[1];
92 CHECK(access(path, F_OK) == 0); // Verify test setup.
93 CHECK(!is_stack_mte_on());
94 std::mutex m;
95 std::condition_variable cv;
96 State state = kInit;
97
98 bool is_early_thread_mte_on = false;
99 std::thread early_th([&] {
100 {
101 std::lock_guard lk(m);
102 state = kThreadStarted;
103 }
104 cv.notify_one();
105 {
106 std::unique_lock lk(m);
107 cv.wait(lk, [&] { return state == kStackRemapped; });
108 }
109 is_early_thread_mte_on = is_stack_mte_on();
110 });
111 {
112 std::unique_lock lk(m);
113 cv.wait(lk, [&] { return state == kThreadStarted; });
114 }
115 void* handle = dlopen(path, RTLD_NOW);
116 {
117 std::lock_guard lk(m);
118 state = kStackRemapped;
119 }
120 cv.notify_one();
121 CHECK(handle != nullptr);
122 CHECK(is_stack_mte_on());
123
124 bool new_stack_page_mte_on = false;
125 uintptr_t low;
126 uintptr_t high;
127 find_main_stack_limits(&low, &high);
128 fault_new_stack_page(low, [&] { new_stack_page_mte_on = is_stack_mte_on(); });
129 CHECK(new_stack_page_mte_on);
130
131 bool is_late_thread_mte_on = false;
132 std::thread late_th([&] { is_late_thread_mte_on = is_stack_mte_on(); });
133 late_th.join();
134 early_th.join();
135 CHECK(is_late_thread_mte_on);
136 CHECK(is_early_thread_mte_on);
137 printf("RAN\n");
138 return 0;
139 }
140
141 #else
main(int,char **)142 extern "C" int main(int, char**) {
143 return 1;
144 }
145 #endif
146