hiddenapi.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // Copyright 2019 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 java
  15. import (
  16. "github.com/google/blueprint"
  17. "android/soong/android"
  18. )
  19. var (
  20. hiddenAPIGenerateCSVRule = pctx.AndroidStaticRule("hiddenAPIGenerateCSV", blueprint.RuleParams{
  21. Command: "${config.Class2NonSdkList} --stub-api-flags ${stubAPIFlags} $in $outFlag $out",
  22. CommandDeps: []string{"${config.Class2NonSdkList}"},
  23. }, "outFlag", "stubAPIFlags")
  24. hiddenAPIGenerateIndexRule = pctx.AndroidStaticRule("hiddenAPIGenerateIndex", blueprint.RuleParams{
  25. Command: "${config.MergeCsvCommand} --zip_input --key_field signature --output=$out $in",
  26. CommandDeps: []string{"${config.MergeCsvCommand}"},
  27. })
  28. )
  29. type hiddenAPI struct {
  30. // True if the module containing this structure contributes to the hiddenapi information or has
  31. // that information encoded within it.
  32. active bool
  33. // The path to the dex jar that is in the boot class path. If this is unset then the associated
  34. // module is not a boot jar, but could be one of the <x>-hiddenapi modules that provide additional
  35. // annotations for the <x> boot dex jar but which do not actually provide a boot dex jar
  36. // themselves.
  37. //
  38. // This must be the path to the unencoded dex jar as the encoded dex jar indirectly depends on
  39. // this file so using the encoded dex jar here would result in a cycle in the ninja rules.
  40. bootDexJarPath OptionalDexJarPath
  41. // The paths to the classes jars that contain classes and class members annotated with
  42. // the UnsupportedAppUsage annotation that need to be extracted as part of the hidden API
  43. // processing.
  44. classesJarPaths android.Paths
  45. // The compressed state of the dex file being encoded. This is used to ensure that the encoded
  46. // dex file has the same state.
  47. uncompressDexState *bool
  48. }
  49. func (h *hiddenAPI) bootDexJar() OptionalDexJarPath {
  50. return h.bootDexJarPath
  51. }
  52. func (h *hiddenAPI) classesJars() android.Paths {
  53. return h.classesJarPaths
  54. }
  55. func (h *hiddenAPI) uncompressDex() *bool {
  56. return h.uncompressDexState
  57. }
  58. // hiddenAPIModule is the interface a module that embeds the hiddenAPI structure must implement.
  59. type hiddenAPIModule interface {
  60. android.Module
  61. hiddenAPIIntf
  62. MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel
  63. }
  64. type hiddenAPIIntf interface {
  65. bootDexJar() OptionalDexJarPath
  66. classesJars() android.Paths
  67. uncompressDex() *bool
  68. }
  69. var _ hiddenAPIIntf = (*hiddenAPI)(nil)
  70. // Initialize the hiddenapi structure
  71. //
  72. // uncompressedDexState should be nil when the module is a prebuilt and so does not require hidden
  73. // API encoding.
  74. func (h *hiddenAPI) initHiddenAPI(ctx android.ModuleContext, dexJar OptionalDexJarPath, classesJar android.Path, uncompressedDexState *bool) {
  75. // Save the classes jars even if this is not active as they may be used by modular hidden API
  76. // processing.
  77. classesJars := android.Paths{classesJar}
  78. ctx.VisitDirectDepsWithTag(hiddenApiAnnotationsTag, func(dep android.Module) {
  79. javaInfo := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
  80. classesJars = append(classesJars, javaInfo.ImplementationJars...)
  81. })
  82. h.classesJarPaths = classesJars
  83. // Save the unencoded dex jar so it can be used when generating the
  84. // hiddenAPISingletonPathsStruct.stubFlags file.
  85. h.bootDexJarPath = dexJar
  86. h.uncompressDexState = uncompressedDexState
  87. // If hiddenapi processing is disabled treat this as inactive.
  88. if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
  89. return
  90. }
  91. // The context module must implement hiddenAPIModule.
  92. module := ctx.Module().(hiddenAPIModule)
  93. // If the frameworks/base directories does not exist and no prebuilt hidden API flag files have
  94. // been configured then it is not possible to do hidden API encoding.
  95. if !ctx.Config().FrameworksBaseDirExists(ctx) && ctx.Config().PrebuiltHiddenApiDir(ctx) == "" {
  96. return
  97. }
  98. // It is important that hiddenapi information is only gathered for/from modules that are actually
  99. // on the boot jars list because the runtime only enforces access to the hidden API for the
  100. // bootclassloader. If information is gathered for modules not on the list then that will cause
  101. // failures in the CtsHiddenApiBlocklist... tests.
  102. h.active = isModuleInBootClassPath(ctx, module)
  103. }
  104. func isModuleInBootClassPath(ctx android.BaseModuleContext, module android.Module) bool {
  105. // Get the configured platform and apex boot jars.
  106. nonApexBootJars := ctx.Config().NonApexBootJars()
  107. apexBootJars := ctx.Config().ApexBootJars()
  108. active := isModuleInConfiguredList(ctx, module, nonApexBootJars) ||
  109. isModuleInConfiguredList(ctx, module, apexBootJars)
  110. return active
  111. }
  112. // hiddenAPIEncodeDex is called by any module that needs to encode dex files.
  113. //
  114. // It ignores any module that has not had initHiddenApi() called on it and which is not in the boot
  115. // jar list. In that case it simply returns the supplied dex jar path.
  116. //
  117. // Otherwise, it creates a copy of the supplied dex file into which it has encoded the hiddenapi
  118. // flags and returns this instead of the supplied dex jar.
  119. func (h *hiddenAPI) hiddenAPIEncodeDex(ctx android.ModuleContext, dexJar android.OutputPath) android.OutputPath {
  120. if !h.active {
  121. return dexJar
  122. }
  123. // A nil uncompressDexState prevents the dex file from being encoded.
  124. if h.uncompressDexState == nil {
  125. ctx.ModuleErrorf("cannot encode dex file %s when uncompressDexState is nil", dexJar)
  126. }
  127. uncompressDex := *h.uncompressDexState
  128. // Create a copy of the dex jar which has been encoded with hiddenapi flags.
  129. flagsCSV := hiddenAPISingletonPaths(ctx).flags
  130. outputDir := android.PathForModuleOut(ctx, "hiddenapi").OutputPath
  131. encodedDex := hiddenAPIEncodeDex(ctx, dexJar, flagsCSV, uncompressDex, android.NoneApiLevel, outputDir)
  132. // Use the encoded dex jar from here onwards.
  133. return encodedDex
  134. }
  135. // buildRuleToGenerateAnnotationFlags builds a ninja rule to generate the annotation-flags.csv file
  136. // from the classes jars and stub-flags.csv files.
  137. //
  138. // The annotation-flags.csv file contains mappings from Java signature to various flags derived from
  139. // annotations in the source, e.g. whether it is public or the sdk version above which it can no
  140. // longer be used.
  141. //
  142. // It is created by the Class2NonSdkList tool which processes the .class files in the class
  143. // implementation jar looking for UnsupportedAppUsage and CovariantReturnType annotations. The
  144. // tool also consumes the hiddenAPISingletonPathsStruct.stubFlags file in order to perform
  145. // consistency checks on the information in the annotations and to filter out bridge methods
  146. // that are already part of the public API.
  147. func buildRuleToGenerateAnnotationFlags(ctx android.ModuleContext, desc string, classesJars android.Paths, stubFlagsCSV android.Path, outputPath android.WritablePath) {
  148. ctx.Build(pctx, android.BuildParams{
  149. Rule: hiddenAPIGenerateCSVRule,
  150. Description: desc,
  151. Inputs: classesJars,
  152. Output: outputPath,
  153. Implicit: stubFlagsCSV,
  154. Args: map[string]string{
  155. "outFlag": "--write-flags-csv",
  156. "stubAPIFlags": stubFlagsCSV.String(),
  157. },
  158. })
  159. }
  160. // buildRuleToGenerateMetadata builds a ninja rule to generate the metadata.csv file from
  161. // the classes jars and stub-flags.csv files.
  162. //
  163. // The metadata.csv file contains mappings from Java signature to the value of properties specified
  164. // on UnsupportedAppUsage annotations in the source.
  165. //
  166. // Like the annotation-flags.csv file this is also created by the Class2NonSdkList in the same way.
  167. // Although the two files could potentially be created in a single invocation of the
  168. // Class2NonSdkList at the moment they are created using their own invocation, with the behavior
  169. // being determined by the property that is used.
  170. func buildRuleToGenerateMetadata(ctx android.ModuleContext, desc string, classesJars android.Paths, stubFlagsCSV android.Path, metadataCSV android.WritablePath) {
  171. ctx.Build(pctx, android.BuildParams{
  172. Rule: hiddenAPIGenerateCSVRule,
  173. Description: desc,
  174. Inputs: classesJars,
  175. Output: metadataCSV,
  176. Implicit: stubFlagsCSV,
  177. Args: map[string]string{
  178. "outFlag": "--write-metadata-csv",
  179. "stubAPIFlags": stubFlagsCSV.String(),
  180. },
  181. })
  182. }
  183. // buildRuleToGenerateIndex builds a ninja rule to generate the index.csv file from the classes
  184. // jars.
  185. //
  186. // The index.csv file contains mappings from Java signature to source location information.
  187. //
  188. // It is created by the merge_csv tool which processes the class implementation jar, extracting
  189. // all the files ending in .uau (which are CSV files) and merges them together. The .uau files are
  190. // created by the unsupported app usage annotation processor during compilation of the class
  191. // implementation jar.
  192. func buildRuleToGenerateIndex(ctx android.ModuleContext, desc string, classesJars android.Paths, indexCSV android.WritablePath) {
  193. ctx.Build(pctx, android.BuildParams{
  194. Rule: hiddenAPIGenerateIndexRule,
  195. Description: desc,
  196. Inputs: classesJars,
  197. Output: indexCSV,
  198. })
  199. }
  200. var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
  201. Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output &&
  202. unzip -qoDD $in 'classes*.dex' -d $tmpDir/dex-input &&
  203. for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do
  204. echo "--input-dex=$${INPUT_DEX}";
  205. echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})";
  206. done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags &&
  207. ${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" &&
  208. ${config.MergeZipsCmd} -j -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" -stripFile "**/*.uau" $out $tmpDir/dex.jar $in`,
  209. CommandDeps: []string{
  210. "${config.HiddenAPI}",
  211. "${config.SoongZipCmd}",
  212. "${config.MergeZipsCmd}",
  213. },
  214. }, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
  215. // hiddenAPIEncodeDex generates the build rule that will encode the supplied dex jar and place the
  216. // encoded dex jar in a file of the same name in the output directory.
  217. //
  218. // The encode dex rule requires unzipping, encoding and rezipping the classes.dex files along with
  219. // all the resources from the input jar. It also ensures that if it was uncompressed in the input
  220. // it stays uncompressed in the output.
  221. func hiddenAPIEncodeDex(ctx android.ModuleContext, dexInput, flagsCSV android.Path, uncompressDex bool, minSdkVersion android.ApiLevel, outputDir android.OutputPath) android.OutputPath {
  222. // The output file has the same name as the input file and is in the output directory.
  223. output := outputDir.Join(ctx, dexInput.Base())
  224. // Create a jar specific temporary directory in which to do the work just in case this is called
  225. // with the same output directory for multiple modules.
  226. tmpDir := outputDir.Join(ctx, dexInput.Base()+"-tmp")
  227. // If the input is uncompressed then generate the output of the encode rule to an intermediate
  228. // file as the final output will need further processing after encoding.
  229. soongZipFlags := ""
  230. encodeRuleOutput := output
  231. if uncompressDex {
  232. soongZipFlags = "-L 0"
  233. encodeRuleOutput = outputDir.Join(ctx, "unaligned", dexInput.Base())
  234. }
  235. // b/149353192: when a module is instrumented, jacoco adds synthetic members
  236. // $jacocoData and $jacocoInit. Since they don't exist when building the hidden API flags,
  237. // don't complain when we don't find hidden API flags for the synthetic members.
  238. hiddenapiFlags := ""
  239. if j, ok := ctx.Module().(interface {
  240. shouldInstrument(android.BaseModuleContext) bool
  241. }); ok && j.shouldInstrument(ctx) {
  242. hiddenapiFlags = "--no-force-assign-all"
  243. }
  244. // If the library is targeted for Q and/or R then make sure that they do not
  245. // have any S+ flags encoded as that will break the runtime.
  246. minApiLevel := minSdkVersion
  247. if !minApiLevel.IsNone() {
  248. if minApiLevel.LessThanOrEqualTo(android.ApiLevelOrPanic(ctx, "R")) {
  249. hiddenapiFlags = hiddenapiFlags + " --max-hiddenapi-level=max-target-r"
  250. }
  251. }
  252. ctx.Build(pctx, android.BuildParams{
  253. Rule: hiddenAPIEncodeDexRule,
  254. Description: "hiddenapi encode dex",
  255. Input: dexInput,
  256. Output: encodeRuleOutput,
  257. Implicit: flagsCSV,
  258. Args: map[string]string{
  259. "flagsCsv": flagsCSV.String(),
  260. "tmpDir": tmpDir.String(),
  261. "soongZipFlags": soongZipFlags,
  262. "hiddenapiFlags": hiddenapiFlags,
  263. },
  264. })
  265. if uncompressDex {
  266. TransformZipAlign(ctx, output, encodeRuleOutput)
  267. }
  268. return output
  269. }
  270. type hiddenApiAnnotationsDependencyTag struct {
  271. blueprint.BaseDependencyTag
  272. android.LicenseAnnotationSharedDependencyTag
  273. }
  274. // Tag used to mark dependencies on java_library instances that contains Java source files whose
  275. // sole purpose is to provide additional hiddenapi annotations.
  276. var hiddenApiAnnotationsTag hiddenApiAnnotationsDependencyTag
  277. // Mark this tag so dependencies that use it are excluded from APEX contents.
  278. func (t hiddenApiAnnotationsDependencyTag) ExcludeFromApexContents() {}
  279. var _ android.ExcludeFromApexContentsTag = hiddenApiAnnotationsTag