Skip to content

Commit 0974fd6

Browse files
committed
Use owned_slice instead of owning_ref to store metadata.
`owning_ref` of a `Box<T>` has soundness problems because it stores pointers to a box that is then invalidated. Instead, use the safer `owned_slice`, which presents a more specialized abstraction. This doesn't change the general layout or indirection of the structure, but only the datastructure used as a wrapper.
1 parent f62b007 commit 0974fd6

File tree

6 files changed

+20
-44
lines changed

6 files changed

+20
-44
lines changed

compiler/rustc_codegen_ssa/src/back/metadata.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use object::{
1313
use snap::write::FrameEncoder;
1414

1515
use rustc_data_structures::memmap::Mmap;
16-
use rustc_data_structures::owning_ref::OwningRef;
17-
use rustc_data_structures::rustc_erase_owner;
16+
use rustc_data_structures::owned_slice::OwnedSlice;
1817
use rustc_data_structures::sync::MetadataRef;
1918
use rustc_metadata::EncodedMetadata;
2019
use rustc_session::cstore::MetadataLoader;
@@ -44,8 +43,11 @@ fn load_metadata_with(
4443
File::open(path).map_err(|e| format!("failed to open file '{}': {}", path.display(), e))?;
4544
let data = unsafe { Mmap::map(file) }
4645
.map_err(|e| format!("failed to mmap file '{}': {}", path.display(), e))?;
47-
let metadata = OwningRef::new(data).try_map(f)?;
48-
return Ok(rustc_erase_owner!(metadata.map_owner_box()));
46+
47+
let metadata: MetadataRef = OwnedSlice::new(Box::new(data));
48+
let metadata = metadata.try_map(f)?;
49+
50+
Ok(metadata)
4951
}
5052

5153
impl MetadataLoader for DefaultMetadataLoader {

compiler/rustc_data_structures/src/sync.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@
1414
//!
1515
//! `MTRef` is an immutable reference if cfg!(parallel_compiler), and a mutable reference otherwise.
1616
//!
17-
//! `rustc_erase_owner!` erases an OwningRef owner into Erased or Erased + Send + Sync
18-
//! depending on the value of cfg!(parallel_compiler).
1917
20-
use crate::owning_ref::{Erased, OwningRef};
18+
use crate::owned_slice::OwnedSlice;
2119
use std::collections::HashMap;
2220
use std::hash::{BuildHasher, Hash};
2321
use std::ops::{Deref, DerefMut};
24-
2522
pub use std::sync::atomic::Ordering;
2623
pub use std::sync::atomic::Ordering::SeqCst;
2724

@@ -33,13 +30,6 @@ cfg_if! {
3330
impl<T: ?Sized> Send for T {}
3431
impl<T: ?Sized> Sync for T {}
3532

36-
#[macro_export]
37-
macro_rules! rustc_erase_owner {
38-
($v:expr) => {
39-
$v.erase_owner()
40-
}
41-
}
42-
4333
use std::ops::Add;
4434
use std::panic::{resume_unwind, catch_unwind, AssertUnwindSafe};
4535

@@ -162,7 +152,7 @@ cfg_if! {
162152
}
163153
}
164154

165-
pub type MetadataRef = OwningRef<Box<dyn Erased>, [u8]>;
155+
pub type MetadataRef = OwnedSlice<Box<dyn Deref<Target = [u8]>>, u8>;
166156

167157
pub use std::rc::Rc as Lrc;
168158
pub use std::rc::Weak as Weak;
@@ -342,20 +332,11 @@ cfg_if! {
342332
t.into_par_iter().for_each(for_each)
343333
}
344334

345-
pub type MetadataRef = OwningRef<Box<dyn Erased + Send + Sync>, [u8]>;
335+
pub type MetadataRef = OwnedSlice<Box<dyn Deref<Target = [u8]> + Send + Sync>, u8>;
346336

347337
/// This makes locks panic if they are already held.
348338
/// It is only useful when you are running in a single thread
349339
const ERROR_CHECKING: bool = false;
350-
351-
#[macro_export]
352-
macro_rules! rustc_erase_owner {
353-
($v:expr) => {{
354-
let v = $v;
355-
::rustc_data_structures::sync::assert_send_val(&v);
356-
v.erase_send_sync_owner()
357-
}}
358-
}
359340
}
360341
}
361342

compiler/rustc_metadata/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ extern crate proc_macro;
2424
extern crate rustc_macros;
2525
#[macro_use]
2626
extern crate rustc_middle;
27-
#[macro_use]
28-
extern crate rustc_data_structures;
2927

