Skip to content

Commit 80c8acb

Browse files
alessandrodLucasSte
authored andcommitted
[SOL] sbf: implement static-syscalls target feature
Expose static-syscalls as a rust target-feature so it can be used with conditional compilation and implement sol_log_(), abort() and sol_alloc_free_() as static syscalls. This bumps src/llvm-project to pull the corresponding LLVM change.
1 parent fcacb1c commit 80c8acb

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

compiler/rustc_target/src/target_features.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ const WASM_ALLOWED_FEATURES: &[(&str, Stability)] = &[
327327

328328
const BPF_ALLOWED_FEATURES: &[(&str, Stability)] = &[("alu32", Unstable(sym::bpf_target_feature))];
329329

330+
const SBF_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] =
331+
&[("alu32", Unstable(sym::sbf_target_feature)), ("static-syscalls", Unstable(sym::sbf_target_feature))];
332+
330333
const CSKY_ALLOWED_FEATURES: &[(&str, Stability)] = &[
331334
// tidy-alphabetical-start
332335
("10e60", Unstable(sym::csky_target_feature)),

library/std/src/sys/sbf/alloc.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ unsafe impl GlobalAlloc for System {
3232
// // 0 as *mut u8
3333
// }
3434
}
35+
36+
#[cfg(not(target_feature = "static-syscalls"))]
3537
extern "C" {
3638
fn sol_alloc_free_(size: u64, ptr: u64) -> *mut u8;
3739
}
40+
41+
#[cfg(target_feature = "static-syscalls")]
42+
fn sol_alloc_free_(size: u64, ptr: u64) -> *mut u8 {
43+
let syscall: extern "C" fn(u64, u64) -> *mut u8 =
44+
unsafe { core::mem::transmute(2213547663u64) }; // murmur32 hash of "sol_alloc_free_"
45+
syscall(size, ptr)
46+
}

library/std/src/sys/sbf/mod.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ pub mod os;
2929
pub mod path;
3030
pub mod pipe;
3131
pub mod process;
32+
pub mod stdio;
3233
pub mod thread;
3334
pub mod time;
34-
pub mod stdio;
3535

3636
#[path = "../unix/os_str.rs"]
3737
pub mod os_str;
@@ -42,31 +42,50 @@ pub mod rwlock;
4242
pub mod thread_local_dtor;
4343
pub mod thread_local_key;
4444

45+
#[cfg(not(target_feature = "static-syscalls"))]
4546
extern "C" {
4647
fn abort() -> !;
4748
#[allow(improper_ctypes)]
4849
fn custom_panic(info: &core::panic::PanicInfo<'_>);
4950
fn sol_log_(message: *const u8, length: u64);
5051
}
5152

53+
#[cfg(target_feature = "static-syscalls")]
54+
unsafe extern "C" fn abort() -> ! {
55+
let syscall: extern "C" fn() -> ! = core::mem::transmute(3069975057u64); // murmur32 hash of "abort"
56+
syscall()
57+
}
58+
59+
#[cfg(target_feature = "static-syscalls")]
60+
unsafe extern "C" fn sol_log_(message: *const u8, length: u64) {
61+
let syscall: extern "C" fn(*const u8, u64) = core::mem::transmute(544561597u64); // murmur32 hash of "sol_log_"
62+
syscall(message, length)
63+
}
64+
5265
pub fn sol_log(message: &[u8]) {
5366
unsafe {
5467
sol_log_(message.as_ptr(), message.len() as u64);
5568
}
5669
}
5770

5871
pub fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
59-
unsafe { custom_panic(info); }
60-
unsafe { abort(); }
72+
unsafe {
73+
#[cfg(not(target_feature = "static-syscalls"))]
74+
custom_panic(info);
75+
76+
#[cfg(target_feature = "static-syscalls")]
77+
sol_log(info.to_string().as_bytes());
78+
79+
abort();
80+
}
6181
}
6282

6383
pub fn unsupported<T>() -> crate::io::Result<T> {
6484
Err(unsupported_err())
6585
}
6686

6787
pub fn unsupported_err() -> crate::io::Error {
68-
crate::io::Error::new(crate::io::ErrorKind::Other,
69-
"operation not supported on SBF yet")
88+
crate::io::Error::new(crate::io::ErrorKind::Other, "operation not supported on SBF yet")
7089
}
7190

7291
pub fn decode_error_kind(_code: i32) -> crate::io::ErrorKind {
@@ -84,7 +103,7 @@ pub unsafe fn strlen(mut s: *const c_char) -> usize {
84103
n += 1;
85104
s = s.offset(1);
86105
}
87-
return n
106+
return n;
88107
}
89108

90109
pub fn abort_internal() -> ! {

0 commit comments

Comments
 (0)