Save an unconditional clone of guest parameters for registered guest functions#1241
Save an unconditional clone of guest parameters for registered guest functions#1241ludfjig wants to merge 2 commits intohyperlight-dev:mainfrom
Conversation
dblnz
left a comment
There was a problem hiding this comment.
This is fine from my point of view. I've got burnt by this slight difference between the guest_dispatch_function definition and the other function definitions.
|
The perf improvement looks great, and I don't think that the amount of breaking that you mention there is necessarily a barrier. If we wanted to be very proper I suppose we would introduce a new Two slight comments:
|
I know @jsturtevant has strong opinions on the permissibility of breaking changes that can cause silent failure in downstreams, so paging him on this as well. |
|
Maybe we can rename Regarding the owned |
|
This LGTM. |
Or we can make the type private, and make people use the |
This seems like a pretty small and reasonable thing to do to avoid any downstream users having any issues with this. I believe building a habit of recognizing and coming up with patterns to avoid unexpected downstream issues is a good thing. |
1c1e23f to
bef122c
Compare
|
Updated @syntactically @jprendes @dblnz @jsturtevant |
f808d25 to
4dec0e4
Compare
4dec0e4 to
fa5bf10
Compare
fa5bf10 to
7614e77
Compare
There was a problem hiding this comment.
Pull request overview
This pull request eliminates unsafe mem::transmute operations by introducing type-safe function pointer handling for guest function registration. The change replaces raw usize function pointers with typed function pointers (GuestFunc for Rust, CGuestFunc for C API), improving memory safety and type correctness. Additionally, the function signature changes from accepting a borrowed &FunctionCall to an owned FunctionCall, avoiding unnecessary clones during guest function dispatch.
Changes:
- Introduce
GuestFunctype alias and makeGuestFunctionDefinitiongeneric over function pointer types - Remove unsafe
mem::transmutecalls when invoking guest functions, replacing with direct typed function pointer invocation - Change guest function signature from
fn(&FunctionCall)tofn(FunctionCall)for owned parameter passing
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/hyperlight_guest_bin/src/guest_function/definition.rs | Introduces GuestFunc type alias and makes GuestFunctionDefinition generic over function pointer type F, updates function signatures to accept owned FunctionCall |
| src/hyperlight_guest_bin/src/guest_function/register.rs | Makes GuestFunctionRegister generic over function pointer type, reorganizes methods for generic vs. GuestFunc-specific implementations |
| src/hyperlight_guest_bin/src/guest_function/call.rs | Removes unsafe mem::transmute and directly invokes typed function pointers |
| src/hyperlight_guest_bin/src/host_comm.rs | Updates print_output_with_host_print to accept owned FunctionCall and use remove(0) instead of cloning |
| src/hyperlight_guest_bin/src/lib.rs | Exports GuestFunc type publicly and updates static REGISTERED_GUEST_FUNCTIONS type annotation |
| src/hyperlight_guest_capi/src/dispatch.rs | Updates C API to use typed CGuestFunc function pointers, removes unsafe transmute, maintains &FfiFunctionCall signature for C compatibility |
| src/tests/rust_guests/simpleguest/src/main.rs | Updates test code to use new typed API with GuestFunctionDefinition::<GuestFunc>::new and pass function pointer directly |
| src/hyperlight_component_util/src/guest.rs | Updates component generation code to accept owned FunctionCall and pass function pointers without casting to usize |
| CHANGELOG.md | Documents the breaking change in API for GuestFunctionDefinition::new |
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
7614e77 to
24a1a74
Compare
vs main:

Only affects registered guest functions (not c api, nor when someone implements their own
guest_dispatch_function).GuestFunctionDefinition was made generic to be able to contain both GuestFunc and CGuestFunc (for use with c-api), also removes some unsafe code and adds some type-safety