blob: fba43b3a1e180b7aaacc52276b9073096e8d8fb9 [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{
Colin Cross97add502019-04-11 14:07:38 -0700131 "external/guava",
Colin Crossc35c5f92019-03-05 15:06:16 -0800132 "external/robolectric-shadows",
133 "framework/layoutlib",
134 }
135
136 return []*rule{
137 neverallow().
138 notIn(javaDeviceForHostProjectsWhitelist...).
139 moduleType("java_device_for_host", "java_host_for_device").
140 because("java_device_for_host can only be used in whitelisted projects"),
141 }
142}
143
Steven Moreland65b3fd92017-12-06 14:18:35 -0800144func neverallowMutator(ctx BottomUpMutatorContext) {
145 m, ok := ctx.Module().(Module)
146 if !ok {
147 return
148 }
149
150 dir := ctx.ModuleDir() + "/"
151 properties := m.GetProperties()
152
153 for _, n := range neverallows {
154 if !n.appliesToPath(dir) {
155 continue
156 }
157
Colin Crossc35c5f92019-03-05 15:06:16 -0800158 if !n.appliesToModuleType(ctx.ModuleType()) {
159 continue
160 }
161
Steven Moreland65b3fd92017-12-06 14:18:35 -0800162 if !n.appliesToProperties(properties) {
163 continue
164 }
165
166 ctx.ModuleErrorf("violates " + n.String())
167 }
168}
169
170type ruleProperty struct {
171 fields []string // e.x.: Vndk.Enabled
172 value string // e.x.: true
173}
174
175type rule struct {
176 // User string for why this is a thing.
177 reason string
178
179 paths []string
180 unlessPaths []string
181
Colin Crossc35c5f92019-03-05 15:06:16 -0800182 moduleTypes []string
183 unlessModuleTypes []string
184
Steven Moreland65b3fd92017-12-06 14:18:35 -0800185 props []ruleProperty
186 unlessProps []ruleProperty
187}
188
189func neverallow() *rule {
190 return &rule{}
191}
Colin Crossc35c5f92019-03-05 15:06:16 -0800192
Steven Moreland65b3fd92017-12-06 14:18:35 -0800193func (r *rule) in(path ...string) *rule {
194 r.paths = append(r.paths, cleanPaths(path)...)
195 return r
196}
Colin Crossc35c5f92019-03-05 15:06:16 -0800197
Steven Moreland65b3fd92017-12-06 14:18:35 -0800198func (r *rule) notIn(path ...string) *rule {
199 r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
200 return r
201}
Colin Crossc35c5f92019-03-05 15:06:16 -0800202
203func (r *rule) moduleType(types ...string) *rule {
204 r.moduleTypes = append(r.moduleTypes, types...)
205 return r
206}
207
208func (r *rule) notModuleType(types ...string) *rule {
209 r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
210 return r
211}
212
Steven Moreland65b3fd92017-12-06 14:18:35 -0800213func (r *rule) with(properties, value string) *rule {
214 r.props = append(r.props, ruleProperty{
215 fields: fieldNamesForProperties(properties),
216 value: value,
217 })
218 return r
219}
Colin Crossc35c5f92019-03-05 15:06:16 -0800220
Steven Moreland65b3fd92017-12-06 14:18:35 -0800221func (r *rule) without(properties, value string) *rule {
222 r.unlessProps = append(r.unlessProps, ruleProperty{
223 fields: fieldNamesForProperties(properties),
224 value: value,
225 })
226 return r
227}
Colin Crossc35c5f92019-03-05 15:06:16 -0800228
Steven Moreland65b3fd92017-12-06 14:18:35 -0800229func (r *rule) because(reason string) *rule {
230 r.reason = reason
231 return r
232}
233
234func (r *rule) String() string {
235 s := "neverallow"
236 for _, v := range r.paths {
237 s += " dir:" + v + "*"
238 }
239 for _, v := range r.unlessPaths {
240 s += " -dir:" + v + "*"
241 }
Colin Crossc35c5f92019-03-05 15:06:16 -0800242 for _, v := range r.moduleTypes {
243 s += " type:" + v
244 }
245 for _, v := range r.unlessModuleTypes {
246 s += " -type:" + v
247 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800248 for _, v := range r.props {
249 s += " " + strings.Join(v.fields, ".") + "=" + v.value
250 }
251 for _, v := range r.unlessProps {
252 s += " -" + strings.Join(v.fields, ".") + "=" + v.value
253 }
254 if len(r.reason) != 0 {
255 s += " which is restricted because " + r.reason
256 }
257 return s
258}
259
260func (r *rule) appliesToPath(dir string) bool {
261 includePath := len(r.paths) == 0 || hasAnyPrefix(dir, r.paths)
262 excludePath := hasAnyPrefix(dir, r.unlessPaths)
263 return includePath && !excludePath
264}
265
Colin Crossc35c5f92019-03-05 15:06:16 -0800266func (r *rule) appliesToModuleType(moduleType string) bool {
267 return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
268}
269
Steven Moreland65b3fd92017-12-06 14:18:35 -0800270func (r *rule) appliesToProperties(properties []interface{}) bool {
271 includeProps := hasAllProperties(properties, r.props)
272 excludeProps := hasAnyProperty(properties, r.unlessProps)
273 return includeProps && !excludeProps
274}
275
276// assorted utils
277
278func cleanPaths(paths []string) []string {
279 res := make([]string, len(paths))
280 for i, v := range paths {
281 res[i] = filepath.Clean(v) + "/"
282 }
283 return res
284}
285
286func fieldNamesForProperties(propertyNames string) []string {
287 names := strings.Split(propertyNames, ".")
288 for i, v := range names {
289 names[i] = proptools.FieldNameForProperty(v)
290 }
291 return names
292}
293
294func hasAnyPrefix(s string, prefixes []string) bool {
295 for _, prefix := range prefixes {
296 if strings.HasPrefix(s, prefix) {
297 return true
298 }
299 }
300 return false
301}
302
303func hasAnyProperty(properties []interface{}, props []ruleProperty) bool {
304 for _, v := range props {
305 if hasProperty(properties, v) {
306 return true
307 }
308 }
309 return false
310}
311
312func hasAllProperties(properties []interface{}, props []ruleProperty) bool {
313 for _, v := range props {
314 if !hasProperty(properties, v) {
315 return false
316 }
317 }
318 return true
319}
320
321func hasProperty(properties []interface{}, prop ruleProperty) bool {
322 for _, propertyStruct := range properties {
323 propertiesValue := reflect.ValueOf(propertyStruct).Elem()
324 for _, v := range prop.fields {
325 if !propertiesValue.IsValid() {
326 break
327 }
328 propertiesValue = propertiesValue.FieldByName(v)
329 }
330 if !propertiesValue.IsValid() {
331 continue
332 }
333
334 check := func(v string) bool {
335 return prop.value == "*" || prop.value == v
336 }
337
338 if matchValue(propertiesValue, check) {
339 return true
340 }
341 }
342 return false
343}
344
345func matchValue(value reflect.Value, check func(string) bool) bool {
346 if !value.IsValid() {
347 return false
348 }
349
350 if value.Kind() == reflect.Ptr {
351 if value.IsNil() {
352 return check("")
353 }
354 value = value.Elem()
355 }
356
357 switch value.Kind() {
358 case reflect.String:
359 return check(value.String())
360 case reflect.Bool:
361 return check(strconv.FormatBool(value.Bool()))
362 case reflect.Int:
363 return check(strconv.FormatInt(value.Int(), 10))
364 case reflect.Slice:
365 slice, ok := value.Interface().([]string)
366 if !ok {
367 panic("Can only handle slice of string")
368 }
369 for _, v := range slice {
370 if check(v) {
371 return true
372 }
373 }
374 return false
375 }
376
377 panic("Can't handle type: " + value.Kind().String())
378}