forked from libriscv/libriscv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify_elf.cpp
226 lines (189 loc) · 7.31 KB
/
verify_elf.cpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <catch2/catch_test_macros.hpp>
#include <libriscv/machine.hpp>
extern std::vector<uint8_t> load_file(const std::string& filename);
static const uint64_t MAX_MEMORY = 680ul << 20; /* 680MB */
static const uint64_t MAX_INSTRUCTIONS = 10'000'000ul;
static const std::string cwd {SRCDIR};
using namespace riscv;
TEST_CASE("Golang Hello World", "[Verify]")
{
const auto binary = load_file(cwd + "/elf/golang-riscv64-hello-world");
riscv::Machine<RISCV64> machine { binary, { .memory_max = MAX_MEMORY } };
// We need to install Linux system calls for maximum gucciness
machine.setup_linux_syscalls();
machine.fds().permit_filesystem = true;
machine.fds().permit_sockets = false;
machine.fds().filter_open = [] (void* user, const std::string& path) {
(void) user; (void) path;
return false;
};
// multi-threading
machine.setup_posix_threads();
// We need to create a Linux environment for runtimes to work well
machine.setup_linux(
{"golang-riscv64-hello-world"},
{"LC_TYPE=C", "LC_ALL=C", "USER=root"});
struct State {
bool output_is_hello_world = false;
} state;
machine.set_userdata(&state);
machine.set_printer([] (const auto& m, const char* data, size_t size) {
auto* state = m.template get_userdata<State> ();
std::string text{data, data + size};
state->output_is_hello_world = (text == "hello world");
});
// Run for at most X instructions before giving up
machine.simulate(MAX_INSTRUCTIONS);
REQUIRE(machine.return_value() == 0);
REQUIRE(state.output_is_hello_world);
}
TEST_CASE("Zig Hello World", "[Verify]")
{
const auto binary = load_file(cwd + "/elf/zig-riscv64-hello-world");
riscv::Machine<RISCV64> machine { binary, { .memory_max = MAX_MEMORY } };
// Install Linux system calls
machine.setup_linux_syscalls();
// Create a Linux environment for runtimes to work well
machine.setup_linux(
{"zig-riscv64-hello-world"},
{"LC_TYPE=C", "LC_ALL=C", "USER=root"});
struct State {
std::string text;
} state;
machine.set_userdata(&state);
machine.set_printer([] (const auto& m, const char* data, size_t size) {
auto* state = m.template get_userdata<State> ();
state->text.append(data, data + size);
});
// Run for at most X instructions before giving up
machine.simulate(MAX_INSTRUCTIONS);
REQUIRE(machine.return_value() == 0);
REQUIRE(state.text == "Hello, world!\n");
}
TEST_CASE("Rust Hello World", "[Verify]")
{
const auto binary = load_file(cwd + "/elf/rust-riscv64-hello-world");
riscv::Machine<RISCV64> machine { binary, { .memory_max = MAX_MEMORY } };
// Install Linux system calls
machine.setup_linux_syscalls();
// Create a Linux environment for runtimes to work well
machine.setup_linux(
{"rust-riscv64-hello-world"},
{"LC_TYPE=C", "LC_ALL=C", "USER=root"});
struct State {
std::string text;
} state;
machine.set_userdata(&state);
machine.set_printer([] (const auto& m, const char* data, size_t size) {
auto* state = m.template get_userdata<State> ();
state->text.append(data, data + size);
});
// Run for at most X instructions before giving up
machine.simulate(MAX_INSTRUCTIONS);
REQUIRE(machine.return_value() == 0);
REQUIRE(state.text == "Hello World!\n");
}
TEST_CASE("RV32 Newlib with B-ext Hello World", "[Verify]")
{
const auto binary = load_file(cwd + "/elf/newlib-rv32gb-hello-world");
riscv::Machine<RISCV32> machine { binary, { .memory_max = MAX_MEMORY } };
// Install Linux system calls
machine.setup_linux_syscalls();
// Create a Linux environment for runtimes to work well
machine.setup_linux(
{"newlib-rv32gb-hello-world"},
{"LC_TYPE=C", "LC_ALL=C", "USER=root"});
struct State {
std::string text;
} state;
machine.set_userdata(&state);
machine.set_printer([] (const auto& m, const char* data, size_t size) {
auto* state = m.template get_userdata<State> ();
state->text.append(data, data + size);
});
// Run for at most X instructions before giving up
machine.simulate(MAX_INSTRUCTIONS);
// Sadly, main() returns int which gets sign-extended to all bits set
// which kind of defeats the purpose of testing ROL... oh well.
REQUIRE(machine.return_value() == 666);
REQUIRE(state.text.find("[confronted]") != std::string::npos);
REQUIRE(state.text.find("[problem]") != std::string::npos);
REQUIRE(state.text.find("[regular]") != std::string::npos);
REQUIRE(state.text.find("[expressions]") != std::string::npos);
REQUIRE(state.text.find("[problems]") != std::string::npos);
REQUIRE(state.text.find("Caught exception: Hello Exceptions!") != std::string::npos);
}
TEST_CASE("RV64 Newlib with B-ext Hello World", "[Verify]")
{
const auto binary = load_file(cwd + "/elf/newlib-rv64gb-hello-world");
riscv::Machine<RISCV64> machine { binary, { .memory_max = MAX_MEMORY } };
// Install Linux system calls
machine.setup_linux_syscalls();
// Create a Linux environment for runtimes to work well
machine.setup_linux(
{"newlib-rv64gb-hello-world"},
{"LC_TYPE=C", "LC_ALL=C", "USER=root"});
struct State {
std::string text;
} state;
machine.set_userdata(&state);
machine.set_printer([] (const auto& m, const char* data, size_t size) {
auto* state = m.template get_userdata<State> ();
state->text.append(data, data + size);
});
// Run for at most X instructions before giving up
machine.simulate(MAX_INSTRUCTIONS);
// Sadly, main() returns int which gets sign-extended to all bits set
// which kind of defeats the purpose of testing ROL... oh well.
REQUIRE(machine.return_value() == 666);
REQUIRE(state.text.find("[confronted]") != std::string::npos);
REQUIRE(state.text.find("[problem]") != std::string::npos);
REQUIRE(state.text.find("[regular]") != std::string::npos);
REQUIRE(state.text.find("[expressions]") != std::string::npos);
REQUIRE(state.text.find("[problems]") != std::string::npos);
REQUIRE(state.text.find("Caught exception: Hello Exceptions!") != std::string::npos);
}
TEST_CASE("TinyCC dynamic fib", "[Verify]")
{
const auto binary = load_file(cwd + "/elf/tinycc-rv64g-fib");
riscv::Machine<RISCV64> machine { binary, { .memory_max = MAX_MEMORY } };
// Install Linux system calls
machine.setup_linux_syscalls();
// Create a Linux environment for runtimes to work well
machine.setup_linux(
{"tinycc-rv64g-fib"},
{"LC_TYPE=C", "LC_ALL=C", "USER=groot"});
// Run for at most X instructions before giving up
machine.simulate(MAX_INSTRUCTIONS);
REQUIRE(machine.return_value() == 75025); // fib(25)
}
TEST_CASE("RV32 Execute-Only Hello World", "[Verify]")
{
const auto binary = load_file(cwd + "/elf/riscv32gb-execute-only");
constexpr uint64_t MAX_MEMORY = 128ul << 20; /* 128MB */
riscv::Machine<RISCV32> machine{binary, {
.memory_max = MAX_MEMORY,
.enforce_exec_only = true,
}};
machine.setup_newlib_syscalls();
machine.setup_argv(
{"riscv32gb-execute-only"}, {});
machine.setup_native_heap(580,
machine.memory.mmap_allocate(0x1800000), 0x1800000);
machine.setup_native_memory(585);
struct State {
std::string text;
} state;
machine.set_userdata(&state);
machine.install_syscall_handler(502,
[] (auto& machine) {
auto [buf, count] = machine.template sysargs<uint32_t, uint32_t>();
auto view = machine.memory.rvview(buf, count);
printf("%.*s", (int) view.size(), view.data());
auto* state = machine.template get_userdata<State>();
state->text.append((const char*) view.data(), view.size());
});
machine.simulate(MAX_INSTRUCTIONS);
REQUIRE(machine.return_value() == 123);
REQUIRE(state.text == "Hello, World!");
}