diff options
Diffstat (limited to 'android/neverallow.go')
| -rw-r--r-- | android/neverallow.go | 65 |
1 files changed, 48 insertions, 17 deletions
diff --git a/android/neverallow.go b/android/neverallow.go index b36bf045a..034861958 100644 --- a/android/neverallow.go +++ b/android/neverallow.go @@ -252,7 +252,7 @@ func neverallowMutator(ctx BottomUpMutatorContext) { continue } - if !n.appliesToProperties(properties) { + if !n.appliesToProperties(ctx, properties) { continue } @@ -272,8 +272,12 @@ func neverallowMutator(ctx BottomUpMutatorContext) { } } +type ValueMatcherContext interface { + Config() Config +} + type ValueMatcher interface { - Test(string) bool + Test(ValueMatcherContext, string) bool String() string } @@ -281,7 +285,7 @@ type equalMatcher struct { expected string } -func (m *equalMatcher) Test(value string) bool { +func (m *equalMatcher) Test(ctx ValueMatcherContext, value string) bool { return m.expected == value } @@ -292,7 +296,7 @@ func (m *equalMatcher) String() string { type anyMatcher struct { } -func (m *anyMatcher) Test(value string) bool { +func (m *anyMatcher) Test(ctx ValueMatcherContext, value string) bool { return true } @@ -306,7 +310,7 @@ type startsWithMatcher struct { prefix string } -func (m *startsWithMatcher) Test(value string) bool { +func (m *startsWithMatcher) Test(ctx ValueMatcherContext, value string) bool { return strings.HasPrefix(value, m.prefix) } @@ -318,7 +322,7 @@ type regexMatcher struct { re *regexp.Regexp } -func (m *regexMatcher) Test(value string) bool { +func (m *regexMatcher) Test(ctx ValueMatcherContext, value string) bool { return m.re.MatchString(value) } @@ -330,7 +334,7 @@ type notInListMatcher struct { allowed []string } -func (m *notInListMatcher) Test(value string) bool { +func (m *notInListMatcher) Test(ctx ValueMatcherContext, value string) bool { return !InList(value, m.allowed) } @@ -340,7 +344,7 @@ func (m *notInListMatcher) String() string { type isSetMatcher struct{} -func (m *isSetMatcher) Test(value string) bool { +func (m *isSetMatcher) Test(ctx ValueMatcherContext, value string) bool { return value != "" } @@ -350,6 +354,19 @@ func (m *isSetMatcher) String() string { var isSetMatcherInstance = &isSetMatcher{} +type sdkVersionMatcher struct { + condition func(ctx ValueMatcherContext, spec SdkSpec) bool + description string +} + +func (m *sdkVersionMatcher) Test(ctx ValueMatcherContext, value string) bool { + return m.condition(ctx, SdkSpecFromWithConfig(ctx.Config(), value)) +} + +func (m *sdkVersionMatcher) String() string { + return ".sdk-version(" + m.description + ")" +} + type ruleProperty struct { fields []string // e.x.: Vndk.Enabled matcher ValueMatcher @@ -563,9 +580,10 @@ func (r *rule) appliesToModuleType(moduleType string) bool { return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes) } -func (r *rule) appliesToProperties(properties []interface{}) bool { - includeProps := hasAllProperties(properties, r.props) - excludeProps := hasAnyProperty(properties, r.unlessProps) +func (r *rule) appliesToProperties(ctx ValueMatcherContext, + properties []interface{}) bool { + includeProps := hasAllProperties(ctx, properties, r.props) + excludeProps := hasAnyProperty(ctx, properties, r.unlessProps) return includeProps && !excludeProps } @@ -585,6 +603,16 @@ func NotInList(allowed []string) ValueMatcher { return ¬InListMatcher{allowed} } +func LessThanSdkVersion(sdk string) ValueMatcher { + return &sdkVersionMatcher{ + condition: func(ctx ValueMatcherContext, spec SdkSpec) bool { + return spec.ApiLevel.LessThan( + SdkSpecFromWithConfig(ctx.Config(), sdk).ApiLevel) + }, + description: "lessThan=" + sdk, + } +} + // assorted utils func cleanPaths(paths []string) []string { @@ -603,25 +631,28 @@ func fieldNamesForProperties(propertyNames string) []string { return names } -func hasAnyProperty(properties []interface{}, props []ruleProperty) bool { +func hasAnyProperty(ctx ValueMatcherContext, properties []interface{}, + props []ruleProperty) bool { for _, v := range props { - if hasProperty(properties, v) { + if hasProperty(ctx, properties, v) { return true } } return false } -func hasAllProperties(properties []interface{}, props []ruleProperty) bool { +func hasAllProperties(ctx ValueMatcherContext, properties []interface{}, + props []ruleProperty) bool { for _, v := range props { - if !hasProperty(properties, v) { + if !hasProperty(ctx, properties, v) { return false } } return true } -func hasProperty(properties []interface{}, prop ruleProperty) bool { +func hasProperty(ctx ValueMatcherContext, properties []interface{}, + prop ruleProperty) bool { for _, propertyStruct := range properties { propertiesValue := reflect.ValueOf(propertyStruct).Elem() for _, v := range prop.fields { @@ -635,7 +666,7 @@ func hasProperty(properties []interface{}, prop ruleProperty) bool { } check := func(value string) bool { - return prop.matcher.Test(value) + return prop.matcher.Test(ctx, value) } if matchValue(propertiesValue, check) { |