From b614cd441b355e48e59d1f5cd61a800103404151 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Fri, 11 Oct 2024 12:52:21 -0700 Subject: Verify that libraries in apexes don't link to implementations outside the apex As part of removing some of the complexity in Soong around using stub vs. implementations for shared library dependencies a syntax will be added to Soong to allow explicitly selecting stubs vs. implementation. To avoid incorrect use, add a verification pass on apexes that ensure that all transitive implementation libraries used to link native libraries or binaries in the apex are themselves in the apex. Bug: 372543712 Test: TestApexVerifyNativeImplementationLibs Flag: EXEMPT host only Change-Id: I4aeaca00a359ce97e8f9efd2d8bffb8f9d2dc0df --- java/app.go | 33 ++++++++++++++++++++++++++++----- java/java.go | 11 +++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) (limited to 'java') diff --git a/java/app.go b/java/app.go index fed971a55..15b1114a7 100644 --- a/java/app.go +++ b/java/app.go @@ -67,6 +67,9 @@ type AppInfo struct { // TestHelperApp is true if the module is a android_test_helper_app TestHelperApp bool + + // EmbeddedJNILibs is the list of paths to JNI libraries that were embedded in the APK. + EmbeddedJNILibs android.Paths } var AppInfoProvider = blueprint.NewProvider[*AppInfo]() @@ -405,9 +408,18 @@ func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) { a.checkEmbedJnis(ctx) a.generateAndroidBuildActions(ctx) a.generateJavaUsedByApex(ctx) + + var embeddedJniLibs []android.Path + + if a.embeddedJniLibs { + for _, jni := range a.jniLibs { + embeddedJniLibs = append(embeddedJniLibs, jni.path) + } + } android.SetProvider(ctx, AppInfoProvider, &AppInfo{ - Updatable: Bool(a.appProperties.Updatable), - TestHelperApp: false, + Updatable: Bool(a.appProperties.Updatable), + TestHelperApp: false, + EmbeddedJNILibs: embeddedJniLibs, }) } @@ -1070,12 +1082,12 @@ func collectAppDeps(ctx android.ModuleContext, app appDepsInterface, app.SdkVersion(ctx).Kind != android.SdkCorePlatform && !app.RequiresStableAPIs(ctx) } jniLib, prebuiltJniPackages := collectJniDeps(ctx, shouldCollectRecursiveNativeDeps, - checkNativeSdkVersion, func(dep cc.LinkableInterface) bool { - return !dep.IsNdk(ctx.Config()) && !dep.IsStubs() - }) + checkNativeSdkVersion, func(dep cc.LinkableInterface) bool { return !dep.IsNdk(ctx.Config()) && !dep.IsStubs() }) var certificates []Certificate + var directImplementationDeps android.Paths + var transitiveImplementationDeps []depset.DepSet[android.Path] ctx.VisitDirectDeps(func(module android.Module) { otherName := ctx.OtherModuleName(module) tag := ctx.OtherModuleDependencyTag(module) @@ -1087,7 +1099,18 @@ func collectAppDeps(ctx android.ModuleContext, app appDepsInterface, ctx.ModuleErrorf("certificate dependency %q must be an android_app_certificate module", otherName) } } + + if IsJniDepTag(tag) { + directImplementationDeps = append(directImplementationDeps, android.OutputFileForModule(ctx, module, "")) + if info, ok := android.OtherModuleProvider(ctx, module, cc.ImplementationDepInfoProvider); ok { + transitiveImplementationDeps = append(transitiveImplementationDeps, info.ImplementationDeps) + } + } }) + android.SetProvider(ctx, cc.ImplementationDepInfoProvider, &cc.ImplementationDepInfo{ + ImplementationDeps: depset.New(depset.PREORDER, directImplementationDeps, transitiveImplementationDeps), + }) + return jniLib, prebuiltJniPackages, certificates } diff --git a/java/java.go b/java/java.go index 85024e199..ac4f49619 100644 --- a/java/java.go +++ b/java/java.go @@ -1621,6 +1621,8 @@ func (j *Test) generateAndroidBuildActionsWithConfig(ctx android.ModuleContext, j.data = append(j.data, android.OutputFileForModule(ctx, dep, "")) }) + var directImplementationDeps android.Paths + var transitiveImplementationDeps []depset.DepSet[android.Path] ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) { sharedLibInfo, _ := android.OtherModuleProvider(ctx, dep, cc.SharedLibraryInfoProvider) if sharedLibInfo.SharedLibrary != nil { @@ -1639,11 +1641,20 @@ func (j *Test) generateAndroidBuildActionsWithConfig(ctx android.ModuleContext, Output: relocatedLib, }) j.data = append(j.data, relocatedLib) + + directImplementationDeps = append(directImplementationDeps, android.OutputFileForModule(ctx, dep, "")) + if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok { + transitiveImplementationDeps = append(transitiveImplementationDeps, info.ImplementationDeps) + } } else { ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep)) } }) + android.SetProvider(ctx, cc.ImplementationDepInfoProvider, &cc.ImplementationDepInfo{ + ImplementationDeps: depset.New(depset.PREORDER, directImplementationDeps, transitiveImplementationDeps), + }) + j.Library.GenerateAndroidBuildActions(ctx) } -- cgit v1.2.3-59-g8ed1b