cverif 0.0.3

Verifies that compiled machine code faithfully matches its C source
Documentation
//! Encodings whose behaviour the architecture leaves UNPREDICTABLE.
//!
//! A real core may do anything on them — including something useful to whoever planted the
//! encoding — so the verifier must not give them a meaning of its own.  The executor refuses
//! every instruction this module flags.  The checks work on the raw encoding and follow the
//! ARMv7-M / ARMv8-M instruction descriptions; restrictions that ARMv8-M lifted (most uses of
//! SP as a general register) are not flagged.

/// Is the instruction (`hw2` is ignored for 16-bit encodings) unpredictable?
/// `itstate` is the IT state *before* the instruction: non-zero low nibble inside a block.
pub fn unpredictable(hw1: u32, hw2: u32, itstate: u8) -> bool {
    let in_it = itstate & 0xf != 0;
    let last = itstate & 0xf == 0x8;
    // Inside an IT block a change of control flow is only allowed in the last position.
    let mid_block = in_it && !last;
    if hw1 >> 11 < 0b11101 {
        narrow(hw1, in_it, mid_block)
    } else {
        wide(hw1, hw2, in_it, mid_block)
    }
}

fn narrow(w: u32, in_it: bool, mid_block: bool) -> bool {
    match w >> 11 {
        0b01000 if w & 0x400 != 0 => {
            let rm = (w >> 3) & 15;
            let rdn = (w & 7) | ((w >> 4) & 8);
            match (w >> 8) & 3 {
                // ADD: pc + pc; a jump in the middle of an IT block
                0 => rdn == 15 && (rm == 15 || mid_block),
                // CMP: both registers low, or the pc
                1 => rdn == 15 || rm == 15 || (rdn < 8 && rm < 8),
                2 => rdn == 15 && mid_block,
                // BX / BLX
                _ => w & 7 != 0 || mid_block || (w & 0x80 != 0 && rm == 15),
            }
        }
        // STMIA / LDMIA
        0b11000 => {
            let (n, list) = ((w >> 8) & 7, w & 0xff);
            list == 0 || (list >> n & 1 != 0 && list & ((1 << n) - 1) != 0)
        }
        0b11001 => w & 0xff == 0,
        0b10110 | 0b10111 => match (w >> 8) & 15 {
            // CBZ / CBNZ
            1 | 3 | 9 | 11 => in_it,
            // PUSH / POP
            4 | 5 => w & 0x1ff == 0,
            // CPS
            6 if w & 0xffe0 == 0xb660 => in_it || w & 3 == 0 || w & 0xc != 0,
            12 | 13 => w & 0x1ff == 0 || (w & 0x100 != 0 && mid_block),
            // IT
            15 if w & 15 != 0 => {
                let cond = (w >> 4) & 15;
                in_it || cond == 15 || (cond == 14 && (w & 15).count_ones() != 1)
            }
            _ => false,
        },
        // conditional branch (the UDF and SVC encodings are not branches)
        0b11010 | 0b11011 => (w >> 8) & 15 < 14 && in_it,
        0b11100 => mid_block,
        _ => false,
    }
}

