blob: f63f461813cffbe6fdd50c5d721555426bb609d6 [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()...)
Colin Crossc35c5f92019-03-05 15:06:16 -080054 rules = append(rules, createJavaDeviceForHostRules()...)
Neil Fullerdf5f3562018-10-21 17:19:10 +010055 return rules
56}
Steven Moreland65b3fd92017-12-06 14:18:35 -080057
Neil Fullerdf5f3562018-10-21 17:19:10 +010058func createTrebleRules() []*rule {
59 return []*rule{
60 neverallow().
61 in("vendor", "device").
62 with("vndk.enabled", "true").
63 without("vendor", "true").
64 because("the VNDK can never contain a library that is device dependent."),
65 neverallow().
66 with("vndk.enabled", "true").
67 without("vendor", "true").
68 without("owner", "").
69 because("a VNDK module can never have an owner."),
Steven Moreland65b3fd92017-12-06 14:18:35 -080070
Neil Fullerdf5f3562018-10-21 17:19:10 +010071 // TODO(b/67974785): always enforce the manifest
72 neverallow().
73 without("name", "libhidltransport").
74 with("product_variables.enforce_vintf_manifest.cflags", "*").
75 because("manifest enforcement should be independent of ."),
76
77 // TODO(b/67975799): vendor code should always use /vendor/bin/sh
78 neverallow().
79 without("name", "libc_bionic_ndk").
80 with("product_variables.treble_linker_namespaces.cflags", "*").
81 because("nothing should care if linker namespaces are enabled or not"),
82
83 // Example:
84 // *neverallow().with("Srcs", "main.cpp"))
85 }
86}
87
88func createLibcoreRules() []*rule {
89 var coreLibraryProjects = []string{
90 "libcore",
91 "external/apache-harmony",
92 "external/apache-xml",
93 "external/bouncycastle",
94 "external/conscrypt",
95 "external/icu",
96 "external/okhttp",
97 "external/wycheproof",
98 }
99
100 var coreModules = []string{
101 "core-all",
102 "core-oj",
103 "core-libart",
Neil Fullerdf5f3562018-10-21 17:19:10 +0100104 "okhttp",
105 "bouncycastle",
106 "conscrypt",
107 "apache-xml",
108 }
109
110 // Core library constraints. Prevent targets adding dependencies on core
111 // library internals, which could lead to compatibility issues with the ART
112 // mainline module. They should use core.platform.api.stubs instead.
113 rules := []*rule{
114 neverallow().
115 notIn(append(coreLibraryProjects, "development")...).
116 with("no_standard_libs", "true"),
117 }
118
119 for _, m := range coreModules {
120 r := neverallow().
121 notIn(coreLibraryProjects...).
122 with("libs", m).
123 because("Only core libraries projects can depend on " + m)
124 rules = append(rules, r)
125 }
126 return rules
Steven Moreland65b3fd92017-12-06 14:18:35 -0800127}
128
Colin Crossc35c5f92019-03-05 15:06:16 -0800129func createJavaDeviceForHostRules() []*rule {
130 javaDeviceForHostProjectsWhitelist := []string{
131 "external/robolectric-shadows",
132 "framework/layoutlib",
133 }
134
135 return []*rule{
136 neverallow().
137 notIn(javaDeviceForHostProjectsWhitelist...).
138 moduleType("java_device_for_host", "java_host_for_device").
139 because("java_device_for_host can only be used in whitelisted projects"),
140 }
141}
142
Steven Moreland65b3fd92017-12-06 14:18:35 -0800143func neverallowMutator(ctx BottomUpMutatorContext) {
144 m, ok := ctx.Module().(Module)
145 if !ok {
146 return
147 }
148
149 dir := ctx.ModuleDir() + "/"
150 properties := m.GetProperties()
151
152 for _, n := range neverallows {
153 if !n.appliesToPath(dir) {
154 continue
155 }
156
Colin Crossc35c5f92019-03-05 15:06:16 -0800157 if !n.appliesToModuleType(ctx.ModuleType()) {
158 continue
159 }
160
Steven Moreland65b3fd92017-12-06 14:18:35 -0800161 if !n.appliesToProperties(properties) {
162 continue
163 }
164
165 ctx.ModuleErrorf("violates " + n.String())
166 }
167}
168
169type ruleProperty struct {
170 fields []string // e.x.: Vndk.Enabled
171 value string // e.x.: true
172}
173
174type rule struct {
175 // User string for why this is a thing.
176 reason string
177
178 paths []string
179 unlessPaths []string
180
Colin Crossc35c5f92019-03-05 15:06:16 -0800181 moduleTypes []string
182 unlessModuleTypes []string
183
Steven Moreland65b3fd92017-12-06 14:18:35 -0800184 props []ruleProperty
185 unlessProps []ruleProperty
186}
187
188func neverallow() *rule {
189 return &rule{}
190}
Colin Crossc35c5f92019-03-05 15:06:16 -0800191
Steven Moreland65b3fd92017-12-06 14:18:35 -0800192func (r *rule) in(path ...string) *rule {
193 r.paths = append(r.paths, cleanPaths(path)...)
194 return r
195}
Colin Crossc35c5f92019-03-05 15:06:16 -0800196
Steven Moreland65b3fd92017-12-06 14:18:35 -0800197func (r *rule) notIn(path ...string) *rule {
198 r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
199 return r
200}
Colin Crossc35c5f92019-03-05 15:06:16 -0800201
202func (r *rule) moduleType(types ...string) *rule {
203 r.moduleTypes = append(r.moduleTypes, types...)
204 return r
205}
206
207func (r *rule) notModuleType(types ...string) *rule {
208 r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
209 return r
210}
211
Steven Moreland65b3fd92017-12-06 14:18:35 -0800212func (r *rule) with(properties, value string) *rule {
213 r.props = append(r.props, ruleProperty{
214 fields: fieldNamesForProperties(properties),
215 value: value,
216 })
217 return r
218}
Colin Crossc35c5f92019-03-05 15:06:16 -0800219
Steven Moreland65b3fd92017-12-06 14:18:35 -0800220func (r *rule) without(properties, value string) *rule {
221 r.unlessProps = append(r.unlessProps, ruleProperty{
222 fields: fieldNamesForProperties(properties),
223 value: value,
224 })
225 return r
226}
Colin Crossc35c5f92019-03-05 15:06:16 -0800227
Steven Moreland65b3fd92017-12-06 14:18:35 -0800228func (r *rule) because(reason string) *rule {
229 r.reason = reason
230 return r
231}
232
233func (r *rule) String() string {
234 s := "neverallow"
235 for _, v := range r.paths {
236 s += " dir:" + v + "*"
237 }
238 for _, v := range r.unlessPaths {
239 s += " -dir:" + v + "*"
240 }
Colin Crossc35c5f92019-03-05 15:06:16 -0800241 for _, v := range r.moduleTypes {
242 s += " type:" + v
243 }
244 for _, v := range r.unlessModuleTypes {
245 s += " -type:" + v
246 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800247 for _, v := range r.props {
248 s += " " + strings.Join(v.fields, ".") + "=" + v.value
249 }
250 for _, v := range r.unlessProps {
251 s += " -" + strings.Join(v.fields, ".") + "=" + v.value
252 }
253 if len(r.reason) != 0 {
254 s += " which is restricted because " + r.reason
255 }
256 return s
257}
258
259func (r *rule) appliesToPath(dir string) bool {
260 includePath := len(r.paths) == 0 || hasAnyPrefix(dir, r.paths)
261 excludePath := hasAnyPrefix(dir, r.unlessPaths)
262 return includePath && !excludePath
263}
264
Colin Crossc35c5f92019-03-05 15:06:16 -0800265func (r *rule) appliesToModuleType(moduleType string) bool {
266 return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
267}
268
Steven Moreland65b3fd92017-12-06 14:18:35 -0800269func (r *rule) appliesToProperties(properties []interface{}) bool {
270 includeProps := hasAllProperties(properties, r.props)
271 excludeProps := hasAnyProperty(properties, r.unlessProps)
272 return includeProps && !excludeProps
273}
274
275// assorted utils
276
277func cleanPaths(paths []string) []string {
278 res := make([]string, len(paths))
279 for i, v := range paths {
280 res[i] = filepath.Clean(v) + "/"
281 }
282 return res
283}
284
285func fieldNamesForProperties(propertyNames string) []string {
286 names := strings.Split(propertyNames, ".")
287 for i, v := range names {
288 names[i] = proptools.FieldNameForProperty(v)
289 }
290 return names
291}
292
293func hasAnyPrefix(s string, prefixes []string) bool {
294 for _, prefix := range prefixes {
295 if strings.HasPrefix(s, prefix) {
296 return true
297 }
298 }
299 return false
300}
301
302func hasAnyProperty(properties []interface{}, props []ruleProperty) bool {
303 for _, v := range props {
304 if hasProperty(properties, v) {
305 return true
306 }
307 }
308 return false
309}
310
311func hasAllProperties(properties []interface{}, props []ruleProperty) bool {
312 for _, v := range props {
313 if !hasProperty(properties, v) {
314 return false
315 }
316 }
317 return true
318}
319
320func hasProperty(properties []interface{}, prop ruleProperty) bool {
321 for _, propertyStruct := range properties {
322 propertiesValue := reflect.ValueOf(propertyStruct).Elem()
323 for _, v := range prop.fields {
324 if !propertiesValue.IsValid() {
325 break
326 }
327 propertiesValue = propertiesValue.FieldByName(v)
328 }
329 if !propertiesValue.IsValid() {
330 continue
331 }
332
333 check := func(v string) bool {
334 return prop.value == "*" || prop.value == v
335 }
336
337 if matchValue(propertiesValue, check) {
338 return true
339 }
340 }
341 return false
342}
343
344func matchValue(value reflect.Value, check func(string) bool) bool {
345 if !value.IsValid() {
346 return false
347 }
348
349 if value.Kind() == reflect.Ptr {
350 if value.IsNil() {
351 return check("")
352 }
353 value = value.Elem()
354 }
355
356 switch value.Kind() {
357 case reflect.String:
358 return check(value.String())
359 case reflect.Bool:
360 return check(strconv.FormatBool(value.Bool()))
361 case reflect.Int:
362 return check(strconv.FormatInt(value.Int(), 10))
363 case reflect.Slice:
364 slice, ok := value.Interface().([]string)
365 if !ok {
366 panic("Can only handle slice of string")
367 }
368 for _, v := range slice {
369 if check(v) {
370 return true
371 }
372 }
373 return false
374 }
375
376 panic("Can't handle type: " + value.Kind().String())
377}