Skip to content

Commit b53e5ee

Browse files
committed
hyperlight-capi
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 350aeef commit b53e5ee

File tree

6 files changed

+30
-26
lines changed

6 files changed

+30
-26
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hyperlight_guest_capi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ workspace = true
1515
hyperlight-guest = { workspace = true, default-features = true }
1616
hyperlight-common = { workspace = true, default-features = false }
1717
log = { version = "0.4", default-features = false }
18+
spin = "0.9.8"
1819

1920
[build-dependencies]
2021
cbindgen = "0.28.0"

src/hyperlight_guest_capi/src/dispatch.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,25 @@ use hyperlight_guest::error::{HyperlightGuestError, Result};
1111
use hyperlight_guest::guest_function_definition::GuestFunctionDefinition;
1212
use hyperlight_guest::guest_function_register::GuestFunctionRegister;
1313
use hyperlight_guest::host_function_call::call_host_function;
14+
use spin::Mutex;
1415

1516
use crate::types::{FfiFunctionCall, FfiVec};
16-
static mut REGISTERED_C_GUEST_FUNCTIONS: GuestFunctionRegister = GuestFunctionRegister::new();
17+
static REGISTERED_C_GUEST_FUNCTIONS: Mutex<GuestFunctionRegister> =
18+
Mutex::new(GuestFunctionRegister::new());
1719

1820
type CGuestFunc = extern "C" fn(&FfiFunctionCall) -> Box<FfiVec>;
1921

