Skip to content

Commit 292f15c

Browse files
committed
Upgrading dlmalloc to 0.2.1
1 parent 596b0d5 commit 292f15c

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -952,9 +952,9 @@ dependencies = [
952952

953953
[[package]]
954954
name = "dlmalloc"
955-
version = "0.1.4"
955+
version = "0.2.1"
956956
source = "registry+https://github.com/rust-lang/crates.io-index"
957-
checksum = "35055b1021724f4eb5262eb49130eebff23fc59fc5a14160e05faad8eeb36673"
957+
checksum = "332570860c2edf2d57914987bf9e24835425f75825086b6ba7d1e6a3e4f1f254"
958958
dependencies = [
959959
"compiler_builtins",
960960
"libc",

library/std/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ features = ['read_core', 'elf', 'macho', 'pe']
3636
rand = "0.7"
3737

3838
[target.'cfg(any(all(target_arch = "wasm32", not(target_os = "emscripten")), all(target_vendor = "fortanix", target_env = "sgx")))'.dependencies]
39-
dlmalloc = { version = "0.1", features = ['rustc-dep-of-std'] }
39+
dlmalloc = { version = "0.2.1", features = ['rustc-dep-of-std'] }
4040

4141
[target.x86_64-fortanix-unknown-sgx.dependencies]
4242
fortanix-sgx-abi = { version = "0.3.2", features = ['rustc-dep-of-std'] }

library/std/src/sys/sgx/abi/mem.rs

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ pub(crate) unsafe fn rel_ptr_mut<T>(offset: u64) -> *mut T {
1212

1313
extern "C" {
1414
static ENCLAVE_SIZE: usize;
15+
static HEAP_BASE: u64;
16+
static HEAP_SIZE: usize;
17+
}
18+
19+
/// Returns the base memory address of the heap
20+
pub(crate) fn heap_base() -> *const u8 {
21+
unsafe { rel_ptr_mut(HEAP_BASE) }
22+
}
23+
24+
/// Returns the size of the heap
25+
pub(crate) fn heap_size() -> usize {
26+
unsafe { HEAP_SIZE }
1527
}
1628

1729
// Do not remove inline: will result in relocation failure

library/std/src/sys/sgx/alloc.rs

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
use crate::alloc::{GlobalAlloc, Layout, System};
2+
use crate::ptr;
3+
use crate::sys::sgx::abi::mem as sgx_mem;
4+
use core::sync::atomic::{AtomicBool, Ordering};
25

36
use super::waitqueue::SpinMutex;
47

@@ -10,7 +13,48 @@ use super::waitqueue::SpinMutex;
1013
// dlmalloc.c from C to Rust.
1114
#[cfg_attr(test, linkage = "available_externally")]
1215
#[export_name = "_ZN16__rust_internals3std3sys3sgx5alloc8DLMALLOCE"]
13-
static DLMALLOC: SpinMutex<dlmalloc::Dlmalloc> = SpinMutex::new(dlmalloc::DLMALLOC_INIT);
16+
static DLMALLOC: SpinMutex<dlmalloc::Dlmalloc<Sgx>> =
17+
SpinMutex::new(dlmalloc::Dlmalloc::new_with_allocator(Sgx {}));
18+
19+
struct Sgx;
20+
21+
unsafe impl dlmalloc::Allocator for Sgx {
22+
/// Allocs system resources
23+
fn alloc(&self, _size: usize) -> (*mut u8, usize, u32) {
24+
static INIT: AtomicBool = AtomicBool::new(false);
25+
26+
// No ordering requirement since this function is protected by the global lock.
27+
if !INIT.swap(true, Ordering::Relaxed) {
28+
(sgx_mem::heap_base() as _, sgx_mem::heap_size(), 0)
29+
} else {
30+
(ptr::null_mut(), 0, 0)
31+
}
32+
}
33+
34+
fn remap(&self, _ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool) -> *mut u8 {
35+
ptr::null_mut()
36+
}
37+
38+
fn free_part(&self, _ptr: *mut u8, _oldsize: usize, _newsize: usize) -> bool {
39+
false
40+
}
41+
42+
fn free(&self, _ptr: *mut u8, _size: usize) -> bool {
43+
return false;
44+
}
45+
46+
fn can_release_part(&self, _flags: u32) -> bool {
47+
false
48+
}
49+
50+
fn allocates_zeros(&self) -> bool {
51+
false
52+
}
53+
54+
fn page_size(&self) -> usize {
55+
0x1000
56+
}
57+
}
1458

1559
#[stable(feature = "alloc_system_type", since = "1.28.0")]
1660
unsafe impl GlobalAlloc for System {

library/std/src/sys/wasm/alloc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
1919
use crate::alloc::{GlobalAlloc, Layout, System};
2020

21-
static mut DLMALLOC: dlmalloc::Dlmalloc = dlmalloc::DLMALLOC_INIT;
21+
static mut DLMALLOC: dlmalloc::Dlmalloc = dlmalloc::Dlmalloc::new();
2222

2323
#[stable(feature = "alloc_system_type", since = "1.28.0")]
2424
unsafe impl GlobalAlloc for System {

0 commit comments

Comments
 (0)