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/AndroidUnwinder.h>
28 #include <unwindstack/DwarfSection.h>
29 #include <unwindstack/Elf.h>
30 #include <unwindstack/ElfInterface.h>
31 
32 #include "ForkTest.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 
40 using VerifyBionicTermination = ForkTest;
41 
DumpFrames(const AndroidUnwinderData & data,AndroidUnwinder & unwinder)42 static std::string DumpFrames(const AndroidUnwinderData& data, AndroidUnwinder& unwinder) {
43   // Init this way so that the first frame of the backtrace starts on a new line.
44   std::string unwind("\n");
45   for (auto& frame : data.frames) {
46     unwind += unwinder.FormatFrame(frame) + '\n';
47   }
48   return unwind;
49 }
50 
GetReturnAddressLocation(uint64_t rel_pc,DwarfSection * section)51 static DwarfLocationEnum GetReturnAddressLocation(uint64_t rel_pc, DwarfSection* section) {
52   if (section == nullptr) {
53     return DWARF_LOCATION_INVALID;
54   }
55 
56   const DwarfFde* fde = section->GetFdeFromPc(rel_pc);
57   if (fde == nullptr || fde->cie == nullptr) {
58     return DWARF_LOCATION_INVALID;
59   }
60   DwarfLocations regs;
61   if (!section->GetCfaLocationInfo(rel_pc, fde, &regs, ARCH_UNKNOWN)) {
62     return DWARF_LOCATION_INVALID;
63   }
64 
65   auto reg_entry = regs.find(fde->cie->return_address_register);
66   if (reg_entry == regs.end()) {
67     return DWARF_LOCATION_INVALID;
68   }
69   return reg_entry->second.type;
70 }
71 
VerifyReturnAddress(const FrameData & frame)72 static void VerifyReturnAddress(const FrameData& frame) {
73   // Now go and find information about the register data and verify that the relative pc results in
74   // an undefined register.
75   auto file_memory = Memory::CreateFileMemory(frame.map_info->name(), 0);
76   Elf elf(file_memory);
77   ASSERT_TRUE(frame.map_info != nullptr);
78   ASSERT_TRUE(elf.Init()) << "Failed to init elf object from " << frame.map_info->name().c_str();
79   ASSERT_TRUE(elf.valid()) << "Elf " << frame.map_info->name().c_str() << " is not valid.";
80   ElfInterface* interface = elf.interface();
81 
82   // Only check the eh_frame and the debug_frame since the undefined register
83   // is set using a cfi directive.
84   // Check debug_frame first, then eh_frame since debug_frame always
85   // contains the most specific data.
86   DwarfLocationEnum location = GetReturnAddressLocation(frame.rel_pc, interface->debug_frame());
87   if (location == DWARF_LOCATION_UNDEFINED) {
88     return;
89   }
90 
91   location = GetReturnAddressLocation(frame.rel_pc, interface->eh_frame());
92   ASSERT_EQ(DWARF_LOCATION_UNDEFINED, location);
93 }
94 
95 // This assumes that the function starts from the main thread, and that the
96 // libc.so on device will include symbols so that function names can
97 // be resolved.
VerifyLibcInitTerminate(AndroidUnwinder & unwinder)98 static void VerifyLibcInitTerminate(AndroidUnwinder& unwinder) {
99   AndroidUnwinderData data;
100   ASSERT_TRUE(unwinder.Unwind(data));
101 
102   SCOPED_TRACE(DumpFrames(data, unwinder));
103 
104   // Look for the frame that includes __libc_init, there should only
105   // be one and it should be the last.
106   bool found = false;
107   const std::vector<FrameData>& frames = data.frames;
108   for (size_t i = 0; i < frames.size(); i++) {
109     const FrameData& frame = frames[i];
110     if (frame.function_name == "__libc_init" && frame.map_info != nullptr &&
111         !frame.map_info->name().empty() &&
112         std::string("libc.so") == basename(frame.map_info->name().c_str())) {
113       ASSERT_EQ(frames.size(), i + 1) << "__libc_init is not last frame.";
114       ASSERT_NO_FATAL_FAILURE(VerifyReturnAddress(frame));
115       found = true;
116     }
117   }
118   ASSERT_TRUE(found) << "Unable to find libc.so:__libc_init frame\n";
119 }
120 
TEST_F(VerifyBionicTermination,local_terminate)121 TEST_F(VerifyBionicTermination, local_terminate) {
122   AndroidLocalUnwinder unwinder;
123   VerifyLibcInitTerminate(unwinder);
124 }
125 
TEST_F(VerifyBionicTermination,remote_terminate)126 TEST_F(VerifyBionicTermination, remote_terminate) {
127   ASSERT_NO_FATAL_FAILURE(Fork());
128 
129   AndroidRemoteUnwinder unwinder(pid_);
130   VerifyLibcInitTerminate(unwinder);
131 }
132 
133 }  // namespace unwindstack
134 
135 #endif
136