20-
extern "C" {
22+
unsafe extern "C" {
2123
// NOTE *mut FfiVec must be a Box<FfiVec>. This will be the case as long as the guest
2224
// returns a FfiVec that they created using the c-api hl_flatbuffer_result_from_* functions.
2325
fn c_guest_dispatch_function(function_call: &FfiFunctionCall) -> *mut FfiVec;
2426
}
2527

26-
#[no_mangle]
28+
#[unsafe(no_mangle)]
2729
pub fn guest_dispatch_function(function_call: FunctionCall) -> Result<Vec<u8>> {
28-
if let Some(registered_func) =
29-
unsafe { REGISTERED_C_GUEST_FUNCTIONS.get(&function_call.function_name) }
30+
if let Some(registered_func) = REGISTERED_C_GUEST_FUNCTIONS
31+
.lock()
32+
.get(&function_call.function_name)
3033
{
3134
let function_call_parameter_types: Vec<ParameterType> = function_call
3235
.parameters
@@ -64,7 +67,7 @@ pub fn guest_dispatch_function(function_call: FunctionCall) -> Result<Vec<u8>> {
6467
}
6568
}
6669

67-
#[no_mangle]
70+
#[unsafe(no_mangle)]
6871
pub extern "C" fn hl_register_function_definition(
6972
function_name: *const c_char,
7073
func_ptr: CGuestFunc,
@@ -79,12 +82,11 @@ pub extern "C" fn hl_register_function_definition(
7982
let func_def =
8083
GuestFunctionDefinition::new(func_name, func_params, return_type, func_ptr as usize);
8184

82-
#[allow(static_mut_refs)]
83-
unsafe { &mut REGISTERED_C_GUEST_FUNCTIONS }.register(func_def);
85+
REGISTERED_C_GUEST_FUNCTIONS.lock().register(func_def);
8486
}
8587

8688
/// The caller is responsible for freeing the memory associated with given `FfiFunctionCall`.
87-
#[no_mangle]
89+
#[unsafe(no_mangle)]
8890
pub extern "C" fn hl_call_host_function(function_call: &FfiFunctionCall) {
8991
let parameters = unsafe { function_call.copy_parameters() };
9092
let func_name = unsafe { function_call.copy_function_name() };

src/hyperlight_guest_capi/src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ use core::ffi::c_char;
33
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
44
use hyperlight_guest::guest_error::setError;
55

6-
#[no_mangle]
6+
#[unsafe(no_mangle)]
77
pub extern "C" fn hl_set_error(err: ErrorCode, message: *const c_char) {
88
unsafe {
99
setError(err.into(), message);
1010
}
1111
}
1212

13-
#[no_mangle]
13+
#[unsafe(no_mangle)]
1414
pub extern "C" fn hl_abort_with_code(err: i32) {
1515
hyperlight_guest::entrypoint::abort_with_code(err);
1616
}
1717

18-
#[no_mangle]
18+
#[unsafe(no_mangle)]
1919
pub extern "C" fn hl_abort_with_code_and_message(err: i32, message: *const c_char) {
2020
unsafe { hyperlight_guest::entrypoint::abort_with_code_and_message(err, message) };
2121
}

src/hyperlight_guest_capi/src/flatbuffer.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,64 +10,64 @@ use crate::types::FfiVec;
1010
// is to match the names of the variants in hl_ReturnType,
1111
// which is used in the C macros in macro.h
1212

13-
#[no_mangle]
13+
#[unsafe(no_mangle)]
1414
pub extern "C" fn hl_flatbuffer_result_from_Int(value: i32) -> Box<FfiVec> {
1515
let vec = get_flatbuffer_result(value);
1616

1717
Box::new(unsafe { FfiVec::from_vec(vec) })
1818
}
1919

20-
#[no_mangle]
20+
#[unsafe(no_mangle)]
2121
pub extern "C" fn hl_flatbuffer_result_from_UInt(value: u32) -> Box<FfiVec> {
2222
let vec = get_flatbuffer_result(value);
2323

2424
Box::new(unsafe { FfiVec::from_vec(vec) })
2525
}
2626

27-
#[no_mangle]
27+
#[unsafe(no_mangle)]
2828
pub extern "C" fn hl_flatbuffer_result_from_Long(value: i64) -> Box<FfiVec> {
2929
let vec = get_flatbuffer_result(value);
3030

3131
Box::new(unsafe { FfiVec::from_vec(vec) })
3232
}
3333

34-
#[no_mangle]
34+
#[unsafe(no_mangle)]
3535
pub extern "C" fn hl_flatbuffer_result_from_ULong(value: u64) -> Box<FfiVec> {
3636
let vec = get_flatbuffer_result(value);
3737

3838
Box::new(unsafe { FfiVec::from_vec(vec) })
3939
}
4040

41-
#[no_mangle]
41+
#[unsafe(no_mangle)]
4242
pub extern "C" fn hl_flatbuffer_result_from_Float(value: f32) -> Box<FfiVec> {
4343
let vec = get_flatbuffer_result(value);
4444

4545
Box::new(unsafe { FfiVec::from_vec(vec) })
4646
}
4747

48-
#[no_mangle]
48+
#[unsafe(no_mangle)]
4949
pub extern "C" fn hl_flatbuffer_result_from_Double(value: f64) -> Box<FfiVec> {
5050
let vec = get_flatbuffer_result(value);
5151

5252
Box::new(unsafe { FfiVec::from_vec(vec) })
5353
}
5454

55-
#[no_mangle]
55+
#[unsafe(no_mangle)]
5656
pub extern "C" fn hl_flatbuffer_result_from_Void() -> Box<FfiVec> {
5757
let vec = get_flatbuffer_result(());
5858

5959
Box::new(unsafe { FfiVec::from_vec(vec) })
6060
}
6161

62-
#[no_mangle]
62+
#[unsafe(no_mangle)]
6363
pub extern "C" fn hl_flatbuffer_result_from_String(value: *const c_char) -> Box<FfiVec> {
6464
let str = unsafe { CStr::from_ptr(value) };
6565
let vec = get_flatbuffer_result(str.to_string_lossy().as_ref());
6666

6767
Box::new(unsafe { FfiVec::from_vec(vec) })
6868
}
6969

70-
#[no_mangle]
70+
#[unsafe(no_mangle)]
7171
pub extern "C" fn hl_flatbuffer_result_from_Bytes(data: *const u8, len: usize) -> Box<FfiVec> {
7272
let slice = unsafe { core::slice::from_raw_parts(data, len) };
7373

@@ -78,23 +78,23 @@ pub extern "C" fn hl_flatbuffer_result_from_Bytes(data: *const u8, len: usize) -
7878

7979
//--- Functions for getting values returned by host functions calls
8080

81-
#[no_mangle]
81+
#[unsafe(no_mangle)]
8282
pub extern "C" fn hl_get_host_return_value_as_Int() -> i32 {
8383
get_host_return_value().expect("Unable to get host return value as int")
8484
}
8585

86-
#[no_mangle]
86+
#[unsafe(no_mangle)]
8787
pub extern "C" fn hl_get_host_return_value_as_UInt() -> u32 {
8888
get_host_return_value().expect("Unable to get host return value as uint")
8989
}
9090

9191
// the same for long, ulong
92-
#[no_mangle]
92+
#[unsafe(no_mangle)]
9393
pub extern "C" fn hl_get_host_return_value_as_Long() -> i64 {
9494
get_host_return_value().expect("Unable to get host return value as long")
9595
}
9696

97-
#[no_mangle]
97+
#[unsafe(no_mangle)]
9898
pub extern "C" fn hl_get_host_return_value_as_ULong() -> u64 {
9999
get_host_return_value().expect("Unable to get host return value as ulong")
100100
}

src/hyperlight_guest_capi/src/logging.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::ffi::c_char;
22

3-
#[no_mangle]
3+
#[unsafe(no_mangle)]
44
pub extern "C" fn hl_log(
55
level: log::Level,
66
message: *const c_char,

0 commit comments

Comments
 (0)