Introduced kernel testing system ktest
This commit also fixed a small issue with panic handler formatting
This commit is contained in:
parent
ab8b2309ae
commit
80ae717dd9
9
Cargo.lock
generated
9
Cargo.lock
generated
|
@ -392,6 +392,7 @@ dependencies = [
|
||||||
"derive_more",
|
"derive_more",
|
||||||
"hashbrown",
|
"hashbrown",
|
||||||
"hbvm",
|
"hbvm",
|
||||||
|
"ktest_macro",
|
||||||
"limine",
|
"limine",
|
||||||
"log",
|
"log",
|
||||||
"sbi",
|
"sbi",
|
||||||
|
@ -404,6 +405,14 @@ dependencies = [
|
||||||
"xml",
|
"xml",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ktest_macro"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
version = "1.5.0"
|
version = "1.5.0"
|
||||||
|
|
|
@ -3,12 +3,15 @@ edition = "2021"
|
||||||
name = "kernel"
|
name = "kernel"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
ktest = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# embedded-graphics = "0.8"
|
# embedded-graphics = "0.8"
|
||||||
hbvm = { git = "https://git.ablecorp.us/AbleOS/holey-bytes.git", features = [
|
hbvm = { git = "https://git.ablecorp.us/AbleOS/holey-bytes.git", features = [
|
||||||
"nightly",
|
"nightly",
|
||||||
] }
|
] }
|
||||||
|
ktest_macro = { path = "ktest_macro" }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
spin = "0.9"
|
spin = "0.9"
|
||||||
slab = { version = "0.4", default-features = false }
|
slab = { version = "0.4", default-features = false }
|
||||||
|
|
11
kernel/ktest_macro/Cargo.toml
Normal file
11
kernel/ktest_macro/Cargo.toml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
[package]
|
||||||
|
edition = "2021"
|
||||||
|
name = "ktest_macro"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
proc-macro = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
quote = "1.0.37"
|
||||||
|
syn = { version = "2.0.89", features = ["full"] }
|
29
kernel/ktest_macro/src/lib.rs
Normal file
29
kernel/ktest_macro/src/lib.rs
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
extern crate proc_macro;
|
||||||
|
extern crate quote;
|
||||||
|
extern crate syn;
|
||||||
|
use {
|
||||||
|
proc_macro::TokenStream,
|
||||||
|
quote::quote,
|
||||||
|
syn::{parse_macro_input, ItemFn}
|
||||||
|
};
|
||||||
|
|
||||||
|
#[proc_macro_attribute]
|
||||||
|
pub fn ktest(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
|
let input = parse_macro_input!(item as ItemFn);
|
||||||
|
let test_name = &input.sig.ident;
|
||||||
|
let static_var_name = syn::Ident::new(
|
||||||
|
&format!("__ktest_{}", test_name),
|
||||||
|
test_name.span(),
|
||||||
|
);
|
||||||
|
let out = quote! {
|
||||||
|
// #[cfg(feature = "ktest")]
|
||||||
|
#input
|
||||||
|
|
||||||
|
// #[cfg(feature = "ktest")]
|
||||||
|
#[unsafe(link_section = ".note.ktest")]
|
||||||
|
#[used]
|
||||||
|
pub static #static_var_name: fn() = #test_name;
|
||||||
|
};
|
||||||
|
|
||||||
|
TokenStream::from(out)
|
||||||
|
}
|
|
@ -41,6 +41,13 @@ SECTIONS
|
||||||
*(.got .got.*)
|
*(.got .got.*)
|
||||||
} :data
|
} :data
|
||||||
|
|
||||||
|
/* Add the .ktest section for test functions */
|
||||||
|
.note.ktest : {
|
||||||
|
__ktest_start = .; /* Mark the beginning of the section */
|
||||||
|
*(.note.ktest) /* Include all items in the .ktest section */
|
||||||
|
__ktest_end = .; /* Mark the end of the section */
|
||||||
|
}
|
||||||
|
|
||||||
.bss : {
|
.bss : {
|
||||||
*(COMMON)
|
*(COMMON)
|
||||||
*(.bss .bss.*)
|
*(.bss .bss.*)
|
||||||
|
|
|
@ -22,6 +22,14 @@ use {
|
||||||
pub fn kmain(_cmdline: &str, boot_modules: BootModules) -> ! {
|
pub fn kmain(_cmdline: &str, boot_modules: BootModules) -> ! {
|
||||||
debug!("Entered kmain");
|
debug!("Entered kmain");
|
||||||
|
|
||||||
|
#[cfg(feature = "ktest")] {
|
||||||
|
use crate::ktest;
|
||||||
|
debug!("TESTING");
|
||||||
|
ktest::test_main();
|
||||||
|
|
||||||
|
loop {};
|
||||||
|
}
|
||||||
|
|
||||||
// let kcmd = build_cmd("Kernel Command Line", cmdline);
|
// let kcmd = build_cmd("Kernel Command Line", cmdline);
|
||||||
// trace!("Cmdline: {kcmd:?}");
|
// trace!("Cmdline: {kcmd:?}");
|
||||||
|
|
||||||
|
@ -148,10 +156,3 @@ pub static IPC_BUFFERS: Lazy<Mutex<IpcBuffers>> = Lazy::new(|| {
|
||||||
|
|
||||||
Mutex::new(bufs)
|
Mutex::new(bufs)
|
||||||
});
|
});
|
||||||
|
|
||||||
#[test_case]
|
|
||||||
fn trivial_assertion() {
|
|
||||||
trace!("trivial assertion... ");
|
|
||||||
assert_eq!(1, 1);
|
|
||||||
info!("[ok]");
|
|
||||||
}
|
|
||||||
|
|
38
kernel/src/ktest.rs
Normal file
38
kernel/src/ktest.rs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
pub use ktest_macro::ktest;
|
||||||
|
use log::debug;
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
static __ktest_start: fn();
|
||||||
|
static __ktest_end: fn();
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Get test_fn linker name (may require no_mangle in macro)
|
||||||
|
// More info on tests (run the rest even if panic)
|
||||||
|
// Implement ktest for arm and riscv (Later problems, see below)
|
||||||
|
// Allow for arch specific tests (Leave for now)
|
||||||
|
// Allow for ktest test name attr
|
||||||
|
// Usefull message at the end of testing
|
||||||
|
pub fn test_main() {
|
||||||
|
unsafe {
|
||||||
|
let mut current_test = &__ktest_start as *const fn();
|
||||||
|
let mut current = 1;
|
||||||
|
let test_end = &__ktest_end as *const fn();
|
||||||
|
|
||||||
|
while current_test < test_end {
|
||||||
|
let test_fn = *current_test;
|
||||||
|
|
||||||
|
debug!("Running test {}", current);
|
||||||
|
|
||||||
|
test_fn();
|
||||||
|
debug!("Test {} passed", current);
|
||||||
|
|
||||||
|
current_test = current_test.add(1);
|
||||||
|
current += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[ktest]
|
||||||
|
pub fn trivial_assertion() {
|
||||||
|
assert_eq!(1, 1);
|
||||||
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
//! Named akern.
|
//! Named akern.
|
||||||
//! Akern is woefully undersupported at the moment but we are looking to add support improve hardware discovery and make our lives as kernel and operating system developers easier and better
|
//! Akern is woefully undersupported at the moment but we are looking to add support improve hardware discovery and make our lives as kernel and operating system developers easier and better
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
#![feature(
|
#![feature(
|
||||||
slice_split_once,
|
slice_split_once,
|
||||||
exclusive_wrapper,
|
exclusive_wrapper,
|
||||||
|
@ -10,11 +11,9 @@
|
||||||
lazy_get,
|
lazy_get,
|
||||||
alloc_error_handler,
|
alloc_error_handler,
|
||||||
ptr_sub_ptr,
|
ptr_sub_ptr,
|
||||||
custom_test_frameworks,
|
|
||||||
naked_functions,
|
naked_functions,
|
||||||
pointer_is_aligned_to
|
pointer_is_aligned_to,
|
||||||
)]
|
)]
|
||||||
#![test_runner(crate::test_runner)]
|
|
||||||
#![allow(dead_code, internal_features, static_mut_refs)]
|
#![allow(dead_code, internal_features, static_mut_refs)]
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
|
@ -33,6 +32,10 @@ mod memory;
|
||||||
mod task;
|
mod task;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
|
// #[cfg(feature = "tests")]
|
||||||
|
mod ktest;
|
||||||
|
|
||||||
|
use alloc::string::ToString;
|
||||||
use versioning::Version;
|
use versioning::Version;
|
||||||
|
|
||||||
/// Kernel's version
|
/// Kernel's version
|
||||||
|
@ -56,15 +59,7 @@ fn panic(info: &core::panic::PanicInfo) -> ! {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let msg = info.message();
|
let msg = info.message().to_string().replace("\n", "\r\n");
|
||||||
let _ = crate::arch::log(format_args!("{msg}\r\n"));
|
let _ = crate::arch::log(format_args!("{msg}\r\n"));
|
||||||
loop {}
|
loop {}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
fn test_runner(tests: &[&dyn Fn()]) {
|
|
||||||
println!("Running {} tests", tests.len());
|
|
||||||
for test in tests {
|
|
||||||
test();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -27,6 +27,7 @@ fn main() -> Result<(), Error> {
|
||||||
let mut release = false;
|
let mut release = false;
|
||||||
let mut debuginfo = false;
|
let mut debuginfo = false;
|
||||||
let mut target = Target::X86_64;
|
let mut target = Target::X86_64;
|
||||||
|
let mut tests = false;
|
||||||
for arg in args {
|
for arg in args {
|
||||||
if arg == "-r" || arg == "--release" {
|
if arg == "-r" || arg == "--release" {
|
||||||
release = true;
|
release = true;
|
||||||
|
@ -38,17 +39,42 @@ fn main() -> Result<(), Error> {
|
||||||
target = Target::Aarch64;
|
target = Target::Aarch64;
|
||||||
} else if arg == "avx2" {
|
} else if arg == "avx2" {
|
||||||
target = Target::X86_64Avx2;
|
target = Target::X86_64Avx2;
|
||||||
|
} else if arg == "--ktest" {
|
||||||
|
tests = true;
|
||||||
} else {
|
} else {
|
||||||
return Err(report!(Error::InvalidSubCom));
|
return Err(report!(Error::InvalidSubCom));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
build(release, target, debuginfo).change_context(Error::Build)
|
build(release, target, debuginfo, tests).change_context(Error::Build)
|
||||||
}
|
}
|
||||||
|
// Some("test" | "t") => {
|
||||||
|
// let mut release = false;
|
||||||
|
// let mut debuginfo = false;
|
||||||
|
// let mut target = Target::X86_64;
|
||||||
|
// for arg in args {
|
||||||
|
// if arg == "-r" || arg == "--release" {
|
||||||
|
// release = true;
|
||||||
|
// } else if arg == "-d" || arg == "--debuginfo" {
|
||||||
|
// debuginfo = true;
|
||||||
|
// } else if arg == "rv64" || arg == "riscv64" || arg == "riscv64-virt" {
|
||||||
|
// target = Target::Riscv64Virt;
|
||||||
|
// } else if arg == "arm64" || arg == "aarch64" || arg == "aarch64-virt" {
|
||||||
|
// target = Target::Aarch64;
|
||||||
|
// } else if arg == "avx2" {
|
||||||
|
// target = Target::X86_64Avx2;
|
||||||
|
// } else {
|
||||||
|
// return Err(report!(Error::InvalidSubCom));
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// test(release, target, debuginfo).change_context(Error::Build)
|
||||||
|
// }
|
||||||
Some("run" | "r") => {
|
Some("run" | "r") => {
|
||||||
let mut release = false;
|
let mut release = false;
|
||||||
let mut debuginfo = false;
|
let mut debuginfo = false;
|
||||||
let mut target = Target::X86_64;
|
let mut target = Target::X86_64;
|
||||||
|
let mut tests = false;
|
||||||
let mut do_accel = true;
|
let mut do_accel = true;
|
||||||
for arg in args {
|
for arg in args {
|
||||||
if arg == "-r" || arg == "--release" {
|
if arg == "-r" || arg == "--release" {
|
||||||
|
@ -63,12 +89,14 @@ fn main() -> Result<(), Error> {
|
||||||
do_accel = false;
|
do_accel = false;
|
||||||
} else if arg == "avx2" {
|
} else if arg == "avx2" {
|
||||||
target = Target::X86_64Avx2;
|
target = Target::X86_64Avx2;
|
||||||
|
} else if arg == "--ktest" {
|
||||||
|
tests = true;
|
||||||
} else {
|
} else {
|
||||||
return Err(report!(Error::InvalidSubCom));
|
return Err(report!(Error::InvalidSubCom));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
build(release, target, debuginfo)?;
|
build(release, target, debuginfo, tests)?;
|
||||||
run(release, target, do_accel)
|
run(release, target, do_accel)
|
||||||
}
|
}
|
||||||
Some("help" | "h") => {
|
Some("help" | "h") => {
|
||||||
|
@ -82,6 +110,7 @@ fn main() -> Result<(), Error> {
|
||||||
" -r / --release: build in release mode\n",
|
" -r / --release: build in release mode\n",
|
||||||
" -d / --debuginfo: build with debug info\n",
|
" -d / --debuginfo: build with debug info\n",
|
||||||
" --noaccel: run without acceleration (e.g, no kvm)\n",
|
" --noaccel: run without acceleration (e.g, no kvm)\n",
|
||||||
|
" --ktest: Enables tests via ktest\n",
|
||||||
"[ rv64 / riscv64 / riscv64-virt / aarch64 / arm64 / aarch64-virt / avx2 ]: sets target"
|
"[ rv64 / riscv64 / riscv64-virt / aarch64 / arm64 / aarch64-virt / avx2 ]: sets target"
|
||||||
),);
|
),);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -310,7 +339,7 @@ fn copy_file_to_img(fpath: &str, fs: &FileSystem<File>) {
|
||||||
.expect("Copy failed");
|
.expect("Copy failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build(release: bool, target: Target, debuginfo: bool) -> Result<(), Error> {
|
fn build(release: bool, target: Target, debuginfo: bool, tests: bool) -> Result<(), Error> {
|
||||||
let fs = get_fs().change_context(Error::Io)?;
|
let fs = get_fs().change_context(Error::Io)?;
|
||||||
let mut com = Command::new("cargo");
|
let mut com = Command::new("cargo");
|
||||||
com.current_dir("kernel");
|
com.current_dir("kernel");
|
||||||
|
@ -322,6 +351,10 @@ fn build(release: bool, target: Target, debuginfo: bool) -> Result<(), Error> {
|
||||||
com.env("RUSTFLAGS", "-Cdebug-assertions=true");
|
com.env("RUSTFLAGS", "-Cdebug-assertions=true");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if tests {
|
||||||
|
com.args(["--features", "ktest"]);
|
||||||
|
}
|
||||||
|
|
||||||
if target == Target::Riscv64Virt {
|
if target == Target::Riscv64Virt {
|
||||||
com.args(["--target", "targets/riscv64-virt-ableos.json"]);
|
com.args(["--target", "targets/riscv64-virt-ableos.json"]);
|
||||||
}
|
}
|
||||||
|
@ -473,6 +506,7 @@ fn run(release: bool, target: Target, do_accel: bool) -> Result<(), Error> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn fetch_ovmf(target: Target) -> Result<String, OvmfFetchError> {
|
fn fetch_ovmf(target: Target) -> Result<String, OvmfFetchError> {
|
||||||
let (ovmf_url, ovmf_path) = match target {
|
let (ovmf_url, ovmf_path) = match target {
|
||||||
Target::X86_64 | Target::X86_64Avx2 => (
|
Target::X86_64 | Target::X86_64Avx2 => (
|
||||||
|
|
Loading…
Reference in a new issue