Skip to content
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

Remove custom alloca #106

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/hyperlight_guest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ Library to build guest applications for hyperlight.
"""

[features]
default = ["libc", "printf", "alloca"]
default = ["libc", "printf"]
libc = [] # compile musl libc
printf = [] # compile printf
alloca = [] # compile alloca wrapper

[dependencies]
anyhow = { version = "1.0.95", default-features = false }
Expand Down
13 changes: 1 addition & 12 deletions src/hyperlight_guest/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ fn copy_includes<P: AsRef<Path>, Q: AsRef<Path> + std::fmt::Debug>(include_dir:

fn cargo_main() {
println!("cargo:rerun-if-changed=third_party");
println!("cargo:rerun-if-changed=src/alloca");
println!("cargo:rerun-if-changed=include");

let mut cfg = cc::Build::new();
Expand All @@ -64,19 +63,9 @@ fn cargo_main() {
.include("third_party/musl/arch/x86_64");
}

if cfg!(feature = "alloca") {
cfg.file("src/alloca/alloca.c")
.define("_alloca", "_alloca_wrapper")
.flag("-Wno-return-stack-address");
}

let is_pe = env::var("CARGO_CFG_WINDOWS").is_ok();

if cfg!(any(
feature = "printf",
feature = "libc",
feature = "alloca"
)) {
if cfg!(any(feature = "printf", feature = "libc")) {
cfg.define("HYPERLIGHT", None); // used in certain musl files for conditional compilation

// silence compiler warnings
Expand Down
23 changes: 0 additions & 23 deletions src/hyperlight_guest/src/alloca.rs

This file was deleted.

29 changes: 0 additions & 29 deletions src/hyperlight_guest/src/alloca/alloca.c

This file was deleted.

1 change: 0 additions & 1 deletion src/hyperlight_guest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ pub mod host_error;
pub mod host_function_call;
pub mod host_functions;

pub mod alloca;
pub(crate) mod guest_logger;
pub mod memory;
pub mod print;
Expand Down
10 changes: 5 additions & 5 deletions src/hyperlight_host/examples/func_ctx/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ fn main() {
/// if anything failed.
fn do_calls(mut ctx: MultiUseGuestCallContext) -> Result<MultiUseSandbox> {
{
let res1: i32 = {
let res1: String = {
let rv = ctx.call(
"StackAllocate",
"Echo",
ReturnType::Int,
Some(vec![ParameterValue::Int(1)]),
Some(vec![ParameterValue::String("hello".to_string())]),
)?;
rv.try_into()
}
.map_err(|e| new_error!("failed to get StackAllocate result: {}", e))?;
println!("got StackAllocate res: {res1}");
.map_err(|e| new_error!("failed to get Echo result: {}", e))?;
println!("got Echo res: {res1}");
}
{
let res2: i32 = {
Expand Down
10 changes: 6 additions & 4 deletions src/hyperlight_host/src/func/call_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,12 @@ mod tests {
thread::spawn(move || {
let calls: Vec<TestFuncCall> = vec![
TestFuncCall {
func_name: "StackAllocate".to_string(),
ret_type: ReturnType::Int,
params: Some(vec![ParameterValue::Int(i + 1)]),
expected_ret: ReturnValue::Int(i + 1),
func_name: "Echo".to_string(),
ret_type: ReturnType::String,
params: Some(vec![ParameterValue::String(
format!("Hello {}", i).to_string(),
)]),
expected_ret: ReturnValue::String(format!("Hello {}", i).to_string()),
},
TestFuncCall {
func_name: "CallMalloc".to_string(),
Expand Down
6 changes: 3 additions & 3 deletions src/hyperlight_host/src/sandbox/initialized_multi_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ mod tests {

for _ in 0..1000 {
ctx.call(
"StackAllocate",
ReturnType::Int,
Some(vec![ParameterValue::Int(1)]),
"Echo",
ReturnType::String,
Some(vec![ParameterValue::String("hello".to_string())]),
)
.unwrap();
}
Expand Down
70 changes: 12 additions & 58 deletions src/hyperlight_host/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use hyperlight_host::mem::memory_region::MemoryRegionFlags;
use hyperlight_host::sandbox::SandboxConfiguration;
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
use hyperlight_host::sandbox_state::transition::Noop;
use hyperlight_host::{GuestBinary, HyperlightError, UninitializedSandbox};
use hyperlight_host::{GuestBinary, HyperlightError, MultiUseSandbox, UninitializedSandbox};
use hyperlight_testing::{c_simple_guest_as_string, simple_guest_as_string};

pub mod common; // pub to disable dead_code warning
Expand Down Expand Up @@ -266,76 +266,30 @@ fn guest_malloc_abort() {
));
}

// checks that alloca works
// Tests libc alloca
#[test]
fn dynamic_stack_allocate() {
let mut sbox = new_uninit().unwrap().evolve(Noop::default()).unwrap();

let bytes = 10_000; // some low number that can be allocated on stack

sbox.call_guest_function_by_name(
"StackAllocate",
ReturnType::Int,
Some(vec![ParameterValue::Int(bytes)]),
)
.unwrap();
}

// checks alloca fails with stackoverflow for large allocations
#[test]
fn dynamic_stack_allocate_overflow() {
let mut sbox1 = new_uninit().unwrap().evolve(Noop::default()).unwrap();

// zero is handled as special case in guest,
// will turn DEFAULT_GUEST_STACK_SIZE + 1
let bytes = 0;

let res = sbox1
.call_guest_function_by_name(
"StackAllocate",
ReturnType::Int,
Some(vec![ParameterValue::Int(bytes)]),
)
.unwrap_err();
println!("{:?}", res);
assert!(matches!(res, HyperlightError::StackOverflow()));
}

// checks alloca fails with overflow when stack pointer overflows
#[test]
fn dynamic_stack_allocate_pointer_overflow() {
let mut sbox1 = new_uninit_rust().unwrap().evolve(Noop::default()).unwrap();
let bytes = 10 * 1024 * 1024; // 10Mb
fn dynamic_stack_allocate_c_guest() {
let path = c_simple_guest_as_string().unwrap();
let guest_path = GuestBinary::FilePath(path);
let uninit = UninitializedSandbox::new(guest_path, None, None, None);
let mut sbox1: MultiUseSandbox = uninit.unwrap().evolve(Noop::default()).unwrap();

let res = sbox1
let res2 = sbox1
.call_guest_function_by_name(
"StackAllocate",
ReturnType::Int,
Some(vec![ParameterValue::Int(bytes)]),
Some(vec![ParameterValue::Int(100)]),
)
.unwrap_err();
println!("{:?}", res);
assert!(matches!(res, HyperlightError::StackOverflow()));
}

// checks alloca fails with stackoverflow for huge allocations with c guest lib
#[test]
fn dynamic_stack_allocate_overflow_c_guest() {
let path = c_simple_guest_as_string().unwrap();
let guest_path = GuestBinary::FilePath(path);
let uninit = UninitializedSandbox::new(guest_path, None, None, None);
let mut sbox1 = uninit.unwrap().evolve(Noop::default()).unwrap();

let bytes = 0; // zero is handled as special case in guest, will turn into large number
.unwrap();
assert!(matches!(res2, ReturnValue::Int(n) if n == 100));

let res = sbox1
.call_guest_function_by_name(
"StackAllocate",
ReturnType::Int,
Some(vec![ParameterValue::Int(bytes)]),
Some(vec![ParameterValue::Int(128 * 1024 * 1024)]),
)
.unwrap_err();
println!("{:?}", res);
assert!(matches!(res, HyperlightError::StackOverflow()));
}

Expand Down
7 changes: 2 additions & 5 deletions src/tests/c_guests/c_simpleguest/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Included from hyperlight_guest/third_party/libc
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
// Included from hyperlight_guest/third_party/printf
#include "printf.h"

Expand Down Expand Up @@ -33,11 +34,7 @@ int print_output(const char *message) {

__attribute__((optnone))
int stack_allocate(int32_t length) {
if (length == 0) {
length = GUEST_STACK_SIZE + 1;
}

void *buffer = _alloca(length);
void *buffer = alloca(length);
(void)buffer;

return length;
Expand Down
28 changes: 0 additions & 28 deletions src/tests/rust_guests/simpleguest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ use hyperlight_common::flatbuffer_wrappers::util::{
get_flatbuffer_result_from_void,
};
use hyperlight_common::mem::PAGE_SIZE;
use hyperlight_guest::alloca::_alloca;
use hyperlight_guest::entrypoint::{abort_with_code, abort_with_code_and_message};
use hyperlight_guest::error::{HyperlightGuestError, Result};
use hyperlight_guest::guest_function_definition::GuestFunctionDefinition;
Expand Down Expand Up @@ -409,25 +408,6 @@ fn print_eleven_args(function_call: &FunctionCall) -> Result<Vec<u8>> {
}
}

fn stack_allocate(function_call: &FunctionCall) -> Result<Vec<u8>> {
if let ParameterValue::Int(length) = function_call.parameters.clone().unwrap()[0].clone() {
let alloc_length = if length == 0 {
DEFAULT_GUEST_STACK_SIZE + 1
} else {
length
};

_alloca(alloc_length as usize);

Ok(get_flatbuffer_result_from_int(alloc_length))
} else {
Err(HyperlightGuestError::new(
ErrorCode::GuestFunctionParameterTypeMismatch,
"Invalid parameters passed to stack_allocate".to_string(),
))
}
}

fn buffer_overrun(function_call: &FunctionCall) -> Result<Vec<u8>> {
if let ParameterValue::String(value) = function_call.parameters.clone().unwrap()[0].clone() {
let c_str = value.as_str();
Expand Down Expand Up @@ -749,14 +729,6 @@ pub extern "C" fn hyperlight_main() {
);
register_function(print_using_printf_def);

let stack_allocate_def = GuestFunctionDefinition::new(
"StackAllocate".to_string(),
Vec::from(&[ParameterType::Int]),
ReturnType::Int,
stack_allocate as i64,
);
register_function(stack_allocate_def);

let stack_overflow_def = GuestFunctionDefinition::new(
"StackOverflow".to_string(),
Vec::from(&[ParameterType::Int]),
Expand Down
Loading