Skip to content

Commit

Permalink
Implement runtime::brk. (#891)
Browse files Browse the repository at this point in the history
`brk` is used by some `malloc` implementations.
  • Loading branch information
sunfishcode authored Oct 22, 2023
1 parent 414309a commit b91ba31
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/backend/linux_raw/runtime/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use crate::backend::c;
#[cfg(target_arch = "x86")]
use crate::backend::conv::by_mut;
use crate::backend::conv::{
by_ref, c_int, c_uint, ret, ret_c_int, ret_c_int_infallible, ret_error, size_of, zero,
by_ref, c_int, c_uint, ret, ret_c_int, ret_c_int_infallible, ret_error, ret_void_star, size_of,
zero,
};
#[cfg(feature = "fs")]
use crate::fd::BorrowedFd;
Expand All @@ -22,6 +23,7 @@ use crate::runtime::{How, Sigaction, Siginfo, Sigset, Stack};
use crate::signal::Signal;
use crate::timespec::Timespec;
use crate::utils::option_as_ptr;
use core::ffi::c_void;
use core::mem::MaybeUninit;
#[cfg(target_pointer_width = "32")]
use linux_raw_sys::general::__kernel_old_timespec;
Expand Down Expand Up @@ -263,3 +265,9 @@ unsafe fn sigtimedwait_old(
pub(crate) fn exit_group(code: c::c_int) -> ! {
unsafe { syscall_noreturn!(__NR_exit_group, c_int(code)) }
}

#[inline]
pub(crate) unsafe fn brk(addr: *mut c::c_void) -> io::Result<*mut c_void> {
// Don't mark this `readonly`, so that loads don't get reordered past it.
ret_void_star(syscall!(__NR_brk, addr))
}
11 changes: 11 additions & 0 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,14 @@ pub unsafe fn sigtimedwait(set: &Sigset, timeout: Option<Timespec>) -> io::Resul
pub fn linux_secure() -> bool {
backend::param::auxv::linux_secure()
}

/// `brk(addr)`—Change the location of the “program break”.
///
/// # Safety
///
/// Be a good sport and don't break the allocator.
#[cfg(linux_raw)]
#[inline]
pub unsafe fn brk(addr: *mut c_void) -> io::Result<*mut c_void> {
backend::runtime::syscalls::brk(addr)
}

0 comments on commit b91ba31

Please sign in to comment.