Skip to content

Commit

Permalink
Must define cfi_panic_handler and CFI_STATE_ORG in C code
Browse files Browse the repository at this point in the history
Frankly, I don't know how the code was compiling before, but I suppose
Rust must have either accidentally left the symbols in the `.a` file?

Also we need to pass non-nil ICCM and DCCM pointers in Go code to Rust,
as Rust now will panic if nil slices are constructed, even if they have
length 0.
  • Loading branch information
swenson authored and ArthurHeymans committed Feb 25, 2025
1 parent de16696 commit 5a81673
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion hw-model/c-binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
caliptra-cfi-lib.workspace = true
caliptra-emu-bus.workspace = true
caliptra-emu-types.workspace = true
caliptra-hw-model.workspace = true
Expand All @@ -19,4 +20,4 @@ itrng = ["caliptra-hw-model/itrng"]
verilator = ["caliptra-hw-model/verilator"]

[build-dependencies]
cbindgen.workspace = true
cbindgen.workspace = true
12 changes: 12 additions & 0 deletions hw-model/c-binding/src/caliptra_model.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
// Licensed under the Apache-2.0 license

use caliptra_api::soc_mgr::SocManager;
use caliptra_cfi_lib::CfiState;
use caliptra_emu_bus::Bus;
use caliptra_hw_model::{DefaultHwModel, HwModel, InitParams, SecurityState};
use std::ffi::*;
use std::slice;

use caliptra_emu_types::RvSize;

// These are needed if CFI is enabled.
#[no_mangle]
pub extern "C" fn cfi_panic_handler(code: u32) -> ! {
std::process::exit(code as i32);
}

#[allow(unused)]
#[no_mangle]
static mut CFI_STATE_ORG: [u8; std::mem::size_of::<CfiState>()] =
[0; std::mem::size_of::<CfiState>()];

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct caliptra_model {
Expand Down
11 changes: 10 additions & 1 deletion test/dpe_verification/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ static int set_fuses()
}
return status;
}
*/
import "C"

Expand Down Expand Up @@ -111,6 +110,16 @@ func getHWModel() *C.struct_caliptra_model {
params.rom.data = (*C.uchar)(cRom)
params.rom.len = C.uintptr_t(len(rom))

// use a dummy value for iccm and dccm because they have to be non-nil
iccm := C.CBytes([]byte{0})
defer C.free(iccm)
params.iccm.data = (*C.uchar)(iccm)
params.iccm.len = C.uintptr_t(0)
dccm := C.CBytes([]byte{0})
defer C.free(dccm)
params.dccm.data = (*C.uchar)(dccm)
params.dccm.len = C.uintptr_t(0)

status := C.caliptra_model_init_default(params, &CaliptraCModel)
if status != 0 {
panic("Failed to initialize caliptra model")
Expand Down

0 comments on commit 5a81673

Please sign in to comment.