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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// Copyright (c) The Libra Core Contributors
// SPDX-License-Identifier: Apache-2.0

use proptest::{
    prelude::*,
    sample::{self, Index as PropIndex},
};
use proptest_helpers::pick_slice_idxs;
use std::collections::BTreeMap;
use vm::{
    errors::{VMStaticViolation, VerificationError},
    file_format::{
        AddressPoolIndex, CompiledModule, CompiledModuleMut, FieldDefinitionIndex,
        FunctionHandleIndex, FunctionSignatureIndex, LocalsSignatureIndex, ModuleHandleIndex,
        StringPoolIndex, StructHandleIndex, TableIndex, TypeSignatureIndex,
    },
    internals::ModuleIndex,
    views::{ModuleView, SignatureTokenView},
    IndexKind,
};

mod code_unit;
pub use code_unit::{ApplyCodeUnitBoundsContext, CodeUnitBoundsMutation};

/// Represents the number of pointers that exist out from a node of a particular kind.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum PointerKind {
    /// Exactly one pointer out with this index kind as its destination.
    One(IndexKind),
    /// Zero or one pointer out with this index kind as its destination. Like the `?` operator in
    /// regular expressions.
    Optional(IndexKind),
    /// Zero or more pointers out with this index kind as its destination. Like the `*` operator
    /// in regular expressions.
    Star(IndexKind),
}

impl PointerKind {
    /// A list of what pointers (indexes) exist out from a particular kind of node within the
    /// module.
    ///
    /// The only special case is `FunctionDefinition`, which contains a `CodeUnit` that can contain
    /// one of several kinds of pointers out. That is not represented in this table.
    #[inline]
    pub fn pointers_from(src_kind: IndexKind) -> &'static [PointerKind] {
        use IndexKind::*;
        use PointerKind::*;

        match src_kind {
            ModuleHandle => &[One(AddressPool), One(StringPool)],
            StructHandle => &[One(ModuleHandle), One(StringPool)],
            FunctionHandle => &[One(ModuleHandle), One(StringPool), One(FunctionSignature)],
            StructDefinition => &[One(StructHandle), One(FieldDefinition)],
            FieldDefinition => &[One(StructHandle), One(StringPool), One(TypeSignature)],
            FunctionDefinition => &[One(FunctionHandle), One(LocalsSignature)],
            TypeSignature => &[Optional(StructHandle)],
            FunctionSignature => &[Star(StructHandle)],
            LocalsSignature => &[Star(StructHandle)],
            StringPool => &[],
            ByteArrayPool => &[],
            AddressPool => &[],
            // LocalPool and CodeDefinition are function-local, and this only works for
            // module-scoped indexes.
            // XXX maybe don't treat LocalPool and CodeDefinition the same way as the others?
            LocalPool => &[],
            CodeDefinition => &[],
        }
    }

    #[inline]
    pub fn to_index_kind(self) -> IndexKind {
        match self {
            PointerKind::One(idx) | PointerKind::Optional(idx) | PointerKind::Star(idx) => idx,
        }
    }
}

pub static VALID_POINTER_SRCS: &[IndexKind] = &[
    IndexKind::ModuleHandle,
    IndexKind::StructHandle,
    IndexKind::FunctionHandle,
    IndexKind::StructDefinition,
    IndexKind::FieldDefinition,
    IndexKind::FunctionDefinition,
    IndexKind::TypeSignature,
    IndexKind::FunctionSignature,
    IndexKind::LocalsSignature,
];

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn pointer_kind_sanity() {
        for variant in IndexKind::variants() {
            if VALID_POINTER_SRCS.iter().any(|x| x == variant) {
                assert!(
                    !PointerKind::pointers_from(*variant).is_empty(),
                    "expected variant {:?} to be a valid pointer source",
                    variant,
                );
            } else {
                assert!(
                    PointerKind::pointers_from(*variant).is_empty(),
                    "expected variant {:?} to not be a valid pointer source",
                    variant,
                );
            }
        }
    }
}

/// Represents a single mutation to a `CompiledModule` to produce an out-of-bounds situation.
///
/// Use `OutOfBoundsMutation::strategy()` to generate them, preferably using `Vec` to generate
/// many at a time. Then use `ApplyOutOfBoundsContext` to apply those mutations.
#[derive(Debug)]
pub struct OutOfBoundsMutation {
    src_kind: IndexKind,
    src_idx: PropIndex,
    dst_kind: IndexKind,
    offset: usize,
}

