forked from PLSysSec/veriwasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheaplattice.rs
665 lines (636 loc) · 24.7 KB
/
heaplattice.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
use crate::lattices::{ConstLattice, VariableState};
use crate::VMCtxField;
use std::fmt::Debug;
use std::ops::Range;
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum HeapValue {
/// The vmctx pointer.
VMCtx,
/// A value loaded from a given location.
Load(Box<HeapValue>),
/// An offset from a given location. Cannot wrap: we go to the
/// lattice's Bot value if we cannot prove this when analyzing
/// range.
Add(Box<HeapValue>, Box<HeapValue>),
/// A multiplication (scaling) operation. Cannot wrap: we go to
/// the lattice's Bot value if we cannot prove this when analyzing
/// range.
Scale(Box<HeapValue>, u8),
/// A single known constant value.
Const(i64),
/// A pointer to somewhere in the .text segment.
TextPointer,
/// An unsigned min (clamping) operation.
UMin(Box<HeapValue>, Box<HeapValue>),
/// Some unknown value. Note that this is distinguished from Bot
/// because it is a definite, concrete value, rather than a
/// potential conflict.
Unknown,
}
/// A tag for a bound value. Corresponds to the field-spec index in
/// the `HeapStrategy::VMCtx`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Tag(pub usize);
pub type HeapValueLattice = ConstLattice<HeapValue>;
pub type HeapLattice = VariableState<HeapValueLattice>;
impl HeapValue {
pub fn imm(value: i64) -> Self {
Self::Const(value)
}
/// Validate that expression is in "normal form":
///
/// ```plain
/// - Atom := VMCtx | Load(_) | Const(_) | TextPointer | Unknown
/// - AtomSummand := Scale(Atom, _) | Atom
/// - AtomSum := Add(AtomSum, AtomSum) | AtomSummand
/// - Min := UMin(AtomSum, AtomSum)
///
/// - Normalized := Min | AtomSum
/// ```
pub fn is_normalized(&self) -> bool {
match self {
Self::UMin(a, b) => a.is_atom_sum() && b.is_atom_sum(),
x => x.is_atom_sum(),
}
}
fn is_atom_sum(&self) -> bool {
match self {
Self::Add(a, b) => a.is_atom_sum() && b.is_atom_sum(),
x => x.is_atom_summand(),
}
}
fn is_atom_summand(&self) -> bool {
match self {
Self::Scale(a, _) => a.is_atom(),
x => x.is_atom(),
}
}
fn is_atom(&self) -> bool {
match self {
Self::VMCtx | Self::Load(_) | Self::Const(_) | Self::TextPointer | Self::Unknown => {
true
}
_ => false,
}
}
/// Simplify an expression to normal form.
///
/// - Add-no-wrap distributes over UMin
/// - `simplify(Add(UMin(a, b), c))` ->
/// `UMin(simplify(Add(a, c)), simplify(Add(b, c)))`
/// - Scale-no-wrap distributes over UMin:
/// - `simplify(Scale(UMin(a, b), k))` ->
/// `UMin(simplify(Scale(a, k)), simplify(Scale(b, k)))`
/// - Scale distributes over Add:
/// - `simplify(Scale(Add(a, b), k))` ->
/// `Add(simplify(Scale(a, k)), simplify(Scale(b, k)))`
/// - `Add` and `Scale` can do constant folding with `Const`.
/// - also, push constants to the right otherwise, and
/// reassociate constants outward.
/// - `Add(Const(a), Const(b))` -> `Const(a + b)`
/// - `Add(Const(a), b)` -> `Add(b, Const(a))`
/// - `Add(Add(a, Const(b)), Const(c))` -> `Add(a, Const(b + c))`
/// - `Add(Add(a, Const(b)), c)` -> `Add(Add(a, c), Const(b))`
/// - `Scale(Const(a), k)` -> `Const(a * k)`
/// - `Load` simplifies its inner expression as well.
/// - UMin can do some constant-folding as well, and reassociates
/// just as `Add` does:
/// - `UMin(Const(a), Const(b))` -> `Const(min(a, b))`
/// - `UMin(Const(a), b)` -> `UMin(b, Const(a))`
/// - `UMin(UMin(a, Const(b)), Const(c))` -> `UMin(a, Const(min(b, c)))`
/// - `UMin(UMin(a, Const(b)), c)` -> `UMin(UMin(a, c), Const(b))`
/// - To avoid indefinite growth, `Load` truncates its expression
/// after two nested `Load`s.
pub fn simplify(self) -> Self {
fn umin(a: i64, b: i64) -> i64 {
std::cmp::min(a as u64, b as u64) as i64
}
match self {
Self::UMin(a, b) => {
let a = a.simplify();
let b = b.simplify();
match (a, b) {
// `UMin(Const(a), Const(b))` -> `Const(min(a, b))`
(Self::Const(a), Self::Const(b)) => Self::Const(umin(a, b)),
// `UMin(Const(a), b)` -> `UMin(b, Const(a))`
(Self::Const(a), b) => {
Self::UMin(Box::new(b), Box::new(Self::Const(a))).simplify()
}
(Self::UMin(a, b), c) | (c, Self::UMin(a, b)) => {
let a = a.simplify();
let b = b.simplify();
match (b, c) {
// `UMin(UMin(a, Const(b)), Const(c))` -> `UMin(a, Const(min(b, c)))`
(Self::Const(b), Self::Const(c)) => {
Self::UMin(Box::new(a), Box::new(Self::Const(umin(b, c))))
}
// `UMin(UMin(a, Const(b)), c)` -> `UMin(UMin(a, c), Const(b))`
(Self::Const(b), c) => Self::UMin(
Box::new(Self::UMin(Box::new(a), Box::new(c))),
Box::new(Self::Const(b)),
),
// `UMin(UMin(a, b), c)` -> `UMin(UMin(a, b), c)`
(b, c) => Self::UMin(
Box::new(Self::UMin(Box::new(a), Box::new(b))),
Box::new(c),
),
}
}
// `UMin(a, b)` -> `UMin(a, b)`
(a, b) => Self::UMin(Box::new(a), Box::new(b)),
}
}
Self::Add(a, b) => {
let a = a.simplify();
let b = b.simplify();
match (a, b) {
// `Add(Const(a), Const(b))` -> `Const(a + b)`
(Self::Const(a), Self::Const(b)) => Self::Const(a.wrapping_add(b)),
// `Add(Const(a), b)` -> `Add(b, Const(a))`
(Self::Const(a), b) => Self::Add(Box::new(b), Box::new(Self::Const(a))),
(Self::Add(a, b), c) | (c, Self::Add(a, b)) => {
let a = a.simplify();
let b = b.simplify();
match (b, c) {
// `Add(Add(a, Const(b)), Const(c))` -> `Add(a, Const(min(b, c)))`
(Self::Const(b), Self::Const(c)) => {
Self::Add(Box::new(a), Box::new(Self::Const(b.wrapping_add(c))))
}
// `Add(Add(a, Const(b)), c)` -> `Add(Add(a, c), Const(b))`
(Self::Const(b), c) => Self::Add(
Box::new(Self::Add(Box::new(a), Box::new(c))),
Box::new(Self::Const(b)),
),
// `Add(Add(a, b), c)` -> `Add(Add(a, b), c)`
(b, c) => Self::Add(
Box::new(Self::Add(Box::new(a), Box::new(b))),
Box::new(c),
),
}
}
// `Add(UMin(a, b), c)` -> `UMin(Add(a, c), Add(b, c))`
(Self::UMin(a, b), c) | (c, Self::UMin(a, b)) => {
let a = a.simplify();
let b = b.simplify();
Self::UMin(
Box::new(Self::Add(Box::new(a), Box::new(c.clone())).simplify()),
Box::new(Self::Add(Box::new(b), Box::new(c.clone())).simplify()),
)
}
// `Add(a, b)` -> `Add(a, b)`
(a, b) => Self::Add(Box::new(a), Box::new(b)),
}
}
Self::Scale(a, k) => {
let a = a.simplify();
match a {
// `Scale(UMin(a, b), k)` -> `UMin(Scale(a, k), Scale(b, k))`
Self::UMin(a, b) => Self::UMin(
Box::new(Self::Scale(a, k).simplify()),
Box::new(Self::Scale(b, k).simplify()),
),
// `Scale(Add(a, b), k)` -> `Add(Scale(a, k), Scale(b, k))`
Self::Add(a, b) => Self::Add(
Box::new(Self::Scale(a, k).simplify()),
Box::new(Self::Scale(b, k).simplify()),
),
// `Scale(Const(a), k)` -> `Const(a * k)`
Self::Const(a) => Self::Const(a.wrapping_mul(k as i64)),
a => Self::Scale(Box::new(a), k),
}
}
Self::Load(addr) => Self::Load(Box::new(addr.simplify())),
x => x,
}
}
pub fn add_offset(&self, off: i64) -> Self {
Self::Add(Box::new(self.clone()), Box::new(Self::Const(off))).simplify()
}
pub fn add(self, other: Self) -> Self {
log::trace!("add: {:?} + {:?}", self, other);
let result = Self::Add(Box::new(self), Box::new(other)).simplify();
log::trace!(" -> {:?}", result);
result
}
pub fn scale(self, scale: u8) -> Self {
Self::Scale(Box::new(self), scale).simplify()
}
pub fn load(self) -> Self {
Self::Load(Box::new(self))
}
/// Truncate depth of expression at a given number of loads.
pub fn truncate(self, max_load_depth: usize) -> Self {
if self.load_depth() <= max_load_depth {
self
} else {
match self {
Self::Load(_) if max_load_depth == 0 => Self::Unknown,
Self::Load(addr) => Self::Load(Box::new(addr.truncate(max_load_depth - 1))),
Self::Add(a, b) => Self::Add(
Box::new(a.truncate(max_load_depth)),
Box::new(b.truncate(max_load_depth)),
),
Self::Scale(a, k) => Self::Scale(Box::new(a.truncate(max_load_depth)), k),
Self::UMin(a, b) => Self::UMin(
Box::new(a.truncate(max_load_depth)),
Box::new(b.truncate(max_load_depth)),
),
Self::Const(_) | Self::TextPointer | Self::Unknown | Self::VMCtx => unreachable!(),
}
}
}
fn load_depth(&self) -> usize {
match self {
Self::VMCtx | Self::TextPointer | Self::Const(_) => 0,
Self::Scale(a, _) => a.load_depth(),
Self::Add(a, b) | Self::UMin(a, b) => std::cmp::max(a.load_depth(), b.load_depth()),
Self::Load(a) => a.load_depth() + 1,
Self::Unknown => 0,
}
}
pub fn clamp32(self) -> Self {
log::trace!("clamp32: {:?}", self);
let result = Self::UMin(Box::new(self), Box::new(Self::Const(u32::MAX as i64)));
log::trace!(" -> {:?}", result);
result
}
pub fn any32() -> Self {
Self::UMin(
Box::new(Self::Unknown),
Box::new(Self::Const(u32::MAX as i64)),
)
}
/// Determine a bound on a value: either a static bound, or a
/// symbolic bound based on the given bound variable.
pub fn find_bound<
T: Copy + Clone + Debug,
Base: Fn(&HeapValue) -> bool,
Bound: Fn(&HeapValue) -> Option<T>,
>(
&self,
base: &Base,
bound: &Bound,
) -> Bounded<T> {
if let Some(bound_token) = bound(self) {
return Bounded::Dynamic {
bound: bound_token,
scale: 1,
offset: 0,
has_base: false,
static_max: 0,
};
}
if base(self) {
return Bounded::Static {
has_base: true,
max: 0,
};
}
match self {
Self::VMCtx | Self::Unknown | Self::Load(_) => Bounded::None,
Self::TextPointer => Bounded::TextPointer,
Self::Const(val) => Bounded::Static {
has_base: false,
max: *val as u64,
},
Self::Add(a, b) => {
let a = a.find_bound(base, bound);
let b = b.find_bound(base, bound);
match (a, b) {
(
Bounded::Static {
has_base: has_base_a,
max: max_a,
},
Bounded::Static {
has_base: has_base_b,
max: max_b,
},
) => {
if has_base_a && has_base_b {
Bounded::None
} else {
if let Some(sum) = max_a.checked_add(max_b) {
Bounded::Static {
has_base: has_base_a || has_base_b,
max: sum,
}
} else {
Bounded::None
}
}
}
(
Bounded::Static {
has_base: has_base_a,
max,
},
Bounded::Dynamic {
has_base: has_base_b,
bound,
scale,
offset,
static_max,
},
)
| (
Bounded::Dynamic {
has_base: has_base_b,
bound,
scale,
offset,
static_max,
},
Bounded::Static {
has_base: has_base_a,
max,
},
) => {
if has_base_a && has_base_b {
Bounded::None
} else {
if let Some(sum) = max.checked_add(offset) {
let static_max = static_max.saturating_add(max);
Bounded::Dynamic {
has_base: has_base_a || has_base_b,
bound,
scale,
offset: sum,
static_max,
}
} else {
Bounded::None
}
}
}
(Bounded::Dynamic { .. }, Bounded::Dynamic { .. }) => Bounded::None,
(Bounded::TextPointer, _) | (_, Bounded::TextPointer) => Bounded::TextPointer,
(Bounded::None, _) | (_, Bounded::None) => Bounded::None,
}
}
Self::Scale(a, k) => {
let a = a.find_bound(base, bound);
match a {
Bounded::TextPointer | Bounded::None => Bounded::None,
Bounded::Static { has_base, max } => {
if has_base {
Bounded::None
} else {
if let Some(product) = max.checked_mul(*k as u64) {
Bounded::Static {
has_base: false,
max: product,
}
} else {
Bounded::None
}
}
}
Bounded::Dynamic {
has_base,
bound,
scale,
offset,
static_max,
} => {
if has_base {
Bounded::None
} else {
if let (Some(scale_product), Some(offset_product)) =
(scale.checked_mul(*k), offset.checked_mul(*k as u64))
{
let static_max = static_max.saturating_mul(*k as u64);
Bounded::Dynamic {
has_base: false,
bound,
scale: scale_product,
offset: offset_product,
static_max,
}
} else {
Bounded::None
}
}
}
}
}
Self::UMin(a, b) => {
let a = a.find_bound(base, bound);
let b = b.find_bound(base, bound);
log::trace!("find_bound: UMin ({:?}): a = {:?} b = {:?}", self, a, b);
match (a, b) {
(Bounded::TextPointer, _)
| (_, Bounded::TextPointer)
| (Bounded::None, _)
| (_, Bounded::None) => Bounded::None,
(
Bounded::Static {
has_base: has_base_a,
max: max_a,
},
Bounded::Static {
has_base: has_base_b,
max: max_b,
},
) => {
if has_base_a == has_base_b {
Bounded::Static {
has_base: has_base_a,
max: std::cmp::min(max_a, max_b),
}
} else {
Bounded::None
}
}
// We can take a bound on either `a` or `b` as the
// overall bound, with some potential loss of
// precision. Here we heuristically choose a
// dynamic bound over a static bound to propagate
// upward, and the first if both are dynamic.
(
Bounded::Dynamic {
has_base: has_base_a,
bound,
scale,
offset,
static_max,
},
other,
)
| (
other,
Bounded::Dynamic {
has_base: has_base_a,
bound,
scale,
offset,
static_max,
},
) => {
if other.has_base() == has_base_a {
Bounded::Dynamic {
has_base: has_base_a,
bound,
scale,
offset,
static_max: std::cmp::min(static_max, other.static_max()),
}
} else {
Bounded::None
}
}
}
}
}
}
/// Determine whether an access to an address described by this
/// HeapValue is safely within the given region.
pub fn addr_ok(&self, base: &HeapValue, bound: &HeapValue, access_size: usize) -> bool {
// Get a bound on our access.
let addr_bound = self.find_bound(&|val| val == base, &|val| {
if val == bound {
Some(())
} else {
None
}
});
log::debug!(
"base: {:?} bound: {:?} addr_bound: {:?}",
base,
bound,
addr_bound
);
match (addr_bound, bound) {
(Bounded::Static { has_base, max }, HeapValue::Const(bound))
if has_base && max < (*bound as u64) =>
{
true
}
(
Bounded::Dynamic {
has_base,
static_max,
..
},
HeapValue::Const(bound),
) if has_base && static_max < (*bound as u64) => true,
(
Bounded::Dynamic {
has_base,
bound,
scale,
offset,
..
},
_,
) if has_base && scale == 1 && offset == 0 => true,
_ => false,
}
}
}
#[derive(Clone, Debug)]
pub enum Bounded<T: Clone + Debug> {
Static {
has_base: bool,
max: u64,
},
Dynamic {
has_base: bool,
bound: T,
scale: u8,
offset: u64,
static_max: u64,
},
TextPointer,
None,
}
impl<T: Clone + Debug> Bounded<T> {
fn has_base(&self) -> bool {
match self {
Self::Static { has_base, .. } | Self::Dynamic { has_base, .. } => *has_base,
_ => false,
}
}
fn static_max(&self) -> u64 {
match self {
Self::Static { max, .. } => *max,
Self::Dynamic { static_max, .. } => *static_max,
_ => u64::MAX,
}
}
}
pub struct VMCtxFieldExprs {
pub base_bound: Vec<(HeapValue, HeapValue)>,
}
impl VMCtxFieldExprs {
fn field_to_base_bound(ctx: HeapValue, field: &VMCtxField) -> (HeapValue, HeapValue) {
match field {
VMCtxField::StaticRegion {
base_ptr_vmctx_offset,
heap_and_guard_size,
} => (
HeapValue::Load(Box::new(HeapValue::Add(
Box::new(ctx),
Box::new(HeapValue::Const(*base_ptr_vmctx_offset as i64)),
))),
HeapValue::Const(*heap_and_guard_size as i64),
),
VMCtxField::DynamicRegion {
base_ptr_vmctx_offset,
len_vmctx_offset,
element_size,
} => {
let len = HeapValue::Load(Box::new(HeapValue::Add(
Box::new(ctx.clone()),
Box::new(HeapValue::Const(*len_vmctx_offset as i64)),
)));
let len = if *element_size != 1 {
assert!(*element_size < 256);
HeapValue::Scale(Box::new(len), *element_size as u8)
} else {
len
};
(
HeapValue::Load(Box::new(HeapValue::Add(
Box::new(ctx),
Box::new(HeapValue::Const(*base_ptr_vmctx_offset as i64)),
))),
len,
)
}
VMCtxField::Field { offset, len } => {
let base = if *offset > 0 {
HeapValue::Add(Box::new(ctx), Box::new(HeapValue::Const(*offset as i64)))
} else {
ctx
};
let len = HeapValue::Const(*len as i64);
(base, len)
}
VMCtxField::Import {
ptr_vmctx_offset,
kind,
} => {
let ctx = HeapValue::Load(Box::new(HeapValue::Add(
Box::new(ctx),
Box::new(HeapValue::Const(*ptr_vmctx_offset as i64)),
)));
Self::field_to_base_bound(ctx, &*kind)
}
}
}
pub fn new(fields: &[VMCtxField], vmctx_size: usize) -> Self {
let mut base_bound = fields
.iter()
.map(|field| Self::field_to_base_bound(HeapValue::VMCtx, field))
.collect::<Vec<_>>();
base_bound.push(Self::field_to_base_bound(
HeapValue::VMCtx,
&VMCtxField::Field {
offset: 0,
len: vmctx_size,
},
));
Self { base_bound }
}
}