Skip to content

Commit 05ad289

Browse files
authored
Remove custom alloca in favor of clang builtin (#106)
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent e6c07c3 commit 05ad289

File tree

11 files changed

+30
-170
lines changed

11 files changed

+30
-170
lines changed

src/hyperlight_guest/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ Library to build guest applications for hyperlight.
1313
"""
1414

1515
[features]
16-
default = ["libc", "printf", "alloca"]
16+
default = ["libc", "printf"]
1717
libc = [] # compile musl libc
1818
printf = [] # compile printf
19-
alloca = [] # compile alloca wrapper
2019

2120
[dependencies]
2221
anyhow = { version = "1.0.95", default-features = false }

src/hyperlight_guest/build.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ fn copy_includes<P: AsRef<Path>, Q: AsRef<Path> + std::fmt::Debug>(include_dir:
4141

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

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

67-
if cfg!(feature = "alloca") {
68-
cfg.file("src/alloca/alloca.c")
69-
.define("_alloca", "_alloca_wrapper")
70-
.flag("-Wno-return-stack-address");
71-
}
72-
7366
let is_pe = env::var("CARGO_CFG_WINDOWS").is_ok();
7467

75-
if cfg!(any(
76-
feature = "printf",
77-
feature = "libc",
78-
feature = "alloca"
79-
)) {
68+
if cfg!(any(feature = "printf", feature = "libc")) {
8069
cfg.define("HYPERLIGHT", None); // used in certain musl files for conditional compilation
8170

8271
// silence compiler warnings

src/hyperlight_guest/src/alloca.rs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/hyperlight_guest/src/alloca/alloca.c

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/hyperlight_guest/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ pub mod host_error;
4242
pub mod host_function_call;
4343
pub mod host_functions;
4444

45-
pub mod alloca;
4645
pub(crate) mod guest_logger;
4746
pub mod memory;
4847
pub mod print;

src/hyperlight_host/examples/func_ctx/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ fn main() {
4949
/// if anything failed.
5050
fn do_calls(mut ctx: MultiUseGuestCallContext) -> Result<MultiUseSandbox> {
5151
{
52-
let res1: i32 = {
52+
let res1: String = {
5353
let rv = ctx.call(
54-
"StackAllocate",
54+
"Echo",
5555
ReturnType::Int,
56-
Some(vec![ParameterValue::Int(1)]),
56+
Some(vec![ParameterValue::String("hello".to_string())]),
5757
)?;
5858
rv.try_into()
5959
}
60-
.map_err(|e| new_error!("failed to get StackAllocate result: {}", e))?;
61-
println!("got StackAllocate res: {res1}");
60+
.map_err(|e| new_error!("failed to get Echo result: {}", e))?;
61+
println!("got Echo res: {res1}");
6262
}
6363
{
6464
let res2: i32 = {

src/hyperlight_host/src/func/call_ctx.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,12 @@ mod tests {
164164
thread::spawn(move || {
165165
let calls: Vec<TestFuncCall> = vec![
166166
TestFuncCall {
167-
func_name: "StackAllocate".to_string(),
168-
ret_type: ReturnType::Int,
169-
params: Some(vec![ParameterValue::Int(i + 1)]),
170-
expected_ret: ReturnValue::Int(i + 1),
167+
func_name: "Echo".to_string(),
168+
ret_type: ReturnType::String,
169+
params: Some(vec![ParameterValue::String(
170+
format!("Hello {}", i).to_string(),
171+
)]),
172+
expected_ret: ReturnValue::String(format!("Hello {}", i).to_string()),
171173
},
172174
TestFuncCall {
173175
func_name: "CallMalloc".to_string(),

src/hyperlight_host/src/sandbox/initialized_multi_use.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ mod tests {
289289

290290
for _ in 0..1000 {
291291
ctx.call(
292-
"StackAllocate",
293-
ReturnType::Int,
294-
Some(vec![ParameterValue::Int(1)]),
292+
"Echo",
293+
ReturnType::String,
294+
Some(vec![ParameterValue::String("hello".to_string())]),
295295
)
296296
.unwrap();
297297
}

src/hyperlight_host/tests/integration_test.rs

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use hyperlight_host::mem::memory_region::MemoryRegionFlags;
2222
use hyperlight_host::sandbox::SandboxConfiguration;
2323
use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox;
2424
use hyperlight_host::sandbox_state::transition::Noop;
25-
use hyperlight_host::{GuestBinary, HyperlightError, UninitializedSandbox};
25+
use hyperlight_host::{GuestBinary, HyperlightError, MultiUseSandbox, UninitializedSandbox};
2626
use hyperlight_testing::{c_simple_guest_as_string, simple_guest_as_string};
2727

2828
pub mod common; // pub to disable dead_code warning
@@ -266,76 +266,30 @@ fn guest_malloc_abort() {
266266
));
267267
}
268268

269-
// checks that alloca works
269+
// Tests libc alloca
270270
#[test]
271-
fn dynamic_stack_allocate() {
272-
let mut sbox = new_uninit().unwrap().evolve(Noop::default()).unwrap();
273-
274-
let bytes = 10_000; // some low number that can be allocated on stack
275-
276-
sbox.call_guest_function_by_name(
277-
"StackAllocate",
278-
ReturnType::Int,
279-
Some(vec![ParameterValue::Int(bytes)]),
280-
)
281-
.unwrap();
282-
}
283-
284-
// checks alloca fails with stackoverflow for large allocations
285-
#[test]
286-
fn dynamic_stack_allocate_overflow() {
287-
let mut sbox1 = new_uninit().unwrap().evolve(Noop::default()).unwrap();
288-
289-
// zero is handled as special case in guest,
290-
// will turn DEFAULT_GUEST_STACK_SIZE + 1
291-
let bytes = 0;
292-
293-
let res = sbox1
294-
.call_guest_function_by_name(
295-
"StackAllocate",
296-
ReturnType::Int,
297-
Some(vec![ParameterValue::Int(bytes)]),
298-
)
299-
.unwrap_err();
300-
println!("{:?}", res);
301-
assert!(matches!(res, HyperlightError::StackOverflow()));
302-
}
303-
304-
// checks alloca fails with overflow when stack pointer overflows
305-
#[test]
306-
fn dynamic_stack_allocate_pointer_overflow() {
307-
let mut sbox1 = new_uninit_rust().unwrap().evolve(Noop::default()).unwrap();
308-
let bytes = 10 * 1024 * 1024; // 10Mb
271+
fn dynamic_stack_allocate_c_guest() {
272+
let path = c_simple_guest_as_string().unwrap();
273+
let guest_path = GuestBinary::FilePath(path);
274+
let uninit = UninitializedSandbox::new(guest_path, None, None, None);
275+
let mut sbox1: MultiUseSandbox = uninit.unwrap().evolve(Noop::default()).unwrap();
309276

310-
let res = sbox1
277+
let res2 = sbox1
311278
.call_guest_function_by_name(
312279
"StackAllocate",
313280
ReturnType::Int,
314-
Some(vec![ParameterValue::Int(bytes)]),
281+
Some(vec![ParameterValue::Int(100)]),
315282
)
316-
.unwrap_err();
317-
println!("{:?}", res);
318-
assert!(matches!(res, HyperlightError::StackOverflow()));
319-
}
320-
321-
// checks alloca fails with stackoverflow for huge allocations with c guest lib
322-
#[test]
323-
fn dynamic_stack_allocate_overflow_c_guest() {
324-
let path = c_simple_guest_as_string().unwrap();
325-
let guest_path = GuestBinary::FilePath(path);
326-
let uninit = UninitializedSandbox::new(guest_path, None, None, None);
327-
let mut sbox1 = uninit.unwrap().evolve(Noop::default()).unwrap();
328-
329-
let bytes = 0; // zero is handled as special case in guest, will turn into large number
283+
.unwrap();
284+
assert!(matches!(res2, ReturnValue::Int(n) if n == 100));
330285

331286
let res = sbox1
332287
.call_guest_function_by_name(
333288
"StackAllocate",
334289
ReturnType::Int,
335-
Some(vec![ParameterValue::Int(bytes)]),
290+
Some(vec![ParameterValue::Int(128 * 1024 * 1024)]),
336291
)
337292
.unwrap_err();
338-
println!("{:?}", res);
339293
assert!(matches!(res, HyperlightError::StackOverflow()));
340294
}
341295

src/tests/c_guests/c_simpleguest/main.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Included from hyperlight_guest/third_party/libc
44
#include "stdint.h"
55
#include "string.h"
6+
#include "stdlib.h"
67
// Included from hyperlight_guest/third_party/printf
78
#include "printf.h"
89

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

3435
__attribute__((optnone))
3536
int stack_allocate(int32_t length) {
36-
if (length == 0) {
37-
length = GUEST_STACK_SIZE + 1;
38-
}
39-
40-
void *buffer = _alloca(length);
37+
void *buffer = alloca(length);
4138
(void)buffer;
4239

4340
return length;

src/tests/rust_guests/simpleguest/src/main.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ use hyperlight_common::flatbuffer_wrappers::util::{
4646
get_flatbuffer_result_from_void,
4747
};
4848
use hyperlight_common::mem::PAGE_SIZE;
49-
use hyperlight_guest::alloca::_alloca;
5049
use hyperlight_guest::entrypoint::{abort_with_code, abort_with_code_and_message};
5150
use hyperlight_guest::error::{HyperlightGuestError, Result};
5251
use hyperlight_guest::guest_function_definition::GuestFunctionDefinition;
@@ -409,25 +408,6 @@ fn print_eleven_args(function_call: &FunctionCall) -> Result<Vec<u8>> {
409408
}
410409
}
411410

412-
fn stack_allocate(function_call: &FunctionCall) -> Result<Vec<u8>> {
413-
if let ParameterValue::Int(length) = function_call.parameters.clone().unwrap()[0].clone() {
414-
let alloc_length = if length == 0 {
415-
DEFAULT_GUEST_STACK_SIZE + 1
416-
} else {
417-
length
418-
};
419-
420-
_alloca(alloc_length as usize);
421-
422-
Ok(get_flatbuffer_result_from_int(alloc_length))
423-
} else {
424-
Err(HyperlightGuestError::new(
425-
ErrorCode::GuestFunctionParameterTypeMismatch,
426-
"Invalid parameters passed to stack_allocate".to_string(),
427-
))
428-
}
429-
}
430-
431411
fn buffer_overrun(function_call: &FunctionCall) -> Result<Vec<u8>> {
432412
if let ParameterValue::String(value) = function_call.parameters.clone().unwrap()[0].clone() {
433413
let c_str = value.as_str();
@@ -749,14 +729,6 @@ pub extern "C" fn hyperlight_main() {
749729
);
750730
register_function(print_using_printf_def);
751731

752-
let stack_allocate_def = GuestFunctionDefinition::new(
753-
"StackAllocate".to_string(),
754-
Vec::from(&[ParameterType::Int]),
755-
ReturnType::Int,
756-
stack_allocate as i64,
757-
);
758-
register_function(stack_allocate_def);
759-
760732
let stack_overflow_def = GuestFunctionDefinition::new(
761733
"StackOverflow".to_string(),
762734
Vec::from(&[ParameterType::Int]),

0 commit comments

Comments
 (0)