impl OutOfBoundsMutation {
    pub fn strategy() -> impl Strategy<Value = Self> {
        (
            Self::src_kind_strategy(),
            any::<PropIndex>(),
            any::<PropIndex>(),
            0..16 as usize,
        )
            .prop_map(|(src_kind, src_idx, dst_kind_idx, offset)| {
                let dst_kind = Self::dst_kind(src_kind, dst_kind_idx);
                Self {
                    src_kind,
                    src_idx,
                    dst_kind,
                    offset,
                }
            })
    }

    // Not all source kinds can be made to be out of bounds (e.g. inherent types can't.)
    fn src_kind_strategy() -> impl Strategy<Value = IndexKind> {
        sample::select(VALID_POINTER_SRCS)
    }

    fn dst_kind(src_kind: IndexKind, dst_kind_idx: PropIndex) -> IndexKind {
        dst_kind_idx
            .get(PointerKind::pointers_from(src_kind))
            .to_index_kind()
    }
}

/// This is used for source indexing, to work with pick_slice_idxs.
impl AsRef<PropIndex> for OutOfBoundsMutation {
    #[inline]
    fn as_ref(&self) -> &PropIndex {
        &self.src_idx
    }
}

pub struct ApplyOutOfBoundsContext {
    module: CompiledModuleMut,
    // This is an Option because it gets moved out in apply before apply_one is called. Rust
    // doesn't let you call another con-consuming method after a partial move out.
    mutations: Option<Vec<OutOfBoundsMutation>>,

    // Some precomputations done for signatures.
    type_sig_structs: Vec<TypeSignatureIndex>,
    function_sig_structs: Vec<FunctionSignatureTokenIndex>,
    locals_sig_structs: Vec<(LocalsSignatureIndex, usize)>,
}

impl ApplyOutOfBoundsContext {
    pub fn new(module: CompiledModule, mutations: Vec<OutOfBoundsMutation>) -> Self {
        let type_sig_structs: Vec<_> = Self::type_sig_structs(&module).collect();
        let function_sig_structs: Vec<_> = Self::function_sig_structs(&module).collect();
        let locals_sig_structs: Vec<_> = Self::locals_sig_structs(&module).collect();

        Self {
            module: module.into_inner(),
            mutations: Some(mutations),
            type_sig_structs,
            function_sig_structs,
            locals_sig_structs,
        }
    }

    pub fn apply(mut self) -> (CompiledModuleMut, Vec<VerificationError>) {
        // This is a map from (source kind, dest kind) to the actual mutations -- this is done to
        // figure out how many mutations to do for a particular pair, which is required for
        // pick_slice_idxs below.
        let mut mutation_map = BTreeMap::new();
        for mutation in self
            .mutations
            .take()
            .expect("mutations should always be present")
        {
            mutation_map
                .entry((mutation.src_kind, mutation.dst_kind))
                .or_insert_with(|| vec![])
                .push(mutation);
        }

        let mut results = vec![];

        for ((src_kind, dst_kind), mutations) in mutation_map {
            // It would be cool to use an iterator here, if someone could figure out exactly how
            // to get the lifetimes right :)
            results.extend(self.apply_one(src_kind, dst_kind, mutations));
        }
        (self.module, results)
    }

    fn apply_one(
        &mut self,
        src_kind: IndexKind,
        dst_kind: IndexKind,
        mutations: Vec<OutOfBoundsMutation>,
    ) -> Vec<VerificationError> {
        let src_count = match src_kind {
            // Only the signature indexes that have structs in them (i.e. are in *_sig_structs)
            // are going to be modifiable, so pick among them.
            IndexKind::TypeSignature => self.type_sig_structs.len(),
            IndexKind::FunctionSignature => self.function_sig_structs.len(),
            IndexKind::LocalsSignature => self.locals_sig_structs.len(),
            // For the other sorts it's always possible to change an index.
            src_kind => self.module.kind_count(src_kind),
        };
        // Any signature can be a destination, not just the ones that have structs in them.
        let dst_count = self.module.kind_count(dst_kind);
        let to_mutate = pick_slice_idxs(src_count, &mutations);

        mutations
            .iter()
            .zip(to_mutate)
            .map(move |(mutation, src_idx)| {
                self.set_index(
                    src_kind,
                    src_idx,
                    dst_kind,
                    dst_count,
                    (dst_count + mutation.offset) as TableIndex,
                )
            })
            .collect()
    }

