NativeProcessProtocolTest.cpp
6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//===-- NativeProcessProtocolTest.cpp ---------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "TestingSupport/Host/NativeProcessTestUtils.h"
#include "lldb/Host/common/NativeProcessProtocol.h"
#include "llvm/Support/Process.h"
#include "llvm/Testing/Support/Error.h"
#include "gmock/gmock.h"
using namespace lldb_private;
using namespace lldb;
using namespace testing;
TEST(NativeProcessProtocolTest, SetBreakpoint) {
NiceMock<MockDelegate> DummyDelegate;
MockProcess<NativeProcessProtocol> Process(DummyDelegate,
ArchSpec("x86_64-pc-linux"));
auto Trap = cantFail(Process.GetSoftwareBreakpointTrapOpcode(1));
InSequence S;
EXPECT_CALL(Process, ReadMemory(0x47, 1))
.WillOnce(Return(ByMove(std::vector<uint8_t>{0xbb})));
EXPECT_CALL(Process, WriteMemory(0x47, Trap)).WillOnce(Return(ByMove(1)));
EXPECT_CALL(Process, ReadMemory(0x47, 1)).WillOnce(Return(ByMove(Trap)));
EXPECT_THAT_ERROR(Process.SetBreakpoint(0x47, 0, false).ToError(),
llvm::Succeeded());
}
TEST(NativeProcessProtocolTest, SetBreakpointFailRead) {
NiceMock<MockDelegate> DummyDelegate;
MockProcess<NativeProcessProtocol> Process(DummyDelegate,
ArchSpec("x86_64-pc-linux"));
EXPECT_CALL(Process, ReadMemory(0x47, 1))
.WillOnce(Return(ByMove(
llvm::createStringError(llvm::inconvertibleErrorCode(), "Foo"))));
EXPECT_THAT_ERROR(Process.SetBreakpoint(0x47, 0, false).ToError(),
llvm::Failed());
}
TEST(NativeProcessProtocolTest, SetBreakpointFailWrite) {
NiceMock<MockDelegate> DummyDelegate;
MockProcess<NativeProcessProtocol> Process(DummyDelegate,
ArchSpec("x86_64-pc-linux"));
auto Trap = cantFail(Process.GetSoftwareBreakpointTrapOpcode(1));
InSequence S;
EXPECT_CALL(Process, ReadMemory(0x47, 1))
.WillOnce(Return(ByMove(std::vector<uint8_t>{0xbb})));
EXPECT_CALL(Process, WriteMemory(0x47, Trap))
.WillOnce(Return(ByMove(
llvm::createStringError(llvm::inconvertibleErrorCode(), "Foo"))));
EXPECT_THAT_ERROR(Process.SetBreakpoint(0x47, 0, false).ToError(),
llvm::Failed());
}
TEST(NativeProcessProtocolTest, SetBreakpointFailVerify) {
NiceMock<MockDelegate> DummyDelegate;
MockProcess<NativeProcessProtocol> Process(DummyDelegate,
ArchSpec("x86_64-pc-linux"));
auto Trap = cantFail(Process.GetSoftwareBreakpointTrapOpcode(1));
InSequence S;
EXPECT_CALL(Process, ReadMemory(0x47, 1))
.WillOnce(Return(ByMove(std::vector<uint8_t>{0xbb})));
EXPECT_CALL(Process, WriteMemory(0x47, Trap)).WillOnce(Return(ByMove(1)));
EXPECT_CALL(Process, ReadMemory(0x47, 1))
.WillOnce(Return(ByMove(
llvm::createStringError(llvm::inconvertibleErrorCode(), "Foo"))));
EXPECT_THAT_ERROR(Process.SetBreakpoint(0x47, 0, false).ToError(),
llvm::Failed());
}
TEST(NativeProcessProtocolTest, ReadMemoryWithoutTrap) {
NiceMock<MockDelegate> DummyDelegate;
MockProcess<NativeProcessProtocol> Process(DummyDelegate,
ArchSpec("aarch64-pc-linux"));
FakeMemory M{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}};
EXPECT_CALL(Process, ReadMemory(_, _))
.WillRepeatedly(Invoke(&M, &FakeMemory::Read));
EXPECT_CALL(Process, WriteMemory(_, _))
.WillRepeatedly(Invoke(&M, &FakeMemory::Write));
EXPECT_THAT_ERROR(Process.SetBreakpoint(0x4, 0, false).ToError(),
llvm::Succeeded());
EXPECT_THAT_EXPECTED(
Process.ReadMemoryWithoutTrap(0, 10),
llvm::HasValue(std::vector<uint8_t>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}));
EXPECT_THAT_EXPECTED(Process.ReadMemoryWithoutTrap(0, 6),
llvm::HasValue(std::vector<uint8_t>{0, 1, 2, 3, 4, 5}));
EXPECT_THAT_EXPECTED(Process.ReadMemoryWithoutTrap(6, 4),
llvm::HasValue(std::vector<uint8_t>{6, 7, 8, 9}));
EXPECT_THAT_EXPECTED(Process.ReadMemoryWithoutTrap(6, 2),
llvm::HasValue(std::vector<uint8_t>{6, 7}));
EXPECT_THAT_EXPECTED(Process.ReadMemoryWithoutTrap(4, 2),
llvm::HasValue(std::vector<uint8_t>{4, 5}));
}
TEST(NativeProcessProtocolTest, ReadCStringFromMemory) {
NiceMock<MockDelegate> DummyDelegate;
MockProcess<NativeProcessProtocol> Process(DummyDelegate,
ArchSpec("aarch64-pc-linux"));
FakeMemory M({'h', 'e', 'l', 'l', 'o', 0, 'w', 'o'});
EXPECT_CALL(Process, ReadMemory(_, _))
.WillRepeatedly(Invoke(&M, &FakeMemory::Read));
char string[1024];
size_t bytes_read;
EXPECT_THAT_EXPECTED(Process.ReadCStringFromMemory(
0x0, &string[0], sizeof(string), bytes_read),
llvm::HasValue(llvm::StringRef("hello")));
EXPECT_EQ(bytes_read, 6UL);
}
TEST(NativeProcessProtocolTest, ReadCStringFromMemory_MaxSize) {
NiceMock<MockDelegate> DummyDelegate;
MockProcess<NativeProcessProtocol> Process(DummyDelegate,
ArchSpec("aarch64-pc-linux"));
FakeMemory M({'h', 'e', 'l', 'l', 'o', 0, 'w', 'o'});
EXPECT_CALL(Process, ReadMemory(_, _))
.WillRepeatedly(Invoke(&M, &FakeMemory::Read));
char string[4];
size_t bytes_read;
EXPECT_THAT_EXPECTED(Process.ReadCStringFromMemory(
0x0, &string[0], sizeof(string), bytes_read),
llvm::HasValue(llvm::StringRef("hel")));
EXPECT_EQ(bytes_read, 3UL);
}
TEST(NativeProcessProtocolTest, ReadCStringFromMemory_CrossPageBoundary) {
NiceMock<MockDelegate> DummyDelegate;
MockProcess<NativeProcessProtocol> Process(DummyDelegate,
ArchSpec("aarch64-pc-linux"));
unsigned string_start = llvm::sys::Process::getPageSizeEstimate() - 3;
FakeMemory M({'h', 'e', 'l', 'l', 'o', 0, 'w', 'o'}, string_start);
EXPECT_CALL(Process, ReadMemory(_, _))
.WillRepeatedly(Invoke(&M, &FakeMemory::Read));
char string[1024];
size_t bytes_read;
EXPECT_THAT_EXPECTED(Process.ReadCStringFromMemory(string_start, &string[0],
sizeof(string),
bytes_read),
llvm::HasValue(llvm::StringRef("hello")));
EXPECT_EQ(bytes_read, 6UL);
}