-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsyscalls.rs
More file actions
156 lines (140 loc) · 4.35 KB
/
Copy pathsyscalls.rs
File metadata and controls
156 lines (140 loc) · 4.35 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#[cfg(target_arch = "riscv64")]
use core::arch::asm;
#[cfg(target_arch = "riscv64")]
// TODO: This should be properly defined
const MAX_PRIVATE_INPUT_SIZE: usize = 6700000;
#[cfg(target_arch = "riscv64")]
enum SyscallNumbers {
Print = 1,
Panic = 2,
GetPrivateInputs = 4,
Commit = 64,
Halt = 93,
}
#[cfg(target_arch = "riscv64")]
/// This is a template for printing in the vm
pub fn print_string(s: &str) {
unsafe {
asm!(
"ecall",
in("a0") s.as_ptr(),
in("a1") s.len(),
in("a7") SyscallNumbers::Print as usize,
);
}
}
#[cfg(not(target_arch = "riscv64"))]
/// This is a template for printing in the vm
pub fn print_string(_: &str) {
unimplemented!("syscalls are only implemented for riscv64 targets");
}
/// # Safety
///
/// This function should not be called by the user
/// It is only for rust std internal uses
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sys_write(_fildes: i32, buf: *const u8, size: usize) -> isize {
print_string("sys_write called\n");
let content = unsafe { core::slice::from_raw_parts(buf, size) };
print_string(&("SYS_WRITE: ".to_owned() + str::from_utf8(content).unwrap_or("<invalid utf8>"))); // Does the print of the sys write
size.try_into().unwrap_or(-1)
}
#[cfg(target_arch = "riscv64")]
/// # Safety
///
/// This function should not be called by the user
/// It is only for rust std internal uses
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sys_panic(msg_ptr: *const u8, len: usize) {
print_string("Sys panic called\n");
unsafe {
asm!(
"ecall",
in("a0") msg_ptr,
in("a1") len,
in("a7") SyscallNumbers::Panic as usize,
)
}
}
#[cfg(target_arch = "riscv64")]
pub fn commit(slice: &[u8]) {
unsafe {
asm!(
"ecall",
in("a0") 1usize,
in("a1") slice.as_ptr(),
in("a2") slice.len(),
in("a7") SyscallNumbers::Commit as usize,
)
}
}
#[cfg(target_arch = "riscv64")]
pub fn get_private_input() -> Result<Vec<u8>, SyscallError> {
print_string("get_private_input called\n");
let mut dest = vec![0u8; MAX_PRIVATE_INPUT_SIZE];
unsafe {
asm!(
"ecall",
in("a0") dest.as_mut_ptr(),
in("a7") SyscallNumbers::GetPrivateInputs as usize,
)
}
let len = u32::from_le_bytes(
dest[0..4]
.try_into()
.map_err(|_| SyscallError::WrongPrivateInputSize)?,
) as usize;
dest.drain(0..4);
dest.truncate(len);
Ok(dest)
}
#[derive(Debug)]
pub enum SyscallError {
WrongPrivateInputSize,
}
#[cfg(target_arch = "riscv64")]
pub fn sys_halt() -> ! {
// NOTE: no print_string here — the Print ecall is unmatched on the Ecall bus
// and would cause a verification failure.
unsafe {
asm!(
"ecall",
in("a0") 0usize, // exit_code = 0 (enforced by HALT read on x10)
in("a7") SyscallNumbers::Halt as usize,
);
}
unreachable!()
}
#[cfg(not(target_arch = "riscv64"))]
pub fn sys_halt() -> ! {
unimplemented!("syscalls are only implemented for riscv64 targets");
}
// =============================================================================
// Stub implementations for unsupported std functions
// These functions are required by Rust's std zkvm module but are not supported
// in Lambda VM. They will panic at runtime if called.
// =============================================================================
/// # Safety
///
/// This function is not supported in Lambda VM.
/// It will panic if called.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sys_read(_fd: u32, _buf: *mut u8, _nbytes: usize) -> usize {
panic!("sys_read is not supported: io::Read for Stdin is not implemented in Lambda VM");
}
/// # Safety
///
/// This function is not supported in Lambda VM.
/// It will panic if called.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sys_argc() -> usize {
panic!("sys_argc is not supported: command-line arguments are not available in Lambda VM");
}
/// # Safety
///
/// This function is not supported in Lambda VM.
/// It will panic if called.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sys_argv(_buf: *mut u32, _buf_nwords: usize, _arg_idx: usize) -> usize {
panic!("sys_argv is not supported: command-line arguments are not available in Lambda VM");
}