Skip to content

Commit b91ba31

Browse files
authored
Implement runtime::brk. (#891)
`brk` is used by some `malloc` implementations.
1 parent 414309a commit b91ba31

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/backend/linux_raw/runtime/syscalls.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use crate::backend::c;
99
#[cfg(target_arch = "x86")]
1010
use crate::backend::conv::by_mut;
1111
use crate::backend::conv::{
12-
by_ref, c_int, c_uint, ret, ret_c_int, ret_c_int_infallible, ret_error, size_of, zero,
12+
by_ref, c_int, c_uint, ret, ret_c_int, ret_c_int_infallible, ret_error, ret_void_star, size_of,
13+
zero,
1314
};
1415
#[cfg(feature = "fs")]
1516
use crate::fd::BorrowedFd;
@@ -22,6 +23,7 @@ use crate::runtime::{How, Sigaction, Siginfo, Sigset, Stack};
2223
use crate::signal::Signal;
2324
use crate::timespec::Timespec;
2425
use crate::utils::option_as_ptr;
26+
use core::ffi::c_void;
2527
use core::mem::MaybeUninit;
2628
#[cfg(target_pointer_width = "32")]
2729
use linux_raw_sys::general::__kernel_old_timespec;
@@ -263,3 +265,9 @@ unsafe fn sigtimedwait_old(
263265
pub(crate) fn exit_group(code: c::c_int) -> ! {
264266
unsafe { syscall_noreturn!(__NR_exit_group, c_int(code)) }
265267
}
268+
269+
#[inline]
270+
pub(crate) unsafe fn brk(addr: *mut c::c_void) -> io::Result<*mut c_void> {
271+
// Don't mark this `readonly`, so that loads don't get reordered past it.
272+
ret_void_star(syscall!(__NR_brk, addr))
273+
}

src/runtime.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,3 +510,14 @@ pub unsafe fn sigtimedwait(set: &Sigset, timeout: Option<Timespec>) -> io::Resul
510510
pub fn linux_secure() -> bool {
511511
backend::param::auxv::linux_secure()
512512
}
513+
514+
/// `brk(addr)`—Change the location of the “program break”.
515+
///
516+
/// # Safety
517+
///
518+
/// Be a good sport and don't break the allocator.
519+
#[cfg(linux_raw)]
520+
#[inline]
521+
pub unsafe fn brk(addr: *mut c_void) -> io::Result<*mut c_void> {
522+
backend::runtime::syscalls::brk(addr)
523+
}

0 commit comments

Comments
 (0)