Skip to content

Fix initializing the global hash map with a small stack #227

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

Merged
merged 1 commit into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "string_cache"
version = "0.7.3" # Also update README.md when making a semver-breaking change
version = "0.7.4" # Also update README.md when making a semver-breaking change
authors = [ "The Servo Project Developers" ]
description = "A string interning library for Rust, developed as part of the Servo project."
license = "MIT / Apache-2.0"
Expand Down Expand Up @@ -41,3 +41,7 @@ string_cache_codegen = { version = "0.4", path = "./string-cache-codegen" }

[build-dependencies]
string_cache_codegen = { version = "0.4", path = "./string-cache-codegen" }

[[test]]
name = "small-stack"
harness = false
9 changes: 7 additions & 2 deletions src/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const NB_BUCKETS: usize = 1 << 12; // 4096
const BUCKET_MASK: u64 = (1 << 12) - 1;

struct StringCache {
buckets: [Option<Box<StringCacheEntry>>; NB_BUCKETS],
buckets: Box<[Option<Box<StringCacheEntry>>; NB_BUCKETS]>,
}

lazy_static! {
Expand Down Expand Up @@ -67,8 +67,13 @@ impl StringCacheEntry {

impl StringCache {
fn new() -> StringCache {
type T = Option<Box<StringCacheEntry>>;
let _static_assert_size_eq = std::mem::transmute::<T, usize>;
let vec = std::mem::ManuallyDrop::new(vec![0_usize; NB_BUCKETS]);
StringCache {
buckets: unsafe { mem::zeroed() },
buckets: unsafe {
Box::from_raw(vec.as_ptr() as *mut [T; NB_BUCKETS])
},
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/small-stack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Regression test for https://github.com/servo/html5ever/issues/393
//
// Create a dynamic atom − causing initialization of the golbal hash map −
// in a thread that has a small stack.
//
// This is a separate test program rather than a `#[test] fn` among others
// to make sure that nothing else has already initialized the map in this process.
fn main() {
std::thread::Builder::new().stack_size(50_000).spawn(|| {
string_cache::DefaultAtom::from("12345678");
}).unwrap().join().unwrap()
}