flowistry_pdg_construction/
encoder.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Readers and writers for the intermediate artifacts we store per crate.
//!
//! Most of this code is adapted/copied from `EncodeContext` and `DecodeContext`
//! in `rustc_metadata`.
//!
//! We use a lot of `min_specialization` here to change how `DefId`s, `Span`s
//! and such are encoded since their default implementations are panics.
//!
//! Specifically for `CrateNum` (e.g. `DefId` also), we use stable crate hashes.
//! These appear to work fine, however I am not sure they are guaranteed to be
//! stable across different crates. Rustc itself uses an explicit remapping
//! replying on `CrateMetadataRef`, which we can construct but not use (relevant
//! functions are hidden).
//!
//! Note that we encode `AllocId`s simply as themselves. This is possibly
//! incorrect but we're not really relying on this information at the moment so
//! we are not investing in it.
use std::fs::File;
use std::io::{self, Read};
use std::path::Path;
use std::sync::Arc;
use std::{num::NonZeroU64, path::PathBuf};

use rustc_const_eval::interpret::AllocId;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def_id::{CrateNum, DefId, DefIndex};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_serialize::{
    opaque::{FileEncoder, MemDecoder},
    Decodable, Decoder, Encodable, Encoder,
};
use rustc_span::{
    AttrId, BytePos, ExpnId, FileName, RealFileName, SourceFile, Span, SpanData, SpanDecoder,
    SpanEncoder, Symbol, SyntaxContext, DUMMY_SP,
};
use rustc_type_ir::{PredicateKind, TyDecoder, TyEncoder};

macro_rules! encoder_methods {
    ($($name:ident($ty:ty);)*) => {
        $(fn $name(&mut self, value: $ty) {
            self.file_encoder.$name(value)
        })*
    }
}

/// A structure that implements `TyEncoder` for us.
pub struct ParalegalEncoder<'tcx> {
    tcx: TyCtxt<'tcx>,
    file_encoder: FileEncoder,
    type_shorthands: FxHashMap<ty::Ty<'tcx>, usize>,
    predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>,
    filepath_shorthands: FxHashMap<FileName, usize>,
}

impl<'tcx> ParalegalEncoder<'tcx> {
    /// Create a new encoder that will write to the provided file.
    pub fn new(path: impl AsRef<Path>, tcx: TyCtxt<'tcx>) -> Self {
        Self {
            tcx,
            file_encoder: FileEncoder::new(path).unwrap(),
            type_shorthands: Default::default(),
            predicate_shorthands: Default::default(),
            filepath_shorthands: Default::default(),
        }
    }

    pub fn finish(mut self) {
        self.file_encoder.finish().unwrap();
    }
}

/// Convenience function that encodes some value to a file.
pub fn encode_to_file<'tcx, V: Encodable<ParalegalEncoder<'tcx>>>(
    tcx: TyCtxt<'tcx>,
    path: impl AsRef<Path>,
    v: &V,
) {
    let mut encoder = ParalegalEncoder::new(path, tcx);
    v.encode(&mut encoder);
    encoder.finish();
}

/// Whatever can't survive the crossing we need to live without.
const CLEAR_CROSS_CRATE: bool = true;

impl Encoder for ParalegalEncoder<'_> {
    encoder_methods! {
        emit_usize(usize);
        emit_u128(u128);
        emit_u64(u64);
        emit_u32(u32);
        emit_u16(u16);
        emit_u8(u8);

        emit_isize(isize);
        emit_i128(i128);
        emit_i64(i64);
        emit_i32(i32);
        emit_i16(i16);

        emit_raw_bytes(&[u8]);
    }
}

impl<'tcx> TyEncoder for ParalegalEncoder<'tcx> {
    type I = TyCtxt<'tcx>;
    const CLEAR_CROSS_CRATE: bool = CLEAR_CROSS_CRATE;

    fn position(&self) -> usize {
        self.file_encoder.position()
    }

    fn type_shorthands(
        &mut self,
    ) -> &mut FxHashMap<<Self::I as rustc_type_ir::Interner>::Ty, usize> {
        &mut self.type_shorthands
    }

    fn predicate_shorthands(&mut self) -> &mut FxHashMap<PredicateKind<Self::I>, usize> {
        &mut self.predicate_shorthands
    }

    fn encode_alloc_id(&mut self, alloc_id: &<Self::I as rustc_type_ir::Interner>::AllocId) {
        u64::from(alloc_id.0).encode(self)
    }
}

/// Something that implements `TyDecoder`.
pub struct ParalegalDecoder<'tcx, 'a> {
    tcx: TyCtxt<'tcx>,
    mem_decoder: MemDecoder<'a>,
    shorthand_map: FxHashMap<usize, Ty<'tcx>>,
    file_shorthands: FxHashMap<usize, Arc<SourceFile>>,
}

impl<'tcx, 'a> ParalegalDecoder<'tcx, 'a> {
    /// Decode what is in this buffer.
    pub fn new(tcx: TyCtxt<'tcx>, buf: &'a [u8]) -> Self {
        Self {
            tcx,
            mem_decoder: MemDecoder::new(buf, 0).unwrap(),
            shorthand_map: Default::default(),
            file_shorthands: Default::default(),
        }
    }
}

