Skip to content

Commit 9697415

Browse files
committed
Add a footer in FileEncoder and check for it in MemDecoder
1 parent d7ea278 commit 9697415

File tree

5 files changed

+27
-14
lines changed

5 files changed

+27
-14
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ impl<'a, 'tcx> TyDecoder for DecodeContext<'a, 'tcx> {
392392
where
393393
F: FnOnce(&mut Self) -> R,
394394
{
395-
let new_opaque = MemDecoder::new(self.opaque.data(), pos);
395+
let new_opaque = self.opaque.split_at(pos);
396396
let old_opaque = mem::replace(&mut self.opaque, new_opaque);
397397
let old_state = mem::replace(&mut self.lazy_state, LazyState::NoNode);
398398
let r = f(self);

compiler/rustc_middle/src/query/on_disk_cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ impl<'a, 'tcx> TyDecoder for CacheDecoder<'a, 'tcx> {
558558
{
559559
debug_assert!(pos < self.opaque.len());
560560

561-
let new_opaque = MemDecoder::new(self.opaque.data(), pos);
561+
let new_opaque = self.opaque.split_at(pos);
562562
let old_opaque = mem::replace(&mut self.opaque, new_opaque);
563563
let r = f(self);
564564
self.opaque = old_opaque;

compiler/rustc_query_system/src/dep_graph/serialized.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,13 @@ impl SerializedDepGraph {
182182
pub fn decode<D: Deps>(d: &mut MemDecoder<'_>) -> Arc<SerializedDepGraph> {
183183
// The last 16 bytes are the node count and edge count.
184184
debug!("position: {:?}", d.position());
185-
let (node_count, edge_count, graph_size) =
186-
d.with_position(d.len() - 3 * IntEncodedWithFixedSize::ENCODED_SIZE, |d| {
185+
let (node_count, edge_count) =
186+
d.with_position(d.len() - 2 * IntEncodedWithFixedSize::ENCODED_SIZE, |d| {
187187
debug!("position: {:?}", d.position());
188188
let node_count = IntEncodedWithFixedSize::decode(d).0 as usize;
189189
let edge_count = IntEncodedWithFixedSize::decode(d).0 as usize;
190-
let graph_size = IntEncodedWithFixedSize::decode(d).0 as usize;
191-
(node_count, edge_count, graph_size)
190+
(node_count, edge_count)
192191
});
193-
assert_eq!(d.len(), graph_size);
194192
debug!("position: {:?}", d.position());
195193

196194
debug!(?node_count, ?edge_count);
@@ -606,8 +604,6 @@ impl<D: Deps> EncoderState<D> {
606604
debug!("position: {:?}", encoder.position());
607605
IntEncodedWithFixedSize(node_count).encode(&mut encoder);
608606
IntEncodedWithFixedSize(edge_count).encode(&mut encoder);
609-
let graph_size = encoder.position() + IntEncodedWithFixedSize::ENCODED_SIZE;
610-
IntEncodedWithFixedSize(graph_size as u64).encode(&mut encoder);
611607
debug!("position: {:?}", encoder.position());
612608
// Drop the encoder so that nothing is written after the counts.
613609
let result = encoder.finish();

compiler/rustc_serialize/src/opaque.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use crate::int_overflow::DebugStrictAdd;
1717

1818
pub type FileEncodeResult = Result<usize, (PathBuf, io::Error)>;
1919

20+
const FOOTER: &[u8] = b"rust-end-file";
21+
2022
/// The size of the buffer in `FileEncoder`.
2123
const BUF_SIZE: usize = 8192;
2224

@@ -181,6 +183,7 @@ impl FileEncoder {
181183
}
182184

183185
pub fn finish(&mut self) -> FileEncodeResult {
186+
self.write_all(FOOTER);
184187
self.flush();
185188
#[cfg(debug_assertions)]
186189
{
@@ -262,14 +265,24 @@ pub struct MemDecoder<'a> {
262265
impl<'a> MemDecoder<'a> {
263266
#[inline]
264267
pub fn new(data: &'a [u8], position: usize) -> MemDecoder<'a> {
268+
let (data, footer) = data.split_at(data.len() - FOOTER.len());
269+
assert_eq!(footer, FOOTER);
265270
let Range { start, end } = data.as_ptr_range();
266271
MemDecoder { start, current: data[position..].as_ptr(), end, _marker: PhantomData }
267272
}
268273

269274
#[inline]
270-
pub fn data(&self) -> &'a [u8] {
271-
// SAFETY: This recovers the original slice, only using members we never modify.
272-
unsafe { std::slice::from_raw_parts(self.start, self.len()) }
275+
pub fn split_at(&self, position: usize) -> MemDecoder<'a> {
276+
assert!(position <= self.len());
277+
// SAFETY: yolo
278+
unsafe {
279+
MemDecoder {
280+
start: self.start,
281+
current: self.start.add(position),
282+
end: self.end,
283+
_marker: PhantomData,
284+
}
285+
}
273286
}
274287

275288
#[inline]

compiler/rustc_serialize/tests/leb128.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ macro_rules! impl_test_unsigned_leb128 {
2525
let n = $write_fn_name(&mut buf, x);
2626
stream.extend(&buf[..n]);
2727
}
28+
let stream_end = stream.len();
29+
stream.extend(b"rust-end-file");
2830

2931
let mut decoder = rustc_serialize::opaque::MemDecoder::new(&stream, 0);
3032
for &expected in &values {
3133
let actual = $read_fn_name(&mut decoder);
3234
assert_eq!(expected, actual);
3335
}
34-
assert_eq!(stream.len(), decoder.position());
36+
assert_eq!(stream_end, decoder.position());
3537
}
3638
};
3739
}
@@ -72,13 +74,15 @@ macro_rules! impl_test_signed_leb128 {
7274
let n = $write_fn_name(&mut buf, x);
7375
stream.extend(&buf[..n]);
7476
}
77+
let stream_end = stream.len();
78+
stream.extend(b"rust-end-file");
7579

7680
let mut decoder = rustc_serialize::opaque::MemDecoder::new(&stream, 0);
7781
for &expected in &values {
7882
let actual = $read_fn_name(&mut decoder);
7983
assert_eq!(expected, actual);
8084
}
81-
assert_eq!(stream.len(), decoder.position());
85+
assert_eq!(stream_end, decoder.position());
8286
}
8387
};
8488
}

0 commit comments

Comments
 (0)