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 <signal.h>
18 #include <stdint.h>
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 #include <unistd.h>
22
23 #include <memory>
24
25 #include <gtest/gtest.h>
26
27 #include <unwindstack/Regs.h>
28
29 #include "PidUtils.h"
30
31 namespace unwindstack {
32
33 class RegsRemoteTest : public ::testing::Test {
34 protected:
SetUp()35 void SetUp() override {
36 if ((pid_ = fork()) == 0) {
37 volatile bool run = true;
38 while (!run) {
39 }
40 exit(1);
41 }
42 ASSERT_TRUE(pid_ != -1);
43 ASSERT_TRUE(Attach(pid_));
44 }
45
TearDown()46 void TearDown() override {
47 if (pid_ == -1) {
48 return;
49 }
50 EXPECT_TRUE(Detach(pid_));
51 kill(pid_, SIGKILL);
52 waitpid(pid_, nullptr, 0);
53 }
54
55 pid_t pid_ = -1;
56 };
57
TEST_F(RegsRemoteTest,remote_get)58 TEST_F(RegsRemoteTest, remote_get) {
59 std::unique_ptr<Regs> regs(Regs::RemoteGet(pid_));
60 #if defined(__arm__)
61 ASSERT_EQ(ARCH_ARM, regs->Arch());
62 #elif defined(__aarch64__)
63 ASSERT_EQ(ARCH_ARM64, regs->Arch());
64 #elif defined(__i386__)
65 ASSERT_EQ(ARCH_X86, regs->Arch());
66 #elif defined(__x86_64__)
67 ASSERT_EQ(ARCH_X86_64, regs->Arch());
68 #else
69 ASSERT_EQ(nullptr, regs.get());
70 #endif
71 }
72
TEST_F(RegsRemoteTest,remote_get_ptrace_fails)73 TEST_F(RegsRemoteTest, remote_get_ptrace_fails) {
74 ErrorCode error_code;
75 // Use our own pid since that will always fail.
76 std::unique_ptr<Regs> regs(Regs::RemoteGet(getpid(), &error_code));
77 ASSERT_TRUE(regs == nullptr);
78 ASSERT_EQ(ERROR_PTRACE_CALL, error_code);
79 }
80
TEST_F(RegsRemoteTest,remote_get_arch)81 TEST_F(RegsRemoteTest, remote_get_arch) {
82 #if defined(__arm__)
83 ASSERT_EQ(ARCH_ARM, Regs::RemoteGetArch(pid_));
84 #elif defined(__aarch64__)
85 ASSERT_EQ(ARCH_ARM64, Regs::RemoteGetArch(pid_));
86 #elif defined(__i386__)
87 ASSERT_EQ(ARCH_X86, Regs::RemoteGetArch(pid_));
88 #elif defined(__x86_64__)
89 ASSERT_EQ(ARCH_X86_64, Regs::RemoteGetArch(pid_));
90 #elif defined(__riscv)
91 ASSERT_EQ(ARCH_RISCV64, Regs::RemoteGetArch(pid_));
92 #else
93 ASSERT_EQ(ARCH_NONE, Regs::RemoteGetArch(pid_));
94 #endif
95 }
96
TEST_F(RegsRemoteTest,remote_get_arch_ptrace_fails)97 TEST_F(RegsRemoteTest, remote_get_arch_ptrace_fails) {
98 ErrorCode error_code;
99 // Use our own pid since that will always fail.
100 ASSERT_EQ(ARCH_UNKNOWN, Regs::RemoteGetArch(getpid(), &error_code));
101 ASSERT_EQ(ERROR_PTRACE_CALL, error_code);
102 }
103
104 } // namespace unwindstack
105