/// Convenience function that decodes a value from a file.
pub fn decode_from_file<'tcx, V: for<'a> Decodable<ParalegalDecoder<'tcx, 'a>>>(
    tcx: TyCtxt<'tcx>,
    path: impl AsRef<Path>,
) -> io::Result<V> {
    let mut file = File::open(path)?;
    let mut buf = Vec::new();
    file.read_to_end(&mut buf)?;
    let mut decoder = ParalegalDecoder::new(tcx, buf.as_slice());
    Ok(V::decode(&mut decoder))
}

impl<'tcx> TyDecoder for ParalegalDecoder<'tcx, '_> {
    const CLEAR_CROSS_CRATE: bool = CLEAR_CROSS_CRATE;

    type I = TyCtxt<'tcx>;

    fn interner(&self) -> Self::I {
        self.tcx
    }

    fn cached_ty_for_shorthand<F>(
        &mut self,
        shorthand: usize,
        or_insert_with: F,
    ) -> <Self::I as ty::Interner>::Ty
    where
        F: FnOnce(&mut Self) -> <Self::I as ty::Interner>::Ty,
    {
        if let Some(ty) = self.shorthand_map.get(&shorthand) {
            return *ty;
        }
        let ty = or_insert_with(self);
        self.shorthand_map.insert(shorthand, ty);
        ty
    }

    fn decode_alloc_id(&mut self) -> <Self::I as ty::Interner>::AllocId {
        AllocId(NonZeroU64::new(u64::decode(self)).unwrap())
    }

    fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
    where
        F: FnOnce(&mut Self) -> R,
    {
        debug_assert!(pos < self.mem_decoder.len());

        let new_opaque = self.mem_decoder.split_at(pos);
        let old_opaque = std::mem::replace(&mut self.mem_decoder, new_opaque);
        let r = f(self);
        self.mem_decoder = old_opaque;
        r
    }
}

macro_rules! decoder_methods {
    ($($name:ident($ty:ty);)*) => {
        $(fn $name(&mut self) -> $ty {
            self.mem_decoder.$name()
        })*
    }
}

impl Decoder for ParalegalDecoder<'_, '_> {
    decoder_methods! {
        read_usize(usize);
        read_u128(u128);
        read_u64(u64);
        read_u32(u32);
        read_u16(u16);
        read_u8(u8);
        read_isize(isize);
        read_i128(i128);
        read_i64(i64);
        read_i32(i32);
        read_i16(i16);
    }
    fn position(&self) -> usize {
        self.mem_decoder.position()
    }
    fn peek_byte(&self) -> u8 {
        self.mem_decoder.peek_byte()
    }
    fn read_raw_bytes(&mut self, len: usize) -> &[u8] {
        self.mem_decoder.read_raw_bytes(len)
    }
}

impl SpanDecoder for ParalegalDecoder<'_, '_> {
    fn decode_crate_num(&mut self) -> CrateNum {
        self.tcx
            .stable_crate_id_to_crate_num(Decodable::decode(self))
    }
    fn decode_def_index(&mut self) -> DefIndex {
        DefIndex::from_u32(u32::decode(self))
    }
    fn decode_span(&mut self) -> Span {
        SpanData::decode(self).span()
    }

    fn decode_syntax_context(&mut self) -> SyntaxContext {
        SyntaxContext::root()
    }

    fn decode_def_id(&mut self) -> DefId {
        DefId {
            krate: self.decode_crate_num(),
            index: self.decode_def_index(),
        }
    }

    fn decode_expn_id(&mut self) -> ExpnId {
        unimplemented!()
    }

    fn decode_symbol(&mut self) -> Symbol {
        Symbol::intern(&String::decode(self))
    }

    fn decode_attr_id(&mut self) -> AttrId {
        unimplemented!()
    }
}

impl SpanEncoder for ParalegalEncoder<'_> {
    fn encode_def_index(&mut self, def_index: DefIndex) {
        def_index.as_u32().encode(self)
    }
    fn encode_span(&mut self, span: Span) {
        span.data().encode(self)
    }
    fn encode_syntax_context(&mut self, _syntax_context: SyntaxContext) {}

    fn encode_crate_num(&mut self, crate_num: CrateNum) {
        self.tcx.stable_crate_id(crate_num).encode(self)
    }
    fn encode_def_id(&mut self, def_id: DefId) {
        self.encode_crate_num(def_id.krate);
        self.encode_def_index(def_id.index);
    }
    fn encode_expn_id(&mut self, _expn_id: ExpnId) {
        unimplemented!()
    }
    fn encode_symbol(&mut self, symbol: Symbol) {
        symbol.as_str().encode(self)
    }
}

const TAG_PARTIAL_SPAN: u8 = 0;
const TAG_VALID_SPAN_FULL: u8 = 1;

