hyperlight_guest/
shared_input_data.rs

1/*
2Copyright 2024 The Hyperlight Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17use alloc::format;
18use alloc::string::ToString;
19use core::any::type_name;
20use core::slice::from_raw_parts_mut;
21
22use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;
23
24use crate::error::{HyperlightGuestError, Result};
25use crate::P_PEB;
26
27// Pops the top element from the shared input data buffer and returns it as a T
28pub fn try_pop_shared_input_data_into<T>() -> Result<T>
29where
30    T: for<'a> TryFrom<&'a [u8]>,
31{
32    let peb_ptr = unsafe { P_PEB.unwrap() };
33    let shared_buffer_size = unsafe { (*peb_ptr).input_stack.size as usize };
34
35    let idb =
36        unsafe { from_raw_parts_mut((*peb_ptr).input_stack.ptr as *mut u8, shared_buffer_size) };
37
38    if idb.is_empty() {
39        return Err(HyperlightGuestError::new(
40            ErrorCode::GuestError,
41            "Got a 0-size buffer in pop_shared_input_data_into".to_string(),
42        ));
43    }
44
45    // get relative offset to next free address
46    let stack_ptr_rel: u64 =
47        u64::from_le_bytes(idb[..8].try_into().expect("Shared input buffer too small"));
48
49    if stack_ptr_rel as usize > shared_buffer_size || stack_ptr_rel < 16 {
50        return Err(HyperlightGuestError::new(
51            ErrorCode::GuestError,
52            format!(
53                "Invalid stack pointer: {} in pop_shared_input_data_into",
54                stack_ptr_rel
55            ),
56        ));
57    }
58
59    // go back 8 bytes and read. This is the offset to the element on top of stack
60    let last_element_offset_rel = u64::from_le_bytes(
61        idb[stack_ptr_rel as usize - 8..stack_ptr_rel as usize]
62            .try_into()
63            .expect("Invalid stack pointer in pop_shared_input_data_into"),
64    );
65
66    let buffer = &idb[last_element_offset_rel as usize..];
67
68    // convert the buffer to T
69    let type_t = match T::try_from(buffer) {
70        Ok(t) => Ok(t),
71        Err(_e) => {
72            return Err(HyperlightGuestError::new(
73                ErrorCode::GuestError,
74                format!("Unable to convert buffer to {}", type_name::<T>()),
75            ));
76        }
77    };
78
79    // update the stack pointer to point to the element we just popped of since that is now free
80    idb[..8].copy_from_slice(&last_element_offset_rel.to_le_bytes());
81
82    // zero out popped off buffer
83    idb[last_element_offset_rel as usize..stack_ptr_rel as usize].fill(0);
84
85    type_t
86}