Skip to content

Commit 2546506

Browse files
committed
Clippy guests
1 parent da37ab7 commit 2546506

File tree

6 files changed

+88
-82
lines changed

6 files changed

+88
-82
lines changed

.github/workflows/dep_rust.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ jobs:
4848
run: just fmt-check
4949

5050
- name: clippy
51-
run: just clippy ${{ matrix.config }}
51+
run: |
52+
just clippy ${{ matrix.config }}
53+
just clippy-guests
5254
5355
- name: Ensure up-to-date Cargo.lock
5456
run: |

Justfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ fmt-apply:
133133
clippy target=default-target:
134134
cargo clippy --all-targets --all-features --profile={{ if target == "debug" { "dev" } else { target } }} -- -D warnings
135135

136+
clippy-guests target=default-target:
137+
cd src/tests/rust_guests/simpleguest && cargo clippy --profile={{ if target == "debug" { "dev" } else { target } }} -- -D warnings
138+
cd src/tests/rust_guests/callbackguest && cargo clippy --profile={{ if target == "debug" { "dev" } else { target } }} -- -D warnings
139+
136140
clippy-apply-fix-unix:
137141
cargo clippy --fix --all
138142

src/hyperlight_guest/src/guest_function_call.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::shared_input_data::try_pop_shared_input_data_into;
2828
use crate::shared_output_data::push_shared_output_data;
2929
use crate::REGISTERED_GUEST_FUNCTIONS;
3030

31-
type GuestFunc = fn(&FunctionCall) -> Result<Vec<u8>>;
31+
pub(crate) type GuestFunc = fn(&FunctionCall) -> Result<Vec<u8>>;
3232

