1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2024 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker // Check that the current test executable only links known exported libraries
18*795d594fSAndroid Build Coastguard Worker // dynamically. Intended to be statically linked into standalone tests.
19*795d594fSAndroid Build Coastguard Worker
20*795d594fSAndroid Build Coastguard Worker #include <dlfcn.h>
21*795d594fSAndroid Build Coastguard Worker #include <fcntl.h>
22*795d594fSAndroid Build Coastguard Worker #include <gelf.h>
23*795d594fSAndroid Build Coastguard Worker #include <libelf.h>
24*795d594fSAndroid Build Coastguard Worker
25*795d594fSAndroid Build Coastguard Worker #include <algorithm>
26*795d594fSAndroid Build Coastguard Worker #include <filesystem>
27*795d594fSAndroid Build Coastguard Worker #include <string>
28*795d594fSAndroid Build Coastguard Worker #include <vector>
29*795d594fSAndroid Build Coastguard Worker
30*795d594fSAndroid Build Coastguard Worker #include "android-base/result-gmock.h"
31*795d594fSAndroid Build Coastguard Worker #include "android-base/result.h"
32*795d594fSAndroid Build Coastguard Worker #include "android-base/scopeguard.h"
33*795d594fSAndroid Build Coastguard Worker #include "android-base/strings.h"
34*795d594fSAndroid Build Coastguard Worker #include "android-base/unique_fd.h"
35*795d594fSAndroid Build Coastguard Worker #include "base/stl_util.h"
36*795d594fSAndroid Build Coastguard Worker #include "gmock/gmock.h"
37*795d594fSAndroid Build Coastguard Worker #include "gtest/gtest.h"
38*795d594fSAndroid Build Coastguard Worker
39*795d594fSAndroid Build Coastguard Worker namespace {
40*795d594fSAndroid Build Coastguard Worker
41*795d594fSAndroid Build Coastguard Worker using ::android::base::ErrnoError;
42*795d594fSAndroid Build Coastguard Worker using ::android::base::Error;
43*795d594fSAndroid Build Coastguard Worker using ::android::base::Result;
44*795d594fSAndroid Build Coastguard Worker
45*795d594fSAndroid Build Coastguard Worker // The allow-listed libraries. Standalone tests can assume that the ART module
46*795d594fSAndroid Build Coastguard Worker // is from the same build as the test(*), but not the platform nor any other
47*795d594fSAndroid Build Coastguard Worker // module. Hence all dynamic libraries listed here must satisfy at least one of
48*795d594fSAndroid Build Coastguard Worker // these conditions:
49*795d594fSAndroid Build Coastguard Worker //
50*795d594fSAndroid Build Coastguard Worker // - Have a stable ABI and be available since the APEX min_sdk_version (31).
51*795d594fSAndroid Build Coastguard Worker // This includes NDK and system APIs.
52*795d594fSAndroid Build Coastguard Worker // - Be loaded from the ART APEX itself(*). Note that linker namespaces aren't
53*795d594fSAndroid Build Coastguard Worker // set up to allow this for libraries that aren't exported, so in practice it
54*795d594fSAndroid Build Coastguard Worker // is restricted to them.
55*795d594fSAndroid Build Coastguard Worker // - Always be pushed to device together with the test.
56*795d594fSAndroid Build Coastguard Worker // - Be a runtime instrumentation library or similar, e.g. for sanitizer test
57*795d594fSAndroid Build Coastguard Worker // builds, where everything is always built from source - platform, module,
58*795d594fSAndroid Build Coastguard Worker // and tests.
59*795d594fSAndroid Build Coastguard Worker //
60*795d594fSAndroid Build Coastguard Worker // *) (Non-MCTS) CTS tests is an exception - they must work with any future
61*795d594fSAndroid Build Coastguard Worker // version of the module and hence restrict themselves to the exported module
62*795d594fSAndroid Build Coastguard Worker // APIs.
63*795d594fSAndroid Build Coastguard Worker constexpr const char* kAllowedDynamicLibDeps[] = {
64*795d594fSAndroid Build Coastguard Worker // LLVM
65*795d594fSAndroid Build Coastguard Worker "libclang_rt.hwasan-aarch64-android.so",
66*795d594fSAndroid Build Coastguard Worker // Bionic
67*795d594fSAndroid Build Coastguard Worker "libc.so",
68*795d594fSAndroid Build Coastguard Worker "libdl.so",
69*795d594fSAndroid Build Coastguard Worker "libdl_android.so",
70*795d594fSAndroid Build Coastguard Worker "libm.so",
71*795d594fSAndroid Build Coastguard Worker // Platform
72*795d594fSAndroid Build Coastguard Worker "heapprofd_client_api.so",
73*795d594fSAndroid Build Coastguard Worker "libbinder_ndk.so",
74*795d594fSAndroid Build Coastguard Worker "liblog.so",
75*795d594fSAndroid Build Coastguard Worker "libselinux.so",
76*795d594fSAndroid Build Coastguard Worker "libz.so",
77*795d594fSAndroid Build Coastguard Worker // Other modules
78*795d594fSAndroid Build Coastguard Worker "libstatspull.so",
79*795d594fSAndroid Build Coastguard Worker "libstatssocket.so",
80*795d594fSAndroid Build Coastguard Worker // ART exported
81*795d594fSAndroid Build Coastguard Worker "libdexfile.so",
82*795d594fSAndroid Build Coastguard Worker "libnativebridge.so",
83*795d594fSAndroid Build Coastguard Worker "libnativehelper.so",
84*795d594fSAndroid Build Coastguard Worker "libnativeloader.so",
85*795d594fSAndroid Build Coastguard Worker };
86*795d594fSAndroid Build Coastguard Worker
GetCurrentElfObjectPath()87*795d594fSAndroid Build Coastguard Worker Result<std::string> GetCurrentElfObjectPath() {
88*795d594fSAndroid Build Coastguard Worker Dl_info info;
89*795d594fSAndroid Build Coastguard Worker if (dladdr(reinterpret_cast<void*>(GetCurrentElfObjectPath), &info) == 0) {
90*795d594fSAndroid Build Coastguard Worker return Error() << "dladdr failed to map own address to a shared object.";
91*795d594fSAndroid Build Coastguard Worker }
92*795d594fSAndroid Build Coastguard Worker return info.dli_fname;
93*795d594fSAndroid Build Coastguard Worker }
94*795d594fSAndroid Build Coastguard Worker
GetDynamicLibDeps(const std::string & filename)95*795d594fSAndroid Build Coastguard Worker Result<std::vector<std::string>> GetDynamicLibDeps(const std::string& filename) {
96*795d594fSAndroid Build Coastguard Worker if (elf_version(EV_CURRENT) == EV_NONE) {
97*795d594fSAndroid Build Coastguard Worker return Errorf("libelf initialization failed: {}", elf_errmsg(-1));
98*795d594fSAndroid Build Coastguard Worker }
99*795d594fSAndroid Build Coastguard Worker
100*795d594fSAndroid Build Coastguard Worker android::base::unique_fd fd(open(filename.c_str(), O_RDONLY));
101*795d594fSAndroid Build Coastguard Worker if (fd.get() == -1) {
102*795d594fSAndroid Build Coastguard Worker return ErrnoErrorf("Error opening {}", filename);
103*795d594fSAndroid Build Coastguard Worker }
104*795d594fSAndroid Build Coastguard Worker
105*795d594fSAndroid Build Coastguard Worker Elf* elf = elf_begin(fd.get(), ELF_C_READ, /*ref=*/nullptr);
106*795d594fSAndroid Build Coastguard Worker if (elf == nullptr) {
107*795d594fSAndroid Build Coastguard Worker return Errorf("Error creating ELF object for {}: {}", filename, elf_errmsg(-1));
108*795d594fSAndroid Build Coastguard Worker }
109*795d594fSAndroid Build Coastguard Worker auto elf_cleanup = android::base::make_scope_guard([&]() { elf_end(elf); });
110*795d594fSAndroid Build Coastguard Worker
111*795d594fSAndroid Build Coastguard Worker std::vector<std::string> libs;
112*795d594fSAndroid Build Coastguard Worker
113*795d594fSAndroid Build Coastguard Worker // Find the dynamic section.
114*795d594fSAndroid Build Coastguard Worker for (Elf_Scn* dyn_scn = nullptr; (dyn_scn = elf_nextscn(elf, dyn_scn)) != nullptr;) {
115*795d594fSAndroid Build Coastguard Worker GElf_Shdr scn_hdr;
116*795d594fSAndroid Build Coastguard Worker if (gelf_getshdr(dyn_scn, &scn_hdr) != &scn_hdr) {
117*795d594fSAndroid Build Coastguard Worker return Errorf("Failed to retrieve ELF section header in {}: {}", filename, elf_errmsg(-1));
118*795d594fSAndroid Build Coastguard Worker }
119*795d594fSAndroid Build Coastguard Worker
120*795d594fSAndroid Build Coastguard Worker if (scn_hdr.sh_type == SHT_DYNAMIC) {
121*795d594fSAndroid Build Coastguard Worker Elf_Data* data = elf_getdata(dyn_scn, /*data=*/nullptr);
122*795d594fSAndroid Build Coastguard Worker
123*795d594fSAndroid Build Coastguard Worker // Iterate through dynamic section entries.
124*795d594fSAndroid Build Coastguard Worker for (int i = 0; i < scn_hdr.sh_size / scn_hdr.sh_entsize; i++) {
125*795d594fSAndroid Build Coastguard Worker GElf_Dyn dyn_entry;
126*795d594fSAndroid Build Coastguard Worker if (gelf_getdyn(data, i, &dyn_entry) != &dyn_entry) {
127*795d594fSAndroid Build Coastguard Worker return Errorf("Failed to get entry {} in ELF dynamic section of {}: {}",
128*795d594fSAndroid Build Coastguard Worker i,
129*795d594fSAndroid Build Coastguard Worker filename,
130*795d594fSAndroid Build Coastguard Worker elf_errmsg(-1));
131*795d594fSAndroid Build Coastguard Worker }
132*795d594fSAndroid Build Coastguard Worker
133*795d594fSAndroid Build Coastguard Worker if (dyn_entry.d_tag == DT_NEEDED) {
134*795d594fSAndroid Build Coastguard Worker const char* lib_name = elf_strptr(elf, scn_hdr.sh_link, dyn_entry.d_un.d_val);
135*795d594fSAndroid Build Coastguard Worker if (lib_name == nullptr) {
136*795d594fSAndroid Build Coastguard Worker return Errorf("Failed to get string from entry {} in ELF dynamic section of {}: {}",
137*795d594fSAndroid Build Coastguard Worker i,
138*795d594fSAndroid Build Coastguard Worker filename,
139*795d594fSAndroid Build Coastguard Worker elf_errmsg(-1));
140*795d594fSAndroid Build Coastguard Worker }
141*795d594fSAndroid Build Coastguard Worker libs.push_back(lib_name);
142*795d594fSAndroid Build Coastguard Worker }
143*795d594fSAndroid Build Coastguard Worker }
144*795d594fSAndroid Build Coastguard Worker break; // Found the dynamic section, no need to continue.
145*795d594fSAndroid Build Coastguard Worker }
146*795d594fSAndroid Build Coastguard Worker }
147*795d594fSAndroid Build Coastguard Worker
148*795d594fSAndroid Build Coastguard Worker return libs;
149*795d594fSAndroid Build Coastguard Worker }
150*795d594fSAndroid Build Coastguard Worker
151*795d594fSAndroid Build Coastguard Worker } // namespace
152*795d594fSAndroid Build Coastguard Worker
TEST(StandaloneTestAllowedLibDeps,test)153*795d594fSAndroid Build Coastguard Worker TEST(StandaloneTestAllowedLibDeps, test) {
154*795d594fSAndroid Build Coastguard Worker Result<std::string> path_to_self = GetCurrentElfObjectPath();
155*795d594fSAndroid Build Coastguard Worker ASSERT_RESULT_OK(path_to_self);
156*795d594fSAndroid Build Coastguard Worker Result<std::vector<std::string>> dyn_lib_deps = GetDynamicLibDeps(path_to_self.value());
157*795d594fSAndroid Build Coastguard Worker ASSERT_RESULT_OK(dyn_lib_deps);
158*795d594fSAndroid Build Coastguard Worker
159*795d594fSAndroid Build Coastguard Worker // Allow .so files in the same directory as the test binary, for shared libs
160*795d594fSAndroid Build Coastguard Worker // pushed with the test using `data_libs`.
161*795d594fSAndroid Build Coastguard Worker std::filesystem::path self_dir = std::filesystem::path(path_to_self.value()).parent_path();
162*795d594fSAndroid Build Coastguard Worker std::vector<std::string> test_libs;
163*795d594fSAndroid Build Coastguard Worker for (const std::filesystem::directory_entry& entry :
164*795d594fSAndroid Build Coastguard Worker std::filesystem::directory_iterator(self_dir)) {
165*795d594fSAndroid Build Coastguard Worker if (entry.is_regular_file() && entry.path().extension() == ".so") {
166*795d594fSAndroid Build Coastguard Worker test_libs.push_back(entry.path().filename());
167*795d594fSAndroid Build Coastguard Worker }
168*795d594fSAndroid Build Coastguard Worker }
169*795d594fSAndroid Build Coastguard Worker
170*795d594fSAndroid Build Coastguard Worker std::vector<std::string> disallowed_libs;
171*795d594fSAndroid Build Coastguard Worker for (const std::string& dyn_lib_dep : dyn_lib_deps.value()) {
172*795d594fSAndroid Build Coastguard Worker if (std::find(std::begin(kAllowedDynamicLibDeps),
173*795d594fSAndroid Build Coastguard Worker std::end(kAllowedDynamicLibDeps),
174*795d594fSAndroid Build Coastguard Worker dyn_lib_dep) != std::end(kAllowedDynamicLibDeps)) {
175*795d594fSAndroid Build Coastguard Worker continue;
176*795d594fSAndroid Build Coastguard Worker }
177*795d594fSAndroid Build Coastguard Worker if (art::ContainsElement(test_libs, dyn_lib_dep)) {
178*795d594fSAndroid Build Coastguard Worker continue;
179*795d594fSAndroid Build Coastguard Worker }
180*795d594fSAndroid Build Coastguard Worker disallowed_libs.push_back(dyn_lib_dep);
181*795d594fSAndroid Build Coastguard Worker }
182*795d594fSAndroid Build Coastguard Worker
183*795d594fSAndroid Build Coastguard Worker EXPECT_THAT(disallowed_libs, testing::IsEmpty())
184*795d594fSAndroid Build Coastguard Worker << path_to_self.value() << " has disallowed shared library dependencies.";
185*795d594fSAndroid Build Coastguard Worker }
186