blob: 8b12bec86ded2620a8054dd5d483a43b7f33f203 [file] [log] [blame]
Mitch Phillipse6997d52020-11-30 15:04:14 -08001/*
2 * Copyright (C) 2021 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 <gtest/gtest.h>
30#include <stdio.h>
Mitch Phillips9634c362022-06-23 11:07:00 -070031#include <sys/file.h>
Mitch Phillipse6997d52020-11-30 15:04:14 -080032#include <string>
33
34#if defined(__BIONIC__)
35
Mitch Phillips9634c362022-06-23 11:07:00 -070036#include "android-base/file.h"
Christopher Ferris8ab38e22023-02-06 18:57:18 -080037#include "android-base/test_utils.h"
Mitch Phillipse6997d52020-11-30 15:04:14 -080038#include "gwp_asan/options.h"
39#include "platform/bionic/malloc.h"
Mitch Phillips9634c362022-06-23 11:07:00 -070040#include "sys/system_properties.h"
Mitch Phillipse6997d52020-11-30 15:04:14 -080041#include "utils.h"
42
Mitch Phillips9634c362022-06-23 11:07:00 -070043// basename is a mess, use gnu basename explicitly to avoid the need for string
44// mutation.
45extern "C" const char* __gnu_basename(const char* path);
Mitch Phillipse6997d52020-11-30 15:04:14 -080046
Mitch Phillips1f3c8d62022-07-11 09:31:41 -070047// GWP-ASan tests can run much slower, especially when combined with HWASan.
48// Triple the deadline to avoid flakes (b/238585984).
49extern "C" bool GetInitialArgs(const char*** args, size_t* num_args) {
50 static const char* initial_args[] = {"--deadline_threshold_ms=270000"};
51 *args = initial_args;
52 *num_args = 1;
53 return true;
54}
55
Mitch Phillipse6997d52020-11-30 15:04:14 -080056// This file implements "torture testing" under GWP-ASan, where we sample every
57// single allocation. The upper limit for the number of GWP-ASan allocations in
58// the torture mode is is generally 40,000, so that svelte devices don't
59// explode, as this uses ~163MiB RAM (4KiB per live allocation).
60TEST(gwp_asan_integration, malloc_tests_under_torture) {
Christopher Ferris92a4d2b2023-03-02 21:45:31 +000061 // Do not override HWASan with GWP ASan.
62 SKIP_WITH_HWASAN;
63
64 RunGwpAsanTest("malloc.*:-malloc.mallinfo*");
Mitch Phillipse6997d52020-11-30 15:04:14 -080065}
66
Mitch Phillips9634c362022-06-23 11:07:00 -070067class SyspropRestorer {
68 private:
69 std::vector<std::pair<std::string, std::string>> props_to_restore_;
70 // System properties are global for a device, so the tests that mutate the
71 // GWP-ASan system properties must be run mutually exclusive. Because
72 // bionic-unit-tests is run in an isolated gtest fashion (each test is run in
73 // its own process), we have to use flocks to synchronise between tests.
74 int flock_fd_;
75
76 public:
77 SyspropRestorer() {
78 std::string path = testing::internal::GetArgvs()[0];
79 flock_fd_ = open(path.c_str(), O_RDONLY);
80 EXPECT_NE(flock_fd_, -1) << "failed to open self for a flock";
81 EXPECT_NE(flock(flock_fd_, LOCK_EX), -1) << "failed to flock myself";
82
83 const char* basename = __gnu_basename(path.c_str());
84 std::vector<std::string> props = {
85 std::string("libc.debug.gwp_asan.sample_rate.") + basename,
86 std::string("libc.debug.gwp_asan.process_sampling.") + basename,
87 std::string("libc.debug.gwp_asan.max_allocs.") + basename,
88 "libc.debug.gwp_asan.sample_rate.system_default",
89 "libc.debug.gwp_asan.sample_rate.app_default",
90 "libc.debug.gwp_asan.process_sampling.system_default",
91 "libc.debug.gwp_asan.process_sampling.app_default",
92 "libc.debug.gwp_asan.max_allocs.system_default",
93 "libc.debug.gwp_asan.max_allocs.app_default",
94 };
95
96 size_t base_props_size = props.size();
97 for (size_t i = 0; i < base_props_size; ++i) {
98 props.push_back("persist." + props[i]);
99 }
100
101 std::string reset_log;
102
103 for (const std::string& prop : props) {
104 std::string value = GetSysprop(prop);
105 props_to_restore_.emplace_back(prop, value);
106 if (!value.empty()) {
107 __system_property_set(prop.c_str(), "");
108 }
109 }
110 }
111
112 ~SyspropRestorer() {
113 for (const auto& kv : props_to_restore_) {
114 if (kv.second != GetSysprop(kv.first)) {
115 __system_property_set(kv.first.c_str(), kv.second.c_str());
116 }
117 }
118 close(flock_fd_);
119 }
120
121 static std::string GetSysprop(const std::string& name) {
122 std::string value;
123 const prop_info* pi = __system_property_find(name.c_str());
124 if (pi == nullptr) return value;
125 __system_property_read_callback(
126 pi,
127 [](void* cookie, const char* /* name */, const char* value, uint32_t /* serial */) {
128 std::string* v = static_cast<std::string*>(cookie);
129 *v = value;
130 },
131 &value);
132 return value;
133 }
134};
135
136TEST(gwp_asan_integration, DISABLED_assert_gwp_asan_enabled) {
137 std::string maps;
138 EXPECT_TRUE(android::base::ReadFileToString("/proc/self/maps", &maps));
139 EXPECT_TRUE(maps.find("GWP-ASan") != std::string::npos) << maps;
140
141 volatile int* x = new int;
142 delete x;
143 EXPECT_DEATH({ *x = 7; }, "");
144}
145
146TEST(gwp_asan_integration, DISABLED_assert_gwp_asan_disabled) {
147 std::string maps;
148 EXPECT_TRUE(android::base::ReadFileToString("/proc/self/maps", &maps));
149 EXPECT_TRUE(maps.find("GWP-ASan") == std::string::npos);
150}
151
152TEST(gwp_asan_integration, sysprops_program_specific) {
Christopher Ferris92a4d2b2023-03-02 21:45:31 +0000153 // Do not override HWASan with GWP ASan.
154 SKIP_WITH_HWASAN;
155
Mitch Phillips9634c362022-06-23 11:07:00 -0700156 SyspropRestorer restorer;
157
158 std::string path = testing::internal::GetArgvs()[0];
159 const char* basename = __gnu_basename(path.c_str());
160 __system_property_set((std::string("libc.debug.gwp_asan.sample_rate.") + basename).c_str(), "1");
161 __system_property_set((std::string("libc.debug.gwp_asan.process_sampling.") + basename).c_str(),
162 "1");
163 __system_property_set((std::string("libc.debug.gwp_asan.max_allocs.") + basename).c_str(),
164 "40000");
165
166 RunSubtestNoEnv("gwp_asan_integration.DISABLED_assert_gwp_asan_enabled");
167}
168
169TEST(gwp_asan_integration, sysprops_persist_program_specific) {
Christopher Ferris92a4d2b2023-03-02 21:45:31 +0000170 // Do not override HWASan with GWP ASan.
171 SKIP_WITH_HWASAN;
172
Mitch Phillips9634c362022-06-23 11:07:00 -0700173 SyspropRestorer restorer;
174
175 std::string path = testing::internal::GetArgvs()[0];
176 const char* basename = __gnu_basename(path.c_str());
177 __system_property_set(
178 (std::string("persist.libc.debug.gwp_asan.sample_rate.") + basename).c_str(), "1");
179 __system_property_set(
180 (std::string("persist.libc.debug.gwp_asan.process_sampling.") + basename).c_str(), "1");
181 __system_property_set((std::string("persist.libc.debug.gwp_asan.max_allocs.") + basename).c_str(),
182 "40000");
183
184 RunSubtestNoEnv("gwp_asan_integration.DISABLED_assert_gwp_asan_enabled");
185}
186
187TEST(gwp_asan_integration, sysprops_system) {
Christopher Ferris92a4d2b2023-03-02 21:45:31 +0000188 // Do not override HWASan with GWP ASan.
189 SKIP_WITH_HWASAN;
190
Mitch Phillips9634c362022-06-23 11:07:00 -0700191 SyspropRestorer restorer;
192
193 __system_property_set("libc.debug.gwp_asan.sample_rate.system_default", "1");
194 __system_property_set("libc.debug.gwp_asan.process_sampling.system_default", "1");
195 __system_property_set("libc.debug.gwp_asan.max_allocs.system_default", "40000");
196
197 RunSubtestNoEnv("gwp_asan_integration.DISABLED_assert_gwp_asan_enabled");
198}
199
200TEST(gwp_asan_integration, sysprops_persist_system) {
Christopher Ferris92a4d2b2023-03-02 21:45:31 +0000201 // Do not override HWASan with GWP ASan.
202 SKIP_WITH_HWASAN;
203
Mitch Phillips9634c362022-06-23 11:07:00 -0700204 SyspropRestorer restorer;
205
206 __system_property_set("persist.libc.debug.gwp_asan.sample_rate.system_default", "1");
207 __system_property_set("persist.libc.debug.gwp_asan.process_sampling.system_default", "1");
208 __system_property_set("persist.libc.debug.gwp_asan.max_allocs.system_default", "40000");
209
210 RunSubtestNoEnv("gwp_asan_integration.DISABLED_assert_gwp_asan_enabled");
211}
212
213TEST(gwp_asan_integration, sysprops_non_persist_overrides_persist) {
Christopher Ferris92a4d2b2023-03-02 21:45:31 +0000214 // Do not override HWASan with GWP ASan.
215 SKIP_WITH_HWASAN;
216
Mitch Phillips9634c362022-06-23 11:07:00 -0700217 SyspropRestorer restorer;
218
219 __system_property_set("libc.debug.gwp_asan.sample_rate.system_default", "1");
220 __system_property_set("libc.debug.gwp_asan.process_sampling.system_default", "1");
221 __system_property_set("libc.debug.gwp_asan.max_allocs.system_default", "40000");
222
223 __system_property_set("persist.libc.debug.gwp_asan.sample_rate.system_default", "0");
224 __system_property_set("persist.libc.debug.gwp_asan.process_sampling.system_default", "0");
225 __system_property_set("persist.libc.debug.gwp_asan.max_allocs.system_default", "0");
226
227 RunSubtestNoEnv("gwp_asan_integration.DISABLED_assert_gwp_asan_enabled");
228}
229
230TEST(gwp_asan_integration, sysprops_program_specific_overrides_default) {
Christopher Ferris92a4d2b2023-03-02 21:45:31 +0000231 // Do not override HWASan with GWP ASan.
232 SKIP_WITH_HWASAN;
233
Mitch Phillips9634c362022-06-23 11:07:00 -0700234 SyspropRestorer restorer;
235
236 std::string path = testing::internal::GetArgvs()[0];
237 const char* basename = __gnu_basename(path.c_str());
238 __system_property_set(
239 (std::string("persist.libc.debug.gwp_asan.sample_rate.") + basename).c_str(), "1");
240 __system_property_set(
241 (std::string("persist.libc.debug.gwp_asan.process_sampling.") + basename).c_str(), "1");
242 __system_property_set((std::string("persist.libc.debug.gwp_asan.max_allocs.") + basename).c_str(),
243 "40000");
244
245 __system_property_set("libc.debug.gwp_asan.sample_rate.system_default", "0");
246 __system_property_set("libc.debug.gwp_asan.process_sampling.system_default", "0");
247 __system_property_set("libc.debug.gwp_asan.max_allocs.system_default", "0");
248
249 RunSubtestNoEnv("gwp_asan_integration.DISABLED_assert_gwp_asan_enabled");
250}
251
252TEST(gwp_asan_integration, sysprops_can_disable) {
Christopher Ferris92a4d2b2023-03-02 21:45:31 +0000253 // Do not override HWASan with GWP ASan.
254 SKIP_WITH_HWASAN;
255
Mitch Phillips9634c362022-06-23 11:07:00 -0700256 SyspropRestorer restorer;
257
258 __system_property_set("libc.debug.gwp_asan.sample_rate.system_default", "0");
259 __system_property_set("libc.debug.gwp_asan.process_sampling.system_default", "0");
260 __system_property_set("libc.debug.gwp_asan.max_allocs.system_default", "0");
261
262 RunSubtestNoEnv("gwp_asan_integration.DISABLED_assert_gwp_asan_disabled");
263}
264
265TEST(gwp_asan_integration, env_overrides_sysprop) {
Christopher Ferris92a4d2b2023-03-02 21:45:31 +0000266 // Do not override HWASan with GWP ASan.
267 SKIP_WITH_HWASAN;
268
Mitch Phillips9634c362022-06-23 11:07:00 -0700269 SyspropRestorer restorer;
270
271 __system_property_set("libc.debug.gwp_asan.sample_rate.system_default", "0");
272 __system_property_set("libc.debug.gwp_asan.process_sampling.system_default", "0");
273 __system_property_set("libc.debug.gwp_asan.max_allocs.system_default", "0");
274
275 RunGwpAsanTest("gwp_asan_integration.DISABLED_assert_gwp_asan_enabled");
276}
277
Mitch Phillipse6997d52020-11-30 15:04:14 -0800278#endif // defined(__BIONIC__)