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.IsApexBinaryConfig() ||
31                                            ctx.IsUnrestrictedSection();
32   // In the system section, we need to have an additional vndk namespace for
33   // product apps. We must have a different name "vndk_product" for this
34   // namespace. "vndk_product" namespace is used only from the native_loader for
35   // product apps.
36   const char* name;
37   if (is_system_or_unrestricted_section &&
38       vndk_user == VndkUserPartition::Product) {
39     name = "vndk_product";
40   } else {
41     name = "vndk";
42   }
43 
44   // Isolated but visible when used in the [system] or [unrestricted] section to
45   // allow links to be created at runtime, e.g. through android_link_namespaces
46   // in libnativeloader. Otherwise it isn't isolated, so visibility doesn't
47   // matter.
48   Namespace ns(name,
49                /*is_isolated=*/ctx.IsSystemSection() || ctx.IsApexBinaryConfig(),
50                /*is_visible=*/is_system_or_unrestricted_section);
51 
52   std::vector<std::string> lib_paths;
53   std::vector<std::string> vndk_paths;
54   std::string vndk_version;
55   if (vndk_user == VndkUserPartition::Product) {
56     lib_paths = {"/product/${LIB}/"};
57     vndk_version = Var("PRODUCT_VNDK_VERSION");
58   } else {
59     // default for vendor
60     lib_paths = {"/odm/${LIB}/", "/vendor/${LIB}/"};
61     vndk_version = Var("VENDOR_VNDK_VERSION");
62   }
63 
64   for (const auto& lib_path : lib_paths) {
65     ns.AddSearchPath(lib_path + "vndk-sp");
66     if (!is_system_or_unrestricted_section) {
67       ns.AddSearchPath(lib_path + "vndk");
68     }
69   }
70   ns.AddSearchPath("/apex/com.android.vndk.v" + vndk_version + "/${LIB}");
71 
72   if (is_system_or_unrestricted_section &&
73       vndk_user == VndkUserPartition::Vendor) {
74     // It is for vendor sp-hal
75     ns.AddPermittedPath("/odm/${LIB}/hw");
76     ns.AddPermittedPath("/odm/${LIB}/egl");
77     ns.AddPermittedPath("/vendor/${LIB}/hw");
78     ns.AddPermittedPath("/vendor/${LIB}/egl");
79     ns.AddPermittedPath("/system/vendor/${LIB}/hw");
80     ns.AddPermittedPath("/system/vendor/${LIB}/egl");
81 
82     // This is exceptionally required since android.hidl.memory@1.0-impl.so is here
83     ns.AddPermittedPath("/apex/com.android.vndk.v" +
84                         Var("VENDOR_VNDK_VERSION") + "/${LIB}/hw");
85   }
86 
87   // For the non-system section, the links should be identical to that of the
88   // 'vndk_in_system' namespace, except the links to 'default' and 'vndk_in_system'.
89   if (vndk_user == VndkUserPartition::Product) {
90     ns.GetLink(ctx.GetSystemNamespaceName())
91         .AddSharedLib({Var("LLNDK_LIBRARIES_PRODUCT")});
92   } else {
93     ns.GetLink(ctx.GetSystemNamespaceName())
94         .AddSharedLib({Var("LLNDK_LIBRARIES_VENDOR")});
95   }
96 
97   if (is_system_or_unrestricted_section) {
98     if (vndk_user == VndkUserPartition::Vendor) {
99       // The "vndk" namespace links to the system namespace for LLNDK libs above
100       // and links to "sphal" namespace for vendor libs. The ordering matters;
101       // the system namespace has higher priority than the "sphal" namespace.
102       ns.GetLink("sphal").AllowAllSharedLibs();
103     }
104   } else {
105     // [vendor] or [product] section
106     ns.GetLink("default").AllowAllSharedLibs();
107 
108     if (android::linkerconfig::modules::IsVndkInSystemNamespace()) {
109       ns.GetLink("vndk_in_system")
110           .AddSharedLib(Var("VNDK_USING_CORE_VARIANT_LIBRARIES"));
111     }
112   }
113 
114   ns.AddRequires(std::vector{"libneuralnetworks.so"});
115 
116   return ns;
117 }
118 }  // namespace contents
119 }  // namespace linkerconfig
120 }  // namespace android
121