Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions c-gull/src/use_libc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ macro_rules! checked_cast {
let src_ptr = $ptr;
let target_ptr = src_ptr.cast();

#[allow(unnecessary_transmutes)]
if false {
let target = crate::use_libc::Pad::new(core::ptr::read(target_ptr));

Expand Down
4 changes: 2 additions & 2 deletions c-scape/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ cc = { version = "1.0.68", optional = true }
[dependencies]
libm = "0.2.1"
rustix = { version = "1.0.0", default-features = false, features = ["event", "fs", "mm", "net", "param", "pipe", "process", "pty", "rand", "runtime", "shm", "stdio", "system", "termios", "thread", "time"] }
rustix-futex-sync = { version = "0.3.0", features = ["atomic_usize"] }
rustix-futex-sync = { version = "0.4.0", features = ["atomic_usize"] }
memoffset = "0.9.0"
realpath-ext = { version = "0.1.0", default-features = false }
origin = { version = "0.25.0", default-features = false, features = ["init-fini-arrays", "program-at-exit", "thread-at-exit", "nightly", "getauxval"] }
origin = { version = "0.26.1", default-features = false, features = ["init-fini-arrays", "program-at-exit", "thread-at-exit", "nightly", "getauxval"] }
# We use the libc crate for C ABI types and constants, but we don't depend on
# the actual platform libc.
libc = { version = "0.2.155", default-features = false }
Expand Down
6 changes: 3 additions & 3 deletions c-scape/src/fs/dir/readdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ unsafe extern "C" fn readdir64_r(
d_name: [0; 256],
};
let len = core::cmp::min(256, e.file_name().to_bytes().len());
(*entry).d_name[..len].copy_from_slice(transmute(e.file_name().to_bytes()));
(&mut *entry).d_name[..len].copy_from_slice(transmute(e.file_name().to_bytes()));
*ptr = entry;
0
}
Expand Down Expand Up @@ -79,7 +79,7 @@ unsafe extern "C" fn readdir64(dir: *mut libc::DIR) -> *mut libc::dirent64 {
d_name: [0; 256],
};
let len = core::cmp::min(256, e.file_name().to_bytes().len());
(*c_scape_dir).storage.dirent64.d_name[..len]
(&mut *c_scape_dir).storage.dirent64.d_name[..len]
.copy_from_slice(transmute(e.file_name().to_bytes()));
&mut (*c_scape_dir).storage.dirent64
}
Expand Down Expand Up @@ -127,7 +127,7 @@ unsafe extern "C" fn readdir(dir: *mut libc::DIR) -> *mut libc::dirent {
};

let len = core::cmp::min(256, e.file_name().to_bytes().len());
(*c_scape_dir).storage.dirent.d_name[..len]
(&mut *c_scape_dir).storage.dirent.d_name[..len]
.copy_from_slice(transmute(e.file_name().to_bytes()));
&mut (*c_scape_dir).storage.dirent
}
Expand Down
99 changes: 25 additions & 74 deletions c-scape/src/fs/xattr.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! Extended attributes.

