xref: /aosp_15_r20/system/extras/simpleperf/JITDebugReader_test.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2022 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #include "JITDebugReader.h"
18*288bf522SAndroid Build Coastguard Worker #include "JITDebugReader_impl.h"
19*288bf522SAndroid Build Coastguard Worker 
20*288bf522SAndroid Build Coastguard Worker #include <sys/mman.h>
21*288bf522SAndroid Build Coastguard Worker 
22*288bf522SAndroid Build Coastguard Worker #include <android-base/file.h>
23*288bf522SAndroid Build Coastguard Worker #include <android-base/scopeguard.h>
24*288bf522SAndroid Build Coastguard Worker #include <android-base/strings.h>
25*288bf522SAndroid Build Coastguard Worker #include <android-base/test_utils.h>
26*288bf522SAndroid Build Coastguard Worker #include <android-base/unique_fd.h>
27*288bf522SAndroid Build Coastguard Worker #include "get_test_data.h"
28*288bf522SAndroid Build Coastguard Worker #include "utils.h"
29*288bf522SAndroid Build Coastguard Worker 
30*288bf522SAndroid Build Coastguard Worker #include <gtest/gtest.h>
31*288bf522SAndroid Build Coastguard Worker 
32*288bf522SAndroid Build Coastguard Worker using namespace simpleperf;
33*288bf522SAndroid Build Coastguard Worker using namespace simpleperf::JITDebugReader_impl;
34*288bf522SAndroid Build Coastguard Worker 
35*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(TempSymFile,smoke)36*288bf522SAndroid Build Coastguard Worker TEST(TempSymFile, smoke) {
37*288bf522SAndroid Build Coastguard Worker   TemporaryFile tmpfile;
38*288bf522SAndroid Build Coastguard Worker   std::unique_ptr<TempSymFile> symfile = TempSymFile::Create(tmpfile.path, false);
39*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(symfile);
40*288bf522SAndroid Build Coastguard Worker   // If we write entries starting from offset 0, libunwindstack will treat the whole file as an elf
41*288bf522SAndroid Build Coastguard Worker   // file in its elf cache. So make sure we don't start from offset 0.
42*288bf522SAndroid Build Coastguard Worker   uint64_t offset = symfile->GetOffset();
43*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(offset, 0u);
44*288bf522SAndroid Build Coastguard Worker 
45*288bf522SAndroid Build Coastguard Worker   // Write data and read it back.
46*288bf522SAndroid Build Coastguard Worker   const std::string test_data = "test_data";
47*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(symfile->WriteEntry(test_data.c_str(), test_data.size()));
48*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(symfile->Flush());
49*288bf522SAndroid Build Coastguard Worker 
50*288bf522SAndroid Build Coastguard Worker   char buf[16];
51*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(android::base::ReadFullyAtOffset(tmpfile.fd, buf, test_data.size(), offset));
52*288bf522SAndroid Build Coastguard Worker   ASSERT_EQ(strncmp(test_data.c_str(), buf, test_data.size()), 0);
53*288bf522SAndroid Build Coastguard Worker }
54*288bf522SAndroid Build Coastguard Worker 
55*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(JITDebugReader,read_dex_file_in_memory)56*288bf522SAndroid Build Coastguard Worker TEST(JITDebugReader, read_dex_file_in_memory) {
57*288bf522SAndroid Build Coastguard Worker   // 1. Create dex file in memory. Use mmap instead of malloc, to avoid the pointer from
58*288bf522SAndroid Build Coastguard Worker   // being modified by memory tag (or pointer authentication?) on ARM64.
59*288bf522SAndroid Build Coastguard Worker   std::string dex_file = GetTestData("base.vdex");
60*288bf522SAndroid Build Coastguard Worker   uint64_t file_size = GetFileSize(dex_file);
61*288bf522SAndroid Build Coastguard Worker   const uint64_t dex_file_offset = 0x28;
62*288bf522SAndroid Build Coastguard Worker   ASSERT_GT(file_size, dex_file_offset);
63*288bf522SAndroid Build Coastguard Worker   uint64_t symfile_size = file_size - dex_file_offset;
64*288bf522SAndroid Build Coastguard Worker   void* symfile_addr =
65*288bf522SAndroid Build Coastguard Worker       mmap(nullptr, symfile_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
66*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(symfile_addr, nullptr);
67*288bf522SAndroid Build Coastguard Worker   android::base::ScopeGuard g([&]() { munmap(symfile_addr, symfile_size); });
68*288bf522SAndroid Build Coastguard Worker   android::base::unique_fd fd(open(dex_file.c_str(), O_RDONLY | O_CLOEXEC));
69*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(fd.ok());
70*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(android::base::ReadFullyAtOffset(fd, symfile_addr, symfile_size, dex_file_offset));
71*288bf522SAndroid Build Coastguard Worker 
72*288bf522SAndroid Build Coastguard Worker   // 2. Create CodeEntry pointing to the dex file in memory.
73*288bf522SAndroid Build Coastguard Worker   Process process;
74*288bf522SAndroid Build Coastguard Worker   process.pid = getpid();
75*288bf522SAndroid Build Coastguard Worker   process.initialized = true;
76*288bf522SAndroid Build Coastguard Worker   std::vector<CodeEntry> code_entries(1);
77*288bf522SAndroid Build Coastguard Worker   code_entries[0].addr = reinterpret_cast<uintptr_t>(&code_entries[0]);
78*288bf522SAndroid Build Coastguard Worker   code_entries[0].symfile_addr = reinterpret_cast<uintptr_t>(symfile_addr);
79*288bf522SAndroid Build Coastguard Worker   code_entries[0].symfile_size = symfile_size;
80*288bf522SAndroid Build Coastguard Worker   code_entries[0].timestamp = 0;
81*288bf522SAndroid Build Coastguard Worker 
82*288bf522SAndroid Build Coastguard Worker   // 3. Test reading symbols from dex file in memory.
83*288bf522SAndroid Build Coastguard Worker   JITDebugReader reader("", JITDebugReader::SymFileOption::kDropSymFiles,
84*288bf522SAndroid Build Coastguard Worker                         JITDebugReader::SyncOption::kNoSync);
85*288bf522SAndroid Build Coastguard Worker   std::vector<JITDebugInfo> debug_info;
86*288bf522SAndroid Build Coastguard Worker   reader.ReadDexFileDebugInfo(process, code_entries, &debug_info);
87*288bf522SAndroid Build Coastguard Worker   ASSERT_EQ(debug_info.size(), 1);
88*288bf522SAndroid Build Coastguard Worker   const JITDebugInfo& info = debug_info[0];
89*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(info.dex_file_map);
90*288bf522SAndroid Build Coastguard Worker   ASSERT_EQ(info.dex_file_map->start_addr, reinterpret_cast<uintptr_t>(symfile_addr));
91*288bf522SAndroid Build Coastguard Worker   ASSERT_EQ(info.dex_file_map->len, symfile_size);
92*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(android::base::StartsWith(info.dex_file_map->name, kDexFileInMemoryPrefix));
93*288bf522SAndroid Build Coastguard Worker   ASSERT_EQ(info.symbols.size(), 12435);
94*288bf522SAndroid Build Coastguard Worker   // 4. Test if the symbols are sorted.
95*288bf522SAndroid Build Coastguard Worker   uint64_t prev_addr = 0;
96*288bf522SAndroid Build Coastguard Worker   for (const auto& symbol : info.symbols) {
97*288bf522SAndroid Build Coastguard Worker     ASSERT_LE(prev_addr, symbol.addr);
98*288bf522SAndroid Build Coastguard Worker     prev_addr = symbol.addr;
99*288bf522SAndroid Build Coastguard Worker   }
100*288bf522SAndroid Build Coastguard Worker }
101