Skip to content

Commit

Permalink
test: fix doctests and run in Makefile target
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
00xc committed Sep 13, 2023
1 parent 5ced958 commit 651b3ca
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ all: svsm.bin

test:
cargo test --target=x86_64-unknown-linux-gnu
RUSTFLAGS="--cfg doctest" cargo test --target=x86_64-unknown-linux-gnu --doc

utils/gen_meta: utils/gen_meta.c
cc -O3 -Wall -o $@ $<
Expand Down
2 changes: 1 addition & 1 deletion src/mm/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ unsafe impl GlobalAlloc for SvsmAllocator {
}
}

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

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

0 comments on commit 651b3ca

Please sign in to comment.