2016-01-06 23:41:07 +01:00
|
|
|
// Copyright 2016 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-11-29 01:37:53 +01:00
|
|
|
"sort"
|
2016-01-06 23:41:07 +01:00
|
|
|
"strings"
|
2017-11-17 20:08:10 +01:00
|
|
|
"sync"
|
2016-01-06 23:41:07 +01:00
|
|
|
|
2018-06-21 22:03:07 +02:00
|
|
|
"github.com/google/blueprint"
|
2021-06-25 20:53:40 +02:00
|
|
|
"github.com/google/blueprint/proptools"
|
2018-06-21 22:03:07 +02:00
|
|
|
|
2016-05-19 00:37:25 +02:00
|
|
|
"android/soong/android"
|
2016-08-15 23:18:24 +02:00
|
|
|
"android/soong/cc/config"
|
2024-05-07 07:47:35 +02:00
|
|
|
"android/soong/etc"
|
2016-01-06 23:41:07 +01:00
|
|
|
)
|
|
|
|
|
2017-06-15 23:45:18 +02:00
|
|
|
var (
|
|
|
|
// Any C flags added by sanitizer which libTooling tools may not
|
|
|
|
// understand also need to be added to ClangLibToolingUnknownCflags in
|
|
|
|
// cc/config/clang.go
|
|
|
|
|
2019-08-21 10:38:40 +02:00
|
|
|
asanCflags = []string{
|
|
|
|
"-fno-omit-frame-pointer",
|
|
|
|
}
|
2017-06-15 23:45:18 +02:00
|
|
|
asanLdflags = []string{"-Wl,-u,__asan_preinit"}
|
2017-02-14 16:59:33 +01:00
|
|
|
|
2023-06-17 01:48:51 +02:00
|
|
|
// DO NOT ADD MLLVM FLAGS HERE! ADD THEM BELOW TO hwasanCommonFlags.
|
2021-11-04 09:14:14 +01:00
|
|
|
hwasanCflags = []string{
|
|
|
|
"-fno-omit-frame-pointer",
|
|
|
|
"-Wno-frame-larger-than=",
|
2020-03-27 20:38:42 +01:00
|
|
|
"-fsanitize-hwaddress-abi=platform",
|
2021-11-04 09:14:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ThinLTO performs codegen during link time, thus these flags need to
|
|
|
|
// passed to both CFLAGS and LDFLAGS.
|
|
|
|
hwasanCommonflags = []string{
|
2019-11-23 03:37:10 +01:00
|
|
|
// The following improves debug location information
|
|
|
|
// availability at the cost of its accuracy. It increases
|
|
|
|
// the likelihood of a stack variable's frame offset
|
|
|
|
// to be recorded in the debug info, which is important
|
|
|
|
// for the quality of hwasan reports. The downside is a
|
|
|
|
// higher number of "optimized out" stack variables.
|
|
|
|
// b/112437883.
|
2021-11-04 09:14:14 +01:00
|
|
|
"-instcombine-lower-dbg-declare=0",
|
2023-06-17 01:50:59 +02:00
|
|
|
"-dom-tree-reachability-max-bbs-to-explore=128",
|
2019-11-23 03:37:10 +01:00
|
|
|
}
|
2018-08-03 01:19:13 +02:00
|
|
|
|
2023-06-12 21:18:28 +02:00
|
|
|
sanitizeIgnorelistPrefix = "-fsanitize-ignorelist="
|
|
|
|
|
2023-03-22 21:22:27 +01:00
|
|
|
cfiBlocklistPath = "external/compiler-rt/lib/cfi"
|
|
|
|
cfiBlocklistFilename = "cfi_blocklist.txt"
|
2023-06-01 23:12:08 +02:00
|
|
|
cfiEnableFlag = "-fsanitize=cfi"
|
2023-04-04 22:13:42 +02:00
|
|
|
cfiCrossDsoFlag = "-fsanitize-cfi-cross-dso"
|
|
|
|
cfiCflags = []string{"-flto", cfiCrossDsoFlag,
|
2023-06-12 21:18:28 +02:00
|
|
|
sanitizeIgnorelistPrefix + cfiBlocklistPath + "/" + cfiBlocklistFilename}
|
2018-08-31 21:54:33 +02:00
|
|
|
// -flto and -fvisibility are required by clang when -fsanitize=cfi is
|
|
|
|
// used, but have no effect on assembly files
|
|
|
|
cfiAsflags = []string{"-flto", "-fvisibility=default"}
|
2023-06-01 23:12:08 +02:00
|
|
|
cfiLdflags = []string{"-flto", cfiCrossDsoFlag, cfiEnableFlag,
|
2017-08-29 06:50:17 +02:00
|
|
|
"-Wl,-plugin-opt,O1"}
|
2023-03-22 21:22:27 +01:00
|
|
|
cfiExportsMapPath = "build/soong/cc/config"
|
|
|
|
cfiExportsMapFilename = "cfi_exports.map"
|
|
|
|
cfiAssemblySupportFlag = "-fno-sanitize-cfi-canonical-jump-tables"
|
2017-06-28 18:10:48 +02:00
|
|
|
|
2021-08-28 00:12:56 +02:00
|
|
|
intOverflowCflags = []string{"-fsanitize-ignorelist=build/soong/cc/config/integer_overflow_blocklist.txt"}
|
2018-11-20 01:03:58 +01:00
|
|
|
|
2019-03-06 19:38:48 +01:00
|
|
|
minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
|
2018-10-08 18:29:39 +02:00
|
|
|
"-fno-sanitize-recover=integer,undefined"}
|
2019-05-15 21:49:54 +02:00
|
|
|
hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
|
2023-01-26 23:19:27 +01:00
|
|
|
"export_memory_stats=0", "max_malloc_fill_size=131072", "malloc_fill_byte=0"}
|
2024-05-01 02:34:58 +02:00
|
|
|
memtagStackCommonFlags = []string{"-march=armv8-a+memtag"}
|
2024-05-03 18:10:04 +02:00
|
|
|
memtagStackLlvmFlags = []string{"-dom-tree-reachability-max-bbs-to-explore=128"}
|
2023-01-19 17:02:47 +01:00
|
|
|
|
|
|
|
hostOnlySanitizeFlags = []string{"-fno-sanitize-recover=all"}
|
2023-10-05 23:50:48 +02:00
|
|
|
deviceOnlySanitizeFlags = []string{"-fsanitize-trap=all"}
|
2023-08-02 22:24:29 +02:00
|
|
|
|
|
|
|
noSanitizeLinkRuntimeFlag = "-fno-sanitize-link-runtime"
|
2016-10-14 01:44:07 +02:00
|
|
|
)
|
|
|
|
|
2020-12-14 17:27:52 +01:00
|
|
|
type SanitizerType int
|
2016-01-06 23:41:07 +01:00
|
|
|
|
|
|
|
const (
|
2020-12-14 17:27:52 +01:00
|
|
|
Asan SanitizerType = iota + 1
|
2021-04-01 20:29:09 +02:00
|
|
|
Hwasan
|
2016-01-06 23:41:07 +01:00
|
|
|
tsan
|
2017-06-28 18:10:48 +02:00
|
|
|
intOverflow
|
2018-11-20 01:03:58 +01:00
|
|
|
scs
|
2020-12-14 17:27:52 +01:00
|
|
|
Fuzzer
|
2021-11-01 15:27:54 +01:00
|
|
|
Memtag_heap
|
2022-08-31 22:57:03 +02:00
|
|
|
Memtag_stack
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
Memtag_globals
|
2021-07-07 22:41:50 +02:00
|
|
|
cfi // cfi is last to prevent it running before incompatible mutators
|
2016-01-06 23:41:07 +01:00
|
|
|
)
|
|
|
|
|
2021-07-07 22:41:50 +02:00
|
|
|
var Sanitizers = []SanitizerType{
|
|
|
|
Asan,
|
|
|
|
Hwasan,
|
|
|
|
tsan,
|
|
|
|
intOverflow,
|
|
|
|
scs,
|
|
|
|
Fuzzer,
|
2021-11-01 15:27:54 +01:00
|
|
|
Memtag_heap,
|
2022-08-31 22:57:03 +02:00
|
|
|
Memtag_stack,
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
Memtag_globals,
|
2021-07-07 22:41:50 +02:00
|
|
|
cfi, // cfi is last to prevent it running before incompatible mutators
|
|
|
|
}
|
|
|
|
|
2019-02-01 02:50:50 +01:00
|
|
|
// Name of the sanitizer variation for this sanitizer type
|
2020-12-14 17:27:52 +01:00
|
|
|
func (t SanitizerType) variationName() string {
|
2016-01-06 23:41:07 +01:00
|
|
|
switch t {
|
2020-12-14 17:27:52 +01:00
|
|
|
case Asan:
|
2016-01-06 23:41:07 +01:00
|
|
|
return "asan"
|
2021-04-01 20:29:09 +02:00
|
|
|
case Hwasan:
|
2018-08-03 01:19:13 +02:00
|
|
|
return "hwasan"
|
2016-01-06 23:41:07 +01:00
|
|
|
case tsan:
|
|
|
|
return "tsan"
|
2017-06-28 18:10:48 +02:00
|
|
|
case intOverflow:
|
|
|
|
return "intOverflow"
|
2017-11-01 10:20:21 +01:00
|
|
|
case cfi:
|
|
|
|
return "cfi"
|
2018-11-20 01:03:58 +01:00
|
|
|
case scs:
|
|
|
|
return "scs"
|
2021-11-01 15:27:54 +01:00
|
|
|
case Memtag_heap:
|
2020-04-29 00:09:12 +02:00
|
|
|
return "memtag_heap"
|
2022-08-31 22:57:03 +02:00
|
|
|
case Memtag_stack:
|
|
|
|
return "memtag_stack"
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
case Memtag_globals:
|
|
|
|
return "memtag_globals"
|
2020-12-14 17:27:52 +01:00
|
|
|
case Fuzzer:
|
2019-05-01 23:42:05 +02:00
|
|
|
return "fuzzer"
|
2016-01-06 23:41:07 +01:00
|
|
|
default:
|
2020-12-14 17:27:52 +01:00
|
|
|
panic(fmt.Errorf("unknown SanitizerType %d", t))
|
2016-01-06 23:41:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 02:50:50 +01:00
|
|
|
// This is the sanitizer names in SANITIZE_[TARGET|HOST]
|
2020-12-14 17:27:52 +01:00
|
|
|
func (t SanitizerType) name() string {
|
2019-02-01 02:50:50 +01:00
|
|
|
switch t {
|
2020-12-14 17:27:52 +01:00
|
|
|
case Asan:
|
2019-02-01 02:50:50 +01:00
|
|
|
return "address"
|
2021-04-01 20:29:09 +02:00
|
|
|
case Hwasan:
|
2019-02-01 02:50:50 +01:00
|
|
|
return "hwaddress"
|
2021-11-01 15:27:54 +01:00
|
|
|
case Memtag_heap:
|
2020-04-29 00:09:12 +02:00
|
|
|
return "memtag_heap"
|
2022-08-31 22:57:03 +02:00
|
|
|
case Memtag_stack:
|
|
|
|
return "memtag_stack"
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
case Memtag_globals:
|
|
|
|
return "memtag_globals"
|
2019-02-01 02:50:50 +01:00
|
|
|
case tsan:
|
|
|
|
return "thread"
|
|
|
|
case intOverflow:
|
|
|
|
return "integer_overflow"
|
|
|
|
case cfi:
|
|
|
|
return "cfi"
|
|
|
|
case scs:
|
|
|
|
return "shadow-call-stack"
|
2020-12-14 17:27:52 +01:00
|
|
|
case Fuzzer:
|
2019-05-01 23:42:05 +02:00
|
|
|
return "fuzzer"
|
2019-02-01 02:50:50 +01:00
|
|
|
default:
|
2020-12-14 17:27:52 +01:00
|
|
|
panic(fmt.Errorf("unknown SanitizerType %d", t))
|
2019-02-01 02:50:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 22:41:50 +02:00
|
|
|
func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
|
|
|
|
switch t {
|
2024-03-27 21:53:39 +01:00
|
|
|
case cfi, Hwasan, Asan, tsan, Fuzzer, scs, Memtag_stack:
|
2022-06-13 20:50:39 +02:00
|
|
|
sanitizer := &sanitizerSplitMutator{t}
|
|
|
|
ctx.TopDown(t.variationName()+"_markapexes", sanitizer.markSanitizableApexesMutator)
|
|
|
|
ctx.Transition(t.variationName(), sanitizer)
|
2024-03-27 21:53:39 +01:00
|
|
|
case Memtag_heap, Memtag_globals, intOverflow:
|
2021-07-07 22:41:50 +02:00
|
|
|
// do nothing
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unknown SanitizerType %d", t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-31 15:31:11 +01:00
|
|
|
// shouldPropagateToSharedLibraryDeps returns whether a sanitizer type should propagate to share
|
|
|
|
// dependencies. In most cases, sanitizers only propagate to static dependencies; however, some
|
|
|
|
// sanitizers also must be enabled for shared libraries for linking.
|
|
|
|
func (t SanitizerType) shouldPropagateToSharedLibraryDeps() bool {
|
|
|
|
switch t {
|
|
|
|
case Fuzzer:
|
|
|
|
// Typically, shared libs are not split. However, for fuzzer, we split even for shared libs
|
|
|
|
// because a library sanitized for fuzzer can't be linked from a library that isn't sanitized
|
|
|
|
// for fuzzer.
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2020-12-14 17:27:52 +01:00
|
|
|
func (*Module) SanitizerSupported(t SanitizerType) bool {
|
|
|
|
switch t {
|
|
|
|
case Asan:
|
|
|
|
return true
|
2021-04-01 20:29:09 +02:00
|
|
|
case Hwasan:
|
2020-12-14 17:27:52 +01:00
|
|
|
return true
|
|
|
|
case tsan:
|
|
|
|
return true
|
|
|
|
case intOverflow:
|
|
|
|
return true
|
|
|
|
case cfi:
|
|
|
|
return true
|
|
|
|
case scs:
|
|
|
|
return true
|
|
|
|
case Fuzzer:
|
|
|
|
return true
|
2021-11-01 15:27:54 +01:00
|
|
|
case Memtag_heap:
|
|
|
|
return true
|
2022-08-31 22:57:03 +02:00
|
|
|
case Memtag_stack:
|
|
|
|
return true
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
case Memtag_globals:
|
|
|
|
return true
|
2020-12-14 17:27:52 +01:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
|
|
|
|
func (t SanitizerType) incompatibleWithCfi() bool {
|
2021-04-01 20:29:09 +02:00
|
|
|
return t == Asan || t == Fuzzer || t == Hwasan
|
2019-07-29 14:27:18 +02:00
|
|
|
}
|
|
|
|
|
2020-09-15 03:32:35 +02:00
|
|
|
type SanitizeUserProps struct {
|
2021-06-25 21:19:27 +02:00
|
|
|
// Prevent use of any sanitizers on this module
|
2020-09-15 03:32:35 +02:00
|
|
|
Never *bool `android:"arch_variant"`
|
|
|
|
|
2021-06-25 21:19:27 +02:00
|
|
|
// ASan (Address sanitizer), incompatible with static binaries.
|
|
|
|
// Always runs in a diagnostic mode.
|
|
|
|
// Use of address sanitizer disables cfi sanitizer.
|
|
|
|
// Hwaddress sanitizer takes precedence over this sanitizer.
|
|
|
|
Address *bool `android:"arch_variant"`
|
|
|
|
// TSan (Thread sanitizer), incompatible with static binaries and 32 bit architectures.
|
|
|
|
// Always runs in a diagnostic mode.
|
|
|
|
// Use of thread sanitizer disables cfi and scudo sanitizers.
|
|
|
|
// Hwaddress sanitizer takes precedence over this sanitizer.
|
|
|
|
Thread *bool `android:"arch_variant"`
|
|
|
|
// HWASan (Hardware Address sanitizer).
|
|
|
|
// Use of hwasan sanitizer disables cfi, address, thread, and scudo sanitizers.
|
2020-09-15 03:32:35 +02:00
|
|
|
Hwaddress *bool `android:"arch_variant"`
|
|
|
|
|
2021-06-25 21:19:27 +02:00
|
|
|
// Undefined behavior sanitizer
|
|
|
|
All_undefined *bool `android:"arch_variant"`
|
|
|
|
// Subset of undefined behavior sanitizer
|
|
|
|
Undefined *bool `android:"arch_variant"`
|
|
|
|
// List of specific undefined behavior sanitizers to enable
|
|
|
|
Misc_undefined []string `android:"arch_variant"`
|
|
|
|
// Fuzzer, incompatible with static binaries.
|
|
|
|
Fuzzer *bool `android:"arch_variant"`
|
|
|
|
// safe-stack sanitizer, incompatible with 32-bit architectures.
|
|
|
|
Safestack *bool `android:"arch_variant"`
|
|
|
|
// cfi sanitizer, incompatible with asan, hwasan, fuzzer, or Darwin
|
|
|
|
Cfi *bool `android:"arch_variant"`
|
|
|
|
// signed/unsigned integer overflow sanitizer, incompatible with Darwin.
|
|
|
|
Integer_overflow *bool `android:"arch_variant"`
|
|
|
|
// scudo sanitizer, incompatible with asan, hwasan, tsan
|
|
|
|
// This should not be used in Android 11+ : https://source.android.com/devices/tech/debug/scudo
|
|
|
|
// deprecated
|
|
|
|
Scudo *bool `android:"arch_variant"`
|
2023-02-09 22:15:47 +01:00
|
|
|
// shadow-call-stack sanitizer, only available on arm64/riscv64.
|
2021-06-25 21:19:27 +02:00
|
|
|
Scs *bool `android:"arch_variant"`
|
|
|
|
// Memory-tagging, only available on arm64
|
|
|
|
// if diag.memtag unset or false, enables async memory tagging
|
2022-08-31 20:30:18 +02:00
|
|
|
Memtag_heap *bool `android:"arch_variant"`
|
2022-08-31 22:57:03 +02:00
|
|
|
// Memory-tagging stack instrumentation, only available on arm64
|
|
|
|
// Adds instrumentation to detect stack buffer overflows and use-after-scope using MTE.
|
|
|
|
Memtag_stack *bool `android:"arch_variant"`
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
// Memory-tagging globals instrumentation, only available on arm64
|
|
|
|
// Adds instrumentation to detect global buffer overflows using MTE.
|
|
|
|
Memtag_globals *bool `android:"arch_variant"`
|
2020-09-15 03:32:35 +02:00
|
|
|
|
|
|
|
// A modifier for ASAN and HWASAN for write only instrumentation
|
|
|
|
Writeonly *bool `android:"arch_variant"`
|
|
|
|
|
|
|
|
// Sanitizers to run in the diagnostic mode (as opposed to the release mode).
|
|
|
|
// Replaces abort() on error with a human-readable error message.
|
|
|
|
// Address and Thread sanitizers always run in diagnostic mode.
|
|
|
|
Diag struct {
|
2021-06-25 21:19:27 +02:00
|
|
|
// Undefined behavior sanitizer, diagnostic mode
|
|
|
|
Undefined *bool `android:"arch_variant"`
|
|
|
|
// cfi sanitizer, diagnostic mode, incompatible with asan, hwasan, fuzzer, or Darwin
|
|
|
|
Cfi *bool `android:"arch_variant"`
|
|
|
|
// signed/unsigned integer overflow sanitizer, diagnostic mode, incompatible with Darwin.
|
|
|
|
Integer_overflow *bool `android:"arch_variant"`
|
|
|
|
// Memory-tagging, only available on arm64
|
|
|
|
// requires sanitizer.memtag: true
|
|
|
|
// if set, enables sync memory tagging
|
|
|
|
Memtag_heap *bool `android:"arch_variant"`
|
|
|
|
// List of specific undefined behavior sanitizers to enable in diagnostic mode
|
|
|
|
Misc_undefined []string `android:"arch_variant"`
|
|
|
|
// List of sanitizers to pass to -fno-sanitize-recover
|
|
|
|
// results in only the first detected error for these sanitizers being reported and program then
|
|
|
|
// exits with a non-zero exit code.
|
|
|
|
No_recover []string `android:"arch_variant"`
|
2020-12-01 20:14:30 +01:00
|
|
|
} `android:"arch_variant"`
|
2020-09-15 03:32:35 +02:00
|
|
|
|
2020-11-16 17:41:00 +01:00
|
|
|
// Sanitizers to run with flag configuration specified
|
|
|
|
Config struct {
|
|
|
|
// Enables CFI support flags for assembly-heavy libraries
|
|
|
|
Cfi_assembly_support *bool `android:"arch_variant"`
|
2020-12-01 20:14:30 +01:00
|
|
|
} `android:"arch_variant"`
|
2020-11-16 17:41:00 +01:00
|
|
|
|
2021-06-25 21:19:27 +02:00
|
|
|
// List of sanitizers to pass to -fsanitize-recover
|
|
|
|
// allows execution to continue for these sanitizers to detect multiple errors rather than only
|
|
|
|
// the first one
|
2020-09-15 03:32:35 +02:00
|
|
|
Recover []string
|
|
|
|
|
2021-08-28 00:12:56 +02:00
|
|
|
// value to pass to -fsanitize-ignorelist
|
2020-09-15 03:32:35 +02:00
|
|
|
Blocklist *string
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
type sanitizeMutatedProperties struct {
|
|
|
|
// Whether sanitizers can be enabled on this module
|
|
|
|
Never *bool `blueprint:"mutated"`
|
|
|
|
|
|
|
|
// Whether ASan (Address sanitizer) is enabled for this module.
|
|
|
|
// Hwaddress sanitizer takes precedence over this sanitizer.
|
|
|
|
Address *bool `blueprint:"mutated"`
|
|
|
|
// Whether TSan (Thread sanitizer) is enabled for this module
|
|
|
|
Thread *bool `blueprint:"mutated"`
|
|
|
|
// Whether HWASan (Hardware Address sanitizer) is enabled for this module
|
|
|
|
Hwaddress *bool `blueprint:"mutated"`
|
|
|
|
|
|
|
|
// Whether Undefined behavior sanitizer is enabled for this module
|
|
|
|
All_undefined *bool `blueprint:"mutated"`
|
|
|
|
// Whether undefined behavior sanitizer subset is enabled for this module
|
|
|
|
Undefined *bool `blueprint:"mutated"`
|
|
|
|
// List of specific undefined behavior sanitizers enabled for this module
|
|
|
|
Misc_undefined []string `blueprint:"mutated"`
|
|
|
|
// Whether Fuzzeris enabled for this module
|
|
|
|
Fuzzer *bool `blueprint:"mutated"`
|
|
|
|
// whether safe-stack sanitizer is enabled for this module
|
|
|
|
Safestack *bool `blueprint:"mutated"`
|
|
|
|
// Whether cfi sanitizer is enabled for this module
|
|
|
|
Cfi *bool `blueprint:"mutated"`
|
|
|
|
// Whether signed/unsigned integer overflow sanitizer is enabled for this module
|
|
|
|
Integer_overflow *bool `blueprint:"mutated"`
|
|
|
|
// Whether scudo sanitizer is enabled for this module
|
|
|
|
Scudo *bool `blueprint:"mutated"`
|
|
|
|
// Whether shadow-call-stack sanitizer is enabled for this module.
|
|
|
|
Scs *bool `blueprint:"mutated"`
|
|
|
|
// Whether Memory-tagging is enabled for this module
|
|
|
|
Memtag_heap *bool `blueprint:"mutated"`
|
|
|
|
// Whether Memory-tagging stack instrumentation is enabled for this module
|
|
|
|
Memtag_stack *bool `blueprint:"mutated"`
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
// Whether Memory-tagging globals instrumentation is enabled for this module
|
|
|
|
Memtag_globals *bool `android:"arch_variant"`
|
2022-10-03 21:07:37 +02:00
|
|
|
|
|
|
|
// Whether a modifier for ASAN and HWASAN for write only instrumentation is enabled for this
|
|
|
|
// module
|
|
|
|
Writeonly *bool `blueprint:"mutated"`
|
|
|
|
|
|
|
|
// Sanitizers to run in the diagnostic mode (as opposed to the release mode).
|
|
|
|
Diag struct {
|
|
|
|
// Whether Undefined behavior sanitizer, diagnostic mode is enabled for this module
|
|
|
|
Undefined *bool `blueprint:"mutated"`
|
|
|
|
// Whether cfi sanitizer, diagnostic mode is enabled for this module
|
|
|
|
Cfi *bool `blueprint:"mutated"`
|
|
|
|
// Whether signed/unsigned integer overflow sanitizer, diagnostic mode is enabled for this
|
|
|
|
// module
|
|
|
|
Integer_overflow *bool `blueprint:"mutated"`
|
|
|
|
// Whether Memory-tagging, diagnostic mode is enabled for this module
|
|
|
|
Memtag_heap *bool `blueprint:"mutated"`
|
|
|
|
// List of specific undefined behavior sanitizers enabled in diagnostic mode
|
|
|
|
Misc_undefined []string `blueprint:"mutated"`
|
|
|
|
} `blueprint:"mutated"`
|
|
|
|
}
|
|
|
|
|
2020-09-15 03:32:35 +02:00
|
|
|
type SanitizeProperties struct {
|
2022-10-03 21:07:37 +02:00
|
|
|
Sanitize SanitizeUserProps `android:"arch_variant"`
|
|
|
|
SanitizeMutated sanitizeMutatedProperties `blueprint:"mutated"`
|
|
|
|
|
|
|
|
SanitizerEnabled bool `blueprint:"mutated"`
|
|
|
|
MinimalRuntimeDep bool `blueprint:"mutated"`
|
|
|
|
BuiltinsDep bool `blueprint:"mutated"`
|
|
|
|
UbsanRuntimeDep bool `blueprint:"mutated"`
|
|
|
|
InSanitizerDir bool `blueprint:"mutated"`
|
|
|
|
Sanitizers []string `blueprint:"mutated"`
|
|
|
|
DiagSanitizers []string `blueprint:"mutated"`
|
2016-01-06 23:41:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type sanitize struct {
|
|
|
|
Properties SanitizeProperties
|
|
|
|
}
|
|
|
|
|
2020-12-10 16:12:38 +01:00
|
|
|
// Mark this tag with a check to see if apex dependency check should be skipped
|
|
|
|
func (t libraryDependencyTag) SkipApexAllowedDependenciesCheck() bool {
|
|
|
|
return t.skipApexAllowedDependenciesCheck
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ android.SkipApexAllowedDependenciesCheck = (*libraryDependencyTag)(nil)
|
|
|
|
|
2017-11-17 20:08:10 +01:00
|
|
|
func init() {
|
2024-04-09 01:54:45 +02:00
|
|
|
pctx.StaticVariable("HostOnlySanitizeFlags", strings.Join(hostOnlySanitizeFlags, " "))
|
2023-08-02 22:24:29 +02:00
|
|
|
|
2017-11-17 20:08:10 +01:00
|
|
|
android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
|
2018-08-03 01:19:13 +02:00
|
|
|
android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
|
2024-03-27 21:53:39 +01:00
|
|
|
android.RegisterMakeVarsProvider(pctx, memtagStackMakeVarsProvider)
|
2024-05-07 07:47:35 +02:00
|
|
|
|
|
|
|
RegisterSanitizerLibrariesTxtType(android.InitRegistrationContext)
|
2017-11-17 20:08:10 +01:00
|
|
|
}
|
|
|
|
|
2016-01-06 23:41:07 +01:00
|
|
|
func (sanitize *sanitize) props() []interface{} {
|
|
|
|
return []interface{}{&sanitize.Properties}
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
func (p *sanitizeMutatedProperties) copyUserPropertiesToMutated(userProps *SanitizeUserProps) {
|
|
|
|
p.Never = userProps.Never
|
|
|
|
p.Address = userProps.Address
|
|
|
|
p.All_undefined = userProps.All_undefined
|
|
|
|
p.Cfi = userProps.Cfi
|
|
|
|
p.Fuzzer = userProps.Fuzzer
|
|
|
|
p.Hwaddress = userProps.Hwaddress
|
|
|
|
p.Integer_overflow = userProps.Integer_overflow
|
|
|
|
p.Memtag_heap = userProps.Memtag_heap
|
|
|
|
p.Memtag_stack = userProps.Memtag_stack
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
p.Memtag_globals = userProps.Memtag_globals
|
2022-10-03 21:07:37 +02:00
|
|
|
p.Safestack = userProps.Safestack
|
|
|
|
p.Scs = userProps.Scs
|
|
|
|
p.Scudo = userProps.Scudo
|
|
|
|
p.Thread = userProps.Thread
|
|
|
|
p.Undefined = userProps.Undefined
|
|
|
|
p.Writeonly = userProps.Writeonly
|
|
|
|
|
|
|
|
p.Misc_undefined = make([]string, 0, len(userProps.Misc_undefined))
|
|
|
|
for _, v := range userProps.Misc_undefined {
|
|
|
|
p.Misc_undefined = append(p.Misc_undefined, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Diag.Cfi = userProps.Diag.Cfi
|
|
|
|
p.Diag.Integer_overflow = userProps.Diag.Integer_overflow
|
|
|
|
p.Diag.Memtag_heap = userProps.Diag.Memtag_heap
|
|
|
|
p.Diag.Undefined = userProps.Diag.Undefined
|
|
|
|
|
|
|
|
p.Diag.Misc_undefined = make([]string, 0, len(userProps.Diag.Misc_undefined))
|
|
|
|
for _, v := range userProps.Diag.Misc_undefined {
|
|
|
|
p.Diag.Misc_undefined = append(p.Diag.Misc_undefined, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 23:41:07 +01:00
|
|
|
func (sanitize *sanitize) begin(ctx BaseModuleContext) {
|
2022-10-03 21:07:37 +02:00
|
|
|
s := &sanitize.Properties.SanitizeMutated
|
|
|
|
s.copyUserPropertiesToMutated(&sanitize.Properties.Sanitize)
|
2016-07-07 19:54:07 +02:00
|
|
|
|
2016-01-06 23:41:07 +01:00
|
|
|
// Don't apply sanitizers to NDK code.
|
2017-09-28 02:01:44 +02:00
|
|
|
if ctx.useSdk() {
|
2017-11-07 19:57:05 +01:00
|
|
|
s.Never = BoolPtr(true)
|
2016-01-06 23:41:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Never always wins.
|
2017-11-07 19:57:05 +01:00
|
|
|
if Bool(s.Never) {
|
2016-01-06 23:41:07 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-31 22:57:03 +02:00
|
|
|
// cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap: false}).
|
2021-06-22 22:57:27 +02:00
|
|
|
if ctx.testBinary() {
|
|
|
|
if s.Memtag_heap == nil {
|
|
|
|
s.Memtag_heap = proptools.BoolPtr(true)
|
|
|
|
}
|
|
|
|
if s.Diag.Memtag_heap == nil {
|
|
|
|
s.Diag.Memtag_heap = proptools.BoolPtr(true)
|
|
|
|
}
|
2021-01-13 03:28:33 +01:00
|
|
|
}
|
|
|
|
|
2016-01-06 23:41:07 +01:00
|
|
|
var globalSanitizers []string
|
2017-06-28 18:10:48 +02:00
|
|
|
var globalSanitizersDiag []string
|
|
|
|
|
2018-10-08 05:54:34 +02:00
|
|
|
if ctx.Host() {
|
|
|
|
if !ctx.Windows() {
|
|
|
|
globalSanitizers = ctx.Config().SanitizeHost()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
arches := ctx.Config().SanitizeDeviceArch()
|
|
|
|
if len(arches) == 0 || inList(ctx.Arch().ArchType.Name, arches) {
|
|
|
|
globalSanitizers = ctx.Config().SanitizeDevice()
|
|
|
|
globalSanitizersDiag = ctx.Config().SanitizeDeviceDiag()
|
2016-01-06 23:41:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-07 19:38:41 +02:00
|
|
|
if len(globalSanitizers) > 0 {
|
|
|
|
var found bool
|
2016-07-07 19:54:07 +02:00
|
|
|
if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.All_undefined = proptools.BoolPtr(true)
|
2016-07-07 19:54:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Undefined = proptools.BoolPtr(true)
|
2016-07-07 19:38:41 +02:00
|
|
|
}
|
2016-01-06 23:41:07 +01:00
|
|
|
|
2019-05-01 23:42:05 +02:00
|
|
|
if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Address = proptools.BoolPtr(true)
|
2016-07-07 19:38:41 +02:00
|
|
|
}
|
2016-01-06 23:41:07 +01:00
|
|
|
|
2016-07-07 19:54:07 +02:00
|
|
|
if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Thread = proptools.BoolPtr(true)
|
2016-07-07 19:38:41 +02:00
|
|
|
}
|
2016-05-12 22:54:53 +02:00
|
|
|
|
2019-05-01 23:42:05 +02:00
|
|
|
if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Fuzzer = proptools.BoolPtr(true)
|
2016-07-07 19:38:41 +02:00
|
|
|
}
|
2016-05-19 01:39:54 +02:00
|
|
|
|
2016-07-07 19:54:07 +02:00
|
|
|
if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Safestack = proptools.BoolPtr(true)
|
2016-07-07 19:38:41 +02:00
|
|
|
}
|
2016-05-19 01:39:54 +02:00
|
|
|
|
2016-08-17 00:39:54 +02:00
|
|
|
if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
|
2017-11-29 09:27:14 +01:00
|
|
|
if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Cfi = proptools.BoolPtr(true)
|
2017-10-31 10:26:14 +01:00
|
|
|
}
|
2016-08-17 00:39:54 +02:00
|
|
|
}
|
|
|
|
|
2018-03-13 18:41:07 +01:00
|
|
|
// Global integer_overflow builds do not support static libraries.
|
2017-06-28 18:10:48 +02:00
|
|
|
if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
|
2018-03-13 18:41:07 +01:00
|
|
|
if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Integer_overflow = proptools.BoolPtr(true)
|
2017-07-13 23:46:05 +02:00
|
|
|
}
|
2017-06-28 18:10:48 +02:00
|
|
|
}
|
|
|
|
|
2018-06-12 23:46:54 +02:00
|
|
|
if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Scudo = proptools.BoolPtr(true)
|
2018-06-12 23:46:54 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 01:19:13 +02:00
|
|
|
if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
|
2023-08-22 19:51:44 +02:00
|
|
|
if !ctx.Config().HWASanDisabledForPath(ctx.ModuleDir()) {
|
|
|
|
s.Hwaddress = proptools.BoolPtr(true)
|
|
|
|
}
|
2018-08-03 01:19:13 +02:00
|
|
|
}
|
|
|
|
|
2020-07-24 00:58:17 +02:00
|
|
|
if found, globalSanitizers = removeFromList("writeonly", globalSanitizers); found && s.Writeonly == nil {
|
|
|
|
// Hwaddress and Address are set before, so we can check them here
|
|
|
|
// If they aren't explicitly set in the blueprint/SANITIZE_(HOST|TARGET), they would be nil instead of false
|
|
|
|
if s.Address == nil && s.Hwaddress == nil {
|
|
|
|
ctx.ModuleErrorf("writeonly modifier cannot be used without 'address' or 'hwaddress'")
|
|
|
|
}
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Writeonly = proptools.BoolPtr(true)
|
2020-07-24 00:58:17 +02:00
|
|
|
}
|
2020-04-29 00:09:12 +02:00
|
|
|
if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
|
2021-01-06 01:41:26 +01:00
|
|
|
if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Memtag_heap = proptools.BoolPtr(true)
|
2021-01-06 01:41:26 +01:00
|
|
|
}
|
2020-04-29 00:09:12 +02:00
|
|
|
}
|
2020-07-24 00:58:17 +02:00
|
|
|
|
2022-08-31 22:57:03 +02:00
|
|
|
if found, globalSanitizers = removeFromList("memtag_stack", globalSanitizers); found && s.Memtag_stack == nil {
|
|
|
|
s.Memtag_stack = proptools.BoolPtr(true)
|
|
|
|
}
|
|
|
|
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
if found, globalSanitizers = removeFromList("memtag_globals", globalSanitizers); found && s.Memtag_globals == nil {
|
|
|
|
s.Memtag_globals = proptools.BoolPtr(true)
|
|
|
|
}
|
|
|
|
|
2016-07-07 19:38:41 +02:00
|
|
|
if len(globalSanitizers) > 0 {
|
|
|
|
ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
|
|
|
|
}
|
2017-06-28 18:10:48 +02:00
|
|
|
|
2018-03-13 18:41:07 +01:00
|
|
|
// Global integer_overflow builds do not support static library diagnostics.
|
2017-06-28 18:10:48 +02:00
|
|
|
if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
|
2018-03-13 18:41:07 +01:00
|
|
|
s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Diag.Integer_overflow = proptools.BoolPtr(true)
|
2017-06-28 18:10:48 +02:00
|
|
|
}
|
|
|
|
|
2017-10-31 10:26:14 +01:00
|
|
|
if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
|
|
|
|
s.Diag.Cfi == nil && Bool(s.Cfi) {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Diag.Cfi = proptools.BoolPtr(true)
|
2017-10-31 10:26:14 +01:00
|
|
|
}
|
|
|
|
|
2021-01-13 03:28:33 +01:00
|
|
|
if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
|
|
|
|
s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Diag.Memtag_heap = proptools.BoolPtr(true)
|
2017-06-28 18:10:48 +02:00
|
|
|
}
|
2016-07-19 00:44:56 +02:00
|
|
|
|
2021-01-13 03:28:33 +01:00
|
|
|
if len(globalSanitizersDiag) > 0 {
|
|
|
|
ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
|
2021-01-06 01:41:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable Memtag for all components in the include paths (for Aarch64 only)
|
2022-06-23 23:51:20 +02:00
|
|
|
if ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
|
2021-01-06 01:41:26 +01:00
|
|
|
if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
|
2021-01-13 03:28:33 +01:00
|
|
|
if s.Memtag_heap == nil {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Memtag_heap = proptools.BoolPtr(true)
|
2021-01-13 03:28:33 +01:00
|
|
|
}
|
|
|
|
if s.Diag.Memtag_heap == nil {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Diag.Memtag_heap = proptools.BoolPtr(true)
|
2021-01-13 03:28:33 +01:00
|
|
|
}
|
2021-01-06 01:41:26 +01:00
|
|
|
} else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
|
2021-01-13 03:28:33 +01:00
|
|
|
if s.Memtag_heap == nil {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Memtag_heap = proptools.BoolPtr(true)
|
2021-01-13 03:28:33 +01:00
|
|
|
}
|
2021-01-06 01:41:26 +01:00
|
|
|
}
|
2020-04-29 00:09:12 +02:00
|
|
|
}
|
|
|
|
|
2023-03-17 06:17:22 +01:00
|
|
|
// Enable HWASan for all components in the include paths (for Aarch64 only)
|
|
|
|
if s.Hwaddress == nil && ctx.Config().HWASanEnabledForPath(ctx.ModuleDir()) &&
|
|
|
|
ctx.Arch().ArchType == android.Arm64 && ctx.toolchain().Bionic() {
|
|
|
|
s.Hwaddress = proptools.BoolPtr(true)
|
|
|
|
}
|
|
|
|
|
2021-06-24 19:15:17 +02:00
|
|
|
// Enable CFI for non-host components in the include paths
|
|
|
|
if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && !ctx.Host() {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Cfi = proptools.BoolPtr(true)
|
2018-03-30 04:55:23 +02:00
|
|
|
if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Diag.Cfi = proptools.BoolPtr(true)
|
2017-10-31 10:26:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 01:55:28 +01:00
|
|
|
// Is CFI actually enabled?
|
|
|
|
if !ctx.Config().EnableCFI() {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Cfi = nil
|
|
|
|
s.Diag.Cfi = nil
|
2017-01-19 22:54:55 +01:00
|
|
|
}
|
|
|
|
|
2018-08-03 01:19:13 +02:00
|
|
|
// HWASan requires AArch64 hardware feature (top-byte-ignore).
|
2022-06-23 23:51:20 +02:00
|
|
|
if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() {
|
2018-08-03 01:19:13 +02:00
|
|
|
s.Hwaddress = nil
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:15:47 +01:00
|
|
|
// SCS is only implemented on AArch64/riscv64.
|
|
|
|
if (ctx.Arch().ArchType != android.Arm64 && ctx.Arch().ArchType != android.Riscv64) || !ctx.toolchain().Bionic() {
|
2018-11-20 01:03:58 +01:00
|
|
|
s.Scs = nil
|
|
|
|
}
|
|
|
|
|
2021-11-01 15:27:54 +01:00
|
|
|
// Memtag_heap is only implemented on AArch64.
|
2022-08-31 22:57:03 +02:00
|
|
|
// Memtag ABI is Android specific for now, so disable for host.
|
|
|
|
if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() || ctx.Host() {
|
2020-04-29 00:09:12 +02:00
|
|
|
s.Memtag_heap = nil
|
2022-08-31 22:57:03 +02:00
|
|
|
s.Memtag_stack = nil
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
s.Memtag_globals = nil
|
2020-04-29 00:09:12 +02:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:42:52 +02:00
|
|
|
// Also disable CFI if ASAN is enabled.
|
2018-08-03 01:19:13 +02:00
|
|
|
if Bool(s.Address) || Bool(s.Hwaddress) {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Cfi = nil
|
|
|
|
s.Diag.Cfi = nil
|
2022-08-31 22:57:03 +02:00
|
|
|
// HWASAN and ASAN win against MTE.
|
|
|
|
s.Memtag_heap = nil
|
|
|
|
s.Memtag_stack = nil
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
s.Memtag_globals = nil
|
2017-04-20 16:42:52 +02:00
|
|
|
}
|
|
|
|
|
2022-02-07 22:55:55 +01:00
|
|
|
// Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
|
|
|
|
if !ctx.Os().Linux() {
|
2021-06-25 20:53:40 +02:00
|
|
|
s.Cfi = nil
|
|
|
|
s.Diag.Cfi = nil
|
2018-03-13 18:41:07 +01:00
|
|
|
s.Misc_undefined = nil
|
|
|
|
s.Undefined = nil
|
|
|
|
s.All_undefined = nil
|
|
|
|
s.Integer_overflow = nil
|
2017-11-17 20:08:10 +01:00
|
|
|
}
|
|
|
|
|
2022-02-07 22:55:55 +01:00
|
|
|
// Disable CFI for musl
|
|
|
|
if ctx.toolchain().Musl() {
|
|
|
|
s.Cfi = nil
|
|
|
|
s.Diag.Cfi = nil
|
|
|
|
}
|
|
|
|
|
2023-05-02 22:02:51 +02:00
|
|
|
// TODO(b/280478629): runtimes don't exist for musl arm64 yet.
|
|
|
|
if ctx.toolchain().Musl() && ctx.Arch().ArchType == android.Arm64 {
|
|
|
|
s.Address = nil
|
|
|
|
s.Hwaddress = nil
|
|
|
|
s.Thread = nil
|
|
|
|
s.Scudo = nil
|
|
|
|
s.Fuzzer = nil
|
|
|
|
s.Cfi = nil
|
|
|
|
s.Diag.Cfi = nil
|
|
|
|
s.Misc_undefined = nil
|
|
|
|
s.Undefined = nil
|
|
|
|
s.All_undefined = nil
|
|
|
|
s.Integer_overflow = nil
|
|
|
|
}
|
|
|
|
|
2024-03-27 21:53:39 +01:00
|
|
|
if ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery() {
|
|
|
|
// HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
|
|
|
|
// Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
|
|
|
|
if !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
|
|
|
|
s.Hwaddress = nil
|
|
|
|
}
|
|
|
|
// Memtag stack in ramdisk makes pKVM unhappy.
|
|
|
|
s.Memtag_stack = nil
|
2018-08-03 01:19:13 +02:00
|
|
|
}
|
|
|
|
|
2016-07-19 00:44:56 +02:00
|
|
|
if ctx.staticBinary() {
|
|
|
|
s.Address = nil
|
2019-05-01 23:42:05 +02:00
|
|
|
s.Fuzzer = nil
|
2016-07-19 00:44:56 +02:00
|
|
|
s.Thread = nil
|
2016-01-06 23:41:07 +01:00
|
|
|
}
|
|
|
|
|
2016-07-07 19:54:07 +02:00
|
|
|
if Bool(s.All_undefined) {
|
|
|
|
s.Undefined = nil
|
|
|
|
}
|
|
|
|
|
2016-05-12 22:54:53 +02:00
|
|
|
if !ctx.toolchain().Is64Bit() {
|
|
|
|
// TSAN and SafeStack are not supported on 32-bit architectures
|
2016-07-07 19:54:07 +02:00
|
|
|
s.Thread = nil
|
|
|
|
s.Safestack = nil
|
2016-01-06 23:41:07 +01:00
|
|
|
// TODO(ccross): error for compile_multilib = "32"?
|
|
|
|
}
|
|
|
|
|
2017-01-28 00:44:44 +01:00
|
|
|
if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) ||
|
2019-05-01 23:42:05 +02:00
|
|
|
Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap) || Bool(s.Memtag_stack) ||
|
|
|
|
Bool(s.Memtag_globals)) {
|
2016-07-19 00:44:56 +02:00
|
|
|
sanitize.Properties.SanitizerEnabled = true
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:42:56 +01:00
|
|
|
// Disable Scudo if ASan or TSan is enabled, or if it's disabled globally.
|
|
|
|
if Bool(s.Address) || Bool(s.Thread) || Bool(s.Hwaddress) || ctx.Config().DisableScudo() {
|
2018-06-12 23:46:54 +02:00
|
|
|
s.Scudo = nil
|
|
|
|
}
|
|
|
|
|
2018-08-03 01:19:13 +02:00
|
|
|
if Bool(s.Hwaddress) {
|
|
|
|
s.Address = nil
|
|
|
|
s.Thread = nil
|
|
|
|
}
|
2022-03-02 02:25:22 +01:00
|
|
|
|
|
|
|
// TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
|
|
|
|
// mutually incompatible.
|
|
|
|
if Bool(s.Fuzzer) {
|
|
|
|
s.Cfi = nil
|
|
|
|
}
|
2016-01-06 23:41:07 +01:00
|
|
|
}
|
|
|
|
|
2018-11-15 23:01:36 +01:00
|
|
|
func toDisableImplicitIntegerChange(flags []string) bool {
|
|
|
|
// Returns true if any flag is fsanitize*integer, and there is
|
|
|
|
// no explicit flag about sanitize=implicit-integer-sign-change.
|
|
|
|
for _, f := range flags {
|
|
|
|
if strings.Contains(f, "sanitize=implicit-integer-sign-change") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, f := range flags {
|
|
|
|
if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-01 00:47:45 +01:00
|
|
|
func toDisableUnsignedShiftBaseChange(flags []string) bool {
|
|
|
|
// Returns true if any flag is fsanitize*integer, and there is
|
|
|
|
// no explicit flag about sanitize=unsigned-shift-base.
|
|
|
|
for _, f := range flags {
|
|
|
|
if strings.Contains(f, "sanitize=unsigned-shift-base") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, f := range flags {
|
|
|
|
if strings.HasPrefix(f, "-fsanitize") && strings.Contains(f, "integer") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
func (s *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
|
|
|
|
if !s.Properties.SanitizerEnabled && !s.Properties.UbsanRuntimeDep {
|
2016-01-06 23:41:07 +01:00
|
|
|
return flags
|
|
|
|
}
|
2022-10-03 21:07:37 +02:00
|
|
|
sanProps := &s.Properties.SanitizeMutated
|
2016-01-06 23:41:07 +01:00
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Address) {
|
2016-05-19 00:37:25 +02:00
|
|
|
if ctx.Arch().ArchType == android.Arm {
|
2016-01-06 23:41:07 +01:00
|
|
|
// Frame pointer based unwinder in ASan requires ARM frame setup.
|
|
|
|
// TODO: put in flags?
|
|
|
|
flags.RequiredInstructionSet = "arm"
|
|
|
|
}
|
2019-11-04 18:37:55 +01:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
|
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
|
2016-01-06 23:41:07 +01:00
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Writeonly) {
|
2020-07-24 00:58:17 +02:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
|
|
|
|
}
|
|
|
|
|
2016-01-06 23:41:07 +01:00
|
|
|
if ctx.Host() {
|
|
|
|
// -nodefaultlibs (provided with libc++) prevents the driver from linking
|
|
|
|
// libraries needed with -fsanitize=address. http://b/18650275 (WAI)
|
2019-11-04 18:37:55 +01:00
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
|
2016-01-06 23:41:07 +01:00
|
|
|
} else {
|
2019-11-04 18:37:55 +01:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
|
2019-02-02 05:13:38 +01:00
|
|
|
if ctx.bootstrap() {
|
|
|
|
flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
|
|
|
|
} else {
|
|
|
|
flags.DynamicLinker = "/system/bin/linker_asan"
|
|
|
|
}
|
2016-01-06 23:41:07 +01:00
|
|
|
if flags.Toolchain.Is64Bit() {
|
|
|
|
flags.DynamicLinker += "64"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Hwaddress) {
|
2019-11-04 18:37:55 +01:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
|
2021-11-04 09:14:14 +01:00
|
|
|
|
|
|
|
for _, flag := range hwasanCommonflags {
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", flag)
|
|
|
|
}
|
|
|
|
for _, flag := range hwasanCommonflags {
|
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,"+flag)
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Writeonly) {
|
2020-07-24 00:58:17 +02:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
|
|
|
|
}
|
2023-03-24 01:48:07 +01:00
|
|
|
if !ctx.staticBinary() && !ctx.Host() {
|
|
|
|
if ctx.bootstrap() {
|
|
|
|
flags.DynamicLinker = "/system/bin/bootstrap/linker_hwasan64"
|
|
|
|
} else {
|
|
|
|
flags.DynamicLinker = "/system/bin/linker_hwasan64"
|
|
|
|
}
|
|
|
|
}
|
2017-10-20 00:52:11 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Fuzzer) {
|
2019-11-04 18:37:55 +01:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
|
2019-05-01 23:42:05 +02:00
|
|
|
|
2022-03-02 02:25:22 +01:00
|
|
|
// TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
|
|
|
|
_, flags.Local.LdFlags = removeFromList("-flto", flags.Local.LdFlags)
|
|
|
|
_, flags.Local.CFlags = removeFromList("-flto", flags.Local.CFlags)
|
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-lto")
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fno-lto")
|
|
|
|
|
Workaround unexported sancov symbols. Fix multiple sanitizer RT deps.
Fuzz targets currently have dependencies on multiple libclang_rt runtime
libraries when building with ASan/HWAsan on device. This is an error.
This happens as Soong adds the dependency on the ASan/HWASan shared
runtime library. These libraries should provide the required UBSan
components. The clang driver was previously being passed
-fsanitize=fuzzer-no-link at link time, and as it doesn't know about the
already-established dependency on ASan/HWASan, it mistakenly thinks that
there is not runtime providing the UBSan components.
This patch fixes that problem by not adding -fsanitize=fuzzer-no-link to
the link-time flags.
This revealed a underlying issue in the upstream runtime compilation.
Android uses emulated TLS, which changes the symbol names from
<my_symbol_name> to __emutls_v._<my_symbol_name>. In particular, this
fails to account for the '__sancov_lowest_stack' symbol, as it no longer
matches the linker script rule for '__sancov*', and the symbol is no
longer exported in the shared library variant of ASan/HWASan.
This patch works around the discovered issue, which is being tracked in
the linked bug. It disables stack depth instrumentation, and we no
longer depend on this symbol. This means we get a missing sanitizer
coverage feature when fuzzing, but shouldn't be too detrimental.
Bug: 142430592
Test: SANITIZE_TARGET=hwaddress m example_fuzzer && \
readelf -d example_fuzzer # ensure only ONE libclang_rt dep (in this
case, hwasan)
Change-Id: Iea6df55d592a801732511c9b690134367429d62a
2019-10-10 02:18:59 +02:00
|
|
|
// TODO(b/142430592): Upstream linker scripts for sanitizer runtime libraries
|
|
|
|
// discard the sancov_lowest_stack symbol, because it's emulated TLS (and thus
|
|
|
|
// doesn't match the linker script due to the "__emutls_v." prefix).
|
2019-11-04 18:37:55 +01:00
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, "-fno-sanitize-coverage=stack-depth")
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-coverage=stack-depth")
|
Workaround unexported sancov symbols. Fix multiple sanitizer RT deps.
Fuzz targets currently have dependencies on multiple libclang_rt runtime
libraries when building with ASan/HWAsan on device. This is an error.
This happens as Soong adds the dependency on the ASan/HWASan shared
runtime library. These libraries should provide the required UBSan
components. The clang driver was previously being passed
-fsanitize=fuzzer-no-link at link time, and as it doesn't know about the
already-established dependency on ASan/HWASan, it mistakenly thinks that
there is not runtime providing the UBSan components.
This patch fixes that problem by not adding -fsanitize=fuzzer-no-link to
the link-time flags.
This revealed a underlying issue in the upstream runtime compilation.
Android uses emulated TLS, which changes the symbol names from
<my_symbol_name> to __emutls_v._<my_symbol_name>. In particular, this
fails to account for the '__sancov_lowest_stack' symbol, as it no longer
matches the linker script rule for '__sancov*', and the symbol is no
longer exported in the shared library variant of ASan/HWASan.
This patch works around the discovered issue, which is being tracked in
the linked bug. It disables stack depth instrumentation, and we no
longer depend on this symbol. This means we get a missing sanitizer
coverage feature when fuzzing, but shouldn't be too detrimental.
Bug: 142430592
Test: SANITIZE_TARGET=hwaddress m example_fuzzer && \
readelf -d example_fuzzer # ensure only ONE libclang_rt dep (in this
case, hwasan)
Change-Id: Iea6df55d592a801732511c9b690134367429d62a
2019-10-10 02:18:59 +02:00
|
|
|
|
2019-08-28 21:41:07 +02:00
|
|
|
// Disable fortify for fuzzing builds. Generally, we'll be building with
|
|
|
|
// UBSan or ASan here and the fortify checks pollute the stack traces.
|
2019-11-04 18:37:55 +01:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
|
2019-12-10 17:44:52 +01:00
|
|
|
|
|
|
|
// Build fuzzer-sanitized libraries with an $ORIGIN DT_RUNPATH. Android's
|
|
|
|
// linker uses DT_RUNPATH, not DT_RPATH. When we deploy cc_fuzz targets and
|
|
|
|
// their libraries to /data/fuzz/<arch>/lib, any transient shared library gets
|
|
|
|
// the DT_RUNPATH from the shared library above it, and not the executable,
|
|
|
|
// meaning that the lookup falls back to the system. Adding the $ORIGIN to the
|
|
|
|
// DT_RUNPATH here means that transient shared libraries can be found
|
|
|
|
// colocated with their parents.
|
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
|
2016-01-06 23:41:07 +01:00
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Cfi) {
|
2017-01-20 23:13:06 +01:00
|
|
|
if ctx.Arch().ArchType == android.Arm {
|
|
|
|
// __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). LLVM is not set up
|
|
|
|
// to do this on a function basis, so force Thumb on the entire module.
|
|
|
|
flags.RequiredInstructionSet = "thumb"
|
|
|
|
}
|
2017-11-01 10:20:21 +01:00
|
|
|
|
2019-11-04 18:37:55 +01:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
|
|
|
|
flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
|
2024-05-03 18:10:04 +02:00
|
|
|
flags.CFlagsDeps = append(flags.CFlagsDeps, android.PathForSource(ctx, cfiBlocklistPath+"/"+cfiBlocklistFilename))
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(s.Properties.Sanitize.Config.Cfi_assembly_support) {
|
2023-03-22 21:22:27 +01:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, cfiAssemblySupportFlag)
|
2020-11-16 17:41:00 +01:00
|
|
|
}
|
2017-11-01 10:20:21 +01:00
|
|
|
// Only append the default visibility flag if -fvisibility has not already been set
|
|
|
|
// to hidden.
|
2019-11-04 18:37:55 +01:00
|
|
|
if !inList("-fvisibility=hidden", flags.Local.CFlags) {
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
|
2017-11-01 10:20:21 +01:00
|
|
|
}
|
2019-11-04 18:37:55 +01:00
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
|
2017-11-01 10:20:21 +01:00
|
|
|
|
|
|
|
if ctx.staticBinary() {
|
2019-11-04 18:37:55 +01:00
|
|
|
_, flags.Local.CFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.CFlags)
|
|
|
|
_, flags.Local.LdFlags = removeFromList("-fsanitize-cfi-cross-dso", flags.Local.LdFlags)
|
2017-11-01 10:20:21 +01:00
|
|
|
}
|
2016-08-17 00:39:54 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Memtag_stack) {
|
2022-08-31 22:57:03 +02:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, memtagStackCommonFlags...)
|
|
|
|
flags.Local.AsFlags = append(flags.Local.AsFlags, memtagStackCommonFlags...)
|
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, memtagStackCommonFlags...)
|
2024-05-01 02:34:58 +02:00
|
|
|
|
|
|
|
for _, flag := range memtagStackLlvmFlags {
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", flag)
|
|
|
|
}
|
|
|
|
for _, flag := range memtagStackLlvmFlags {
|
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,"+flag)
|
|
|
|
}
|
2022-08-31 22:57:03 +02:00
|
|
|
}
|
|
|
|
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
if (Bool(sanProps.Memtag_heap) || Bool(sanProps.Memtag_stack) || Bool(sanProps.Memtag_globals)) && ctx.binary() {
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Diag.Memtag_heap) {
|
2022-08-31 22:57:03 +02:00
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=sync")
|
|
|
|
} else {
|
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=async")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Integer_overflow) {
|
2019-11-04 18:37:55 +01:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
|
2017-06-28 18:10:48 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if len(s.Properties.Sanitizers) > 0 {
|
|
|
|
sanitizeArg := "-fsanitize=" + strings.Join(s.Properties.Sanitizers, ",")
|
2019-11-04 18:37:55 +01:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
|
|
|
|
flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
|
2022-02-07 22:49:03 +01:00
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
|
|
|
|
|
2022-02-07 22:55:55 +01:00
|
|
|
if ctx.toolchain().Bionic() || ctx.toolchain().Musl() {
|
|
|
|
// Bionic and musl sanitizer runtimes have already been added as dependencies so that
|
|
|
|
// the right variant of the runtime will be used (with the "-android" or "-musl"
|
|
|
|
// suffixes), so don't let clang the runtime library.
|
2023-08-02 22:24:29 +02:00
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, noSanitizeLinkRuntimeFlag)
|
2022-02-07 22:49:03 +01:00
|
|
|
} else {
|
2017-01-28 00:44:44 +01:00
|
|
|
// Host sanitizers only link symbols in the final executable, so
|
|
|
|
// there will always be undefined symbols in intermediate libraries.
|
2019-11-04 18:37:55 +01:00
|
|
|
_, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
|
2022-06-03 00:11:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !ctx.toolchain().Bionic() {
|
|
|
|
// non-Bionic toolchain prebuilts are missing UBSan's vptr and function san.
|
|
|
|
// Musl toolchain prebuilts have vptr and function sanitizers, but enabling them
|
|
|
|
// implicitly enables RTTI which causes RTTI mismatch issues with dependencies.
|
2020-02-19 21:24:02 +01:00
|
|
|
|
2022-02-07 22:49:03 +01:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
|
2020-02-19 21:24:02 +01:00
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Fuzzer) {
|
2019-05-01 23:42:05 +02:00
|
|
|
// When fuzzing, we wish to crash with diagnostics on any bug.
|
2019-11-04 18:37:55 +01:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
|
2019-05-01 23:42:05 +02:00
|
|
|
} else if ctx.Host() {
|
2023-01-19 17:02:47 +01:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, hostOnlySanitizeFlags...)
|
2019-05-01 23:42:05 +02:00
|
|
|
} else {
|
2023-01-19 17:02:47 +01:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, deviceOnlySanitizeFlags...)
|
2019-05-01 23:42:05 +02:00
|
|
|
}
|
2022-06-24 20:09:18 +02:00
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if enableMinimalRuntime(s) {
|
2022-06-24 20:09:18 +02:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
|
|
|
|
}
|
|
|
|
|
2018-11-15 23:01:36 +01:00
|
|
|
// http://b/119329758, Android core does not boot up with this sanitizer yet.
|
2019-11-04 18:37:55 +01:00
|
|
|
if toDisableImplicitIntegerChange(flags.Local.CFlags) {
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
|
2018-11-15 23:01:36 +01:00
|
|
|
}
|
2020-12-01 00:47:45 +01:00
|
|
|
// http://b/171275751, Android doesn't build with this sanitizer yet.
|
|
|
|
if toDisableUnsignedShiftBaseChange(flags.Local.CFlags) {
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=unsigned-shift-base")
|
|
|
|
}
|
2016-01-06 23:41:07 +01:00
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if len(s.Properties.DiagSanitizers) > 0 {
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(s.Properties.DiagSanitizers, ","))
|
2016-08-17 00:39:54 +02:00
|
|
|
}
|
|
|
|
// FIXME: enable RTTI if diag + (cfi or vptr)
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if s.Properties.Sanitize.Recover != nil {
|
2019-11-04 18:37:55 +01:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
|
2022-10-03 21:07:37 +02:00
|
|
|
strings.Join(s.Properties.Sanitize.Recover, ","))
|
2017-05-08 22:15:23 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if s.Properties.Sanitize.Diag.No_recover != nil {
|
2019-11-04 18:37:55 +01:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
|
2022-10-03 21:07:37 +02:00
|
|
|
strings.Join(s.Properties.Sanitize.Diag.No_recover, ","))
|
2018-12-12 18:36:31 +01:00
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
blocklist := android.OptionalPathForModuleSrc(ctx, s.Properties.Sanitize.Blocklist)
|
2020-07-27 20:49:51 +02:00
|
|
|
if blocklist.Valid() {
|
2023-06-12 21:18:28 +02:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, sanitizeIgnorelistPrefix+blocklist.String())
|
2020-07-27 20:49:51 +02:00
|
|
|
flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
|
|
|
|
}
|
|
|
|
|
2016-01-06 23:41:07 +01:00
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
func (s *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
|
2019-07-29 14:27:18 +02:00
|
|
|
// Add a suffix for cfi/hwasan/scs-enabled static/header libraries to allow surfacing
|
|
|
|
// both the sanitized and non-sanitized variants to make without a name conflict.
|
2020-02-24 21:01:37 +01:00
|
|
|
if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(s.Properties.SanitizeMutated.Cfi) {
|
2020-02-24 21:01:37 +01:00
|
|
|
entries.SubName += ".cfi"
|
2019-07-29 14:27:18 +02:00
|
|
|
}
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(s.Properties.SanitizeMutated.Hwaddress) {
|
2020-02-24 21:01:37 +01:00
|
|
|
entries.SubName += ".hwasan"
|
2019-07-29 14:27:18 +02:00
|
|
|
}
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(s.Properties.SanitizeMutated.Scs) {
|
2020-02-24 21:01:37 +01:00
|
|
|
entries.SubName += ".scs"
|
2019-07-29 14:27:18 +02:00
|
|
|
}
|
2018-11-20 01:03:58 +01:00
|
|
|
}
|
2017-05-08 22:44:11 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
func (s *sanitize) inSanitizerDir() bool {
|
|
|
|
return s.Properties.InSanitizerDir
|
2016-05-04 03:02:42 +02:00
|
|
|
}
|
|
|
|
|
2020-12-14 17:27:52 +01:00
|
|
|
// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
|
2022-10-03 21:07:37 +02:00
|
|
|
func (s *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
|
2016-01-06 23:41:07 +01:00
|
|
|
switch t {
|
2020-12-14 17:27:52 +01:00
|
|
|
case Asan:
|
2022-10-03 21:07:37 +02:00
|
|
|
return s.Properties.SanitizeMutated.Address
|
2021-04-01 20:29:09 +02:00
|
|
|
case Hwasan:
|
2022-10-03 21:07:37 +02:00
|
|
|
return s.Properties.SanitizeMutated.Hwaddress
|
2016-01-06 23:41:07 +01:00
|
|
|
case tsan:
|
2022-10-03 21:07:37 +02:00
|
|
|
return s.Properties.SanitizeMutated.Thread
|
2017-06-28 18:10:48 +02:00
|
|
|
case intOverflow:
|
2022-10-03 21:07:37 +02:00
|
|
|
return s.Properties.SanitizeMutated.Integer_overflow
|
2017-11-01 10:20:21 +01:00
|
|
|
case cfi:
|
2022-10-03 21:07:37 +02:00
|
|
|
return s.Properties.SanitizeMutated.Cfi
|
2018-11-20 01:03:58 +01:00
|
|
|
case scs:
|
2022-10-03 21:07:37 +02:00
|
|
|
return s.Properties.SanitizeMutated.Scs
|
2021-11-01 15:27:54 +01:00
|
|
|
case Memtag_heap:
|
2022-10-03 21:07:37 +02:00
|
|
|
return s.Properties.SanitizeMutated.Memtag_heap
|
2022-08-31 22:57:03 +02:00
|
|
|
case Memtag_stack:
|
2022-10-03 21:07:37 +02:00
|
|
|
return s.Properties.SanitizeMutated.Memtag_stack
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
case Memtag_globals:
|
|
|
|
return s.Properties.SanitizeMutated.Memtag_globals
|
2020-12-14 17:27:52 +01:00
|
|
|
case Fuzzer:
|
2022-10-03 21:07:37 +02:00
|
|
|
return s.Properties.SanitizeMutated.Fuzzer
|
2016-01-06 23:41:07 +01:00
|
|
|
default:
|
2020-12-14 17:27:52 +01:00
|
|
|
panic(fmt.Errorf("unknown SanitizerType %d", t))
|
2016-01-06 23:41:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 17:27:52 +01:00
|
|
|
// isUnsanitizedVariant returns true if no sanitizers are enabled.
|
2018-01-19 21:30:45 +01:00
|
|
|
func (sanitize *sanitize) isUnsanitizedVariant() bool {
|
2020-12-14 17:27:52 +01:00
|
|
|
return !sanitize.isSanitizerEnabled(Asan) &&
|
2021-04-01 20:29:09 +02:00
|
|
|
!sanitize.isSanitizerEnabled(Hwasan) &&
|
2018-01-19 21:30:45 +01:00
|
|
|
!sanitize.isSanitizerEnabled(tsan) &&
|
2018-11-20 01:03:58 +01:00
|
|
|
!sanitize.isSanitizerEnabled(cfi) &&
|
2019-05-01 23:42:05 +02:00
|
|
|
!sanitize.isSanitizerEnabled(scs) &&
|
2021-11-01 15:27:54 +01:00
|
|
|
!sanitize.isSanitizerEnabled(Memtag_heap) &&
|
2022-08-31 22:57:03 +02:00
|
|
|
!sanitize.isSanitizerEnabled(Memtag_stack) &&
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
!sanitize.isSanitizerEnabled(Memtag_globals) &&
|
2020-12-14 17:27:52 +01:00
|
|
|
!sanitize.isSanitizerEnabled(Fuzzer)
|
2018-01-19 21:30:45 +01:00
|
|
|
}
|
|
|
|
|
2020-12-14 17:27:52 +01:00
|
|
|
// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
|
2018-05-11 00:29:24 +02:00
|
|
|
func (sanitize *sanitize) isVariantOnProductionDevice() bool {
|
2020-12-14 17:27:52 +01:00
|
|
|
return !sanitize.isSanitizerEnabled(Asan) &&
|
2021-04-01 20:29:09 +02:00
|
|
|
!sanitize.isSanitizerEnabled(Hwasan) &&
|
2019-05-01 23:42:05 +02:00
|
|
|
!sanitize.isSanitizerEnabled(tsan) &&
|
2020-12-14 17:27:52 +01:00
|
|
|
!sanitize.isSanitizerEnabled(Fuzzer)
|
2018-05-11 00:29:24 +02:00
|
|
|
}
|
|
|
|
|
2020-12-14 17:27:52 +01:00
|
|
|
func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
|
2021-06-25 20:53:40 +02:00
|
|
|
bPtr := proptools.BoolPtr(b)
|
|
|
|
if !b {
|
|
|
|
bPtr = nil
|
|
|
|
}
|
2017-08-02 23:18:08 +02:00
|
|
|
switch t {
|
2020-12-14 17:27:52 +01:00
|
|
|
case Asan:
|
2022-10-03 21:07:37 +02:00
|
|
|
sanitize.Properties.SanitizeMutated.Address = bPtr
|
2022-09-30 00:48:08 +02:00
|
|
|
// For ASAN variant, we need to disable Memtag_stack
|
2022-10-03 21:07:37 +02:00
|
|
|
sanitize.Properties.SanitizeMutated.Memtag_stack = nil
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
sanitize.Properties.SanitizeMutated.Memtag_globals = nil
|
2021-04-01 20:29:09 +02:00
|
|
|
case Hwasan:
|
2022-10-03 21:07:37 +02:00
|
|
|
sanitize.Properties.SanitizeMutated.Hwaddress = bPtr
|
2022-09-30 00:48:08 +02:00
|
|
|
// For HWAsan variant, we need to disable Memtag_stack
|
2022-10-03 21:07:37 +02:00
|
|
|
sanitize.Properties.SanitizeMutated.Memtag_stack = nil
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
sanitize.Properties.SanitizeMutated.Memtag_globals = nil
|
2017-08-02 23:18:08 +02:00
|
|
|
case tsan:
|
2022-10-03 21:07:37 +02:00
|
|
|
sanitize.Properties.SanitizeMutated.Thread = bPtr
|
2017-08-02 23:18:08 +02:00
|
|
|
case intOverflow:
|
2022-10-03 21:07:37 +02:00
|
|
|
sanitize.Properties.SanitizeMutated.Integer_overflow = bPtr
|
2017-11-01 10:20:21 +01:00
|
|
|
case cfi:
|
2022-10-03 21:07:37 +02:00
|
|
|
sanitize.Properties.SanitizeMutated.Cfi = bPtr
|
2018-11-20 01:03:58 +01:00
|
|
|
case scs:
|
2022-10-03 21:07:37 +02:00
|
|
|
sanitize.Properties.SanitizeMutated.Scs = bPtr
|
2021-11-01 15:27:54 +01:00
|
|
|
case Memtag_heap:
|
2022-10-03 21:07:37 +02:00
|
|
|
sanitize.Properties.SanitizeMutated.Memtag_heap = bPtr
|
2022-08-31 22:57:03 +02:00
|
|
|
case Memtag_stack:
|
2022-10-03 21:07:37 +02:00
|
|
|
sanitize.Properties.SanitizeMutated.Memtag_stack = bPtr
|
2022-09-30 00:48:08 +02:00
|
|
|
// We do not need to disable ASAN or HWASan here, as there is no Memtag_stack variant.
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
case Memtag_globals:
|
|
|
|
sanitize.Properties.Sanitize.Memtag_globals = bPtr
|
2020-12-14 17:27:52 +01:00
|
|
|
case Fuzzer:
|
2022-10-03 21:07:37 +02:00
|
|
|
sanitize.Properties.SanitizeMutated.Fuzzer = bPtr
|
2017-08-02 23:18:08 +02:00
|
|
|
default:
|
2020-12-14 17:27:52 +01:00
|
|
|
panic(fmt.Errorf("unknown SanitizerType %d", t))
|
2017-08-02 23:18:08 +02:00
|
|
|
}
|
2017-08-11 02:53:16 +02:00
|
|
|
if b {
|
|
|
|
sanitize.Properties.SanitizerEnabled = true
|
2017-08-02 23:18:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-01 10:20:21 +01:00
|
|
|
// Check if the sanitizer is explicitly disabled (as opposed to nil by
|
|
|
|
// virtue of not being set).
|
2020-12-14 17:27:52 +01:00
|
|
|
func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
|
2017-11-01 10:20:21 +01:00
|
|
|
if sanitize == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
sanitizerVal := sanitize.getSanitizerBoolPtr(t)
|
|
|
|
return sanitizerVal != nil && *sanitizerVal == false
|
|
|
|
}
|
|
|
|
|
|
|
|
// There isn't an analog of the method above (ie:isSanitizerExplicitlyEnabled)
|
|
|
|
// because enabling a sanitizer either directly (via the blueprint) or
|
|
|
|
// indirectly (via a mutator) sets the bool ptr to true, and you can't
|
|
|
|
// distinguish between the cases. It isn't needed though - both cases can be
|
|
|
|
// treated identically.
|
2023-09-26 22:48:04 +02:00
|
|
|
func (s *sanitize) isSanitizerEnabled(t SanitizerType) bool {
|
|
|
|
if s == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if proptools.Bool(s.Properties.SanitizeMutated.Never) {
|
2017-11-01 10:20:21 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-09-26 22:48:04 +02:00
|
|
|
sanitizerVal := s.getSanitizerBoolPtr(t)
|
2017-11-01 10:20:21 +01:00
|
|
|
return sanitizerVal != nil && *sanitizerVal == true
|
|
|
|
}
|
|
|
|
|
2020-12-14 17:27:52 +01:00
|
|
|
// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
|
|
|
|
func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
|
2020-07-28 06:26:48 +02:00
|
|
|
switch t := tag.(type) {
|
|
|
|
case dependencyTag:
|
|
|
|
return t == reuseObjTag || t == objDepTag
|
|
|
|
case libraryDependencyTag:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2018-06-21 22:03:07 +02:00
|
|
|
}
|
|
|
|
|
2020-12-14 17:27:52 +01:00
|
|
|
func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
|
|
|
|
return IsSanitizableDependencyTag
|
|
|
|
}
|
|
|
|
|
2022-06-13 20:50:39 +02:00
|
|
|
type sanitizerSplitMutator struct {
|
|
|
|
sanitizer SanitizerType
|
|
|
|
}
|
|
|
|
|
|
|
|
// If an APEX is sanitized or not depends on whether it contains at least one
|
|
|
|
// sanitized module. Transition mutators cannot propagate information up the
|
|
|
|
// dependency graph this way, so we need an auxiliary mutator to do so.
|
|
|
|
func (s *sanitizerSplitMutator) markSanitizableApexesMutator(ctx android.TopDownMutatorContext) {
|
|
|
|
if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
|
|
|
|
enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
|
|
|
|
ctx.VisitDirectDeps(func(dep android.Module) {
|
2023-08-23 20:20:25 +02:00
|
|
|
if c, ok := dep.(PlatformSanitizeable); ok && c.IsSanitizerEnabled(s.sanitizer) {
|
Add cfi static libraries to vendor snapshot
CFI modules can't link against non-CFI static libraries, and vice versa.
So without capturing both CFI and non-CFI static libraries, vendor
modules won't be able to use CFI, which will be a critical security
hole.
This captures both CFI and non-CFI variants of all static libraries for
vendor snapshot, except for those whose cfi are explicitly disabled.
For example, suppose that "libfoo" is defined as follows.
cc_library_static {
name: "libfoo",
vendor_available: true,
}
As it doesn't have cfi disabled, two libraries "libfoo.a" and
"libfoo.cfi.a" will be captured. When installed, vendor snapshot module
for "libfoo" will look like:
vendor_snapshot_static {
name: "libfoo",
src: "libfoo.a",
cfi: {
src: "libfoo.cfi.a",
},
}
The build system will recognize the "cfi" property, and will create both
CFI and non-CFI variant, allowing any modules to link against "libfoo"
safely, no matter whether CFI is enabled or not.
Two clarification:
1) The reason why we don't create separate modules is that DepsMutator
runs before sanitize mutators. CFI and non-CFI variant of a library
should exist in a single module.
2) We can't capture CFI variant if the source module explicitly disables
cfi variant by specifying the following.
sanitize: {
cfi: false,
}
In this case, only non-CFI variant will be created for the vendor
snapshot module.
Bug: 65377115
Test: m dist vendor-snapshot && install && build against snapshot
Change-Id: Idbf3e3205d581800d6093c8d6cf6152374129ba4
2020-07-29 13:32:10 +02:00
|
|
|
enabled = true
|
|
|
|
}
|
2022-06-13 20:50:39 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if enabled {
|
|
|
|
sanitizeable.EnableSanitizer(s.sanitizer.name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sanitizerSplitMutator) Split(ctx android.BaseModuleContext) []string {
|
|
|
|
if c, ok := ctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
|
|
|
|
// If the given sanitizer is not requested in the .bp file for a module, it
|
|
|
|
// won't automatically build the sanitized variation.
|
|
|
|
if !c.IsSanitizerEnabled(s.sanitizer) {
|
|
|
|
return []string{""}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Binary() {
|
|
|
|
// If a sanitizer is enabled for a binary, we do not build the version
|
|
|
|
// without the sanitizer
|
|
|
|
return []string{s.sanitizer.variationName()}
|
|
|
|
} else if c.StaticallyLinked() || c.Header() {
|
|
|
|
// For static libraries, we build both versions. Some Make modules
|
|
|
|
// apparently depend on this behavior.
|
|
|
|
return []string{"", s.sanitizer.variationName()}
|
|
|
|
} else {
|
|
|
|
// We only build the requested variation of dynamic libraries
|
|
|
|
return []string{s.sanitizer.variationName()}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := ctx.Module().(JniSanitizeable); ok {
|
|
|
|
// TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
|
|
|
|
// that is short-circuited for now
|
|
|
|
return []string{""}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If an APEX has a sanitized dependency, we build the APEX in the sanitized
|
|
|
|
// variation. This is useful because such APEXes require extra dependencies.
|
|
|
|
if sanitizeable, ok := ctx.Module().(Sanitizeable); ok {
|
|
|
|
enabled := sanitizeable.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
|
|
|
|
if enabled {
|
|
|
|
return []string{s.sanitizer.variationName()}
|
|
|
|
} else {
|
|
|
|
return []string{""}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return []string{""}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sanitizerSplitMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
|
|
|
|
if c, ok := ctx.Module().(PlatformSanitizeable); ok {
|
|
|
|
if !c.SanitizableDepTagChecker()(ctx.DepTag()) {
|
|
|
|
// If the dependency is through a non-sanitizable tag, use the
|
|
|
|
// non-sanitized variation
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return sourceVariation
|
|
|
|
} else if _, ok := ctx.Module().(JniSanitizeable); ok {
|
|
|
|
// TODO: this should call into JniSanitizable.IsSanitizerEnabledForJni but
|
|
|
|
// that is short-circuited for now
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
// Otherwise, do not rock the boat.
|
|
|
|
return sourceVariation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sanitizerSplitMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
|
|
|
|
if d, ok := ctx.Module().(PlatformSanitizeable); ok {
|
|
|
|
if !d.SanitizePropDefined() ||
|
|
|
|
d.SanitizeNever() ||
|
|
|
|
d.IsSanitizerExplicitlyDisabled(s.sanitizer) ||
|
|
|
|
!d.SanitizerSupported(s.sanitizer) {
|
|
|
|
// If a module opts out of a sanitizer, use its non-sanitized variation
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Binaries are always built in the variation they requested.
|
|
|
|
if d.Binary() {
|
|
|
|
if d.IsSanitizerEnabled(s.sanitizer) {
|
|
|
|
return s.sanitizer.variationName()
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a shared library requests to be sanitized, it will be built for that
|
|
|
|
// sanitizer. Otherwise, some sanitizers propagate through shared library
|
|
|
|
// dependency edges, some do not.
|
|
|
|
if !d.StaticallyLinked() && !d.Header() {
|
|
|
|
if d.IsSanitizerEnabled(s.sanitizer) {
|
|
|
|
return s.sanitizer.variationName()
|
|
|
|
}
|
|
|
|
|
2022-10-31 15:31:11 +01:00
|
|
|
// Some sanitizers do not propagate to shared dependencies
|
|
|
|
if !s.sanitizer.shouldPropagateToSharedLibraryDeps() {
|
2022-06-13 20:50:39 +02:00
|
|
|
return ""
|
2022-03-25 23:50:53 +01:00
|
|
|
}
|
2022-06-13 20:50:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Static and header libraries inherit whether they are sanitized from the
|
|
|
|
// module they are linked into
|
|
|
|
return incomingVariation
|
|
|
|
} else if d, ok := ctx.Module().(Sanitizeable); ok {
|
|
|
|
// If an APEX contains a sanitized module, it will be built in the variation
|
|
|
|
// corresponding to that sanitizer.
|
|
|
|
enabled := d.IsSanitizerEnabled(ctx.Config(), s.sanitizer.name())
|
|
|
|
if enabled {
|
|
|
|
return s.sanitizer.variationName()
|
|
|
|
}
|
|
|
|
|
|
|
|
return incomingVariation
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sanitizerSplitMutator) Mutate(mctx android.BottomUpMutatorContext, variationName string) {
|
|
|
|
sanitizerVariation := variationName == s.sanitizer.variationName()
|
|
|
|
|
|
|
|
if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
|
|
|
|
sanitizerEnabled := c.IsSanitizerEnabled(s.sanitizer)
|
|
|
|
|
|
|
|
oneMakeVariation := false
|
|
|
|
if c.StaticallyLinked() || c.Header() {
|
|
|
|
if s.sanitizer != cfi && s.sanitizer != scs && s.sanitizer != Hwasan {
|
|
|
|
// These sanitizers export only one variation to Make. For the rest,
|
|
|
|
// Make targets can depend on both the sanitized and non-sanitized
|
|
|
|
// versions.
|
|
|
|
oneMakeVariation = true
|
|
|
|
}
|
|
|
|
} else if !c.Binary() {
|
|
|
|
// Shared library. These are the sanitizers that do propagate through shared
|
|
|
|
// library dependencies and therefore can cause multiple variations of a
|
|
|
|
// shared library to be built.
|
|
|
|
if s.sanitizer != cfi && s.sanitizer != Hwasan && s.sanitizer != scs && s.sanitizer != Asan {
|
|
|
|
oneMakeVariation = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if oneMakeVariation {
|
|
|
|
if sanitizerEnabled != sanitizerVariation {
|
|
|
|
c.SetPreventInstall()
|
|
|
|
c.SetHideFromMake()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sanitizerVariation {
|
|
|
|
c.SetSanitizer(s.sanitizer, true)
|
|
|
|
|
|
|
|
// CFI is incompatible with ASAN so disable it in ASAN variations
|
|
|
|
if s.sanitizer.incompatibleWithCfi() {
|
|
|
|
cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
|
|
|
|
if mctx.Device() && cfiSupported {
|
|
|
|
c.SetSanitizer(cfi, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// locate the asan libraries under /data/asan
|
|
|
|
if !c.Binary() && !c.StaticallyLinked() && !c.Header() && mctx.Device() && s.sanitizer == Asan && sanitizerEnabled {
|
|
|
|
c.SetInSanitizerDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.StaticallyLinked() && c.ExportedToMake() {
|
|
|
|
if s.sanitizer == Hwasan {
|
|
|
|
hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
|
|
|
|
} else if s.sanitizer == cfi {
|
|
|
|
cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
|
2024-03-27 21:53:39 +01:00
|
|
|
} else if s.sanitizer == Memtag_stack {
|
2024-05-07 07:47:35 +02:00
|
|
|
memtagStackStaticLibs(mctx.Config()).add(c, c.Module().Name())
|
2022-06-13 20:50:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if c.IsSanitizerEnabled(s.sanitizer) {
|
|
|
|
// Disable the sanitizer for the non-sanitized variation
|
|
|
|
c.SetSanitizer(s.sanitizer, false)
|
|
|
|
}
|
|
|
|
} else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
|
|
|
|
// If an APEX has sanitized dependencies, it gets a few more dependencies
|
|
|
|
if sanitizerVariation {
|
|
|
|
sanitizeable.AddSanitizerDependencies(mctx, s.sanitizer.name())
|
|
|
|
}
|
2023-08-23 20:20:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 17:27:52 +01:00
|
|
|
func (c *Module) SanitizeNever() bool {
|
2022-10-03 21:07:37 +02:00
|
|
|
return Bool(c.sanitize.Properties.SanitizeMutated.Never)
|
2020-12-14 17:27:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
|
|
|
|
return c.sanitize.isSanitizerExplicitlyDisabled(t)
|
|
|
|
}
|
|
|
|
|
2018-02-22 00:49:20 +01:00
|
|
|
// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
|
2018-06-21 22:03:07 +02:00
|
|
|
func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
|
2020-12-14 17:27:52 +01:00
|
|
|
// Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
|
2018-06-21 22:03:07 +02:00
|
|
|
if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
|
2020-12-14 17:27:52 +01:00
|
|
|
isSanitizableDependencyTag := c.SanitizableDepTagChecker()
|
2018-06-21 22:03:07 +02:00
|
|
|
mctx.WalkDeps(func(child, parent android.Module) bool {
|
|
|
|
if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-22 03:11:29 +01:00
|
|
|
d, ok := child.(*Module)
|
|
|
|
if !ok || !d.static() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if d.sanitize != nil {
|
2018-06-21 22:03:07 +02:00
|
|
|
if enableMinimalRuntime(d.sanitize) {
|
|
|
|
// If a static dependency is built with the minimal runtime,
|
|
|
|
// make sure we include the ubsan minimal runtime.
|
|
|
|
c.sanitize.Properties.MinimalRuntimeDep = true
|
2019-11-15 01:59:12 +01:00
|
|
|
} else if enableUbsanRuntime(d.sanitize) {
|
2018-06-21 22:03:07 +02:00
|
|
|
// If a static dependency runs with full ubsan diagnostics,
|
|
|
|
// make sure we include the ubsan runtime.
|
|
|
|
c.sanitize.Properties.UbsanRuntimeDep = true
|
2018-02-22 00:49:20 +01:00
|
|
|
}
|
2019-06-20 08:00:20 +02:00
|
|
|
|
|
|
|
if c.sanitize.Properties.MinimalRuntimeDep &&
|
|
|
|
c.sanitize.Properties.UbsanRuntimeDep {
|
|
|
|
// both flags that this mutator might set are true, so don't bother recursing
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-19 21:24:02 +01:00
|
|
|
if c.Os() == android.Linux {
|
|
|
|
c.sanitize.Properties.BuiltinsDep = true
|
|
|
|
}
|
|
|
|
|
2019-06-20 08:00:20 +02:00
|
|
|
return true
|
2018-06-21 22:03:07 +02:00
|
|
|
}
|
2020-01-22 03:11:29 +01:00
|
|
|
|
|
|
|
return false
|
2018-06-21 22:03:07 +02:00
|
|
|
})
|
2018-02-22 00:49:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 18:47:14 +01:00
|
|
|
// Add the dependency to the runtime library for each of the sanitizer variants
|
|
|
|
func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
|
|
|
|
if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
|
2024-04-12 02:43:00 +02:00
|
|
|
if !c.Enabled(mctx) {
|
2019-01-25 01:20:35 +01:00
|
|
|
return
|
|
|
|
}
|
2018-12-18 18:47:14 +01:00
|
|
|
var sanitizers []string
|
|
|
|
var diagSanitizers []string
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
sanProps := &c.sanitize.Properties.SanitizeMutated
|
|
|
|
|
|
|
|
if Bool(sanProps.All_undefined) {
|
2018-12-18 18:47:14 +01:00
|
|
|
sanitizers = append(sanitizers, "undefined")
|
|
|
|
} else {
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Undefined) {
|
2018-12-18 18:47:14 +01:00
|
|
|
sanitizers = append(sanitizers,
|
|
|
|
"bool",
|
|
|
|
"integer-divide-by-zero",
|
|
|
|
"return",
|
|
|
|
"returns-nonnull-attribute",
|
|
|
|
"shift-exponent",
|
|
|
|
"unreachable",
|
|
|
|
"vla-bound",
|
|
|
|
// TODO(danalbert): The following checks currently have compiler performance issues.
|
|
|
|
//"alignment",
|
|
|
|
//"bounds",
|
|
|
|
//"enum",
|
|
|
|
//"float-cast-overflow",
|
|
|
|
//"float-divide-by-zero",
|
|
|
|
//"nonnull-attribute",
|
|
|
|
//"null",
|
|
|
|
//"shift-base",
|
|
|
|
//"signed-integer-overflow",
|
|
|
|
// TODO(danalbert): Fix UB in libc++'s __tree so we can turn this on.
|
|
|
|
// https://llvm.org/PR19302
|
|
|
|
// http://reviews.llvm.org/D6974
|
|
|
|
// "object-size",
|
|
|
|
)
|
|
|
|
}
|
2022-10-03 21:07:37 +02:00
|
|
|
sanitizers = append(sanitizers, sanProps.Misc_undefined...)
|
2018-12-18 18:47:14 +01:00
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Diag.Undefined) {
|
2018-12-18 18:47:14 +01:00
|
|
|
diagSanitizers = append(diagSanitizers, "undefined")
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
diagSanitizers = append(diagSanitizers, sanProps.Diag.Misc_undefined...)
|
2018-12-18 18:47:14 +01:00
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Address) {
|
2018-12-18 18:47:14 +01:00
|
|
|
sanitizers = append(sanitizers, "address")
|
|
|
|
diagSanitizers = append(diagSanitizers, "address")
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Hwaddress) {
|
2018-12-18 18:47:14 +01:00
|
|
|
sanitizers = append(sanitizers, "hwaddress")
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Thread) {
|
2018-12-18 18:47:14 +01:00
|
|
|
sanitizers = append(sanitizers, "thread")
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Safestack) {
|
2018-12-18 18:47:14 +01:00
|
|
|
sanitizers = append(sanitizers, "safe-stack")
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Cfi) {
|
2018-12-18 18:47:14 +01:00
|
|
|
sanitizers = append(sanitizers, "cfi")
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Diag.Cfi) {
|
2018-12-18 18:47:14 +01:00
|
|
|
diagSanitizers = append(diagSanitizers, "cfi")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Integer_overflow) {
|
2018-12-18 18:47:14 +01:00
|
|
|
sanitizers = append(sanitizers, "unsigned-integer-overflow")
|
|
|
|
sanitizers = append(sanitizers, "signed-integer-overflow")
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Diag.Integer_overflow) {
|
2018-12-18 18:47:14 +01:00
|
|
|
diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
|
|
|
|
diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Scudo) {
|
2018-12-18 18:47:14 +01:00
|
|
|
sanitizers = append(sanitizers, "scudo")
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Scs) {
|
2018-12-18 18:47:14 +01:00
|
|
|
sanitizers = append(sanitizers, "shadow-call-stack")
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Memtag_heap) && c.Binary() {
|
2022-08-31 22:57:03 +02:00
|
|
|
sanitizers = append(sanitizers, "memtag-heap")
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Memtag_stack) {
|
2022-08-31 22:57:03 +02:00
|
|
|
sanitizers = append(sanitizers, "memtag-stack")
|
2020-04-29 00:09:12 +02:00
|
|
|
}
|
|
|
|
|
[MTE] Add memtag_globals as a sanitizer target.
MTE globals needs four pieces:
1. (done) clang/llvm to produce objfiles with MTE globals metadata
2. (in flight, https://reviews.llvm.org/D152921) lld to process the metadata in the objfiles, and produce information in each DSO describing the global variables that need to be tagged (alongside some other stuff).
3. (this patch) android to handle the new sanitizer
4. (not yet sent) bionic's libc/linker to interpret the information in each DSO, and tag the global variables.
Because #2 will take some time to come through the toolchain rolls, and #3 and #4 can be landed asynchronously, we can land this ahead of time. Should make my life easier by preventing constant rebases.
Bug: N/A
Test: Build {libc, libm, libc++, libm, and libnetd} in internal master
with these patches and an experimental compiler with the lld support.
Also, `SANITIZE_TARGET=memtag_globals m` here with in-tree compiler
without lld support.
Change-Id: Ie7882d474d4d776232de3a3d571a82274df14bf0
2023-06-01 14:23:09 +02:00
|
|
|
if Bool(sanProps.Memtag_globals) {
|
|
|
|
sanitizers = append(sanitizers, "memtag-globals")
|
|
|
|
// TODO(mitchp): For now, enable memtag-heap with memtag-globals because the linker
|
|
|
|
// isn't new enough (https://reviews.llvm.org/differential/changeset/?ref=4243566).
|
|
|
|
sanitizers = append(sanitizers, "memtag-heap")
|
|
|
|
}
|
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Fuzzer) {
|
2019-05-01 23:42:05 +02:00
|
|
|
sanitizers = append(sanitizers, "fuzzer-no-link")
|
|
|
|
}
|
|
|
|
|
2018-12-18 18:47:14 +01:00
|
|
|
// Save the list of sanitizers. These will be used again when generating
|
|
|
|
// the build rules (for Cflags, etc.)
|
|
|
|
c.sanitize.Properties.Sanitizers = sanitizers
|
|
|
|
c.sanitize.Properties.DiagSanitizers = diagSanitizers
|
|
|
|
|
2020-03-06 18:01:21 +01:00
|
|
|
// TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
|
|
|
|
if c.Host() {
|
|
|
|
diagSanitizers = sanitizers
|
|
|
|
}
|
|
|
|
|
2023-02-15 22:57:57 +01:00
|
|
|
addStaticDeps := func(dep string, hideSymbols bool) {
|
|
|
|
// static executable gets static runtime libs
|
|
|
|
depTag := libraryDependencyTag{Kind: staticLibraryDependency, unexportedSymbols: hideSymbols}
|
|
|
|
variations := append(mctx.Target().Variations(),
|
|
|
|
blueprint.Variation{Mutator: "link", Variation: "static"})
|
|
|
|
if c.Device() {
|
|
|
|
variations = append(variations, c.ImageVariation())
|
|
|
|
}
|
|
|
|
if c.UseSdk() {
|
|
|
|
variations = append(variations,
|
|
|
|
blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
|
|
|
|
}
|
|
|
|
mctx.AddFarVariationDependencies(variations, depTag, dep)
|
|
|
|
}
|
|
|
|
|
2018-12-18 18:47:14 +01:00
|
|
|
// Determine the runtime library required
|
2023-02-15 22:57:57 +01:00
|
|
|
runtimeSharedLibrary := ""
|
2018-12-18 18:47:14 +01:00
|
|
|
toolchain := c.toolchain(mctx)
|
2022-10-03 21:07:37 +02:00
|
|
|
if Bool(sanProps.Address) {
|
2023-02-15 21:40:20 +01:00
|
|
|
if toolchain.Musl() || (c.staticBinary() && toolchain.Bionic()) {
|
|
|
|
// Use a static runtime for musl to match what clang does for glibc.
|
2024-05-07 07:47:35 +02:00
|
|
|
addStaticDeps(config.AddressSanitizerStaticRuntimeLibrary(), false)
|
|
|
|
addStaticDeps(config.AddressSanitizerCXXStaticRuntimeLibrary(), false)
|
2023-02-15 21:40:20 +01:00
|
|
|
} else {
|
2024-05-07 07:47:35 +02:00
|
|
|
runtimeSharedLibrary = config.AddressSanitizerRuntimeLibrary()
|
2023-02-15 21:40:20 +01:00
|
|
|
}
|
2022-10-03 21:07:37 +02:00
|
|
|
} else if Bool(sanProps.Hwaddress) {
|
2018-12-18 18:47:14 +01:00
|
|
|
if c.staticBinary() {
|
2024-05-07 07:47:35 +02:00
|
|
|
addStaticDeps(config.HWAddressSanitizerStaticLibrary(), true)
|
2023-02-15 22:57:57 +01:00
|
|
|
addStaticDeps("libdl", false)
|
2018-12-18 18:47:14 +01:00
|
|
|
} else {
|
2024-05-07 07:47:35 +02:00
|
|
|
runtimeSharedLibrary = config.HWAddressSanitizerRuntimeLibrary()
|
2018-12-18 18:47:14 +01:00
|
|
|
}
|
2022-10-03 21:07:37 +02:00
|
|
|
} else if Bool(sanProps.Thread) {
|
2024-05-07 07:47:35 +02:00
|
|
|
runtimeSharedLibrary = config.ThreadSanitizerRuntimeLibrary()
|
2022-10-03 21:07:37 +02:00
|
|
|
} else if Bool(sanProps.Scudo) {
|
2018-12-18 18:47:14 +01:00
|
|
|
if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
|
2024-05-07 07:47:35 +02:00
|
|
|
runtimeSharedLibrary = config.ScudoMinimalRuntimeLibrary()
|
2018-12-18 18:47:14 +01:00
|
|
|
} else {
|
2024-05-07 07:47:35 +02:00
|
|
|
runtimeSharedLibrary = config.ScudoRuntimeLibrary()
|
2018-12-18 18:47:14 +01:00
|
|
|
}
|
Workaround unexported sancov symbols. Fix multiple sanitizer RT deps.
Fuzz targets currently have dependencies on multiple libclang_rt runtime
libraries when building with ASan/HWAsan on device. This is an error.
This happens as Soong adds the dependency on the ASan/HWASan shared
runtime library. These libraries should provide the required UBSan
components. The clang driver was previously being passed
-fsanitize=fuzzer-no-link at link time, and as it doesn't know about the
already-established dependency on ASan/HWASan, it mistakenly thinks that
there is not runtime providing the UBSan components.
This patch fixes that problem by not adding -fsanitize=fuzzer-no-link to
the link-time flags.
This revealed a underlying issue in the upstream runtime compilation.
Android uses emulated TLS, which changes the symbol names from
<my_symbol_name> to __emutls_v._<my_symbol_name>. In particular, this
fails to account for the '__sancov_lowest_stack' symbol, as it no longer
matches the linker script rule for '__sancov*', and the symbol is no
longer exported in the shared library variant of ASan/HWASan.
This patch works around the discovered issue, which is being tracked in
the linked bug. It disables stack depth instrumentation, and we no
longer depend on this symbol. This means we get a missing sanitizer
coverage feature when fuzzing, but shouldn't be too detrimental.
Bug: 142430592
Test: SANITIZE_TARGET=hwaddress m example_fuzzer && \
readelf -d example_fuzzer # ensure only ONE libclang_rt dep (in this
case, hwasan)
Change-Id: Iea6df55d592a801732511c9b690134367429d62a
2019-10-10 02:18:59 +02:00
|
|
|
} else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
|
2022-10-03 21:07:37 +02:00
|
|
|
Bool(sanProps.Fuzzer) ||
|
|
|
|
Bool(sanProps.Undefined) ||
|
|
|
|
Bool(sanProps.All_undefined) {
|
2023-08-24 07:20:51 +02:00
|
|
|
if toolchain.Musl() || c.staticBinary() {
|
|
|
|
// Use a static runtime for static binaries. For sanitized glibc binaries the runtime is
|
|
|
|
// added automatically by clang, but for static glibc binaries that are not sanitized but
|
|
|
|
// have a sanitized dependency the runtime needs to be added manually.
|
|
|
|
// Also manually add a static runtime for musl to match what clang does for glibc.
|
|
|
|
// Otherwise dlopening libraries that depend on libclang_rt.ubsan_standalone.so fails with:
|
2022-10-20 00:46:53 +02:00
|
|
|
// Error relocating ...: initial-exec TLS resolves to dynamic definition
|
2024-05-07 07:47:35 +02:00
|
|
|
addStaticDeps(config.UndefinedBehaviorSanitizerRuntimeLibrary()+".static", true)
|
2023-02-15 22:57:57 +01:00
|
|
|
} else {
|
2024-05-07 07:47:35 +02:00
|
|
|
runtimeSharedLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary()
|
2021-03-29 22:41:37 +02:00
|
|
|
}
|
2018-12-18 18:47:14 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 19:34:19 +01:00
|
|
|
if enableMinimalRuntime(c.sanitize) || c.sanitize.Properties.MinimalRuntimeDep {
|
2024-05-07 07:47:35 +02:00
|
|
|
addStaticDeps(config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(), true)
|
2022-02-10 19:34:19 +01:00
|
|
|
}
|
|
|
|
if c.sanitize.Properties.BuiltinsDep {
|
2024-05-07 07:47:35 +02:00
|
|
|
addStaticDeps(config.BuiltinsRuntimeLibrary(), true)
|
2022-02-10 19:34:19 +01:00
|
|
|
}
|
|
|
|
|
2024-05-03 18:10:04 +02:00
|
|
|
if runtimeSharedLibrary != "" && (toolchain.Bionic() || toolchain.Musl()) {
|
2020-02-19 21:24:02 +01:00
|
|
|
// UBSan is supported on non-bionic linux host builds as well
|
2018-12-18 18:47:14 +01:00
|
|
|
|
|
|
|
// Adding dependency to the runtime library. We are using *FarVariation*
|
|
|
|
// because the runtime libraries themselves are not mutated by sanitizer
|
|
|
|
// mutators and thus don't have sanitizer variants whereas this module
|
|
|
|
// has been already mutated.
|
|
|
|
//
|
|
|
|
// Note that by adding dependency with {static|shared}DepTag, the lib is
|
|
|
|
// added to libFlags and LOCAL_SHARED_LIBRARIES by cc.Module
|
2023-02-15 22:57:57 +01:00
|
|
|
if c.staticBinary() {
|
|
|
|
// Most sanitizers are either disabled for static binaries or have already
|
|
|
|
// handled the static binary case above through a direct call to addStaticDeps.
|
|
|
|
// If not, treat the runtime shared library as a static library and hope for
|
|
|
|
// the best.
|
|
|
|
addStaticDeps(runtimeSharedLibrary, true)
|
2020-12-14 17:27:52 +01:00
|
|
|
} else if !c.static() && !c.Header() {
|
2020-12-10 16:12:38 +01:00
|
|
|
// Skip apex dependency check for sharedLibraryDependency
|
|
|
|
// when sanitizer diags are enabled. Skipping the check will allow
|
|
|
|
// building with diag libraries without having to list the
|
|
|
|
// dependency in Apex's allowed_deps file.
|
|
|
|
diagEnabled := len(diagSanitizers) > 0
|
2019-01-29 03:15:04 +01:00
|
|
|
// dynamic executable and shared libs get shared runtime libs
|
2020-12-10 16:12:38 +01:00
|
|
|
depTag := libraryDependencyTag{
|
|
|
|
Kind: sharedLibraryDependency,
|
|
|
|
Order: earlyLibraryDependency,
|
|
|
|
|
|
|
|
skipApexAllowedDependenciesCheck: diagEnabled,
|
|
|
|
}
|
2020-08-22 01:15:23 +02:00
|
|
|
variations := append(mctx.Target().Variations(),
|
|
|
|
blueprint.Variation{Mutator: "link", Variation: "shared"})
|
|
|
|
if c.Device() {
|
|
|
|
variations = append(variations, c.ImageVariation())
|
|
|
|
}
|
2022-02-10 19:34:19 +01:00
|
|
|
if c.UseSdk() {
|
|
|
|
variations = append(variations,
|
|
|
|
blueprint.Variation{Mutator: "sdk", Variation: "sdk"})
|
|
|
|
}
|
2023-02-15 22:57:57 +01:00
|
|
|
AddSharedLibDependenciesWithVersions(mctx, c, variations, depTag, runtimeSharedLibrary, "", true)
|
2018-12-18 18:47:14 +01:00
|
|
|
}
|
|
|
|
// static lib does not have dependency to the runtime library. The
|
|
|
|
// dependency will be added to the executables or shared libs using
|
|
|
|
// the static lib.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Sanitizeable interface {
|
|
|
|
android.Module
|
2022-06-17 08:59:37 +02:00
|
|
|
IsSanitizerEnabled(config android.Config, sanitizerName string) bool
|
2019-02-13 12:28:58 +01:00
|
|
|
EnableSanitizer(sanitizerName string)
|
2020-05-15 12:05:05 +02:00
|
|
|
AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
|
2018-12-18 18:47:14 +01:00
|
|
|
}
|
|
|
|
|
2022-03-25 23:50:53 +01:00
|
|
|
type JniSanitizeable interface {
|
|
|
|
android.Module
|
|
|
|
IsSanitizerEnabledForJni(ctx android.BaseModuleContext, sanitizerName string) bool
|
|
|
|
}
|
|
|
|
|
2021-04-01 15:49:36 +02:00
|
|
|
func (c *Module) MinimalRuntimeDep() bool {
|
|
|
|
return c.sanitize.Properties.MinimalRuntimeDep
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Module) UbsanRuntimeDep() bool {
|
|
|
|
return c.sanitize.Properties.UbsanRuntimeDep
|
|
|
|
}
|
|
|
|
|
2020-12-14 17:27:52 +01:00
|
|
|
func (c *Module) SanitizePropDefined() bool {
|
|
|
|
return c.sanitize != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Module) IsSanitizerEnabled(t SanitizerType) bool {
|
|
|
|
return c.sanitize.isSanitizerEnabled(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Module) StaticallyLinked() bool {
|
|
|
|
return c.static()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Module) SetInSanitizerDir() {
|
|
|
|
if c.sanitize != nil {
|
|
|
|
c.sanitize.Properties.InSanitizerDir = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Module) SetSanitizer(t SanitizerType, b bool) {
|
|
|
|
if c.sanitize != nil {
|
|
|
|
c.sanitize.SetSanitizer(t, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ PlatformSanitizeable = (*Module)(nil)
|
|
|
|
|
2020-08-03 17:41:38 +02:00
|
|
|
type sanitizerStaticLibsMap struct {
|
|
|
|
// libsMap contains one list of modules per each image and each arch.
|
|
|
|
// e.g. libs[vendor]["arm"] contains arm modules installed to vendor
|
2020-12-14 17:27:52 +01:00
|
|
|
libsMap map[ImageVariantType]map[string][]string
|
2020-08-03 17:41:38 +02:00
|
|
|
libsMapLock sync.Mutex
|
2020-12-14 17:27:52 +01:00
|
|
|
sanitizerType SanitizerType
|
2020-08-03 17:41:38 +02:00
|
|
|
}
|
2019-02-04 20:22:08 +01:00
|
|
|
|
2020-12-14 17:27:52 +01:00
|
|
|
func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
|
2020-08-03 17:41:38 +02:00
|
|
|
return &sanitizerStaticLibsMap{
|
|
|
|
sanitizerType: t,
|
2020-12-14 17:27:52 +01:00
|
|
|
libsMap: make(map[ImageVariantType]map[string][]string),
|
2020-08-03 17:41:38 +02:00
|
|
|
}
|
2017-11-17 20:08:10 +01:00
|
|
|
}
|
|
|
|
|
2020-08-03 17:41:38 +02:00
|
|
|
// Add the current module to sanitizer static libs maps
|
|
|
|
// Each module should pass its exported name as names of Make and Soong can differ.
|
2020-12-14 17:27:52 +01:00
|
|
|
func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
|
|
|
|
image := GetImageVariantType(c)
|
|
|
|
arch := c.Module().Target().Arch.ArchType.String()
|
2019-02-04 20:22:08 +01:00
|
|
|
|
2020-08-03 17:41:38 +02:00
|
|
|
s.libsMapLock.Lock()
|
|
|
|
defer s.libsMapLock.Unlock()
|
|
|
|
|
|
|
|
if _, ok := s.libsMap[image]; !ok {
|
|
|
|
s.libsMap[image] = make(map[string][]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.libsMap[image][arch] = append(s.libsMap[image][arch], name)
|
2018-08-03 01:19:13 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 17:41:38 +02:00
|
|
|
// Exports makefile variables in the following format:
|
|
|
|
// SOONG_{sanitizer}_{image}_{arch}_STATIC_LIBRARIES
|
|
|
|
// e.g. SOONG_cfi_core_x86_STATIC_LIBRARIES
|
|
|
|
// These are to be used by use_soong_sanitized_static_libraries.
|
|
|
|
// See build/make/core/binary.mk for more details.
|
|
|
|
func (s *sanitizerStaticLibsMap) exportToMake(ctx android.MakeVarsContext) {
|
2023-03-01 01:02:16 +01:00
|
|
|
for _, image := range android.SortedKeys(s.libsMap) {
|
2020-12-14 17:27:52 +01:00
|
|
|
archMap := s.libsMap[ImageVariantType(image)]
|
2023-03-01 01:02:16 +01:00
|
|
|
for _, arch := range android.SortedKeys(archMap) {
|
2020-08-03 17:41:38 +02:00
|
|
|
libs := archMap[arch]
|
|
|
|
sort.Strings(libs)
|
|
|
|
|
|
|
|
key := fmt.Sprintf(
|
|
|
|
"SOONG_%s_%s_%s_STATIC_LIBRARIES",
|
|
|
|
s.sanitizerType.variationName(),
|
|
|
|
image, // already upper
|
|
|
|
arch)
|
|
|
|
|
|
|
|
ctx.Strict(key, strings.Join(libs, " "))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-04 20:22:08 +01:00
|
|
|
|
2020-08-03 17:41:38 +02:00
|
|
|
var cfiStaticLibsKey = android.NewOnceKey("cfiStaticLibs")
|
|
|
|
|
|
|
|
func cfiStaticLibs(config android.Config) *sanitizerStaticLibsMap {
|
|
|
|
return config.Once(cfiStaticLibsKey, func() interface{} {
|
|
|
|
return newSanitizerStaticLibsMap(cfi)
|
|
|
|
}).(*sanitizerStaticLibsMap)
|
2018-08-03 01:19:13 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 17:41:38 +02:00
|
|
|
var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
|
|
|
|
|
|
|
|
func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
|
|
|
|
return config.Once(hwasanStaticLibsKey, func() interface{} {
|
2021-04-01 20:29:09 +02:00
|
|
|
return newSanitizerStaticLibsMap(Hwasan)
|
2020-08-03 17:41:38 +02:00
|
|
|
}).(*sanitizerStaticLibsMap)
|
2019-07-29 14:27:18 +02:00
|
|
|
}
|
|
|
|
|
2024-03-27 21:53:39 +01:00
|
|
|
var memtagStackStaticLibsKey = android.NewOnceKey("memtagStackStaticLibs")
|
|
|
|
|
|
|
|
func memtagStackStaticLibs(config android.Config) *sanitizerStaticLibsMap {
|
|
|
|
return config.Once(memtagStackStaticLibsKey, func() interface{} {
|
|
|
|
return newSanitizerStaticLibsMap(Memtag_stack)
|
|
|
|
}).(*sanitizerStaticLibsMap)
|
|
|
|
}
|
|
|
|
|
2018-02-22 00:49:20 +01:00
|
|
|
func enableMinimalRuntime(sanitize *sanitize) bool {
|
2022-10-03 21:07:37 +02:00
|
|
|
if sanitize.isSanitizerEnabled(Asan) {
|
|
|
|
return false
|
|
|
|
} else if sanitize.isSanitizerEnabled(Hwasan) {
|
|
|
|
return false
|
|
|
|
} else if sanitize.isSanitizerEnabled(Fuzzer) {
|
|
|
|
return false
|
|
|
|
}
|
2020-02-19 21:24:02 +01:00
|
|
|
|
2022-10-03 21:07:37 +02:00
|
|
|
if enableUbsanRuntime(sanitize) {
|
|
|
|
return false
|
2018-02-22 00:49:20 +01:00
|
|
|
}
|
2022-10-03 21:07:37 +02:00
|
|
|
|
|
|
|
sanitizeProps := &sanitize.Properties.SanitizeMutated
|
|
|
|
if Bool(sanitizeProps.Diag.Cfi) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return Bool(sanitizeProps.Integer_overflow) ||
|
|
|
|
len(sanitizeProps.Misc_undefined) > 0 ||
|
|
|
|
Bool(sanitizeProps.Undefined) ||
|
|
|
|
Bool(sanitizeProps.All_undefined)
|
2018-02-22 00:49:20 +01:00
|
|
|
}
|
|
|
|
|
2021-04-01 15:49:36 +02:00
|
|
|
func (m *Module) UbsanRuntimeNeeded() bool {
|
|
|
|
return enableUbsanRuntime(m.sanitize)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Module) MinimalRuntimeNeeded() bool {
|
|
|
|
return enableMinimalRuntime(m.sanitize)
|
|
|
|
}
|
|
|
|
|
2019-11-15 01:59:12 +01:00
|
|
|
func enableUbsanRuntime(sanitize *sanitize) bool {
|
2022-10-03 21:07:37 +02:00
|
|
|
sanitizeProps := &sanitize.Properties.SanitizeMutated
|
|
|
|
return Bool(sanitizeProps.Diag.Integer_overflow) ||
|
|
|
|
Bool(sanitizeProps.Diag.Undefined) ||
|
|
|
|
len(sanitizeProps.Diag.Misc_undefined) > 0
|
2019-11-15 01:59:12 +01:00
|
|
|
}
|
|
|
|
|
2017-11-17 20:08:10 +01:00
|
|
|
func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
|
2020-08-03 17:41:38 +02:00
|
|
|
cfiStaticLibs(ctx.Config()).exportToMake(ctx)
|
2017-11-17 20:08:10 +01:00
|
|
|
}
|
2018-08-03 01:19:13 +02:00
|
|
|
|
|
|
|
func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
|
2020-08-03 17:41:38 +02:00
|
|
|
hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
|
2018-08-03 01:19:13 +02:00
|
|
|
}
|
2024-03-27 21:53:39 +01:00
|
|
|
|
|
|
|
func memtagStackMakeVarsProvider(ctx android.MakeVarsContext) {
|
|
|
|
memtagStackStaticLibs(ctx.Config()).exportToMake(ctx)
|
|
|
|
}
|
2024-05-07 07:47:35 +02:00
|
|
|
|
|
|
|
type sanitizerLibrariesTxtModule struct {
|
|
|
|
android.ModuleBase
|
|
|
|
|
|
|
|
outputFile android.OutputPath
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ etc.PrebuiltEtcModule = (*sanitizerLibrariesTxtModule)(nil)
|
|
|
|
|
|
|
|
func RegisterSanitizerLibrariesTxtType(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("sanitizer_libraries_txt", sanitizerLibrariesTxtFactory)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sanitizerLibrariesTxtFactory() android.Module {
|
|
|
|
m := &sanitizerLibrariesTxtModule{}
|
|
|
|
android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
type sanitizerLibraryDependencyTag struct {
|
|
|
|
blueprint.BaseDependencyTag
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t sanitizerLibraryDependencyTag) AllowDisabledModuleDependency(target android.Module) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ android.AllowDisabledModuleDependency = (*sanitizerLibraryDependencyTag)(nil)
|
|
|
|
|
|
|
|
func (txt *sanitizerLibrariesTxtModule) DepsMutator(actx android.BottomUpMutatorContext) {
|
|
|
|
targets := actx.Config().Targets[android.Android]
|
|
|
|
depTag := sanitizerLibraryDependencyTag{}
|
|
|
|
|
|
|
|
for _, target := range targets {
|
|
|
|
variation := append(target.Variations(),
|
|
|
|
blueprint.Variation{Mutator: "image", Variation: ""},
|
|
|
|
blueprint.Variation{Mutator: "sdk", Variation: ""},
|
|
|
|
blueprint.Variation{Mutator: "link", Variation: "shared"},
|
|
|
|
)
|
|
|
|
for _, lib := range android.SortedStringValues(sanitizerVariables) {
|
|
|
|
if actx.OtherModuleFarDependencyVariantExists(variation, lib) {
|
|
|
|
actx.AddFarVariationDependencies(variation, depTag, lib)
|
|
|
|
}
|
|
|
|
|
|
|
|
prebuiltLibName := "prebuilt_" + lib
|
|
|
|
if actx.OtherModuleFarDependencyVariantExists(variation, prebuiltLibName) {
|
|
|
|
actx.AddFarVariationDependencies(variation, depTag, prebuiltLibName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (txt *sanitizerLibrariesTxtModule) getSanitizerLibs(ctx android.ModuleContext) string {
|
|
|
|
var sanitizerLibStems []string
|
|
|
|
|
|
|
|
ctx.VisitDirectDepsIf(func(m android.Module) bool {
|
|
|
|
if !m.Enabled(ctx) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
ccModule, _ := m.(*Module)
|
|
|
|
if ccModule == nil || ccModule.library == nil || !ccModule.library.shared() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
targets := ctx.Config().Targets[android.Android]
|
|
|
|
|
|
|
|
for _, target := range targets {
|
|
|
|
if m.Target().Os == target.Os && m.Target().Arch.ArchType == target.Arch.ArchType {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}, func(m android.Module) {
|
|
|
|
ccModule, _ := m.(*Module)
|
|
|
|
outputFile := ccModule.outputFile
|
|
|
|
if outputFile.Valid() {
|
|
|
|
sanitizerLibStems = append(sanitizerLibStems, outputFile.Path().Base())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
sanitizerLibStems = android.SortedUniqueStrings(sanitizerLibStems)
|
|
|
|
return strings.Join(sanitizerLibStems, "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (txt *sanitizerLibrariesTxtModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
|
|
|
filename := txt.Name()
|
|
|
|
|
|
|
|
txt.outputFile = android.PathForModuleOut(ctx, filename).OutputPath
|
|
|
|
android.WriteFileRule(ctx, txt.outputFile, txt.getSanitizerLibs(ctx))
|
|
|
|
|
|
|
|
installPath := android.PathForModuleInstall(ctx, "etc")
|
|
|
|
ctx.InstallFile(installPath, filename, txt.outputFile)
|
2024-06-11 00:09:45 +02:00
|
|
|
|
|
|
|
ctx.SetOutputFiles(android.Paths{txt.outputFile}, "")
|
2024-05-07 07:47:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (txt *sanitizerLibrariesTxtModule) AndroidMkEntries() []android.AndroidMkEntries {
|
|
|
|
return []android.AndroidMkEntries{{
|
|
|
|
Class: "ETC",
|
|
|
|
OutputFile: android.OptionalPathForPath(txt.outputFile),
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrebuiltEtcModule interface
|
|
|
|
func (txt *sanitizerLibrariesTxtModule) BaseDir() string {
|
|
|
|
return "etc"
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrebuiltEtcModule interface
|
|
|
|
func (txt *sanitizerLibrariesTxtModule) SubDir() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (txt *sanitizerLibrariesTxtModule) OutputFiles(tag string) (android.Paths, error) {
|
|
|
|
return android.Paths{txt.outputFile}, nil
|
|
|
|
}
|