From 62ccad7f178cea26ae7a7b96728d40c23fd4efdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20L=C3=B3pez?= Date: Wed, 13 Sep 2023 16:30:16 +0200 Subject: [PATCH] test: fix doctests and run in Makefile target MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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/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/svsm#93 Fixes: 8cdea8e2a215 ("Cargo manifest: disable doctests") Signed-off-by: Carlos López --- Makefile | 1 + src/mm/alloc.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 127e6b638..0cefcc2e1 100644 --- a/Makefile +++ b/Makefile @@ -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 $@ $< diff --git a/src/mm/alloc.rs b/src/mm/alloc.rs index 6b3cf6339..6e0d4ae1d 100644 --- a/src/mm/alloc.rs +++ b/src/mm/alloc.rs @@ -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) {