Skip to content

Commit 4159a2c

Browse files
committed
Fix hyperlight-guest for Rust Edition 2024
The is mainly the result of running `cargo fix --edition` along with a couple of manula changes to fix clippy "shared reference to mutable static" warnings Signed-off-by: Simon Davies <[email protected]>
1 parent 855b3cb commit 4159a2c

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

src/hyperlight_guest_capi/src/dispatch.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ static mut REGISTERED_C_GUEST_FUNCTIONS: GuestFunctionRegister = GuestFunctionRe
1717

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

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

26-
#[no_mangle]
26+
#[unsafe(no_mangle)]
2727
pub fn guest_dispatch_function(function_call: FunctionCall) -> Result<Vec<u8>> {
28+
// Use &raw const to get an immutable reference to the static HashMap
29+
// this is to avoid the clippy warning "shared reference to mutable static"
2830
if let Some(registered_func) =
29-
unsafe { REGISTERED_C_GUEST_FUNCTIONS.get(&function_call.function_name) }
31+
unsafe { (*(&raw const REGISTERED_C_GUEST_FUNCTIONS)).get(&function_call.function_name) }
3032
{
3133
let function_call_parameter_types: Vec<ParameterType> = function_call
3234
.parameters
@@ -64,7 +66,7 @@ pub fn guest_dispatch_function(function_call: FunctionCall) -> Result<Vec<u8>> {
6466
}
6567
}
6668

67-
#[no_mangle]
69+
#[unsafe(no_mangle)]
6870
pub extern "C" fn hl_register_function_definition(
6971
function_name: *const c_char,
7072
func_ptr: CGuestFunc,
@@ -79,12 +81,13 @@ pub extern "C" fn hl_register_function_definition(
7981
let func_def =
8082
GuestFunctionDefinition::new(func_name, func_params, return_type, func_ptr as usize);
8183

82-
#[allow(static_mut_refs)]
83-
unsafe { &mut REGISTERED_C_GUEST_FUNCTIONS }.register(func_def);
84+
// Use &raw mut to get a mutable raw pointer, then dereference it
85+
// this is to avoid the clippy warning "shared reference to mutable static"
86+
unsafe { (&mut *(&raw mut REGISTERED_C_GUEST_FUNCTIONS)).register(func_def) };
8487
}
8588

8689
/// The caller is responsible for freeing the memory associated with given `FfiFunctionCall`.
87-
#[no_mangle]
90+
#[unsafe(no_mangle)]
8891
pub extern "C" fn hl_call_host_function(function_call: &FfiFunctionCall) {
8992
let parameters = unsafe { function_call.copy_parameters() };
9093
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 as u8]);
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 as u8], 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)