blob: abe25e6f2e931328bb1535a229607815d4b4af4c [file] [log] [blame]
Steven Moreland65b3fd92017-12-06 14:18:35 -08001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package android
16
17import (
18 "path/filepath"
19 "reflect"
20 "strconv"
21 "strings"
22
23 "github.com/google/blueprint/proptools"
24)
25
26// "neverallow" rules for the build system.
27//
28// This allows things which aren't related to the build system and are enforced
29// for sanity, in progress code refactors, or policy to be expressed in a
30// straightforward away disjoint from implementations and tests which should
31// work regardless of these restrictions.
32//
33// A module is disallowed if all of the following are true:
34// - it is in one of the "in" paths
35// - it is not in one of the "notIn" paths
36// - it has all "with" properties matched
37// - - values are matched in their entirety
38// - - nil is interpreted as an empty string
39// - - nested properties are separated with a '.'
40// - - if the property is a list, any of the values in the list being matches
41// counts as a match
42// - it has none of the "without" properties matched (same rules as above)
43
44func registerNeverallowMutator(ctx RegisterMutatorsContext) {
45 ctx.BottomUp("neverallow", neverallowMutator).Parallel()
46}
47
Neil Fullerdf5f3562018-10-21 17:19:10 +010048var neverallows = createNeverAllows()
Steven Moreland65b3fd92017-12-06 14:18:35 -080049
Neil Fullerdf5f3562018-10-21 17:19:10 +010050func createNeverAllows() []*rule {
51 rules := []*rule{}
52 rules = append(rules, createTrebleRules()...)
53 rules = append(rules, createLibcoreRules()...)
Dongwon Kang50a299f2019-02-04 09:00:51 -080054 rules = append(rules, createMediaRules()...)
Colin Crossfd4f7432019-03-05 15:06:16 -080055 rules = append(rules, createJavaDeviceForHostRules()...)
Neil Fullerdf5f3562018-10-21 17:19:10 +010056 return rules
57}
Steven Moreland65b3fd92017-12-06 14:18:35 -080058
Neil Fullerdf5f3562018-10-21 17:19:10 +010059func createTrebleRules() []*rule {
60 return []*rule{
61 neverallow().
62 in("vendor", "device").
63 with("vndk.enabled", "true").
64 without("vendor", "true").
65 because("the VNDK can never contain a library that is device dependent."),
66 neverallow().
67 with("vndk.enabled", "true").
68 without("vendor", "true").
69 without("owner", "").
70 because("a VNDK module can never have an owner."),
Steven Moreland65b3fd92017-12-06 14:18:35 -080071
Neil Fullerdf5f3562018-10-21 17:19:10 +010072 // TODO(b/67974785): always enforce the manifest
73 neverallow().
74 without("name", "libhidltransport").
75 with("product_variables.enforce_vintf_manifest.cflags", "*").
76 because("manifest enforcement should be independent of ."),
77
78 // TODO(b/67975799): vendor code should always use /vendor/bin/sh
79 neverallow().
80 without("name", "libc_bionic_ndk").
81 with("product_variables.treble_linker_namespaces.cflags", "*").
82 because("nothing should care if linker namespaces are enabled or not"),
83
84 // Example:
85 // *neverallow().with("Srcs", "main.cpp"))
86 }
87}
88
89func createLibcoreRules() []*rule {
90 var coreLibraryProjects = []string{
91 "libcore",
92 "external/apache-harmony",
93 "external/apache-xml",
94 "external/bouncycastle",
95 "external/conscrypt",
96 "external/icu",
97 "external/okhttp",
98 "external/wycheproof",
99 }
100
Paul Duffin25f35fa2019-04-30 13:13:39 +0100101 // Core library constraints. The no_standard_libs can only be used in core
102 // library projects. Access to core library targets is restricted using
103 // visibility rules.
Neil Fullerdf5f3562018-10-21 17:19:10 +0100104 rules := []*rule{
105 neverallow().
106 notIn(append(coreLibraryProjects, "development")...).
107 with("no_standard_libs", "true"),
108 }
109
Neil Fullerdf5f3562018-10-21 17:19:10 +0100110 return rules
Steven Moreland65b3fd92017-12-06 14:18:35 -0800111}
112
Dongwon Kang50a299f2019-02-04 09:00:51 -0800113func createMediaRules() []*rule {
114 return []*rule{
115 neverallow().
116 with("libs", "updatable-media").
117 because("updatable-media includes private APIs. Use updatable_media_stubs instead."),
118 }
119}
120
Colin Crossfd4f7432019-03-05 15:06:16 -0800121func createJavaDeviceForHostRules() []*rule {
122 javaDeviceForHostProjectsWhitelist := []string{
Colin Crossb5191a52019-04-11 14:07:38 -0700123 "external/guava",
Colin Crossfd4f7432019-03-05 15:06:16 -0800124 "external/robolectric-shadows",
125 "framework/layoutlib",
126 }
127
128 return []*rule{
129 neverallow().
130 notIn(javaDeviceForHostProjectsWhitelist...).
131 moduleType("java_device_for_host", "java_host_for_device").
132 because("java_device_for_host can only be used in whitelisted projects"),
133 }
134}
135
Steven Moreland65b3fd92017-12-06 14:18:35 -0800136func neverallowMutator(ctx BottomUpMutatorContext) {
137 m, ok := ctx.Module().(Module)
138 if !ok {
139 return
140 }
141
142 dir := ctx.ModuleDir() + "/"
143 properties := m.GetProperties()
144
145 for _, n := range neverallows {
146 if !n.appliesToPath(dir) {
147 continue
148 }
149
Colin Crossfd4f7432019-03-05 15:06:16 -0800150 if !n.appliesToModuleType(ctx.ModuleType()) {
151 continue
152 }
153
Steven Moreland65b3fd92017-12-06 14:18:35 -0800154 if !n.appliesToProperties(properties) {
155 continue
156 }
157
158 ctx.ModuleErrorf("violates " + n.String())
159 }
160}
161
162type ruleProperty struct {
163 fields []string // e.x.: Vndk.Enabled
164 value string // e.x.: true
165}
166
167type rule struct {
168 // User string for why this is a thing.
169 reason string
170
171 paths []string
172 unlessPaths []string
173
Colin Crossfd4f7432019-03-05 15:06:16 -0800174 moduleTypes []string
175 unlessModuleTypes []string
176
Steven Moreland65b3fd92017-12-06 14:18:35 -0800177 props []ruleProperty
178 unlessProps []ruleProperty
179}
180
181func neverallow() *rule {
182 return &rule{}
183}
Colin Crossfd4f7432019-03-05 15:06:16 -0800184
Steven Moreland65b3fd92017-12-06 14:18:35 -0800185func (r *rule) in(path ...string) *rule {
186 r.paths = append(r.paths, cleanPaths(path)...)
187 return r
188}
Colin Crossfd4f7432019-03-05 15:06:16 -0800189
Steven Moreland65b3fd92017-12-06 14:18:35 -0800190func (r *rule) notIn(path ...string) *rule {
191 r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
192 return r
193}
Colin Crossfd4f7432019-03-05 15:06:16 -0800194
195func (r *rule) moduleType(types ...string) *rule {
196 r.moduleTypes = append(r.moduleTypes, types...)
197 return r
198}
199
200func (r *rule) notModuleType(types ...string) *rule {
201 r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
202 return r
203}
204
Steven Moreland65b3fd92017-12-06 14:18:35 -0800205func (r *rule) with(properties, value string) *rule {
206 r.props = append(r.props, ruleProperty{
207 fields: fieldNamesForProperties(properties),
208 value: value,
209 })
210 return r
211}
Colin Crossfd4f7432019-03-05 15:06:16 -0800212
Steven Moreland65b3fd92017-12-06 14:18:35 -0800213func (r *rule) without(properties, value string) *rule {
214 r.unlessProps = append(r.unlessProps, ruleProperty{
215 fields: fieldNamesForProperties(properties),
216 value: value,
217 })
218 return r
219}
Colin Crossfd4f7432019-03-05 15:06:16 -0800220
Steven Moreland65b3fd92017-12-06 14:18:35 -0800221func (r *rule) because(reason string) *rule {
222 r.reason = reason
223 return r
224}
225
226func (r *rule) String() string {
227 s := "neverallow"
228 for _, v := range r.paths {
229 s += " dir:" + v + "*"
230 }
231 for _, v := range r.unlessPaths {
232 s += " -dir:" + v + "*"
233 }
Colin Crossfd4f7432019-03-05 15:06:16 -0800234 for _, v := range r.moduleTypes {
235 s += " type:" + v
236 }
237 for _, v := range r.unlessModuleTypes {
238 s += " -type:" + v
239 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800240 for _, v := range r.props {
241 s += " " + strings.Join(v.fields, ".") + "=" + v.value
242 }
243 for _, v := range r.unlessProps {
244 s += " -" + strings.Join(v.fields, ".") + "=" + v.value
245 }
246 if len(r.reason) != 0 {
247 s += " which is restricted because " + r.reason
248 }
249 return s
250}
251
252func (r *rule) appliesToPath(dir string) bool {
253 includePath := len(r.paths) == 0 || hasAnyPrefix(dir, r.paths)
254 excludePath := hasAnyPrefix(dir, r.unlessPaths)
255 return includePath && !excludePath
256}
257
Colin Crossfd4f7432019-03-05 15:06:16 -0800258func (r *rule) appliesToModuleType(moduleType string) bool {
259 return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
260}
261
Steven Moreland65b3fd92017-12-06 14:18:35 -0800262func (r *rule) appliesToProperties(properties []interface{}) bool {
263 includeProps := hasAllProperties(properties, r.props)
264 excludeProps := hasAnyProperty(properties, r.unlessProps)
265 return includeProps && !excludeProps
266}
267
268// assorted utils
269
270func cleanPaths(paths []string) []string {
271 res := make([]string, len(paths))
272 for i, v := range paths {
273 res[i] = filepath.Clean(v) + "/"
274 }
275 return res
276}
277
278func fieldNamesForProperties(propertyNames string) []string {
279 names := strings.Split(propertyNames, ".")
280 for i, v := range names {
281 names[i] = proptools.FieldNameForProperty(v)
282 }
283 return names
284}
285
286func hasAnyPrefix(s string, prefixes []string) bool {
287 for _, prefix := range prefixes {
288 if strings.HasPrefix(s, prefix) {
289 return true
290 }
291 }
292 return false
293}
294
295func hasAnyProperty(properties []interface{}, props []ruleProperty) bool {
296 for _, v := range props {
297 if hasProperty(properties, v) {
298 return true
299 }
300 }
301 return false
302}
303
304func hasAllProperties(properties []interface{}, props []ruleProperty) bool {
305 for _, v := range props {
306 if !hasProperty(properties, v) {
307 return false
308 }
309 }
310 return true
311}
312
313func hasProperty(properties []interface{}, prop ruleProperty) bool {
314 for _, propertyStruct := range properties {
315 propertiesValue := reflect.ValueOf(propertyStruct).Elem()
316 for _, v := range prop.fields {
317 if !propertiesValue.IsValid() {
318 break
319 }
320 propertiesValue = propertiesValue.FieldByName(v)
321 }
322 if !propertiesValue.IsValid() {
323 continue
324 }
325
326 check := func(v string) bool {
327 return prop.value == "*" || prop.value == v
328 }
329
330 if matchValue(propertiesValue, check) {
331 return true
332 }
333 }
334 return false
335}
336
337func matchValue(value reflect.Value, check func(string) bool) bool {
338 if !value.IsValid() {
339 return false
340 }
341
342 if value.Kind() == reflect.Ptr {
343 if value.IsNil() {
344 return check("")
345 }
346 value = value.Elem()
347 }
348
349 switch value.Kind() {
350 case reflect.String:
351 return check(value.String())
352 case reflect.Bool:
353 return check(strconv.FormatBool(value.Bool()))
354 case reflect.Int:
355 return check(strconv.FormatInt(value.Int(), 10))
356 case reflect.Slice:
357 slice, ok := value.Interface().([]string)
358 if !ok {
359 panic("Can only handle slice of string")
360 }
361 for _, v := range slice {
362 if check(v) {
363 return true
364 }
365 }
366 return false
367 }
368
369 panic("Can't handle type: " + value.Kind().String())
370}