class_loader_context.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. // Copyright 2020 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. package dexpreopt
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "sort"
  19. "strconv"
  20. "strings"
  21. "android/soong/android"
  22. )
  23. // This comment describes the following:
  24. // 1. the concept of class loader context (CLC) and its relation to classpath
  25. // 2. how PackageManager constructs CLC from shared libraries and their dependencies
  26. // 3. build-time vs. run-time CLC and why this matters for dexpreopt
  27. // 4. manifest fixer: a tool that adds missing <uses-library> tags to the manifests
  28. // 5. build system support for CLC
  29. //
  30. // 1. Class loader context
  31. // -----------------------
  32. //
  33. // Java libraries and apps that have run-time dependency on other libraries should list the used
  34. // libraries in their manifest (AndroidManifest.xml file). Each used library should be specified in
  35. // a <uses-library> tag that has the library name and an optional attribute specifying if the
  36. // library is optional or required. Required libraries are necessary for the library/app to run (it
  37. // will fail at runtime if the library cannot be loaded), and optional libraries are used only if
  38. // they are present (if not, the library/app can run without them).
  39. //
  40. // The libraries listed in <uses-library> tags are in the classpath of a library/app.
  41. //
  42. // Besides libraries, an app may also use another APK (for example in the case of split APKs), or
  43. // anything that gets added by the app dynamically. In general, it is impossible to know at build
  44. // time what the app may use at runtime. In the build system we focus on the known part: libraries.
  45. //
  46. // Class loader context (CLC) is a tree-like structure that describes class loader hierarchy. The
  47. // build system uses CLC in a more narrow sense: it is a tree of libraries that represents
  48. // transitive closure of all <uses-library> dependencies of a library/app. The top-level elements of
  49. // a CLC are the direct <uses-library> dependencies specified in the manifest (aka. classpath). Each
  50. // node of a CLC tree is a <uses-library> which may have its own <uses-library> sub-nodes.
  51. //
  52. // Because <uses-library> dependencies are, in general, a graph and not necessarily a tree, CLC may
  53. // contain subtrees for the same library multiple times. In other words, CLC is the dependency graph
  54. // "unfolded" to a tree. The duplication is only on a logical level, and the actual underlying class
  55. // loaders are not duplicated (at runtime there is a single class loader instance for each library).
  56. //
  57. // Example: A has <uses-library> tags B, C and D; C has <uses-library tags> B and D;
  58. //
  59. // D has <uses-library> E; B and E have no <uses-library> dependencies. The CLC is:
  60. // A
  61. // ├── B
  62. // ├── C
  63. // │ ├── B
  64. // │ └── D
  65. // │ └── E
  66. // └── D
  67. // └── E
  68. //
  69. // CLC defines the lookup order of libraries when resolving Java classes used by the library/app.
  70. // The lookup order is important because libraries may contain duplicate classes, and the class is
  71. // resolved to the first match.
  72. //
  73. // 2. PackageManager and "shared" libraries
  74. // ----------------------------------------
  75. //
  76. // In order to load an APK at runtime, PackageManager (in frameworks/base) creates a CLC. It adds
  77. // the libraries listed in the <uses-library> tags in the app's manifest as top-level CLC elements.
  78. // For each of the used libraries PackageManager gets all its <uses-library> dependencies (specified
  79. // as tags in the manifest of that library) and adds a nested CLC for each dependency. This process
  80. // continues recursively until all leaf nodes of the constructed CLC tree are libraries that have no
  81. // <uses-library> dependencies.
  82. //
  83. // PackageManager is aware only of "shared" libraries. The definition of "shared" here differs from
  84. // its usual meaning (as in shared vs. static). In Android, Java "shared" libraries are those listed
  85. // in /system/etc/permissions/platform.xml file. This file is installed on device. Each entry in it
  86. // contains the name of a "shared" library, a path to its DEX jar file and a list of dependencies
  87. // (other "shared" libraries that this one uses at runtime and specifies them in <uses-library> tags
  88. // in its manifest).
  89. //
  90. // In other words, there are two sources of information that allow PackageManager to construct CLC
  91. // at runtime: <uses-library> tags in the manifests and "shared" library dependencies in
  92. // /system/etc/permissions/platform.xml.
  93. //
  94. // 3. Build-time and run-time CLC and dexpreopt
  95. // --------------------------------------------
  96. //
  97. // CLC is needed not only when loading a library/app, but also when compiling it. Compilation may
  98. // happen either on device (known as "dexopt") or during the build (known as "dexpreopt"). Since
  99. // dexopt takes place on device, it has the same information as PackageManager (manifests and
  100. // shared library dependencies). Dexpreopt, on the other hand, takes place on host and in a totally
  101. // different environment, and it has to get the same information from the build system (see the
  102. // section about build system support below).
  103. //
  104. // Thus, the build-time CLC used by dexpreopt and the run-time CLC used by PackageManager are
  105. // the same thing, but computed in two different ways.
  106. //
  107. // It is important that build-time and run-time CLCs coincide, otherwise the AOT-compiled code
  108. // created by dexpreopt will be rejected. In order to check the equality of build-time and
  109. // run-time CLCs, the dex2oat compiler records build-time CLC in the *.odex files (in the
  110. // "classpath" field of the OAT file header). To find the stored CLC, use the following command:
  111. // `oatdump --oat-file=<FILE> | grep '^classpath = '`.
  112. //
  113. // Mismatch between build-time and run-time CLC is reported in logcat during boot (search with
  114. // `logcat | grep -E 'ClassLoaderContext [a-z ]+ mismatch'`. Mismatch is bad for performance, as it
  115. // forces the library/app to either be dexopted, or to run without any optimizations (e.g. the app's
  116. // code may need to be extracted in memory from the APK, a very expensive operation).
  117. //
  118. // A <uses-library> can be either optional or required. From dexpreopt standpoint, required library
  119. // must be present at build time (its absence is a build error). An optional library may be either
  120. // present or absent at build time: if present, it will be added to the CLC, passed to dex2oat and
  121. // recorded in the *.odex file; otherwise, if the library is absent, it will be skipped and not
  122. // added to CLC. If there is a mismatch between built-time and run-time status (optional library is
  123. // present in one case, but not the other), then the build-time and run-time CLCs won't match and
  124. // the compiled code will be rejected. It is unknown at build time if the library will be present at
  125. // runtime, therefore either including or excluding it may cause CLC mismatch.
  126. //
  127. // 4. Manifest fixer
  128. // -----------------
  129. //
  130. // Sometimes <uses-library> tags are missing from the source manifest of a library/app. This may
  131. // happen for example if one of the transitive dependencies of the library/app starts using another
  132. // <uses-library>, and the library/app's manifest isn't updated to include it.
  133. //
  134. // Soong can compute some of the missing <uses-library> tags for a given library/app automatically
  135. // as SDK libraries in the transitive dependency closure of the library/app. The closure is needed
  136. // because a library/app may depend on a static library that may in turn depend on an SDK library,
  137. // (possibly transitively via another library).
  138. //
  139. // Not all <uses-library> tags can be computed in this way, because some of the <uses-library>
  140. // dependencies are not SDK libraries, or they are not reachable via transitive dependency closure.
  141. // But when possible, allowing Soong to calculate the manifest entries is less prone to errors and
  142. // simplifies maintenance. For example, consider a situation when many apps use some static library
  143. // that adds a new <uses-library> dependency -- all the apps will have to be updated. That is
  144. // difficult to maintain.
  145. //
  146. // Soong computes the libraries that need to be in the manifest as the top-level libraries in CLC.
  147. // These libraries are passed to the manifest_fixer.
  148. //
  149. // All libraries added to the manifest should be "shared" libraries, so that PackageManager can look
  150. // up their dependencies and reconstruct the nested subcontexts at runtime. There is no build check
  151. // to ensure this, it is an assumption.
  152. //
  153. // 5. Build system support
  154. // -----------------------
  155. //
  156. // In order to construct CLC for dexpreopt and manifest_fixer, the build system needs to know all
  157. // <uses-library> dependencies of the dexpreopted library/app (including transitive dependencies).
  158. // For each <uses-librarry> dependency it needs to know the following information:
  159. //
  160. // - the real name of the <uses-library> (it may be different from the module name)
  161. // - build-time (on host) and run-time (on device) paths to the DEX jar file of the library
  162. // - whether this library is optional or required
  163. // - all <uses-library> dependencies
  164. //
  165. // Since the build system doesn't have access to the manifest contents (it cannot read manifests at
  166. // the time of build rule generation), it is necessary to copy this information to the Android.bp
  167. // and Android.mk files. For blueprints, the relevant properties are `uses_libs` and
  168. // `optional_uses_libs`. For makefiles, relevant variables are `LOCAL_USES_LIBRARIES` and
  169. // `LOCAL_OPTIONAL_USES_LIBRARIES`. It is preferable to avoid specifying these properties explicilty
  170. // when they can be computed automatically by Soong (as the transitive closure of SDK library
  171. // dependencies).
  172. //
  173. // Some of the Java libraries that are used as <uses-library> are not SDK libraries (they are
  174. // defined as `java_library` rather than `java_sdk_library` in the Android.bp files). In order for
  175. // the build system to handle them automatically like SDK libraries, it is possible to set a
  176. // property `provides_uses_lib` or variable `LOCAL_PROVIDES_USES_LIBRARY` on the blueprint/makefile
  177. // module of such library. This property can also be used to specify real library name in cases
  178. // when it differs from the module name.
  179. //
  180. // Because the information from the manifests has to be duplicated in the Android.bp/Android.mk
  181. // files, there is a danger that it may get out of sync. To guard against that, the build system
  182. // generates a rule that checks the metadata in the build files against the contents of a manifest
  183. // (verify_uses_libraries). The manifest can be available as a source file, or as part of a prebuilt
  184. // APK. Note that reading the manifests at the Ninja stage of the build is fine, unlike the build
  185. // rule generation phase.
  186. //
  187. // ClassLoaderContext is a structure that represents CLC.
  188. type ClassLoaderContext struct {
  189. // The name of the library.
  190. Name string
  191. // If the library is optional or required.
  192. Optional bool
  193. // On-host build path to the library dex file (used in dex2oat argument --class-loader-context).
  194. Host android.Path
  195. // On-device install path (used in dex2oat argument --stored-class-loader-context).
  196. Device string
  197. // Nested sub-CLC for dependencies.
  198. Subcontexts []*ClassLoaderContext
  199. }
  200. // excludeLibs excludes the libraries from this ClassLoaderContext.
  201. //
  202. // This treats the supplied context as being immutable (as it may come from a dependency). So, it
  203. // implements copy-on-exclusion logic. That means that if any of the excluded libraries are used
  204. // within this context then this will return a deep copy of this without those libraries.
  205. //
  206. // If this ClassLoaderContext matches one of the libraries to exclude then this returns (nil, true)
  207. // to indicate that this context should be excluded from the containing list.
  208. //
  209. // If any of this ClassLoaderContext's Subcontexts reference the excluded libraries then this
  210. // returns a pointer to a copy of this without the excluded libraries and true to indicate that this
  211. // was copied.
  212. //
  213. // Otherwise, this returns a pointer to this and false to indicate that this was not copied.
  214. func (c *ClassLoaderContext) excludeLibs(excludedLibs []string) (*ClassLoaderContext, bool) {
  215. if android.InList(c.Name, excludedLibs) {
  216. return nil, true
  217. }
  218. if excludedList, modified := excludeLibsFromCLCList(c.Subcontexts, excludedLibs); modified {
  219. clcCopy := *c
  220. clcCopy.Subcontexts = excludedList
  221. return &clcCopy, true
  222. }
  223. return c, false
  224. }
  225. // ClassLoaderContextMap is a map from SDK version to CLC. There is a special entry with key
  226. // AnySdkVersion that stores unconditional CLC that is added regardless of the target SDK version.
  227. //
  228. // Conditional CLC is for compatibility libraries which didn't exist prior to a certain SDK version
  229. // (say, N), but classes in them were in the bootclasspath jars, etc., and in version N they have
  230. // been separated into a standalone <uses-library>. Compatibility libraries should only be in the
  231. // CLC if the library/app that uses them has `targetSdkVersion` less than N in the manifest.
  232. //
  233. // Currently only apps (but not libraries) use conditional CLC.
  234. //
  235. // Target SDK version information is unavailable to the build system at rule generation time, so
  236. // the build system doesn't know whether conditional CLC is needed for a given app or not. So it
  237. // generates a build rule that includes conditional CLC for all versions, extracts the target SDK
  238. // version from the manifest, and filters the CLCs based on that version. Exact final CLC that is
  239. // passed to dex2oat is unknown to the build system, and gets known only at Ninja stage.
  240. type ClassLoaderContextMap map[int][]*ClassLoaderContext
  241. // Compatibility libraries. Some are optional, and some are required: this is the default that
  242. // affects how they are handled by the Soong logic that automatically adds implicit SDK libraries
  243. // to the manifest_fixer, but an explicit `uses_libs`/`optional_uses_libs` can override this.
  244. var OrgApacheHttpLegacy = "org.apache.http.legacy"
  245. var AndroidTestBase = "android.test.base"
  246. var AndroidTestMock = "android.test.mock"
  247. var AndroidHidlBase = "android.hidl.base-V1.0-java"
  248. var AndroidHidlManager = "android.hidl.manager-V1.0-java"
  249. // Compatibility libraries grouped by version/optionality (for convenience, to avoid repeating the
  250. // same lists in multiple places).
  251. var OptionalCompatUsesLibs28 = []string{
  252. OrgApacheHttpLegacy,
  253. }
  254. var OptionalCompatUsesLibs30 = []string{
  255. AndroidTestBase,
  256. AndroidTestMock,
  257. }
  258. var CompatUsesLibs29 = []string{
  259. AndroidHidlManager,
  260. AndroidHidlBase,
  261. }
  262. var OptionalCompatUsesLibs = append(android.CopyOf(OptionalCompatUsesLibs28), OptionalCompatUsesLibs30...)
  263. var CompatUsesLibs = android.CopyOf(CompatUsesLibs29)
  264. const UnknownInstallLibraryPath = "error"
  265. // AnySdkVersion means that the class loader context is needed regardless of the targetSdkVersion
  266. // of the app. The numeric value affects the key order in the map and, as a result, the order of
  267. // arguments passed to construct_context.py (high value means that the unconditional context goes
  268. // last). We use the converntional "current" SDK level (10000), but any big number would do as well.
  269. const AnySdkVersion int = android.FutureApiLevelInt
  270. // Add class loader context for the given library to the map entry for the given SDK version.
  271. func (clcMap ClassLoaderContextMap) addContext(ctx android.ModuleInstallPathContext, sdkVer int, lib string,
  272. optional bool, hostPath, installPath android.Path, nestedClcMap ClassLoaderContextMap) error {
  273. // For prebuilts, library should have the same name as the source module.
  274. lib = android.RemoveOptionalPrebuiltPrefix(lib)
  275. devicePath := UnknownInstallLibraryPath
  276. if installPath == nil {
  277. if android.InList(lib, CompatUsesLibs) || android.InList(lib, OptionalCompatUsesLibs) {
  278. // Assume that compatibility libraries are installed in /system/framework.
  279. installPath = android.PathForModuleInstall(ctx, "framework", lib+".jar")
  280. } else {
  281. // For some stub libraries the only known thing is the name of their implementation
  282. // library, but the library itself is unavailable (missing or part of a prebuilt). In
  283. // such cases we still need to add the library to <uses-library> tags in the manifest,
  284. // but we cannot use it for dexpreopt.
  285. }
  286. }
  287. if installPath != nil {
  288. devicePath = android.InstallPathToOnDevicePath(ctx, installPath.(android.InstallPath))
  289. }
  290. // Nested class loader context shouldn't have conditional part (it is allowed only at the top level).
  291. for ver, _ := range nestedClcMap {
  292. if ver != AnySdkVersion {
  293. clcStr, _ := ComputeClassLoaderContext(nestedClcMap)
  294. return fmt.Errorf("nested class loader context shouldn't have conditional part: %s", clcStr)
  295. }
  296. }
  297. subcontexts := nestedClcMap[AnySdkVersion]
  298. // Check if the library with this name is already present in unconditional top-level CLC.
  299. for _, clc := range clcMap[sdkVer] {
  300. if clc.Name != lib {
  301. // Ok, a different library.
  302. } else if clc.Host == hostPath && clc.Device == devicePath {
  303. // Ok, the same library with the same paths. Don't re-add it, but don't raise an error
  304. // either, as the same library may be reachable via different transitional dependencies.
  305. return nil
  306. } else {
  307. // Fail, as someone is trying to add the same library with different paths. This likely
  308. // indicates an error somewhere else, like trying to add a stub library.
  309. return fmt.Errorf("a <uses-library> named %q is already in class loader context,"+
  310. "but the library paths are different:\t\n", lib)
  311. }
  312. }
  313. clcMap[sdkVer] = append(clcMap[sdkVer], &ClassLoaderContext{
  314. Name: lib,
  315. Optional: optional,
  316. Host: hostPath,
  317. Device: devicePath,
  318. Subcontexts: subcontexts,
  319. })
  320. return nil
  321. }
  322. // Add class loader context for the given SDK version. Don't fail on unknown build/install paths, as
  323. // libraries with unknown paths still need to be processed by manifest_fixer (which doesn't care
  324. // about paths). For the subset of libraries that are used in dexpreopt, their build/install paths
  325. // are validated later before CLC is used (in validateClassLoaderContext).
  326. func (clcMap ClassLoaderContextMap) AddContext(ctx android.ModuleInstallPathContext, sdkVer int,
  327. lib string, optional bool, hostPath, installPath android.Path, nestedClcMap ClassLoaderContextMap) {
  328. err := clcMap.addContext(ctx, sdkVer, lib, optional, hostPath, installPath, nestedClcMap)
  329. if err != nil {
  330. ctx.ModuleErrorf(err.Error())
  331. }
  332. }
  333. // Merge the other class loader context map into this one, do not override existing entries.
  334. // The implicitRootLib parameter is the name of the library for which the other class loader
  335. // context map was constructed. If the implicitRootLib is itself a <uses-library>, it should be
  336. // already present in the class loader context (with the other context as its subcontext) -- in
  337. // that case do not re-add the other context. Otherwise add the other context at the top-level.
  338. func (clcMap ClassLoaderContextMap) AddContextMap(otherClcMap ClassLoaderContextMap, implicitRootLib string) {
  339. if otherClcMap == nil {
  340. return
  341. }
  342. // If the implicit root of the merged map is already present as one of top-level subtrees, do
  343. // not merge it second time.
  344. for _, clc := range clcMap[AnySdkVersion] {
  345. if clc.Name == implicitRootLib {
  346. return
  347. }
  348. }
  349. for sdkVer, otherClcs := range otherClcMap {
  350. for _, otherClc := range otherClcs {
  351. alreadyHave := false
  352. for _, clc := range clcMap[sdkVer] {
  353. if clc.Name == otherClc.Name {
  354. alreadyHave = true
  355. break
  356. }
  357. }
  358. if !alreadyHave {
  359. clcMap[sdkVer] = append(clcMap[sdkVer], otherClc)
  360. }
  361. }
  362. }
  363. }
  364. // Returns top-level libraries in the CLC (conditional CLC, i.e. compatibility libraries are not
  365. // included). This is the list of libraries that should be in the <uses-library> tags in the
  366. // manifest. Some of them may be present in the source manifest, others are added by manifest_fixer.
  367. // Required and optional libraries are in separate lists.
  368. func (clcMap ClassLoaderContextMap) UsesLibs() (required []string, optional []string) {
  369. if clcMap != nil {
  370. clcs := clcMap[AnySdkVersion]
  371. required = make([]string, 0, len(clcs))
  372. optional = make([]string, 0, len(clcs))
  373. for _, clc := range clcs {
  374. if clc.Optional {
  375. optional = append(optional, clc.Name)
  376. } else {
  377. required = append(required, clc.Name)
  378. }
  379. }
  380. }
  381. return required, optional
  382. }
  383. func (clcMap ClassLoaderContextMap) Dump() string {
  384. jsonCLC := toJsonClassLoaderContext(clcMap)
  385. bytes, err := json.MarshalIndent(jsonCLC, "", " ")
  386. if err != nil {
  387. panic(err)
  388. }
  389. return string(bytes)
  390. }
  391. // excludeLibsFromCLCList excludes the libraries from the ClassLoaderContext in this list.
  392. //
  393. // This treats the supplied list as being immutable (as it may come from a dependency). So, it
  394. // implements copy-on-exclusion logic. That means that if any of the excluded libraries are used
  395. // within the contexts in the list then this will return a deep copy of the list without those
  396. // libraries.
  397. //
  398. // If any of the ClassLoaderContext in the list reference the excluded libraries then this returns a
  399. // copy of this list without the excluded libraries and true to indicate that this was copied.
  400. //
  401. // Otherwise, this returns the list and false to indicate that this was not copied.
  402. func excludeLibsFromCLCList(clcList []*ClassLoaderContext, excludedLibs []string) ([]*ClassLoaderContext, bool) {
  403. modifiedList := false
  404. copiedList := make([]*ClassLoaderContext, 0, len(clcList))
  405. for _, clc := range clcList {
  406. resultClc, modifiedClc := clc.excludeLibs(excludedLibs)
  407. if resultClc != nil {
  408. copiedList = append(copiedList, resultClc)
  409. }
  410. modifiedList = modifiedList || modifiedClc
  411. }
  412. if modifiedList {
  413. return copiedList, true
  414. } else {
  415. return clcList, false
  416. }
  417. }
  418. // ExcludeLibs excludes the libraries from the ClassLoaderContextMap.
  419. //
  420. // If the list o libraries is empty then this returns the ClassLoaderContextMap.
  421. //
  422. // This treats the ClassLoaderContextMap as being immutable (as it may come from a dependency). So,
  423. // it implements copy-on-exclusion logic. That means that if any of the excluded libraries are used
  424. // within the contexts in the map then this will return a deep copy of the map without those
  425. // libraries.
  426. //
  427. // Otherwise, this returns the map unchanged.
  428. func (clcMap ClassLoaderContextMap) ExcludeLibs(excludedLibs []string) ClassLoaderContextMap {
  429. if len(excludedLibs) == 0 {
  430. return clcMap
  431. }
  432. excludedClcMap := make(ClassLoaderContextMap)
  433. modifiedMap := false
  434. for sdkVersion, clcList := range clcMap {
  435. excludedList, modifiedList := excludeLibsFromCLCList(clcList, excludedLibs)
  436. if len(excludedList) != 0 {
  437. excludedClcMap[sdkVersion] = excludedList
  438. }
  439. modifiedMap = modifiedMap || modifiedList
  440. }
  441. if modifiedMap {
  442. return excludedClcMap
  443. } else {
  444. return clcMap
  445. }
  446. }
  447. // Now that the full unconditional context is known, reconstruct conditional context.
  448. // Apply filters for individual libraries, mirroring what the PackageManager does when it
  449. // constructs class loader context on device.
  450. //
  451. // TODO(b/132357300): remove "android.hidl.manager" and "android.hidl.base" for non-system apps.
  452. func fixClassLoaderContext(clcMap ClassLoaderContextMap) {
  453. required, optional := clcMap.UsesLibs()
  454. usesLibs := append(required, optional...)
  455. for sdkVer, clcs := range clcMap {
  456. if sdkVer == AnySdkVersion {
  457. continue
  458. }
  459. fixedClcs := []*ClassLoaderContext{}
  460. for _, clc := range clcs {
  461. if android.InList(clc.Name, usesLibs) {
  462. // skip compatibility libraries that are already included in unconditional context
  463. } else if clc.Name == AndroidTestMock && !android.InList("android.test.runner", usesLibs) {
  464. // android.test.mock is only needed as a compatibility library (in conditional class
  465. // loader context) if android.test.runner is used, otherwise skip it
  466. } else {
  467. fixedClcs = append(fixedClcs, clc)
  468. }
  469. clcMap[sdkVer] = fixedClcs
  470. }
  471. }
  472. }
  473. // Return true if all build/install library paths are valid (including recursive subcontexts),
  474. // otherwise return false. A build path is valid if it's not nil. An install path is valid if it's
  475. // not equal to a special "error" value.
  476. func validateClassLoaderContext(clcMap ClassLoaderContextMap) (bool, error) {
  477. for sdkVer, clcs := range clcMap {
  478. if valid, err := validateClassLoaderContextRec(sdkVer, clcs); !valid || err != nil {
  479. return valid, err
  480. }
  481. }
  482. return true, nil
  483. }
  484. // Helper function for validateClassLoaderContext() that handles recursion.
  485. func validateClassLoaderContextRec(sdkVer int, clcs []*ClassLoaderContext) (bool, error) {
  486. for _, clc := range clcs {
  487. if clc.Host == nil || clc.Device == UnknownInstallLibraryPath {
  488. if sdkVer == AnySdkVersion {
  489. // Return error if dexpreopt doesn't know paths to one of the <uses-library>
  490. // dependencies. In the future we may need to relax this and just disable dexpreopt.
  491. if clc.Host == nil {
  492. return false, fmt.Errorf("invalid build path for <uses-library> \"%s\"", clc.Name)
  493. } else {
  494. return false, fmt.Errorf("invalid install path for <uses-library> \"%s\"", clc.Name)
  495. }
  496. } else {
  497. // No error for compatibility libraries, as Soong doesn't know if they are needed
  498. // (this depends on the targetSdkVersion in the manifest), but the CLC is invalid.
  499. return false, nil
  500. }
  501. }
  502. if valid, err := validateClassLoaderContextRec(sdkVer, clc.Subcontexts); !valid || err != nil {
  503. return valid, err
  504. }
  505. }
  506. return true, nil
  507. }
  508. // Return the class loader context as a string, and a slice of build paths for all dependencies.
  509. // Perform a depth-first preorder traversal of the class loader context tree for each SDK version.
  510. // Return the resulting string and a slice of on-host build paths to all library dependencies.
  511. func ComputeClassLoaderContext(clcMap ClassLoaderContextMap) (clcStr string, paths android.Paths) {
  512. // CLC for different SDK versions should come in specific order that agrees with PackageManager.
  513. // Since PackageManager processes SDK versions in ascending order and prepends compatibility
  514. // libraries at the front, the required order is descending, except for AnySdkVersion that has
  515. // numerically the largest order, but must be the last one. Example of correct order: [30, 29,
  516. // 28, AnySdkVersion]. There are Soong tests to ensure that someone doesn't change this by
  517. // accident, but there is no way to guard against changes in the PackageManager, except for
  518. // grepping logcat on the first boot for absence of the following messages:
  519. //
  520. // `logcat | grep -E 'ClassLoaderContext [a-z ]+ mismatch`
  521. //
  522. versions := make([]int, 0, len(clcMap))
  523. for ver, _ := range clcMap {
  524. if ver != AnySdkVersion {
  525. versions = append(versions, ver)
  526. }
  527. }
  528. sort.Sort(sort.Reverse(sort.IntSlice(versions))) // descending order
  529. versions = append(versions, AnySdkVersion)
  530. for _, sdkVer := range versions {
  531. sdkVerStr := fmt.Sprintf("%d", sdkVer)
  532. if sdkVer == AnySdkVersion {
  533. sdkVerStr = "any" // a special keyword that means any SDK version
  534. }
  535. hostClc, targetClc, hostPaths := computeClassLoaderContextRec(clcMap[sdkVer])
  536. if hostPaths != nil {
  537. clcStr += fmt.Sprintf(" --host-context-for-sdk %s %s", sdkVerStr, hostClc)
  538. clcStr += fmt.Sprintf(" --target-context-for-sdk %s %s", sdkVerStr, targetClc)
  539. }
  540. paths = append(paths, hostPaths...)
  541. }
  542. return clcStr, android.FirstUniquePaths(paths)
  543. }
  544. // Helper function for ComputeClassLoaderContext() that handles recursion.
  545. func computeClassLoaderContextRec(clcs []*ClassLoaderContext) (string, string, android.Paths) {
  546. var paths android.Paths
  547. var clcsHost, clcsTarget []string
  548. for _, clc := range clcs {
  549. subClcHost, subClcTarget, subPaths := computeClassLoaderContextRec(clc.Subcontexts)
  550. if subPaths != nil {
  551. subClcHost = "{" + subClcHost + "}"
  552. subClcTarget = "{" + subClcTarget + "}"
  553. }
  554. clcsHost = append(clcsHost, "PCL["+clc.Host.String()+"]"+subClcHost)
  555. clcsTarget = append(clcsTarget, "PCL["+clc.Device+"]"+subClcTarget)
  556. paths = append(paths, clc.Host)
  557. paths = append(paths, subPaths...)
  558. }
  559. clcHost := strings.Join(clcsHost, "#")
  560. clcTarget := strings.Join(clcsTarget, "#")
  561. return clcHost, clcTarget, paths
  562. }
  563. // Class loader contexts that come from Make via JSON dexpreopt.config. JSON CLC representation is
  564. // the same as Soong representation except that SDK versions and paths are represented with strings.
  565. type jsonClassLoaderContext struct {
  566. Name string
  567. Optional bool
  568. Host string
  569. Device string
  570. Subcontexts []*jsonClassLoaderContext
  571. }
  572. // A map from SDK version (represented with a JSON string) to JSON CLCs.
  573. type jsonClassLoaderContextMap map[string][]*jsonClassLoaderContext
  574. // Convert JSON CLC map to Soong represenation.
  575. func fromJsonClassLoaderContext(ctx android.PathContext, jClcMap jsonClassLoaderContextMap) ClassLoaderContextMap {
  576. clcMap := make(ClassLoaderContextMap)
  577. for sdkVerStr, clcs := range jClcMap {
  578. sdkVer, ok := strconv.Atoi(sdkVerStr)
  579. if ok != nil {
  580. if sdkVerStr == "any" {
  581. sdkVer = AnySdkVersion
  582. } else {
  583. android.ReportPathErrorf(ctx, "failed to parse SDK version in dexpreopt.config: '%s'", sdkVerStr)
  584. }
  585. }
  586. clcMap[sdkVer] = fromJsonClassLoaderContextRec(ctx, clcs)
  587. }
  588. return clcMap
  589. }
  590. // Recursive helper for fromJsonClassLoaderContext.
  591. func fromJsonClassLoaderContextRec(ctx android.PathContext, jClcs []*jsonClassLoaderContext) []*ClassLoaderContext {
  592. clcs := make([]*ClassLoaderContext, 0, len(jClcs))
  593. for _, clc := range jClcs {
  594. clcs = append(clcs, &ClassLoaderContext{
  595. Name: clc.Name,
  596. Optional: clc.Optional,
  597. Host: constructPath(ctx, clc.Host),
  598. Device: clc.Device,
  599. Subcontexts: fromJsonClassLoaderContextRec(ctx, clc.Subcontexts),
  600. })
  601. }
  602. return clcs
  603. }
  604. // Convert Soong CLC map to JSON representation for Make.
  605. func toJsonClassLoaderContext(clcMap ClassLoaderContextMap) jsonClassLoaderContextMap {
  606. jClcMap := make(jsonClassLoaderContextMap)
  607. for sdkVer, clcs := range clcMap {
  608. sdkVerStr := fmt.Sprintf("%d", sdkVer)
  609. if sdkVer == AnySdkVersion {
  610. sdkVerStr = "any"
  611. }
  612. jClcMap[sdkVerStr] = toJsonClassLoaderContextRec(clcs)
  613. }
  614. return jClcMap
  615. }
  616. // Recursive helper for toJsonClassLoaderContext.
  617. func toJsonClassLoaderContextRec(clcs []*ClassLoaderContext) []*jsonClassLoaderContext {
  618. jClcs := make([]*jsonClassLoaderContext, len(clcs))
  619. for i, clc := range clcs {
  620. var host string
  621. if clc.Host == nil {
  622. // Defer build failure to when this CLC is actually used.
  623. host = fmt.Sprintf("implementation-jar-for-%s-is-not-available.jar", clc.Name)
  624. } else {
  625. host = clc.Host.String()
  626. }
  627. jClcs[i] = &jsonClassLoaderContext{
  628. Name: clc.Name,
  629. Optional: clc.Optional,
  630. Host: host,
  631. Device: clc.Device,
  632. Subcontexts: toJsonClassLoaderContextRec(clc.Subcontexts),
  633. }
  634. }
  635. return jClcs
  636. }