Skip to content

Commit 9c52e10

Browse files
author
bors-servo
authored
Auto merge of #227 - servo:small-stack, r=nox
Fix initializing the global hash map with a small stack Fix servo/html5ever#393 <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/string-cache/227) <!-- Reviewable:end -->
2 parents 51b58ce + af7a9c1 commit 9c52e10

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

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

4242
[build-dependencies]
4343
string_cache_codegen = { version = "0.4", path = "./string-cache-codegen" }
44+
45+
[[test]]
46+
name = "small-stack"
47+
harness = false

src/atom.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const NB_BUCKETS: usize = 1 << 12; // 4096
3939
const BUCKET_MASK: u64 = (1 << 12) - 1;
4040

4141
struct StringCache {
42-
buckets: [Option<Box<StringCacheEntry>>; NB_BUCKETS],
42+
buckets: Box<[Option<Box<StringCacheEntry>>; NB_BUCKETS]>,
4343
}
4444

4545
lazy_static! {
@@ -67,8 +67,13 @@ impl StringCacheEntry {
6767

6868
impl StringCache {
6969
fn new() -> StringCache {
70+
type T = Option<Box<StringCacheEntry>>;
71+
let _static_assert_size_eq = std::mem::transmute::<T, usize>;
72+
let vec = std::mem::ManuallyDrop::new(vec![0_usize; NB_BUCKETS]);
7073
StringCache {
71-
buckets: unsafe { mem::zeroed() },
74+
buckets: unsafe {
75+
Box::from_raw(vec.as_ptr() as *mut [T; NB_BUCKETS])
76+
},
7277
}
7378
}
7479

tests/small-stack.rs

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

0 commit comments

Comments
 (0)