2016-01-07 06:41:07 +08: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 08:37:53 +08:00
|
|
|
"sort"
|
2016-01-07 06:41:07 +08:00
|
|
|
"strings"
|
2017-11-18 03:08:10 +08:00
|
|
|
"sync"
|
2016-01-07 06:41:07 +08:00
|
|
|
|
2018-06-22 04:03:07 +08:00
|
|
|
"github.com/google/blueprint"
|
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
"android/soong/android"
|
2016-08-16 05:18:24 +08:00
|
|
|
"android/soong/cc/config"
|
2016-01-07 06:41:07 +08:00
|
|
|
)
|
|
|
|
|
2017-06-16 05:45:18 +08: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 16:38:40 +08:00
|
|
|
asanCflags = []string{
|
|
|
|
"-fno-omit-frame-pointer",
|
|
|
|
"-fno-experimental-new-pass-manager",
|
|
|
|
}
|
2017-06-16 05:45:18 +08:00
|
|
|
asanLdflags = []string{"-Wl,-u,__asan_preinit"}
|
2017-02-14 23:59:33 +08:00
|
|
|
|
2019-03-20 12:39:54 +08:00
|
|
|
hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=",
|
2020-03-28 03:38:42 +08:00
|
|
|
"-fsanitize-hwaddress-abi=platform",
|
|
|
|
"-fno-experimental-new-pass-manager",
|
2019-11-23 10:37:10 +08: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.
|
|
|
|
"-mllvm", "-instcombine-lower-dbg-declare=0",
|
2020-06-23 04:28:23 +08:00
|
|
|
// TODO(b/159343917): HWASan and GlobalISel don't play nicely, and
|
|
|
|
// GlobalISel is the default at -O0 on aarch64.
|
|
|
|
"-mllvm", "--aarch64-enable-global-isel-at-O=-1",
|
|
|
|
"-mllvm", "-fast-isel=false",
|
2019-11-23 10:37:10 +08:00
|
|
|
}
|
2018-08-03 07:19:13 +08:00
|
|
|
|
2017-11-01 17:20:21 +08:00
|
|
|
cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
|
2020-07-28 02:22:35 +08:00
|
|
|
"-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blocklist.txt"}
|
2018-09-01 03:54:33 +08: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"}
|
2017-06-16 05:45:18 +08:00
|
|
|
cfiLdflags = []string{"-flto", "-fsanitize-cfi-cross-dso", "-fsanitize=cfi",
|
2017-08-29 12:50:17 +08:00
|
|
|
"-Wl,-plugin-opt,O1"}
|
2020-08-03 23:41:38 +08:00
|
|
|
cfiExportsMapPath = "build/soong/cc/config/cfi_exports.map"
|
2017-06-29 00:10:48 +08:00
|
|
|
|
2020-07-28 02:22:35 +08:00
|
|
|
intOverflowCflags = []string{"-fsanitize-blacklist=build/soong/cc/config/integer_overflow_blocklist.txt"}
|
2018-11-20 08:03:58 +08:00
|
|
|
|
2019-03-07 02:38:48 +08:00
|
|
|
minimalRuntimeFlags = []string{"-fsanitize-minimal-runtime", "-fno-sanitize-trap=integer,undefined",
|
2018-10-09 00:29:39 +08:00
|
|
|
"-fno-sanitize-recover=integer,undefined"}
|
2019-05-16 03:49:54 +08:00
|
|
|
hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
|
|
|
|
"export_memory_stats=0", "max_malloc_fill_size=0"}
|
2016-10-14 07:44:07 +08:00
|
|
|
)
|
|
|
|
|
2020-12-15 00:27:52 +08:00
|
|
|
type SanitizerType int
|
2016-01-07 06:41:07 +08:00
|
|
|
|
2016-07-08 01:54:07 +08:00
|
|
|
func boolPtr(v bool) *bool {
|
|
|
|
if v {
|
|
|
|
return &v
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 06:41:07 +08:00
|
|
|
const (
|
2020-12-15 00:27:52 +08:00
|
|
|
Asan SanitizerType = iota + 1
|
2021-04-02 02:29:09 +08:00
|
|
|
Hwasan
|
2016-01-07 06:41:07 +08:00
|
|
|
tsan
|
2017-06-29 00:10:48 +08:00
|
|
|
intOverflow
|
2017-11-01 17:20:21 +08:00
|
|
|
cfi
|
2018-11-20 08:03:58 +08:00
|
|
|
scs
|
2020-12-15 00:27:52 +08:00
|
|
|
Fuzzer
|
2020-04-29 06:09:12 +08:00
|
|
|
memtag_heap
|
2016-01-07 06:41:07 +08:00
|
|
|
)
|
|
|
|
|
2019-02-01 09:50:50 +08:00
|
|
|
// Name of the sanitizer variation for this sanitizer type
|
2020-12-15 00:27:52 +08:00
|
|
|
func (t SanitizerType) variationName() string {
|
2016-01-07 06:41:07 +08:00
|
|
|
switch t {
|
2020-12-15 00:27:52 +08:00
|
|
|
case Asan:
|
2016-01-07 06:41:07 +08:00
|
|
|
return "asan"
|
2021-04-02 02:29:09 +08:00
|
|
|
case Hwasan:
|
2018-08-03 07:19:13 +08:00
|
|
|
return "hwasan"
|
2016-01-07 06:41:07 +08:00
|
|
|
case tsan:
|
|
|
|
return "tsan"
|
2017-06-29 00:10:48 +08:00
|
|
|
case intOverflow:
|
|
|
|
return "intOverflow"
|
2017-11-01 17:20:21 +08:00
|
|
|
case cfi:
|
|
|
|
return "cfi"
|
2018-11-20 08:03:58 +08:00
|
|
|
case scs:
|
|
|
|
return "scs"
|
2020-04-29 06:09:12 +08:00
|
|
|
case memtag_heap:
|
|
|
|
return "memtag_heap"
|
2020-12-15 00:27:52 +08:00
|
|
|
case Fuzzer:
|
2019-05-02 05:42:05 +08:00
|
|
|
return "fuzzer"
|
2016-01-07 06:41:07 +08:00
|
|
|
default:
|
2020-12-15 00:27:52 +08:00
|
|
|
panic(fmt.Errorf("unknown SanitizerType %d", t))
|
2016-01-07 06:41:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 09:50:50 +08:00
|
|
|
// This is the sanitizer names in SANITIZE_[TARGET|HOST]
|
2020-12-15 00:27:52 +08:00
|
|
|
func (t SanitizerType) name() string {
|
2019-02-01 09:50:50 +08:00
|
|
|
switch t {
|
2020-12-15 00:27:52 +08:00
|
|
|
case Asan:
|
2019-02-01 09:50:50 +08:00
|
|
|
return "address"
|
2021-04-02 02:29:09 +08:00
|
|
|
case Hwasan:
|
2019-02-01 09:50:50 +08:00
|
|
|
return "hwaddress"
|
2020-04-29 06:09:12 +08:00
|
|
|
case memtag_heap:
|
|
|
|
return "memtag_heap"
|
2019-02-01 09:50:50 +08:00
|
|
|
case tsan:
|
|
|
|
return "thread"
|
|
|
|
case intOverflow:
|
|
|
|
return "integer_overflow"
|
|
|
|
case cfi:
|
|
|
|
return "cfi"
|
|
|
|
case scs:
|
|
|
|
return "shadow-call-stack"
|
2020-12-15 00:27:52 +08:00
|
|
|
case Fuzzer:
|
2019-05-02 05:42:05 +08:00
|
|
|
return "fuzzer"
|
2019-02-01 09:50:50 +08:00
|
|
|
default:
|
2020-12-15 00:27:52 +08:00
|
|
|
panic(fmt.Errorf("unknown SanitizerType %d", t))
|
2019-02-01 09:50:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 00:27:52 +08:00
|
|
|
func (*Module) SanitizerSupported(t SanitizerType) bool {
|
|
|
|
switch t {
|
|
|
|
case Asan:
|
|
|
|
return true
|
2021-04-02 02:29:09 +08:00
|
|
|
case Hwasan:
|
2020-12-15 00:27:52 +08:00
|
|
|
return true
|
|
|
|
case tsan:
|
|
|
|
return true
|
|
|
|
case intOverflow:
|
|
|
|
return true
|
|
|
|
case cfi:
|
|
|
|
return true
|
|
|
|
case scs:
|
|
|
|
return true
|
|
|
|
case Fuzzer:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// incompatibleWithCfi returns true if a sanitizer is incompatible with CFI.
|
|
|
|
func (t SanitizerType) incompatibleWithCfi() bool {
|
2021-04-02 02:29:09 +08:00
|
|
|
return t == Asan || t == Fuzzer || t == Hwasan
|
2019-07-29 20:27:18 +08:00
|
|
|
}
|
|
|
|
|
2020-09-15 09:32:35 +08:00
|
|
|
type SanitizeUserProps struct {
|
|
|
|
Never *bool `android:"arch_variant"`
|
|
|
|
|
|
|
|
// main sanitizers
|
|
|
|
Address *bool `android:"arch_variant"`
|
|
|
|
Thread *bool `android:"arch_variant"`
|
|
|
|
Hwaddress *bool `android:"arch_variant"`
|
|
|
|
|
|
|
|
// local sanitizers
|
|
|
|
Undefined *bool `android:"arch_variant"`
|
|
|
|
All_undefined *bool `android:"arch_variant"`
|
|
|
|
Misc_undefined []string `android:"arch_variant"`
|
|
|
|
Fuzzer *bool `android:"arch_variant"`
|
|
|
|
Safestack *bool `android:"arch_variant"`
|
|
|
|
Cfi *bool `android:"arch_variant"`
|
|
|
|
Integer_overflow *bool `android:"arch_variant"`
|
|
|
|
Scudo *bool `android:"arch_variant"`
|
|
|
|
Scs *bool `android:"arch_variant"`
|
2020-04-29 06:09:12 +08:00
|
|
|
Memtag_heap *bool `android:"arch_variant"`
|
2020-09-15 09:32:35 +08: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 {
|
2017-06-29 00:10:48 +08:00
|
|
|
Undefined *bool `android:"arch_variant"`
|
|
|
|
Cfi *bool `android:"arch_variant"`
|
|
|
|
Integer_overflow *bool `android:"arch_variant"`
|
2020-04-29 06:09:12 +08:00
|
|
|
Memtag_heap *bool `android:"arch_variant"`
|
2020-09-15 09:32:35 +08:00
|
|
|
Misc_undefined []string `android:"arch_variant"`
|
2020-12-02 03:14:30 +08:00
|
|
|
No_recover []string `android:"arch_variant"`
|
|
|
|
} `android:"arch_variant"`
|
2020-09-15 09:32:35 +08:00
|
|
|
|
2020-11-17 00:41:00 +08: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-02 03:14:30 +08:00
|
|
|
} `android:"arch_variant"`
|
2020-11-17 00:41:00 +08:00
|
|
|
|
2020-09-15 09:32:35 +08:00
|
|
|
// value to pass to -fsanitize-recover=
|
|
|
|
Recover []string
|
|
|
|
|
|
|
|
// value to pass to -fsanitize-blacklist
|
|
|
|
Blocklist *string
|
|
|
|
}
|
|
|
|
|
|
|
|
type SanitizeProperties struct {
|
|
|
|
// Enable AddressSanitizer, ThreadSanitizer, UndefinedBehaviorSanitizer, and
|
|
|
|
// others. Please see SanitizerUserProps in build/soong/cc/sanitize.go for
|
|
|
|
// details.
|
|
|
|
Sanitize SanitizeUserProps `android:"arch_variant"`
|
|
|
|
SanitizerEnabled bool `blueprint:"mutated"`
|
|
|
|
SanitizeDep 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-07 06:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type sanitize struct {
|
|
|
|
Properties SanitizeProperties
|
|
|
|
}
|
|
|
|
|
2020-12-10 23:12:38 +08: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-18 03:08:10 +08:00
|
|
|
func init() {
|
|
|
|
android.RegisterMakeVarsProvider(pctx, cfiMakeVarsProvider)
|
2018-08-03 07:19:13 +08:00
|
|
|
android.RegisterMakeVarsProvider(pctx, hwasanMakeVarsProvider)
|
2017-11-18 03:08:10 +08:00
|
|
|
}
|
|
|
|
|
2016-01-07 06:41:07 +08:00
|
|
|
func (sanitize *sanitize) props() []interface{} {
|
|
|
|
return []interface{}{&sanitize.Properties}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sanitize *sanitize) begin(ctx BaseModuleContext) {
|
2016-07-08 01:54:07 +08:00
|
|
|
s := &sanitize.Properties.Sanitize
|
|
|
|
|
2016-01-07 06:41:07 +08:00
|
|
|
// Don't apply sanitizers to NDK code.
|
2017-09-28 08:01:44 +08:00
|
|
|
if ctx.useSdk() {
|
2017-11-08 02:57:05 +08:00
|
|
|
s.Never = BoolPtr(true)
|
2016-01-07 06:41:07 +08:00
|
|
|
}
|
|
|
|
|
2019-01-18 06:44:05 +08:00
|
|
|
// Sanitizers do not work on Fuchsia yet.
|
|
|
|
if ctx.Fuchsia() {
|
|
|
|
s.Never = BoolPtr(true)
|
|
|
|
}
|
|
|
|
|
2016-01-07 06:41:07 +08:00
|
|
|
// Never always wins.
|
2017-11-08 02:57:05 +08:00
|
|
|
if Bool(s.Never) {
|
2016-01-07 06:41:07 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-13 10:28:33 +08:00
|
|
|
// cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap}).
|
|
|
|
if ctx.testBinary() && s.Memtag_heap == nil {
|
|
|
|
s.Memtag_heap = boolPtr(true)
|
|
|
|
s.Diag.Memtag_heap = boolPtr(true)
|
|
|
|
}
|
|
|
|
|
2016-01-07 06:41:07 +08:00
|
|
|
var globalSanitizers []string
|
2017-06-29 00:10:48 +08:00
|
|
|
var globalSanitizersDiag []string
|
|
|
|
|
2018-10-08 11:54:34 +08: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-07 06:41:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-08 01:38:41 +08:00
|
|
|
if len(globalSanitizers) > 0 {
|
|
|
|
var found bool
|
2016-07-08 01:54:07 +08:00
|
|
|
if found, globalSanitizers = removeFromList("undefined", globalSanitizers); found && s.All_undefined == nil {
|
|
|
|
s.All_undefined = boolPtr(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
if found, globalSanitizers = removeFromList("default-ub", globalSanitizers); found && s.Undefined == nil {
|
|
|
|
s.Undefined = boolPtr(true)
|
2016-07-08 01:38:41 +08:00
|
|
|
}
|
2016-01-07 06:41:07 +08:00
|
|
|
|
2019-05-02 05:42:05 +08:00
|
|
|
if found, globalSanitizers = removeFromList("address", globalSanitizers); found && s.Address == nil {
|
|
|
|
s.Address = boolPtr(true)
|
2016-07-08 01:38:41 +08:00
|
|
|
}
|
2016-01-07 06:41:07 +08:00
|
|
|
|
2016-07-08 01:54:07 +08:00
|
|
|
if found, globalSanitizers = removeFromList("thread", globalSanitizers); found && s.Thread == nil {
|
|
|
|
s.Thread = boolPtr(true)
|
2016-07-08 01:38:41 +08:00
|
|
|
}
|
2016-05-13 04:54:53 +08:00
|
|
|
|
2019-05-02 05:42:05 +08:00
|
|
|
if found, globalSanitizers = removeFromList("fuzzer", globalSanitizers); found && s.Fuzzer == nil {
|
|
|
|
s.Fuzzer = boolPtr(true)
|
2016-07-08 01:38:41 +08:00
|
|
|
}
|
2016-05-19 07:39:54 +08:00
|
|
|
|
2016-07-08 01:54:07 +08:00
|
|
|
if found, globalSanitizers = removeFromList("safe-stack", globalSanitizers); found && s.Safestack == nil {
|
|
|
|
s.Safestack = boolPtr(true)
|
2016-07-08 01:38:41 +08:00
|
|
|
}
|
2016-05-19 07:39:54 +08:00
|
|
|
|
2016-08-17 06:39:54 +08:00
|
|
|
if found, globalSanitizers = removeFromList("cfi", globalSanitizers); found && s.Cfi == nil {
|
2017-11-29 16:27:14 +08:00
|
|
|
if !ctx.Config().CFIDisabledForPath(ctx.ModuleDir()) {
|
2017-10-31 17:26:14 +08:00
|
|
|
s.Cfi = boolPtr(true)
|
|
|
|
}
|
2016-08-17 06:39:54 +08:00
|
|
|
}
|
|
|
|
|
2018-03-14 01:41:07 +08:00
|
|
|
// Global integer_overflow builds do not support static libraries.
|
2017-06-29 00:10:48 +08:00
|
|
|
if found, globalSanitizers = removeFromList("integer_overflow", globalSanitizers); found && s.Integer_overflow == nil {
|
2018-03-14 01:41:07 +08:00
|
|
|
if !ctx.Config().IntegerOverflowDisabledForPath(ctx.ModuleDir()) && !ctx.static() {
|
2017-07-14 05:46:05 +08:00
|
|
|
s.Integer_overflow = boolPtr(true)
|
|
|
|
}
|
2017-06-29 00:10:48 +08:00
|
|
|
}
|
|
|
|
|
2018-06-13 05:46:54 +08:00
|
|
|
if found, globalSanitizers = removeFromList("scudo", globalSanitizers); found && s.Scudo == nil {
|
|
|
|
s.Scudo = boolPtr(true)
|
|
|
|
}
|
|
|
|
|
2018-08-03 07:19:13 +08:00
|
|
|
if found, globalSanitizers = removeFromList("hwaddress", globalSanitizers); found && s.Hwaddress == nil {
|
|
|
|
s.Hwaddress = boolPtr(true)
|
|
|
|
}
|
|
|
|
|
2020-07-24 06:58:17 +08: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'")
|
|
|
|
}
|
|
|
|
s.Writeonly = boolPtr(true)
|
|
|
|
}
|
2020-04-29 06:09:12 +08:00
|
|
|
if found, globalSanitizers = removeFromList("memtag_heap", globalSanitizers); found && s.Memtag_heap == nil {
|
2021-01-06 08:41:26 +08:00
|
|
|
if !ctx.Config().MemtagHeapDisabledForPath(ctx.ModuleDir()) {
|
|
|
|
s.Memtag_heap = boolPtr(true)
|
|
|
|
}
|
2020-04-29 06:09:12 +08:00
|
|
|
}
|
2020-07-24 06:58:17 +08:00
|
|
|
|
2016-07-08 01:38:41 +08:00
|
|
|
if len(globalSanitizers) > 0 {
|
|
|
|
ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
|
|
|
|
}
|
2017-06-29 00:10:48 +08:00
|
|
|
|
2018-03-14 01:41:07 +08:00
|
|
|
// Global integer_overflow builds do not support static library diagnostics.
|
2017-06-29 00:10:48 +08:00
|
|
|
if found, globalSanitizersDiag = removeFromList("integer_overflow", globalSanitizersDiag); found &&
|
2018-03-14 01:41:07 +08:00
|
|
|
s.Diag.Integer_overflow == nil && Bool(s.Integer_overflow) && !ctx.static() {
|
2017-06-29 00:10:48 +08:00
|
|
|
s.Diag.Integer_overflow = boolPtr(true)
|
|
|
|
}
|
|
|
|
|
2017-10-31 17:26:14 +08:00
|
|
|
if found, globalSanitizersDiag = removeFromList("cfi", globalSanitizersDiag); found &&
|
|
|
|
s.Diag.Cfi == nil && Bool(s.Cfi) {
|
|
|
|
s.Diag.Cfi = boolPtr(true)
|
|
|
|
}
|
|
|
|
|
2021-01-13 10:28:33 +08:00
|
|
|
if found, globalSanitizersDiag = removeFromList("memtag_heap", globalSanitizersDiag); found &&
|
|
|
|
s.Diag.Memtag_heap == nil && Bool(s.Memtag_heap) {
|
|
|
|
s.Diag.Memtag_heap = boolPtr(true)
|
2017-06-29 00:10:48 +08:00
|
|
|
}
|
2016-07-19 06:44:56 +08:00
|
|
|
|
2021-01-13 10:28:33 +08:00
|
|
|
if len(globalSanitizersDiag) > 0 {
|
|
|
|
ctx.ModuleErrorf("unknown global sanitizer diagnostics option %s", globalSanitizersDiag[0])
|
2021-01-06 08:41:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable Memtag for all components in the include paths (for Aarch64 only)
|
2021-01-13 10:28:33 +08:00
|
|
|
if ctx.Arch().ArchType == android.Arm64 {
|
2021-01-06 08:41:26 +08:00
|
|
|
if ctx.Config().MemtagHeapSyncEnabledForPath(ctx.ModuleDir()) {
|
2021-01-13 10:28:33 +08:00
|
|
|
if s.Memtag_heap == nil {
|
|
|
|
s.Memtag_heap = boolPtr(true)
|
|
|
|
}
|
|
|
|
if s.Diag.Memtag_heap == nil {
|
|
|
|
s.Diag.Memtag_heap = boolPtr(true)
|
|
|
|
}
|
2021-01-06 08:41:26 +08:00
|
|
|
} else if ctx.Config().MemtagHeapAsyncEnabledForPath(ctx.ModuleDir()) {
|
2021-01-13 10:28:33 +08:00
|
|
|
if s.Memtag_heap == nil {
|
|
|
|
s.Memtag_heap = boolPtr(true)
|
|
|
|
}
|
2021-01-06 08:41:26 +08:00
|
|
|
}
|
2020-04-29 06:09:12 +08:00
|
|
|
}
|
|
|
|
|
2018-05-25 09:36:18 +08:00
|
|
|
// Enable CFI for all components in the include paths (for Aarch64 only)
|
|
|
|
if s.Cfi == nil && ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) && ctx.Arch().ArchType == android.Arm64 {
|
2018-03-30 10:55:23 +08:00
|
|
|
s.Cfi = boolPtr(true)
|
|
|
|
if inList("cfi", ctx.Config().SanitizeDeviceDiag()) {
|
|
|
|
s.Diag.Cfi = boolPtr(true)
|
2017-10-31 17:26:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 08:55:28 +08:00
|
|
|
// Is CFI actually enabled?
|
|
|
|
if !ctx.Config().EnableCFI() {
|
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 19:32:10 +08:00
|
|
|
s.Cfi = boolPtr(false)
|
|
|
|
s.Diag.Cfi = boolPtr(false)
|
2017-01-20 05:54:55 +08:00
|
|
|
}
|
|
|
|
|
2018-08-03 07:19:13 +08:00
|
|
|
// HWASan requires AArch64 hardware feature (top-byte-ignore).
|
|
|
|
if ctx.Arch().ArchType != android.Arm64 {
|
|
|
|
s.Hwaddress = nil
|
|
|
|
}
|
|
|
|
|
2018-11-20 08:03:58 +08:00
|
|
|
// SCS is only implemented on AArch64.
|
2019-03-07 02:38:48 +08:00
|
|
|
if ctx.Arch().ArchType != android.Arm64 {
|
2018-11-20 08:03:58 +08:00
|
|
|
s.Scs = nil
|
|
|
|
}
|
|
|
|
|
2020-04-29 06:09:12 +08:00
|
|
|
// memtag_heap is only implemented on AArch64.
|
|
|
|
if ctx.Arch().ArchType != android.Arm64 {
|
|
|
|
s.Memtag_heap = nil
|
|
|
|
}
|
|
|
|
|
2017-04-20 22:42:52 +08:00
|
|
|
// Also disable CFI if ASAN is enabled.
|
2018-08-03 07:19:13 +08:00
|
|
|
if Bool(s.Address) || Bool(s.Hwaddress) {
|
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 19:32:10 +08:00
|
|
|
s.Cfi = boolPtr(false)
|
|
|
|
s.Diag.Cfi = boolPtr(false)
|
2017-04-20 22:42:52 +08:00
|
|
|
}
|
|
|
|
|
2020-02-20 04:24:02 +08:00
|
|
|
// Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
|
|
|
|
if !ctx.Os().Linux() {
|
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 19:32:10 +08:00
|
|
|
s.Cfi = boolPtr(false)
|
|
|
|
s.Diag.Cfi = boolPtr(false)
|
2018-03-14 01:41:07 +08:00
|
|
|
s.Misc_undefined = nil
|
|
|
|
s.Undefined = nil
|
|
|
|
s.All_undefined = nil
|
|
|
|
s.Integer_overflow = nil
|
2017-11-18 03:08:10 +08:00
|
|
|
}
|
|
|
|
|
2018-05-29 04:54:48 +08:00
|
|
|
// Also disable CFI for VNDK variants of components
|
|
|
|
if ctx.isVndk() && ctx.useVndk() {
|
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 19:32:10 +08:00
|
|
|
if ctx.static() {
|
|
|
|
// Cfi variant for static vndk should be captured as vendor snapshot,
|
|
|
|
// so don't strictly disable Cfi.
|
|
|
|
s.Cfi = nil
|
|
|
|
s.Diag.Cfi = nil
|
|
|
|
} else {
|
|
|
|
s.Cfi = boolPtr(false)
|
|
|
|
s.Diag.Cfi = boolPtr(false)
|
|
|
|
}
|
2020-01-22 10:11:29 +08:00
|
|
|
}
|
|
|
|
|
2018-08-03 07:19:13 +08:00
|
|
|
// HWASan ramdisk (which is built from recovery) goes over some bootloader limit.
|
2020-10-22 06:17:56 +08:00
|
|
|
// Keep libc instrumented so that ramdisk / vendor_ramdisk / recovery can run hwasan-instrumented code if necessary.
|
|
|
|
if (ctx.inRamdisk() || ctx.inVendorRamdisk() || ctx.inRecovery()) && !strings.HasPrefix(ctx.ModuleDir(), "bionic/libc") {
|
2018-08-03 07:19:13 +08:00
|
|
|
s.Hwaddress = nil
|
|
|
|
}
|
|
|
|
|
2016-07-19 06:44:56 +08:00
|
|
|
if ctx.staticBinary() {
|
|
|
|
s.Address = nil
|
2019-05-02 05:42:05 +08:00
|
|
|
s.Fuzzer = nil
|
2016-07-19 06:44:56 +08:00
|
|
|
s.Thread = nil
|
2016-01-07 06:41:07 +08:00
|
|
|
}
|
|
|
|
|
2016-07-08 01:54:07 +08:00
|
|
|
if Bool(s.All_undefined) {
|
|
|
|
s.Undefined = nil
|
|
|
|
}
|
|
|
|
|
2016-05-13 04:54:53 +08:00
|
|
|
if !ctx.toolchain().Is64Bit() {
|
|
|
|
// TSAN and SafeStack are not supported on 32-bit architectures
|
2016-07-08 01:54:07 +08:00
|
|
|
s.Thread = nil
|
|
|
|
s.Safestack = nil
|
2016-01-07 06:41:07 +08:00
|
|
|
// TODO(ccross): error for compile_multilib = "32"?
|
|
|
|
}
|
|
|
|
|
2017-01-28 07:44:44 +08:00
|
|
|
if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) ||
|
2019-05-02 05:42:05 +08:00
|
|
|
Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
|
2020-04-29 06:09:12 +08:00
|
|
|
Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap)) {
|
2016-07-19 06:44:56 +08:00
|
|
|
sanitize.Properties.SanitizerEnabled = true
|
|
|
|
}
|
|
|
|
|
2019-02-02 00:42:56 +08: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-13 05:46:54 +08:00
|
|
|
s.Scudo = nil
|
|
|
|
}
|
|
|
|
|
2018-08-03 07:19:13 +08:00
|
|
|
if Bool(s.Hwaddress) {
|
|
|
|
s.Address = nil
|
|
|
|
s.Thread = nil
|
|
|
|
}
|
|
|
|
|
2019-05-02 05:42:05 +08:00
|
|
|
// TODO(b/131771163): CFI transiently depends on LTO, and thus Fuzzer is
|
|
|
|
// mutually incompatible.
|
|
|
|
if Bool(s.Fuzzer) {
|
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 19:32:10 +08:00
|
|
|
s.Cfi = boolPtr(false)
|
2016-01-07 06:41:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
|
|
|
|
if !sanitize.Properties.SanitizerEnabled { // || c.static() {
|
|
|
|
return deps
|
|
|
|
}
|
|
|
|
|
|
|
|
return deps
|
|
|
|
}
|
|
|
|
|
2018-11-16 06:01:36 +08: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 07:47:45 +08: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
|
|
|
|
}
|
|
|
|
|
2016-01-07 06:41:07 +08:00
|
|
|
func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
|
2018-05-11 05:17:22 +08:00
|
|
|
minimalRuntimeLib := config.UndefinedBehaviorSanitizerMinimalRuntimeLibrary(ctx.toolchain()) + ".a"
|
|
|
|
minimalRuntimePath := "${config.ClangAsanLibDir}/" + minimalRuntimeLib
|
2020-02-20 04:24:02 +08:00
|
|
|
builtinsRuntimeLib := config.BuiltinsRuntimeLibrary(ctx.toolchain()) + ".a"
|
|
|
|
builtinsRuntimePath := "${config.ClangAsanLibDir}/" + builtinsRuntimeLib
|
2018-02-22 07:49:20 +08:00
|
|
|
|
2020-02-20 04:24:02 +08:00
|
|
|
if sanitize.Properties.MinimalRuntimeDep {
|
2019-11-05 01:37:55 +08:00
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags,
|
|
|
|
minimalRuntimePath,
|
|
|
|
"-Wl,--exclude-libs,"+minimalRuntimeLib)
|
2018-02-22 07:49:20 +08:00
|
|
|
}
|
2020-02-20 04:24:02 +08:00
|
|
|
|
|
|
|
if sanitize.Properties.BuiltinsDep {
|
|
|
|
flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
|
|
|
|
}
|
|
|
|
|
2018-03-14 01:41:07 +08:00
|
|
|
if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
|
2016-01-07 06:41:07 +08:00
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2016-07-08 01:54:07 +08:00
|
|
|
if Bool(sanitize.Properties.Sanitize.Address) {
|
2016-05-19 06:37:25 +08:00
|
|
|
if ctx.Arch().ArchType == android.Arm {
|
2016-01-07 06:41:07 +08:00
|
|
|
// Frame pointer based unwinder in ASan requires ARM frame setup.
|
|
|
|
// TODO: put in flags?
|
|
|
|
flags.RequiredInstructionSet = "arm"
|
|
|
|
}
|
2019-11-05 01:37:55 +08:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...)
|
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...)
|
2016-01-07 06:41:07 +08:00
|
|
|
|
2020-07-24 06:58:17 +08:00
|
|
|
if Bool(sanitize.Properties.Sanitize.Writeonly) {
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0")
|
|
|
|
}
|
|
|
|
|
2016-01-07 06:41:07 +08:00
|
|
|
if ctx.Host() {
|
|
|
|
// -nodefaultlibs (provided with libc++) prevents the driver from linking
|
|
|
|
// libraries needed with -fsanitize=address. http://b/18650275 (WAI)
|
2019-11-05 01:37:55 +08:00
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--no-as-needed")
|
2016-01-07 06:41:07 +08:00
|
|
|
} else {
|
2019-11-05 01:37:55 +08:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-globals=0")
|
2019-02-02 12:13:38 +08:00
|
|
|
if ctx.bootstrap() {
|
|
|
|
flags.DynamicLinker = "/system/bin/bootstrap/linker_asan"
|
|
|
|
} else {
|
|
|
|
flags.DynamicLinker = "/system/bin/linker_asan"
|
|
|
|
}
|
2016-01-07 06:41:07 +08:00
|
|
|
if flags.Toolchain.Is64Bit() {
|
|
|
|
flags.DynamicLinker += "64"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 07:19:13 +08:00
|
|
|
if Bool(sanitize.Properties.Sanitize.Hwaddress) {
|
2019-11-05 01:37:55 +08:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...)
|
2020-07-24 06:58:17 +08:00
|
|
|
if Bool(sanitize.Properties.Sanitize.Writeonly) {
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0")
|
|
|
|
}
|
2017-10-20 06:52:11 +08:00
|
|
|
}
|
|
|
|
|
2019-05-02 05:42:05 +08:00
|
|
|
if Bool(sanitize.Properties.Sanitize.Fuzzer) {
|
2019-11-05 01:37:55 +08:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize=fuzzer-no-link")
|
2019-05-02 05:42:05 +08:00
|
|
|
|
|
|
|
// TODO(b/131771163): LTO and Fuzzer support is mutually incompatible.
|
2019-11-05 01:37:55 +08:00
|
|
|
_, 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")
|
2019-06-18 01:33:52 +08: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 08:18:59 +08: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-05 01:37:55 +08: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 08:18:59 +08:00
|
|
|
|
2019-06-18 01:33:52 +08:00
|
|
|
// TODO(b/133876586): Experimental PM breaks sanitizer coverage.
|
2019-11-05 01:37:55 +08:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fno-experimental-new-pass-manager")
|
2019-08-29 03:41:07 +08: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-05 01:37:55 +08:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-U_FORTIFY_SOURCE")
|
2019-12-11 00:44:52 +08: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-07 06:41:07 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:39:54 +08:00
|
|
|
if Bool(sanitize.Properties.Sanitize.Cfi) {
|
2017-01-21 06:13:06 +08: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 17:20:21 +08:00
|
|
|
|
2019-11-05 01:37:55 +08:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, cfiCflags...)
|
|
|
|
flags.Local.AsFlags = append(flags.Local.AsFlags, cfiAsflags...)
|
2020-11-17 00:41:00 +08:00
|
|
|
if Bool(sanitize.Properties.Sanitize.Config.Cfi_assembly_support) {
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-cfi-canonical-jump-tables")
|
|
|
|
}
|
2017-11-01 17:20:21 +08:00
|
|
|
// Only append the default visibility flag if -fvisibility has not already been set
|
|
|
|
// to hidden.
|
2019-11-05 01:37:55 +08:00
|
|
|
if !inList("-fvisibility=hidden", flags.Local.CFlags) {
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fvisibility=default")
|
2017-11-01 17:20:21 +08:00
|
|
|
}
|
2019-11-05 01:37:55 +08:00
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, cfiLdflags...)
|
2017-11-01 17:20:21 +08:00
|
|
|
|
|
|
|
if ctx.staticBinary() {
|
2019-11-05 01:37:55 +08: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 17:20:21 +08:00
|
|
|
}
|
2016-08-17 06:39:54 +08:00
|
|
|
}
|
|
|
|
|
2017-06-29 00:10:48 +08:00
|
|
|
if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
|
2019-11-05 01:37:55 +08:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
|
2017-06-29 00:10:48 +08:00
|
|
|
}
|
|
|
|
|
2018-12-19 01:47:14 +08:00
|
|
|
if len(sanitize.Properties.Sanitizers) > 0 {
|
|
|
|
sanitizeArg := "-fsanitize=" + strings.Join(sanitize.Properties.Sanitizers, ",")
|
2018-02-22 07:49:20 +08:00
|
|
|
|
2019-11-05 01:37:55 +08:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, sanitizeArg)
|
|
|
|
flags.Local.AsFlags = append(flags.Local.AsFlags, sanitizeArg)
|
2016-01-07 06:41:07 +08:00
|
|
|
if ctx.Host() {
|
2017-01-28 07:44:44 +08:00
|
|
|
// Host sanitizers only link symbols in the final executable, so
|
|
|
|
// there will always be undefined symbols in intermediate libraries.
|
2019-11-05 01:37:55 +08:00
|
|
|
_, flags.Global.LdFlags = removeFromList("-Wl,--no-undefined", flags.Global.LdFlags)
|
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, sanitizeArg)
|
2020-02-20 04:24:02 +08:00
|
|
|
|
|
|
|
// non-Bionic toolchain prebuilts are missing UBSan's vptr and function sanitizers
|
|
|
|
if !ctx.toolchain().Bionic() {
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=vptr,function")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if enableMinimalRuntime(sanitize) {
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, strings.Join(minimalRuntimeFlags, " "))
|
|
|
|
flags.libFlags = append([]string{minimalRuntimePath}, flags.libFlags...)
|
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--exclude-libs,"+minimalRuntimeLib)
|
|
|
|
if !ctx.toolchain().Bionic() {
|
|
|
|
flags.libFlags = append([]string{builtinsRuntimePath}, flags.libFlags...)
|
2018-02-22 07:49:20 +08:00
|
|
|
}
|
2016-01-07 06:41:07 +08:00
|
|
|
}
|
2019-05-02 05:42:05 +08:00
|
|
|
|
|
|
|
if Bool(sanitize.Properties.Sanitize.Fuzzer) {
|
|
|
|
// When fuzzing, we wish to crash with diagnostics on any bug.
|
2019-11-05 01:37:55 +08:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap=all", "-fno-sanitize-recover=all")
|
2019-05-02 05:42:05 +08:00
|
|
|
} else if ctx.Host() {
|
2019-11-05 01:37:55 +08:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover=all")
|
2019-05-02 05:42:05 +08:00
|
|
|
} else {
|
2019-11-05 01:37:55 +08:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-trap=all", "-ftrap-function=abort")
|
2019-05-02 05:42:05 +08:00
|
|
|
}
|
2018-11-16 06:01:36 +08:00
|
|
|
// http://b/119329758, Android core does not boot up with this sanitizer yet.
|
2019-11-05 01:37:55 +08:00
|
|
|
if toDisableImplicitIntegerChange(flags.Local.CFlags) {
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize=implicit-integer-sign-change")
|
2018-11-16 06:01:36 +08:00
|
|
|
}
|
2020-12-01 07:47:45 +08: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-07 06:41:07 +08:00
|
|
|
}
|
|
|
|
|
2018-12-19 01:47:14 +08:00
|
|
|
if len(sanitize.Properties.DiagSanitizers) > 0 {
|
2019-11-05 01:37:55 +08:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-trap="+strings.Join(sanitize.Properties.DiagSanitizers, ","))
|
2016-08-17 06:39:54 +08:00
|
|
|
}
|
|
|
|
// FIXME: enable RTTI if diag + (cfi or vptr)
|
|
|
|
|
2017-05-09 04:15:23 +08:00
|
|
|
if sanitize.Properties.Sanitize.Recover != nil {
|
2019-11-05 01:37:55 +08:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-recover="+
|
2017-05-09 04:15:23 +08:00
|
|
|
strings.Join(sanitize.Properties.Sanitize.Recover, ","))
|
|
|
|
}
|
|
|
|
|
2018-12-13 01:36:31 +08:00
|
|
|
if sanitize.Properties.Sanitize.Diag.No_recover != nil {
|
2019-11-05 01:37:55 +08:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fno-sanitize-recover="+
|
2018-12-13 01:36:31 +08:00
|
|
|
strings.Join(sanitize.Properties.Sanitize.Diag.No_recover, ","))
|
|
|
|
}
|
|
|
|
|
2020-07-28 02:49:51 +08:00
|
|
|
blocklist := android.OptionalPathForModuleSrc(ctx, sanitize.Properties.Sanitize.Blocklist)
|
|
|
|
if blocklist.Valid() {
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fsanitize-blacklist="+blocklist.String())
|
|
|
|
flags.CFlagsDeps = append(flags.CFlagsDeps, blocklist.Path())
|
|
|
|
}
|
|
|
|
|
2016-01-07 06:41:07 +08:00
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2020-02-25 04:01:37 +08:00
|
|
|
func (sanitize *sanitize) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {
|
2019-07-29 20:27:18 +08: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-25 04:01:37 +08:00
|
|
|
if entries.Class == "STATIC_LIBRARIES" || entries.Class == "HEADER_LIBRARIES" {
|
2019-07-29 20:27:18 +08:00
|
|
|
if Bool(sanitize.Properties.Sanitize.Cfi) {
|
2020-02-25 04:01:37 +08:00
|
|
|
entries.SubName += ".cfi"
|
2019-07-29 20:27:18 +08:00
|
|
|
}
|
|
|
|
if Bool(sanitize.Properties.Sanitize.Hwaddress) {
|
2020-02-25 04:01:37 +08:00
|
|
|
entries.SubName += ".hwasan"
|
2019-07-29 20:27:18 +08:00
|
|
|
}
|
|
|
|
if Bool(sanitize.Properties.Sanitize.Scs) {
|
2020-02-25 04:01:37 +08:00
|
|
|
entries.SubName += ".scs"
|
2019-07-29 20:27:18 +08:00
|
|
|
}
|
2018-11-20 08:03:58 +08:00
|
|
|
}
|
2017-05-09 04:44:11 +08:00
|
|
|
}
|
|
|
|
|
2017-03-30 13:00:18 +08:00
|
|
|
func (sanitize *sanitize) inSanitizerDir() bool {
|
|
|
|
return sanitize.Properties.InSanitizerDir
|
2016-05-04 09:02:42 +08:00
|
|
|
}
|
|
|
|
|
2020-12-15 00:27:52 +08:00
|
|
|
// getSanitizerBoolPtr returns the SanitizerTypes associated bool pointer from SanitizeProperties.
|
|
|
|
func (sanitize *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
|
2016-01-07 06:41:07 +08:00
|
|
|
switch t {
|
2020-12-15 00:27:52 +08:00
|
|
|
case Asan:
|
2017-11-01 17:20:21 +08:00
|
|
|
return sanitize.Properties.Sanitize.Address
|
2021-04-02 02:29:09 +08:00
|
|
|
case Hwasan:
|
2018-08-03 07:19:13 +08:00
|
|
|
return sanitize.Properties.Sanitize.Hwaddress
|
2016-01-07 06:41:07 +08:00
|
|
|
case tsan:
|
2017-11-01 17:20:21 +08:00
|
|
|
return sanitize.Properties.Sanitize.Thread
|
2017-06-29 00:10:48 +08:00
|
|
|
case intOverflow:
|
2017-11-01 17:20:21 +08:00
|
|
|
return sanitize.Properties.Sanitize.Integer_overflow
|
|
|
|
case cfi:
|
|
|
|
return sanitize.Properties.Sanitize.Cfi
|
2018-11-20 08:03:58 +08:00
|
|
|
case scs:
|
|
|
|
return sanitize.Properties.Sanitize.Scs
|
2020-04-29 06:09:12 +08:00
|
|
|
case memtag_heap:
|
|
|
|
return sanitize.Properties.Sanitize.Memtag_heap
|
2020-12-15 00:27:52 +08:00
|
|
|
case Fuzzer:
|
2019-05-02 05:42:05 +08:00
|
|
|
return sanitize.Properties.Sanitize.Fuzzer
|
2016-01-07 06:41:07 +08:00
|
|
|
default:
|
2020-12-15 00:27:52 +08:00
|
|
|
panic(fmt.Errorf("unknown SanitizerType %d", t))
|
2016-01-07 06:41:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 00:27:52 +08:00
|
|
|
// isUnsanitizedVariant returns true if no sanitizers are enabled.
|
2018-01-20 04:30:45 +08:00
|
|
|
func (sanitize *sanitize) isUnsanitizedVariant() bool {
|
2020-12-15 00:27:52 +08:00
|
|
|
return !sanitize.isSanitizerEnabled(Asan) &&
|
2021-04-02 02:29:09 +08:00
|
|
|
!sanitize.isSanitizerEnabled(Hwasan) &&
|
2018-01-20 04:30:45 +08:00
|
|
|
!sanitize.isSanitizerEnabled(tsan) &&
|
2018-11-20 08:03:58 +08:00
|
|
|
!sanitize.isSanitizerEnabled(cfi) &&
|
2019-05-02 05:42:05 +08:00
|
|
|
!sanitize.isSanitizerEnabled(scs) &&
|
2020-04-29 06:09:12 +08:00
|
|
|
!sanitize.isSanitizerEnabled(memtag_heap) &&
|
2020-12-15 00:27:52 +08:00
|
|
|
!sanitize.isSanitizerEnabled(Fuzzer)
|
2018-01-20 04:30:45 +08:00
|
|
|
}
|
|
|
|
|
2020-12-15 00:27:52 +08:00
|
|
|
// isVariantOnProductionDevice returns true if variant is for production devices (no non-production sanitizers enabled).
|
2018-05-11 06:29:24 +08:00
|
|
|
func (sanitize *sanitize) isVariantOnProductionDevice() bool {
|
2020-12-15 00:27:52 +08:00
|
|
|
return !sanitize.isSanitizerEnabled(Asan) &&
|
2021-04-02 02:29:09 +08:00
|
|
|
!sanitize.isSanitizerEnabled(Hwasan) &&
|
2019-05-02 05:42:05 +08:00
|
|
|
!sanitize.isSanitizerEnabled(tsan) &&
|
2020-12-15 00:27:52 +08:00
|
|
|
!sanitize.isSanitizerEnabled(Fuzzer)
|
2018-05-11 06:29:24 +08:00
|
|
|
}
|
|
|
|
|
2020-12-15 00:27:52 +08:00
|
|
|
func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
|
2017-08-03 05:18:08 +08:00
|
|
|
switch t {
|
2020-12-15 00:27:52 +08:00
|
|
|
case Asan:
|
2017-08-11 08:53:16 +08:00
|
|
|
sanitize.Properties.Sanitize.Address = boolPtr(b)
|
2021-04-02 02:29:09 +08:00
|
|
|
case Hwasan:
|
2018-08-03 07:19:13 +08:00
|
|
|
sanitize.Properties.Sanitize.Hwaddress = boolPtr(b)
|
2017-08-03 05:18:08 +08:00
|
|
|
case tsan:
|
2017-08-11 08:53:16 +08:00
|
|
|
sanitize.Properties.Sanitize.Thread = boolPtr(b)
|
2017-08-03 05:18:08 +08:00
|
|
|
case intOverflow:
|
2017-08-11 08:53:16 +08:00
|
|
|
sanitize.Properties.Sanitize.Integer_overflow = boolPtr(b)
|
2017-11-01 17:20:21 +08:00
|
|
|
case cfi:
|
|
|
|
sanitize.Properties.Sanitize.Cfi = boolPtr(b)
|
2018-11-20 08:03:58 +08:00
|
|
|
case scs:
|
|
|
|
sanitize.Properties.Sanitize.Scs = boolPtr(b)
|
2020-04-29 06:09:12 +08:00
|
|
|
case memtag_heap:
|
|
|
|
sanitize.Properties.Sanitize.Memtag_heap = boolPtr(b)
|
2020-12-15 00:27:52 +08:00
|
|
|
case Fuzzer:
|
2019-05-02 05:42:05 +08:00
|
|
|
sanitize.Properties.Sanitize.Fuzzer = boolPtr(b)
|
2017-08-03 05:18:08 +08:00
|
|
|
default:
|
2020-12-15 00:27:52 +08:00
|
|
|
panic(fmt.Errorf("unknown SanitizerType %d", t))
|
2017-08-03 05:18:08 +08:00
|
|
|
}
|
2017-08-11 08:53:16 +08:00
|
|
|
if b {
|
|
|
|
sanitize.Properties.SanitizerEnabled = true
|
2017-08-03 05:18:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-01 17:20:21 +08:00
|
|
|
// Check if the sanitizer is explicitly disabled (as opposed to nil by
|
|
|
|
// virtue of not being set).
|
2020-12-15 00:27:52 +08:00
|
|
|
func (sanitize *sanitize) isSanitizerExplicitlyDisabled(t SanitizerType) bool {
|
2017-11-01 17:20:21 +08: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.
|
2020-12-15 00:27:52 +08:00
|
|
|
func (sanitize *sanitize) isSanitizerEnabled(t SanitizerType) bool {
|
2017-11-01 17:20:21 +08:00
|
|
|
if sanitize == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
sanitizerVal := sanitize.getSanitizerBoolPtr(t)
|
|
|
|
return sanitizerVal != nil && *sanitizerVal == true
|
|
|
|
}
|
|
|
|
|
2020-12-15 00:27:52 +08:00
|
|
|
// IsSanitizableDependencyTag returns true if the dependency tag is sanitizable.
|
|
|
|
func IsSanitizableDependencyTag(tag blueprint.DependencyTag) bool {
|
2020-07-28 12:26:48 +08:00
|
|
|
switch t := tag.(type) {
|
|
|
|
case dependencyTag:
|
|
|
|
return t == reuseObjTag || t == objDepTag
|
|
|
|
case libraryDependencyTag:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2018-06-22 04:03:07 +08:00
|
|
|
}
|
|
|
|
|
2020-12-15 00:27:52 +08:00
|
|
|
func (m *Module) SanitizableDepTagChecker() SantizableDependencyTagChecker {
|
|
|
|
return IsSanitizableDependencyTag
|
|
|
|
}
|
|
|
|
|
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 19:32:10 +08:00
|
|
|
// Determines if the current module is a static library going to be captured
|
|
|
|
// as vendor snapshot. Such modules must create both cfi and non-cfi variants,
|
|
|
|
// except for ones which explicitly disable cfi.
|
|
|
|
func needsCfiForVendorSnapshot(mctx android.TopDownMutatorContext) bool {
|
2020-09-01 07:07:58 +08:00
|
|
|
if isVendorProprietaryModule(mctx) {
|
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 19:32:10 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-15 00:27:52 +08:00
|
|
|
c := mctx.Module().(PlatformSanitizeable)
|
|
|
|
|
|
|
|
if !c.InVendor() {
|
|
|
|
return false
|
|
|
|
}
|
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 19:32:10 +08:00
|
|
|
|
2020-12-15 00:27:52 +08:00
|
|
|
if !c.StaticallyLinked() {
|
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 19:32:10 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-15 00:27:52 +08:00
|
|
|
if c.IsPrebuilt() {
|
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 19:32:10 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-15 00:27:52 +08:00
|
|
|
if !c.SanitizerSupported(cfi) {
|
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 19:32:10 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-15 00:27:52 +08:00
|
|
|
return c.SanitizePropDefined() &&
|
|
|
|
!c.SanitizeNever() &&
|
|
|
|
!c.IsSanitizerExplicitlyDisabled(cfi)
|
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 19:32:10 +08:00
|
|
|
}
|
|
|
|
|
2018-08-03 07:19:13 +08:00
|
|
|
// Propagate sanitizer requirements down from binaries
|
2020-12-15 00:27:52 +08:00
|
|
|
func sanitizerDepsMutator(t SanitizerType) func(android.TopDownMutatorContext) {
|
2016-05-19 06:37:25 +08:00
|
|
|
return func(mctx android.TopDownMutatorContext) {
|
2020-12-15 00:27:52 +08:00
|
|
|
if c, ok := mctx.Module().(PlatformSanitizeable); ok {
|
|
|
|
enabled := c.IsSanitizerEnabled(t)
|
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 19:32:10 +08:00
|
|
|
if t == cfi && needsCfiForVendorSnapshot(mctx) {
|
|
|
|
// We shouldn't change the result of isSanitizerEnabled(cfi) to correctly
|
|
|
|
// determine defaultVariation in sanitizerMutator below.
|
|
|
|
// Instead, just mark SanitizeDep to forcefully create cfi variant.
|
|
|
|
enabled = true
|
2020-12-15 00:27:52 +08:00
|
|
|
c.SetSanitizeDep(true)
|
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 19:32:10 +08:00
|
|
|
}
|
|
|
|
if enabled {
|
2020-12-15 00:27:52 +08:00
|
|
|
isSanitizableDependencyTag := c.SanitizableDepTagChecker()
|
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 19:32:10 +08:00
|
|
|
mctx.WalkDeps(func(child, parent android.Module) bool {
|
|
|
|
if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
|
|
|
|
return false
|
|
|
|
}
|
2020-12-15 00:27:52 +08:00
|
|
|
if d, ok := child.(PlatformSanitizeable); ok && d.SanitizePropDefined() &&
|
|
|
|
!d.SanitizeNever() &&
|
|
|
|
!d.IsSanitizerExplicitlyDisabled(t) {
|
2021-04-02 02:29:09 +08:00
|
|
|
if t == cfi || t == Hwasan || t == scs {
|
2020-12-15 00:27:52 +08:00
|
|
|
if d.StaticallyLinked() && d.SanitizerSupported(t) {
|
|
|
|
// Rust does not support some of these sanitizers, so we need to check if it's
|
|
|
|
// supported before setting this true.
|
|
|
|
d.SetSanitizeDep(true)
|
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 19:32:10 +08:00
|
|
|
}
|
|
|
|
} else {
|
2020-12-15 00:27:52 +08:00
|
|
|
d.SetSanitizeDep(true)
|
2018-08-03 07:19:13 +08:00
|
|
|
}
|
2017-11-01 17:20:21 +08:00
|
|
|
}
|
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 19:32:10 +08:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
2019-02-13 19:28:58 +08:00
|
|
|
} else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok {
|
|
|
|
// If an APEX module includes a lib which is enabled for a sanitizer T, then
|
|
|
|
// the APEX module is also enabled for the same sanitizer type.
|
|
|
|
mctx.VisitDirectDeps(func(child android.Module) {
|
|
|
|
if c, ok := child.(*Module); ok && c.sanitize.isSanitizerEnabled(t) {
|
|
|
|
sanitizeable.EnableSanitizer(t.name())
|
|
|
|
}
|
|
|
|
})
|
2016-01-07 06:41:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 00:27:52 +08:00
|
|
|
func (c *Module) SanitizeNever() bool {
|
|
|
|
return Bool(c.sanitize.Properties.Sanitize.Never)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Module) IsSanitizerExplicitlyDisabled(t SanitizerType) bool {
|
|
|
|
return c.sanitize.isSanitizerExplicitlyDisabled(t)
|
|
|
|
}
|
|
|
|
|
2018-02-22 07:49:20 +08:00
|
|
|
// Propagate the ubsan minimal runtime dependency when there are integer overflow sanitized static dependencies.
|
2018-06-22 04:03:07 +08:00
|
|
|
func sanitizerRuntimeDepsMutator(mctx android.TopDownMutatorContext) {
|
2020-12-15 00:27:52 +08:00
|
|
|
// Change this to PlatformSanitizable when/if non-cc modules support ubsan sanitizers.
|
2018-06-22 04:03:07 +08:00
|
|
|
if c, ok := mctx.Module().(*Module); ok && c.sanitize != nil {
|
2020-12-15 00:27:52 +08:00
|
|
|
isSanitizableDependencyTag := c.SanitizableDepTagChecker()
|
2018-06-22 04:03:07 +08:00
|
|
|
mctx.WalkDeps(func(child, parent android.Module) bool {
|
|
|
|
if !isSanitizableDependencyTag(mctx.OtherModuleDependencyTag(child)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-22 10:11:29 +08:00
|
|
|
d, ok := child.(*Module)
|
|
|
|
if !ok || !d.static() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if d.sanitize != nil {
|
2018-06-22 04:03:07 +08: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 08:59:12 +08:00
|
|
|
} else if enableUbsanRuntime(d.sanitize) {
|
2018-06-22 04:03:07 +08: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 07:49:20 +08:00
|
|
|
}
|
2019-06-20 14:00:20 +08: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-20 04:24:02 +08:00
|
|
|
if c.Os() == android.Linux {
|
|
|
|
c.sanitize.Properties.BuiltinsDep = true
|
|
|
|
}
|
|
|
|
|
2019-06-20 14:00:20 +08:00
|
|
|
return true
|
2018-06-22 04:03:07 +08:00
|
|
|
}
|
2020-01-22 10:11:29 +08:00
|
|
|
|
2020-11-14 04:07:36 +08:00
|
|
|
if p, ok := d.linker.(*snapshotLibraryDecorator); ok {
|
2020-01-22 10:11:29 +08:00
|
|
|
if Bool(p.properties.Sanitize_minimal_dep) {
|
|
|
|
c.sanitize.Properties.MinimalRuntimeDep = true
|
|
|
|
}
|
|
|
|
if Bool(p.properties.Sanitize_ubsan_dep) {
|
|
|
|
c.sanitize.Properties.UbsanRuntimeDep = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2018-06-22 04:03:07 +08:00
|
|
|
})
|
2018-02-22 07:49:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 01:47:14 +08: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 {
|
2019-01-25 08:20:35 +08:00
|
|
|
if !c.Enabled() {
|
|
|
|
return
|
|
|
|
}
|
2018-12-19 01:47:14 +08:00
|
|
|
var sanitizers []string
|
|
|
|
var diagSanitizers []string
|
|
|
|
|
|
|
|
if Bool(c.sanitize.Properties.Sanitize.All_undefined) {
|
|
|
|
sanitizers = append(sanitizers, "undefined")
|
|
|
|
} else {
|
|
|
|
if Bool(c.sanitize.Properties.Sanitize.Undefined) {
|
|
|
|
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",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
sanitizers = append(sanitizers, c.sanitize.Properties.Sanitize.Misc_undefined...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if Bool(c.sanitize.Properties.Sanitize.Diag.Undefined) {
|
|
|
|
diagSanitizers = append(diagSanitizers, "undefined")
|
|
|
|
}
|
|
|
|
|
|
|
|
diagSanitizers = append(diagSanitizers, c.sanitize.Properties.Sanitize.Diag.Misc_undefined...)
|
|
|
|
|
|
|
|
if Bool(c.sanitize.Properties.Sanitize.Address) {
|
|
|
|
sanitizers = append(sanitizers, "address")
|
|
|
|
diagSanitizers = append(diagSanitizers, "address")
|
|
|
|
}
|
|
|
|
|
|
|
|
if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
|
|
|
|
sanitizers = append(sanitizers, "hwaddress")
|
|
|
|
}
|
|
|
|
|
|
|
|
if Bool(c.sanitize.Properties.Sanitize.Thread) {
|
|
|
|
sanitizers = append(sanitizers, "thread")
|
|
|
|
}
|
|
|
|
|
|
|
|
if Bool(c.sanitize.Properties.Sanitize.Safestack) {
|
|
|
|
sanitizers = append(sanitizers, "safe-stack")
|
|
|
|
}
|
|
|
|
|
|
|
|
if Bool(c.sanitize.Properties.Sanitize.Cfi) {
|
|
|
|
sanitizers = append(sanitizers, "cfi")
|
|
|
|
|
|
|
|
if Bool(c.sanitize.Properties.Sanitize.Diag.Cfi) {
|
|
|
|
diagSanitizers = append(diagSanitizers, "cfi")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if Bool(c.sanitize.Properties.Sanitize.Integer_overflow) {
|
|
|
|
sanitizers = append(sanitizers, "unsigned-integer-overflow")
|
|
|
|
sanitizers = append(sanitizers, "signed-integer-overflow")
|
|
|
|
if Bool(c.sanitize.Properties.Sanitize.Diag.Integer_overflow) {
|
|
|
|
diagSanitizers = append(diagSanitizers, "unsigned-integer-overflow")
|
|
|
|
diagSanitizers = append(diagSanitizers, "signed-integer-overflow")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if Bool(c.sanitize.Properties.Sanitize.Scudo) {
|
|
|
|
sanitizers = append(sanitizers, "scudo")
|
|
|
|
}
|
|
|
|
|
|
|
|
if Bool(c.sanitize.Properties.Sanitize.Scs) {
|
|
|
|
sanitizers = append(sanitizers, "shadow-call-stack")
|
|
|
|
}
|
|
|
|
|
2020-04-29 06:09:12 +08:00
|
|
|
if Bool(c.sanitize.Properties.Sanitize.Memtag_heap) && c.binary() {
|
|
|
|
noteDep := "note_memtag_heap_async"
|
|
|
|
if Bool(c.sanitize.Properties.Sanitize.Diag.Memtag_heap) {
|
|
|
|
noteDep = "note_memtag_heap_sync"
|
|
|
|
}
|
2021-04-08 16:10:31 +08:00
|
|
|
// If we're using snapshots, redirect to snapshot whenever possible
|
|
|
|
// TODO(b/178470649): clean manual snapshot redirections
|
|
|
|
snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
|
|
|
|
if lib, ok := snapshot.StaticLibs[noteDep]; ok {
|
|
|
|
noteDep = lib
|
|
|
|
}
|
2020-04-29 06:09:12 +08:00
|
|
|
depTag := libraryDependencyTag{Kind: staticLibraryDependency, wholeStatic: true}
|
|
|
|
variations := append(mctx.Target().Variations(),
|
|
|
|
blueprint.Variation{Mutator: "link", Variation: "static"})
|
|
|
|
if c.Device() {
|
|
|
|
variations = append(variations, c.ImageVariation())
|
|
|
|
}
|
|
|
|
mctx.AddFarVariationDependencies(variations, depTag, noteDep)
|
|
|
|
}
|
|
|
|
|
2019-05-02 05:42:05 +08:00
|
|
|
if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {
|
|
|
|
sanitizers = append(sanitizers, "fuzzer-no-link")
|
|
|
|
}
|
|
|
|
|
2018-12-19 01:47:14 +08: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-07 01:01:21 +08:00
|
|
|
// TODO(b/150822854) Hosts have a different default behavior and assume the runtime library is used.
|
|
|
|
if c.Host() {
|
|
|
|
diagSanitizers = sanitizers
|
|
|
|
}
|
|
|
|
|
2018-12-19 01:47:14 +08:00
|
|
|
// Determine the runtime library required
|
|
|
|
runtimeLibrary := ""
|
Stop linking libdl.a into static bins
libdl.a has a no-op dlopen, which breaks static libraries that need a real
dlopen. Instead of automatically linking libdl.a into static executables,
make it optional.
Until recently, the libunwind_llvm.a unwinder, used on arm32, needed the
no-op dladdr, but it's now built using -D_LIBUNWIND_USE_DLADDR=0.
The HWASan run-time uses dlsym and dladdr, so add a libdl dependency for
HWASan-built static binaries. We could also remove the dependency from
libclang_rt.hwasan_static-*.a, but this is also easy to do.
Bug: http://b/141485154
Test: bionic unit tests, device boots, verify that static and dynamic
executables can throw/catch an exception
Test: verify that a static executable using dlopen doesn't link (unless it
adds an explicit dependency on libdl)
Change-Id: Ic52c3f336b671b4ed335e99c94a64dfe8614b618
2019-10-12 06:03:34 +08:00
|
|
|
var extraStaticDeps []string
|
2018-12-19 01:47:14 +08:00
|
|
|
toolchain := c.toolchain(mctx)
|
|
|
|
if Bool(c.sanitize.Properties.Sanitize.Address) {
|
|
|
|
runtimeLibrary = config.AddressSanitizerRuntimeLibrary(toolchain)
|
|
|
|
} else if Bool(c.sanitize.Properties.Sanitize.Hwaddress) {
|
|
|
|
if c.staticBinary() {
|
|
|
|
runtimeLibrary = config.HWAddressSanitizerStaticLibrary(toolchain)
|
Stop linking libdl.a into static bins
libdl.a has a no-op dlopen, which breaks static libraries that need a real
dlopen. Instead of automatically linking libdl.a into static executables,
make it optional.
Until recently, the libunwind_llvm.a unwinder, used on arm32, needed the
no-op dladdr, but it's now built using -D_LIBUNWIND_USE_DLADDR=0.
The HWASan run-time uses dlsym and dladdr, so add a libdl dependency for
HWASan-built static binaries. We could also remove the dependency from
libclang_rt.hwasan_static-*.a, but this is also easy to do.
Bug: http://b/141485154
Test: bionic unit tests, device boots, verify that static and dynamic
executables can throw/catch an exception
Test: verify that a static executable using dlopen doesn't link (unless it
adds an explicit dependency on libdl)
Change-Id: Ic52c3f336b671b4ed335e99c94a64dfe8614b618
2019-10-12 06:03:34 +08:00
|
|
|
extraStaticDeps = []string{"libdl"}
|
2018-12-19 01:47:14 +08:00
|
|
|
} else {
|
|
|
|
runtimeLibrary = config.HWAddressSanitizerRuntimeLibrary(toolchain)
|
|
|
|
}
|
|
|
|
} else if Bool(c.sanitize.Properties.Sanitize.Thread) {
|
|
|
|
runtimeLibrary = config.ThreadSanitizerRuntimeLibrary(toolchain)
|
|
|
|
} else if Bool(c.sanitize.Properties.Sanitize.Scudo) {
|
|
|
|
if len(diagSanitizers) == 0 && !c.sanitize.Properties.UbsanRuntimeDep {
|
|
|
|
runtimeLibrary = config.ScudoMinimalRuntimeLibrary(toolchain)
|
|
|
|
} else {
|
|
|
|
runtimeLibrary = config.ScudoRuntimeLibrary(toolchain)
|
|
|
|
}
|
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 08:18:59 +08:00
|
|
|
} else if len(diagSanitizers) > 0 || c.sanitize.Properties.UbsanRuntimeDep ||
|
2020-02-20 04:24:02 +08:00
|
|
|
Bool(c.sanitize.Properties.Sanitize.Fuzzer) ||
|
|
|
|
Bool(c.sanitize.Properties.Sanitize.Undefined) ||
|
|
|
|
Bool(c.sanitize.Properties.Sanitize.All_undefined) {
|
2018-12-19 01:47:14 +08:00
|
|
|
runtimeLibrary = config.UndefinedBehaviorSanitizerRuntimeLibrary(toolchain)
|
2021-03-30 04:41:37 +08:00
|
|
|
if c.staticBinary() {
|
|
|
|
runtimeLibrary += ".static"
|
|
|
|
}
|
2018-12-19 01:47:14 +08:00
|
|
|
}
|
|
|
|
|
2020-02-20 04:24:02 +08:00
|
|
|
if runtimeLibrary != "" && (toolchain.Bionic() || c.sanitize.Properties.UbsanRuntimeDep) {
|
|
|
|
// UBSan is supported on non-bionic linux host builds as well
|
2018-12-19 01:47:14 +08: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
|
|
|
|
if c.staticBinary() {
|
2020-01-22 10:11:29 +08:00
|
|
|
deps := append(extraStaticDeps, runtimeLibrary)
|
2021-01-12 09:31:17 +08:00
|
|
|
// If we're using snapshots, redirect to snapshot whenever possible
|
|
|
|
snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
|
|
|
|
for idx, dep := range deps {
|
|
|
|
if lib, ok := snapshot.StaticLibs[dep]; ok {
|
|
|
|
deps[idx] = lib
|
2020-01-22 10:11:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 01:47:14 +08:00
|
|
|
// static executable gets static runtime libs
|
2020-07-28 12:26:48 +08:00
|
|
|
depTag := libraryDependencyTag{Kind: staticLibraryDependency}
|
2020-08-22 07:15:23 +08:00
|
|
|
variations := append(mctx.Target().Variations(),
|
|
|
|
blueprint.Variation{Mutator: "link", Variation: "static"})
|
|
|
|
if c.Device() {
|
|
|
|
variations = append(variations, c.ImageVariation())
|
|
|
|
}
|
|
|
|
mctx.AddFarVariationDependencies(variations, depTag, deps...)
|
2020-12-15 00:27:52 +08:00
|
|
|
} else if !c.static() && !c.Header() {
|
2021-01-12 09:31:17 +08:00
|
|
|
// If we're using snapshots, redirect to snapshot whenever possible
|
|
|
|
snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
|
|
|
|
if lib, ok := snapshot.SharedLibs[runtimeLibrary]; ok {
|
|
|
|
runtimeLibrary = lib
|
2020-01-22 10:11:29 +08:00
|
|
|
}
|
2021-01-12 09:31:17 +08:00
|
|
|
|
2020-12-10 23:12:38 +08: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 10:15:04 +08:00
|
|
|
// dynamic executable and shared libs get shared runtime libs
|
2020-12-10 23:12:38 +08:00
|
|
|
depTag := libraryDependencyTag{
|
|
|
|
Kind: sharedLibraryDependency,
|
|
|
|
Order: earlyLibraryDependency,
|
|
|
|
|
|
|
|
skipApexAllowedDependenciesCheck: diagEnabled,
|
|
|
|
}
|
2020-08-22 07:15:23 +08:00
|
|
|
variations := append(mctx.Target().Variations(),
|
|
|
|
blueprint.Variation{Mutator: "link", Variation: "shared"})
|
|
|
|
if c.Device() {
|
|
|
|
variations = append(variations, c.ImageVariation())
|
|
|
|
}
|
2020-09-25 00:56:18 +08:00
|
|
|
c.addSharedLibDependenciesWithVersions(mctx, variations, depTag, runtimeLibrary, "", true)
|
2018-12-19 01:47:14 +08: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
|
2019-01-28 18:47:32 +08:00
|
|
|
IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool
|
2019-02-13 19:28:58 +08:00
|
|
|
EnableSanitizer(sanitizerName string)
|
2020-05-15 18:05:05 +08:00
|
|
|
AddSanitizerDependencies(ctx android.BottomUpMutatorContext, sanitizerName string)
|
2018-12-19 01:47:14 +08:00
|
|
|
}
|
|
|
|
|
2020-12-15 00:27:52 +08: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) SanitizeDep() bool {
|
|
|
|
return c.sanitize.Properties.SanitizeDep
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Module) SetSanitizeDep(b bool) {
|
|
|
|
if c.sanitize != nil {
|
|
|
|
c.sanitize.Properties.SanitizeDep = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ PlatformSanitizeable = (*Module)(nil)
|
|
|
|
|
2017-11-01 17:20:21 +08:00
|
|
|
// Create sanitized variants for modules that need them
|
2020-12-15 00:27:52 +08:00
|
|
|
func sanitizerMutator(t SanitizerType) func(android.BottomUpMutatorContext) {
|
2016-05-19 06:37:25 +08:00
|
|
|
return func(mctx android.BottomUpMutatorContext) {
|
2020-12-15 00:27:52 +08:00
|
|
|
if c, ok := mctx.Module().(PlatformSanitizeable); ok && c.SanitizePropDefined() {
|
|
|
|
if c.IsDependencyRoot() && c.IsSanitizerEnabled(t) {
|
2019-02-01 09:50:50 +08:00
|
|
|
modules := mctx.CreateVariations(t.variationName())
|
2020-12-15 00:27:52 +08:00
|
|
|
modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
|
|
|
|
} else if c.IsSanitizerEnabled(t) || c.SanitizeDep() {
|
|
|
|
isSanitizerEnabled := c.IsSanitizerEnabled(t)
|
|
|
|
if c.StaticallyLinked() || c.Header() || t == Asan || t == Fuzzer {
|
2019-07-29 20:27:18 +08:00
|
|
|
// Static and header libs are split into non-sanitized and sanitized variants.
|
|
|
|
// Shared libs are not split. However, for asan and fuzzer, we split even for shared
|
|
|
|
// libs because a library sanitized for asan/fuzzer can't be linked from a library
|
|
|
|
// that isn't sanitized for asan/fuzzer.
|
|
|
|
//
|
|
|
|
// Note for defaultVariation: since we don't split for shared libs but for static/header
|
|
|
|
// libs, it is possible for the sanitized variant of a static/header lib to depend
|
|
|
|
// on non-sanitized variant of a shared lib. Such unfulfilled variation causes an
|
|
|
|
// error when the module is split. defaultVariation is the name of the variation that
|
|
|
|
// will be used when such a dangling dependency occurs during the split of the current
|
|
|
|
// module. By setting it to the name of the sanitized variation, the dangling dependency
|
|
|
|
// is redirected to the sanitized variant of the dependent module.
|
|
|
|
defaultVariation := t.variationName()
|
2020-12-15 00:27:52 +08:00
|
|
|
// Not all PlatformSanitizeable modules support the CFI sanitizer
|
|
|
|
cfiSupported := mctx.Module().(PlatformSanitizeable).SanitizerSupported(cfi)
|
2019-07-29 20:27:18 +08:00
|
|
|
mctx.SetDefaultDependencyVariation(&defaultVariation)
|
2020-12-15 00:27:52 +08:00
|
|
|
|
2019-07-29 20:27:18 +08:00
|
|
|
modules := mctx.CreateVariations("", t.variationName())
|
2020-12-15 00:27:52 +08:00
|
|
|
modules[0].(PlatformSanitizeable).SetSanitizer(t, false)
|
|
|
|
modules[1].(PlatformSanitizeable).SetSanitizer(t, true)
|
|
|
|
modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
|
|
|
|
modules[1].(PlatformSanitizeable).SetSanitizeDep(false)
|
2019-07-29 20:27:18 +08:00
|
|
|
|
2020-12-15 00:27:52 +08:00
|
|
|
if mctx.Device() && t.incompatibleWithCfi() && cfiSupported {
|
2020-03-11 04:23:57 +08:00
|
|
|
// TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
|
|
|
|
// are incompatible with cfi
|
2020-12-15 00:27:52 +08:00
|
|
|
modules[1].(PlatformSanitizeable).SetSanitizer(cfi, false)
|
2020-03-11 04:23:57 +08:00
|
|
|
}
|
|
|
|
|
2019-07-29 20:27:18 +08:00
|
|
|
// For cfi/scs/hwasan, we can export both sanitized and un-sanitized variants
|
|
|
|
// to Make, because the sanitized version has a different suffix in name.
|
|
|
|
// For other types of sanitizers, suppress the variation that is disabled.
|
2021-04-02 02:29:09 +08:00
|
|
|
if t != cfi && t != scs && t != Hwasan {
|
2018-11-20 08:03:58 +08:00
|
|
|
if isSanitizerEnabled {
|
2020-12-15 00:27:52 +08:00
|
|
|
modules[0].(PlatformSanitizeable).SetPreventInstall()
|
|
|
|
modules[0].(PlatformSanitizeable).SetHideFromMake()
|
2018-11-20 08:03:58 +08:00
|
|
|
} else {
|
2020-12-15 00:27:52 +08:00
|
|
|
modules[1].(PlatformSanitizeable).SetPreventInstall()
|
|
|
|
modules[1].(PlatformSanitizeable).SetHideFromMake()
|
2018-11-20 08:03:58 +08:00
|
|
|
}
|
|
|
|
}
|
2018-08-03 07:19:13 +08:00
|
|
|
|
2019-07-29 20:27:18 +08:00
|
|
|
// Export the static lib name to make
|
2020-12-15 00:27:52 +08:00
|
|
|
if c.StaticallyLinked() && c.ExportedToMake() {
|
2019-07-29 20:27:18 +08:00
|
|
|
if t == cfi {
|
2020-12-15 00:27:52 +08:00
|
|
|
cfiStaticLibs(mctx.Config()).add(c, c.Module().Name())
|
2021-04-02 02:29:09 +08:00
|
|
|
} else if t == Hwasan {
|
2020-12-15 00:27:52 +08:00
|
|
|
hwasanStaticLibs(mctx.Config()).add(c, c.Module().Name())
|
2018-08-03 07:19:13 +08:00
|
|
|
}
|
|
|
|
}
|
2019-07-29 20:27:18 +08:00
|
|
|
} else {
|
|
|
|
// Shared libs are not split. Only the sanitized variant is created.
|
|
|
|
modules := mctx.CreateVariations(t.variationName())
|
2020-12-15 00:27:52 +08:00
|
|
|
modules[0].(PlatformSanitizeable).SetSanitizer(t, true)
|
|
|
|
modules[0].(PlatformSanitizeable).SetSanitizeDep(false)
|
2019-07-29 20:27:18 +08:00
|
|
|
|
|
|
|
// locate the asan libraries under /data/asan
|
2020-12-15 00:27:52 +08:00
|
|
|
if mctx.Device() && t == Asan && isSanitizerEnabled {
|
|
|
|
modules[0].(PlatformSanitizeable).SetInSanitizerDir()
|
2019-07-29 20:27:18 +08:00
|
|
|
}
|
2020-03-11 04:23:57 +08:00
|
|
|
|
|
|
|
if mctx.Device() && t.incompatibleWithCfi() {
|
|
|
|
// TODO: Make sure that cfi mutator runs "after" any of the sanitizers that
|
|
|
|
// are incompatible with cfi
|
2020-12-15 00:27:52 +08:00
|
|
|
modules[0].(PlatformSanitizeable).SetSanitizer(cfi, false)
|
2020-03-11 04:23:57 +08:00
|
|
|
}
|
2017-11-02 10:42:45 +08:00
|
|
|
}
|
2016-01-07 06:41:07 +08:00
|
|
|
}
|
2020-12-15 00:27:52 +08:00
|
|
|
c.SetSanitizeDep(false)
|
2019-02-01 09:50:50 +08:00
|
|
|
} else if sanitizeable, ok := mctx.Module().(Sanitizeable); ok && sanitizeable.IsSanitizerEnabled(mctx, t.name()) {
|
2018-12-19 01:47:14 +08:00
|
|
|
// APEX modules fall here
|
2020-05-15 18:05:05 +08:00
|
|
|
sanitizeable.AddSanitizerDependencies(mctx, t.name())
|
2019-02-01 09:50:50 +08:00
|
|
|
mctx.CreateVariations(t.variationName())
|
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 19:32:10 +08:00
|
|
|
} else if c, ok := mctx.Module().(*Module); ok {
|
2020-12-15 00:27:52 +08:00
|
|
|
//TODO: When Rust modules have vendor support, enable this path for PlatformSanitizeable
|
|
|
|
|
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 19:32:10 +08:00
|
|
|
// Check if it's a snapshot module supporting sanitizer
|
|
|
|
if s, ok := c.linker.(snapshotSanitizer); ok && s.isSanitizerEnabled(t) {
|
|
|
|
// Set default variation as above.
|
|
|
|
defaultVariation := t.variationName()
|
|
|
|
mctx.SetDefaultDependencyVariation(&defaultVariation)
|
|
|
|
modules := mctx.CreateVariations("", t.variationName())
|
|
|
|
modules[0].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, false)
|
|
|
|
modules[1].(*Module).linker.(snapshotSanitizer).setSanitizerVariation(t, true)
|
|
|
|
|
|
|
|
// Export the static lib name to make
|
|
|
|
if c.static() && c.ExportedToMake() {
|
|
|
|
if t == cfi {
|
|
|
|
// use BaseModuleName which is the name for Make.
|
|
|
|
cfiStaticLibs(mctx.Config()).add(c, c.BaseModuleName())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-07 06:41:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-18 03:08:10 +08:00
|
|
|
|
2020-08-03 23:41:38 +08: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-15 00:27:52 +08:00
|
|
|
libsMap map[ImageVariantType]map[string][]string
|
2020-08-03 23:41:38 +08:00
|
|
|
libsMapLock sync.Mutex
|
2020-12-15 00:27:52 +08:00
|
|
|
sanitizerType SanitizerType
|
2020-08-03 23:41:38 +08:00
|
|
|
}
|
2019-02-05 03:22:08 +08:00
|
|
|
|
2020-12-15 00:27:52 +08:00
|
|
|
func newSanitizerStaticLibsMap(t SanitizerType) *sanitizerStaticLibsMap {
|
2020-08-03 23:41:38 +08:00
|
|
|
return &sanitizerStaticLibsMap{
|
|
|
|
sanitizerType: t,
|
2020-12-15 00:27:52 +08:00
|
|
|
libsMap: make(map[ImageVariantType]map[string][]string),
|
2020-08-03 23:41:38 +08:00
|
|
|
}
|
2017-11-18 03:08:10 +08:00
|
|
|
}
|
|
|
|
|
2020-08-03 23:41:38 +08: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-15 00:27:52 +08:00
|
|
|
func (s *sanitizerStaticLibsMap) add(c LinkableInterface, name string) {
|
|
|
|
image := GetImageVariantType(c)
|
|
|
|
arch := c.Module().Target().Arch.ArchType.String()
|
2019-02-05 03:22:08 +08:00
|
|
|
|
2020-08-03 23:41:38 +08: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 07:19:13 +08:00
|
|
|
}
|
|
|
|
|
2020-08-03 23:41:38 +08: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) {
|
|
|
|
for _, image := range android.SortedStringKeys(s.libsMap) {
|
2020-12-15 00:27:52 +08:00
|
|
|
archMap := s.libsMap[ImageVariantType(image)]
|
2020-08-03 23:41:38 +08:00
|
|
|
for _, arch := range android.SortedStringKeys(archMap) {
|
|
|
|
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-05 03:22:08 +08:00
|
|
|
|
2020-08-03 23:41:38 +08: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 07:19:13 +08:00
|
|
|
}
|
|
|
|
|
2020-08-03 23:41:38 +08:00
|
|
|
var hwasanStaticLibsKey = android.NewOnceKey("hwasanStaticLibs")
|
|
|
|
|
|
|
|
func hwasanStaticLibs(config android.Config) *sanitizerStaticLibsMap {
|
|
|
|
return config.Once(hwasanStaticLibsKey, func() interface{} {
|
2021-04-02 02:29:09 +08:00
|
|
|
return newSanitizerStaticLibsMap(Hwasan)
|
2020-08-03 23:41:38 +08:00
|
|
|
}).(*sanitizerStaticLibsMap)
|
2019-07-29 20:27:18 +08:00
|
|
|
}
|
|
|
|
|
2018-02-22 07:49:20 +08:00
|
|
|
func enableMinimalRuntime(sanitize *sanitize) bool {
|
|
|
|
if !Bool(sanitize.Properties.Sanitize.Address) &&
|
2018-08-03 07:19:13 +08:00
|
|
|
!Bool(sanitize.Properties.Sanitize.Hwaddress) &&
|
2019-05-02 05:42:05 +08:00
|
|
|
!Bool(sanitize.Properties.Sanitize.Fuzzer) &&
|
2020-02-20 04:24:02 +08:00
|
|
|
|
2018-02-22 07:49:20 +08:00
|
|
|
(Bool(sanitize.Properties.Sanitize.Integer_overflow) ||
|
2020-02-20 04:24:02 +08:00
|
|
|
len(sanitize.Properties.Sanitize.Misc_undefined) > 0 ||
|
|
|
|
Bool(sanitize.Properties.Sanitize.Undefined) ||
|
|
|
|
Bool(sanitize.Properties.Sanitize.All_undefined)) &&
|
|
|
|
|
2018-02-22 07:49:20 +08:00
|
|
|
!(Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
|
|
|
|
Bool(sanitize.Properties.Sanitize.Diag.Cfi) ||
|
2020-02-20 04:24:02 +08:00
|
|
|
Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
|
2018-02-22 07:49:20 +08:00
|
|
|
len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0) {
|
2020-02-20 04:24:02 +08:00
|
|
|
|
2018-02-22 07:49:20 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-11-15 08:59:12 +08:00
|
|
|
func enableUbsanRuntime(sanitize *sanitize) bool {
|
|
|
|
return Bool(sanitize.Properties.Sanitize.Diag.Integer_overflow) ||
|
2020-02-20 04:24:02 +08:00
|
|
|
Bool(sanitize.Properties.Sanitize.Diag.Undefined) ||
|
2019-11-15 08:59:12 +08:00
|
|
|
len(sanitize.Properties.Sanitize.Diag.Misc_undefined) > 0
|
|
|
|
}
|
|
|
|
|
2017-11-18 03:08:10 +08:00
|
|
|
func cfiMakeVarsProvider(ctx android.MakeVarsContext) {
|
2020-08-03 23:41:38 +08:00
|
|
|
cfiStaticLibs(ctx.Config()).exportToMake(ctx)
|
2017-11-18 03:08:10 +08:00
|
|
|
}
|
2018-08-03 07:19:13 +08:00
|
|
|
|
|
|
|
func hwasanMakeVarsProvider(ctx android.MakeVarsContext) {
|
2020-08-03 23:41:38 +08:00
|
|
|
hwasanStaticLibs(ctx.Config()).exportToMake(ctx)
|
2018-08-03 07:19:13 +08:00
|
|
|
}
|