3333
pub(crate) fn call_guest_function(function_call: FunctionCall) -> Result<Vec<u8>> {
3434
// Validate this is a Guest Function Call
@@ -56,10 +56,7 @@ pub(crate) fn call_guest_function(function_call: FunctionCall) -> Result<Vec<u8>
5656
// Verify that the function call has the correct parameter types and length.
5757
registered_function_definition.verify_parameters(&function_call_parameter_types)?;
5858

59-
let p_function = unsafe {
60-
let function_pointer = registered_function_definition.function_pointer;
61-
core::mem::transmute::<i64, GuestFunc>(function_pointer)
62-
};
59+
let p_function = registered_function_definition.function_pointer;
6360

6461
p_function(&function_call)
6562
} else {

src/hyperlight_guest/src/guest_function_definition.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use hyperlight_common::flatbuffer_wrappers::function_types::{ParameterType, Retu
2222
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
2323

2424
use crate::error::{HyperlightGuestError, Result};
25+
use crate::guest_function_call::GuestFunc;
2526

2627
/// The definition of a function exposed from the guest to the host
2728
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -33,7 +34,7 @@ pub struct GuestFunctionDefinition {
3334
/// The type of the return value from the host function call
3435
pub return_type: ReturnType,
3536
/// The function pointer to the guest function
36-
pub function_pointer: i64,
37+
pub function_pointer: GuestFunc,
3738
}
3839

3940
impl GuestFunctionDefinition {
@@ -42,7 +43,7 @@ impl GuestFunctionDefinition {
4243
function_name: String,
4344
parameter_types: Vec<ParameterType>,
4445
return_type: ReturnType,
45-
function_pointer: i64,
46+
function_pointer: GuestFunc,
4647
) -> Self {
4748
Self {
4849
function_name,

src/tests/rust_guests/callbackguest/src/main.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,47 +62,47 @@ fn guest_function(function_call: &FunctionCall) -> Result<Vec<u8>> {
6262
if let ParameterValue::String(message) = &function_call.parameters.as_ref().unwrap()[0] {
6363
send_message_to_host_method("HostMethod", "Hello from GuestFunction, ", message)
6464
} else {
65-
return Err(HyperlightGuestError::new(
65+
Err(HyperlightGuestError::new(
6666
ErrorCode::GuestFunctionParameterTypeMismatch,
6767
"Invalid parameters passed to guest_function".to_string(),
68-
));
68+
))
6969
}
7070
}
7171

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

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

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

105-
fn guest_function4() -> Result<Vec<u8>> {
105+
fn guest_function4(_: &FunctionCall) -> Result<Vec<u8>> {
106106
call_host_function(
107107
"HostMethod4",
108108
Some(Vec::from(&[ParameterValue::String(
@@ -125,7 +125,7 @@ fn guest_log_message(function_call: &FunctionCall) -> Result<Vec<u8>> {
125125
&function_call.parameters.as_ref().unwrap()[2],
126126
) {
127127
let mut log_level = *level;
128-
if log_level < 0 || log_level > 6 {
128+
if !(0..=6).contains(&log_level) {
129129
log_level = 0;
130130
}
131131

@@ -140,25 +140,25 @@ fn guest_log_message(function_call: &FunctionCall) -> Result<Vec<u8>> {
140140

141141
Ok(get_flatbuffer_result_from_int(message.len() as i32))
142142
} else {
143-
return Err(HyperlightGuestError::new(
143+
Err(HyperlightGuestError::new(
144144
ErrorCode::GuestFunctionParameterTypeMismatch,
145145
"Invalid parameters passed to guest_log_message".to_string(),
146-
));
146+
))
147147
}
148148
}
149149

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

161-
fn call_host_spin() -> Result<Vec<u8>> {
161+
fn call_host_spin(_: &FunctionCall) -> Result<Vec<u8>> {
162162
call_host_function("Spin", None, ReturnType::Void)?;
163163
Ok(get_flatbuffer_result_from_void())
164164
}
@@ -169,47 +169,47 @@ pub extern "C" fn hyperlight_main() {
169169
"PrintOutput".to_string(),
170170
Vec::from(&[ParameterType::String]),
171171
ReturnType::Int,
172-
print_output_as_guest_function as i64,
172+
print_output_as_guest_function,
173173
);
174174
register_function(print_output_def);
175175

176176
let guest_function_def = GuestFunctionDefinition::new(
177177
"GuestMethod".to_string(),
178178
Vec::from(&[ParameterType::String]),
179179
ReturnType::Int,
180-
guest_function as i64,
180+
guest_function,
181181
);
182182
register_function(guest_function_def);
183183

184184
let guest_function1_def = GuestFunctionDefinition::new(
185185
"GuestMethod1".to_string(),
186186
Vec::from(&[ParameterType::String]),
187187
ReturnType::Int,
188-
guest_function1 as i64,
188+
guest_function1,
189189
);
190190
register_function(guest_function1_def);
191191

192192
let guest_function2_def = GuestFunctionDefinition::new(
193193
"GuestMethod2".to_string(),
194194
Vec::from(&[ParameterType::String]),
195195
ReturnType::Int,
196-
guest_function2 as i64,
196+
guest_function2,
197197
);
198198
register_function(guest_function2_def);
199199

200200
let guest_function3_def = GuestFunctionDefinition::new(
201201
"GuestMethod3".to_string(),
202202
Vec::from(&[ParameterType::String]),
203203
ReturnType::Int,
204-
guest_function3 as i64,
204+
guest_function3,
205205
);
206206
register_function(guest_function3_def);
207207

208208
let guest_function4_def = GuestFunctionDefinition::new(
209209
"GuestMethod4".to_string(),
210210
Vec::new(),
211211
ReturnType::Int,
212-
guest_function4 as i64,
212+
guest_function4,
213213
);
214214
register_function(guest_function4_def);
215215

@@ -221,23 +221,23 @@ pub extern "C" fn hyperlight_main() {
221221
ParameterType::Int,
222222
]),
223223
ReturnType::Int,
224-
guest_log_message as i64,
224+
guest_log_message,
225225
);
226226
register_function(guest_log_message_def);
227227

228228
let call_error_method_def = GuestFunctionDefinition::new(
229229
"CallErrorMethod".to_string(),
230230
Vec::from(&[ParameterType::String]),
231231
ReturnType::Int,
232-
call_error_method as i64,
232+
call_error_method,
233233
);
234234
register_function(call_error_method_def);
235235

236236
let call_host_spin_def = GuestFunctionDefinition::new(
237237
"CallHostSpin".to_string(),
238238
Vec::new(),
239239
ReturnType::Int,
240-
call_host_spin as i64,
240+
call_host_spin,
241241
);
242242
register_function(call_host_spin_def);
243243
}

0 commit comments

Comments
 (0)