blob: 50fbe5e8903e05c0324e141cf63ccc50ea0cdf43 [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
101 var coreModules = []string{
102 "core-all",
103 "core-oj",
104 "core-libart",
Neil Fullerdf5f3562018-10-21 17:19:10 +0100105 "okhttp",
106 "bouncycastle",
107 "conscrypt",
108 "apache-xml",
109 }
110
111 // Core library constraints. Prevent targets adding dependencies on core
112 // library internals, which could lead to compatibility issues with the ART
113 // mainline module. They should use core.platform.api.stubs instead.
114 rules := []*rule{
115 neverallow().
116 notIn(append(coreLibraryProjects, "development")...).
117 with("no_standard_libs", "true"),
118 }
119
120 for _, m := range coreModules {
121 r := neverallow().
122 notIn(coreLibraryProjects...).
123 with("libs", m).
124 because("Only core libraries projects can depend on " + m)
125 rules = append(rules, r)
126 }
127 return rules
Steven Moreland65b3fd92017-12-06 14:18:35 -0800128}
129
Dongwon Kang50a299f2019-02-04 09:00:51 -0800130func createMediaRules() []*rule {
131 return []*rule{
132 neverallow().
133 with("libs", "updatable-media").
134 because("updatable-media includes private APIs. Use updatable_media_stubs instead."),
135 }
136}
137
Colin Crossfd4f7432019-03-05 15:06:16 -0800138func createJavaDeviceForHostRules() []*rule {
139 javaDeviceForHostProjectsWhitelist := []string{
Colin Crossb5191a52019-04-11 14:07:38 -0700140 "external/guava",
Colin Crossfd4f7432019-03-05 15:06:16 -0800141 "external/robolectric-shadows",
142 "framework/layoutlib",
143 }
144
145 return []*rule{
146 neverallow().
147 notIn(javaDeviceForHostProjectsWhitelist...).
148 moduleType("java_device_for_host", "java_host_for_device").
149 because("java_device_for_host can only be used in whitelisted projects"),
150 }
151}
152
Steven Moreland65b3fd92017-12-06 14:18:35 -0800153func neverallowMutator(ctx BottomUpMutatorContext) {
154 m, ok := ctx.Module().(Module)
155 if !ok {
156 return
157 }
158
159 dir := ctx.ModuleDir() + "/"
160 properties := m.GetProperties()
161
162 for _, n := range neverallows {
163 if !n.appliesToPath(dir) {
164 continue
165 }
166
Colin Crossfd4f7432019-03-05 15:06:16 -0800167 if !n.appliesToModuleType(ctx.ModuleType()) {
168 continue
169 }
170
Steven Moreland65b3fd92017-12-06 14:18:35 -0800171 if !n.appliesToProperties(properties) {
172 continue
173 }
174
175 ctx.ModuleErrorf("violates " + n.String())
176 }
177}
178
179type ruleProperty struct {
180 fields []string // e.x.: Vndk.Enabled
181 value string // e.x.: true
182}
183
184type rule struct {
185 // User string for why this is a thing.
186 reason string
187
188 paths []string
189 unlessPaths []string
190
Colin Crossfd4f7432019-03-05 15:06:16 -0800191 moduleTypes []string
192 unlessModuleTypes []string
193
Steven Moreland65b3fd92017-12-06 14:18:35 -0800194 props []ruleProperty
195 unlessProps []ruleProperty
196}
197
198func neverallow() *rule {
199 return &rule{}
200}
Colin Crossfd4f7432019-03-05 15:06:16 -0800201
Steven Moreland65b3fd92017-12-06 14:18:35 -0800202func (r *rule) in(path ...string) *rule {
203 r.paths = append(r.paths, cleanPaths(path)...)
204 return r
205}
Colin Crossfd4f7432019-03-05 15:06:16 -0800206
Steven Moreland65b3fd92017-12-06 14:18:35 -0800207func (r *rule) notIn(path ...string) *rule {
208 r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
209 return r
210}
Colin Crossfd4f7432019-03-05 15:06:16 -0800211
212func (r *rule) moduleType(types ...string) *rule {
213 r.moduleTypes = append(r.moduleTypes, types...)
214 return r
215}
216
217func (r *rule) notModuleType(types ...string) *rule {
218 r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
219 return r
220}
221
Steven Moreland65b3fd92017-12-06 14:18:35 -0800222func (r *rule) with(properties, value string) *rule {
223 r.props = append(r.props, ruleProperty{
224 fields: fieldNamesForProperties(properties),
225 value: value,
226 })
227 return r
228}
Colin Crossfd4f7432019-03-05 15:06:16 -0800229
Steven Moreland65b3fd92017-12-06 14:18:35 -0800230func (r *rule) without(properties, value string) *rule {
231 r.unlessProps = append(r.unlessProps, ruleProperty{
232 fields: fieldNamesForProperties(properties),
233 value: value,
234 })
235 return r
236}
Colin Crossfd4f7432019-03-05 15:06:16 -0800237
Steven Moreland65b3fd92017-12-06 14:18:35 -0800238func (r *rule) because(reason string) *rule {
239 r.reason = reason
240 return r
241}
242
243func (r *rule) String() string {
244 s := "neverallow"
245 for _, v := range r.paths {
246 s += " dir:" + v + "*"
247 }
248 for _, v := range r.unlessPaths {
249 s += " -dir:" + v + "*"
250 }
Colin Crossfd4f7432019-03-05 15:06:16 -0800251 for _, v := range r.moduleTypes {
252 s += " type:" + v
253 }
254 for _, v := range r.unlessModuleTypes {
255 s += " -type:" + v
256 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800257 for _, v := range r.props {
258 s += " " + strings.Join(v.fields, ".") + "=" + v.value
259 }
260 for _, v := range r.unlessProps {
261 s += " -" + strings.Join(v.fields, ".") + "=" + v.value
262 }
263 if len(r.reason) != 0 {
264 s += " which is restricted because " + r.reason
265 }
266 return s
267}
268
269func (r *rule) appliesToPath(dir string) bool {
270 includePath := len(r.paths) == 0 || hasAnyPrefix(dir, r.paths)
271 excludePath := hasAnyPrefix(dir, r.unlessPaths)
272 return includePath && !excludePath
273}
274
Colin Crossfd4f7432019-03-05 15:06:16 -0800275func (r *rule) appliesToModuleType(moduleType string) bool {
276 return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
277}
278
Steven Moreland65b3fd92017-12-06 14:18:35 -0800279func (r *rule) appliesToProperties(properties []interface{}) bool {
280 includeProps := hasAllProperties(properties, r.props)
281 excludeProps := hasAnyProperty(properties, r.unlessProps)
282 return includeProps && !excludeProps
283}
284
285// assorted utils
286
287func cleanPaths(paths []string) []string {
288 res := make([]string, len(paths))
289 for i, v := range paths {
290 res[i] = filepath.Clean(v) + "/"
291 }
292 return res
293}
294
295func fieldNamesForProperties(propertyNames string) []string {
296 names := strings.Split(propertyNames, ".")
297 for i, v := range names {
298 names[i] = proptools.FieldNameForProperty(v)
299 }
300 return names
301}
302
303func hasAnyPrefix(s string, prefixes []string) bool {
304 for _, prefix := range prefixes {
305 if strings.HasPrefix(s, prefix) {
306 return true
307 }
308 }
309 return false
310}
311
312func hasAnyProperty(properties []interface{}, props []ruleProperty) bool {
313 for _, v := range props {
314 if hasProperty(properties, v) {
315 return true
316 }
317 }
318 return false
319}
320
321func hasAllProperties(properties []interface{}, props []ruleProperty) bool {
322 for _, v := range props {
323 if !hasProperty(properties, v) {
324 return false
325 }
326 }
327 return true
328}
329
330func hasProperty(properties []interface{}, prop ruleProperty) bool {
331 for _, propertyStruct := range properties {
332 propertiesValue := reflect.ValueOf(propertyStruct).Elem()
333 for _, v := range prop.fields {
334 if !propertiesValue.IsValid() {
335 break
336 }
337 propertiesValue = propertiesValue.FieldByName(v)
338 }
339 if !propertiesValue.IsValid() {
340 continue
341 }
342
343 check := func(v string) bool {
344 return prop.value == "*" || prop.value == v
345 }
346
347 if matchValue(propertiesValue, check) {
348 return true
349 }
350 }
351 return false
352}
353
354func matchValue(value reflect.Value, check func(string) bool) bool {
355 if !value.IsValid() {
356 return false
357 }
358
359 if value.Kind() == reflect.Ptr {
360 if value.IsNil() {
361 return check("")
362 }
363 value = value.Elem()
364 }
365
366 switch value.Kind() {
367 case reflect.String:
368 return check(value.String())
369 case reflect.Bool:
370 return check(strconv.FormatBool(value.Bool()))
371 case reflect.Int:
372 return check(strconv.FormatInt(value.Int(), 10))
373 case reflect.Slice:
374 slice, ok := value.Interface().([]string)
375 if !ok {
376 panic("Can only handle slice of string")
377 }
378 for _, v := range slice {
379 if check(v) {
380 return true
381 }
382 }
383 return false
384 }
385
386 panic("Can't handle type: " + value.Kind().String())
387}