1 /*
2 * Copyright (C) 2022 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 #include "JITDebugReader.h"
18 #include "JITDebugReader_impl.h"
19
20 #include <sys/mman.h>
21
22 #include <android-base/file.h>
23 #include <android-base/scopeguard.h>
24 #include <android-base/strings.h>
25 #include <android-base/test_utils.h>
26 #include <android-base/unique_fd.h>
27 #include "get_test_data.h"
28 #include "utils.h"
29
30 #include <gtest/gtest.h>
31
32 using namespace simpleperf;
33 using namespace simpleperf::JITDebugReader_impl;
34
35 // @CddTest = 6.1/C-0-2
TEST(TempSymFile,smoke)36 TEST(TempSymFile, smoke) {
37 TemporaryFile tmpfile;
38 std::unique_ptr<TempSymFile> symfile = TempSymFile::Create(tmpfile.path, false);
39 ASSERT_TRUE(symfile);
40 // If we write entries starting from offset 0, libunwindstack will treat the whole file as an elf
41 // file in its elf cache. So make sure we don't start from offset 0.
42 uint64_t offset = symfile->GetOffset();
43 ASSERT_NE(offset, 0u);
44
45 // Write data and read it back.
46 const std::string test_data = "test_data";
47 ASSERT_TRUE(symfile->WriteEntry(test_data.c_str(), test_data.size()));
48 ASSERT_TRUE(symfile->Flush());
49
50 char buf[16];
51 ASSERT_TRUE(android::base::ReadFullyAtOffset(tmpfile.fd, buf, test_data.size(), offset));
52 ASSERT_EQ(strncmp(test_data.c_str(), buf, test_data.size()), 0);
53 }
54
55 // @CddTest = 6.1/C-0-2
TEST(JITDebugReader,read_dex_file_in_memory)56 TEST(JITDebugReader, read_dex_file_in_memory) {
57 // 1. Create dex file in memory. Use mmap instead of malloc, to avoid the pointer from
58 // being modified by memory tag (or pointer authentication?) on ARM64.
59 std::string dex_file = GetTestData("base.vdex");
60 uint64_t file_size = GetFileSize(dex_file);
61 const uint64_t dex_file_offset = 0x28;
62 ASSERT_GT(file_size, dex_file_offset);
63 uint64_t symfile_size = file_size - dex_file_offset;
64 void* symfile_addr =
65 mmap(nullptr, symfile_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
66 ASSERT_NE(symfile_addr, nullptr);
67 android::base::ScopeGuard g([&]() { munmap(symfile_addr, symfile_size); });
68 android::base::unique_fd fd(open(dex_file.c_str(), O_RDONLY | O_CLOEXEC));
69 ASSERT_TRUE(fd.ok());
70 ASSERT_TRUE(android::base::ReadFullyAtOffset(fd, symfile_addr, symfile_size, dex_file_offset));
71
72 // 2. Create CodeEntry pointing to the dex file in memory.
73 Process process;
74 process.pid = getpid();
75 process.initialized = true;
76 std::vector<CodeEntry> code_entries(1);
77 code_entries[0].addr = reinterpret_cast<uintptr_t>(&code_entries[0]);
78 code_entries[0].symfile_addr = reinterpret_cast<uintptr_t>(symfile_addr);
79 code_entries[0].symfile_size = symfile_size;
80 code_entries[0].timestamp = 0;
81
82 // 3. Test reading symbols from dex file in memory.
83 JITDebugReader reader("", JITDebugReader::SymFileOption::kDropSymFiles,
84 JITDebugReader::SyncOption::kNoSync);
85 std::vector<JITDebugInfo> debug_info;
86 reader.ReadDexFileDebugInfo(process, code_entries, &debug_info);
87 ASSERT_EQ(debug_info.size(), 1);
88 const JITDebugInfo& info = debug_info[0];
89 ASSERT_TRUE(info.dex_file_map);
90 ASSERT_EQ(info.dex_file_map->start_addr, reinterpret_cast<uintptr_t>(symfile_addr));
91 ASSERT_EQ(info.dex_file_map->len, symfile_size);
92 ASSERT_TRUE(android::base::StartsWith(info.dex_file_map->name, kDexFileInMemoryPrefix));
93 ASSERT_EQ(info.symbols.size(), 12435);
94 // 4. Test if the symbols are sorted.
95 uint64_t prev_addr = 0;
96 for (const auto& symbol : info.symbols) {
97 ASSERT_LE(prev_addr, symbol.addr);
98 prev_addr = symbol.addr;
99 }
100 }
101