3028
pub use rmeta::{provide, provide_extern};
3129

compiler/rustc_metadata/src/locator.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ use crate::rmeta::{rustc_version, MetadataBlob, METADATA_HEADER};
217217

218218
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
219219
use rustc_data_structures::memmap::Mmap;
220-
use rustc_data_structures::owning_ref::OwningRef;
221220
use rustc_data_structures::svh::Svh;
222221
use rustc_data_structures::sync::MetadataRef;
223222
use rustc_errors::{struct_span_err, FatalError};
@@ -231,6 +230,7 @@ use rustc_span::symbol::{sym, Symbol};
231230
use rustc_span::Span;
232231
use rustc_target::spec::{Target, TargetTriple};
233232

233+
use rustc_data_structures::owned_slice::OwnedSlice;
234234
use snap::read::FrameDecoder;
235235
use std::fmt::Write as _;
236236
use std::io::{Read, Result as IoResult, Write};
@@ -774,7 +774,7 @@ fn get_metadata_section<'p>(
774774
// don't have to grow the buffer as much.
775775
let mut inflated = Vec::with_capacity(compressed_bytes.len());
776776
match FrameDecoder::new(compressed_bytes).read_to_end(&mut inflated) {
777-
Ok(_) => rustc_erase_owner!(OwningRef::new(inflated).map_owner_box()),
777+
Ok(_) => OwnedSlice::new(Box::new(inflated)),
778778
Err(_) => {
779779
return Err(MetadataError::LoadFailure(format!(
780780
"failed to decompress metadata: {}",
@@ -799,7 +799,7 @@ fn get_metadata_section<'p>(
799799
))
800800
})?;
801801

802-
rustc_erase_owner!(OwningRef::new(mmap).map_owner_box())
802+
OwnedSlice::new(Box::new(mmap))
803803
}
804804
};
805805
let blob = MetadataBlob::new(raw_bytes);

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,13 @@ mod cstore_impl;
5656
#[derive(Clone)]
5757
pub(crate) struct MetadataBlob(Lrc<MetadataRef>);
5858

59-
// This is needed so we can create an OwningRef into the blob.
60-
// The data behind a `MetadataBlob` has a stable address because it is
61-
// contained within an Rc/Arc.
62-
unsafe impl rustc_data_structures::owning_ref::StableAddress for MetadataBlob {}
63-
64-
// This is needed so we can create an OwningRef into the blob.
59+
// This is needed so we can create an OwnedSlice into the blob.
6560
impl std::ops::Deref for MetadataBlob {
66-
type Target = [u8];
61+
type Target = MetadataRef;
6762

6863
#[inline]
69-
fn deref(&self) -> &[u8] {
70-
&self.0[..]
64+
fn deref(&self) -> &Self::Target {
65+
&self.0
7166
}
7267
}
7368

compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::rmeta::DecodeContext;
22
use crate::rmeta::EncodeContext;
33
use crate::rmeta::MetadataBlob;
4-
use rustc_data_structures::owning_ref::OwningRef;
4+
use rustc_data_structures::owned_slice::OwnedSlice;
55
use rustc_hir::def_path_hash_map::{Config as HashMapConfig, DefPathHashMap};
66
use rustc_middle::parameterized_over_tcx;
77
use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder};
88
use rustc_span::def_id::{DefIndex, DefPathHash};
99

1010
pub(crate) enum DefPathHashMapRef<'tcx> {
11-
OwnedFromMetadata(odht::HashTable<HashMapConfig, OwningRef<MetadataBlob, [u8]>>),
11+
OwnedFromMetadata(odht::HashTable<HashMapConfig, OwnedSlice<MetadataBlob, u8>>),
1212
BorrowedFromTcx(&'tcx DefPathHashMap),
1313
}
1414

@@ -50,9 +50,9 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for DefPathHashMapRef<'static>
5050

5151
let len = d.read_usize();
5252
let pos = d.position();
53-
let o = OwningRef::new(d.blob().clone()).map(|x| &x[pos..pos + len]);
53+
let o = OwnedSlice::new(d.blob().clone()).map(|x| &x[pos..pos + len]);
5454

55-
// Although we already have the data we need via the OwningRef, we still need
55+
// Although we already have the data we need via the OwnedSlice, we still need
5656
// to advance the DecodeContext's position so it's in a valid state after
5757
// the method. We use read_raw_bytes() for that.
5858
let _ = d.read_raw_bytes(len);

0 commit comments

Comments
 (0)