Skip to content

Commit cefec12

Browse files
committed
[guest-bin] move panic handler to guest-bin lib
Signed-off-by: danbugs <[email protected]>
1 parent b5b0296 commit cefec12

File tree

12 files changed

+65
-19
lines changed

12 files changed

+65
-19
lines changed

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hyperlight_guest/src/lib.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@ limitations under the License.
1616

1717
#![no_std]
1818
// Deps
19-
use alloc::string::ToString;
2019

2120
use buddy_system_allocator::LockedHeap;
2221
use guest_function_register::GuestFunctionRegister;
23-
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
2422
use hyperlight_common::mem::HyperlightPEB;
2523

26-
use crate::entrypoint::abort_with_code_and_message;
2724
extern crate alloc;
2825

2926
// Modules
@@ -53,22 +50,6 @@ pub mod exceptions {
5350
}
5451
pub mod logging;
5552

56-
// It looks like rust-analyzer doesn't correctly manage no_std crates,
57-
// and so it displays an error about a duplicate panic_handler.
58-
// See more here: https://github.com/rust-lang/rust-analyzer/issues/4490
59-
// The cfg_attr attribute is used to avoid clippy failures as test pulls in std which pulls in a panic handler
60-
#[cfg_attr(not(test), panic_handler)]
61-
#[allow(clippy::panic)]
62-
// to satisfy the clippy when cfg == test
63-
#[allow(dead_code)]
64-
fn panic(info: &core::panic::PanicInfo) -> ! {
65-
let msg = info.to_string();
66-
let c_string = alloc::ffi::CString::new(msg)
67-
.unwrap_or_else(|_| alloc::ffi::CString::new("panic (invalid utf8)").unwrap());
68-
69-
unsafe { abort_with_code_and_message(&[ErrorCode::UnknownError as u8], c_string.as_ptr()) }
70-
}
71-
7253
// Globals
7354
#[global_allocator]
7455
pub(crate) static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::empty();

src/hyperlight_guest_bin/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ repository.workspace = true
99
readme.workspace = true
1010

1111
[dependencies]
12+
hyperlight-guest = { workspace = true, default-features = false}
13+
hyperlight-common = { workspace = true, default-features = false}
1214

1315
[lints]
1416
workspace = true

src/hyperlight_guest_bin/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,28 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16+
17+
#![no_std]
18+
19+
extern crate alloc;
20+
21+
use alloc::string::ToString;
22+
23+
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
24+
use hyperlight_guest::entrypoint::abort_with_code_and_message;
25+
26+
// It looks like rust-analyzer doesn't correctly manage no_std crates,
27+
// and so it displays an error about a duplicate panic_handler.
28+
// See more here: https://github.com/rust-lang/rust-analyzer/issues/4490
29+
// The cfg_attr attribute is used to avoid clippy failures as test pulls in std which pulls in a panic handler
30+
#[cfg_attr(not(test), panic_handler)]
31+
#[allow(clippy::panic)]
32+
// to satisfy the clippy when cfg == test
33+
#[allow(dead_code)]
34+
fn panic(info: &core::panic::PanicInfo) -> ! {
35+
let msg = info.to_string();
36+
let c_string = alloc::ffi::CString::new(msg)
37+
.unwrap_or_else(|_| alloc::ffi::CString::new("panic (invalid utf8)").unwrap());
38+
39+
unsafe { abort_with_code_and_message(&[ErrorCode::UnknownError as u8], c_string.as_ptr()) }
40+
}

src/hyperlight_guest_capi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ workspace = true
1313

1414
[dependencies]
1515
hyperlight-guest = { workspace = true, default-features = true }
16+
hyperlight-guest-bin = { workspace = true, default-features = false }
1617
hyperlight-common = { workspace = true, default-features = false }
1718
log = { version = "0.4", default-features = false }
1819

src/hyperlight_guest_capi/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
extern crate alloc;
55

6+
// TODO(danbugs): this is needed so the panic handler is actually brought in.
7+
// We can remove it later once more functionality was moved to hyperlight-guest-bin
8+
extern crate hyperlight_guest_bin;
9+
610
pub mod dispatch;
711
pub mod error;
812
pub mod flatbuffer;

src/tests/rust_guests/callbackguest/Cargo.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tests/rust_guests/callbackguest/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ edition = "2021"
55

66
[dependencies]
77
hyperlight-guest = { path = "../../../hyperlight_guest" }
8+
hyperlight-guest-bin = { path = "../../../hyperlight_guest_bin" }
89
hyperlight-common = { path = "../../../hyperlight_common", default-features = false }

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ use hyperlight_guest::guest_function_register::register_function;
3737
use hyperlight_guest::host_function_call::{call_host_function, print_output_with_host_print};
3838
use hyperlight_guest::logging::log_message;
3939

40+
// TODO(danbugs): this is needed so the panic handler is actually brought in.
41+
// We can remove it later once more functionality was moved to hyperlight-guest-bin
42+
extern crate hyperlight_guest_bin;
43+
4044
fn send_message_to_host_method(
4145
method_name: &str,
4246
guest_message: &str,

src/tests/rust_guests/simpleguest/Cargo.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)