    /// Sets the particular index in the table
    ///
    /// For example, with `src_kind` set to `ModuleHandle` and `dst_kind` set to `AddressPool`,
    /// this will set self.module_handles[src_idx].address to new_idx.
    ///
    /// This is mainly used for test generation.
    fn set_index(
        &mut self,
        src_kind: IndexKind,
        src_idx: usize,
        dst_kind: IndexKind,
        dst_count: usize,
        new_idx: TableIndex,
    ) -> VerificationError {
        use IndexKind::*;

        // These are default values, but some of the match arms below mutate them.
        let mut src_idx = src_idx;
        let mut err = VMStaticViolation::IndexOutOfBounds(dst_kind, dst_count, new_idx as usize);

        // A dynamic type system would be able to express this next block of code far more
        // concisely. A static type system would require some sort of complicated dependent type
        // structure that Rust doesn't have. As things stand today, every possible case needs to
        // be listed out.

        match (src_kind, dst_kind) {
            (ModuleHandle, AddressPool) => {
                self.module.module_handles[src_idx].address = AddressPoolIndex::new(new_idx);
            }
            (ModuleHandle, StringPool) => {
                self.module.module_handles[src_idx].name = StringPoolIndex::new(new_idx)
            }
            (StructHandle, ModuleHandle) => {
                self.module.struct_handles[src_idx].module = ModuleHandleIndex::new(new_idx)
            }
            (StructHandle, StringPool) => {
                self.module.struct_handles[src_idx].name = StringPoolIndex::new(new_idx)
            }
            (FunctionHandle, ModuleHandle) => {
                self.module.function_handles[src_idx].module = ModuleHandleIndex::new(new_idx)
            }
            (FunctionHandle, StringPool) => {
                self.module.function_handles[src_idx].name = StringPoolIndex::new(new_idx)
            }
            (FunctionHandle, FunctionSignature) => {
                self.module.function_handles[src_idx].signature =
                    FunctionSignatureIndex::new(new_idx)
            }
            (StructDefinition, StructHandle) => {
                self.module.struct_defs[src_idx].struct_handle = StructHandleIndex::new(new_idx)
            }
            (StructDefinition, FieldDefinition) => {
                // Consider a situation with 3 fields, and with first field = 1 and count = 2.
                // A graphical representation of that might be:
                //
                //      |___|___|___|
                //  idx   0   1   2
                //          ^       ^
                //          |       |
                // first field = 1  (first field + count) = 3
                //
                // Given that the lowest value for new_idx is 3 (offset 0), the goal is to make
                // (first field + count) at least 4, or (new_idx + 1). This means that the first
                // field would be new_idx + 1 - count.
                let end_idx = new_idx + 1;
                let first_new_idx = end_idx - self.module.struct_defs[src_idx].field_count;
                self.module.struct_defs[src_idx].fields = FieldDefinitionIndex::new(first_new_idx);
                err = VMStaticViolation::RangeOutOfBounds(
                    dst_kind,
                    dst_count,
                    first_new_idx as usize,
                    end_idx as usize,
                );
            }
            (FieldDefinition, StructHandle) => {
                self.module.field_defs[src_idx].struct_ = StructHandleIndex::new(new_idx)
            }
            (FieldDefinition, StringPool) => {
                self.module.field_defs[src_idx].name = StringPoolIndex::new(new_idx)
            }
            (FieldDefinition, TypeSignature) => {
                self.module.field_defs[src_idx].signature = TypeSignatureIndex::new(new_idx)
            }
            (FunctionDefinition, FunctionHandle) => {
                self.module.function_defs[src_idx].function = FunctionHandleIndex::new(new_idx)
            }
            (FunctionDefinition, LocalsSignature) => {
                self.module.function_defs[src_idx].code.locals = LocalsSignatureIndex::new(new_idx)
            }
            (TypeSignature, StructHandle) => {
                // For this and the other signatures, the source index will be picked from
                // only the ones that have struct handles in them.
                src_idx = self.type_sig_structs[src_idx].into_index();
                self.module.type_signatures[src_idx]
                    .0
                    .debug_set_sh_idx(StructHandleIndex::new(new_idx));
            }
            (FunctionSignature, StructHandle) => match &self.function_sig_structs[src_idx] {
                FunctionSignatureTokenIndex::ReturnType(actual_src_idx, ret_idx) => {
                    src_idx = actual_src_idx.into_index();
                    self.module.function_signatures[src_idx].return_types[*ret_idx]
                        .debug_set_sh_idx(StructHandleIndex::new(new_idx));
                }
                FunctionSignatureTokenIndex::ArgType(actual_src_idx, arg_idx) => {
                    src_idx = actual_src_idx.into_index();
                    self.module.function_signatures[src_idx].arg_types[*arg_idx]
                        .debug_set_sh_idx(StructHandleIndex::new(new_idx));
                }
            },
            (LocalsSignature, StructHandle) => {
                let (actual_src_idx, arg_idx) = self.locals_sig_structs[src_idx];
                src_idx = actual_src_idx.into_index();
                self.module.locals_signatures[src_idx].0[arg_idx]
                    .debug_set_sh_idx(StructHandleIndex::new(new_idx));
            }
            _ => panic!("Invalid pointer kind: {:?} -> {:?}", src_kind, dst_kind),
        }

        VerificationError {
            kind: src_kind,
            idx: src_idx,
            err,
        }
    }

