0.3.0: converge relibc to upstream 0.6.0 + Red Bear patches
This commit is contained in:
+117
@@ -0,0 +1,117 @@
|
||||
name: CI
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
name: Test
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- os: ubuntu-latest
|
||||
rust: stable
|
||||
- os: ubuntu-latest
|
||||
rust: beta
|
||||
- os: ubuntu-latest
|
||||
rust: nightly
|
||||
- os: macos-latest
|
||||
rust: stable
|
||||
- os: windows-latest
|
||||
rust: stable
|
||||
- os: ubuntu-latest
|
||||
rust: stable
|
||||
target: wasm32-wasip1
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- run: rustup update ${{ matrix.rust }} --no-self-update && rustup default ${{ matrix.rust }}
|
||||
shell: bash
|
||||
|
||||
# Configure cross-builds by adding the rustup target and configuring future
|
||||
# cargo invocations.
|
||||
- run: |
|
||||
rustup target add ${{ matrix.target }}
|
||||
echo CARGO_BUILD_TARGET=${{ matrix.target }} >> $GITHUB_ENV
|
||||
if: matrix.target != ''
|
||||
|
||||
# For wasm install wasmtime as a test runner and configure it with Cargo.
|
||||
- name: Setup `wasmtime`
|
||||
uses: bytecodealliance/actions/wasmtime/setup@v1
|
||||
if: matrix.target == 'wasm32-wasip1'
|
||||
- run: echo CARGO_TARGET_WASM32_WASIP1_RUNNER=wasmtime >> $GITHUB_ENV
|
||||
if: matrix.target == 'wasm32-wasip1'
|
||||
|
||||
- run: cargo test
|
||||
- run: cargo test --features debug
|
||||
- run: cargo test --features global
|
||||
- run: cargo test --release
|
||||
env:
|
||||
CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS: true
|
||||
- run: cargo test --release
|
||||
env:
|
||||
CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS: false
|
||||
- run: cargo test --features debug --release
|
||||
env:
|
||||
CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS: true
|
||||
- run: RUSTFLAGS='--cfg test_lots' cargo test --release
|
||||
shell: bash
|
||||
env:
|
||||
CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS: true
|
||||
- run: RUSTFLAGS='--cfg test_lots' cargo test --release --features debug
|
||||
shell: bash
|
||||
env:
|
||||
CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS: true
|
||||
|
||||
rustfmt:
|
||||
name: Rustfmt
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install Rust
|
||||
run: rustup update stable && rustup default stable && rustup component add rustfmt
|
||||
- run: cargo fmt -- --check
|
||||
|
||||
wasm:
|
||||
name: WebAssembly
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install Rust
|
||||
run: rustup update stable && rustup default stable && rustup target add wasm32-unknown-unknown
|
||||
- run: cargo build --target wasm32-unknown-unknown
|
||||
- run: cargo build --target wasm32-unknown-unknown --release
|
||||
|
||||
external-platform:
|
||||
name: external-platform
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install Rust
|
||||
run: rustup update stable && rustup default stable && rustup target add x86_64-fortanix-unknown-sgx
|
||||
- run: cargo build --target x86_64-fortanix-unknown-sgx
|
||||
|
||||
fuzz:
|
||||
name: Build Fuzzers
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install Rust
|
||||
run: rustup update nightly && rustup default nightly
|
||||
- run: cargo install cargo-fuzz
|
||||
- run: cargo fuzz build --dev
|
||||
|
||||
miri:
|
||||
name: Miri
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install Miri
|
||||
run: |
|
||||
rustup toolchain install nightly --component miri
|
||||
rustup override set nightly
|
||||
cargo miri setup
|
||||
- name: Test with Miri Stack Borrows
|
||||
run: cargo miri test
|
||||
- name: Test with Miri Tree Borrows
|
||||
run: cargo miri test
|
||||
env:
|
||||
MIRIFLAGS: -Zmiri-tree-borrows
|
||||
@@ -0,0 +1,3 @@
|
||||
/target/
|
||||
**/*.rs.bk
|
||||
Cargo.lock
|
||||
@@ -0,0 +1,70 @@
|
||||
[package]
|
||||
name = "dlmalloc"
|
||||
version = "0.2.8"
|
||||
authors = ["Alex Crichton <alex@alexcrichton.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/alexcrichton/dlmalloc-rs"
|
||||
homepage = "https://github.com/alexcrichton/dlmalloc-rs"
|
||||
documentation = "https://docs.rs/dlmalloc"
|
||||
description = """
|
||||
A Rust port of the dlmalloc allocator
|
||||
"""
|
||||
edition.workspace = true
|
||||
|
||||
[workspace]
|
||||
members = ['fuzz']
|
||||
|
||||
[workspace.package]
|
||||
edition = '2021'
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
features = ['global']
|
||||
|
||||
[lib]
|
||||
doctest = false
|
||||
|
||||
[target.'cfg(all(unix, not(target_arch = "wasm32")))'.dependencies]
|
||||
libc = { version = "0.2", default-features = false, optional = true }
|
||||
|
||||
[dependencies]
|
||||
# For more information on these dependencies see rust-lang/rust's
|
||||
# `src/tools/rustc-std-workspace` folder
|
||||
core = { version = '1.0.0', optional = true, package = 'rustc-std-workspace-core' }
|
||||
compiler_builtins = { version = '0.1.0', optional = true }
|
||||
cfg-if = "1.0"
|
||||
|
||||
[target.'cfg(target_os = "windows")'.dependencies.windows-sys]
|
||||
version = ">=0.52.0, <=0.59.*"
|
||||
features = [
|
||||
"Win32_Foundation",
|
||||
"Win32_System_Memory",
|
||||
"Win32_System_Threading",
|
||||
"Win32_System_SystemInformation",
|
||||
]
|
||||
|
||||
[dev-dependencies]
|
||||
arbitrary = "1.3.2"
|
||||
rand = { version = "0.8", features = ['small_rng'] }
|
||||
|
||||
[profile.release]
|
||||
debug-assertions = true
|
||||
|
||||
[features]
|
||||
# Enable implementations of the `GlobalAlloc` standard library API, exporting a
|
||||
# new `GlobalDlmalloc` as well which implements this trait.
|
||||
global = ["system", "rust_api"]
|
||||
|
||||
# Enable very expensive debug checks in this crate
|
||||
debug = []
|
||||
|
||||
# Enables OS APIs based on the current target, can be implemented manually
|
||||
# otherwise.
|
||||
system = ["libc"]
|
||||
|
||||
rustc-dep-of-std = ['core', 'compiler_builtins/rustc-dep-of-std']
|
||||
|
||||
c_api = []
|
||||
rust_api = []
|
||||
|
||||
default = ["global", "rust_api"]
|
||||
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -0,0 +1,25 @@
|
||||
Copyright (c) 2014 Alex Crichton
|
||||
|
||||
Permission is hereby granted, free of charge, to any
|
||||
person obtaining a copy of this software and associated
|
||||
documentation files (the "Software"), to deal in the
|
||||
Software without restriction, including without
|
||||
limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software
|
||||
is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice
|
||||
shall be included in all copies or substantial portions
|
||||
of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
@@ -0,0 +1,40 @@
|
||||
# dlmalloc-rs
|
||||
|
||||
A port of [dlmalloc] to Rust.
|
||||
|
||||
[Documentation](https://docs.rs/dlmalloc)
|
||||
|
||||
[dlmalloc]: https://gee.cs.oswego.edu/dl/html/malloc.html
|
||||
|
||||
## Why dlmalloc?
|
||||
|
||||
This crate is a port of [dlmalloc] to Rust, and doesn't rely on C. The primary
|
||||
purpose of this crate is to serve as the default allocator for Rust on the
|
||||
`wasm32-unknown-unknown` target. At the time this was written the wasm target
|
||||
didn't support C code, so it was required to have a Rust-only solution.
|
||||
|
||||
This allocator is not the most performant by a longshot. It is primarily, I
|
||||
think, intended for being easy to port and easy to learn. I didn't dive too deep
|
||||
into the implementation when writing it, it's just a straight port of the C
|
||||
version.
|
||||
|
||||
It's unlikely that Rust code needs to worry/interact with this allocator in
|
||||
general. Most of the time you'll be manually switching to a different allocator
|
||||
:)
|
||||
|
||||
# License
|
||||
|
||||
This project is licensed under either of
|
||||
|
||||
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
||||
http://www.apache.org/licenses/LICENSE-2.0)
|
||||
* MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
||||
http://opensource.org/licenses/MIT)
|
||||
|
||||
at your option.
|
||||
|
||||
### Contribution
|
||||
|
||||
Unless you explicitly state otherwise, any contribution intentionally submitted
|
||||
for inclusion in this project by you, as defined in the Apache-2.0 license,
|
||||
shall be dual licensed as above, without any additional terms or conditions.
|
||||
@@ -0,0 +1,2 @@
|
||||
corpus
|
||||
artifacts
|
||||
@@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "dlmalloc-fuzz"
|
||||
version = "0.0.1"
|
||||
publish = false
|
||||
edition.workspace = true
|
||||
|
||||
[package.metadata]
|
||||
cargo-fuzz = true
|
||||
|
||||
[dependencies]
|
||||
arbitrary = "1.3.2"
|
||||
dlmalloc = { path = '..' }
|
||||
libfuzzer-sys = "0.4.7"
|
||||
|
||||
[[bin]]
|
||||
name = "alloc"
|
||||
path = "fuzz_targets/alloc.rs"
|
||||
test = false
|
||||
bench = false
|
||||
@@ -0,0 +1,8 @@
|
||||
#![no_main]
|
||||
|
||||
use arbitrary::Unstructured;
|
||||
use libfuzzer_sys::fuzz_target;
|
||||
|
||||
fuzz_target!(|bytes: &[u8]| {
|
||||
let _ = dlmalloc_fuzz::run(&mut Unstructured::new(bytes));
|
||||
});
|
||||
@@ -0,0 +1,108 @@
|
||||
use arbitrary::{Result, Unstructured};
|
||||
use dlmalloc::Dlmalloc;
|
||||
use std::cmp;
|
||||
|
||||
const MAX_ALLOCATED: usize = 100 << 20; // 100 MB
|
||||
|
||||
pub fn run(u: &mut Unstructured<'_>) -> Result<()> {
|
||||
let mut a = Dlmalloc::new();
|
||||
let mut ptrs = Vec::new();
|
||||
let mut allocated = 0;
|
||||
unsafe {
|
||||
while u.arbitrary()? {
|
||||
// If there are pointers to free then have a chance of deallocating
|
||||
// a pointer. Try not to deallocate things until there's a "large"
|
||||
// working set but afterwards give it a 50/50 chance of allocating
|
||||
// or deallocating.
|
||||
let free = match ptrs.len() {
|
||||
0 => false,
|
||||
0..=10_000 => u.ratio(1, 3)?,
|
||||
_ => u.arbitrary()?,
|
||||
};
|
||||
if free {
|
||||
let idx = u.choose_index(ptrs.len())?;
|
||||
let (ptr, size, align) = ptrs.swap_remove(idx);
|
||||
allocated -= size;
|
||||
a.free(ptr, size, align);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 1/100 chance of reallocating a pointer to a different size.
|
||||
if ptrs.len() > 0 && u.ratio(1, 100)? {
|
||||
let idx = u.choose_index(ptrs.len())?;
|
||||
let (ptr, size, align) = ptrs.swap_remove(idx);
|
||||
|
||||
// Arbitrarily choose whether to make this allocation either
|
||||
// twice as large or half as small.
|
||||
let new_size = if u.arbitrary()? {
|
||||
u.int_in_range(size..=size * 2)?
|
||||
} else if size > 10 {
|
||||
u.int_in_range(size / 2..=size)?
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
if allocated + new_size - size > MAX_ALLOCATED {
|
||||
ptrs.push((ptr, size, align));
|
||||
continue;
|
||||
}
|
||||
allocated -= size;
|
||||
allocated += new_size;
|
||||
|
||||
// Perform the `realloc` and assert that all bytes were copied.
|
||||
let mut tmp = Vec::new();
|
||||
for i in 0..cmp::min(size, new_size) {
|
||||
tmp.push(*ptr.offset(i as isize));
|
||||
}
|
||||
let ptr = a.realloc(ptr, size, align, new_size);
|
||||
assert!(!ptr.is_null());
|
||||
for (i, byte) in tmp.iter().enumerate() {
|
||||
assert_eq!(*byte, *ptr.offset(i as isize));
|
||||
}
|
||||
ptrs.push((ptr, new_size, align));
|
||||
}
|
||||
|
||||
// Aribtrarily choose a size to allocate as well as an alignment.
|
||||
// Enable small sizes with standard alignment happening a fair bit.
|
||||
let size = if u.arbitrary()? {
|
||||
u.int_in_range(1..=128)?
|
||||
} else {
|
||||
u.int_in_range(1..=128 * 1024)?
|
||||
};
|
||||
let align = if u.ratio(1, 10)? {
|
||||
1 << u.int_in_range(3..=8)?
|
||||
} else {
|
||||
8
|
||||
};
|
||||
|
||||
if size + allocated > MAX_ALLOCATED {
|
||||
continue;
|
||||
}
|
||||
allocated += size;
|
||||
|
||||
// Choose arbitrarily between a zero-allocated chunk and a normal
|
||||
// allocated chunk.
|
||||
let zero = u.ratio(1, 50)?;
|
||||
let ptr = if zero {
|
||||
a.calloc(size, align)
|
||||
} else {
|
||||
a.malloc(size, align)
|
||||
};
|
||||
for i in 0..size {
|
||||
if zero {
|
||||
assert_eq!(*ptr.offset(i as isize), 0);
|
||||
}
|
||||
*ptr.offset(i as isize) = 0xce;
|
||||
}
|
||||
ptrs.push((ptr, size, align));
|
||||
}
|
||||
|
||||
// Deallocate everythign when we're done.
|
||||
for (ptr, size, align) in ptrs {
|
||||
a.free(ptr, size, align);
|
||||
}
|
||||
|
||||
a.destroy();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,42 @@
|
||||
use crate::Allocator;
|
||||
use core::ptr;
|
||||
|
||||
pub struct System {
|
||||
_priv: (),
|
||||
}
|
||||
|
||||
impl System {
|
||||
pub const fn new() -> System {
|
||||
System { _priv: () }
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Allocator for System {
|
||||
fn alloc(&self, _size: usize) -> (*mut u8, usize, u32) {
|
||||
(ptr::null_mut(), 0, 0)
|
||||
}
|
||||
|
||||
fn remap(&self, _ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool) -> *mut u8 {
|
||||
ptr::null_mut()
|
||||
}
|
||||
|
||||
fn free_part(&self, _ptr: *mut u8, _oldsize: usize, _newsize: usize) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn free(&self, _ptr: *mut u8, _size: usize) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn can_release_part(&self, _flags: u32) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn allocates_zeros(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn page_size(&self) -> usize {
|
||||
1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
use crate::Dlmalloc;
|
||||
use core::alloc::{GlobalAlloc, Layout};
|
||||
use core::ptr;
|
||||
|
||||
pub use crate::sys::enable_alloc_after_fork;
|
||||
|
||||
/// An instance of a "global allocator" backed by `Dlmalloc`
|
||||
///
|
||||
/// This API requires the `global` feature is activated, and this type
|
||||
/// implements the `GlobalAlloc` trait in the standard library.
|
||||
pub struct GlobalDlmalloc;
|
||||
|
||||
static mut DLMALLOC: Dlmalloc = Dlmalloc::new();
|
||||
|
||||
unsafe impl GlobalAlloc for GlobalDlmalloc {
|
||||
#[inline]
|
||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||
let _guard = lock();
|
||||
let dlmalloc = ptr::addr_of_mut!(DLMALLOC);
|
||||
(*dlmalloc).malloc(layout.size(), layout.align())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
||||
let _guard = lock();
|
||||
let dlmalloc = ptr::addr_of_mut!(DLMALLOC);
|
||||
(*dlmalloc).free(ptr, layout.size(), layout.align())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
|
||||
let _guard = lock();
|
||||
let dlmalloc = ptr::addr_of_mut!(DLMALLOC);
|
||||
(*dlmalloc).calloc(layout.size(), layout.align())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
|
||||
let _guard = lock();
|
||||
let dlmalloc = ptr::addr_of_mut!(DLMALLOC);
|
||||
(*dlmalloc).realloc(ptr, layout.size(), layout.align(), new_size)
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn lock() -> impl Drop {
|
||||
crate::sys::acquire_global_lock();
|
||||
|
||||
struct Guard;
|
||||
impl Drop for Guard {
|
||||
fn drop(&mut self) {
|
||||
crate::sys::release_global_lock()
|
||||
}
|
||||
}
|
||||
|
||||
Guard
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
//! A Rust port of the `dlmalloc` allocator.
|
||||
//!
|
||||
//! The `dlmalloc` allocator is described at
|
||||
//! <https://gee.cs.oswego.edu/dl/html/malloc.html> and this Rust crate is a straight
|
||||
//! port of the C code for the allocator into Rust. The implementation is
|
||||
//! wrapped up in a `Dlmalloc` type and has support for Linux, OSX, and Wasm
|
||||
//! currently.
|
||||
//!
|
||||
//! The primary purpose of this crate is that it serves as the default memory
|
||||
//! allocator for the `wasm32-unknown-unknown` target in the standard library.
|
||||
//! Support for other platforms is largely untested and unused, but is used when
|
||||
//! testing this crate.
|
||||
|
||||
#![allow(dead_code)]
|
||||
#![no_std]
|
||||
#![deny(missing_docs)]
|
||||
|
||||
#[cfg(feature = "rust_api")]
|
||||
use core::{cmp, ptr};
|
||||
|
||||
#[cfg(feature = "system")]
|
||||
use sys::System;
|
||||
|
||||
#[cfg(feature = "global")]
|
||||
pub use self::global::{enable_alloc_after_fork, GlobalDlmalloc};
|
||||
|
||||
mod dlmalloc;
|
||||
|
||||
#[cfg(feature = "c_api")]
|
||||
pub use dlmalloc::Dlmalloc as DlmallocCApi;
|
||||
|
||||
#[cfg(feature = "global")]
|
||||
mod global;
|
||||
|
||||
/// In order for this crate to efficiently manage memory, it needs a way to communicate with the
|
||||
/// underlying platform. This `Allocator` trait provides an interface for this communication.
|
||||
pub unsafe trait Allocator: Send {
|
||||
/// Allocates system memory region of at least `size` bytes
|
||||
/// Returns a triple of `(base, size, flags)` where `base` is a pointer to the beginning of the
|
||||
/// allocated memory region. `size` is the actual size of the region while `flags` specifies
|
||||
/// properties of the allocated region. If `EXTERN_BIT` (bit 0) set in flags, then we did not
|
||||
/// allocate this segment and so should not try to deallocate or merge with others.
|
||||
/// This function can return a `std::ptr::null_mut()` when allocation fails (other values of
|
||||
/// the triple will be ignored).
|
||||
fn alloc(&self, size: usize) -> (*mut u8, usize, u32);
|
||||
|
||||
/// Remaps system memory region at `ptr` with size `oldsize` to a potential new location with
|
||||
/// size `newsize`. `can_move` indicates if the location is allowed to move to a completely new
|
||||
/// location, or that it is only allowed to change in size. Returns a pointer to the new
|
||||
/// location in memory.
|
||||
/// This function can return a `std::ptr::null_mut()` to signal an error.
|
||||
fn remap(&self, ptr: *mut u8, oldsize: usize, newsize: usize, can_move: bool) -> *mut u8;
|
||||
|
||||
/// Frees a part of a memory chunk. The original memory chunk starts at `ptr` with size `oldsize`
|
||||
/// and is turned into a memory region starting at the same address but with `newsize` bytes.
|
||||
/// Returns `true` iff the access memory region could be freed.
|
||||
fn free_part(&self, ptr: *mut u8, oldsize: usize, newsize: usize) -> bool;
|
||||
|
||||
/// Frees an entire memory region. Returns `true` iff the operation succeeded. When `false` is
|
||||
/// returned, the `dlmalloc` may re-use the location on future allocation requests
|
||||
fn free(&self, ptr: *mut u8, size: usize) -> bool;
|
||||
|
||||
/// Indicates if the system can release a part of memory. For the `flags` argument, see
|
||||
/// `Allocator::alloc`
|
||||
fn can_release_part(&self, flags: u32) -> bool;
|
||||
|
||||
/// Indicates whether newly allocated regions contain zeros.
|
||||
fn allocates_zeros(&self) -> bool;
|
||||
|
||||
/// Returns the page size. Must be a power of two
|
||||
fn page_size(&self) -> usize;
|
||||
}
|
||||
|
||||
/// An allocator instance
|
||||
///
|
||||
/// Instances of this type are used to allocate blocks of memory. For best
|
||||
/// results only use one of these. Currently doesn't implement `Drop` to release
|
||||
/// lingering memory back to the OS. That may happen eventually though!
|
||||
#[cfg(feature = "rust_api")]
|
||||
pub struct Dlmalloc<
|
||||
#[cfg(feature = "system")]
|
||||
A = System,
|
||||
#[cfg(not(feature = "system"))]
|
||||
A,
|
||||
>(dlmalloc::Dlmalloc<A>);
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(all(feature = "system", target_family = "wasm"))] {
|
||||
#[path = "wasm.rs"]
|
||||
mod sys;
|
||||
} else if #[cfg(all(feature = "system", target_os = "windows"))] {
|
||||
#[path = "windows.rs"]
|
||||
mod sys;
|
||||
} else if #[cfg(all(feature = "system", target_os = "xous"))] {
|
||||
#[path = "xous.rs"]
|
||||
mod sys;
|
||||
} else if #[cfg(all(feature = "system", any(target_os = "linux", target_os = "macos", target_os = "redox")))] {
|
||||
#[path = "unix.rs"]
|
||||
mod sys;
|
||||
} else {
|
||||
#[path = "dummy.rs"]
|
||||
mod sys;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "system")]
|
||||
#[cfg(feature = "rust_api")]
|
||||
impl Dlmalloc<System> {
|
||||
/// Creates a new instance of an allocator
|
||||
pub const fn new() -> Dlmalloc<System> {
|
||||
Dlmalloc(dlmalloc::Dlmalloc::new(System::new()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "rust_api")]
|
||||
impl<A> Dlmalloc<A> {
|
||||
/// Creates a new instance of an allocator
|
||||
pub const fn new_with_allocator(sys_allocator: A) -> Dlmalloc<A> {
|
||||
Dlmalloc(dlmalloc::Dlmalloc::new(sys_allocator))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "rust_api")]
|
||||
impl<A: Allocator> Dlmalloc<A> {
|
||||
/// Allocates `size` bytes with `align` align.
|
||||
///
|
||||
/// Returns a null pointer if allocation fails. Returns a valid pointer
|
||||
/// otherwise.
|
||||
///
|
||||
/// Safety and contracts are largely governed by the `GlobalAlloc::alloc`
|
||||
/// method contracts.
|
||||
#[inline]
|
||||
pub unsafe fn malloc(&mut self, size: usize, align: usize) -> *mut u8 {
|
||||
if align <= self.0.malloc_alignment() {
|
||||
self.0.malloc(size)
|
||||
} else {
|
||||
self.0.memalign(align, size)
|
||||
}
|
||||
}
|
||||
|
||||
/// Same as `malloc`, except if the allocation succeeds it's guaranteed to
|
||||
/// point to `size` bytes of zeros.
|
||||
#[inline]
|
||||
pub unsafe fn calloc(&mut self, size: usize, align: usize) -> *mut u8 {
|
||||
let ptr = self.malloc(size, align);
|
||||
if !ptr.is_null() && self.0.calloc_must_clear(ptr) {
|
||||
ptr::write_bytes(ptr, 0, size);
|
||||
}
|
||||
ptr
|
||||
}
|
||||
|
||||
/// Deallocates a `ptr` with `size` and `align` as the previous request used
|
||||
/// to allocate it.
|
||||
///
|
||||
/// Safety and contracts are largely governed by the `GlobalAlloc::dealloc`
|
||||
/// method contracts.
|
||||
#[inline]
|
||||
pub unsafe fn free(&mut self, ptr: *mut u8, size: usize, align: usize) {
|
||||
let _ = align;
|
||||
self.0.validate_size(ptr, size);
|
||||
self.0.free(ptr)
|
||||
}
|
||||
|
||||
/// Reallocates `ptr`, a previous allocation with `old_size` and
|
||||
/// `old_align`, to have `new_size` and the same alignment as before.
|
||||
///
|
||||
/// Returns a null pointer if the memory couldn't be reallocated, but `ptr`
|
||||
/// is still valid. Returns a valid pointer and frees `ptr` if the request
|
||||
/// is satisfied.
|
||||
///
|
||||
/// Safety and contracts are largely governed by the `GlobalAlloc::realloc`
|
||||
/// method contracts.
|
||||
#[inline]
|
||||
pub unsafe fn realloc(
|
||||
&mut self,
|
||||
ptr: *mut u8,
|
||||
old_size: usize,
|
||||
old_align: usize,
|
||||
new_size: usize,
|
||||
) -> *mut u8 {
|
||||
self.0.validate_size(ptr, old_size);
|
||||
|
||||
if old_align <= self.0.malloc_alignment() {
|
||||
self.0.realloc(ptr, new_size)
|
||||
} else {
|
||||
let res = self.malloc(new_size, old_align);
|
||||
if !res.is_null() {
|
||||
let size = cmp::min(old_size, new_size);
|
||||
ptr::copy_nonoverlapping(ptr, res, size);
|
||||
self.free(ptr, old_size, old_align);
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
/// If possible, gives memory back to the system if there is unused memory
|
||||
/// at the high end of the malloc pool or in unused segments.
|
||||
///
|
||||
/// You can call this after freeing large blocks of memory to potentially
|
||||
/// reduce the system-level memory requirements of a program. However, it
|
||||
/// cannot guarantee to reduce memory. Under some allocation patterns, some
|
||||
/// large free blocks of memory will be locked between two used chunks, so
|
||||
/// they cannot be given back to the system.
|
||||
///
|
||||
/// The `pad` argument represents the amount of free trailing space to
|
||||
/// leave untrimmed. If this argument is zero, only the minimum amount of
|
||||
/// memory to maintain internal data structures will be left. Non-zero
|
||||
/// arguments can be supplied to maintain enough trailing space to service
|
||||
/// future expected allocations without having to re-obtain memory from the
|
||||
/// system.
|
||||
///
|
||||
/// Returns `true` if it actually released any memory, else `false`.
|
||||
pub unsafe fn trim(&mut self, pad: usize) -> bool {
|
||||
self.0.trim(pad)
|
||||
}
|
||||
|
||||
/// Releases all allocations in this allocator back to the system,
|
||||
/// consuming self and preventing further use.
|
||||
///
|
||||
/// Returns the number of bytes released to the system.
|
||||
pub unsafe fn destroy(self) -> usize {
|
||||
self.0.destroy()
|
||||
}
|
||||
|
||||
/// Get a reference the underlying [`Allocator`] that this `Dlmalloc` was
|
||||
/// constructed with.
|
||||
pub fn allocator(&self) -> &A {
|
||||
self.0.allocator()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
use crate::Allocator;
|
||||
use core::ptr;
|
||||
|
||||
/// System setting for Linux
|
||||
pub struct System {
|
||||
_priv: (),
|
||||
}
|
||||
|
||||
impl System {
|
||||
pub const fn new() -> System {
|
||||
System { _priv: () }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "global")]
|
||||
static mut LOCK: libc::pthread_mutex_t = libc::PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
unsafe impl Allocator for System {
|
||||
fn alloc(&self, size: usize) -> (*mut u8, usize, u32) {
|
||||
let addr = unsafe {
|
||||
libc::mmap(
|
||||
ptr::null_mut(),
|
||||
size,
|
||||
libc::PROT_WRITE | libc::PROT_READ,
|
||||
libc::MAP_ANON | libc::MAP_PRIVATE,
|
||||
-1,
|
||||
0,
|
||||
)
|
||||
};
|
||||
if addr == libc::MAP_FAILED {
|
||||
(ptr::null_mut(), 0, 0)
|
||||
} else {
|
||||
(addr.cast(), size, 0)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn remap(&self, ptr: *mut u8, oldsize: usize, newsize: usize, can_move: bool) -> *mut u8 {
|
||||
let flags = if can_move { libc::MREMAP_MAYMOVE } else { 0 };
|
||||
let ptr = unsafe { libc::mremap(ptr.cast(), oldsize, newsize, flags) };
|
||||
if ptr == libc::MAP_FAILED {
|
||||
ptr::null_mut()
|
||||
} else {
|
||||
ptr.cast()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "redox", target_os = "macos"))]
|
||||
fn remap(&self, _ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool) -> *mut u8 {
|
||||
ptr::null_mut()
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn free_part(&self, ptr: *mut u8, oldsize: usize, newsize: usize) -> bool {
|
||||
unsafe {
|
||||
let rc = libc::mremap(ptr.cast(), oldsize, newsize, 0);
|
||||
if rc != libc::MAP_FAILED {
|
||||
return true;
|
||||
}
|
||||
libc::munmap(ptr.add(newsize).cast(), oldsize - newsize) == 0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "redox", target_os = "macos"))]
|
||||
fn free_part(&self, ptr: *mut u8, oldsize: usize, newsize: usize) -> bool {
|
||||
unsafe { libc::munmap(ptr.add(newsize).cast(), oldsize - newsize) == 0 }
|
||||
}
|
||||
|
||||
fn free(&self, ptr: *mut u8, size: usize) -> bool {
|
||||
unsafe { libc::munmap(ptr.cast(), size) == 0 }
|
||||
}
|
||||
|
||||
fn can_release_part(&self, _flags: u32) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn allocates_zeros(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn page_size(&self) -> usize {
|
||||
4096
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "global")]
|
||||
pub fn acquire_global_lock() {
|
||||
unsafe { assert_eq!(libc::pthread_mutex_lock(ptr::addr_of_mut!(LOCK)), 0) }
|
||||
}
|
||||
|
||||
#[cfg(feature = "global")]
|
||||
pub fn release_global_lock() {
|
||||
unsafe { assert_eq!(libc::pthread_mutex_unlock(ptr::addr_of_mut!(LOCK)), 0) }
|
||||
}
|
||||
|
||||
#[cfg(feature = "global")]
|
||||
/// allows the allocator to remain unsable in the child process,
|
||||
/// after a call to `fork(2)`
|
||||
///
|
||||
/// #Safety
|
||||
///
|
||||
/// if used, this function must be called,
|
||||
/// before any allocations are made with the global allocator.
|
||||
pub unsafe fn enable_alloc_after_fork() {
|
||||
// atfork must only be called once, to avoid a deadlock,
|
||||
// where the handler attempts to acquire the global lock twice
|
||||
static mut FORK_PROTECTED: bool = false;
|
||||
|
||||
unsafe extern "C" fn _acquire_global_lock() {
|
||||
acquire_global_lock()
|
||||
}
|
||||
|
||||
unsafe extern "C" fn _release_global_lock() {
|
||||
release_global_lock()
|
||||
}
|
||||
|
||||
acquire_global_lock();
|
||||
// if a process forks,
|
||||
// it will acquire the lock before any other thread,
|
||||
// protecting it from deadlock,
|
||||
// due to the child being created with only the calling thread.
|
||||
if !FORK_PROTECTED {
|
||||
libc::pthread_atfork(
|
||||
Some(_acquire_global_lock),
|
||||
Some(_release_global_lock),
|
||||
Some(_release_global_lock),
|
||||
);
|
||||
FORK_PROTECTED = true;
|
||||
}
|
||||
release_global_lock();
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
use crate::Allocator;
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use core::arch::wasm32 as wasm;
|
||||
#[cfg(target_arch = "wasm64")]
|
||||
use core::arch::wasm64 as wasm;
|
||||
use core::ptr;
|
||||
|
||||
/// System setting for Wasm
|
||||
pub struct System {
|
||||
_priv: (),
|
||||
}
|
||||
|
||||
impl System {
|
||||
pub const fn new() -> System {
|
||||
System { _priv: () }
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Allocator for System {
|
||||
fn alloc(&self, size: usize) -> (*mut u8, usize, u32) {
|
||||
let pages = size / self.page_size();
|
||||
let prev = wasm::memory_grow(0, pages);
|
||||
if prev == usize::max_value() {
|
||||
return (ptr::null_mut(), 0, 0);
|
||||
}
|
||||
(
|
||||
(prev * self.page_size()) as *mut u8,
|
||||
pages * self.page_size(),
|
||||
0,
|
||||
)
|
||||
}
|
||||
|
||||
fn remap(&self, _ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool) -> *mut u8 {
|
||||
// TODO: I think this can be implemented near the end?
|
||||
ptr::null_mut()
|
||||
}
|
||||
|
||||
fn free_part(&self, _ptr: *mut u8, _oldsize: usize, _newsize: usize) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn free(&self, _ptr: *mut u8, _size: usize) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn can_release_part(&self, _flags: u32) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn allocates_zeros(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn page_size(&self) -> usize {
|
||||
64 * 1024
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "global")]
|
||||
pub fn acquire_global_lock() {
|
||||
// single threaded, no need!
|
||||
assert!(!cfg!(target_feature = "atomics"));
|
||||
}
|
||||
|
||||
#[cfg(feature = "global")]
|
||||
pub fn release_global_lock() {
|
||||
// single threaded, no need!
|
||||
assert!(!cfg!(target_feature = "atomics"));
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[cfg(feature = "global")]
|
||||
pub unsafe fn enable_alloc_after_fork() {
|
||||
// single threaded, no need!
|
||||
assert!(!cfg!(target_feature = "atomics"));
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
use crate::Allocator;
|
||||
use core::mem::MaybeUninit;
|
||||
use core::ptr;
|
||||
use windows_sys::Win32::System::Memory::*;
|
||||
use windows_sys::Win32::System::SystemInformation::*;
|
||||
#[cfg(feature = "global")]
|
||||
use windows_sys::Win32::System::Threading::*;
|
||||
|
||||
pub struct System {
|
||||
_priv: (),
|
||||
}
|
||||
|
||||
impl System {
|
||||
pub const fn new() -> System {
|
||||
System { _priv: () }
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Allocator for System {
|
||||
fn alloc(&self, size: usize) -> (*mut u8, usize, u32) {
|
||||
let addr = unsafe {
|
||||
VirtualAlloc(
|
||||
ptr::null_mut(),
|
||||
size,
|
||||
MEM_RESERVE | MEM_COMMIT,
|
||||
PAGE_READWRITE,
|
||||
)
|
||||
};
|
||||
|
||||
if addr.is_null() {
|
||||
(ptr::null_mut(), 0, 0)
|
||||
} else {
|
||||
(addr.cast(), size, 0)
|
||||
}
|
||||
}
|
||||
|
||||
fn remap(&self, _ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool) -> *mut u8 {
|
||||
ptr::null_mut()
|
||||
}
|
||||
|
||||
fn free_part(&self, ptr: *mut u8, oldsize: usize, newsize: usize) -> bool {
|
||||
unsafe { VirtualFree(ptr.add(newsize).cast(), oldsize - newsize, MEM_DECOMMIT) != 0 }
|
||||
}
|
||||
|
||||
fn free(&self, ptr: *mut u8, _size: usize) -> bool {
|
||||
unsafe { VirtualFree(ptr.cast(), 0, MEM_DECOMMIT) != 0 }
|
||||
}
|
||||
|
||||
fn can_release_part(&self, _flags: u32) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn allocates_zeros(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn page_size(&self) -> usize {
|
||||
unsafe {
|
||||
let mut info = MaybeUninit::uninit();
|
||||
GetSystemInfo(info.as_mut_ptr());
|
||||
info.assume_init_ref().dwPageSize as usize
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NB: `SRWLOCK_INIT` doesn't appear to be in `windows-sys`
|
||||
#[cfg(feature = "global")]
|
||||
static mut LOCK: SRWLOCK = SRWLOCK {
|
||||
Ptr: ptr::null_mut(),
|
||||
};
|
||||
|
||||
#[cfg(feature = "global")]
|
||||
pub fn acquire_global_lock() {
|
||||
unsafe {
|
||||
AcquireSRWLockExclusive(ptr::addr_of_mut!(LOCK));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "global")]
|
||||
pub fn release_global_lock() {
|
||||
unsafe {
|
||||
ReleaseSRWLockExclusive(ptr::addr_of_mut!(LOCK));
|
||||
}
|
||||
}
|
||||
|
||||
/// Not needed on Windows
|
||||
#[cfg(feature = "global")]
|
||||
pub unsafe fn enable_alloc_after_fork() {}
|
||||
@@ -0,0 +1,117 @@
|
||||
use crate::Allocator;
|
||||
use core::ptr;
|
||||
|
||||
pub struct System {
|
||||
_priv: (),
|
||||
}
|
||||
|
||||
impl System {
|
||||
pub const fn new() -> System {
|
||||
System { _priv: () }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "riscv32")]
|
||||
mod sys {
|
||||
use core::arch::asm;
|
||||
|
||||
pub fn increase_heap(length: usize) -> Result<(usize, usize), ()> {
|
||||
let syscall_no_increase_heap = 10usize;
|
||||
let memory_flags_read_write = 2usize | 4usize;
|
||||
|
||||
let mut a0 = syscall_no_increase_heap;
|
||||
let mut a1 = length;
|
||||
let mut a2 = memory_flags_read_write;
|
||||
|
||||
unsafe {
|
||||
asm!(
|
||||
"ecall",
|
||||
inlateout("a0") a0,
|
||||
inlateout("a1") a1,
|
||||
inlateout("a2") a2,
|
||||
out("a3") _,
|
||||
out("a4") _,
|
||||
out("a5") _,
|
||||
out("a6") _,
|
||||
out("a7") _,
|
||||
)
|
||||
};
|
||||
|
||||
let result = a0;
|
||||
let address = a1;
|
||||
let length = a2;
|
||||
|
||||
// 3 is the "MemoryRange" type, and the result is only valid
|
||||
// if we get nonzero address and length.
|
||||
if result == 3 && address != 0 && length != 0 {
|
||||
Ok((address, length))
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Allocator for System {
|
||||
/// Allocate an additional `size` bytes on the heap, and return a new
|
||||
/// chunk of memory, as well as the size of the allocation and some
|
||||
/// flags. Since flags are unused on this platform, they will always
|
||||
/// be `0`.
|
||||
fn alloc(&self, size: usize) -> (*mut u8, usize, u32) {
|
||||
let size = if size == 0 {
|
||||
4096
|
||||
} else if size & 4095 == 0 {
|
||||
size
|
||||
} else {
|
||||
size + (4096 - (size & 4095))
|
||||
};
|
||||
|
||||
if let Ok((address, length)) = sys::increase_heap(size) {
|
||||
let start = address - size + length;
|
||||
(start as *mut u8, size, 0)
|
||||
} else {
|
||||
(ptr::null_mut(), 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
fn remap(&self, _ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool) -> *mut u8 {
|
||||
// TODO
|
||||
ptr::null_mut()
|
||||
}
|
||||
|
||||
fn free_part(&self, _ptr: *mut u8, _oldsize: usize, _newsize: usize) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn free(&self, _ptr: *mut u8, _size: usize) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn can_release_part(&self, _flags: u32) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn allocates_zeros(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn page_size(&self) -> usize {
|
||||
4 * 1024
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "global")]
|
||||
pub fn acquire_global_lock() {
|
||||
// global feature should not be enabled
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
#[cfg(feature = "global")]
|
||||
pub fn release_global_lock() {
|
||||
// global feature should not be enabled
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
#[cfg(feature = "global")]
|
||||
pub unsafe fn enable_alloc_after_fork() {
|
||||
// platform does not support `fork()` call
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
extern crate dlmalloc;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::thread;
|
||||
|
||||
#[global_allocator]
|
||||
#[cfg(feature = "global")]
|
||||
static A: dlmalloc::GlobalDlmalloc = dlmalloc::GlobalDlmalloc;
|
||||
|
||||
#[test]
|
||||
fn foo() {
|
||||
println!("hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn map() {
|
||||
let mut m = HashMap::new();
|
||||
m.insert(1, 2);
|
||||
m.insert(5, 3);
|
||||
drop(m);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strings() {
|
||||
format!("foo, bar, {}", "baz");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(not(target_family = "wasm"))]
|
||||
fn threads() {
|
||||
assert!(thread::spawn(|| panic!()).join().is_err());
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
use arbitrary::Unstructured;
|
||||
use dlmalloc::Dlmalloc;
|
||||
use rand::{rngs::SmallRng, RngCore, SeedableRng};
|
||||
|
||||
#[test]
|
||||
fn smoke() {
|
||||
let mut a = Dlmalloc::new();
|
||||
unsafe {
|
||||
let ptr = a.malloc(1, 1);
|
||||
assert!(!ptr.is_null());
|
||||
*ptr = 9;
|
||||
assert_eq!(*ptr, 9);
|
||||
a.free(ptr, 1, 1);
|
||||
|
||||
let ptr = a.malloc(1, 1);
|
||||
assert!(!ptr.is_null());
|
||||
*ptr = 10;
|
||||
assert_eq!(*ptr, 10);
|
||||
a.free(ptr, 1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[path = "../fuzz/src/lib.rs"]
|
||||
mod fuzz;
|
||||
|
||||
#[test]
|
||||
fn stress() {
|
||||
let mut rng = SmallRng::seed_from_u64(0);
|
||||
let mut buf = vec![0; 4096];
|
||||
let iters = if cfg!(miri) { 5 } else { 2000 };
|
||||
for _ in 0..iters {
|
||||
rng.fill_bytes(&mut buf);
|
||||
let mut u = Unstructured::new(&buf);
|
||||
let _ = fuzz::run(&mut u);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user