/// Some of this code is lifted from `EncodeContext`.
///
/// However we directly encode file names because that's easier.
impl<'tcx> Encodable<ParalegalEncoder<'tcx>> for SpanData {
    fn encode(&self, s: &mut ParalegalEncoder<'tcx>) {
        if self.is_dummy() {
            return TAG_PARTIAL_SPAN.encode(s);
        }
        let source_map = s.tcx.sess.source_map();
        let source_file = source_map.lookup_source_file(self.lo);
        if !source_file.contains(self.hi) {
            // Unfortunately, macro expansion still sometimes generates Spans
            // that malformed in this way.
            return TAG_PARTIAL_SPAN.encode(s);
        }
        TAG_VALID_SPAN_FULL.encode(s);
        source_file.cnum.encode(s);
        let mut name = source_file.name.clone();
        if let FileName::Real(RealFileName::Remapped { local_path, .. }) = &mut name {
            local_path.take();
        }
        s.encode_file_name(&name);

        let lo = self.lo - source_file.start_pos;
        let len = self.hi - self.lo;
        lo.encode(s);
        len.encode(s);
    }
}

impl ParalegalEncoder<'_> {
    fn encode_file_name(&mut self, n: &FileName) {
        if let Some(&idx) = self.filepath_shorthands.get(n) {
            TAG_ENCODE_REMOTE.encode(self);
            idx.encode(self);
        } else {
            TAG_ENCODE_LOCAL.encode(self);
            self.filepath_shorthands
                .insert(n.clone(), self.file_encoder.position());
            n.encode(self);
        }
    }
}

impl ParalegalDecoder<'_, '_> {
    fn decode_file_name(&mut self, crate_num: CrateNum) -> Arc<SourceFile> {
        let tag = u8::decode(self);
        let pos = if tag == TAG_ENCODE_REMOTE {
            let index = usize::decode(self);
            if let Some(cached) = self.file_shorthands.get(&index) {
                return cached.clone();
            }
            Some(index)
        } else if tag == TAG_ENCODE_LOCAL {
            None
        } else {
            panic!("Unexpected tag value {tag}");
        };
        let (index, file) = if let Some(idx) = pos {
            (
                idx,
                self.with_position(idx, |slf| slf.decode_filename_local(crate_num)),
            )
        } else {
            (self.position(), self.decode_filename_local(crate_num))
        };

        self.file_shorthands.insert(index, file.clone());
        file
    }

    fn decode_filename_local(&mut self, crate_num: CrateNum) -> Arc<SourceFile> {
        let file_name = FileName::decode(self);
        let source_map = self.tcx.sess.source_map();
        let matching_source_files = source_map
            .files()
            .iter()
            .filter(|f| {
                f.cnum == crate_num && (file_name == f.name || matches!((&file_name, &f.name), (FileName::Real(r), FileName::Real(other)) if {
                    let before = path_in_real_path(r);
                    let after = path_in_real_path(other);
                    after.ends_with(before)
                }))
            })
            .cloned()
            .collect::<Box<[_]>>();
        match matching_source_files.as_ref() {
            [sf] => sf.clone(),
            [] => match &file_name {
                FileName::Real(RealFileName::LocalPath(local)) if source_map.file_exists(local) => {
                    source_map.load_file(local).unwrap()
                }
                _ => panic!("Could not load file {}", file_name.prefer_local()),
            },
            other => {
                let names = other.iter().map(|f| &f.name).collect::<Vec<_>>();
                panic!("Too many matching file names for {file_name:?}: {names:?}")
            }
        }
    }
}

const TAG_ENCODE_REMOTE: u8 = 0;
const TAG_ENCODE_LOCAL: u8 = 1;

/// Which path in a [`RealFileName`] do we care about?
fn path_in_real_path(r: &RealFileName) -> &PathBuf {
    match r {
        RealFileName::LocalPath(p) => p,
        RealFileName::Remapped { virtual_name, .. } => virtual_name,
    }
}

/// Partially uses code similar to `DecodeContext`. But we fully encode file
/// names. We then map them back by searching the currently loaded files. If the
/// file we care about is not found there, we try and load its source.
impl<'tcx, 'a> Decodable<ParalegalDecoder<'tcx, 'a>> for SpanData {
    fn decode(d: &mut ParalegalDecoder<'tcx, 'a>) -> Self {
        let ctxt = SyntaxContext::decode(d);
        let tag = u8::decode(d);
        if tag == TAG_PARTIAL_SPAN {
            return DUMMY_SP.with_ctxt(ctxt).data();
        }
        debug_assert_eq!(tag, TAG_VALID_SPAN_FULL);
        let crate_num = CrateNum::decode(d);
        let source_file = d.decode_file_name(crate_num);
        let lo = BytePos::decode(d);
        let len = BytePos::decode(d);
        let hi = lo + len;
        let lo = source_file.start_pos + lo;
        let hi = source_file.start_pos + hi;
        assert!(source_file.contains(lo));
        assert!(source_file.contains(hi));
        SpanData {
            lo,
            hi,
            ctxt,
            parent: None,
        }
    }
}