-
Notifications
You must be signed in to change notification settings - Fork 162
Explicitly error out on host-guest version mismatch #1252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ludfjig
wants to merge
1
commit into
hyperlight-dev:main
Choose a base branch
from
ludfjig:custom_elf_section_hl_version
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| Copyright 2025 The Hyperlight Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| //! ELF note types for embedding hyperlight version metadata in guest binaries. | ||
| //! | ||
| //! Guest binaries built with `hyperlight-guest-bin` include a `.note.hyperlight.version` | ||
| //! ELF note section containing the crate version they were compiled against. | ||
| //! The host reads this section at load time to verify ABI compatibility. | ||
|
|
||
| /// The ELF note section name used to embed the hyperlight-guest-bin version in guest binaries. | ||
| pub const HYPERLIGHT_VERSION_SECTION: &str = ".note.hyperlight.version"; | ||
|
|
||
| /// The owner name used in the ELF note header for hyperlight version metadata. | ||
| pub const HYPERLIGHT_NOTE_NAME: &str = "Hyperlight"; | ||
|
|
||
| /// The note type value used in the ELF note header for hyperlight version metadata. | ||
| pub const HYPERLIGHT_NOTE_TYPE: u32 = 1; | ||
|
|
||
| /// A byte array with 4-byte alignment, used for ELF note name/descriptor | ||
| /// fields. The compiler inserts trailing padding automatically so that the | ||
| /// next field starts at a 4-byte boundary. | ||
| #[repr(C, align(4))] | ||
| struct Aligned4<const N: usize>(pub(self) [u8; N]); | ||
|
|
||
| /// An ELF note structure suitable for embedding in a `#[link_section]` static. | ||
| /// | ||
| /// `NAME_SZ` and `DESC_SZ` must include the null terminator. | ||
| /// The `+ 1` can't be hidden inside the struct because stable Rust doesn't | ||
| /// allow `[u8; N + 1]` in struct fields. [`Aligned4`] handles the 4-byte | ||
| /// alignment padding required by the note format. | ||
| #[repr(C)] | ||
| pub struct ElfNote<const NAME_SZ: usize, const DESC_SZ: usize> { | ||
| namesz: u32, | ||
| descsz: u32, | ||
| n_type: u32, | ||
| name: Aligned4<NAME_SZ>, | ||
| desc: Aligned4<DESC_SZ>, | ||
| } | ||
|
|
||
| // SAFETY: ElfNote contains only plain data (`u32` and `[u8; N]`). | ||
| // Required because ElfNote is used in a `static` (for `#[link_section]`), | ||
| // and `static` values must be `Sync`. | ||
| unsafe impl<const N: usize, const D: usize> Sync for ElfNote<N, D> {} | ||
|
|
||
| impl<const NAME_SZ: usize, const DESC_SZ: usize> ElfNote<NAME_SZ, DESC_SZ> { | ||
| /// Create a new ELF note from a name string, descriptor string, and type. | ||
| /// | ||
| /// `NAME_SZ` and `DESC_SZ` must equal `name.len() + 1` and `desc.len() + 1` | ||
| /// respectively (the `+ 1` accounts for the null terminator). | ||
| pub const fn new(name: &str, desc: &str, n_type: u32) -> Self { | ||
| Self { | ||
| namesz: NAME_SZ as u32, | ||
| descsz: DESC_SZ as u32, | ||
| n_type, | ||
| name: Aligned4(pad_str_to_array(name)), | ||
| desc: Aligned4(pad_str_to_array(desc)), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Copy a string into a zero-initialised byte array at compile time. | ||
| const fn pad_str_to_array<const N: usize>(s: &str) -> [u8; N] { | ||
| let bytes = s.as_bytes(); | ||
| let mut result = [0u8; N]; | ||
| let mut i = 0; | ||
| while i < bytes.len() { | ||
| result[i] = bytes[i]; | ||
| i += 1; | ||
| } | ||
| result | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we have an
accepts_guest_not_built_with_higihterlight guest?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added