Skip to content

Commit dc4bfe6

Browse files
committed
Update hyperlight-guest-capi to 2024 edition
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 110ec8e commit dc4bfe6

File tree

5 files changed

+57
-32
lines changed

5 files changed

+57
-32
lines changed

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: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,25 @@ 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;
1414

15+
use spin::Mutex;
16+
1517
use crate::types::{FfiFunctionCall, FfiVec};
16-
static mut REGISTERED_C_GUEST_FUNCTIONS: GuestFunctionRegister = GuestFunctionRegister::new();
18+
static REGISTERED_C_GUEST_FUNCTIONS: Mutex<GuestFunctionRegister> =
19+
Mutex::new(GuestFunctionRegister::new());
1720

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

20-
extern "C" {
23+
unsafe extern "C" {
2124
// NOTE *mut FfiVec must be a Box<FfiVec>. This will be the case as long as the guest
2225
// returns a FfiVec that they created using the c-api hl_flatbuffer_result_from_* functions.
2326
fn c_guest_dispatch_function(function_call: &FfiFunctionCall) -> *mut FfiVec;
2427
}
2528

26-
#[no_mangle]
29+
#[unsafe(no_mangle)]
2730
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) }
31+
if let Some(registered_func) = REGISTERED_C_GUEST_FUNCTIONS
32+
.lock()
33+
.get(&function_call.function_name)
3034
{
3135
let function_call_parameter_types: Vec<ParameterType> = function_call
3236
.parameters
@@ -64,8 +68,11 @@ pub fn guest_dispatch_function(function_call: FunctionCall) -> Result<Vec<u8>> {
6468
}
6569
}
6670

67-
#[no_mangle]
68-
pub extern "C" fn hl_register_function_definition(
71+
/// # Safety
72+
///
73+
/// Dereferences the given raw pointers
74+
#[unsafe(no_mangle)]
75+
pub unsafe extern "C" fn hl_register_function_definition(
6976
function_name: *const c_char,
7077
func_ptr: CGuestFunc,
7178
param_no: usize,
@@ -79,12 +86,11 @@ pub extern "C" fn hl_register_function_definition(
7986
let func_def =
8087
GuestFunctionDefinition::new(func_name, func_params, return_type, func_ptr as usize);
8188

82-
#[allow(static_mut_refs)]
83-
unsafe { &mut REGISTERED_C_GUEST_FUNCTIONS }.register(func_def);
89+
REGISTERED_C_GUEST_FUNCTIONS.lock().register(func_def);
8490
}
8591

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

src/hyperlight_guest_capi/src/error.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,25 @@ 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]
7-
pub extern "C" fn hl_set_error(err: ErrorCode, message: *const c_char) {
6+
/// # Safety
7+
///
8+
/// Dereferences the given raw pointer.
9+
#[unsafe(no_mangle)]
10+
pub unsafe extern "C" fn hl_set_error(err: ErrorCode, message: *const c_char) {
811
unsafe {
912
setError(err.into(), message);
1013
}
1114
}
1215

13-
#[no_mangle]
16+
#[unsafe(no_mangle)]
1417
pub extern "C" fn hl_abort_with_code(err: i32) {
1518
hyperlight_guest::entrypoint::abort_with_code(err);
1619
}
1720

18-
#[no_mangle]
19-
pub extern "C" fn hl_abort_with_code_and_message(err: i32, message: *const c_char) {
21+
/// # Safety
22+
///
23+
/// Dereferences the given raw pointer.
24+
#[unsafe(no_mangle)]
25+
pub unsafe extern "C" fn hl_abort_with_code_and_message(err: i32, message: *const c_char) {
2026
unsafe { hyperlight_guest::entrypoint::abort_with_code_and_message(err, message) };
2127
}

src/hyperlight_guest_capi/src/flatbuffer.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,65 +10,74 @@ 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]
63-
pub extern "C" fn hl_flatbuffer_result_from_String(value: *const c_char) -> Box<FfiVec> {
62+
/// # Safety
63+
///
64+
/// Dereferences the given raw pointer.
65+
#[unsafe(no_mangle)]
66+
pub unsafe extern "C" fn hl_flatbuffer_result_from_String(value: *const c_char) -> Box<FfiVec> {
6467
let str = unsafe { CStr::from_ptr(value) };
6568
let vec = get_flatbuffer_result(str.to_string_lossy().as_ref());
6669

6770
Box::new(unsafe { FfiVec::from_vec(vec) })
6871
}
6972

70-
#[no_mangle]
71-
pub extern "C" fn hl_flatbuffer_result_from_Bytes(data: *const u8, len: usize) -> Box<FfiVec> {
73+
/// # Safety
74+
///
75+
/// Dereferences the given raw pointer.
76+
#[unsafe(no_mangle)]
77+
pub unsafe extern "C" fn hl_flatbuffer_result_from_Bytes(
78+
data: *const u8,
79+
len: usize,
80+
) -> Box<FfiVec> {
7281
let slice = unsafe { core::slice::from_raw_parts(data, len) };
7382

7483
let vec = get_flatbuffer_result(slice);
@@ -78,23 +87,23 @@ pub extern "C" fn hl_flatbuffer_result_from_Bytes(data: *const u8, len: usize) -
7887

7988
//--- Functions for getting values returned by host functions calls
8089

81-
#[no_mangle]
90+
#[unsafe(no_mangle)]
8291
pub extern "C" fn hl_get_host_return_value_as_Int() -> i32 {
8392
get_host_return_value().expect("Unable to get host return value as int")
8493
}
8594

86-
#[no_mangle]
95+
#[unsafe(no_mangle)]
8796
pub extern "C" fn hl_get_host_return_value_as_UInt() -> u32 {
8897
get_host_return_value().expect("Unable to get host return value as uint")
8998
}
9099

91100
// the same for long, ulong
92-
#[no_mangle]
101+
#[unsafe(no_mangle)]
93102
pub extern "C" fn hl_get_host_return_value_as_Long() -> i64 {
94103
get_host_return_value().expect("Unable to get host return value as long")
95104
}
96105

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

src/hyperlight_guest_capi/src/logging.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use core::ffi::c_char;
22

3-
#[no_mangle]
4-
pub extern "C" fn hl_log(
3+
/// # Safety
4+
///
5+
/// Dereferences the given raw pointers.
6+
#[unsafe(no_mangle)]
7+
pub unsafe extern "C" fn hl_log(
58
level: log::Level,
69
message: *const c_char,
710
line: i32,

0 commit comments

Comments
 (0)