From a80d62d9c75b5dfc3650bc818b0f51591a9159da Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Sun, 22 Oct 2023 11:57:19 -0700 Subject: [PATCH] Implement `runtime::brk`. `brk` is used by some `malloc` implementations. --- src/backend/linux_raw/runtime/syscalls.rs | 6 ++++++ src/runtime.rs | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/backend/linux_raw/runtime/syscalls.rs b/src/backend/linux_raw/runtime/syscalls.rs index c1ff3d239..53b0cea14 100644 --- a/src/backend/linux_raw/runtime/syscalls.rs +++ b/src/backend/linux_raw/runtime/syscalls.rs @@ -263,3 +263,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<()> { + // Don't mark this `readonly`, so that loads don't get reordered past it. + ret(syscall!(__NR_brk, addr)) +} diff --git a/src/runtime.rs b/src/runtime.rs index 6055b1eea..6dabc01f7 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -510,3 +510,14 @@ pub unsafe fn sigtimedwait(set: &Sigset, timeout: Option) -> 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<()> { + backend::runtime::syscalls::brk(addr) +}