solana_define_syscall/
lib.rs

1#![no_std]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3
4pub mod definitions;
5
6#[cfg(any(
7    target_feature = "static-syscalls",
8    all(target_arch = "bpf", feature = "unstable-static-syscalls")
9))]
10#[macro_export]
11macro_rules! define_syscall {
12    ($(#[$attr:meta])* fn $name:ident($($arg:ident: $typ:ty),*) -> $ret:ty) => {
13        $(#[$attr])*
14        #[inline]
15        pub unsafe fn $name($($arg: $typ),*) -> $ret {
16            // this enum is used to force the hash to be computed in a const context
17            #[repr(usize)]
18            enum Syscall {
19                Code = $crate::sys_hash(stringify!($name)),
20            }
21
22            let syscall: extern "C" fn($($arg: $typ),*) -> $ret = core::mem::transmute(Syscall::Code);
23            syscall($($arg),*)
24        }
25
26    };
27    ($(#[$attr:meta])* fn $name:ident($($arg:ident: $typ:ty),*)) => {
28        define_syscall!($(#[$attr])* fn $name($($arg: $typ),*) -> ());
29    }
30}
31
32#[cfg(not(any(
33    target_feature = "static-syscalls",
34    all(target_arch = "bpf", feature = "unstable-static-syscalls")
35)))]
36#[macro_export]
37macro_rules! define_syscall {
38    ($(#[$attr:meta])* fn $name:ident($($arg:ident: $typ:ty),*) -> $ret:ty) => {
39        extern "C" {
40            $(#[$attr])*
41            pub fn $name($($arg: $typ),*) -> $ret;
42        }
43    };
44    ($(#[$attr:meta])* fn $name:ident($($arg:ident: $typ:ty),*)) => {
45        define_syscall!($(#[$attr])* fn $name($($arg: $typ),*) -> ());
46    }
47}
48
49#[cfg(any(
50    target_feature = "static-syscalls",
51    all(target_arch = "bpf", feature = "unstable-static-syscalls")
52))]
53pub const fn sys_hash(name: &str) -> usize {
54    murmur3_32(name.as_bytes(), 0) as usize
55}
56
57#[cfg(any(
58    target_feature = "static-syscalls",
59    all(target_arch = "bpf", feature = "unstable-static-syscalls")
60))]
61const fn murmur3_32(buf: &[u8], seed: u32) -> u32 {
62    const fn pre_mix(buf: [u8; 4]) -> u32 {
63        u32::from_le_bytes(buf)
64            .wrapping_mul(0xcc9e2d51)
65            .rotate_left(15)
66            .wrapping_mul(0x1b873593)
67    }
68
69    let mut hash = seed;
70
71    let mut i = 0;
72    while i < buf.len() / 4 {
73        let buf = [buf[i * 4], buf[i * 4 + 1], buf[i * 4 + 2], buf[i * 4 + 3]];
74        hash ^= pre_mix(buf);
75        hash = hash.rotate_left(13);
76        hash = hash.wrapping_mul(5).wrapping_add(0xe6546b64);
77
78        i += 1;
79    }
80
81    match buf.len() % 4 {
82        0 => {}
83        1 => {
84            hash = hash ^ pre_mix([buf[i * 4], 0, 0, 0]);
85        }
86        2 => {
87            hash = hash ^ pre_mix([buf[i * 4], buf[i * 4 + 1], 0, 0]);
88        }
89        3 => {
90            hash = hash ^ pre_mix([buf[i * 4], buf[i * 4 + 1], buf[i * 4 + 2], 0]);
91        }
92        _ => { /* unreachable!() */ }
93    }
94
95    hash = hash ^ buf.len() as u32;
96    hash = hash ^ (hash.wrapping_shr(16));
97    hash = hash.wrapping_mul(0x85ebca6b);
98    hash = hash ^ (hash.wrapping_shr(13));
99    hash = hash.wrapping_mul(0xc2b2ae35);
100    hash = hash ^ (hash.wrapping_shr(16));
101
102    hash
103}