blob: 50f55ce759e412f705777cb56977418050e4ad2a [file] [log] [blame]
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001// Copyright 2020 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rust
16
17import (
Ivan Lozano5482d6a2021-11-01 10:13:25 -040018 "fmt"
19 "strings"
20
21 "github.com/google/blueprint"
22 "github.com/google/blueprint/proptools"
23
Ivan Lozano6cd99e62020-02-11 08:24:25 -050024 "android/soong/android"
25 "android/soong/cc"
26 "android/soong/rust/config"
Ivan Lozano6cd99e62020-02-11 08:24:25 -050027)
28
Ivan Lozano62cd0382021-11-01 10:27:54 -040029// TODO: When Rust has sanitizer-parity with CC, deduplicate this struct
Ivan Lozano6cd99e62020-02-11 08:24:25 -050030type SanitizeProperties struct {
Tri Vo0a74c3e2021-04-01 13:59:27 -070031 // enable AddressSanitizer, HWAddressSanitizer, and others.
Ivan Lozano6cd99e62020-02-11 08:24:25 -050032 Sanitize struct {
Tri Vo0a74c3e2021-04-01 13:59:27 -070033 Address *bool `android:"arch_variant"`
34 Hwaddress *bool `android:"arch_variant"`
Ivan Lozano62cd0382021-11-01 10:27:54 -040035
36 // Memory-tagging, only available on arm64
37 // if diag.memtag unset or false, enables async memory tagging
38 Memtag_heap *bool `android:"arch_variant"`
39 Fuzzer *bool `android:"arch_variant"`
40 Never *bool `android:"arch_variant"`
41
42 // Sanitizers to run in the diagnostic mode (as opposed to the release mode).
43 // Replaces abort() on error with a human-readable error message.
44 // Address and Thread sanitizers always run in diagnostic mode.
45 Diag struct {
46 // Memory-tagging, only available on arm64
47 // requires sanitizer.memtag: true
48 // if set, enables sync memory tagging
49 Memtag_heap *bool `android:"arch_variant"`
50 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -050051 }
Lukacs T. Berki6c716762022-06-13 20:50:39 +020052 SanitizerEnabled bool `blueprint:"mutated"`
Ivan Lozano6cd99e62020-02-11 08:24:25 -050053
54 // Used when we need to place libraries in their own directory, such as ASAN.
55 InSanitizerDir bool `blueprint:"mutated"`
Ivan Lozano9eaacc82024-10-30 14:28:17 +000056
57 // ForceDisable is set by the version mutator to disable sanitization of stubs variants
58 ForceDisable bool `blueprint:"mutated"`
Ivan Lozano6cd99e62020-02-11 08:24:25 -050059}
60
61var fuzzerFlags = []string{
Ivan Lozano357433a2024-02-12 14:13:37 -050062 "-Z external-clangrt=true",
63
Charisee5ddec432022-03-01 03:02:51 +000064 "-C passes='sancov-module'",
Ivan Lozano6cd99e62020-02-11 08:24:25 -050065
66 "--cfg fuzzing",
Ivan Lozanoc044f5b2021-04-02 12:43:28 -040067 "-C llvm-args=-sanitizer-coverage-level=3",
Ivan Lozano6cd99e62020-02-11 08:24:25 -050068 "-C llvm-args=-sanitizer-coverage-trace-compares",
69 "-C llvm-args=-sanitizer-coverage-inline-8bit-counters",
Ivan Lozano1247b3c2023-06-12 14:57:44 -040070 "-C llvm-args=-sanitizer-coverage-pc-table",
Ivan Lozano6cd99e62020-02-11 08:24:25 -050071
Ivan Lozanoaa7c9802021-08-02 09:43:46 -040072 // See https://github.com/rust-fuzz/cargo-fuzz/pull/193
73 "-C link-dead-code",
74
Ivan Lozano6cd99e62020-02-11 08:24:25 -050075 // Sancov breaks with lto
Charisee5ddec432022-03-01 03:02:51 +000076 // TODO: Remove when https://bugs.llvm.org/show_bug.cgi?id=41734 is resolved and sancov-module works with LTO
Ivan Lozano6cd99e62020-02-11 08:24:25 -050077 "-C lto=no",
78}
79
80var asanFlags = []string{
Ivan Lozano357433a2024-02-12 14:13:37 -050081 "-Z external-clangrt=true",
Ivan Lozano6cd99e62020-02-11 08:24:25 -050082 "-Z sanitizer=address",
83}
84
Ivan Lozano5482d6a2021-11-01 10:13:25 -040085// See cc/sanitize.go's hwasanGlobalOptions for global hwasan options.
Tri Vo0a74c3e2021-04-01 13:59:27 -070086var hwasanFlags = []string{
Ivan Lozano357433a2024-02-12 14:13:37 -050087 "-Z external-clangrt=true",
Tri Vo0a74c3e2021-04-01 13:59:27 -070088 "-Z sanitizer=hwaddress",
89 "-C target-feature=+tagged-globals",
Ivan Lozano5482d6a2021-11-01 10:13:25 -040090
91 // Flags from cc/sanitize.go hwasanFlags
92 "-C llvm-args=--aarch64-enable-global-isel-at-O=-1",
93 "-C llvm-args=-fast-isel=false",
94 "-C llvm-args=-instcombine-lower-dbg-declare=0",
95
96 // Additional flags for HWASAN-ified Rust/C interop
97 "-C llvm-args=--hwasan-with-ifunc",
Tri Vo0a74c3e2021-04-01 13:59:27 -070098}
99
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500100func init() {
101}
102func (sanitize *sanitize) props() []interface{} {
103 return []interface{}{&sanitize.Properties}
104}
105
106func (sanitize *sanitize) begin(ctx BaseModuleContext) {
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400107 s := &sanitize.Properties.Sanitize
108
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000109 if sanitize.Properties.ForceDisable {
110 return
111 }
112
Colin Cross9759ed72024-10-29 11:47:46 -0700113 // Disable sanitizers for musl x86 modules, rustc does not support any sanitizers.
114 if ctx.Os() == android.LinuxMusl && ctx.Arch().ArchType == android.X86 {
115 s.Never = proptools.BoolPtr(true)
116 }
117
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400118 // Never always wins.
119 if Bool(s.Never) {
120 return
121 }
122
Ivan Lozano62cd0382021-11-01 10:27:54 -0400123 // rust_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {Memtag_heap}).
124 if binary, ok := ctx.RustModule().compiler.(binaryInterface); ok && binary.testBinary() {
125 if s.Memtag_heap == nil {
126 s.Memtag_heap = proptools.BoolPtr(true)
127 }
128 if s.Diag.Memtag_heap == nil {
129 s.Diag.Memtag_heap = proptools.BoolPtr(true)
130 }
131 }
132
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400133 var globalSanitizers []string
Ivan Lozano62cd0382021-11-01 10:27:54 -0400134 var globalSanitizersDiag []string
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400135
136 if ctx.Host() {
137 if !ctx.Windows() {
138 globalSanitizers = ctx.Config().SanitizeHost()
139 }
140 } else {
141 arches := ctx.Config().SanitizeDeviceArch()
142 if len(arches) == 0 || android.InList(ctx.Arch().ArchType.Name, arches) {
143 globalSanitizers = ctx.Config().SanitizeDevice()
Ivan Lozano62cd0382021-11-01 10:27:54 -0400144 globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400145 }
146 }
147
148 if len(globalSanitizers) > 0 {
149 var found bool
150
151 // Global Sanitizers
152 if found, globalSanitizers = android.RemoveFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
Ivan Lozano6c5b8f42021-11-15 09:35:12 -0500153 // TODO(b/204776996): HWASan for static Rust binaries isn't supported yet.
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400154 if !ctx.RustModule().StaticExecutable() {
155 s.Hwaddress = proptools.BoolPtr(true)
156 }
157 }
158
Ivan Lozano62cd0382021-11-01 10:27:54 -0400159 if found, globalSanitizers = android.RemoveFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
160 if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
161 s.Memtag_heap = proptools.BoolPtr(true)
162 }
163 }
164
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400165 if found, globalSanitizers = android.RemoveFromList("address", globalSanitizers); found && s.Address == nil {
166 s.Address = proptools.BoolPtr(true)
167 }
168
169 if found, globalSanitizers = android.RemoveFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
Ivan Lozano6c5b8f42021-11-15 09:35:12 -0500170 // TODO(b/204776996): HWASan for static Rust binaries isn't supported yet, and fuzzer enables HWAsan
171 if !ctx.RustModule().StaticExecutable() {
172 s.Fuzzer = proptools.BoolPtr(true)
173 }
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400174 }
175
Ivan Lozano62cd0382021-11-01 10:27:54 -0400176 // Global Diag Sanitizers
177 if found, globalSanitizersDiag = android.RemoveFromList("memtag_heap", globalSanitizersDiag); found &&
178 s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
179 s.Diag.Memtag_heap = proptools.BoolPtr(true)
180 }
181 }
182
183 // Enable Memtag for all components in the include paths (for Aarch64 only)
Colin Cross1faf8232022-06-24 18:42:32 -0700184 if ctx.Arch().ArchType == android.Arm64 && ctx.Os().Bionic() {
Ivan Lozano62cd0382021-11-01 10:27:54 -0400185 if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
186 if s.Memtag_heap == nil {
187 s.Memtag_heap = proptools.BoolPtr(true)
188 }
189 if s.Diag.Memtag_heap == nil {
190 s.Diag.Memtag_heap = proptools.BoolPtr(true)
191 }
192 } else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
193 if s.Memtag_heap == nil {
194 s.Memtag_heap = proptools.BoolPtr(true)
195 }
196 }
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400197 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500198
Tri Vo0a74c3e2021-04-01 13:59:27 -0700199 // HWASan requires AArch64 hardware feature (top-byte-ignore).
Colin Cross1faf8232022-06-24 18:42:32 -0700200 if ctx.Arch().ArchType != android.Arm64 || !ctx.Os().Bionic() {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700201 s.Hwaddress = nil
202 }
203
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400204 // HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
205 // Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
206 if (ctx.RustModule().InRamdisk() || ctx.RustModule().InVendorRamdisk() || ctx.RustModule().InRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
207 s.Hwaddress = nil
208 }
209
210 if Bool(s.Hwaddress) {
211 s.Address = nil
212 }
213
Ivan Lozano62cd0382021-11-01 10:27:54 -0400214 // Memtag_heap is only implemented on AArch64.
Colin Cross1faf8232022-06-24 18:42:32 -0700215 if ctx.Arch().ArchType != android.Arm64 || !ctx.Os().Bionic() {
Ivan Lozano62cd0382021-11-01 10:27:54 -0400216 s.Memtag_heap = nil
217 }
218
Florian Mayer296d5952022-10-12 19:30:14 +0000219 // TODO:(b/178369775)
Ivan Lozano2fcbffa2023-07-27 10:40:52 -0400220 // For now sanitizing is only supported on non-windows targets
221 if ctx.Os() != android.Windows && (Bool(s.Hwaddress) || Bool(s.Address) || Bool(s.Memtag_heap) || Bool(s.Fuzzer)) {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700222 sanitize.Properties.SanitizerEnabled = true
223 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500224}
225
226type sanitize struct {
227 Properties SanitizeProperties
228}
229
230func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000231 if sanitize.Properties.ForceDisable {
232 return flags, deps
233 }
234
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500235 if !sanitize.Properties.SanitizerEnabled {
236 return flags, deps
237 }
Ivan Lozano408d3732023-07-27 13:28:08 -0400238
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500239 if Bool(sanitize.Properties.Sanitize.Fuzzer) {
240 flags.RustFlags = append(flags.RustFlags, fuzzerFlags...)
Ivan Lozano408d3732023-07-27 13:28:08 -0400241 }
242
243 if Bool(sanitize.Properties.Sanitize.Hwaddress) {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700244 flags.RustFlags = append(flags.RustFlags, hwasanFlags...)
Ivan Lozano408d3732023-07-27 13:28:08 -0400245 }
246
247 if Bool(sanitize.Properties.Sanitize.Address) {
Ivan Lozanof3e8fc32021-11-13 07:43:54 -0500248 flags.RustFlags = append(flags.RustFlags, asanFlags...)
Ivan Lozano2fcbffa2023-07-27 10:40:52 -0400249 if ctx.Host() {
250 // -nodefaultlibs (provided with libc++) prevents the driver from linking
251 // libraries needed with -fsanitize=address. http://b/18650275 (WAI)
252 flags.LinkFlags = append(flags.LinkFlags, []string{"-Wl,--no-as-needed"}...)
253 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700254 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500255 return flags, deps
256}
257
258func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
259 return deps
260}
261
262func rustSanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
263 if mod, ok := mctx.Module().(*Module); ok && mod.sanitize != nil {
Cole Fausta963b942024-04-11 17:43:00 -0700264 if !mod.Enabled(mctx) {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500265 return
266 }
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000267 if mod.sanitize.Properties.ForceDisable {
268 return
269 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700270
Ivan Lozano62cd0382021-11-01 10:27:54 -0400271 if Bool(mod.sanitize.Properties.Sanitize.Memtag_heap) && mod.Binary() {
272 noteDep := "note_memtag_heap_async"
273 if Bool(mod.sanitize.Properties.Sanitize.Diag.Memtag_heap) {
274 noteDep = "note_memtag_heap_sync"
275 }
Ivan Lozano62cd0382021-11-01 10:27:54 -0400276 depTag := cc.StaticDepTag(true)
277 variations := append(mctx.Target().Variations(),
278 blueprint.Variation{Mutator: "link", Variation: "static"})
279 if mod.Device() {
280 variations = append(variations, mod.ImageVariation())
281 }
282 mctx.AddFarVariationDependencies(variations, depTag, noteDep)
283 }
284
Tri Vo0a74c3e2021-04-01 13:59:27 -0700285 variations := mctx.Target().Variations()
286 var depTag blueprint.DependencyTag
287 var deps []string
288
Ivan Lozano408d3732023-07-27 13:28:08 -0400289 if mod.IsSanitizerEnabled(cc.Asan) {
Ivan Lozano2fcbffa2023-07-27 10:40:52 -0400290 if mod.Host() {
291 variations = append(variations,
292 blueprint.Variation{Mutator: "link", Variation: "static"})
293 depTag = cc.StaticDepTag(false)
294 deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "asan.static")}
295 } else {
296 variations = append(variations,
297 blueprint.Variation{Mutator: "link", Variation: "shared"})
Colin Cross8acea3e2024-12-12 14:53:30 -0800298 depTag = cc.SharedDepTag()
Ivan Lozano2fcbffa2023-07-27 10:40:52 -0400299 deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "asan")}
300 }
Ivan Lozano408d3732023-07-27 13:28:08 -0400301 } else if mod.IsSanitizerEnabled(cc.Hwasan) {
Ivan Lozano6c5b8f42021-11-15 09:35:12 -0500302 // TODO(b/204776996): HWASan for static Rust binaries isn't supported yet.
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400303 if binary, ok := mod.compiler.(binaryInterface); ok {
304 if binary.staticallyLinked() {
Tri Vo0a74c3e2021-04-01 13:59:27 -0700305 mctx.ModuleErrorf("HWASan is not supported for static Rust executables yet.")
306 }
307 }
308
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400309 // Always link against the shared library -- static binaries will pull in the static
310 // library during final link if necessary
311 variations = append(variations,
312 blueprint.Variation{Mutator: "link", Variation: "shared"})
Colin Cross8acea3e2024-12-12 14:53:30 -0800313 depTag = cc.SharedDepTag()
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400314 deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "hwasan")}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500315 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700316
Ivan Lozano62cd0382021-11-01 10:27:54 -0400317 if len(deps) > 0 {
318 mctx.AddFarVariationDependencies(variations, depTag, deps...)
319 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500320 }
321}
322
323func (sanitize *sanitize) SetSanitizer(t cc.SanitizerType, b bool) {
324 sanitizerSet := false
325 switch t {
326 case cc.Fuzzer:
Colin Cross9759ed72024-10-29 11:47:46 -0700327 sanitize.Properties.Sanitize.Fuzzer = proptools.BoolPtr(b)
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500328 sanitizerSet = true
329 case cc.Asan:
Colin Cross9759ed72024-10-29 11:47:46 -0700330 sanitize.Properties.Sanitize.Address = proptools.BoolPtr(b)
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500331 sanitizerSet = true
Tri Vo0a74c3e2021-04-01 13:59:27 -0700332 case cc.Hwasan:
Colin Cross9759ed72024-10-29 11:47:46 -0700333 sanitize.Properties.Sanitize.Hwaddress = proptools.BoolPtr(b)
Tri Vo0a74c3e2021-04-01 13:59:27 -0700334 sanitizerSet = true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400335 case cc.Memtag_heap:
Colin Cross9759ed72024-10-29 11:47:46 -0700336 sanitize.Properties.Sanitize.Memtag_heap = proptools.BoolPtr(b)
Ivan Lozano62cd0382021-11-01 10:27:54 -0400337 sanitizerSet = true
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500338 default:
339 panic(fmt.Errorf("setting unsupported sanitizerType %d", t))
340 }
341 if b && sanitizerSet {
342 sanitize.Properties.SanitizerEnabled = true
343 }
344}
345
Ivan Lozanod7586b62021-04-01 09:49:36 -0400346func (m *Module) UbsanRuntimeNeeded() bool {
347 return false
348}
349
350func (m *Module) MinimalRuntimeNeeded() bool {
351 return false
352}
353
354func (m *Module) UbsanRuntimeDep() bool {
355 return false
356}
357
358func (m *Module) MinimalRuntimeDep() bool {
359 return false
360}
361
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500362// Check if the sanitizer is explicitly disabled (as opposed to nil by
363// virtue of not being set).
364func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t cc.SanitizerType) bool {
365 if sanitize == nil {
366 return false
367 }
368 if Bool(sanitize.Properties.Sanitize.Never) {
369 return true
370 }
371 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
372 return sanitizerVal != nil && *sanitizerVal == false
373}
374
375// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
376// because enabling a sanitizer either directly (via the blueprint) or
377// indirectly (via a mutator) sets the bool ptr to true, and you can't
378// distinguish between the cases. It isn't needed though - both cases can be
379// treated identically.
380func (sanitize *sanitize) isSanitizerEnabled(t cc.SanitizerType) bool {
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000381 if sanitize == nil || !sanitize.Properties.SanitizerEnabled || sanitize.Properties.ForceDisable {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500382 return false
383 }
384
385 sanitizerVal := sanitize.getSanitizerBoolPtr(t)
386 return sanitizerVal != nil && *sanitizerVal == true
387}
388
389func (sanitize *sanitize) getSanitizerBoolPtr(t cc.SanitizerType) *bool {
390 switch t {
391 case cc.Fuzzer:
392 return sanitize.Properties.Sanitize.Fuzzer
393 case cc.Asan:
394 return sanitize.Properties.Sanitize.Address
Tri Vo0a74c3e2021-04-01 13:59:27 -0700395 case cc.Hwasan:
396 return sanitize.Properties.Sanitize.Hwaddress
Ivan Lozano62cd0382021-11-01 10:27:54 -0400397 case cc.Memtag_heap:
398 return sanitize.Properties.Sanitize.Memtag_heap
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500399 default:
400 return nil
401 }
402}
403
Tri Vo0a74c3e2021-04-01 13:59:27 -0700404func (sanitize *sanitize) AndroidMk(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
405 // Add a suffix for hwasan rlib libraries to allow surfacing both the sanitized and
406 // non-sanitized variants to make without a name conflict.
407 if entries.Class == "RLIB_LIBRARIES" || entries.Class == "STATIC_LIBRARIES" {
408 if sanitize.isSanitizerEnabled(cc.Hwasan) {
409 entries.SubName += ".hwasan"
410 }
411 }
412}
413
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500414func (mod *Module) SanitizerSupported(t cc.SanitizerType) bool {
Ivan Lozano2fcbffa2023-07-27 10:40:52 -0400415 // Sanitizers are not supported on Windows targets.
416 if mod.Os() == android.Windows {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500417 return false
418 }
419 switch t {
420 case cc.Fuzzer:
421 return true
422 case cc.Asan:
423 return true
Tri Vo0a74c3e2021-04-01 13:59:27 -0700424 case cc.Hwasan:
Ivan Lozano5482d6a2021-11-01 10:13:25 -0400425 // TODO(b/180495975): HWASan for static Rust binaries isn't supported yet.
426 if mod.StaticExecutable() {
427 return false
428 }
Tri Vo0a74c3e2021-04-01 13:59:27 -0700429 return true
Ivan Lozano62cd0382021-11-01 10:27:54 -0400430 case cc.Memtag_heap:
431 return true
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500432 default:
433 return false
434 }
435}
436
437func (mod *Module) IsSanitizerEnabled(t cc.SanitizerType) bool {
438 return mod.sanitize.isSanitizerEnabled(t)
439}
440
441func (mod *Module) IsSanitizerExplicitlyDisabled(t cc.SanitizerType) bool {
Ivan Lozano2fcbffa2023-07-27 10:40:52 -0400442 // Sanitizers are not supported on Windows targets.
443 if mod.Os() == android.Windows {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500444 return true
445 }
446
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500447 return mod.sanitize.isSanitizerExplicitlyDisabled(t)
448}
449
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500450func (mod *Module) SetSanitizer(t cc.SanitizerType, b bool) {
451 if !Bool(mod.sanitize.Properties.Sanitize.Never) {
452 mod.sanitize.SetSanitizer(t, b)
453 }
454}
455
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500456func (mod *Module) StaticallyLinked() bool {
457 if lib, ok := mod.compiler.(libraryInterface); ok {
Tri Vo505b0e82021-04-08 15:50:48 -0700458 return lib.rlib() || lib.static()
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400459 } else if binary, ok := mod.compiler.(binaryInterface); ok {
460 return binary.staticallyLinked()
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500461 }
462 return false
463}
464
465func (mod *Module) SetInSanitizerDir() {
466 mod.sanitize.Properties.InSanitizerDir = true
467}
468
469func (mod *Module) SanitizeNever() bool {
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000470 return Bool(mod.sanitize.Properties.Sanitize.Never) || mod.sanitize.Properties.ForceDisable
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500471}
472
473var _ cc.PlatformSanitizeable = (*Module)(nil)
474
475func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
476 switch t := tag.(type) {
477 case dependencyTag:
478 return t.library
479 default:
480 return cc.IsSanitizableDependencyTag(tag)
481 }
482}
483
484func (m *Module) SanitizableDepTagChecker() cc.SantizableDependencyTagChecker {
485 return IsSanitizableDependencyTag
486}