forked from M-Labs/artiq
1
0
Fork 0

firmware/ksupport: add exception unittests

This commit is contained in:
abdul124 2024-08-12 12:59:06 +08:00 committed by Sébastien Bourdeauducq
parent 09128f87e6
commit cd4a0bb39e
2 changed files with 29 additions and 1 deletions

View File

@ -173,4 +173,11 @@ static mut API: &'static [(&'static str, *const ())] = &[
api!(spi_set_config = ::nrt_bus::spi::set_config), api!(spi_set_config = ::nrt_bus::spi::set_config),
api!(spi_write = ::nrt_bus::spi::write), api!(spi_write = ::nrt_bus::spi::write),
api!(spi_read = ::nrt_bus::spi::read), api!(spi_read = ::nrt_bus::spi::read),
/*
* syscall for unit tests
* Used in `artiq.tests.coredevice.test_exceptions.ExceptionTest.test_raise_exceptions_kernel`
* `EmbeddingMap` (`artiq.compiler.embedding`) and `EXCEPTION_ID_LOOKUP` (`artiq.firmware.ksupport.eh_artiq`)
*/
api!(test_exception_id_sync = ::eh_artiq::test_exception_id_sync)
]; ];

View File

@ -328,7 +328,7 @@ extern fn stop_fn(_version: c_int,
} }
} }
// Must be kept in sync with `artq.compiler.embedding` // Must be kept in sync with `artiq.compiler.embedding`
static EXCEPTION_ID_LOOKUP: [(&str, u32); 20] = [ static EXCEPTION_ID_LOOKUP: [(&str, u32); 20] = [
("RTIOUnderflow", 0), ("RTIOUnderflow", 0),
("RTIOOverflow", 1), ("RTIOOverflow", 1),
@ -361,3 +361,24 @@ pub fn get_exception_id(name: &str) -> u32 {
unimplemented!("unallocated internal exception id") unimplemented!("unallocated internal exception id")
} }
// Performs a reverse lookup on `EXCEPTION_ID_LOOKUP`
// Unconditionally raises an exception given its id
#[no_mangle]
pub extern "C-unwind" fn test_exception_id_sync(exn_id: u32) {
let message = EXCEPTION_ID_LOOKUP
.iter()
.find_map(|&(name, id)| if id == exn_id { Some(name) } else { None })
.unwrap_or("Unknown exception ID");
let exn = Exception {
id: exn_id,
file: file!().as_c_slice(),
line: 0,
column: 0,
function: "test_exception_id_sync".as_c_slice(),
message: message.as_c_slice(),
param: [0, 0, 0]
};
unsafe { raise(&exn) };
}