rust: Rust sanitized snapshots variations
This adds support for correctly handling Rust sanitized snapshots,
ensuring they only have one variation.
The presence of multiple variations were causing build failures
when a rust_fuzz module for host was defined and a snapshot
build was requested.
This also sets -Z link-native-libraries=no on host modules
(in addition to device modules) to avoid emitting extra linkage
flags due to link attributes.
Bug: 282897366
Test: SOONG_SDK_SNAPSHOT_USE_SRCJAR=true m
Change-Id: Idf980c29145f11c530ad635a4eb5b01a1730ac24
diff --git a/rust/snapshot_prebuilt.go b/rust/snapshot_prebuilt.go
index 32d3916..42e3cef 100644
--- a/rust/snapshot_prebuilt.go
+++ b/rust/snapshot_prebuilt.go
@@ -15,6 +15,8 @@
package rust
import (
+ "fmt"
+
"android/soong/android"
"android/soong/cc"
@@ -26,17 +28,80 @@
*libraryDecorator
properties cc.SnapshotLibraryProperties
sanitizerProperties struct {
- CfiEnabled bool `blueprint:"mutated"`
+ SanitizerVariation cc.SanitizerType `blueprint:"mutated"`
- // Library flags for cfi variant.
- Cfi cc.SnapshotLibraryProperties `android:"arch_variant"`
+ //TODO: Library flags for cfi variant when CFI is supported.
+ //Cfi cc.SnapshotLibraryProperties `android:"arch_variant"`
+
+ // Library flags for hwasan variant.
+ Hwasan cc.SnapshotLibraryProperties `android:"arch_variant"`
}
}
+var _ cc.SnapshotSanitizer = (*snapshotLibraryDecorator)(nil)
+
+func (library *snapshotLibraryDecorator) IsSanitizerAvailable(t cc.SanitizerType) bool {
+ switch t {
+ //TODO: When CFI is supported, add a check here as well
+ case cc.Hwasan:
+ return library.sanitizerProperties.Hwasan.Src != nil
+ default:
+ return false
+ }
+}
+
+func (library *snapshotLibraryDecorator) SetSanitizerVariation(t cc.SanitizerType, enabled bool) {
+ if !enabled || library.IsSanitizerEnabled(t) {
+ return
+ }
+ if !library.IsUnsanitizedVariant() {
+ panic(fmt.Errorf("snapshot Sanitizer must be one of Cfi or Hwasan but not both"))
+ }
+ library.sanitizerProperties.SanitizerVariation = t
+}
+
+func (library *snapshotLibraryDecorator) IsSanitizerEnabled(t cc.SanitizerType) bool {
+ return library.sanitizerProperties.SanitizerVariation == t
+}
+
+func (library *snapshotLibraryDecorator) IsUnsanitizedVariant() bool {
+ //TODO: When CFI is supported, add a check here as well
+ return !library.IsSanitizerEnabled(cc.Hwasan)
+}
+
func init() {
registerRustSnapshotModules(android.InitRegistrationContext)
}
+func (mod *Module) IsSnapshotSanitizerAvailable(t cc.SanitizerType) bool {
+ if ss, ok := mod.compiler.(cc.SnapshotSanitizer); ok {
+ return ss.IsSanitizerAvailable(t)
+ }
+ return false
+}
+
+func (mod *Module) SetSnapshotSanitizerVariation(t cc.SanitizerType, enabled bool) {
+ if ss, ok := mod.compiler.(cc.SnapshotSanitizer); ok {
+ ss.SetSanitizerVariation(t, enabled)
+ } else {
+ panic(fmt.Errorf("Calling SetSnapshotSanitizerVariation on a non-snapshotLibraryDecorator: %s", mod.Name()))
+ }
+}
+
+func (mod *Module) IsSnapshotUnsanitizedVariant() bool {
+ if ss, ok := mod.compiler.(cc.SnapshotSanitizer); ok {
+ return ss.IsUnsanitizedVariant()
+ }
+ return false
+}
+
+func (mod *Module) IsSnapshotSanitizer() bool {
+ if _, ok := mod.compiler.(cc.SnapshotSanitizer); ok {
+ return true
+ }
+ return false
+}
+
func registerRustSnapshotModules(ctx android.RegistrationContext) {
cc.VendorSnapshotImageSingleton.RegisterAdditionalModule(ctx,
"vendor_snapshot_rlib", VendorSnapshotRlibFactory)
@@ -81,6 +146,9 @@
library.SetSnapshotAndroidMkSuffix(ctx, variant)
+ if library.IsSanitizerEnabled(cc.Hwasan) {
+ library.properties = library.sanitizerProperties.Hwasan
+ }
if !library.MatchesWithDevice(ctx.DeviceConfig()) {
return buildOutput{}
}