-
Notifications
You must be signed in to change notification settings - Fork 13.3k
BTreeSet
causes general protection fault with zero-sized key-value pairs and jemalloc
#28493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The use std::collections::BTreeMap;
fn main() {
let _: BTreeMap<(), ()> = BTreeMap::new();
} Note that |
Note also that this is only the case with jemalloc. The following runs successfully: #![feature(alloc_system)]
extern crate alloc_system;
use std::collections::BTreeMap;
fn main() {
let _: BTreeMap<(), ()> = BTreeMap::new();
} |
BTreeSet
causes general protection fault with zero-sized key-value pairsBTreeSet
causes general protection fault with zero-sized key-value pairs and jemalloc
Actually, this is just related to calling jemalloc with 0 for the size: #![feature(alloc, heap_api)]
extern crate alloc;
fn main() {
let size = std::mem::size_of::<()>();
let align = std::mem::align_of::<()>();
unsafe {
alloc::heap::allocate(size, align);
}
} > valgrind ./foo
==16282== Memcheck, a memory error detector
==16282== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==16282== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==16282== Command: ./foo
==16282==
==16282== Invalid read of size 1
==16282== at 0x11B7B1: je_mallocx (in /home/andrew/foo)
==16282== by 0x10C78F: heap::allocate::hef78f640aa355f8bPda (in /home/andrew/foo)
==16282== by 0x10C710: main::h2fbc6b3b4aacc11afaa (in /home/andrew/foo)
==16282== by 0x112D94: sys_common::unwind::try::try_fn::h14601444602367533931 (in /home/andrew/foo)
==16282== by 0x10FDA8: __rust_try (in /home/andrew/foo)
==16282== by 0x112A08: rt::lang_start::h7135051af8990a95Zkx (in /home/andrew/foo)
==16282== by 0x10C7C9: main (in /home/andrew/foo)
==16282== Address 0x200000000013dd7f is not stack'd, malloc'd or (recently) free'd
==16282==
==16282==
==16282== Process terminating with default action of signal 11 (SIGSEGV)
==16282== General Protection Fault
==16282== at 0x11B7B1: je_mallocx (in /home/andrew/foo)
==16282== by 0x10C78F: heap::allocate::hef78f640aa355f8bPda (in /home/andrew/foo)
==16282== by 0x10C710: main::h2fbc6b3b4aacc11afaa (in /home/andrew/foo)
==16282== by 0x112D94: sys_common::unwind::try::try_fn::h14601444602367533931 (in /home/andrew/foo)
==16282== by 0x10FDA8: __rust_try (in /home/andrew/foo)
==16282== by 0x112A08: rt::lang_start::h7135051af8990a95Zkx (in /home/andrew/foo)
==16282== by 0x10C7C9: main (in /home/andrew/foo)
==16282==
==16282== HEAP SUMMARY:
==16282== in use at exit: 32 bytes in 1 blocks
==16282== total heap usage: 5 allocs, 4 frees, 976 bytes allocated
==16282==
==16282== LEAK SUMMARY:
==16282== definitely lost: 0 bytes in 0 blocks
==16282== indirectly lost: 0 bytes in 0 blocks
==16282== possibly lost: 0 bytes in 0 blocks
==16282== still reachable: 32 bytes in 1 blocks
==16282== suppressed: 0 bytes in 0 blocks
==16282== Rerun with --leak-check=full to see details of leaked memory
==16282==
==16282== For counts of detected and suppressed errors, rerun with: -v
==16282== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped) |
We should probably apply a temporary fix for this (and audit all other uses of |
triage: I-nominated Seems pretty serious and something we may want to fix soon! |
cc @gankro |
I have a potential PR in progress, just running tests locally. |
When both the key and value types were zero-sized, `BTreeMap` previously called `heap::allocate` with `size == 0` for leaf nodes, which is undefined behavior, and jemalloc would attempt to read invalid memory, crashing the process. This avoids undefined behavior by allocating enough space to store one edge in leaf nodes that would otherwise have `size == 0`. Although this uses extra memory, maps with zero-sized key types that have sensible implementations of the ordering traits can only contain a single key-value pair (and therefore only a single leaf node), and maps with key and value types that are both zero-sized have few uses, if any. Furthermore, this is a temporary fix that will likely be unnecessary once the `BTreeMap` implementation is rewritten to use parent pointers. Closes #28493.
foo.rs:
Output:
The text was updated successfully, but these errors were encountered: