Skip to content

Commit b5ef0fd

Browse files
committed
add secure_basic and secure_checked to the Rust crate
1 parent 97740cd commit b5ef0fd

5 files changed

Lines changed: 22 additions & 12 deletions

File tree

‎contrib/rust/Cargo.toml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ members = ["sys"]
2323
default = []
2424
# build options of the C library (see `sys/Cargo.toml`)
2525
secure = ["mimalloc-sys/secure"]
26+
secure_basic = ["mimalloc-sys/secure_basic"]
27+
secure_checked = ["mimalloc-sys/secure_checked"]
2628
override = ["mimalloc-sys/override"]
2729
debug = ["mimalloc-sys/debug"]
2830
debug_in_debug = ["mimalloc-sys/debug_in_debug"]
2931
local_dynamic_tls = ["mimalloc-sys/local_dynamic_tls"]
30-
win_direct_tls = ["mimalloc-sys/win_direct_tls"]
3132
no_thp = ["mimalloc-sys/no_thp"]
3233
opt_arch = ["mimalloc-sys/opt_arch"]
3334
mte = ["mimalloc-sys/mte"]

‎contrib/rust/README.md‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,29 @@ let heap = Heap::new_in_arena(arena);
4444
- An `exclusive` arena keeps all other heaps out; otherwise any heap can use it as well.
4545
- Arenas cannot be removed, so they live until the process exits.
4646
- Arenas need at least `Arena::min_size()` bytes (32 MiB on 64-bit), and their start is
47-
aligned to `Arena::min_alignment()` (256 MiB on 64-bit, or 64 KiB with the `secure` feature),
47+
aligned to `Arena::min_alignment()` (256 MiB on 64-bit, or 64 KiB with the `secure` or `secure_checked` feature),
4848
so memory at an unaligned address needs `max(min_size, min_alignment) + min_alignment` bytes.
4949

5050
## Features
5151

5252
| Feature | Description |
5353
|---------------------|-------------|
54-
| `secure` | Security mitigations (guard pages, randomization, encoded free lists) |
54+
| `secure` | Security mitigations (as `-DMI_SECURE=ON`) |
55+
| `secure_basic` | Cheap security mitigations: guard pages, randomization, encoded free lists, double free detection (as `-DMI_SECURE=BASIC`) |
56+
| `secure_checked` | `secure_basic` plus validated free pointers and randomized free lists (as `-DMI_SECURE=CHECKED`) |
5557
| `override` | Also override `malloc`, `free`, etc. in the final binary |
5658
| `debug` | Build mimalloc with assertions and internal checks |
5759
| `debug_in_debug` | As `debug`, but only when `debug_assertions` are on |
5860
| `local_dynamic_tls` | Use the local-dynamic TLS model (for dynamically loaded libraries) |
59-
| `win_direct_tls` | Use direct TLS slots on Windows |
6061
| `no_thp` | Disable transparent huge pages by default on Linux |
6162
| `opt_arch` | Architecture specific optimizations (as `-DMI_OPT_ARCH=ON`) |
6263
| `mte` | Memory tagging; requires MTE support at runtime (aarch64 Linux, Android, and Apple only (where macOS also needs the MTE entitlement); as `-DMI_MTE=ON`, which also enables PAC unless `MIMALLOC_DEFINES="MI_PAC=0"`) |
6364
| `pac` | Sign internal pointers with pointer authentication; requires PAC support at runtime (aarch64 Linux, Android, and Apple only; as `-DMI_PAC=ON`) |
6465
| `nightly` | `Allocator` support on nightly compilers before Rust 1.100 (`mimalloc` only) |
6566

6667
All features except `nightly` are build options of the C library in `mimalloc-sys`
67-
(and are forwarded by `mimalloc`). Feature `mte` does not a separate `secure` (but encompasses it).
68-
The combination `mte` and `pac` offers strong hardware supported security mitigation.
68+
(and are forwarded by `mimalloc`). Feature `mte` does not need a separate `secure` or `pac` (but encompasses it).
69+
If several `secure*` features are enabled, the highest level is used.
6970

7071
Architecture optimizations are also enabled automatically if the Rust target features
7172
already imply them (e.g. `-C target-cpu=native`).

‎contrib/rust/sys/Cargo.toml‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ include = ["/src", "/build.rs", "/README.md", "/mimalloc/LICENSE", "/mimalloc/in
2020

2121
[features]
2222
default = []
23-
# security mitigations (guard pages, randomization, encoded free lists, double free detection)
23+
# security mitigations (as cmake `-DMI_SECURE=ON`); with several secure features the highest level is used
2424
secure = []
25+
# cheap security mitigations: guard pages around metadata, randomization, encoded free lists, double free detection (as `-DMI_SECURE=BASIC`)
26+
secure_basic = []
27+
# `secure_basic` plus validated free pointers, stronger free list encoding, and randomized free lists (as `-DMI_SECURE=CHECKED`)
28+
secure_checked = []
2529
# also override the standard `malloc`, `free`, etc. in the final binary
2630
override = []
2731
# build mimalloc with assertions and internal invariant checks
@@ -30,8 +34,6 @@ debug = []
3034
debug_in_debug = []
3135
# use the local-dynamic TLS model (when mimalloc is part of a dynamically loaded library)
3236
local_dynamic_tls = []
33-
# use only direct TLS slots on Windows (faster but requires fewer than 64 TLS slots in total)
34-
win_direct_tls = []
3537
# disable transparent huge pages by default on Linux
3638
no_thp = []
3739
# use architecture specific optimizations (as cmake `-DMI_OPT_ARCH=ON`)

‎contrib/rust/sys/build.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,13 @@ fn main() {
126126
}
127127
define("MI_PAC", "2"); // ON
128128
}
129+
// the highest enabled secure level is used (as features are additive)
129130
if feature("secure") {
130131
define("MI_SECURE", "4"); // ON
132+
} else if feature("secure_checked") {
133+
define("MI_SECURE", "2"); // CHECKED
134+
} else if feature("secure_basic") {
135+
define("MI_SECURE", "1"); // BASIC
131136
} // (otherwise the default level in `types.h` is used: 2 with `mte` or `pac`, 3 on CHERI, and 0 otherwise;
132137
// opt out explicitly with `MIMALLOC_DEFINES="MI_SECURE=0"`)
133138
let is_override = feature("override") && !is_windows;
@@ -146,9 +151,6 @@ fn main() {
146151
}
147152
// Rust never uses the C++ `new` API, so don't define or reference `std::get_new_handler`
148153
define("MI_NO_CXX_NEW_HANDLER", "1");
149-
if feature("win_direct_tls") && is_windows {
150-
define("MI_WIN_DIRECT_TLS", "1");
151-
}
152154
for (name, value) in &user {
153155
build.define(name, value.as_deref());
154156
}

‎contrib/rust/tests/arena.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ fn expect_pinned_arena(arena: Option<Arena>) -> Option<Arena> {
2929
// Match the C defaults and explicit overrides, not just the Cargo `secure` feature.
3030
let default_secure = if cfg!(feature = "secure") {
3131
4
32+
} else if cfg!(feature = "secure_checked") {
33+
2
34+
} else if cfg!(feature = "secure_basic") {
35+
1
3236
} else if build_define("MI_MTE", 2 * usize::from(cfg!(feature = "mte"))) != 0
3337
|| build_define("MI_PAC", 2 * usize::from(cfg!(feature = "pac"))) != 0
3438
{

0 commit comments

Comments
 (0)