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).inputdata.inputDataSize as usize };
34
35    let idb = unsafe {
36        from_raw_parts_mut(
37            (*peb_ptr).inputdata.inputDataBuffer as *mut u8,
38            shared_buffer_size,
39        )
40    };
41
42    if idb.is_empty() {
43        return Err(HyperlightGuestError::new(
44            ErrorCode::GuestError,
45            "Got a 0-size buffer in pop_shared_input_data_into".to_string(),
46        ));
47    }
48
49    // get relative offset to next free address
50    let stack_ptr_rel: usize =
51        usize::from_le_bytes(idb[..8].try_into().expect("Shared input buffer too small"));
52
53    if stack_ptr_rel > shared_buffer_size || stack_ptr_rel < 16 {
54        return Err(HyperlightGuestError::new(
55            ErrorCode::GuestError,
56            format!(
57                "Invalid stack pointer: {} in pop_shared_input_data_into",
58                stack_ptr_rel
59            ),
60        ));
61    }
62
63    // go back 8 bytes and read. This is the offset to the element on top of stack
64    let last_element_offset_rel = usize::from_le_bytes(
65        idb[stack_ptr_rel - 8..stack_ptr_rel]
66            .try_into()
67            .expect("Invalid stack pointer in pop_shared_input_data_into"),
68    );
69
70    let buffer = &idb[last_element_offset_rel..];
71
72    // convert the buffer to T
73    let type_t = match T::try_from(buffer) {
74        Ok(t) => Ok(t),
75        Err(_e) => {
76            return Err(HyperlightGuestError::new(
77                ErrorCode::GuestError,
78                format!("Unable to convert buffer to {}", type_name::<T>()),
79            ));
80        }
81    };
82
83    // update the stack pointer to point to the element we just popped of since that is now free
84    idb[..8].copy_from_slice(&last_element_offset_rel.to_le_bytes());
85
86    // zero out popped off buffer
87    idb[last_element_offset_rel..stack_ptr_rel].fill(0);
88
89    type_t
90}