    /// Returns the indexes of type signatures that contain struct handles inside them.
    fn type_sig_structs<'b>(
        module: &'b CompiledModule,
    ) -> impl Iterator<Item = TypeSignatureIndex> + 'b {
        let module_view = ModuleView::new(module);
        module_view
            .type_signatures()
            .enumerate()
            .filter_map(|(idx, signature)| {
                signature
                    .token()
                    .struct_handle()
                    .map(|_| TypeSignatureIndex::new(idx as u16))
            })
    }

    /// Returns the indexes of function signatures that contain struct handles inside them.
    fn function_sig_structs<'b>(
        module: &'b CompiledModule,
    ) -> impl Iterator<Item = FunctionSignatureTokenIndex> + 'b {
        let module_view = ModuleView::new(module);
        let return_tokens = module_view
            .function_signatures()
            .enumerate()
            .map(|(idx, signature)| {
                let idx = FunctionSignatureIndex::new(idx as u16);
                Self::find_struct_tokens(signature.return_tokens(), move |arg_idx| {
                    FunctionSignatureTokenIndex::ReturnType(idx, arg_idx)
                })
            })
            .flatten();
        let arg_tokens = module_view
            .function_signatures()
            .enumerate()
            .map(|(idx, signature)| {
                let idx = FunctionSignatureIndex::new(idx as u16);
                Self::find_struct_tokens(signature.arg_tokens(), move |arg_idx| {
                    FunctionSignatureTokenIndex::ArgType(idx, arg_idx)
                })
            })
            .flatten();
        return_tokens.chain(arg_tokens)
    }

    /// Returns the indexes of locals signatures that contain struct handles inside them.
    fn locals_sig_structs<'b>(
        module: &'b CompiledModule,
    ) -> impl Iterator<Item = (LocalsSignatureIndex, usize)> + 'b {
        let module_view = ModuleView::new(module);
        module_view
            .locals_signatures()
            .enumerate()
            .map(|(idx, signature)| {
                let idx = LocalsSignatureIndex::new(idx as u16);
                Self::find_struct_tokens(signature.tokens(), move |arg_idx| (idx, arg_idx))
            })
            .flatten()
    }

    #[inline]
    fn find_struct_tokens<'b, F, T>(
        tokens: impl IntoIterator<Item = SignatureTokenView<'b, CompiledModule>> + 'b,
        map_fn: F,
    ) -> impl Iterator<Item = T> + 'b
    where
        F: Fn(usize) -> T + 'b,
    {
        tokens
            .into_iter()
            .enumerate()
            .filter_map(move |(arg_idx, token)| token.struct_handle().map(|_| map_fn(arg_idx)))
    }
}

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
enum FunctionSignatureTokenIndex {
    ReturnType(FunctionSignatureIndex, usize),
    ArgType(FunctionSignatureIndex, usize),
}