rust: Switch rust_fuzz to HWASan

Bug: 180495975
Test: example_rust_fuzzer
Change-Id: I26e6f15136ee2d5f4ed1167be5c1c6a14b19421a
diff --git a/rust/fuzz.go b/rust/fuzz.go
index 6b0a943..d699971 100644
--- a/rust/fuzz.go
+++ b/rust/fuzz.go
@@ -73,9 +73,6 @@
 	if libFuzzerRuntimeLibrary := config.LibFuzzerRuntimeLibrary(ctx.toolchain()); libFuzzerRuntimeLibrary != "" {
 		deps.StaticLibs = append(deps.StaticLibs, libFuzzerRuntimeLibrary)
 	}
-	if libclangRuntimeLibrary := config.LibclangRuntimeLibrary(ctx.toolchain(), "asan"); libclangRuntimeLibrary != "" {
-		deps.SharedLibs = append(deps.SharedLibs, libclangRuntimeLibrary)
-	}
 	deps.SharedLibs = append(deps.SharedLibs, "libc++")
 	deps.Rlibs = append(deps.Rlibs, "liblibfuzzer_sys")
 
diff --git a/rust/fuzz_test.go b/rust/fuzz_test.go
index f93ccc7..2524f91 100644
--- a/rust/fuzz_test.go
+++ b/rust/fuzz_test.go
@@ -37,9 +37,6 @@
 
 	// Check that appropriate dependencies are added and that the rustlib linkage is correct.
 	fuzz_libtest_mod := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Module().(*Module)
-	if !android.InList("libclang_rt.asan-aarch64-android", fuzz_libtest_mod.Properties.AndroidMkSharedLibs) {
-		t.Errorf("libclang_rt.asan-aarch64-android shared library dependency missing for rust_fuzz module.")
-	}
 	if !android.InList("liblibfuzzer_sys.rlib-std", fuzz_libtest_mod.Properties.AndroidMkRlibs) {
 		t.Errorf("liblibfuzzer_sys rlib library dependency missing for rust_fuzz module. %#v", fuzz_libtest_mod.Properties.AndroidMkRlibs)
 	}
@@ -49,18 +46,18 @@
 
 	// Check that compiler flags are set appropriately .
 	fuzz_libtest := ctx.ModuleForTests("fuzz_libtest", "android_arm64_armv8-a_fuzzer").Output("fuzz_libtest")
-	if !strings.Contains(fuzz_libtest.Args["rustcFlags"], "-Z sanitizer=address") ||
+	if !strings.Contains(fuzz_libtest.Args["rustcFlags"], "-Z sanitizer=hwaddress") ||
 		!strings.Contains(fuzz_libtest.Args["rustcFlags"], "-C passes='sancov'") ||
 		!strings.Contains(fuzz_libtest.Args["rustcFlags"], "--cfg fuzzing") {
-		t.Errorf("rust_fuzz module does not contain the expected flags (sancov, cfg fuzzing, address sanitizer).")
+		t.Errorf("rust_fuzz module does not contain the expected flags (sancov, cfg fuzzing, hwaddress sanitizer).")
 
 	}
 
 	// Check that dependencies have 'fuzzer' variants produced for them as well.
 	libtest_fuzzer := ctx.ModuleForTests("libtest_fuzzing", "android_arm64_armv8-a_rlib_rlib-std_fuzzer").Output("libtest_fuzzing.rlib")
-	if !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "-Z sanitizer=address") ||
+	if !strings.Contains(libtest_fuzzer.Args["rustcFlags"], "-Z sanitizer=hwaddress") ||
 		!strings.Contains(libtest_fuzzer.Args["rustcFlags"], "-C passes='sancov'") ||
 		!strings.Contains(libtest_fuzzer.Args["rustcFlags"], "--cfg fuzzing") {
-		t.Errorf("rust_fuzz dependent library does not contain the expected flags (sancov, cfg fuzzing, address sanitizer).")
+		t.Errorf("rust_fuzz dependent library does not contain the expected flags (sancov, cfg fuzzing, hwaddress sanitizer).")
 	}
 }
diff --git a/rust/sanitize.go b/rust/sanitize.go
index ae3eff0..0a53f98 100644
--- a/rust/sanitize.go
+++ b/rust/sanitize.go
@@ -46,7 +46,6 @@
 	"-C llvm-args=-sanitizer-coverage-inline-8bit-counters",
 	"-C llvm-args=-sanitizer-coverage-trace-geps",
 	"-C llvm-args=-sanitizer-coverage-prune-blocks=0",
-	"-Z sanitizer=address",
 
 	// Sancov breaks with lto
 	// TODO: Remove when https://bugs.llvm.org/show_bug.cgi?id=41734 is resolved and sancov works with LTO
@@ -109,6 +108,11 @@
 	}
 	if Bool(sanitize.Properties.Sanitize.Fuzzer) {
 		flags.RustFlags = append(flags.RustFlags, fuzzerFlags...)
+		if ctx.Arch().ArchType == android.Arm64 {
+			flags.RustFlags = append(flags.RustFlags, hwasanFlags...)
+		} else {
+			flags.RustFlags = append(flags.RustFlags, asanFlags...)
+		}
 	}
 	if Bool(sanitize.Properties.Sanitize.Address) {
 		flags.RustFlags = append(flags.RustFlags, asanFlags...)
@@ -133,12 +137,14 @@
 		var depTag blueprint.DependencyTag
 		var deps []string
 
-		if Bool(mod.sanitize.Properties.Sanitize.Fuzzer) || Bool(mod.sanitize.Properties.Sanitize.Address) {
+		if mod.IsSanitizerEnabled(cc.Asan) ||
+			(mod.IsSanitizerEnabled(cc.Fuzzer) && mctx.Arch().ArchType != android.Arm64) {
 			variations = append(variations,
 				blueprint.Variation{Mutator: "link", Variation: "shared"})
 			depTag = cc.SharedDepTag()
 			deps = []string{config.LibclangRuntimeLibrary(mod.toolchain(mctx), "asan")}
-		} else if mod.IsSanitizerEnabled(cc.Hwasan) {
+		} else if mod.IsSanitizerEnabled(cc.Hwasan) ||
+			(mod.IsSanitizerEnabled(cc.Fuzzer) && mctx.Arch().ArchType == android.Arm64) {
 			// TODO(b/180495975): HWASan for static Rust binaries isn't supported yet.
 			if binary, ok := mod.compiler.(*binaryDecorator); ok {
 				if Bool(binary.Properties.Static_executable) {
@@ -285,11 +291,9 @@
 
 func (mod *Module) StaticallyLinked() bool {
 	if lib, ok := mod.compiler.(libraryInterface); ok {
-		if lib.rlib() || lib.static() {
-			return true
-		}
-	} else if Bool(mod.compiler.(*binaryDecorator).Properties.Static_executable) {
-		return true
+		return lib.rlib() || lib.static()
+	} else if binary, ok := mod.compiler.(*binaryDecorator); ok {
+		return Bool(binary.Properties.Static_executable)
 	}
 	return false
 }