Skip to content

Commit

Permalink
Run clippy on guests in CI (#237)
Browse files Browse the repository at this point in the history
* Clippy guests

Signed-off-by: Ludvig Liljenberg <lliljenberg@microsoft.com>

* Add matrix target to recipe

Signed-off-by: Ludvig Liljenberg <lliljenberg@microsoft.com>

* fix broken rebase

Signed-off-by: Ludvig Liljenberg <lliljenberg@microsoft.com>

---------

Signed-off-by: Ludvig Liljenberg <lliljenberg@microsoft.com>
  • Loading branch information
ludfjig authored Feb 28, 2025
1 parent 137457c commit a10b86c
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 87 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/dep_rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ jobs:
run: just fmt-check

- name: clippy
run: just clippy ${{ matrix.config }}
run: |
just clippy ${{ matrix.config }}
just clippy-guests ${{ matrix.config }}
# Does not check for updated Cargo.lock files for test rust guests as this causes an issue with this checkwhen deoendabot updates dependencies in common crates
- name: Ensure up-to-date Cargo.lock
Expand Down
4 changes: 4 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ fmt-apply:
clippy target=default-target:
cargo clippy --all-targets --all-features --profile={{ if target == "debug" { "dev" } else { target } }} -- -D warnings

clippy-guests target=default-target:
cd src/tests/rust_guests/simpleguest && cargo clippy --profile={{ if target == "debug" { "dev" } else { target } }} -- -D warnings
cd src/tests/rust_guests/callbackguest && cargo clippy --profile={{ if target == "debug" { "dev" } else { target } }} -- -D warnings

clippy-apply-fix-unix:
cargo clippy --fix --all

Expand Down
2 changes: 1 addition & 1 deletion src/hyperlight_guest/src/guest_function_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub(crate) fn call_guest_function(function_call: FunctionCall) -> Result<Vec<u8>

let p_function = unsafe {
let function_pointer = registered_function_definition.function_pointer;
core::mem::transmute::<i64, GuestFunc>(function_pointer)
core::mem::transmute::<usize, GuestFunc>(function_pointer)
};

p_function(&function_call)
Expand Down
4 changes: 2 additions & 2 deletions src/hyperlight_guest/src/guest_function_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct GuestFunctionDefinition {
/// The type of the return value from the host function call
pub return_type: ReturnType,
/// The function pointer to the guest function
pub function_pointer: i64,
pub function_pointer: usize,
}

impl GuestFunctionDefinition {
Expand All @@ -42,7 +42,7 @@ impl GuestFunctionDefinition {
function_name: String,
parameter_types: Vec<ParameterType>,
return_type: ReturnType,
function_pointer: i64,
function_pointer: usize,
) -> Self {
Self {
function_name,
Expand Down
10 changes: 3 additions & 7 deletions src/hyperlight_guest_capi/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn guest_dispatch_function(function_call: FunctionCall) -> Result<Vec<u8>> {
let ffi_func_call = FfiFunctionCall::from_function_call(function_call)?;

let guest_func =
unsafe { mem::transmute::<i64, CGuestFunc>(registered_func.function_pointer) };
unsafe { mem::transmute::<usize, CGuestFunc>(registered_func.function_pointer) };
let function_result = guest_func(&ffi_func_call);

unsafe { Ok(FfiVec::into_vec(*function_result)) }
Expand Down Expand Up @@ -76,12 +76,8 @@ pub extern "C" fn hl_register_function_definition(

let func_params = unsafe { slice::from_raw_parts(params_type, param_no).to_vec() };

let func_def = GuestFunctionDefinition::new(
func_name,
func_params,
return_type,
func_ptr as usize as i64,
);
let func_def =
GuestFunctionDefinition::new(func_name, func_params, return_type, func_ptr as usize);

#[allow(static_mut_refs)]
unsafe { &mut REGISTERED_C_GUEST_FUNCTIONS }.register(func_def);
Expand Down
48 changes: 24 additions & 24 deletions src/tests/rust_guests/callbackguest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,47 +60,47 @@ fn guest_function(function_call: &FunctionCall) -> Result<Vec<u8>> {
if let ParameterValue::String(message) = &function_call.parameters.as_ref().unwrap()[0] {
send_message_to_host_method("HostMethod", "Hello from GuestFunction, ", message)
} else {
return Err(HyperlightGuestError::new(
Err(HyperlightGuestError::new(
ErrorCode::GuestFunctionParameterTypeMismatch,
"Invalid parameters passed to guest_function".to_string(),
));
))
}
}

fn guest_function1(function_call: &FunctionCall) -> Result<Vec<u8>> {
if let ParameterValue::String(message) = &function_call.parameters.as_ref().unwrap()[0] {
send_message_to_host_method("HostMethod1", "Hello from GuestFunction1, ", message)
} else {
return Err(HyperlightGuestError::new(
Err(HyperlightGuestError::new(
ErrorCode::GuestFunctionParameterTypeMismatch,
"Invalid parameters passed to guest_function1".to_string(),
));
))
}
}

fn guest_function2(function_call: &FunctionCall) -> Result<Vec<u8>> {
if let ParameterValue::String(message) = &function_call.parameters.as_ref().unwrap()[0] {
send_message_to_host_method("HostMethod1", "Hello from GuestFunction2, ", message)
} else {
return Err(HyperlightGuestError::new(
Err(HyperlightGuestError::new(
ErrorCode::GuestFunctionParameterTypeMismatch,
"Invalid parameters passed to guest_function2".to_string(),
));
))
}
}

fn guest_function3(function_call: &FunctionCall) -> Result<Vec<u8>> {
if let ParameterValue::String(message) = &function_call.parameters.as_ref().unwrap()[0] {
send_message_to_host_method("HostMethod1", "Hello from GuestFunction3, ", message)
} else {
return Err(HyperlightGuestError::new(
Err(HyperlightGuestError::new(
ErrorCode::GuestFunctionParameterTypeMismatch,
"Invalid parameters passed to guest_function3".to_string(),
));
))
}
}

fn guest_function4() -> Result<Vec<u8>> {
fn guest_function4(_: &FunctionCall) -> Result<Vec<u8>> {
call_host_function(
"HostMethod4",
Some(Vec::from(&[ParameterValue::String(
Expand All @@ -123,7 +123,7 @@ fn guest_log_message(function_call: &FunctionCall) -> Result<Vec<u8>> {
&function_call.parameters.as_ref().unwrap()[2],
) {
let mut log_level = *level;
if log_level < 0 || log_level > 6 {
if !(0..=6).contains(&log_level) {
log_level = 0;
}

Expand All @@ -138,25 +138,25 @@ fn guest_log_message(function_call: &FunctionCall) -> Result<Vec<u8>> {

Ok(get_flatbuffer_result(message.len() as i32))
} else {
return Err(HyperlightGuestError::new(
Err(HyperlightGuestError::new(
ErrorCode::GuestFunctionParameterTypeMismatch,
"Invalid parameters passed to guest_log_message".to_string(),
));
))
}
}

fn call_error_method(function_call: &FunctionCall) -> Result<Vec<u8>> {
if let ParameterValue::String(message) = &function_call.parameters.as_ref().unwrap()[0] {
send_message_to_host_method("ErrorMethod", "Error From Host: ", message)
} else {
return Err(HyperlightGuestError::new(
Err(HyperlightGuestError::new(
ErrorCode::GuestFunctionParameterTypeMismatch,
"Invalid parameters passed to call_error_method".to_string(),
));
))
}
}

fn call_host_spin() -> Result<Vec<u8>> {
fn call_host_spin(_: &FunctionCall) -> Result<Vec<u8>> {
call_host_function("Spin", None, ReturnType::Void)?;
Ok(get_flatbuffer_result(()))
}
Expand All @@ -167,47 +167,47 @@ pub extern "C" fn hyperlight_main() {
"PrintOutput".to_string(),
Vec::from(&[ParameterType::String]),
ReturnType::Int,
print_output_as_guest_function as i64,
print_output_as_guest_function as usize,
);
register_function(print_output_def);

let guest_function_def = GuestFunctionDefinition::new(
"GuestMethod".to_string(),
Vec::from(&[ParameterType::String]),
ReturnType::Int,
guest_function as i64,
guest_function as usize,
);
register_function(guest_function_def);

let guest_function1_def = GuestFunctionDefinition::new(
"GuestMethod1".to_string(),
Vec::from(&[ParameterType::String]),
ReturnType::Int,
guest_function1 as i64,
guest_function1 as usize,
);
register_function(guest_function1_def);

let guest_function2_def = GuestFunctionDefinition::new(
"GuestMethod2".to_string(),
Vec::from(&[ParameterType::String]),
ReturnType::Int,
guest_function2 as i64,
guest_function2 as usize,
);
register_function(guest_function2_def);

let guest_function3_def = GuestFunctionDefinition::new(
"GuestMethod3".to_string(),
Vec::from(&[ParameterType::String]),
ReturnType::Int,
guest_function3 as i64,
guest_function3 as usize,
);
register_function(guest_function3_def);

let guest_function4_def = GuestFunctionDefinition::new(
"GuestMethod4".to_string(),
Vec::new(),
ReturnType::Int,
guest_function4 as i64,
guest_function4 as usize,
);
register_function(guest_function4_def);

Expand All @@ -219,23 +219,23 @@ pub extern "C" fn hyperlight_main() {
ParameterType::Int,
]),
ReturnType::Int,
guest_log_message as i64,
guest_log_message as usize,
);
register_function(guest_log_message_def);

let call_error_method_def = GuestFunctionDefinition::new(
"CallErrorMethod".to_string(),
Vec::from(&[ParameterType::String]),
ReturnType::Int,
call_error_method as i64,
call_error_method as usize,
);
register_function(call_error_method_def);

let call_host_spin_def = GuestFunctionDefinition::new(
"CallHostSpin".to_string(),
Vec::new(),
ReturnType::Int,
call_host_spin as i64,
call_host_spin as usize,
);
register_function(call_host_spin_def);
}
Expand Down
Loading

0 comments on commit a10b86c

Please sign in to comment.