blob: d6a14b2959e586f417c309cc8f7551cdd1682158 [file] [log] [blame]
Ivan Lozano62cd0382021-11-01 10:27:54 -04001package rust
2
3import (
4 "fmt"
5 "strings"
6 "testing"
7
8 "android/soong/android"
9)
10
11type MemtagNoteType int
12
13const (
14 None MemtagNoteType = iota + 1
15 Sync
16 Async
17)
18
19func (t MemtagNoteType) str() string {
20 switch t {
21 case None:
22 return "none"
23 case Sync:
24 return "sync"
25 case Async:
26 return "async"
27 default:
28 panic("type_note_invalid")
29 }
30}
31
32func checkHasMemtagNote(t *testing.T, m android.TestingModule, expected MemtagNoteType) {
33 t.Helper()
34 note_async := "note_memtag_heap_async"
35 note_sync := "note_memtag_heap_sync"
36
37 found := None
38 implicits := m.Rule("rustc").Implicits
39 for _, lib := range implicits {
40 if strings.Contains(lib.Rel(), note_async) {
41 found = Async
42 break
43 } else if strings.Contains(lib.Rel(), note_sync) {
44 found = Sync
45 break
46 }
47 }
48
49 if found != expected {
50 t.Errorf("Wrong Memtag note in target %q: found %q, expected %q", m.Module().(*Module).Name(), found.str(), expected.str())
51 }
52}
53
54var prepareForTestWithMemtagHeap = android.GroupFixturePreparers(
55 android.FixtureModifyMockFS(func(fs android.MockFS) {
56 templateBp := `
57 rust_test {
58 name: "unset_test_%[1]s",
59 srcs: ["foo.rs"],
60 }
61
62 rust_test {
63 name: "no_memtag_test_%[1]s",
64 srcs: ["foo.rs"],
65 sanitize: { memtag_heap: false },
66 }
67
68 rust_test {
69 name: "set_memtag_test_%[1]s",
70 srcs: ["foo.rs"],
71 sanitize: { memtag_heap: true },
72 }
73
74 rust_test {
75 name: "set_memtag_set_async_test_%[1]s",
76 srcs: ["foo.rs"],
77 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
78 }
79
80 rust_test {
81 name: "set_memtag_set_sync_test_%[1]s",
82 srcs: ["foo.rs"],
83 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
84 }
85
86 rust_test {
87 name: "unset_memtag_set_sync_test_%[1]s",
88 srcs: ["foo.rs"],
89 sanitize: { diag: { memtag_heap: true } },
90 }
91
92 rust_binary {
93 name: "unset_binary_%[1]s",
94 srcs: ["foo.rs"],
95 }
96
97 rust_binary {
98 name: "no_memtag_binary_%[1]s",
99 srcs: ["foo.rs"],
100 sanitize: { memtag_heap: false },
101 }
102
103 rust_binary {
104 name: "set_memtag_binary_%[1]s",
105 srcs: ["foo.rs"],
106 sanitize: { memtag_heap: true },
107 }
108
109 rust_binary {
110 name: "set_memtag_set_async_binary_%[1]s",
111 srcs: ["foo.rs"],
112 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
113 }
114
115 rust_binary {
116 name: "set_memtag_set_sync_binary_%[1]s",
117 srcs: ["foo.rs"],
118 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
119 }
120
121 rust_binary {
122 name: "unset_memtag_set_sync_binary_%[1]s",
123 srcs: ["foo.rs"],
124 sanitize: { diag: { memtag_heap: true } },
125 }
126 `
127 subdirNoOverrideBp := fmt.Sprintf(templateBp, "no_override")
128 subdirOverrideDefaultDisableBp := fmt.Sprintf(templateBp, "override_default_disable")
129 subdirSyncBp := fmt.Sprintf(templateBp, "override_default_sync")
130 subdirAsyncBp := fmt.Sprintf(templateBp, "override_default_async")
131
132 fs.Merge(android.MockFS{
133 "subdir_no_override/Android.bp": []byte(subdirNoOverrideBp),
134 "subdir_override_default_disable/Android.bp": []byte(subdirOverrideDefaultDisableBp),
135 "subdir_sync/Android.bp": []byte(subdirSyncBp),
136 "subdir_async/Android.bp": []byte(subdirAsyncBp),
137 })
138 }),
139 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
140 variables.MemtagHeapExcludePaths = []string{"subdir_override_default_disable"}
141 // "subdir_override_default_disable" is covered by both include and override_default_disable paths. override_default_disable wins.
142 variables.MemtagHeapSyncIncludePaths = []string{"subdir_sync", "subdir_override_default_disable"}
143 variables.MemtagHeapAsyncIncludePaths = []string{"subdir_async", "subdir_override_default_disable"}
144 }),
145)
146
147func TestSanitizeMemtagHeap(t *testing.T) {
148 variant := "android_arm64_armv8-a"
149
150 result := android.GroupFixturePreparers(
151 prepareForRustTest,
152 prepareForTestWithMemtagHeap,
153 ).RunTest(t)
154 ctx := result.TestContext
155
156 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_binary_no_override", variant), None)
157 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_binary_override_default_async", variant), None)
158 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_binary_override_default_disable", variant), None)
159 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_binary_override_default_sync", variant), None)
160
161 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_test_no_override", variant), None)
162 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_test_override_default_async", variant), None)
163 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_test_override_default_disable", variant), None)
164 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_test_override_default_sync", variant), None)
165
166 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_binary_no_override", variant), Async)
167 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_binary_override_default_async", variant), Async)
168 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_binary_override_default_disable", variant), Async)
169 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_binary_override_default_sync", variant), Sync)
170
171 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_test_no_override", variant), Sync)
172 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_test_override_default_async", variant), Sync)
173 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_test_override_default_disable", variant), Sync)
174 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_test_override_default_sync", variant), Sync)
175
176 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_binary_no_override", variant), Async)
177 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_binary_override_default_async", variant), Async)
178 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_binary_override_default_disable", variant), Async)
179 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_binary_override_default_sync", variant), Async)
180
181 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_test_no_override", variant), Async)
182 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_test_override_default_async", variant), Async)
183 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_test_override_default_disable", variant), Async)
184 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_test_override_default_sync", variant), Async)
185
186 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_binary_no_override", variant), Sync)
187 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_binary_override_default_async", variant), Sync)
188 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_binary_override_default_disable", variant), Sync)
189 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_binary_override_default_sync", variant), Sync)
190
191 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_test_no_override", variant), Sync)
192 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_test_override_default_async", variant), Sync)
193 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_test_override_default_disable", variant), Sync)
194 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_test_override_default_sync", variant), Sync)
195
196 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_binary_no_override", variant), None)
197 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_binary_override_default_async", variant), Sync)
198 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_binary_override_default_disable", variant), None)
199 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_binary_override_default_sync", variant), Sync)
200
201 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_test_no_override", variant), Sync)
202 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_test_override_default_async", variant), Sync)
203 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_test_override_default_disable", variant), Sync)
204 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_test_override_default_sync", variant), Sync)
205
206 checkHasMemtagNote(t, ctx.ModuleForTests("unset_binary_no_override", variant), None)
207 checkHasMemtagNote(t, ctx.ModuleForTests("unset_binary_override_default_async", variant), Async)
208 checkHasMemtagNote(t, ctx.ModuleForTests("unset_binary_override_default_disable", variant), None)
209 checkHasMemtagNote(t, ctx.ModuleForTests("unset_binary_override_default_sync", variant), Sync)
210
211 checkHasMemtagNote(t, ctx.ModuleForTests("unset_test_no_override", variant), Sync)
212 checkHasMemtagNote(t, ctx.ModuleForTests("unset_test_override_default_async", variant), Sync)
213 checkHasMemtagNote(t, ctx.ModuleForTests("unset_test_override_default_disable", variant), Sync)
214 checkHasMemtagNote(t, ctx.ModuleForTests("unset_test_override_default_sync", variant), Sync)
215}
216
217func TestSanitizeMemtagHeapWithSanitizeDevice(t *testing.T) {
218 variant := "android_arm64_armv8-a"
219
220 result := android.GroupFixturePreparers(
221 prepareForRustTest,
222 prepareForTestWithMemtagHeap,
223 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
224 variables.SanitizeDevice = []string{"memtag_heap"}
225 }),
226 ).RunTest(t)
227 ctx := result.TestContext
228
229 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_binary_no_override", variant), None)
230 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_binary_override_default_async", variant), None)
231 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_binary_override_default_disable", variant), None)
232 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_binary_override_default_sync", variant), None)
233
234 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_test_no_override", variant), None)
235 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_test_override_default_async", variant), None)
236 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_test_override_default_disable", variant), None)
237 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_test_override_default_sync", variant), None)
238
239 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_binary_no_override", variant), Async)
240 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_binary_override_default_async", variant), Async)
241 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_binary_override_default_disable", variant), Async)
242 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_binary_override_default_sync", variant), Sync)
243
244 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_test_no_override", variant), Sync)
245 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_test_override_default_async", variant), Sync)
246 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_test_override_default_disable", variant), Sync)
247 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_test_override_default_sync", variant), Sync)
248
249 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_binary_no_override", variant), Async)
250 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_binary_override_default_async", variant), Async)
251 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_binary_override_default_disable", variant), Async)
252 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_binary_override_default_sync", variant), Async)
253
254 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_test_no_override", variant), Async)
255 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_test_override_default_async", variant), Async)
256 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_test_override_default_disable", variant), Async)
257 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_test_override_default_sync", variant), Async)
258
259 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_binary_no_override", variant), Sync)
260 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_binary_override_default_async", variant), Sync)
261 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_binary_override_default_disable", variant), Sync)
262 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_binary_override_default_sync", variant), Sync)
263
264 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_test_no_override", variant), Sync)
265 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_test_override_default_async", variant), Sync)
266 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_test_override_default_disable", variant), Sync)
267 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_test_override_default_sync", variant), Sync)
268
269 // should sanitize: { diag: { memtag: true } } result in Sync instead of None here?
270 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_binary_no_override", variant), Sync)
271 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_binary_override_default_async", variant), Sync)
272 // should sanitize: { diag: { memtag: true } } result in Sync instead of None here?
273 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_binary_override_default_disable", variant), None)
274 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_binary_override_default_sync", variant), Sync)
275
276 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_test_no_override", variant), Sync)
277 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_test_override_default_async", variant), Sync)
278 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_test_override_default_disable", variant), Sync)
279 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_test_override_default_sync", variant), Sync)
280
281 checkHasMemtagNote(t, ctx.ModuleForTests("unset_binary_no_override", variant), Async)
282 checkHasMemtagNote(t, ctx.ModuleForTests("unset_binary_override_default_async", variant), Async)
283 checkHasMemtagNote(t, ctx.ModuleForTests("unset_binary_override_default_disable", variant), None)
284 checkHasMemtagNote(t, ctx.ModuleForTests("unset_binary_override_default_sync", variant), Sync)
285
286 checkHasMemtagNote(t, ctx.ModuleForTests("unset_test_no_override", variant), Sync)
287 checkHasMemtagNote(t, ctx.ModuleForTests("unset_test_override_default_async", variant), Sync)
288 checkHasMemtagNote(t, ctx.ModuleForTests("unset_test_override_default_disable", variant), Sync)
289 checkHasMemtagNote(t, ctx.ModuleForTests("unset_test_override_default_sync", variant), Sync)
290}
291
292func TestSanitizeMemtagHeapWithSanitizeDeviceDiag(t *testing.T) {
293 variant := "android_arm64_armv8-a"
294
295 result := android.GroupFixturePreparers(
296 prepareForRustTest,
297 prepareForTestWithMemtagHeap,
298 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
299 variables.SanitizeDevice = []string{"memtag_heap"}
300 variables.SanitizeDeviceDiag = []string{"memtag_heap"}
301 }),
302 ).RunTest(t)
303 ctx := result.TestContext
304
305 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_binary_no_override", variant), None)
306 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_binary_override_default_async", variant), None)
307 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_binary_override_default_disable", variant), None)
308 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_binary_override_default_sync", variant), None)
309
310 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_test_no_override", variant), None)
311 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_test_override_default_async", variant), None)
312 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_test_override_default_disable", variant), None)
313 checkHasMemtagNote(t, ctx.ModuleForTests("no_memtag_test_override_default_sync", variant), None)
314
315 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_binary_no_override", variant), Sync)
316 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_binary_override_default_async", variant), Sync)
317 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_binary_override_default_disable", variant), Sync)
318 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_binary_override_default_sync", variant), Sync)
319
320 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_test_no_override", variant), Sync)
321 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_test_override_default_async", variant), Sync)
322 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_test_override_default_disable", variant), Sync)
323 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_test_override_default_sync", variant), Sync)
324
325 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_binary_no_override", variant), Async)
326 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_binary_override_default_async", variant), Async)
327 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_binary_override_default_disable", variant), Async)
328 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_binary_override_default_sync", variant), Async)
329
330 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_test_no_override", variant), Async)
331 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_test_override_default_async", variant), Async)
332 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_test_override_default_disable", variant), Async)
333 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_async_test_override_default_sync", variant), Async)
334
335 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_binary_no_override", variant), Sync)
336 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_binary_override_default_async", variant), Sync)
337 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_binary_override_default_disable", variant), Sync)
338 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_binary_override_default_sync", variant), Sync)
339
340 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_test_no_override", variant), Sync)
341 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_test_override_default_async", variant), Sync)
342 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_test_override_default_disable", variant), Sync)
343 checkHasMemtagNote(t, ctx.ModuleForTests("set_memtag_set_sync_test_override_default_sync", variant), Sync)
344
345 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_binary_no_override", variant), Sync)
346 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_binary_override_default_async", variant), Sync)
347 // should sanitize: { diag: { memtag: true } } result in Sync instead of None here?
348 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_binary_override_default_disable", variant), None)
349 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_binary_override_default_sync", variant), Sync)
350
351 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_test_no_override", variant), Sync)
352 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_test_override_default_async", variant), Sync)
353 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_test_override_default_disable", variant), Sync)
354 checkHasMemtagNote(t, ctx.ModuleForTests("unset_memtag_set_sync_test_override_default_sync", variant), Sync)
355
356 checkHasMemtagNote(t, ctx.ModuleForTests("unset_binary_no_override", variant), Sync)
357 checkHasMemtagNote(t, ctx.ModuleForTests("unset_binary_override_default_async", variant), Sync)
358 checkHasMemtagNote(t, ctx.ModuleForTests("unset_binary_override_default_disable", variant), None)
359 checkHasMemtagNote(t, ctx.ModuleForTests("unset_binary_override_default_sync", variant), Sync)
360
361 checkHasMemtagNote(t, ctx.ModuleForTests("unset_test_no_override", variant), Sync)
362 checkHasMemtagNote(t, ctx.ModuleForTests("unset_test_override_default_async", variant), Sync)
363 checkHasMemtagNote(t, ctx.ModuleForTests("unset_test_override_default_disable", variant), Sync)
364 checkHasMemtagNote(t, ctx.ModuleForTests("unset_test_override_default_sync", variant), Sync)
365}