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 #define _GNU_SOURCE 1
18 #include <stdint.h>
19 #include <string.h>
20
21 #include <string>
22
23 #if defined(__BIONIC__)
24
25 #include <gtest/gtest.h>
26
27 #include <unwindstack/DwarfSection.h>
28 #include <unwindstack/Elf.h>
29 #include <unwindstack/ElfInterface.h>
30 #include <unwindstack/Regs.h>
31 #include <unwindstack/RegsGetLocal.h>
32 #include <unwindstack/Unwinder.h>
33
34 // This test is specific to bionic to verify that __libc_init is
35 // properly setting the return address to undefined so that the
36 // unwind properly terminates.
37
38 namespace unwindstack {
39
DumpFrames(const UnwinderFromPid & unwinder)40 static std::string DumpFrames(const UnwinderFromPid& unwinder) {
41 std::string unwind;
42 for (size_t i = 0; i < unwinder.NumFrames(); i++) {
43 unwind += unwinder.FormatFrame(i) + '\n';
44 }
45 return unwind;
46 }
47
GetReturnAddressLocation(uint64_t rel_pc,DwarfSection * section)48 static DwarfLocationEnum GetReturnAddressLocation(uint64_t rel_pc, DwarfSection* section) {
49 if (section == nullptr) {
50 return DWARF_LOCATION_INVALID;
51 }
52
53 const DwarfFde* fde = section->GetFdeFromPc(rel_pc);
54 if (fde == nullptr || fde->cie == nullptr) {
55 return DWARF_LOCATION_INVALID;
56 }
57 dwarf_loc_regs_t regs;
58 if (!section->GetCfaLocationInfo(rel_pc, fde, ®s, ARCH_UNKNOWN)) {
59 return DWARF_LOCATION_INVALID;
60 }
61
62 auto reg_entry = regs.find(fde->cie->return_address_register);
63 if (reg_entry == regs.end()) {
64 return DWARF_LOCATION_INVALID;
65 }
66 return reg_entry->second.type;
67 }
68
VerifyReturnAddress(const FrameData & frame)69 static void VerifyReturnAddress(const FrameData& frame) {
70 // Now go and find information about the register data and verify that the relative pc results in
71 // an undefined register.
72 Elf elf(Memory::CreateFileMemory(frame.map_name, 0).release());
73 ASSERT_TRUE(elf.Init()) << "Failed to init elf object from " << frame.map_name;
74 ASSERT_TRUE(elf.valid()) << "Elf " << frame.map_name << " is not valid.";
75 ElfInterface* interface = elf.interface();
76
77 // Only check the eh_frame and the debug_frame since the undefined register
78 // is set using a cfi directive.
79 // Check debug_frame first, then eh_frame since debug_frame always
80 // contains the most specific data.
81 DwarfLocationEnum location = GetReturnAddressLocation(frame.rel_pc, interface->debug_frame());
82 if (location == DWARF_LOCATION_UNDEFINED) {
83 return;
84 }
85
86 location = GetReturnAddressLocation(frame.rel_pc, interface->eh_frame());
87 ASSERT_EQ(DWARF_LOCATION_UNDEFINED, location);
88 }
89
90 // This test assumes that it starts from the main thread, and that the
91 // libc.so on device will include symbols so that function names can
92 // be resolved.
TEST(VerifyBionicTermination,local_terminate)93 TEST(VerifyBionicTermination, local_terminate) {
94 std::unique_ptr<Regs> regs(Regs::CreateFromLocal());
95
96 UnwinderFromPid unwinder(512, getpid());
97 ASSERT_TRUE(unwinder.Init(regs->Arch()));
98 unwinder.SetRegs(regs.get());
99
100 RegsGetLocal(regs.get());
101 unwinder.Unwind();
102 ASSERT_LT(0U, unwinder.NumFrames());
103
104 SCOPED_TRACE(DumpFrames(unwinder));
105
106 // Look for the frame that includes __libc_init, there should only
107 // be one and it should be the last.
108 bool found = false;
109 const std::vector<FrameData>& frames = unwinder.frames();
110 for (size_t i = 0; i < unwinder.NumFrames(); i++) {
111 const FrameData& frame = frames[i];
112 if (frame.function_name == "__libc_init" && !frame.map_name.empty() &&
113 std::string("libc.so") == basename(frame.map_name.c_str())) {
114 ASSERT_EQ(unwinder.NumFrames(), i + 1) << "__libc_init is not last frame.";
115 ASSERT_NO_FATAL_FAILURE(VerifyReturnAddress(frame));
116 found = true;
117 }
118 }
119 ASSERT_TRUE(found) << "Unable to find libc.so:__libc_init frame\n";
120 }
121
122 } // namespace unwindstack
123
124 #endif
125