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 // SP-HAL(Sameprocess-HAL)s are the only vendor libraries that are allowed to be
18 // loaded inside system processes. libEGL_<chipset>.so, libGLESv2_<chipset>.so,
19 // android.hardware.graphics.mapper@2.0-impl.so, etc are SP-HALs.
20 //
21 // This namespace is exclusivly for SP-HALs. When the framework tries to
22 // dynamically load SP-HALs, android_dlopen_ext() is used to explicitly specify
23 // that they should be searched and loaded from this namespace.
24 //
25 // Note that there is no link from the default namespace to this namespace.
26 
27 #include "linkerconfig/namespacebuilder.h"
28 
29 using android::linkerconfig::modules::Namespace;
30 
31 namespace android {
32 namespace linkerconfig {
33 namespace contents {
BuildSphalNamespace(const Context & ctx)34 Namespace BuildSphalNamespace([[maybe_unused]] const Context& ctx) {
35   // Visible to allow use with android_dlopen_ext, and with
36   // android_link_namespaces in libnativeloader.
37   Namespace ns("sphal",
38                /*is_isolated=*/!ctx.IsUnrestrictedSection(),
39                /*is_visible=*/true);
40   ns.AddSearchPath("/odm/${LIB}");
41   ns.AddSearchPath("/vendor/${LIB}");
42   ns.AddSearchPath("/vendor/${LIB}/egl");
43   ns.AddSearchPath("/vendor/${LIB}/hw");
44 
45   ns.AddPermittedPath("/odm/${LIB}");
46   ns.AddPermittedPath("/vendor/${LIB}");
47   ns.AddPermittedPath("/system/vendor/${LIB}");
48 
49   if (ctx.IsApexBinaryConfig() && !ctx.IsVndkAvailable()) {
50     // If device is legacy, let Sphal libraries access to system lib path for
51     // VNDK-SP libraries
52     ns.AddSearchPath("/system/${LIB}");
53     ns.AddPermittedPath("/system/${LIB}");
54   }
55 
56   if (ctx.IsApexBinaryConfig()) {
57     if (ctx.IsVndkAvailable()) {
58       ns.AddRequires(std::vector{":vndksp"});
59       ns.GetLink(ctx.GetSystemNamespaceName())
60           .AddSharedLib(Var("LLNDK_LIBRARIES_VENDOR", ""));
61     }
62   } else {
63     // Once in this namespace, access to libraries in /system/lib is restricted.
64     // Only libs listed here can be used. Order is important here as the
65     // namespaces are tried in this order. rs should be before vndk because both
66     // are capable of loading libRS_internal.so
67     if (ctx.IsSystemSection() || ctx.IsUnrestrictedSection()) {
68       ns.GetLink("rs").AddSharedLib("libRS_internal.so");
69     }
70     ns.GetLink(ctx.GetSystemNamespaceName())
71         .AddSharedLib(Var("LLNDK_LIBRARIES_VENDOR", ""));
72     ns.GetLink("vndk").AddSharedLib(
73         Var("VNDK_SAMEPROCESS_LIBRARIES_VENDOR", ""));
74     ns.AddRequires(std::vector{"libneuralnetworks.so"});
75   }
76 
77   return ns;
78 }
79 }  // namespace contents
80 }  // namespace linkerconfig
81 }  // namespace android
82