xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/sandbox2/util/minielf_test.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "sandboxed_api/sandbox2/util/minielf.h"
16 
17 #include <cstdint>
18 #include <string>
19 #include <vector>
20 
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 #include "absl/algorithm/container.h"
24 #include "absl/status/statusor.h"
25 #include "sandboxed_api/sandbox2/util/maps_parser.h"
26 #include "sandboxed_api/testing.h"
27 #include "sandboxed_api/util/file_helpers.h"
28 #include "sandboxed_api/util/status_matchers.h"
29 
ExportedFunction()30 extern "C" void ExportedFunction() {
31   // Don't do anything - used to generate a symbol.
32 }
33 
34 namespace file = ::sapi::file;
35 using ::sapi::GetTestSourcePath;
36 using ::sapi::IsOk;
37 using ::testing::ElementsAre;
38 using ::testing::Eq;
39 using ::testing::IsTrue;
40 using ::testing::Ne;
41 using ::testing::StrEq;
42 
43 namespace sandbox2 {
44 namespace {
45 
TEST(MinielfTest,Chrome70)46 TEST(MinielfTest, Chrome70) {
47   SAPI_ASSERT_OK_AND_ASSIGN(
48       ElfFile elf,
49       ElfFile::ParseFromFile(
50           GetTestSourcePath("sandbox2/util/testdata/chrome_grte_header"),
51           ElfFile::kGetInterpreter));
52   EXPECT_THAT(elf.interpreter(), StrEq("/usr/grte/v4/ld64"));
53 }
54 
TEST(MinielfTest,SymbolResolutionWorks)55 TEST(MinielfTest, SymbolResolutionWorks) {
56   SAPI_ASSERT_OK_AND_ASSIGN(
57       ElfFile elf,
58       ElfFile::ParseFromFile("/proc/self/exe", ElfFile::kLoadSymbols));
59   ASSERT_THAT(elf.position_independent(), IsTrue());
60 
61   // Load /proc/self/maps to take ASLR into account.
62   std::string maps_buffer;
63   ASSERT_THAT(
64       file::GetContents("/proc/self/maps", &maps_buffer, file::Defaults()),
65       IsOk());
66   SAPI_ASSERT_OK_AND_ASSIGN(std::vector<MapsEntry> maps,
67                             ParseProcMaps(maps_buffer));
68 
69   // Find maps entry that covers this entry.
70   uint64_t function_address = reinterpret_cast<uint64_t>(&ExportedFunction);
71   auto entry =
72       absl::c_find_if(maps, [function_address](const MapsEntry& entry) {
73         return entry.start <= function_address && entry.end > function_address;
74       });
75   ASSERT_THAT(entry, Ne(maps.end()));
76 
77   auto function_symbol =
78       absl::c_find_if(elf.symbols(), [](const ElfFile::Symbol& symbol) {
79         return symbol.name == "ExportedFunction";
80       });
81   ASSERT_THAT(function_symbol, Ne(elf.symbols().end()));
82 
83   function_address -= entry->start - entry->pgoff;
84   EXPECT_THAT(function_symbol->address, Eq(function_address));
85 }
86 
TEST(MinielfTest,ImportedLibraries)87 TEST(MinielfTest, ImportedLibraries) {
88   SAPI_ASSERT_OK_AND_ASSIGN(
89       ElfFile elf, ElfFile::ParseFromFile(
90                        GetTestSourcePath("sandbox2/util/testdata/hello_world"),
91                        ElfFile::kLoadImportedLibraries));
92   EXPECT_THAT(elf.imported_libraries(), ElementsAre("libc.so.6"));
93 }
94 
95 }  // namespace
96 }  // namespace sandbox2
97