Skip to content

Commit

Permalink
Merge pull request #1216 from CarlWachter/fix/extern_C_no_mangle
Browse files Browse the repository at this point in the history
Added no_mangle to all extern C functions
  • Loading branch information
mkroening authored May 22, 2024
2 parents 20d6c06 + df197d2 commit a54f7f7
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 3 deletions.
21 changes: 18 additions & 3 deletions hermit-macro/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,27 @@ fn parse_sig(sig: &Signature) -> Result<ParsedSig> {
}

fn validate_attrs(attrs: &[Attribute]) -> Result<()> {
let mut no_mangle_found = false;
for attr in attrs {
if !attr.path().is_ident("cfg") && !attr.path().is_ident("doc") {
if !attr.path().is_ident("cfg")
&& !attr.path().is_ident("doc")
&& !attr.path().is_ident("no_mangle")
{
bail!(
attr,
"#[system] functions may only have `#[doc]` and `#[cfg]` attributes"
"#[system] functions may only have `#[doc]`, `#[no_mangle]` and `#[cfg]` attributes"
);
}
if attr.path().is_ident("no_mangle") {
no_mangle_found = true;
}
}

if !no_mangle_found {
bail!(
attrs.first(),
"#[system] functions must have `#[no_mangle]` attribute"
);
}

Ok(())
Expand Down Expand Up @@ -96,7 +110,6 @@ fn emit_func(mut func: ItemFn, sig: &ParsedSig) -> Result<ItemFn> {

let func = parse_quote! {
#(#attrs)*
#[no_mangle]
#vis #sig {
#func

Expand Down Expand Up @@ -128,6 +141,7 @@ mod tests {
///
/// This is very important.
#[cfg(target_os = "none")]
#[no_mangle]
pub extern "C" fn sys_test(a: i8, b: i16) -> i32 {
let c = i16::from(a) + b;
i32::from(c)
Expand Down Expand Up @@ -164,6 +178,7 @@ mod tests {
///
/// This is very important.
#[cfg(target_os = "none")]
#[no_mangle]
pub unsafe extern "C" fn sys_test(a: i8, b: i16) -> i32 {
let c = i16::from(a) + b;
i32::from(c)
Expand Down
5 changes: 5 additions & 0 deletions src/syscalls/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl CondQueue {
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_destroy_queue(ptr: usize) -> i32 {
unsafe {
let id = ptr::with_exposed_provenance_mut::<usize>(ptr);
Expand All @@ -42,6 +43,7 @@ pub unsafe extern "C" fn sys_destroy_queue(ptr: usize) -> i32 {
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_notify(ptr: usize, count: i32) -> i32 {
unsafe {
let id = ptr::with_exposed_provenance::<usize>(ptr);
Expand Down Expand Up @@ -79,6 +81,7 @@ pub unsafe extern "C" fn sys_notify(ptr: usize, count: i32) -> i32 {
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_init_queue(ptr: usize) -> i32 {
unsafe {
let id = ptr::with_exposed_provenance_mut::<usize>(ptr);
Expand All @@ -98,6 +101,7 @@ pub unsafe extern "C" fn sys_init_queue(ptr: usize) -> i32 {
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_add_queue(ptr: usize, timeout_ns: i64) -> i32 {
unsafe {
let id = ptr::with_exposed_provenance_mut::<usize>(ptr);
Expand Down Expand Up @@ -126,6 +130,7 @@ pub unsafe extern "C" fn sys_add_queue(ptr: usize, timeout_ns: i64) -> i32 {
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_wait(ptr: usize) -> i32 {
unsafe {
let id = ptr::with_exposed_provenance_mut::<usize>(ptr);
Expand Down
4 changes: 4 additions & 0 deletions src/syscalls/entropy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub unsafe extern "C" fn sys_read_entropy(buf: *mut u8, len: usize, flags: u32)
/// the function returns `-1`.
#[cfg(not(feature = "newlib"))]
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_secure_rand32(value: *mut u32) -> i32 {
let mut buf = value.cast();
let mut len = size_of::<u32>();
Expand All @@ -84,6 +85,7 @@ pub unsafe extern "C" fn sys_secure_rand32(value: *mut u32) -> i32 {
/// the function returns -1.
#[cfg(not(feature = "newlib"))]
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_secure_rand64(value: *mut u64) -> i32 {
let mut buf = value.cast();
let mut len = size_of::<u64>();
Expand All @@ -103,13 +105,15 @@ pub unsafe extern "C" fn sys_secure_rand64(value: *mut u64) -> i32 {
/// The function computes a sequence of pseudo-random integers
/// in the range of 0 to RAND_MAX
#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_rand() -> u32 {
generate_park_miller_lehmer_random_number()
}

/// The function sets its argument as the seed for a new sequence
/// of pseudo-random numbers to be returned by rand()
#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_srand(seed: u32) {
*(PARK_MILLER_LEHMER_SEED.lock()) = seed;
}
Expand Down
2 changes: 2 additions & 0 deletions src/syscalls/futex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::time::timespec;
/// * `timeout` is negative
/// * `flags` contains unknown flags
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_futex_wait(
address: *mut u32,
expected: u32,
Expand Down Expand Up @@ -42,6 +43,7 @@ pub unsafe extern "C" fn sys_futex_wait(
///
/// Returns -EINVAL if `address` is null.
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_futex_wake(address: *mut u32, count: i32) -> i32 {
if address.is_null() {
return -EINVAL;
Expand Down
5 changes: 5 additions & 0 deletions src/syscalls/lwip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,33 @@ use crate::arch::core_local::core_scheduler;
use crate::{arch, console};

#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_lwip_get_errno() -> i32 {
core_scheduler().get_lwip_errno()
}

#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_lwip_set_errno(errno: i32) {
core_scheduler().set_lwip_errno(errno);
}

#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_acquire_putchar_lock() {
// FIXME: use core-local storage instead
// better yet: remove and replace all of this
MutexGuard::leak(console::CONSOLE.lock());
}

#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_putchar(character: u8) {
arch::output_message_buf(&[character]);
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_release_putchar_lock() {
unsafe {
console::CONSOLE.force_unlock();
Expand Down
26 changes: 26 additions & 0 deletions src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub(crate) fn init() {
///
#[cfg(all(target_os = "none", not(feature = "common-os")))]
#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_alloc(size: usize, align: usize) -> *mut u8 {
let layout_res = Layout::from_size_align(size, align);
if layout_res.is_err() || size == 0 {
Expand All @@ -106,6 +107,7 @@ pub extern "C" fn sys_alloc(size: usize, align: usize) -> *mut u8 {

#[cfg(all(target_os = "none", not(feature = "common-os")))]
#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_alloc_zeroed(size: usize, align: usize) -> *mut u8 {
let layout_res = Layout::from_size_align(size, align);
if layout_res.is_err() || size == 0 {
Expand All @@ -130,6 +132,7 @@ pub extern "C" fn sys_alloc_zeroed(size: usize, align: usize) -> *mut u8 {

#[cfg(all(target_os = "none", not(feature = "common-os")))]
#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_malloc(size: usize, align: usize) -> *mut u8 {
let layout_res = Layout::from_size_align(size, align);
if layout_res.is_err() || size == 0 {
Expand Down Expand Up @@ -173,6 +176,7 @@ pub extern "C" fn sys_malloc(size: usize, align: usize) -> *mut u8 {
/// allocator, or if reallocation otherwise fails.
#[cfg(all(target_os = "none", not(feature = "common-os")))]
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_realloc(
ptr: *mut u8,
size: usize,
Expand Down Expand Up @@ -219,6 +223,7 @@ pub unsafe extern "C" fn sys_realloc(
/// May panic if debug assertions are enabled and invalid parameters `size` or `align` where passed.
#[cfg(all(target_os = "none", not(feature = "common-os")))]
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_dealloc(ptr: *mut u8, size: usize, align: usize) {
unsafe {
let layout_res = Layout::from_size_align(size, align);
Expand All @@ -243,6 +248,7 @@ pub unsafe extern "C" fn sys_dealloc(ptr: *mut u8, size: usize, align: usize) {

#[cfg(all(target_os = "none", not(feature = "common-os")))]
#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_free(ptr: *mut u8, size: usize, align: usize) {
unsafe {
let layout_res = Layout::from_size_align(size, align);
Expand Down Expand Up @@ -278,11 +284,13 @@ pub(crate) fn shutdown(arg: i32) -> ! {
}

#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_shutdown(arg: i32) -> ! {
shutdown(arg)
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_unlink(name: *const u8) -> i32 {
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();

Expand All @@ -294,6 +302,7 @@ pub unsafe extern "C" fn sys_unlink(name: *const u8) -> i32 {
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_mkdir(name: *const u8, mode: u32) -> i32 {
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();
let mode = if let Some(mode) = AccessPermission::from_bits(mode) {
Expand All @@ -310,6 +319,7 @@ pub unsafe extern "C" fn sys_mkdir(name: *const u8, mode: u32) -> i32 {
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_rmdir(name: *const c_char) -> i32 {
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();

Expand All @@ -321,6 +331,7 @@ pub unsafe extern "C" fn sys_rmdir(name: *const c_char) -> i32 {
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_stat(name: *const c_char, stat: *mut FileAttr) -> i32 {
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();

Expand All @@ -334,6 +345,7 @@ pub unsafe extern "C" fn sys_stat(name: *const c_char, stat: *mut FileAttr) -> i
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_lstat(name: *const c_char, stat: *mut FileAttr) -> i32 {
let name = unsafe { CStr::from_ptr(name as _) }.to_str().unwrap();

Expand All @@ -347,6 +359,7 @@ pub unsafe extern "C" fn sys_lstat(name: *const c_char, stat: *mut FileAttr) ->
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_fstat(fd: FileDescriptor, stat: *mut FileAttr) -> i32 {
let stat = unsafe { &mut *stat };
let obj = get_object(fd);
Expand All @@ -360,6 +373,7 @@ pub unsafe extern "C" fn sys_fstat(fd: FileDescriptor, stat: *mut FileAttr) -> i
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_opendir(name: *const c_char) -> FileDescriptor {
if let Ok(name) = unsafe { CStr::from_ptr(name as _) }.to_str() {
crate::fs::opendir(name).unwrap_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap())
Expand All @@ -369,6 +383,7 @@ pub unsafe extern "C" fn sys_opendir(name: *const c_char) -> FileDescriptor {
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_open(name: *const c_char, flags: i32, mode: u32) -> FileDescriptor {
let flags = if let Some(flags) = OpenOption::from_bits(flags) {
flags
Expand All @@ -390,12 +405,14 @@ pub unsafe extern "C" fn sys_open(name: *const c_char, flags: i32, mode: u32) ->
}

#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_close(fd: FileDescriptor) -> i32 {
let obj = remove_object(fd);
obj.map_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap(), |_| 0)
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_read(fd: FileDescriptor, buf: *mut u8, len: usize) -> isize {
let slice = unsafe { core::slice::from_raw_parts_mut(buf, len) };
crate::fd::read(fd, slice).map_or_else(
Expand All @@ -413,11 +430,13 @@ unsafe fn write(fd: FileDescriptor, buf: *const u8, len: usize) -> isize {
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_write(fd: FileDescriptor, buf: *const u8, len: usize) -> isize {
unsafe { write(fd, buf, len) }
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_ioctl(
fd: FileDescriptor,
cmd: i32,
Expand All @@ -443,6 +462,7 @@ pub unsafe extern "C" fn sys_ioctl(

/// manipulate file descriptor
#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_fcntl(fd: i32, cmd: i32, arg: i32) -> i32 {
const F_SETFD: i32 = 2;
const F_SETFL: i32 = 4;
Expand All @@ -466,6 +486,7 @@ pub extern "C" fn sys_fcntl(fd: i32, cmd: i32, arg: i32) -> i32 {
}

#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_lseek(fd: FileDescriptor, offset: isize, whence: i32) -> isize {
let obj = get_object(fd);
obj.map_or_else(
Expand Down Expand Up @@ -493,6 +514,7 @@ pub struct Dirent64 {
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_getdents64(
fd: FileDescriptor,
dirp: *mut Dirent64,
Expand Down Expand Up @@ -546,11 +568,13 @@ pub unsafe extern "C" fn sys_getdents64(
}

#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_dup(fd: i32) -> i32 {
dup_object(fd).unwrap_or_else(|e| -num::ToPrimitive::to_i32(&e).unwrap())
}

#[hermit_macro::system]
#[no_mangle]
pub unsafe extern "C" fn sys_poll(fds: *mut PollFd, nfds: usize, timeout: i32) -> i32 {
let slice = unsafe { core::slice::from_raw_parts_mut(fds, nfds) };
let timeout = if timeout >= 0 {
Expand All @@ -574,6 +598,7 @@ pub unsafe extern "C" fn sys_poll(fds: *mut PollFd, nfds: usize, timeout: i32) -
}

#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_eventfd(initval: u64, flags: i16) -> i32 {
if let Some(flags) = EventFlags::from_bits(flags) {
crate::fd::eventfd(initval, flags)
Expand All @@ -584,6 +609,7 @@ pub extern "C" fn sys_eventfd(initval: u64, flags: i16) -> i32 {
}

#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_image_start_addr() -> usize {
crate::mm::kernel_start_address().0.try_into().unwrap()
}
3 changes: 3 additions & 0 deletions src/syscalls/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ use crate::arch::get_processor_count;

/// Returns the number of processors currently online.
#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_get_processor_count() -> usize {
get_processor_count().try_into().unwrap()
}

#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_available_parallelism() -> usize {
get_processor_count().try_into().unwrap()
}

/// Returns the processor frequency in MHz.
#[hermit_macro::system]
#[no_mangle]
pub extern "C" fn sys_get_processor_frequency() -> u16 {
crate::arch::processor::get_frequency()
}
Loading

0 comments on commit a54f7f7

Please sign in to comment.