fn wide(hw1: u32, hw2: u32, in_it: bool, mid_block: bool) -> bool {
    let rn = hw1 & 15;
    let rd = (hw2 >> 8) & 15;
    let rt = (hw2 >> 12) & 15;
    let rm = hw2 & 15;
    let s = hw1 & 0x10 != 0;
    // First operand / destination rules shared by the two data-processing groups.
    let dp = |op: u32| -> bool {
        let test = rd == 15 && s;
        (rd == 15 && (!s || !matches!(op, 0 | 4 | 8 | 13))) || (rn == 15 && (test || !matches!(op, 2 | 3)))
    };
    match (hw1 >> 11) & 3 {
        1 if hw1 & 0x640 == 0 => {
            // LDM / STM / PUSH / POP
            let load = hw1 & 0x10 != 0;
            let wback = hw1 & 0x20 != 0;
            rn == 15
                || hw2.count_ones() < 2
                || hw2 & 0x2000 != 0
                || (!load && hw2 & 0x8000 != 0)
                || (load && hw2 & 0xc000 == 0xc000)
                || (wback && hw2 >> rn & 1 != 0)
                || (load && hw2 & 0x8000 != 0 && mid_block)
        }
        1 if hw1 & 0x600 == 0 => {
            let (p, u, w, load) = (hw1 & 0x100 != 0, hw1 & 0x80 != 0, hw1 & 0x20 != 0, hw1 & 0x10 != 0);
            if !p && !w {
                // TBB / TBH (the exclusive-access instructions are not modelled at all)
                return u && load && hw2 & 0xffe0 == 0xf000 && (rm == 13 || rm == 15 || rn == 13 || mid_block);
            }
            // LDRD / STRD
            let (t, t2) = (rt, rd);
            t == 15
                || t2 == 15
                || t == 13
                || t2 == 13
                || (load && t == t2)
                || (w && (rn == 15 || rn == t || rn == t2))
                || (!load && rn == 15)
        }
        1 if hw1 & 0x600 == 0x200 => rm == 15 || dp((hw1 >> 5) & 15),
        2 if hw2 & 0x8000 != 0 => {
            // branches: B<c>.W is never allowed in an IT block, the others only last
            match (hw2 >> 12) & 5 {
                0 => (hw1 >> 6) & 15 < 14 && in_it,
                _ => mid_block,
            }
        }
        2 if hw1 & 0x200 == 0 => dp((hw1 >> 5) & 15),
        2 => {
            let lsb = ((hw2 >> 12) & 7) << 2 | ((hw2 >> 6) & 3);
            let low5 = hw2 & 31;
            rd == 15
                || match (hw1 >> 4) & 31 {
                    0b10000 | 0b10010 | 0b11000 | 0b11010 => rn == 15,
                    0b10100 | 0b11100 => rn == 15 || lsb + low5 > 31,
                    0b10110 => low5 < lsb,
                    _ => false,
                }
        }
        3 if hw1 & 0x400 == 0 => {
            let sub = (hw1 >> 4) & 0x7f;
            let load = sub & 1 != 0;
            if sub & 0b1110001 == 0 || (sub & 0b1100001 == 1 && (sub >> 1) & 3 != 3) {
                // single loads and stores
                let word = (hw1 >> 5) & 3 == 2;
                let pre_post = rn != 15 && hw1 & 0x80 == 0 && hw2 & 0x800 != 0;
                let reg_off = rn != 15 && hw1 & 0x80 == 0 && hw2 & 0x800 == 0;
                if load && rt == 15 && !word {
                    return reg_off && (rm == 13 || rm == 15); // preload hints
                }
                return (!load && (rt == 15 || rn == 15))
                    || (pre_post && hw2 & 0x100 != 0 && rn == rt)
                    || (reg_off && (rm == 13 || rm == 15))
                    || (load && rt == 15 && mid_block);
            }
            if sub >> 4 == 0b010 {
                // shifts by register, extensions, byte reversals, CLZ
                if rd == 15 || rm == 15 {
                    return true;
                }
                if hw1 & 0x80 == 0 {
                    return hw2 & 0xf0 == 0 && rn == 15;
                }
                return hw2 & 0xc0 == 0x80 && rn != rm;
            }
            if sub >> 3 == 0b0110 {
                // multiply (accumulate): MLS needs a real accumulator
                return rd == 15 || rn == 15 || rm == 15 || (sub & 7 == 0 && (hw2 >> 4) & 15 == 1 && rt == 15);
            }
            if sub >> 3 == 0b0111 {
                let divide = matches!(sub & 7, 1 | 3);
                return rd == 15 || rn == 15 || rm == 15 || (!divide && (rt == 15 || rt == rd));
            }
            false
        }
        _ => false,
    }
}