1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // This namespace is exclusively for vndk-sp libs.
18 
19 #include "linkerconfig/environment.h"
20 #include "linkerconfig/namespacebuilder.h"
21 
22 using android::linkerconfig::modules::Namespace;
23 
24 namespace android {
25 namespace linkerconfig {
26 namespace contents {
BuildVndkNamespace(const Context & ctx,VndkUserPartition vndk_user)27 Namespace BuildVndkNamespace([[maybe_unused]] const Context& ctx,
28                              VndkUserPartition vndk_user) {
29   bool is_system_or_unrestricted_section = ctx.IsSystemSection() ||
30                                            ctx.IsUnrestrictedSection();
31   if (ctx.IsApexBinaryConfig()) {
32     is_system_or_unrestricted_section = ctx.GetCurrentApex().InSystem();
33   }
34   // In the system section, we need to have an additional vndk namespace for
35   // product apps. We must have a different name "vndk_product" for this
36   // namespace. "vndk_product" namespace is used only from the native_loader for
37   // product apps.
38   const char* name;
39   if (is_system_or_unrestricted_section &&
40       vndk_user == VndkUserPartition::Product) {
41     name = "vndk_product";
42   } else {
43     name = "vndk";
44   }
45 
46   // Isolated but visible when used in the [system] or [unrestricted] section to
47   // allow links to be created at runtime, e.g. through android_link_namespaces
48   // in libnativeloader. Otherwise it isn't isolated, so visibility doesn't
49   // matter.
50   Namespace ns(name,
51                /*is_isolated=*/ctx.IsSystemSection() || ctx.IsApexBinaryConfig(),
52                /*is_visible=*/is_system_or_unrestricted_section);
53 
54   std::vector<std::string> lib_paths;
55   std::string vndk_version;
56   if (vndk_user == VndkUserPartition::Product) {
57     lib_paths = {Var("PRODUCT") + "/${LIB}"};
58     vndk_version = Var("PRODUCT_VNDK_VERSION");
59   } else {
60     // default for vendor
61     lib_paths = {"/odm/${LIB}", "/vendor/${LIB}"};
62     vndk_version = Var("VENDOR_VNDK_VERSION");
63   }
64 
65   // Search order:
66   // 1. VNDK Extensions
67   // 2. VNDK APEX
68   // 3. vendor/lib or product/lib to allow extensions to use them
69 
70   // 1. VNDK Extensions
71   for (const auto& lib_path : lib_paths) {
72     ns.AddSearchPath(lib_path + "/vndk-sp");
73     if (!is_system_or_unrestricted_section) {
74       ns.AddSearchPath(lib_path + "/vndk");
75     }
76   }
77 
78   // 2. VNDK APEX
79   ns.AddSearchPath("/apex/com.android.vndk.v" + vndk_version + "/${LIB}");
80 
81   if (is_system_or_unrestricted_section &&
82       vndk_user == VndkUserPartition::Vendor) {
83     // It is for vendor sp-hal
84     ns.AddPermittedPath("/odm/${LIB}/hw");
85     ns.AddPermittedPath("/odm/${LIB}/egl");
86     ns.AddPermittedPath("/vendor/${LIB}/hw");
87     ns.AddPermittedPath("/vendor/${LIB}/egl");
88     ns.AddPermittedPath("/system/vendor/${LIB}/hw");
89     ns.AddPermittedPath("/system/vendor/${LIB}/egl");
90 
91     // This is exceptionally required since android.hidl.memory@1.0-impl.so is here
92     ns.AddPermittedPath("/apex/com.android.vndk.v" +
93                         Var("VENDOR_VNDK_VERSION") + "/${LIB}/hw");
94   }
95 
96   // 3. vendor/lib or product/lib
97   for (const auto& lib_path : lib_paths) {
98     ns.AddSearchPath(lib_path);
99   }
100 
101   // For the non-system section, the links should be identical to that of the
102   // 'vndk_in_system' namespace, except the links to 'default' and 'vndk_in_system'.
103   if (vndk_user == VndkUserPartition::Product) {
104     ns.GetLink(ctx.GetSystemNamespaceName())
105         .AddSharedLib({Var("LLNDK_LIBRARIES_PRODUCT")});
106   } else {
107     ns.GetLink(ctx.GetSystemNamespaceName())
108         .AddSharedLib({Var("LLNDK_LIBRARIES_VENDOR")});
109   }
110 
111   if (ctx.IsProductSection() || ctx.IsVendorSection()) {
112     if (android::linkerconfig::modules::IsVndkInSystemNamespace()) {
113       ns.GetLink("vndk_in_system")
114           .AddSharedLib(Var("VNDK_USING_CORE_VARIANT_LIBRARIES"));
115     }
116   }
117 
118   ns.AddRequires(std::vector{"libneuralnetworks.so"});
119 
120   return ns;
121 }
122 }  // namespace contents
123 }  // namespace linkerconfig
124 }  // namespace android
125