Skip to content

Commit f019c24

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 dbc2c50 commit f019c24

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

compiler/rustc_codegen_ssa/src/target_features.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ const WASM_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
305305

306306
const BPF_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[("alu32", Some(sym::bpf_target_feature))];
307307

308+
const SBF_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] =
309+
&[("alu32", Some(sym::sbf_target_feature)), ("static-syscalls", Some(sym::sbf_target_feature))];
310+
311+
308312
const CSKY_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
309313
// tidy-alphabetical-start
310314
("10e60", Some(sym::csky_target_feature)),
@@ -379,6 +383,7 @@ pub fn all_known_features() -> impl Iterator<Item = (&'static str, Option<Symbol
379383
.chain(RISCV_ALLOWED_FEATURES.iter())
380384
.chain(WASM_ALLOWED_FEATURES.iter())
381385
.chain(BPF_ALLOWED_FEATURES.iter())
386+
.chain(SBF_ALLOWED_FEATURES.iter())
382387
.chain(CSKY_ALLOWED_FEATURES)
383388
.chain(LOONGARCH_ALLOWED_FEATURES)
384389
.cloned()
@@ -397,7 +402,7 @@ pub fn supported_target_features(sess: &Session) -> &'static [(&'static str, Opt
397402
"csky" => CSKY_ALLOWED_FEATURES,
398403
"loongarch64" => LOONGARCH_ALLOWED_FEATURES,
399404
"bpf" => BPF_ALLOWED_FEATURES,
400-
"sbf" => BPF_ALLOWED_FEATURES,
405+
"sbf" => SBF_ALLOWED_FEATURES,
401406
_ => &[],
402407
}
403408
}

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)