hyperlight_guest/
shared_input_data.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
Copyright 2024 The Hyperlight Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

use alloc::format;
use alloc::string::ToString;
use core::any::type_name;
use core::slice::from_raw_parts_mut;

use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode;

use crate::error::{HyperlightGuestError, Result};
use crate::P_PEB;

// Pops the top element from the shared input data buffer and returns it as a T
pub fn try_pop_shared_input_data_into<T>() -> Result<T>
where
    T: for<'a> TryFrom<&'a [u8]>,
{
    let peb_ptr = unsafe { P_PEB.unwrap() };
    let shared_buffer_size = unsafe { (*peb_ptr).inputdata.inputDataSize as usize };

    let idb = unsafe {
        from_raw_parts_mut(
            (*peb_ptr).inputdata.inputDataBuffer as *mut u8,
            shared_buffer_size,
        )
    };

    if idb.is_empty() {
        return Err(HyperlightGuestError::new(
            ErrorCode::GuestError,
            "Got a 0-size buffer in pop_shared_input_data_into".to_string(),
        ));
    }

    // get relative offset to next free address
    let stack_ptr_rel: usize =
        usize::from_le_bytes(idb[..8].try_into().expect("Shared input buffer too small"));

    if stack_ptr_rel > shared_buffer_size || stack_ptr_rel < 16 {
        return Err(HyperlightGuestError::new(
            ErrorCode::GuestError,
            format!(
                "Invalid stack pointer: {} in pop_shared_input_data_into",
                stack_ptr_rel
            ),
        ));
    }

    // go back 8 bytes and read. This is the offset to the element on top of stack
    let last_element_offset_rel = usize::from_le_bytes(
        idb[stack_ptr_rel - 8..stack_ptr_rel]
            .try_into()
            .expect("Invalid stack pointer in pop_shared_input_data_into"),
    );

    let buffer = &idb[last_element_offset_rel..];

    // convert the buffer to T
    let type_t = match T::try_from(buffer) {
        Ok(t) => Ok(t),
        Err(_e) => {
            return Err(HyperlightGuestError::new(
                ErrorCode::GuestError,
                format!("Unable to convert buffer to {}", type_name::<T>()),
            ));
        }
    };

    // update the stack pointer to point to the element we just popped of since that is now free
    idb[..8].copy_from_slice(&last_element_offset_rel.to_le_bytes());

    // zero out popped off buffer
    idb[last_element_offset_rel..stack_ptr_rel].fill(0);

    type_t
}