use crate::convert_res;
use alloc::vec;
use core::ffi::CStr;
use core::ptr::copy_nonoverlapping;
use core::mem::MaybeUninit;
use core::slice;
use libc::{c_char, c_int, c_void, size_t, ssize_t};
use rustix::fd::BorrowedFd;
Expand All @@ -20,18 +19,10 @@ unsafe extern "C" fn getxattr(

let path = CStr::from_ptr(path);
let name = CStr::from_ptr(name);
// `slice::from_raw_parts_mut` assumes that the memory is initialized,
// which our C API here doesn't guarantee. Since rustix currently requires
// a slice, use a temporary copy.
let mut buf = vec![0; len];
match convert_res(rustix::fs::getxattr(path, name, &mut buf)) {
Some(size) => {
// If `size` is 0, `value` could be null.
if size != 0 {
copy_nonoverlapping(buf.as_ptr(), value.cast(), size);
}
size as ssize_t
}
let buf = slice::from_raw_parts_mut(value.cast::<MaybeUninit<u8>>(), len);

match convert_res(rustix::fs::getxattr(path, name, buf)) {
Some((init, _uninit)) => init.len() as ssize_t,
None => -1,
}
}
Expand All @@ -47,18 +38,10 @@ unsafe extern "C" fn lgetxattr(

let path = CStr::from_ptr(path);
let name = CStr::from_ptr(name);
// `slice::from_raw_parts_mut` assumes that the memory is initialized,
// which our C API here doesn't guarantee. Since rustix currently requires
// a slice, use a temporary copy.
let mut buf = vec![0; len];
match convert_res(rustix::fs::lgetxattr(path, name, &mut buf)) {
Some(size) => {
// If `size` is 0, `value` could be null.
if size != 0 {
copy_nonoverlapping(buf.as_ptr(), value.cast(), size);
}
size as ssize_t
}
let buf = slice::from_raw_parts_mut(value.cast::<MaybeUninit<u8>>(), len);

match convert_res(rustix::fs::lgetxattr(path, name, buf)) {
Some((init, _uninit)) => init.len() as ssize_t,
None => -1,
}
}
Expand All @@ -74,18 +57,10 @@ unsafe extern "C" fn fgetxattr(

let fd = BorrowedFd::borrow_raw(fd);
let name = CStr::from_ptr(name);
// `slice::from_raw_parts_mut` assumes that the memory is initialized,
// which our C API here doesn't guarantee. Since rustix currently requires
// a slice, use a temporary copy.
let mut buf = vec![0; len];
match convert_res(rustix::fs::fgetxattr(fd, name, &mut buf)) {
Some(size) => {
// If `size` is 0, `value` could be null.
if size != 0 {
copy_nonoverlapping(buf.as_ptr(), value.cast(), size);
}
size as ssize_t
}
let buf = slice::from_raw_parts_mut(value.cast::<MaybeUninit<u8>>(), len);

match convert_res(rustix::fs::fgetxattr(fd, name, buf)) {
Some((init, _uninit)) => init.len() as ssize_t,
None => -1,
}
}
Expand Down Expand Up @@ -155,18 +130,10 @@ unsafe extern "C" fn listxattr(path: *const c_char, list: *mut c_char, len: size
libc!(libc::listxattr(path, list, len));

let path = CStr::from_ptr(path);
// `slice::from_raw_parts_mut` assumes that the memory is initialized,
// which our C API here doesn't guarantee. Since rustix currently requires
// a slice, use a temporary copy.
let mut buf = vec![0; len];
match convert_res(rustix::fs::listxattr(path, &mut buf)) {
Some(size) => {
// If `size` is 0, `value` could be null.
if size != 0 {
copy_nonoverlapping(buf.as_ptr(), list.cast(), size);
}
size as ssize_t
}
let buf = slice::from_raw_parts_mut(list.cast::<MaybeUninit<u8>>(), len);

match convert_res(rustix::fs::listxattr(path, buf)) {
Some((init, _uninit)) => init.len() as ssize_t,
None => -1,
}
}
Expand All @@ -176,18 +143,10 @@ unsafe extern "C" fn llistxattr(path: *const c_char, list: *mut c_char, len: siz
libc!(libc::llistxattr(path, list, len));

let path = CStr::from_ptr(path);
// `slice::from_raw_parts_mut` assumes that the memory is initialized,
// which our C API here doesn't guarantee. Since rustix currently requires
// a slice, use a temporary copy.
let mut buf = vec![0; len];
match convert_res(rustix::fs::llistxattr(path, &mut buf)) {
Some(size) => {
// If `size` is 0, `value` could be null.
if size != 0 {
copy_nonoverlapping(buf.as_ptr(), list.cast(), size);
}
size as ssize_t
}
let buf = slice::from_raw_parts_mut(list.cast::<MaybeUninit<u8>>(), len);

match convert_res(rustix::fs::llistxattr(path, buf)) {
Some((init, _uninit)) => init.len() as ssize_t,
None => -1,
}
}
Expand All @@ -197,18 +156,10 @@ unsafe extern "C" fn flistxattr(fd: c_int, list: *mut c_char, len: size_t) -> ss
libc!(libc::flistxattr(fd, list, len));

let fd = BorrowedFd::borrow_raw(fd);
// `slice::from_raw_parts_mut` assumes that the memory is initialized,
// which our C API here doesn't guarantee. Since rustix currently requires
// a slice, use a temporary copy.
let mut buf = vec![0; len];
match convert_res(rustix::fs::flistxattr(fd, &mut buf)) {
Some(size) => {
// If `size` is 0, `value` could be null.
if size != 0 {
copy_nonoverlapping(buf.as_ptr(), list.cast(), size);
}
size as ssize_t
}
let buf = slice::from_raw_parts_mut(list.cast::<MaybeUninit<u8>>(), len);

match convert_res(rustix::fs::flistxattr(fd, buf)) {
Some((init, _uninit)) => init.len() as ssize_t,
None => -1,
}
}
Expand Down
6 changes: 3 additions & 3 deletions c-scape/src/jmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type sigjmp_buf = *mut c_void;
target_arch = "x86_64",
target_arch = "x86"
),
naked
unsafe(naked)
)]
unsafe extern "C" fn setjmp(env: jmp_buf) -> c_int {
//libc!(libc::setjmp(env));
Expand Down Expand Up @@ -191,7 +191,7 @@ core::arch::global_asm!(".globl _setjmp", ".set _setjmp, setjmp");
target_arch = "x86_64",
target_arch = "x86"
),
naked
unsafe(naked)
)]
unsafe extern "C" fn longjmp(env: jmp_buf, val: c_int) -> ! {
//libc!(libc::longjmp(env, val));
Expand Down Expand Up @@ -358,7 +358,7 @@ core::arch::global_asm!(".globl _longjmp", ".set _longjmp, longjmp");
target_arch = "x86_64",
target_arch = "x86"
),
naked
unsafe(naked)
)]
unsafe extern "C" fn sigsetjmp(_env: sigjmp_buf, _savesigs: c_int) -> c_int {
//libc!(libc::sigsetjmp(env, savesigs));
Expand Down
1 change: 0 additions & 1 deletion c-scape/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#![feature(c_variadic)] // for `printf`, `ioctl`, etc.
#![feature(sync_unsafe_cell)] // for lots of libc static variables
#![feature(linkage)] // for `malloc` etc.
#![feature(naked_functions)] // for `setjmp` etc.
// Disable some common warnings.
#![allow(unexpected_cfgs)]
// Don't warn if `try_into()` is fallible on some targets.
Expand Down
1 change: 1 addition & 0 deletions c-scape/src/use_libc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ macro_rules! checked_cast {
let src_ptr = $ptr;
let target_ptr = src_ptr.cast();

#[allow(unnecessary_transmutes)]
if false {
let target = crate::use_libc::Pad::new(core::ptr::read(target_ptr));

Expand Down
2 changes: 1 addition & 1 deletion example-crates/c-scape-unwinding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ package = "c-scape"
errno = { version = "0.3.3", default-features = false }
rustix-dlmalloc = { version = "0.2.1", features = ["global"] }
# Depend on `unwinding` so that we can do `catch_unwind`.
unwinding = { version = "0.2.3", default-features = false, features = ["panic"] }
unwinding = { version = "0.2.6", default-features = false, features = ["panic"] }

# This is just an example crate, and not part of the c-ward workspace.
[workspace]
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2025-01-02"
channel = "nightly-2025-04-28"
components = ["rustc", "cargo", "rust-std", "rust-src", "rustfmt"]
2 changes: 1 addition & 1 deletion tests/example_crates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn example_crate_c_gull_unwinding() {
&[],
&[("RUST_BACKTRACE", "0")],
"Hello, world!\nHello world using libc `printf`!\n",
"thread 'main' panicked at src/main.rs:18:5:\ncatch me!\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace\n",
"\nthread 'main' panicked at src/main.rs:18:5:\ncatch me!\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace\n",
None,
);
}
Expand Down