From bb4511df9463d44bda26b7ec7f8064f0701d445f Mon Sep 17 00:00:00 2001 From: Jasraj Bedi Date: Thu, 23 Jul 2020 22:58:17 +0000 Subject: [PATCH] Added write only sanitizer for ASAN and HWASAN Bug: 162024969 Test: Successfully builds targets for both host and device "writeonly" flag in SANITIZE_(HOST|TARGET) enables it with "address" and "hwaddress" Change-Id: Ia89d43230deef15a67dee09ed015fea14f0717ff --- cc/sanitize.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/cc/sanitize.go b/cc/sanitize.go index 72ad6d7a0..136a51061 100644 --- a/cc/sanitize.go +++ b/cc/sanitize.go @@ -158,6 +158,9 @@ type SanitizeProperties struct { Scudo *bool `android:"arch_variant"` Scs *bool `android:"arch_variant"` + // 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. @@ -279,6 +282,15 @@ func (sanitize *sanitize) begin(ctx BaseModuleContext) { s.Hwaddress = boolPtr(true) } + 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) + } + if len(globalSanitizers) > 0 { ctx.ModuleErrorf("unknown global sanitizer option %s", globalSanitizers[0]) } @@ -456,6 +468,10 @@ func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags { flags.Local.CFlags = append(flags.Local.CFlags, asanCflags...) flags.Local.LdFlags = append(flags.Local.LdFlags, asanLdflags...) + if Bool(sanitize.Properties.Sanitize.Writeonly) { + flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-asan-instrument-reads=0") + } + if ctx.Host() { // -nodefaultlibs (provided with libc++) prevents the driver from linking // libraries needed with -fsanitize=address. http://b/18650275 (WAI) @@ -475,6 +491,9 @@ func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags { if Bool(sanitize.Properties.Sanitize.Hwaddress) { flags.Local.CFlags = append(flags.Local.CFlags, hwasanCflags...) + if Bool(sanitize.Properties.Sanitize.Writeonly) { + flags.Local.CFlags = append(flags.Local.CFlags, "-mllvm", "-hwasan-instrument-reads=0") + } } if Bool(sanitize.Properties.Sanitize.Fuzzer) {