Skip to content

Commit 4728998

Browse files
committed
fixup! [common/input_output] making input_output stacks portable
1 parent 711387a commit 4728998

File tree

8 files changed

+41
-30
lines changed

8 files changed

+41
-30
lines changed

src/hyperlight_common/src/host_calling.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
use crate::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType, ReturnValue};
2-
use crate::input_output::{InputDataSection, OutputDataSection};
1+
use alloc::string::ToString;
2+
use alloc::vec::Vec;
3+
use core::ffi::c_char;
4+
35
use anyhow::Result;
6+
47
use crate::flatbuffer_wrappers::function_call::{FunctionCall, FunctionCallType};
8+
use crate::flatbuffer_wrappers::function_types::{ParameterValue, ReturnType, ReturnValue};
9+
use crate::input_output::{InputDataSection, OutputDataSection};
510
use crate::outb::{outb, OutBAction};
611
use crate::PEB;
712

8-
use alloc::vec::Vec;
9-
use alloc::string::ToString;
10-
use core::ffi::c_char;
11-
1213
/// Get a return value from a host function call.
1314
/// This usually requires a host function to be called first using `call_host_function`.
1415
pub fn get_host_return_value<T: TryFrom<ReturnValue>>() -> Result<T> {
@@ -19,7 +20,10 @@ pub fn get_host_return_value<T: TryFrom<ReturnValue>>() -> Result<T> {
1920
.expect("Unable to deserialize a return value from host");
2021

2122
T::try_from(return_value).map_err(|_| {
22-
anyhow::anyhow!("Host return value was not a {} as expected", core::any::type_name::<T>())
23+
anyhow::anyhow!(
24+
"Host return value was not a {} as expected",
25+
core::any::type_name::<T>()
26+
)
2327
})
2428
}
2529

@@ -44,8 +48,7 @@ pub fn call_host_function(
4448

4549
let output_data_section: OutputDataSection =
4650
unsafe { (*PEB).clone() }.get_output_data_region().into();
47-
output_data_section
48-
.push_shared_output_data(host_function_call_buffer)?;
51+
output_data_section.push_shared_output_data(host_function_call_buffer)?;
4952

5053
outb(OutBAction::CallFunction as u16, 0);
5154

@@ -66,4 +69,4 @@ pub fn print(message: &str) {
6669
#[no_mangle]
6770
pub unsafe extern "C" fn _putchar(c: c_char) {
6871
outb(OutBAction::DebugPrint as u16, c as u8);
69-
}
72+
}

src/hyperlight_common/src/input_output.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ 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-
use anyhow::{bail, Result};
1716
use alloc::vec::Vec;
1817
use core::slice::from_raw_parts_mut;
1918

19+
use anyhow::{bail, Result};
20+
2021
pub struct InputDataSection {
2122
ptr: *mut u8,
2223
len: u64,
@@ -33,9 +34,7 @@ impl InputDataSection {
3334
{
3435
let shared_buffer_size = self.len as usize;
3536

36-
let idb = unsafe {
37-
from_raw_parts_mut(self.ptr, shared_buffer_size)
38-
};
37+
let idb = unsafe { from_raw_parts_mut(self.ptr, shared_buffer_size) };
3938

4039
if idb.is_empty() {
4140
bail!("Got a 0-size buffer in try_pop_shared_input_data_into");
@@ -48,14 +47,17 @@ impl InputDataSection {
4847
});
4948

5049
if stack_ptr_rel as usize > shared_buffer_size || stack_ptr_rel < 16 {
51-
bail!("Invalid stack pointer: {} in try_pop_shared_input_data_into", stack_ptr_rel);
50+
bail!(
51+
"Invalid stack pointer: {} in try_pop_shared_input_data_into",
52+
stack_ptr_rel
53+
);
5254
}
5355

5456
// go back 8 bytes and read. This is the offset to the element on top of stack
5557
let last_element_offset_rel = u64::from_le_bytes(
5658
match idb[stack_ptr_rel as usize - 8..stack_ptr_rel as usize].try_into() {
5759
Ok(bytes) => bytes,
58-
Err(_) => bail!("Invalid stack pointer in pop_shared_input_data_into")
60+
Err(_) => bail!("Invalid stack pointer in pop_shared_input_data_into"),
5961
},
6062
);
6163

@@ -91,18 +93,18 @@ impl OutputDataSection {
9193

9294
pub fn push_shared_output_data(&self, data: Vec<u8>) -> Result<()> {
9395
let shared_buffer_size = self.len as usize;
94-
let odb: &mut [u8] =
95-
unsafe { from_raw_parts_mut(self.ptr, shared_buffer_size) };
96+
let odb: &mut [u8] = unsafe { from_raw_parts_mut(self.ptr, shared_buffer_size) };
9697

9798
if odb.len() < Self::STACK_PTR_SIZE {
9899
bail!("shared output buffer is too small");
99100
}
100101

101102
// get offset to next free address on the stack
102-
let mut stack_ptr_rel: u64 = u64::from_le_bytes(match odb[..Self::STACK_PTR_SIZE].try_into() {
103-
Ok(bytes) => bytes,
104-
Err(_) => bail!("failed to get stack pointer in shared output buffer"),
105-
});
103+
let mut stack_ptr_rel: u64 =
104+
u64::from_le_bytes(match odb[..Self::STACK_PTR_SIZE].try_into() {
105+
Ok(bytes) => bytes,
106+
Err(_) => bail!("failed to get stack pointer in shared output buffer"),
107+
});
106108

107109
// if stack_ptr_rel is 0, it means this is the first time we're using the output buffer, so
108110
// we want to offset it by 8 as to not overwrite the stack_ptr location.
@@ -152,4 +154,4 @@ impl From<(u64, u64)> for OutputDataSection {
152154
fn from((ptr, len): (u64, u64)) -> Self {
153155
OutputDataSection::new(ptr as *mut u8, len)
154156
}
155-
}
157+
}

src/hyperlight_common/src/outb.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use core::arch;
2+
23
use anyhow::{bail, Result};
4+
35
use crate::hyperlight_peb::RunMode;
46
use crate::RUNNING_MODE;
57

@@ -30,7 +32,9 @@ impl TryFrom<u16> for OutBAction {
3032

3133
/// Issues an OUTB instruction to the specified port with the given value.
3234
fn hloutb(port: u16, val: u8) {
33-
unsafe { arch::asm!("out dx, al", in("dx") port, in("al") val, options(preserves_flags, nomem, nostack)); }
35+
unsafe {
36+
arch::asm!("out dx, al", in("dx") port, in("al") val, options(preserves_flags, nomem, nostack));
37+
}
3438
}
3539

3640
pub fn outb(port: u16, value: u8) {

src/hyperlight_guest/src/entrypoint.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ use core::ffi::{c_char, CStr};
1818
use core::ptr::copy_nonoverlapping;
1919

2020
use hyperlight_common::hyperlight_peb::{HyperlightPEB, RunMode};
21-
use log::LevelFilter;
22-
use spin::Once;
2321
use hyperlight_common::outb::{outb, OutBAction};
2422
use hyperlight_common::{PEB, RUNNING_MODE};
23+
use log::LevelFilter;
24+
use spin::Once;
25+
2526
use crate::gdt::load_gdt;
2627
use crate::guest_error::reset_error;
2728
use crate::guest_function_call::dispatch_function;

src/hyperlight_guest/src/guest_error.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ use alloc::vec::Vec;
1919
use core::ffi::{c_char, CStr};
2020

2121
use hyperlight_common::flatbuffer_wrappers::guest_error::{ErrorCode, GuestError};
22-
use log::error;
2322
use hyperlight_common::outb::{outb, OutBAction};
2423
use hyperlight_common::PEB;
24+
use log::error;
25+
2526
use crate::entrypoint::halt;
2627

2728
pub(crate) fn write_error(error_code: ErrorCode, message: Option<&str>) {

src/hyperlight_guest/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,3 @@ pub static mut MIN_STACK_ADDRESS: u64 = 0;
9696

9797
pub(crate) static mut REGISTERED_GUEST_FUNCTIONS: GuestFunctionRegister =
9898
GuestFunctionRegister::new();
99-

src/hyperlight_host/src/sandbox/outb.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ use std::sync::{Arc, Mutex};
1919
use hyperlight_common::flatbuffer_wrappers::function_types::ParameterValue;
2020
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
2121
use hyperlight_common::flatbuffer_wrappers::guest_log_data::GuestLogData;
22+
use hyperlight_common::outb::OutBAction;
2223
use log::{Level, Record};
2324
use tracing::{instrument, Span};
2425
use tracing_log::format_trace;
25-
use hyperlight_common::outb::OutBAction;
26+
2627
use super::host_funcs::HostFuncsWrapper;
2728
use crate::hypervisor::handlers::{OutBHandler, OutBHandlerFunction, OutBHandlerWrapper};
2829
use crate::mem::mgr::SandboxMemoryManager;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use hyperlight_common::flatbuffer_wrappers::function_types::{
4040
use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
4141
use hyperlight_common::flatbuffer_wrappers::guest_log_level::LogLevel;
4242
use hyperlight_common::flatbuffer_wrappers::util::get_flatbuffer_result;
43+
use hyperlight_common::host_calling::{call_host_function, get_host_return_value, print};
4344
use hyperlight_common::PAGE_SIZE;
4445
use hyperlight_guest::entrypoint::{abort_with_code, abort_with_code_and_message};
4546
use hyperlight_guest::error::{HyperlightGuestError, Result};
@@ -48,7 +49,6 @@ use hyperlight_guest::guest_function_register::register_function;
4849
use hyperlight_guest::memory::malloc;
4950
use hyperlight_guest::{logging, MIN_STACK_ADDRESS};
5051
use log::{error, LevelFilter};
51-
use hyperlight_common::host_calling::{call_host_function, get_host_return_value, print};
5252

5353
extern crate hyperlight_guest;
5454

0 commit comments

Comments
 (0)