1 /*
2 * Copyright (C) 2018 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 <stdint.h>
18
19 #include <thread>
20
21 #include <gtest/gtest.h>
22
23 #include <unwindstack/MapInfo.h>
24 #include <unwindstack/Maps.h>
25
26 #include "ElfFake.h"
27
28 namespace unwindstack {
29
TEST(MapInfoTest,maps_constructor_const_char)30 TEST(MapInfoTest, maps_constructor_const_char) {
31 MapInfo prev_map(nullptr, nullptr, 0, 0, 0, 0, "");
32 MapInfo map_info(&prev_map, &prev_map, 1, 2, 3, 4, "map");
33
34 EXPECT_EQ(&prev_map, map_info.prev_map());
35 EXPECT_EQ(1UL, map_info.start());
36 EXPECT_EQ(2UL, map_info.end());
37 EXPECT_EQ(3UL, map_info.offset());
38 EXPECT_EQ(4UL, map_info.flags());
39 EXPECT_EQ("map", map_info.name());
40 EXPECT_EQ(INT64_MAX, map_info.load_bias());
41 EXPECT_EQ(0UL, map_info.elf_offset());
42 EXPECT_TRUE(map_info.elf().get() == nullptr);
43 }
44
TEST(MapInfoTest,maps_constructor_string)45 TEST(MapInfoTest, maps_constructor_string) {
46 std::string name("string_map");
47 MapInfo prev_map(nullptr, nullptr, 0, 0, 0, 0, "");
48 MapInfo map_info(&prev_map, &prev_map, 1, 2, 3, 4, name);
49
50 EXPECT_EQ(&prev_map, map_info.prev_map());
51 EXPECT_EQ(1UL, map_info.start());
52 EXPECT_EQ(2UL, map_info.end());
53 EXPECT_EQ(3UL, map_info.offset());
54 EXPECT_EQ(4UL, map_info.flags());
55 EXPECT_EQ("string_map", map_info.name());
56 EXPECT_EQ(INT64_MAX, map_info.load_bias());
57 EXPECT_EQ(0UL, map_info.elf_offset());
58 EXPECT_TRUE(map_info.elf().get() == nullptr);
59 }
60
TEST(MapInfoTest,get_function_name)61 TEST(MapInfoTest, get_function_name) {
62 ElfFake* elf = new ElfFake(nullptr);
63 ElfInterfaceFake* interface = new ElfInterfaceFake(nullptr);
64 elf->FakeSetInterface(interface);
65 interface->FakePushFunctionData(FunctionData("function", 1000));
66
67 MapInfo map_info(nullptr, nullptr, 1, 2, 3, 4, "");
68 map_info.set_elf(elf);
69
70 SharedString name;
71 uint64_t offset;
72 ASSERT_TRUE(map_info.GetFunctionName(1000, &name, &offset));
73 EXPECT_EQ("function", name);
74 EXPECT_EQ(1000UL, offset);
75 }
76
TEST(MapInfoTest,multiple_thread_get_elf_fields)77 TEST(MapInfoTest, multiple_thread_get_elf_fields) {
78 MapInfo map_info(nullptr, nullptr, 0, 0, 0, 0, "");
79
80 static constexpr size_t kNumConcurrentThreads = 100;
81 MapInfo::ElfFields* elf_fields[kNumConcurrentThreads];
82
83 std::atomic_bool wait;
84 wait = true;
85 // Create all of the threads and have them do the call at the same time
86 // to make it likely that a race will occur.
87 std::vector<std::thread*> threads;
88 for (size_t i = 0; i < kNumConcurrentThreads; i++) {
89 std::thread* thread = new std::thread([i, &wait, &map_info, &elf_fields]() {
90 while (wait)
91 ;
92 elf_fields[i] = &map_info.GetElfFields();
93 });
94 threads.push_back(thread);
95 }
96
97 // Set them all going and wait for the threads to finish.
98 wait = false;
99 for (auto thread : threads) {
100 thread->join();
101 delete thread;
102 }
103
104 // Now verify that all of elf fields are exactly the same and valid.
105 MapInfo::ElfFields* expected_elf_fields = &map_info.GetElfFields();
106 ASSERT_TRUE(expected_elf_fields != nullptr);
107 for (size_t i = 0; i < kNumConcurrentThreads; i++) {
108 EXPECT_EQ(expected_elf_fields, elf_fields[i]) << "Thread " << i << " mismatched.";
109 }
110 }
111
112 } // namespace unwindstack
113