Merge "Revert "[soong] Add memtag-stack sanitizer, switch to linker-gen...""

This commit is contained in:
Florian Mayer
2022-08-31 18:37:30 +00:00
committed by Gerrit Code Review
2 changed files with 32 additions and 53 deletions

View File

@@ -77,7 +77,6 @@ var (
"-fno-sanitize-recover=integer,undefined"} "-fno-sanitize-recover=integer,undefined"}
hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512", hwasanGlobalOptions = []string{"heap_history_size=1023", "stack_history_size=512",
"export_memory_stats=0", "max_malloc_fill_size=4096", "malloc_fill_byte=0"} "export_memory_stats=0", "max_malloc_fill_size=4096", "malloc_fill_byte=0"}
memtagStackCommonFlags = []string{"-march=armv8-a+memtag"}
) )
type SanitizerType int type SanitizerType int
@@ -90,7 +89,6 @@ const (
scs scs
Fuzzer Fuzzer
Memtag_heap Memtag_heap
Memtag_stack
cfi // cfi is last to prevent it running before incompatible mutators cfi // cfi is last to prevent it running before incompatible mutators
) )
@@ -102,7 +100,6 @@ var Sanitizers = []SanitizerType{
scs, scs,
Fuzzer, Fuzzer,
Memtag_heap, Memtag_heap,
Memtag_stack,
cfi, // cfi is last to prevent it running before incompatible mutators cfi, // cfi is last to prevent it running before incompatible mutators
} }
@@ -123,8 +120,6 @@ func (t SanitizerType) variationName() string {
return "scs" return "scs"
case Memtag_heap: case Memtag_heap:
return "memtag_heap" return "memtag_heap"
case Memtag_stack:
return "memtag_stack"
case Fuzzer: case Fuzzer:
return "fuzzer" return "fuzzer"
default: default:
@@ -141,8 +136,6 @@ func (t SanitizerType) name() string {
return "hwaddress" return "hwaddress"
case Memtag_heap: case Memtag_heap:
return "memtag_heap" return "memtag_heap"
case Memtag_stack:
return "memtag_stack"
case tsan: case tsan:
return "thread" return "thread"
case intOverflow: case intOverflow:
@@ -164,7 +157,7 @@ func (t SanitizerType) registerMutators(ctx android.RegisterMutatorsContext) {
sanitizer := &sanitizerSplitMutator{t} sanitizer := &sanitizerSplitMutator{t}
ctx.TopDown(t.variationName()+"_markapexes", sanitizer.markSanitizableApexesMutator) ctx.TopDown(t.variationName()+"_markapexes", sanitizer.markSanitizableApexesMutator)
ctx.Transition(t.variationName(), sanitizer) ctx.Transition(t.variationName(), sanitizer)
case Memtag_heap, Memtag_stack, intOverflow: case Memtag_heap, intOverflow:
// do nothing // do nothing
default: default:
panic(fmt.Errorf("unknown SanitizerType %d", t)) panic(fmt.Errorf("unknown SanitizerType %d", t))
@@ -189,8 +182,6 @@ func (*Module) SanitizerSupported(t SanitizerType) bool {
return true return true
case Memtag_heap: case Memtag_heap:
return true return true
case Memtag_stack:
return true
default: default:
return false return false
} }
@@ -242,9 +233,6 @@ type SanitizeUserProps struct {
// Memory-tagging, only available on arm64 // Memory-tagging, only available on arm64
// if diag.memtag unset or false, enables async memory tagging // if diag.memtag unset or false, enables async memory tagging
Memtag_heap *bool `android:"arch_variant"` Memtag_heap *bool `android:"arch_variant"`
// Memory-tagging stack instrumentation, only available on arm64
// Adds instrumentation to detect stack buffer overflows and use-after-scope using MTE.
Memtag_stack *bool `android:"arch_variant"`
// A modifier for ASAN and HWASAN for write only instrumentation // A modifier for ASAN and HWASAN for write only instrumentation
Writeonly *bool `android:"arch_variant"` Writeonly *bool `android:"arch_variant"`
@@ -330,7 +318,7 @@ func (sanitize *sanitize) begin(ctx BaseModuleContext) {
return return
} }
// cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap: false}). // cc_test targets default to SYNC MemTag unless explicitly set to ASYNC (via diag: {memtag_heap}).
if ctx.testBinary() { if ctx.testBinary() {
if s.Memtag_heap == nil { if s.Memtag_heap == nil {
s.Memtag_heap = proptools.BoolPtr(true) s.Memtag_heap = proptools.BoolPtr(true)
@@ -416,10 +404,6 @@ func (sanitize *sanitize) begin(ctx BaseModuleContext) {
} }
} }
if found, globalSanitizers = removeFromList("memtag_stack", globalSanitizers); found && s.Memtag_stack == nil {
s.Memtag_stack = proptools.BoolPtr(true)
}
if len(globalSanitizers) > 0 { if len(globalSanitizers) > 0 {
ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0]) ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0])
} }
@@ -488,16 +472,12 @@ func (sanitize *sanitize) begin(ctx BaseModuleContext) {
// Memtag_heap is only implemented on AArch64. // Memtag_heap is only implemented on AArch64.
if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() { if ctx.Arch().ArchType != android.Arm64 || !ctx.toolchain().Bionic() {
s.Memtag_heap = nil s.Memtag_heap = nil
s.Memtag_stack = nil
} }
// Also disable CFI if ASAN is enabled. // Also disable CFI if ASAN is enabled.
if Bool(s.Address) || Bool(s.Hwaddress) { if Bool(s.Address) || Bool(s.Hwaddress) {
s.Cfi = nil s.Cfi = nil
s.Diag.Cfi = nil s.Diag.Cfi = nil
// HWASAN and ASAN win against MTE.
s.Memtag_heap = nil
s.Memtag_stack = nil
} }
// Disable sanitizers that depend on the UBSan runtime for windows/darwin builds. // Disable sanitizers that depend on the UBSan runtime for windows/darwin builds.
@@ -554,7 +534,7 @@ func (sanitize *sanitize) begin(ctx BaseModuleContext) {
if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) || if ctx.Os() != android.Windows && (Bool(s.All_undefined) || Bool(s.Undefined) || Bool(s.Address) || Bool(s.Thread) ||
Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 || Bool(s.Fuzzer) || Bool(s.Safestack) || Bool(s.Cfi) || Bool(s.Integer_overflow) || len(s.Misc_undefined) > 0 ||
Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap) || Bool(s.Memtag_stack)) { Bool(s.Scudo) || Bool(s.Hwaddress) || Bool(s.Scs) || Bool(s.Memtag_heap)) {
sanitize.Properties.SanitizerEnabled = true sanitize.Properties.SanitizerEnabled = true
} }
@@ -711,20 +691,6 @@ func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
} }
} }
if Bool(sanitize.Properties.Sanitize.Memtag_stack) {
flags.Local.CFlags = append(flags.Local.CFlags, memtagStackCommonFlags...)
flags.Local.AsFlags = append(flags.Local.AsFlags, memtagStackCommonFlags...)
flags.Local.LdFlags = append(flags.Local.LdFlags, memtagStackCommonFlags...)
}
if (Bool(sanitize.Properties.Sanitize.Memtag_heap) || Bool(sanitize.Properties.Sanitize.Memtag_stack)) && ctx.binary() {
if Bool(sanitize.Properties.Sanitize.Diag.Memtag_heap) {
flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=sync")
} else {
flags.Local.LdFlags = append(flags.Local.LdFlags, "-fsanitize-memtag-mode=async")
}
}
if Bool(sanitize.Properties.Sanitize.Integer_overflow) { if Bool(sanitize.Properties.Sanitize.Integer_overflow) {
flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...) flags.Local.CFlags = append(flags.Local.CFlags, intOverflowCflags...)
} }
@@ -838,8 +804,6 @@ func (sanitize *sanitize) getSanitizerBoolPtr(t SanitizerType) *bool {
return sanitize.Properties.Sanitize.Scs return sanitize.Properties.Sanitize.Scs
case Memtag_heap: case Memtag_heap:
return sanitize.Properties.Sanitize.Memtag_heap return sanitize.Properties.Sanitize.Memtag_heap
case Memtag_stack:
return sanitize.Properties.Sanitize.Memtag_stack
case Fuzzer: case Fuzzer:
return sanitize.Properties.Sanitize.Fuzzer return sanitize.Properties.Sanitize.Fuzzer
default: default:
@@ -855,7 +819,6 @@ func (sanitize *sanitize) isUnsanitizedVariant() bool {
!sanitize.isSanitizerEnabled(cfi) && !sanitize.isSanitizerEnabled(cfi) &&
!sanitize.isSanitizerEnabled(scs) && !sanitize.isSanitizerEnabled(scs) &&
!sanitize.isSanitizerEnabled(Memtag_heap) && !sanitize.isSanitizerEnabled(Memtag_heap) &&
!sanitize.isSanitizerEnabled(Memtag_stack) &&
!sanitize.isSanitizerEnabled(Fuzzer) !sanitize.isSanitizerEnabled(Fuzzer)
} }
@@ -887,8 +850,6 @@ func (sanitize *sanitize) SetSanitizer(t SanitizerType, b bool) {
sanitize.Properties.Sanitize.Scs = bPtr sanitize.Properties.Sanitize.Scs = bPtr
case Memtag_heap: case Memtag_heap:
sanitize.Properties.Sanitize.Memtag_heap = bPtr sanitize.Properties.Sanitize.Memtag_heap = bPtr
case Memtag_stack:
sanitize.Properties.Sanitize.Memtag_stack = bPtr
case Fuzzer: case Fuzzer:
sanitize.Properties.Sanitize.Fuzzer = bPtr sanitize.Properties.Sanitize.Fuzzer = bPtr
default: default:
@@ -1350,11 +1311,23 @@ func sanitizerRuntimeMutator(mctx android.BottomUpMutatorContext) {
} }
if Bool(c.sanitize.Properties.Sanitize.Memtag_heap) && c.Binary() { if Bool(c.sanitize.Properties.Sanitize.Memtag_heap) && c.Binary() {
sanitizers = append(sanitizers, "memtag-heap") noteDep := "note_memtag_heap_async"
if Bool(c.sanitize.Properties.Sanitize.Diag.Memtag_heap) {
noteDep = "note_memtag_heap_sync"
} }
// If we're using snapshots, redirect to snapshot whenever possible
if Bool(c.sanitize.Properties.Sanitize.Memtag_stack) { // TODO(b/178470649): clean manual snapshot redirections
sanitizers = append(sanitizers, "memtag-stack") snapshot := mctx.Provider(SnapshotInfoProvider).(SnapshotInfo)
if lib, ok := snapshot.StaticLibs[noteDep]; ok {
noteDep = lib
}
depTag := StaticDepTag(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)
} }
if Bool(c.sanitize.Properties.Sanitize.Fuzzer) { if Bool(c.sanitize.Properties.Sanitize.Fuzzer) {

View File

@@ -344,13 +344,19 @@ func (t MemtagNoteType) str() string {
func checkHasMemtagNote(t *testing.T, m android.TestingModule, expected MemtagNoteType) { func checkHasMemtagNote(t *testing.T, m android.TestingModule, expected MemtagNoteType) {
t.Helper() t.Helper()
note_async := "note_memtag_heap_async"
note_sync := "note_memtag_heap_sync"
found := None found := None
ldFlags := m.Rule("ld").Args["ldFlags"] implicits := m.Rule("ld").Implicits
if strings.Contains(ldFlags, "-fsanitize-memtag-mode=async") { for _, lib := range implicits {
if strings.Contains(lib.Rel(), note_async) {
found = Async found = Async
} else if strings.Contains(ldFlags, "-fsanitize-memtag-mode=sync") { break
} else if strings.Contains(lib.Rel(), note_sync) {
found = Sync found = Sync
break
}
} }
if found != expected { if found != expected {