Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2a7bf750 authored by Jihoon Kang's avatar Jihoon Kang
Browse files

Add a neverallow rule for prebuilt_* module types

This change adds a neverallow rule to the following module types:
- prebuilt_usr_srec
- prebuilt_priv_app
- prebuilt_rfs
- prebuilt_framework
- prebuilt_res
- prebuilt_wlc_upt
- prebuilt_odm

that these modules cannot be defined in bp files, but can only be auto
generated by other modules.

Test: m nothing --no-skip-soong-tests
Bug: 375053752
Change-Id: Ie1b73966d8ada3863c29f9aca710aa8c735286dd
parent 0da5ae93
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -87,6 +87,10 @@ type BaseModuleContext interface {
	// This method shouldn't be used directly, prefer the type-safe android.OtherModuleProvider instead.
	otherModuleProvider(m blueprint.Module, provider blueprint.AnyProviderKey) (any, bool)

	// OtherModuleIsAutoGenerated returns true if the module is auto generated by another module
	// instead of being defined in Android.bp file.
	OtherModuleIsAutoGenerated(m blueprint.Module) bool

	// Provider returns the value for a provider for the current module.  If the value is
	// not set it returns nil and false.  It panics if called before the appropriate
	// mutator or GenerateBuildActions pass for the provider.  The value returned may be a deep
@@ -275,6 +279,10 @@ func (b *baseModuleContext) otherModuleProvider(m blueprint.Module, provider blu
	return b.bp.OtherModuleProvider(m, provider)
}

func (b *baseModuleContext) OtherModuleIsAutoGenerated(m blueprint.Module) bool {
	return b.bp.OtherModuleIsAutoGenerated(m)
}

func (b *baseModuleContext) provider(provider blueprint.AnyProviderKey) (any, bool) {
	return b.bp.Provider(provider)
}
+40 −0
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@ func init() {
	AddNeverAllowRules(createLimitDirgroupRule()...)
	AddNeverAllowRules(createFilesystemIsAutoGeneratedRule())
	AddNeverAllowRules(createKotlinPluginRule()...)
	AddNeverAllowRules(createPrebuiltEtcBpDefineRule())
}

// Add a NeverAllow rule to the set of rules to apply.
@@ -321,6 +322,23 @@ func createKotlinPluginRule() []Rule {
	}
}

// These module types are introduced to convert PRODUCT_COPY_FILES to Soong,
// and is only intended to be used by filesystem_creator.
func createPrebuiltEtcBpDefineRule() Rule {
	return NeverAllow().
		ModuleType(
			"prebuilt_usr_srec",
			"prebuilt_priv_app",
			"prebuilt_rfs",
			"prebuilt_framework",
			"prebuilt_res",
			"prebuilt_wlc_upt",
			"prebuilt_odm",
		).
		DefinedInBpFile().
		Because("module type not allowed to be defined in bp file")
}

func neverallowMutator(ctx BottomUpMutatorContext) {
	m, ok := ctx.Module().(Module)
	if !ok {
@@ -354,6 +372,10 @@ func neverallowMutator(ctx BottomUpMutatorContext) {
			continue
		}

		if !n.appliesToBpDefinedModule(ctx) {
			continue
		}

		ctx.ModuleErrorf("violates " + n.String())
	}
}
@@ -477,6 +499,8 @@ type Rule interface {

	WithoutMatcher(properties string, matcher ValueMatcher) Rule

	DefinedInBpFile() Rule

	Because(reason string) Rule
}

@@ -498,6 +522,8 @@ type rule struct {
	unlessProps ruleProperties

	onlyBootclasspathJar bool

	definedInBp bool
}

// Create a new NeverAllow rule.
@@ -571,6 +597,13 @@ func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule {
	return r
}

// DefinedInBpFile specifies that this rule applies to modules that are defined
// in bp files, and does not apply to modules that are auto generated by other modules.
func (r *rule) DefinedInBpFile() Rule {
	r.definedInBp = true
	return r
}

func selectMatcher(expected string) ValueMatcher {
	if expected == "*" {
		return anyMatcherInstance
@@ -665,6 +698,13 @@ func (r *rule) appliesToProperties(ctx BottomUpMutatorContext, properties []inte
	return includeProps && !excludeProps
}

func (r *rule) appliesToBpDefinedModule(ctx BottomUpMutatorContext) bool {
	if !r.definedInBp {
		return true
	}
	return !ctx.OtherModuleIsAutoGenerated(ctx.Module()) == r.definedInBp
}

func StartsWith(prefix string) ValueMatcher {
	return &startsWithMatcher{prefix}
}
+28 −0
Original line number Diff line number Diff line
@@ -374,6 +374,20 @@ var neverallowTests = []struct {
			`is_auto_generated property is only allowed for filesystem modules in build/soong/fsgen directory`,
		},
	},
	// Test for the rule restricting use of prebuilt_* module
	{
		name: `"prebuilt_usr_srec" defined in Android.bp file`,
		fs: map[string][]byte{
			"a/b/Android.bp": []byte(`
				prebuilt_usr_srec {
					name: "foo",
				}
			`),
		},
		expectedErrors: []string{
			`module type not allowed to be defined in bp file`,
		},
	},
}

var prepareForNeverAllowTest = GroupFixturePreparers(
@@ -383,6 +397,7 @@ var prepareForNeverAllowTest = GroupFixturePreparers(
		ctx.RegisterModuleType("java_library_host", newMockJavaLibraryModule)
		ctx.RegisterModuleType("java_device_for_host", newMockJavaLibraryModule)
		ctx.RegisterModuleType("filesystem", newMockFilesystemModule)
		ctx.RegisterModuleType("prebuilt_usr_srec", newMockPrebuiltUsrSrecModule)
	}),
)

@@ -482,3 +497,16 @@ func newMockJavaLibraryModule() Module {

func (p *mockJavaLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
}

type mockPrebuiltUsrSrecModule struct {
	ModuleBase
}

func (p *mockPrebuiltUsrSrecModule) GenerateAndroidBuildActions(ModuleContext) {
}

func newMockPrebuiltUsrSrecModule() Module {
	m := &mockPrebuiltUsrSrecModule{}
	InitAndroidModule(m)
	return m
}