Skip to content

Commit

Permalink
Merge branch 'main' into harness
Browse files Browse the repository at this point in the history
  • Loading branch information
wenyuzhao committed May 24, 2024
2 parents f765a84 + 576f804 commit a903579
Show file tree
Hide file tree
Showing 38 changed files with 770 additions and 392 deletions.
6 changes: 5 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
[build]
rustflags = ["-Z", "tls_model=initial-exec"]
rustflags = ["-Z", "tls_model=initial-exec"]

[alias]
# Helper command to run a program with a malloc implementation
x = "run -p mallockit-dev-tools --quiet --"
65 changes: 8 additions & 57 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"
members = [
"mallockit",
"mallockit/macros",
"mallockit/dev",
"bump",
"buddy",
"hoard",
Expand Down Expand Up @@ -30,7 +31,6 @@ errno = "0.2.8"
spin = { version = "0.9.3", features = ["std"] }
ctor = "0.1"
num_cpus = "1.13.1"
crossbeam = "0.8.1"
atomic = "0.5.1"
quote = "1.0.20"
syn = "1.0.98"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
## Getting Started

```console
$ cargo build --release
$ env LD_PRELOAD=./target/release/libbump.so cargo # or some other command
$ cargo build -p hoard --release --features malloc
$ env LD_PRELOAD=./target/release/libhoard.so cargo --help # or some other command
```
#### Run on macOS

```console
$ env DYLD_INSERT_LIBRARIES=./target/release/libbump.dylib cargo # or some other command
$ env DYLD_INSERT_LIBRARIES=./target/release/libhoard.dylib cargo --help # or some other command
```

*Note: If you'd like to hijack the system apps and libraries as well, disable System Integrity Protection (SIP). Do this at your own risk 😉*
Expand Down
2 changes: 1 addition & 1 deletion buddy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "buddy"
version = { workspace = true }
authors = ["Wenyu Zhao <[email protected]>"]
authors = ["Wenyu Zhao <[email protected]>"]
edition = { workspace = true }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
6 changes: 2 additions & 4 deletions buddy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ impl Mutator for BuddyMutator {

fn new() -> Self {
Self {
freelist: FreeListAllocator::new::<FREELIST_SPACE>(Lazy::new(|| {
&Self::plan().freelist_space
})),
los: LargeObjectAllocator::new(Lazy::new(|| &Self::plan().large_object_space)),
freelist: FreeListAllocator::new::<FREELIST_SPACE>(&Self::plan().freelist_space),
los: LargeObjectAllocator::new(&Self::plan().large_object_space),
}
}

Expand Down
2 changes: 1 addition & 1 deletion bump/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "bump"
version = { workspace = true }
authors = ["Wenyu Zhao <[email protected]>"]
authors = ["Wenyu Zhao <[email protected]>"]
edition = { workspace = true }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
2 changes: 1 addition & 1 deletion bump/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Mutator for BumpMutator {

fn new() -> Self {
Self {
bump: BumpAllocator::new(Lazy::new(|| &Self::plan().immortal)),
bump: BumpAllocator::new(&Self::plan().immortal),
}
}

Expand Down
3 changes: 3 additions & 0 deletions examples/malloc-override/example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cargo build -p hoard --release --features malloc

env LD_PRELOAD=./target/release/libhoard.so cargo --help
2 changes: 1 addition & 1 deletion hoard/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "hoard"
version = { workspace = true }
authors = ["Wenyu Zhao <[email protected]>"]
authors = ["Wenyu Zhao <[email protected]>"]
edition = { workspace = true }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
31 changes: 15 additions & 16 deletions hoard/src/hoard_space.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
use super::{page_resource::BlockPageResource, Allocator, Space, SpaceId};
use crate::{pool::Pool, super_block::SuperBlock};
use mallockit::{
space::meta::{Box, Meta},
space::{
meta::{Box, Meta},
page_resource::MemRegion,
},
util::{mem::alloc::discrete_tlab::DiscreteTLAB, *},
};

/// Global heap
pub struct HoardSpace {
id: SpaceId,
pr: BlockPageResource,
pr: BlockPageResource<SuperBlock>,
pub(crate) pool: Pool,
}

impl Space for HoardSpace {
const MAX_ALLOCATION_SIZE: usize = SuperBlock::BYTES / 4;
type PR = BlockPageResource;
type PR = BlockPageResource<SuperBlock>;

fn new(id: SpaceId) -> Self {
Self {
id,
pr: BlockPageResource::new(id, SuperBlock::LOG_BYTES),
pr: BlockPageResource::new(id),
pool: Pool::new(true),
}
}
Expand Down Expand Up @@ -59,11 +62,7 @@ impl HoardSpace {
return Some(block);
}
// Acquire new memory
let addr = self
.acquire::<Size4K>(1 << (SuperBlock::LOG_BYTES - Size4K::LOG_BYTES))?
.start
.start();
let block = SuperBlock::new(addr);
let block = self.pr.acquire_block()?;
block.init(local.static_ref(), size_class);
debug_assert!(!block.is_full());
debug_assert!(block.is_empty());
Expand All @@ -78,25 +77,25 @@ impl HoardSpace {
}

pub fn release_block(&self, block: SuperBlock) {
self.release::<Size4K>(Page::new(block.start()));
self.pr.release_block(block)
}
}
/// Thread-local heap
pub struct HoardAllocator {
space: Lazy<&'static HoardSpace, Local>,
space: &'static HoardSpace,
tlab: DiscreteTLAB<{ SizeClass::<4>::from_bytes(Self::LARGEST_SMALL_OBJECT).as_usize() + 1 }>,
local: Lazy<Box<Pool>, Local>,
local: Box<Pool>,
}

impl HoardAllocator {
const LOCAL_HEAP_THRESHOLD: usize = 16 * 1024 * 1024;
const LARGEST_SMALL_OBJECT: usize = 1024;

pub const fn new(space: Lazy<&'static HoardSpace, Local>, _space_id: SpaceId) -> Self {
pub fn new(space: &'static HoardSpace, _space_id: SpaceId) -> Self {
Self {
space,
tlab: DiscreteTLAB::new(),
local: Lazy::new(|| Box::new_in(Pool::new(false), Meta)),
local: Box::new_in(Pool::new(false), Meta),
}
}
}
Expand All @@ -109,7 +108,7 @@ impl Allocator for HoardAllocator {
return Some(cell);
}
}
self.local.alloc_cell(size_class, &self.space)
self.local.alloc_cell(size_class, self.space)
}

fn dealloc(&mut self, cell: Address) {
Expand All @@ -120,7 +119,7 @@ impl Allocator for HoardAllocator {
{
self.tlab.push(block.size_class, cell);
} else {
self.local.free_cell(cell, &self.space);
self.local.free_cell(cell, self.space);
}
}
}
4 changes: 2 additions & 2 deletions hoard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ impl Mutator for HoardMutator {

fn new() -> Self {
Self {
hoard: HoardAllocator::new(Lazy::new(|| &Self::plan().hoard_space), HOARD_SPACE),
los: LargeObjectAllocator::new(Lazy::new(|| &Self::plan().large_object_space)),
hoard: HoardAllocator::new(&Self::plan().hoard_space, HOARD_SPACE),
los: LargeObjectAllocator::new(&Self::plan().large_object_space),
}
}

Expand Down
17 changes: 10 additions & 7 deletions hoard/src/pool.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{hoard_space::HoardSpace, super_block::SuperBlock};
use array_const_fn_init::array_const_fn_init;
use mallockit::util::{mem::size_class::SizeClass, Address, Lazy, Local};
use mallockit::{
space::page_resource::MemRegion,
util::{mem::size_class::SizeClass, Address},
};
use spin::{relax::Yield, MutexGuard};
use std::sync::atomic::{AtomicUsize, Ordering};

Expand Down Expand Up @@ -36,7 +39,7 @@ impl BlockList {
fn group(block: SuperBlock, alloc: bool) -> usize {
let u = block.used_bytes()
+ if alloc { block.size_class.bytes() } else { 0 }
+ (Address::ZERO + SuperBlock::HEADER_BYTES)
+ (Address::ZERO + SuperBlock::META_BYTES)
.align_up(block.size_class.bytes())
.as_usize();
(u << 2) >> SuperBlock::LOG_BYTES
Expand Down Expand Up @@ -252,7 +255,7 @@ impl Pool {
&self,
size_class: SizeClass,
blocks: &mut BlockList,
space: &Lazy<&'static HoardSpace, Local>,
space: &'static HoardSpace,
) -> SuperBlock {
// Get a block from global pool
let block = space
Expand All @@ -273,7 +276,7 @@ impl Pool {
pub fn alloc_cell(
&mut self,
size_class: SizeClass,
space: &Lazy<&'static HoardSpace, Local>,
space: &'static HoardSpace,
) -> Option<Address> {
debug_assert!(!self.global);
let mut blocks = unsafe { self.blocks.get_unchecked(size_class.as_usize()).lock() };
Expand All @@ -289,7 +292,7 @@ impl Pool {
}

#[cold]
pub fn free_cell(&self, cell: Address, space: &Lazy<&'static HoardSpace, Local>) {
pub fn free_cell(&self, cell: Address, space: &'static HoardSpace) {
let block = SuperBlock::containing(cell);
let mut owner = block.owner;
let mut blocks = owner.lock_blocks(block.size_class);
Expand All @@ -305,7 +308,7 @@ impl Pool {
fn free_cell_slow_impl(
&self,
cell: Address,
space: &Lazy<&'static HoardSpace, Local>,
space: &'static HoardSpace,
blocks: &mut BlockList,
) {
let block = SuperBlock::containing(cell);
Expand All @@ -328,7 +331,7 @@ impl Pool {
fn flush_block_slow(
&self,
size_class: SizeClass,
space: &Lazy<&'static HoardSpace, Local>,
space: &'static HoardSpace,
blocks: &mut BlockList,
) {
// Transit a mostly-empty block to the global pool
Expand Down
Loading

0 comments on commit a903579

Please sign in to comment.