about summary refs log tree commit diff
path: root/src/tools/miri/tests/pass/const-addrs.rs
blob: 0d1531c73cd0b53b2f392d3025453ce273b16b77 (plain)
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
// The interpreter used to create a new AllocId every time it evaluates any const.
// This caused unbounded memory use in Miri.
// This test verifies that we only create a bounded amount of addresses for any given const.
// In practice, the interpreter always returns the same address, but we *do not guarantee* that.
//@compile-flags: -Zinline-mir=no

const EVALS: usize = 64;

use std::collections::HashSet;
fn main() {
    let mut addrs = HashSet::new();
    for _ in 0..EVALS {
        addrs.insert(const_addr());
    }
    // Check that we always return the same base address for the const allocation.
    assert_eq!(addrs.len(), 1);

    // Check that within a call we always produce the same address
    let mut prev = 0;
    for iter in 0..EVALS {
        let addr = "test".as_bytes().as_ptr().addr();
        if iter > 0 {
            assert_eq!(prev, addr);
        }
        prev = addr;
    }
}

fn const_addr() -> usize {
    "test".as_bytes().as_ptr().addr()
}