Skip to content

Commit 6e067da

Browse files
committed
Add Rust ELF unit test
1 parent b0b5d5c commit 6e067da

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

tests/unit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function(add_unit_test NAME)
2121
endfunction()
2222

2323
add_unit_test(basic basic.cpp)
24+
add_unit_test(elf elf.cpp)
2425
add_unit_test(fork fork.cpp)
2526
add_unit_test(mmap mmap.cpp)
2627
add_unit_test(remote remote.cpp)

tests/unit/elf.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include <tinykvm/machine.hpp>
4+
extern std::vector<uint8_t> load_file(const std::string& filename);
5+
static const uint64_t MAX_MEMORY = 8ul << 20; /* 8MB */
6+
static const std::vector<std::string> env{
7+
"LC_TYPE=C", "LC_ALL=C", "USER=root"};
8+
9+
TEST_CASE("Initialize KVM", "[Initialize]")
10+
{
11+
// Create KVM file descriptors etc.
12+
tinykvm::Machine::init();
13+
}
14+
15+
TEST_CASE("Verify Rust ELF", "[ELF]")
16+
{
17+
const auto binary = load_file("../unit/elf/rust.elf");
18+
19+
tinykvm::Machine machine { binary, { .max_mem = MAX_MEMORY } };
20+
// We need to create a Linux environment for runtimes to work well
21+
machine.setup_linux({"verify"}, env);
22+
23+
// Run for at most 4 seconds before giving up
24+
machine.run(4.0f);
25+
26+
REQUIRE(machine.return_value() == 231);
27+
}
28+
29+
TEST_CASE("Verify Rust ELF (himem)", "[ELF]")
30+
{
31+
const auto binary = load_file("../unit/elf/rust.elf");
32+
33+
const uint64_t HIMEM = 128ULL << 30; /* 128GB */
34+
tinykvm::Machine machine{binary, {
35+
.max_mem = MAX_MEMORY,
36+
.dylink_address_hint = HIMEM + 0x200000,
37+
.vmem_base_address = HIMEM
38+
}};
39+
// We need to create a Linux environment for runtimes to work well
40+
machine.setup_linux({"verify"}, env);
41+
REQUIRE(machine.entry_address() > HIMEM);
42+
43+
// Run for at most 4 seconds before giving up
44+
machine.run(4.0f);
45+
46+
REQUIRE(machine.return_value() == 231);
47+
}

tests/unit/elf/rust.elf

1.27 MB
Binary file not shown.

0 commit comments

Comments
 (0)