Skip to content

Commit 651b3ca

Browse files
committed
test: fix doctests and run in Makefile target
Our custom bare-metal allocator (`SvsmAllocator`) does not work during tests, as they run as regular userspace binaries. Thus, we must not set it as the global Rust allocator. To do this, we need a compile-time directive that lets us know we are building doctests and not the regular binary (`cfg(doctests)`), but sadly it is not properly set during test compilation (see rust-lang/rust#45599, rust-lang/rust#67295 and coconut-svsm#93). In order to bypass this, set the compile time flag ourselves via the RUSTFLAGS environment variable so that tests work. Also add the new invocation to the `test` target in the Makefile. Fixes: coconut-svsm#93 Fixes: 8cdea8e ("Cargo manifest: disable doctests") Signed-off-by: Carlos López <[email protected]>
1 parent 5ced958 commit 651b3ca

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ all: svsm.bin
1818

1919
test:
2020
cargo test --target=x86_64-unknown-linux-gnu
21+
RUSTFLAGS="--cfg doctest" cargo test --target=x86_64-unknown-linux-gnu --doc
2122

2223
utils/gen_meta: utils/gen_meta.c
2324
cc -O3 -Wall -o $@ $<

src/mm/alloc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ unsafe impl GlobalAlloc for SvsmAllocator {
12821282
}
12831283
}
12841284

1285-
#[cfg_attr(not(test), global_allocator)]
1285+
#[cfg_attr(not(any(test, doctest)), global_allocator)]
12861286
pub static mut ALLOCATOR: SvsmAllocator = SvsmAllocator::new();
12871287

12881288
pub fn root_mem_init(pstart: PhysAddr, vstart: VirtAddr, page_count: usize) {

0 commit comments

Comments
 (0)