Skip to content

Guest function improvements and macros#851

Merged
jprendes merged 9 commits intohyperlight-dev:mainfrom
jprendes:guest-registration
Dec 3, 2025
Merged

Guest function improvements and macros#851
jprendes merged 9 commits intohyperlight-dev:mainfrom
jprendes:guest-registration

Conversation

@jprendes
Copy link
Copy Markdown
Contributor

@jprendes jprendes commented Sep 2, 2025

This pull request introduces a new func module to the hyperlight-common crate, which is used to providing new register_fn function for guest function registration: register_fn("function_name", the_function). The traits in the func module are used to specify and constrain the type of the_function.

It refactors existing function-related code from hyperlight_host into hyperlight_common.

This pull request also introduces a new hyperlight_guest_macro crate that defines two proc-macros: host_function and guest_function.

  • host_function: This macro is used to declare host functions in a hyperlight guest. The specified function must have no body (like an ffi function declaration), and the signature is used to create a wrapper for a host function call with the correct types.
  • guest_function: This macros is used to declare guest functions in a hyperlight guest. The function signature is used to create a wrapper that takes care of extracting the arguments from the FunctionCall and serializing the result using into_flatbuffer_result. The function is also automatically registered before the call to hyperlight_main, removing the need to do manual registration in hyperlight_main.

To enable the automatic registration of functions, the MSRV is bumped to 1.89 (which includes rust-lang/rust#137685, which is required as the automatic registration uses linkme)

@jprendes jprendes added the kind/enhancement For PRs adding features, improving functionality, docs, tests, etc. label Sep 2, 2025
@jprendes jprendes force-pushed the guest-registration branch 5 times, most recently from b86709b to 870d40b Compare December 2, 2025 14:15
@jprendes jprendes marked this pull request as ready for review December 2, 2025 15:10
Comment thread src/hyperlight_host/src/func/host_functions.rs
Comment thread src/hyperlight_host/src/sandbox/initialized_multi_use.rs
Comment thread src/hyperlight_guest_bin/src/guest_function/definition.rs
Comment thread src/hyperlight_common/src/func/ret_type.rs Outdated
Comment thread src/hyperlight_guest_macro/src/lib.rs
Comment thread src/hyperlight_guest_macro/src/lib.rs
Comment thread src/tests/rust_guests/simpleguest/src/main.rs
Comment thread src/tests/rust_guests/simpleguest/src/main.rs
@jprendes jprendes force-pushed the guest-registration branch 4 times, most recently from 87b2390 to da5bad6 Compare December 3, 2025 11:43
Copy link
Copy Markdown
Contributor

@ludfjig ludfjig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Btw, how does linkme work? xD

Comment thread README.md
@jprendes jprendes enabled auto-merge (squash) December 3, 2025 21:00
simongdavies
simongdavies previously approved these changes Dec 3, 2025
Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
…uest-bin

Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
@jprendes
Copy link
Copy Markdown
Contributor Author

jprendes commented Dec 3, 2025

Looks good to me. Btw, how does linkme work? xD

This is the best explanation I found, other than inspecting the source code :-)
https://internals.rust-lang.org/t/from-life-before-main-to-common-life-in-main/16006/24

Copy link
Copy Markdown
Contributor

@dblnz dblnz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Good work Jorge!
I didn't get to look very thoroughly, but overall it looks good

@jprendes jprendes merged commit 09346ac into hyperlight-dev:main Dec 3, 2025
41 checks passed
vshailesh pushed a commit to vshailesh/hyperlight that referenced this pull request Dec 7, 2025
* move function type traits to hyperlight_common

Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>

* Use function type traits to improve registration ergonomics

Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>

* Add from_result method to ResultType

Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>

* Add guest macro crate

Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>

* use guest_function and host_function macros in simpleguest

Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>

* update test guests Cargo.lock files

Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>

* Move result unwrap used in host_function macro from hl-common to hl-guest-bin

Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>

* Add comments on tricky areas

Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>

* update README.md to use new macros

Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>

---------

Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement For PRs adding features, improving functionality, docs, tests, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants