diff --git a/doc/architectural/front-page.md b/doc/architectural/front-page.md
index 24c34ad3ee8a..fd2848cfc7e8 100644
--- a/doc/architectural/front-page.md
+++ b/doc/architectural/front-page.md
@@ -67,6 +67,7 @@ you can access it here.
* \subpage memory-bounds-checking
+* \subpage inline-assembly
* \subpage satabs
## For contributors:
diff --git a/doc/architectural/inline-assembly.md b/doc/architectural/inline-assembly.md
new file mode 100644
index 000000000000..55f86a2b6134
--- /dev/null
+++ b/doc/architectural/inline-assembly.md
@@ -0,0 +1,222 @@
+\ingroup module_hidden
+
+\page inline-assembly Inline Assembly
+
+This page describes how `remove_asm` (`src/assembler/`) gives meaning to
+inline assembly, and how to extend the instruction semantics. The user-level
+description is `doc/cprover-manual/modeling-inline-asm.md`.
+
+## Overview
+
+`remove_asm` replaces every inline assembly statement by goto-program
+instructions. Every tool runs it before linking the CPROVER library. For a
+GCC-style statement (`code_asm_gcct`), it tries, in this order:
+
+1. the instruction semantics of the configured architecture
+ (`config.ansi_c.arch`, see `make_asm_semantics`), if there are any: the
+ statement is *interpreted*;
+2. the architecture-independent translation of fences, the x87 control word,
+ and no-ops (`handle_gcc_command` in `remove_asm.cpp`);
+3. the conservative approximation (`asm_approximationt`), with a warning.
+
+MSVC-style blocks use steps 2 and 3 only.
+
+## Interpretation
+
+The interpretation of a statement has these parts:
+
+- `asm_operandst` (`asm_operands.h`) parses the constraints of the operands
+ and classifies each operand as a register, memory, immediate, or flag
+ output operand. Register inputs that are arrays decay to pointers. The
+ symbolic names used in `%[name]` references are kept by the C front end in
+ `ID_identifier`.
+- `split_asm_template` (`asm_template.h`) splits the template into
+ instructions, and resolves `%N`, `%[name]`, `%lN`, and local label
+ references. It reads numbers as GAS does: `0x` is hexadecimal, `0b`
+ binary (unless it refers to the local label 0), and a leading `0` octal.
+ The modifiers of operand references are left to the semantics, which fail
+ on modifiers they do not understand (on x86, `%c`, `%P`, and `%n`, which
+ negates, are the modifiers of immediates).
+- `asm_lowering_contextt` (`asm_semantics.h`) holds the goto code being
+ built: the storage of register operands, hardware registers, and flags (all
+ block-local temporaries), labels, and helpers to read and write registers
+ and memory, branch, and fence.
+- `asm_semanticst` is the interface of the per-architecture semantics;
+ `asm_x86.cpp` implements x86 (i386, x86-64, x32), `asm_aarch64.cpp`
+ implements AArch64, `asm_arm.cpp` implements AArch32; `asm_arm_flags.h`
+ has the ARM condition flags and barriers. Semantics that depend on the
+ statement as a whole create an `asm_semantics_statet` (`make_state`), which
+ `lower` reads from `asm_lowering_contextt::semantics_state`.
+
+The code of a statement consists of a prologue that evaluates the inputs, the
+code of the instructions, and an epilogue that assigns the register outputs.
+Memory operands are accessed in place. For an operand whose constraint
+allows both a register and memory (`asm_operandt::may_be_memory`, as for
+`"rm"` and `"g"`), GCC may choose either, and the instructions may have a
+different effect: `remove_asmt::lower_gcc` lowers every combination of
+choices (`asm_operandst::memory_alternative`; an rvalue input in memory is
+a temporary object) and chooses one nondeterministically. Constraints with
+several alternatives are not interpreted. Where an operand may also be an
+immediate (`asm_operandt::may_be_immediate`), the semantics must fail on
+uses whose effect would differ for an immediate.
+The bits of a register above the
+width of the input operand it holds are unspecified. `prepare` determines
+whether they may be read: by a reference to the operand or to another operand
+that shares the register (an output the input is tied to, or an operand bound
+to the same register by its constraint), see
+`asm_semanticst::reference_bits`; by the name of the register
+(`register_name_bits`); by an instruction that reads the register without
+naming it (`implicit_reads`); or as part of an output that shares the
+register. If so, the upper bits are nondeterministic; otherwise the input is
+zero-extended. Zero-extension keeps a constant input constant, which symbolic
+execution relies on: otherwise, for example, a spin lock on a lock word that
+is written through such an operand would make the lock word symbolic, and the
+spin loops would be unwound to the unwinding bound.
+
+The code of the instructions has the checks for undefined behaviour of C
+disabled (via pragmas on its source location), as it models hardware
+operations whose results are defined. The prologue additionally evaluates the
+inputs at the plain source location of the statement, so that the checks
+apply to the C expressions of the operands.
+
+The following rules keep the interpretation sound. A statement that violates
+one of them is not interpreted:
+
+- The template must be in the dialect that the semantics read (AT&T on
+ x86). `goto-cc` sets `config.ansi_c.asm_dialect` from `-masm`, and the C
+ front end (`c_typecheck_baset::typecheck_asm`) records it on each
+ GCC-style statement as `ID_asm_dialect`, so that it survives into goto
+ binaries; `remove_asmt::lower_gcc` does not interpret statements in
+ another dialect, and `lower_instructions` does not interpret templates
+ that switch to Intel syntax with `.intel_syntax`.
+- Every instruction, prefix, operand form, and directive must be understood.
+- Instructions in another section (`.pushsection`/`.section`) are not
+ executed in sequence; they may be executed when another instruction faults
+ (exception fixups). Data in another section that refers to a label of the
+ statement, or to a C label of `asm goto`, records the label for run-time
+ patching (the Linux kernel's jump tables, `__jump_table`, by which a `jmp`
+ becomes a `nop` or vice versa) or as the continuation of a fault (exception
+ tables, `__ex_table`): the statement may then jump where its instructions
+ do not show. Such references are allowed only in `.smp_locks`, which
+ records lock prefixes that the kernel replaces by a no-op prefix on a
+ single processor, and in the sections that the linker discards
+ (`.discard.*`). Other data in other sections is ignored. This check is in
+ `remove_asmt::lower_instructions` and applies to all architectures.
+- A write to a hardware register that is neither bound to an operand by its
+ constraint nor clobbered may be a write to a register variable
+ (`register long x asm("rax")`), which the front end does not record.
+- A memory access must not use an address that may depend on a value that
+ is not modeled: a hardware register that the statement does not
+ initialise (which may hold a register variable), a nondeterministic value
+ (such as a system register, or `rdtsc`), an output operand read before it
+ is written, or memory that such a value was stored to. CBMC cannot relate
+ such an address to the objects it may point to, so the access would change
+ no object. `asm_lowering_contextt::depends_on_unknown` tracks these values
+ per position: an unconditional assignment of a known value makes its
+ target known; a label joins the states of the fall-through and of the
+ forward branches to it; a backward branch must not make a value unknown
+ that its target took as known.
+- An input tied to a memory output must be the same object.
+- The base of the x86 segments `%fs` and `%gs` is not modeled: a
+ segment-relative store may change a C object, unless it is not at a memory
+ operand and the statement has neither a `"memory"` clobber nor a memory
+ output (GCC's operand contract then excludes it).
+- GCC's operand contract must hold, as the operand storage does not model
+ registers that GCC gives to several operands
+ (`asm_lowering_contextt::check_operand_contract`): no instruction may
+ write an input-only register operand (GCC assumes it unchanged, and may
+ hold two inputs of the same value in one register), and no instruction
+ may read a register input or the address of a memory operand after an
+ instruction (in sequence, or in an earlier iteration of a loop formed by
+ a backward branch) wrote an output without the earlyclobber modifier `&`,
+ as GCC may allocate that output to the same register. An output that
+ holds its own value when the statement starts (`+`, or tied to an input)
+ shares a register only with an operand that GCC knows to have the same
+ value, which its register allocator only knows for operands of the same
+ machine mode; so for such an output, only operands of its width (or of
+ the width of an input tied to it) are considered. These checks are
+ independent of the architecture.
+
+An instruction that is atomic on the hardware (on x86: lock-prefixed
+instructions and `xchg` with a memory operand) is an atomic section of its
+own, with a full fence; see `asm_lowering_contextt::mark_atomic`. Making the
+whole statement atomic would hide races on the statement's other
+instructions.
+
+A load-exclusive followed by a store-exclusive is an atomic section from the
+load to the store (`asm_lowering_contextt::begin_exclusive`, `end_exclusive`)
+if there is no other memory access, branch, or label in between. Such an
+access, branch, or label abandons the region
+(`asm_lowering_contextt::abandon_exclusive`): the load-exclusive is then a
+plain load, and a later store-exclusive is not atomic with it. Without the
+atomic section the model admits more interleavings, so this is sound. The
+store-exclusive succeeds nondeterministically: the architecture permits
+spurious failures, and a store-exclusive without a load-exclusive in the same
+statement may succeed if a load-exclusive executed before. After `clrex` or a
+store-exclusive, a store-exclusive fails, unless a label or a load-exclusive
+comes in between: the monitor state is decided along the control flow, and a
+branch to the label may skip the instruction that cleared the monitor
+(`asm_lowering_contextt::define_label` forgets that the monitor is cleared).
+
+AArch32 needs two statement-wide properties (`make_a32_state` in
+`asm_arm.cpp`). In divided syntax, which GCC uses for inline assembly in A32
+code and for Thumb-1 targets, the Thumb-1 data-processing instructions set
+the flags without an `s` suffix (`add r0, r1, #1` is `adds`), depending on
+their registers. Unless the statement starts with `.syntax unified` or uses
+an instruction or form that GNU as does not accept in divided syntax for
+Thumb-1 targets (conditional execution other than of branches, `it`, `s`
+suffixes other than `movs`, the width qualifiers `.n` and `.w`, `teq`,
+`clz`, ...), the flags are nondeterministic after such an instruction;
+`movw`, `movt`, `cbz`, `cbnz`,
+and the exclusive loads and stores are no such evidence, as ARMv8-M Baseline
+has them. An explicit `.syntax divided` anywhere in the statement selects
+these forms in Thumb-2 code too, where the assembler also accepts the other
+instructions, so the flags are then nondeterministic in any case. In such
+code, `movs` between low registers is `adds` with `#0`, which clears C and V,
+so the flags are nondeterministic after `movs` as well.
+
+And `msr` of the control field may change the processor mode, which banks r8
+to r14. A statement that does so is not interpreted if it names any of these
+registers, or if it uses an operand, which the compiler may allocate to one
+of them, after the first such `msr`. Operands may be used before it, and as
+its source, unless a label precedes it, to which a later branch could
+return. The mode itself is not modeled, so that a statement that leaves the
+processor in another mode (`msr cpsr_c, #0x1f`, say) is interpreted as if
+the mode were unchanged: the C code after it, which uses the banked `sp` and
+`lr` of the new mode, and the output operands, which the compiler may read
+from banked registers, are modeled as in the original mode.
+An `it` instruction has no effect: the instructions of the IT block carry
+their conditions as suffixes, which the assemblers check against it.
+A conditional store-exclusive may not execute, so it does not clear the
+exclusive monitor. As the exclusive access region and the monitor are
+tracked while lowering, not on the paths that execute the instructions, a
+statement with a conditional load-exclusive, or with a conditional
+store-exclusive and another store-exclusive, is not interpreted (a
+conditional load-exclusive would open the region on both paths, and a
+conditional store-exclusive ends it on both).
+
+For `asm goto`, goto conversion creates a variable that selects the label to
+jump to (`code_asm_gcct::goto_target`) and dispatches on it after the
+statement; the interpretation assigns it the index of the label that the
+instructions jump to, or 0.
+
+## Adding instructions or an architecture
+
+To add an instruction, extend `lower` of the architecture's
+`asm_semanticst`. Return false (via `asm_lowering_contextt::fail`, with a
+reason) for any form that is not modeled exactly: the statement is then
+approximated, which is always sound. Model values that the architecture
+leaves undefined as nondeterministic, not as what one implementation does.
+
+To add an architecture, implement `asm_semanticst` (syntax, register width,
+canonical register names, constraint letters, flag conditions, and `lower`)
+and return it from `make_asm_semantics`.
+
+Tests that rely on the interpretation must pin the architecture with
+`--arch`, as the default is the host architecture. Tests of the
+architecture-independent translation and of the approximation use
+`--arch riscv64`, which has no instruction semantics and for which CBMC
+passes no `-m` option to the host preprocessor. The x86 tests use
+preprocessed input (`main.i`), as preprocessing with `-m64` or `-m32` fails
+on non-x86 hosts. Expected values of tests of instruction semantics should be
+observed on hardware.
diff --git a/doc/cprover-manual/index.md b/doc/cprover-manual/index.md
index 12e1a9c046b7..83b0a2eb85ba 100644
--- a/doc/cprover-manual/index.md
+++ b/doc/cprover-manual/index.md
@@ -24,6 +24,7 @@
* [Floating Point](modeling/floating-point/)
* [Generating Environments](goto-harness/)
* [Memory-mapped I/O](modeling/mmio/)
+ * [Inline Assembly](modeling/inline-asm/)
* [Shadow Memory](modeling/shadow-memory/)
8. Build Systems
diff --git a/doc/cprover-manual/modeling-inline-asm.md b/doc/cprover-manual/modeling-inline-asm.md
new file mode 100644
index 000000000000..efb318a2ca4e
--- /dev/null
+++ b/doc/cprover-manual/modeling-inline-asm.md
@@ -0,0 +1,506 @@
+[CPROVER Manual TOC](../../)
+
+## Modeling Inline Assembly
+
+CBMC supports GCC-style (`asm`/`__asm__`) and MSVC-style (`__asm {}`)
+inline assembly syntax. Blocks in braces (`asm { ... }`, `__asm { ... }`,
+and `__asm__ { ... }`, which Clang and Apple's GCC accept with
+`-fasm-blocks`) and functions whose body is a block of assembly (as in
+`void asm f(int *p) { ... }`, which Apple's GCC and CodeWarrior accept) use
+Intel syntax and name C variables; CBMC treats them as MSVC-style blocks in
+all modes. Before verification, the `remove_asm` pass replaces each
+inline-assembly statement by goto-program instructions:
+
+- On x86 (`--arch i386`, `--arch x86_64`, or `--arch x32`, which uses the
+ x86-64 instructions with 32-bit pointers), AArch64 (`--arch arm64`), and
+ AArch32 (`--arch arm`, `armel`, or `armhf`), the default being the host
+ architecture, a GCC-style statement is **interpreted** if CBMC's
+ instruction semantics for the architecture support all of its
+ instructions (see "x86 Instruction Semantics", "AArch64 Instruction
+ Semantics", and "AArch32 Instruction Semantics" below): it is translated
+ into goto-program instructions with the same effect on the C program
+ state.
+- Otherwise, if CBMC models every instruction of the statement (see
+ "Supported Instructions" below, on any architecture), the statement is
+ translated into the corresponding fences or calls to library functions,
+ followed by assignments to its output operands.
+- Otherwise the statement is **approximated conservatively**: everything the
+ statement may change, as far as CBMC can tell from its operands, is
+ assigned a nondeterministic value, and a warning that says why the
+ statement is not modeled is printed. The modeled instructions of such a
+ statement are not translated either.
+
+A statement with an empty assembly template has no instructions, so CBMC
+models it. Such a statement only writes its output operands (see Output
+Operands below); without outputs, as in the compiler barrier
+`asm volatile("" ::: "memory")`, it does nothing.
+
+None of the instructions in the tables below jumps, so an `asm goto`
+statement whose instructions are all listed there continues with the next
+statement. An interpreted `asm goto` statement jumps to the label that its
+instructions jump to.
+
+### x86 Instruction Semantics
+
+The x86 semantics cover the instructions that kernel and library code
+commonly uses in inline assembly, with AT&T syntax:
+
+- data movement: `mov`, `movabs`, `movzx`/`movz*`, `movsx`/`movs*`, `lea`,
+ `xchg`, `bswap`, `cmov`, `set`, sign extensions of the accumulator
+ (`cltq`, `cwtl`, `cltd`, `cqto`, ...);
+- arithmetic and logic, with the flags they set: `add`, `adc`, `sub`, `sbb`,
+ `and`, `or`, `xor`, `not`, `neg`, `inc`, `dec`, `cmp`, `test`, and the
+ carry flag instructions `clc`, `stc`, and `cmc`;
+- shifts and rotates (`shl`, `sal`, `shr`, `sar`, `rol`, `ror`), bit tests
+ (`bt`, `bts`, `btr`, `btc`), and bit scans and counts (`bsf`, `bsr`,
+ `tzcnt`, `lzcnt`, `popcnt`);
+- atomics: the `lock` prefix, `xchg` with a memory operand, `xadd`,
+ `cmpxchg`;
+- control flow within the statement: `jmp`, `j` to local and C labels
+ (`asm goto`), and flag outputs (`=@cc`);
+- fences (`mfence`, `lfence`, `sfence`), the x87 control word (`fstcw`,
+ `fnstcw`, `fldcw`), and instructions without effect on the C program state
+ (`nop`, `pause`, `cli`, `sti`, `hlt`, `wrmsr`, `out`); `cpuid`, `rdtsc`,
+ `rdtscp`, `rdmsr`, and `in` produce nondeterministic values.
+
+Operands follow GCC's rules: inputs are evaluated before the first
+instruction (and are subject to the checks CBMC performs), register outputs
+are assigned after the last instruction, and memory operands are accessed in
+place. Values that the architecture leaves undefined (such as some flags, or
+the destination of `bsf` with a zero source) are nondeterministic, and so are
+the bits of a register above the width of the input operand it holds, when
+an instruction may read them. For an operand whose constraint allows both a
+register and memory (such as `"rm"` or `"g"`), the compiler may choose
+either, and the effect of an instruction may depend on the choice (a 32-bit
+write clears the upper half of a 64-bit register, but not of memory): both
+choices are modeled, and one of them is chosen nondeterministically. The
+
+checks for undefined behaviour of C (such as `--signed-overflow-check`) do
+not apply to the instructions, which have defined results on the hardware;
+the checks of memory accesses do.
+
+A statement is not interpreted (and hence approximated, unless all of its
+instructions are listed in the tables below) if it:
+
+- contains an instruction, operand, prefix, or directive that the semantics
+ do not support, or instructions in another section (such as exception
+ fixups, which run when an instruction faults);
+- is in Intel syntax: the x86 semantics read AT&T syntax only. `goto-cc`
+ records the dialect that `-masm=intel` selects on each statement, so that
+ this also applies when another tool processes the goto binary; a template
+ that switches to Intel syntax with `.intel_syntax` is not interpreted
+ either;
+- contains an instruction without a size suffix whose operand size is not
+ given by a register operand (other than the count of a shift): the
+ assembler does not see the C type of a memory operand, and uses a default
+ size instead;
+- has more than three operands that may be in a register or in memory, or a
+ constraint with several alternatives (such as `"r,m"`), or reads an
+ operand that may also be an immediate (such as `"ri"` or `"g"`) in a way
+ that depends on whether it is one (at a width other than its own, or as the
+ bit offset of `bt` into memory);
+- contains data in another section that refers to one of its labels or, for
+ `asm goto`, to one of its C labels, other than in `.smp_locks` (the lock
+ prefixes that the Linux kernel removes on a single processor) and the
+ sections that the linker discards (`.discard.*`): such data records the
+ label for run-time patching, as do the Linux kernel's jump tables (static
+ branches), or as the place where execution continues when an instruction
+ faults, as do exception tables. The approximation of such an `asm goto`
+ statement jumps to any of its labels. Other data in other sections is
+ ignored;
+- relies on more than GCC's operand contract guarantees: it writes an
+ input-only operand that is held in a register (GCC assumes the register
+ unchanged, and may hold two inputs with the same value in one register),
+ or it reads an input operand held in a register, or the address of a
+ memory operand, after it wrote an output operand without the earlyclobber
+ modifier `&` (GCC may allocate both to the same register; for an output
+ that holds its own value when the statement starts, as with `+`, only
+ operands of the same width are considered);
+- stores to memory relative to the `%fs` or `%gs` segment, whose base CBMC
+ does not model (with a zero base, as for `%gs` in x86-64 Linux user space,
+ the store changes the object at the address), at a memory operand, or in
+ a statement with a `"memory"` clobber or a memory output. Other
+ segment-relative stores change no C object, as GCC's operand contract
+ requires, and segment-relative loads produce nondeterministic values;
+- has more than one output operand, or a `"memory"` clobber, and a register
+ output whose address depends on the program state (as `arr[idx]` does):
+ compilers differ in whether they compute that address before the
+ statement or after writing the other outputs;
+- writes a register that is neither bound to an operand nor clobbered: as
+ CBMC does not track register variables (`register long x asm("rax")`),
+ such a write may change a C variable;
+- accesses memory at an address that may be computed from a value that CBMC
+ does not model, such as a register that the statement does not initialise
+ (which may hold a register variable) or a value that is nondeterministic
+ (such as one produced by `rdtsc`): CBMC could not tell which object the
+ access changes.
+
+A lock-prefixed instruction (or `xchg` with a memory operand) is an atomic
+section with a full fence; the other instructions of the statement are not
+part of that atomic section.
+
+### AArch64 Instruction Semantics
+
+The AArch64 semantics cover:
+
+- data movement and processing on general-purpose registers, with the
+ condition flags: `mov`, `mvn`, `movz`, `movk`, `movn`, `neg`, `negs`,
+ `add`, `adds`, `sub`, `subs`, `and`, `ands`, `orr`, `eor`, `bic`, `bics`,
+ `orn`, `eon`, `lsl`, `lsr`, `asr`, `ror`, `cmp`, `cmn`, `tst`, and the
+ conditional selects (`csel`, `csinc`, `csinv`, `csneg`, `cset`, `csetm`);
+- loads and stores of single registers (including acquire/release
+ variants), the LSE atomics
+ (`ldadd`, `ldclr`, `ldeor`, `ldset`, `ldsmax`, `ldsmin`, `ldumax`,
+ `ldumin`, their `st` forms, `swp`, `cas`), and load-exclusive/
+ store-exclusive (`ldxr`, `ldaxr`, `stxr`, `stlxr`, `clrex`);
+- branches within the statement and to the labels of `asm goto`, and flag
+ outputs (`=@cc`);
+- barriers: `dmb` and `dsb` order the accesses that their option selects
+ (no option or one of `sy`, `ish`, `osh`, `nsh` orders all accesses, an
+ option ending in `ld` orders loads before later loads and stores, and an
+ option ending in `st` orders stores before later stores and, for `dsb`,
+ before later loads), and `isb` orders nothing by itself;
+- `mrs` and `msr` of the flags (`nzcv`), and `msr` of the interrupt masks
+ (`daif`, `daifset`, `daifclr`); `mrs` of other system registers produces
+ a nondeterministic value, and `msr` of other system registers (such as
+ `fpcr`, which sets the rounding mode) is not modeled;
+- instructions without effect on the C program state: `nop`, `yield`,
+ `wfe`, `wfi`, `sev`, `sevl`, `csdb`, `esb`, `sb`, `prfm`, and `hint #`
+ with the number of one of these hints or of `dgh`, `psb csync`,
+ `tsb csync`, `gcsb dsync`, `clrbhb`, or a `bti` variant. Other numbers of
+ `hint`, such as those of the pointer authentication instructions (which
+ write x16, x17, or x30) and of `chkfeat` (which writes x16), are not
+ interpreted.
+
+A load-exclusive followed by a store-exclusive in the same statement, with
+no other memory access, branch, or label in between, is an atomic section.
+Otherwise the load-exclusive is a plain load, and a later store-exclusive is
+not atomic with it (for example, in a compare-and-swap loop that branches
+past the store-exclusive if the values differ): the architecture does not
+guarantee the reservation across another memory access, and a branch to a
+label between the two may bypass the load-exclusive.
+The store-exclusive succeeds or fails nondeterministically, as the
+architecture permits spurious failures, and so does a store-exclusive
+without a preceding load-exclusive in the statement (a load-exclusive may
+have executed before). Hence symbolic execution needs an unwinding bound for
+the loop that retries the sequence until the store succeeds, and unwinding
+assertions for that loop fail. If the retried code has no effect when the
+store fails (it only computes registers that the next attempt computes
+again), the executions with more retries reach the same states, so that
+bounding the loop (with `--unwindset`, without unwinding assertions) does not
+miss any state.
+
+A store-exclusive fails if the exclusive monitor is cleared, by `clrex` or
+by an earlier store-exclusive. CBMC decides the monitor state along the
+control flow: it takes the monitor to be cleared after `clrex` or a
+store-exclusive only until the next label, as a branch to the label may skip
+the clearing instruction; after a label, a store-exclusive succeeds or fails
+nondeterministically.
+
+### AArch32 Instruction Semantics
+
+The AArch32 semantics cover A32 and T32 (Thumb) code in unified or divided
+syntax:
+
+- data processing on general-purpose registers, with the condition flags
+ (`s` suffix) and conditional execution (condition suffixes, and `it`
+ blocks): `mov`, `mvn`, `movw`, `movt`, `add`, `adc`, `sub`, `sbc`, `rsb`,
+ `neg`, `and`, `orr`, `eor`, `bic`, `lsl`, `lsr`, `asr`, `ror`, `mul`,
+ `cmp`, `cmn`, `tst`, `teq`, `clz`, `rev`, `rev16`, `rbit`, `uxtb`, `uxth`,
+ `sxtb`, `sxth`, with register or immediate operands (not shifted
+ registers); the shifts with an `s` suffix set C to the last bit shifted
+ out, while C is nondeterministic after `muls`, and after `movs`, `mvns`,
+ and the logical operations with an immediate operand (for which C depends
+ on the encoding of the immediate);
+- loads and stores of single registers (`ldr`, `ldrb`, `ldrh`, `ldrsb`,
+ `ldrsh`, `str`, `strb`, `strh`) with an immediate or register offset (not
+ with writeback), and load-exclusive/store-exclusive (`ldrex`, `strex`,
+ their byte and halfword forms, and `clrex`), as on AArch64 (see above);
+ a conditional store-exclusive, as in compare-and-swap sequences, is
+ modeled if it is the only store-exclusive of the statement, but a
+ statement with a conditional load-exclusive, or with a conditional
+ store-exclusive and another store-exclusive, is not interpreted;
+- branches within the statement and to the labels of `asm goto` (`b`,
+ `b`, `cbz`, `cbnz`), and flag outputs (`=@cc`);
+- barriers (`dmb`, `dsb`, `isb`, and their CP15 forms), `mrs` and `msr` of
+ the flags (`apsr`, `cpsr`), the interrupt masks (`msr` of `cpsr_c`,
+ `cpsid`, `cpsie`), hints (`nop`, `yield`, `wfe`, `pld`, ...), and CP15
+ cache and TLB maintenance and thread ID registers (`mcr`); `mrs` of other
+ special registers and `mrc` produce nondeterministic values. Of the cache
+ maintenance operations, those that invalidate data or unified caches
+ without cleaning them (`mcr p15, 0, Rt, c7, c6, 1`, for example) are not
+ modeled, as they may discard data that is not written back to memory.
+
+In divided syntax, which GCC uses for inline assembly in A32 code and for
+Thumb-1 targets, Thumb-1 instructions such as `add` set the flags without an
+`s` suffix. Unless the statement starts with `.syntax unified`, or uses an
+instruction or form that the assembler does not accept in divided syntax for
+Thumb-1 targets (such as conditional execution, `it`, `.w`, or `clz`;
+ARMv8-M Baseline has `movw`, `movt`, `cbz`, `cbnz`, and `ldrex`), the flags
+are nondeterministic after a data-processing instruction without `s` suffix,
+and after `movs`, which is then `adds` with `#0`. They are so in any case if
+the statement selects divided syntax with `.syntax divided`, which applies to
+Thumb-2 code as well.
+
+A statement that changes the interrupt masks or the processor mode with `msr`
+is not interpreted if it names any of the registers r8 to r14, which are
+banked by mode, or if it uses an operand, which the compiler may allocate to
+one of these registers, after the first such `msr` (the source of that `msr`
+may be an operand). The processor mode itself is not modeled: a statement
+that leaves the processor in another mode, such as `msr cpsr_c, #0x1f`, is
+interpreted as if the mode were unchanged, although the code after it uses
+the banked registers of the new mode (`sp` and `lr`, and r8 to r12 in FIQ
+mode), including the registers from which the compiler reads output
+operands. Reading or writing `pc` is not modeled.
+
+### Supported Instructions
+
+CBMC splits the assembler text into instructions at newlines and `;`, except
+in MSVC-style blocks, where `;` starts a comment that extends to the end of
+the line, as in MASM. On x86 (and in MSVC-style blocks), Power, MIPS, RISC-V,
+s390, and LoongArch, `#` starts a comment that extends to the end of the line.
+For the other targets (see `--arch`), including ARM and AArch64, `#` starts a
+comment only at the start of a line or statement; elsewhere it introduces an
+immediate operand, as in `dmb #11`.
+
+#### x86
+
+| Instruction | Effect in CBMC |
+|---|---|
+| `mfence` | Full memory fence |
+| `lfence` | Orders loads before later loads and stores; a store may still be delayed beyond a later load |
+| `sfence` | Orders stores before later stores; a store may still be delayed beyond a later load |
+| `fstcw` / `fnstcw` | Stores the rounding mode (`__CPROVER_rounding_mode`) to bits 10 and 11 of the operand; the other bits of the control word (exception masks and precision control) are nondeterministic |
+| `fldcw` | Sets the rounding mode from bits 10 and 11 of the operand |
+| `stmxcsr` | Stores the rounding mode to bits 13 and 14 of the operand; the other bits of the SSE control and status register are nondeterministic |
+| `ldmxcsr` | Sets the rounding mode from bits 13 and 14 of the operand |
+| `nop`, `pause` | No effect |
+
+For GCC-style statements, the operand of `fstcw`, `fnstcw`, `fldcw`,
+`stmxcsr`, and `ldmxcsr` must be an operand reference (`%0`, `%1`, ...); for
+MSVC-style blocks, it must be the name of a variable, and the only variable
+of that name among the local variables and parameters of the enclosing
+function and the global variables (as CBMC does not know which of them is
+in scope). Neither the `lock` prefix nor the read-modify-write instructions
+that take it are modeled, and neither is `xchg`, so statements using them
+are approximated.
+
+#### ARM
+
+| Instruction | Effect in CBMC |
+|---|---|
+| `dmb`, `dsb` | Full fence without an option or with option `sy`, `ish`, `osh`, or `nsh`; with an option ending in `ld` (such as `ishld`), loads are ordered before later loads and stores; with an option ending in `st` (such as `ishst`), stores are ordered before later stores, and a `dsb` also orders them before later loads, as it waits for the stores to complete before any later instruction executes |
+| `isb` | Empty fence; no observable effect unless combined with a branch |
+| `nop`, `yield` | No effect |
+
+The barrier options can also be given as immediates: `#15` is `sy`, `#14`
+`st`, `#13` `ld`, `#11` `ish`, `#10` `ishst`, `#9` `ishld`, `#7` `nsh`, `#6`
+`nshst`, `#5` `nshld`, `#3` `osh`, `#2` `oshst`, and `#1` `oshld`. The other
+values are reserved, and barriers with them are not modeled.
+
+#### Power
+
+| Instruction | Effect in CBMC |
+|---|---|
+| `sync`, `hwsync` | Full memory barrier |
+| `lwsync` | Lightweight sync: orders loads before later loads and stores, and stores before later stores, but not stores before later loads |
+| `eieio` | Orders stores before later stores |
+| `isync` | Empty fence; no observable effect unless combined with a branch |
+| `nop` | No effect |
+
+Only the x86 fences, `fstcw`, `fnstcw`, `fldcw`, `stmxcsr`, `ldmxcsr`,
+`nop`, and `pause` are modeled in MSVC-style blocks.
+
+### Output Operands
+
+If CBMC models every instruction of a GCC-style statement, the output
+operands with an `=` constraint are written as follows, after the
+instructions:
+
+- An output that a modeled instruction writes (the operand of `fstcw`,
+ `fnstcw`, or `stmxcsr`) receives the value that the instruction stores.
+- An output that is tied to an input operand by a matching constraint (an
+ input constraint that is the number of the output, as in
+ `asm("" : "=r"(x) : "0"(y))`, or its symbolic name in brackets, as in
+ `"[out]"`) receives the value of that input, as none of the modeled
+ instructions changes the register that holds both. This requires that the
+ types of input and output are the same, or are integer, enumeration, or
+ pointer types of the same width; otherwise the output is treated as not
+ tied.
+- Any other output is assigned a nondeterministic value: none of the
+ modeled instructions writes it, so it receives whatever the register (or
+ memory location) that the compiler chose for it holds.
+
+All inputs and the addresses of all outputs are evaluated before any output
+is written. A statement with more than one output, one of which has an
+address that depends on the program state (as `arr[idx]` does), is
+approximated instead: compilers differ in whether they compute that address
+before or after writing the other outputs (GCC at `-O0` computes it after).
+In-out operands (those with a `+` constraint) keep their values
+unless a modeled instruction writes them.
+
+### Conservative Approximation
+
+The operands of a GCC-style statement are evaluated before the statement,
+including any side effects they have. If the statement contains an
+instruction that is not modeled, then
+
+1. the input operands (and the in-out operands, those with a `+`
+ constraint) are read, which makes them subject to the checks that CBMC
+ performs, for example `--pointer-check`;
+2. the objects that the input and in-out operands point to, and all objects
+ reachable from these, are assigned nondeterministic values. This includes
+ operands of pointer type, arrays (which decay to pointers), pointers cast
+ to integer types (as in `(uintptr_t)&x`), and the pointer members of
+ operands of struct or union type;
+3. if the statement has a `"memory"` clobber, or no operands and no clobbers
+ at all (basic assembly), all objects of static lifetime, all local
+ variables whose address is taken of all functions on the call stack, and
+ all objects reachable from these are assigned nondeterministic values;
+4. the output operands are assigned nondeterministic values, at the
+ addresses they had before the statement and, for outputs whose address
+ depends on the program state, also at the addresses they have after it,
+ and an `asm goto` statement jumps to any of its labels, or continues with
+ the next statement;
+5. the rounding mode is assigned a nondeterministic value.
+
+Symbolic execution computes the reachable objects from the value sets of the
+pointers stored in the objects (following pointers transitively), so this
+includes, for example, all nodes of a linked list whose head is stored in a
+variable of static lifetime. The pointers stored in the objects that are
+changed are not made arbitrary: each of them is assigned a nondeterministic
+choice among its previous value, null, the following candidates, and a
+pointer to any aligned position (see below) within the objects that the
+candidates point to, at which an object of the pointer's target type fits: the
+pointers previously stored in the changed objects, and the reachable objects
+(of a compatible type). The statement can thus relink a list or advance a
+pointer within an array, and dereferencing the pointers afterwards remains
+precise.
+
+Two kinds of memory have no address that CBMC knows, and are changed as
+well. Dereferencing a pointer variable whose target is not known (for
+example, the result of a function without a body) yields a placeholder
+object for that variable; the placeholder objects of the reachable pointer
+variables are changed (the pointers stored in them are not candidate values
+of other pointers, as they are mostly not written). Memory at integer
+addresses (such as a memory-mapped register accessed as
+`*(volatile int *)0x1000`), which CBMC models as the array
+`__CPROVER_memory`, is changed if the statement may access arbitrary memory
+(item 3 above), or if a reachable pointer may point to an integer address.
+
+MSVC-style blocks have no operand interface. If a block contains an
+instruction that is not modeled, each local variable (or parameter) of the
+enclosing function that the block names (all of them, if a name is
+shadowed) is treated like an output operand and like an input operand of
+pointer type, and the block is treated as having a `"memory"` clobber. A
+member access such as `s.a` names the variable `s`.
+
+### Limitations
+
+- The approximation only reaches the objects listed above. Objects that the
+ statement can reach only through pointers that CBMC does not track (for
+ example, pointers stored in local variables of a caller whose address is
+ not taken) keep their values. If an operand whose target is not known is
+ not a pointer variable (or computed from one by pointer arithmetic and
+ casts), for example a pointer read from uninitialized memory, a warning is
+ printed, as the object it points to is not changed. As for C code, CBMC
+ does not consider that two pointers whose targets are not known may point
+ to the same object: changing the placeholder object of one of them does not
+ change that of the other.
+- A pointer stored in a changed object is restricted to the candidate values
+ described above. The approximation thus assumes that the statement does
+ not store other pointers in it: no pointer one past the end of an object or
+ to a position at which an object of the pointer's target type does not fit
+ entirely, no pointer to an object that is not reachable, no pointer into an
+ object of a different type unless a pointer of a compatible type previously
+ pointed into it, and no pointer computed from an integer. Dereferencing a
+ pointer that may point to other positions than the start of an object, such
+ as `p[1]` for a pointer `p` into an array, may fail the pointer checks after
+ the statement, as the statement may have moved `p` to the last element.
+- A pointer that the statement may move within an object is also assumed to
+ be aligned: its offset from the start of the object is a multiple of the
+ alignment of the pointer's target type, which is its size for scalars
+ whose size is a power of two (and 1 for other scalars), the alignment of
+ the elements for arrays, and the largest alignment of the members for
+ structs and unions (1 if they are packed); a smaller alignment given with
+ `__attribute__((aligned))` is used instead. The statement is thus assumed
+ not to store pointers such as `(int *)((char *)a + 1)` for an `int` array
+ `a`, although the processor may be able to access them. Pointers of type
+ `char *` and `void *` may point to any byte. Objects that contain a packed
+ struct or union, or a type whose given alignment is smaller than that of its
+ members, are exempt: pointers may point to any byte in them. The
+ assumption also applies to objects of unknown type, such as the objects
+ behind pointers whose targets are unknown. Arbitrary positions are not used
+ in general, as dereferencing a pointer that may point to them is costly
+ (25 times slower for four pointers into an array of 256 `int`).
+- The candidate values are only used if there are at most 64 of them
+ (counting the pointer's own previous value), and only until the pointers
+ that one statement restricts to their candidates have 65536 candidate
+ values in total. Otherwise, the pointer is assigned an arbitrary value,
+ and a warning is printed. Such a pointer may point to any object whose
+ address is stored anywhere, to any reachable object of a compatible type,
+ or be invalid: dereferencing it considers all these objects, which is slow
+ if there are many, and the pointer checks of the dereference fail.
+ Pointers in unions and in arrays of more than 64 elements, or of a size
+ that is not constant, are assigned arbitrary values as well, and a warning
+ is printed.
+- Constant objects are not changed.
+- Objects of static lifetime that the CPROVER library adds to the program
+ (such as `optind` for `getopt`) are changed like the objects of the
+ program. Objects whose names start with `__CPROVER_` hold CBMC's internal
+ state (for example, the bookkeeping of dynamic memory) and are not
+ changed, except the rounding mode, `__CPROVER_memory` (see above), and the
+ objects of the library that stand for state the program can access:
+ `__CPROVER_errno` (`errno`), `__CPROVER_passwd` (the result of `getpwnam`
+ and `getpwuid`), and `__CPROVER_fpu_control_word` (the control word of
+ `_controlfp`).
+- Symbolic execution (`cbmc`) computes the reachable objects most
+ precisely. The static analyses use their own pointer analyses: the value
+ set analyses (as used by `goto-instrument`) and the reaching definitions and
+ dependence graph (as used by `goto-analyzer`) follow pointers using their
+ value sets, but do not track the contents of heap objects. The other
+ abstract domains of `goto-analyzer` treat the statement as changing all
+ variables (constant propagation: all objects of static lifetime and all
+ address-taken variables). `--full-slice` does not slice programs that
+ contain approximated inline assembly with pointer operands, a `"memory"`
+ clobber, or no operands (basic assembly). The race check
+ (`goto-instrument --race-check`) and the instrumentation of interrupt
+ handlers (`--isr`) take the statement to write the reachable variables (all
+ objects of static lifetime, if it may access arbitrary memory) found by the
+ value set analysis. The objects that the static analyses take the
+ statement to read and write (for the dependence graph, the race check, and
+ `--isr`) do not include `__CPROVER_memory` and the placeholder objects of
+ pointers whose targets are not known. The weak memory instrumentation
+ (`--mm`) ignores the
+ statement: its writes are not delayed, but become visible to other threads
+ at once.
+- In C++ code, the C++ front end discards the operands and clobbers of
+ GCC-style statements. Such a statement is thus treated as basic assembly:
+ its outputs are not written (neither by the approximation nor as
+ described under Output Operands), and the objects its operands point to
+ are changed only if they are objects of static lifetime or address-taken
+ local variables. CBMC may hence prove properties of C++ code that fail
+ when compiled.
+- Atomicity and memory ordering of instructions that are not modeled are not
+ modeled; such statements are not atomic and do not act as fences.
+- A loop that retries an approximated statement until it succeeds, such as a
+ spin lock that is acquired with `xchg` or `cmpxchg`, may retry arbitrarily
+ often, as the result of each attempt is nondeterministic. Symbolic
+ execution thus unwinds such loops up to the unwinding bound, and their
+ unwinding assertions fail. Replace the functions that contain such loops
+ by C models (for example, with `goto-instrument --remove-function-body`
+ and a definition of the function without inline assembly).
+- CBMC does not check whether the instructions are valid for the target
+ architecture: for example, `sync` is modeled as a Power barrier regardless
+ of the `--arch` option.
+- CBMC has a single rounding mode, `__CPROVER_rounding_mode`, which applies
+ to all floating-point operations, while x86 processors have one for x87
+ operations (set by `fldcw`) and one for SSE operations (set by
+ `ldmxcsr`). Both instructions set CBMC's rounding mode, and `fstcw`,
+ `fnstcw`, and `stmxcsr` all read it. On x86-64, where `float` and `double`
+ arithmetic uses SSE, `fldcw` thus changes how CBMC rounds these operations
+ although the processor does not, and CBMC may prove properties that fail
+ on hardware. C libraries set both control registers in `fesetround`.
+- You can use `goto-instrument --show-goto-functions` (or
+ `cbmc --show-goto-functions`) to inspect how an inline-assembly statement
+ was translated.
diff --git a/jbmc/src/jbmc/Makefile b/jbmc/src/jbmc/Makefile
index f87d8d3662d5..cd58c57f5ec2 100644
--- a/jbmc/src/jbmc/Makefile
+++ b/jbmc/src/jbmc/Makefile
@@ -10,6 +10,7 @@ OBJ += ../$(CPROVER_DIR)/src/ansi-c/ansi-c$(LIBEXT) \
../$(CPROVER_DIR)/src/goto-checker/goto-checker$(LIBEXT) \
../$(CPROVER_DIR)/src/goto-programs/goto-programs$(LIBEXT) \
../$(CPROVER_DIR)/src/goto-symex/goto-symex$(LIBEXT) \
+ ../$(CPROVER_DIR)/src/pointer-analysis/reachable_objects$(OBJEXT) \
../$(CPROVER_DIR)/src/pointer-analysis/value_set$(OBJEXT) \
../$(CPROVER_DIR)/src/pointer-analysis/value_set_analysis_fi$(OBJEXT) \
../$(CPROVER_DIR)/src/pointer-analysis/value_set_domain_fi$(OBJEXT) \
diff --git a/regression/cbmc-concurrency/CMakeLists.txt b/regression/cbmc-concurrency/CMakeLists.txt
index 58bbbe6cdb6e..01b695c1e260 100644
--- a/regression/cbmc-concurrency/CMakeLists.txt
+++ b/regression/cbmc-concurrency/CMakeLists.txt
@@ -1,3 +1,9 @@
+if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
+ set(gcc_only_string "-X;gcc-only;")
+else()
+ set(gcc_only_string "")
+endif()
+
if((NOT WIN32) AND (NOT APPLE) AND (NOT (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")))
add_test_pl_tests(
"$ --validate-goto-model --validate-ssa-equation"
@@ -6,7 +12,7 @@ else()
add_test_pl_profile(
"cbmc-concurrency"
"$ --validate-goto-model --validate-ssa-equation"
- "-C;-X;pthread"
+ "${gcc_only_string}-C;-X;pthread"
"CORE"
)
endif()
diff --git a/regression/cbmc-concurrency/Makefile b/regression/cbmc-concurrency/Makefile
index 5d8281162303..7fb41fd257a6 100644
--- a/regression/cbmc-concurrency/Makefile
+++ b/regression/cbmc-concurrency/Makefile
@@ -9,11 +9,15 @@ ifeq ($(filter-out OSX MSVC FreeBSD,$(BUILD_ENV_)),)
no_pthread = -X pthread
endif
+ifeq ($(BUILD_ENV_),MSVC)
+ gcc_only = -X gcc-only
+endif
+
test:
- @../test.pl -e -p -c "../../../src/cbmc/cbmc --validate-goto-model --validate-ssa-equation" $(no_pthread)
+ @../test.pl -e -p -c "../../../src/cbmc/cbmc --validate-goto-model --validate-ssa-equation" $(no_pthread) $(gcc_only)
tests.log: ../test.pl
- @../test.pl -e -p -c "../../../src/cbmc/cbmc --validate-goto-model --validate-ssa-equation" $(no_pthread)
+ @../test.pl -e -p -c "../../../src/cbmc/cbmc --validate-goto-model --validate-ssa-equation" $(no_pthread) $(gcc_only)
clean:
find . -name '*.out' -execdir $(RM) '{}' \;
diff --git a/regression/cbmc-concurrency/asm_aarch64_barrier_st/dmb.desc b/regression/cbmc-concurrency/asm_aarch64_barrier_st/dmb.desc
new file mode 100644
index 000000000000..f74988a44eab
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_aarch64_barrier_st/dmb.desc
@@ -0,0 +1,12 @@
+CORE gcc-only
+main.c
+--arch arm64 --mm tso -DBARRIER='"dmb ishst"'
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 27 store buffering: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly
+--
+dmb ishst orders stores only before later stores, so both loads may read 0.
diff --git a/regression/cbmc-concurrency/asm_aarch64_barrier_st/main.c b/regression/cbmc-concurrency/asm_aarch64_barrier_st/main.c
new file mode 100644
index 000000000000..e2f0229ddc6c
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_aarch64_barrier_st/main.c
@@ -0,0 +1,29 @@
+// Store buffering: each thread stores to one variable and then loads the
+// other. A dsb with an st option completes the stores before it before any
+// later instruction executes, so the loads cannot both read 0. A dmb with an
+// st option orders stores only before later stores.
+#ifndef BARRIER
+# define BARRIER "dsb ishst"
+#endif
+
+int x, y, r1, r2, done;
+
+void t(void)
+{
+ x = 1;
+ __asm__ volatile(BARRIER ::: "memory");
+ r1 = y;
+ done = 1;
+}
+
+int main(void)
+{
+__CPROVER_ASYNC_1:
+ t();
+ y = 1;
+ __asm__ volatile(BARRIER ::: "memory");
+ r2 = x;
+ __CPROVER_assume(done);
+ __CPROVER_assert(r1 == 1 || r2 == 1, "store buffering");
+ return 0;
+}
diff --git a/regression/cbmc-concurrency/asm_aarch64_barrier_st/test.desc b/regression/cbmc-concurrency/asm_aarch64_barrier_st/test.desc
new file mode 100644
index 000000000000..4139d0c8b0ae
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_aarch64_barrier_st/test.desc
@@ -0,0 +1,14 @@
+CORE gcc-only
+main.c
+--arch arm64 --mm tso
+^EXIT=0$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 27 store buffering: SUCCESS$
+^VERIFICATION SUCCESSFUL$
+--
+^warning: ignoring
+approximating inline assembly
+--
+dsb ishst orders the stores before it before the loads after it, as no
+instruction after a dsb executes before the dsb completes (Arm ARM, "Data
+Synchronization Barrier"). The memory model tso relaxes only that order.
diff --git a/regression/cbmc-concurrency/asm_aarch64_exclusive/main.c b/regression/cbmc-concurrency/asm_aarch64_exclusive/main.c
new file mode 100644
index 000000000000..46d347b2b69e
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_aarch64_exclusive/main.c
@@ -0,0 +1,35 @@
+// A load-exclusive/store-exclusive sequence is atomic; plain loads and
+// stores are not.
+unsigned long x, y, done;
+
+void t(void)
+{
+ unsigned long tmp;
+ unsigned int status;
+ __asm__ volatile(
+ "1: ldxr %0, [%2]\n"
+ " add %0, %0, #1\n"
+ " stxr %w1, %0, [%2]\n"
+ " cbnz %w1, 1b"
+ : "=&r"(tmp), "=&r"(status)
+ : "r"(&x)
+ : "memory");
+ __asm__ volatile(
+ "ldr %0, [%1]\n"
+ " add %0, %0, #1\n"
+ " str %0, [%1]"
+ : "=&r"(tmp)
+ : "r"(&y)
+ : "memory");
+}
+
+int main(void)
+{
+__CPROVER_ASYNC_1:
+ t(), done = 1;
+ t();
+ __CPROVER_assume(done);
+ __CPROVER_assert(x == 2, "ll/sc increment");
+ __CPROVER_assert(y == 2, "plain increment"); // fails
+ return 0;
+}
diff --git a/regression/cbmc-concurrency/asm_aarch64_exclusive/test.desc b/regression/cbmc-concurrency/asm_aarch64_exclusive/test.desc
new file mode 100644
index 000000000000..3935b28ec264
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_aarch64_exclusive/test.desc
@@ -0,0 +1,14 @@
+CORE gcc-only
+main.c
+--arch arm64 --unwind 2 --no-unwinding-assertions
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 32 ll/sc increment: SUCCESS$
+^\[main\.assertion\.2\] line 33 plain increment: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly
+--
+A load-exclusive/store-exclusive sequence is an atomic section; a plain
+read-modify-write is not.
diff --git a/regression/cbmc-concurrency/asm_arm_conditional_exclusive/main.c b/regression/cbmc-concurrency/asm_arm_conditional_exclusive/main.c
new file mode 100644
index 000000000000..72d5b4ac6261
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_arm_conditional_exclusive/main.c
@@ -0,0 +1,30 @@
+// A load-exclusive, then a strexeq that does not execute and a strexne that
+// does, which hardware makes atomic with the load-exclusive. The statement is
+// approximated.
+unsigned x, done;
+
+void two_stores(unsigned nz)
+{
+ unsigned tmp, status;
+ __asm__ volatile(
+ "ldrex %0, [%2]\n"
+ " add %0, %0, #1\n"
+ " cmp %3, #0\n"
+ " ite eq\n"
+ " strexeq %1, %0, [%2]\n"
+ " strexne %1, %0, [%2]"
+ : "=&r"(tmp), "=&r"(status)
+ : "r"(&x), "r"(nz)
+ : "cc", "memory");
+ __CPROVER_assume(status == 0);
+}
+
+int main(void)
+{
+__CPROVER_ASYNC_1:
+ two_stores(1), done = 1;
+ two_stores(1);
+ __CPROVER_assume(done);
+ __CPROVER_assert(x == 2, "two store-exclusives");
+ return 0;
+}
diff --git a/regression/cbmc-concurrency/asm_arm_conditional_exclusive/test.desc b/regression/cbmc-concurrency/asm_arm_conditional_exclusive/test.desc
new file mode 100644
index 000000000000..bcfd3bf47d7f
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_arm_conditional_exclusive/test.desc
@@ -0,0 +1,15 @@
+CORE gcc-only
+main.c
+--arch arm
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 28 two store-exclusives: FAILURE$
+approximating inline assembly statement at .* function two_stores: conditional load-exclusive, or conditional store-exclusive and another store-exclusive
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+The property holds on hardware, but the statement is approximated, as its
+lowering would not make the store-exclusive that executes atomic with the
+load-exclusive. A conditional store-exclusive that is the only one of its
+statement is modeled (see asm_arm_exclusive).
diff --git a/regression/cbmc-concurrency/asm_arm_dsb_st_sb/dmb.desc b/regression/cbmc-concurrency/asm_arm_dsb_st_sb/dmb.desc
new file mode 100644
index 000000000000..397b8ddea1ae
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_arm_dsb_st_sb/dmb.desc
@@ -0,0 +1,12 @@
+CORE
+dmb.i
+--arch arm64 --mm tso
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 22 store buffering: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+dmb ishst only orders stores before later stores, so the store may be
+delayed beyond the later load.
diff --git a/regression/cbmc-concurrency/asm_arm_dsb_st_sb/dmb.i b/regression/cbmc-concurrency/asm_arm_dsb_st_sb/dmb.i
new file mode 100644
index 000000000000..a0b6f3167cfe
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_arm_dsb_st_sb/dmb.i
@@ -0,0 +1,24 @@
+// Store buffering: each thread stores to one variable and then loads the
+// other. A dmb ishst separates the store from the load. Preprocessed input, as
+// preprocessing for --arch arm64 may fail on hosts of other architectures.
+int x, y, r0, r1, done;
+
+void t0(void)
+{
+ x = 1;
+ __asm__ volatile("dmb ishst" ::: "memory");
+ r0 = y;
+ done = 1;
+}
+
+int main(void)
+{
+__CPROVER_ASYNC_1:
+ t0();
+ y = 1;
+ __asm__ volatile("dmb ishst" ::: "memory");
+ r1 = x;
+ __CPROVER_assume(done);
+ __CPROVER_assert(!(r0 == 0 && r1 == 0), "store buffering");
+ return 0;
+}
diff --git a/regression/cbmc-concurrency/asm_arm_dsb_st_sb/dsb.i b/regression/cbmc-concurrency/asm_arm_dsb_st_sb/dsb.i
new file mode 100644
index 000000000000..fb40bf7be03f
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_arm_dsb_st_sb/dsb.i
@@ -0,0 +1,24 @@
+// Store buffering: each thread stores to one variable and then loads the
+// other. A dsb ishst separates the store from the load. Preprocessed input, as
+// preprocessing for --arch arm64 may fail on hosts of other architectures.
+int x, y, r0, r1, done;
+
+void t0(void)
+{
+ x = 1;
+ __asm__ volatile("dsb ishst" ::: "memory");
+ r0 = y;
+ done = 1;
+}
+
+int main(void)
+{
+__CPROVER_ASYNC_1:
+ t0();
+ y = 1;
+ __asm__ volatile("dsb ishst" ::: "memory");
+ r1 = x;
+ __CPROVER_assume(done);
+ __CPROVER_assert(!(r0 == 0 && r1 == 0), "store buffering");
+ return 0;
+}
diff --git a/regression/cbmc-concurrency/asm_arm_dsb_st_sb/test.desc b/regression/cbmc-concurrency/asm_arm_dsb_st_sb/test.desc
new file mode 100644
index 000000000000..c6f6c65bf7e1
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_arm_dsb_st_sb/test.desc
@@ -0,0 +1,14 @@
+CORE
+dsb.i
+--arch arm64 --mm tso
+^EXIT=0$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 22 store buffering: SUCCESS$
+^VERIFICATION SUCCESSFUL$
+--
+^warning: ignoring
+--
+A dsb waits for the accesses it orders, here the stores, to complete before
+any later instruction executes, so it also orders the store before the later
+load, and store buffering is prevented. dsb ishst used to be modeled like
+dmb ishst, which only orders stores before later stores.
diff --git a/regression/cbmc-concurrency/asm_arm_exclusive/main.c b/regression/cbmc-concurrency/asm_arm_exclusive/main.c
new file mode 100644
index 000000000000..d256d9c51874
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_arm_exclusive/main.c
@@ -0,0 +1,55 @@
+// A load-exclusive/store-exclusive sequence is atomic; plain loads and
+// stores are not.
+unsigned x, y, z, done;
+
+// compare and swap with a conditional store-exclusive, as in Linux
+unsigned cmpxchg(unsigned *p, unsigned old, unsigned new)
+{
+ unsigned res, prev;
+ __asm__ volatile(
+ "ldrex %1, [%2]\n"
+ " mov %0, #0\n"
+ " teq %1, %3\n"
+ " it eq\n"
+ " strexeq %0, %4, [%2]"
+ : "=&r"(res), "=&r"(prev)
+ : "r"(p), "r"(old), "r"(new)
+ : "cc", "memory");
+ __CPROVER_assume(res == 0);
+ return prev;
+}
+
+void t(void)
+{
+ unsigned tmp, status;
+ __asm__ volatile(
+ "1: ldrex %0, [%2]\n"
+ " add %0, %0, #1\n"
+ " strex %1, %0, [%2]\n"
+ " teq %1, #0\n"
+ " bne 1b"
+ : "=&r"(tmp), "=&r"(status)
+ : "r"(&x)
+ : "cc", "memory");
+ __asm__ volatile(
+ "ldr %0, [%1]\n"
+ " add %0, %0, #1\n"
+ " str %0, [%1]"
+ : "=&r"(tmp)
+ : "r"(&y)
+ : "memory");
+ unsigned old = z;
+ __CPROVER_assume(cmpxchg(&z, old, old + 1) == old);
+}
+
+int main(void)
+{
+__CPROVER_ASYNC_1:
+ t(), done = 1;
+ t();
+ __CPROVER_assume(done);
+ __CPROVER_assert(x == 2, "ll/sc increment");
+ __CPROVER_assert(y == 2, "plain increment"); // fails
+ __CPROVER_assert(z == 2, "compare and swap increment");
+ return 0;
+}
diff --git a/regression/cbmc-concurrency/asm_arm_exclusive/test.desc b/regression/cbmc-concurrency/asm_arm_exclusive/test.desc
new file mode 100644
index 000000000000..a0ddf6ab0830
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_arm_exclusive/test.desc
@@ -0,0 +1,16 @@
+CORE gcc-only
+main.c
+--arch arm --unwind 2 --no-unwinding-assertions
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 51 ll/sc increment: SUCCESS$
+^\[main\.assertion\.2\] line 52 plain increment: FAILURE$
+^\[main\.assertion\.3\] line 53 compare and swap increment: SUCCESS$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly
+--
+A load-exclusive/store-exclusive sequence is an atomic section; a plain
+read-modify-write is not. A conditional store-exclusive that is the only
+store-exclusive of its statement is part of the atomic section.
diff --git a/regression/cbmc-concurrency/asm_x86_atomic_instruction/main.i b/regression/cbmc-concurrency/asm_x86_atomic_instruction/main.i
new file mode 100644
index 000000000000..6410b00cc812
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_x86_atomic_instruction/main.i
@@ -0,0 +1,27 @@
+// An atomic instruction is an atomic section on its own, not the whole
+// statement: the read-modify-write of x by plain instructions can race.
+unsigned x, y, z, dummy;
+
+void t(void)
+{
+ __asm__ __volatile__(
+ "movl %0,%%eax\n\t"
+ "incl %%eax\n\t"
+ "movl %%eax,%0\n\t"
+ "lock orl $0,%1"
+ : "+m"(x), "+m"(dummy)
+ :
+ : "eax", "cc", "memory");
+ __asm__ __volatile__("lock incl %0" : "+m"(y) : : "cc", "memory");
+}
+
+int main(void)
+{
+__CPROVER_ASYNC_1:
+ t(), z = 1;
+ t();
+ __CPROVER_assume(z);
+ __CPROVER_assert(x == 2, "plain read-modify-write"); // fails
+ __CPROVER_assert(y == 2, "locked increment");
+ return 0;
+}
diff --git a/regression/cbmc-concurrency/asm_x86_atomic_instruction/test.desc b/regression/cbmc-concurrency/asm_x86_atomic_instruction/test.desc
new file mode 100644
index 000000000000..d27a063be22d
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_x86_atomic_instruction/test.desc
@@ -0,0 +1,14 @@
+CORE gcc-only
+main.i
+--arch x86_64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 24 plain read-modify-write: FAILURE$
+^\[main\.assertion\.2\] line 25 locked increment: SUCCESS$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly
+--
+A lock-prefixed instruction is an atomic section on its own: making the whole
+statement atomic would hide the race on the plain read-modify-write of x.
diff --git a/regression/cbmc-concurrency/asm_x86_fences_sb/lfence.desc b/regression/cbmc-concurrency/asm_x86_fences_sb/lfence.desc
new file mode 100644
index 000000000000..a8ca00c62dee
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_x86_fences_sb/lfence.desc
@@ -0,0 +1,12 @@
+CORE
+main.c
+--mm tso -DLFENCE
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 43 store buffering: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+lfence only orders loads before later loads and stores. It used to be
+modeled as a full fence, which prevented store buffering.
diff --git a/regression/cbmc-concurrency/asm_x86_fences_sb/main.c b/regression/cbmc-concurrency/asm_x86_fences_sb/main.c
new file mode 100644
index 000000000000..2a8f3ee93ca8
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_x86_fences_sb/main.c
@@ -0,0 +1,45 @@
+// Store buffering: each thread stores to one variable and then loads the
+// other. On x86, a store may be delayed beyond a later load, unless a fence
+// that orders stores before later loads (mfence) separates them. lfence
+// and sfence do not, and x86 hardware exhibits r0 == 0 && r1 == 0 with
+// either of them.
+#if defined(LFENCE)
+# define FENCE "lfence"
+# define KINDS "RRfence", "RWfence"
+#elif defined(SFENCE)
+# define FENCE "sfence"
+# define KINDS "WWfence"
+#else
+# define FENCE "mfence"
+# define KINDS "WWfence", "RRfence", "RWfence", "WRfence"
+#endif
+
+int x, y, r0, r1, done;
+
+void t0(void)
+{
+ x = 1;
+#ifdef __GNUC__
+ __asm__ volatile(FENCE ::: "memory");
+#else
+ __CPROVER_fence(KINDS);
+#endif
+ r0 = y;
+ done = 1;
+}
+
+int main(void)
+{
+__CPROVER_ASYNC_1:
+ t0();
+ y = 1;
+#ifdef __GNUC__
+ __asm__ volatile(FENCE ::: "memory");
+#else
+ __CPROVER_fence(KINDS);
+#endif
+ r1 = x;
+ __CPROVER_assume(done);
+ __CPROVER_assert(!(r0 == 0 && r1 == 0), "store buffering");
+ return 0;
+}
diff --git a/regression/cbmc-concurrency/asm_x86_fences_sb/sfence.desc b/regression/cbmc-concurrency/asm_x86_fences_sb/sfence.desc
new file mode 100644
index 000000000000..f2047923fcaa
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_x86_fences_sb/sfence.desc
@@ -0,0 +1,12 @@
+CORE
+main.c
+--mm tso -DSFENCE
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 43 store buffering: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+sfence only orders stores before later stores. It used to be modeled as a
+full fence, which prevented store buffering.
diff --git a/regression/cbmc-concurrency/asm_x86_fences_sb/test.desc b/regression/cbmc-concurrency/asm_x86_fences_sb/test.desc
new file mode 100644
index 000000000000..159f8667959c
--- /dev/null
+++ b/regression/cbmc-concurrency/asm_x86_fences_sb/test.desc
@@ -0,0 +1,11 @@
+CORE
+main.c
+--mm tso
+^EXIT=0$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 43 store buffering: SUCCESS$
+^VERIFICATION SUCCESSFUL$
+--
+^warning: ignoring
+--
+mfence orders stores before later loads, so store buffering is prevented.
diff --git a/regression/cbmc-cpp/asm_gcc_operands/main.cpp b/regression/cbmc-cpp/asm_gcc_operands/main.cpp
new file mode 100644
index 000000000000..ed666266c13f
--- /dev/null
+++ b/regression/cbmc-cpp/asm_gcc_operands/main.cpp
@@ -0,0 +1,14 @@
+int main()
+{
+ // an output of an approximated statement
+ int out = 0;
+ asm volatile("movl $1, %0" : "=r"(out));
+ __CPROVER_assert(out == 0, "output of an approximated statement"); // fails
+
+ // an output tied to an input by a matching constraint
+ int x = 0, y = 5;
+ asm("" : "=r"(x) : "0"(y));
+ __CPROVER_assert(x == 5, "tied output"); // holds
+
+ return 0;
+}
diff --git a/regression/cbmc-cpp/asm_gcc_operands/test.desc b/regression/cbmc-cpp/asm_gcc_operands/test.desc
new file mode 100644
index 000000000000..7aecdf70100c
--- /dev/null
+++ b/regression/cbmc-cpp/asm_gcc_operands/test.desc
@@ -0,0 +1,15 @@
+KNOWNBUG gcc-only
+main.cpp
+
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 6 output of an approximated statement: FAILURE$
+^\[main\.assertion\.2\] line 11 tied output: SUCCESS$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+The C++ parser (rGCCAsmStatement in src/cpp/parse.cpp) parses the operands
+of gcc-style inline assembly, but discards them. remove_asm thus treats the
+statements as basic assembly: it neither writes out nor x, and CBMC proves
+out == 0, which fails with g++.
diff --git a/regression/cbmc-library/__asm_fnstcw/main.c b/regression/cbmc-library/__asm_fnstcw/main.c
index e06e67bee515..1d5f938536ca 100644
--- a/regression/cbmc-library/__asm_fnstcw/main.c
+++ b/regression/cbmc-library/__asm_fnstcw/main.c
@@ -1,9 +1,20 @@
#include
-#include
+#include
int main()
{
- __asm_fnstcw();
- assert(0);
- return 0;
+ unsigned short cw;
+#ifdef __GNUC__
+ __asm__("fnstcw %0" : "=m"(cw));
+#else
+ __asm { fnstcw cw; }
+#endif
+ assert((cw & 0xc00) == 0);
+ fesetround(FE_UPWARD);
+#ifdef __GNUC__
+ __asm__("fnstcw %0" : "=m"(cw));
+#else
+ __asm { fnstcw cw; }
+#endif
+ assert((cw & 0xc00) == 0x800);
}
diff --git a/regression/cbmc-library/__asm_fnstcw/test.desc b/regression/cbmc-library/__asm_fnstcw/test.desc
index 9542d988e8d5..96c9b4bcd7bc 100644
--- a/regression/cbmc-library/__asm_fnstcw/test.desc
+++ b/regression/cbmc-library/__asm_fnstcw/test.desc
@@ -1,4 +1,4 @@
-KNOWNBUG
+CORE
main.c
--pointer-check --bounds-check
^EXIT=0$
diff --git a/regression/cbmc-library/__asm_ldmxcsr/main.c b/regression/cbmc-library/__asm_ldmxcsr/main.c
new file mode 100644
index 000000000000..1777e89dc22a
--- /dev/null
+++ b/regression/cbmc-library/__asm_ldmxcsr/main.c
@@ -0,0 +1,10 @@
+#include
+#include
+
+int main()
+{
+ unsigned csr = 0x6000; // round toward zero
+ __asm__("ldmxcsr %0" : : "m"(csr));
+ assert(fegetround() == FE_TOWARDZERO);
+ return 0;
+}
diff --git a/regression/cbmc-library/__asm_ldmxcsr/test.desc b/regression/cbmc-library/__asm_ldmxcsr/test.desc
new file mode 100644
index 000000000000..28345e3383da
--- /dev/null
+++ b/regression/cbmc-library/__asm_ldmxcsr/test.desc
@@ -0,0 +1,8 @@
+CORE gcc-only
+main.c
+--pointer-check --bounds-check
+^EXIT=0$
+^SIGNAL=0$
+^VERIFICATION SUCCESSFUL$
+--
+^warning: ignoring
diff --git a/regression/cbmc-library/__asm_lfence/main.c b/regression/cbmc-library/__asm_lfence/main.c
index 6ebebf84c631..140c4ea48894 100644
--- a/regression/cbmc-library/__asm_lfence/main.c
+++ b/regression/cbmc-library/__asm_lfence/main.c
@@ -1,9 +1,5 @@
-#include
-#include
-
int main()
{
- __asm_lfence();
- assert(0);
+ __asm__ volatile("lfence");
return 0;
}
diff --git a/regression/cbmc-library/__asm_lfence/test.desc b/regression/cbmc-library/__asm_lfence/test.desc
index 9542d988e8d5..195cf69da630 100644
--- a/regression/cbmc-library/__asm_lfence/test.desc
+++ b/regression/cbmc-library/__asm_lfence/test.desc
@@ -1,8 +1,13 @@
-KNOWNBUG
+CORE gcc-only
main.c
---pointer-check --bounds-check
+--show-goto-functions
^EXIT=0$
^SIGNAL=0$
-^VERIFICATION SUCCESSFUL$
+^ *FENCE RR RW$
--
+^ *FENCE WW
+^ *FENCE .*WR
^warning: ignoring
+--
+lfence orders loads before later loads and stores, but it does not order
+stores before later accesses.
diff --git a/regression/cbmc-library/__asm_sfence/main.c b/regression/cbmc-library/__asm_sfence/main.c
index fefd01fb46e5..7cbec5d7f44a 100644
--- a/regression/cbmc-library/__asm_sfence/main.c
+++ b/regression/cbmc-library/__asm_sfence/main.c
@@ -1,9 +1,5 @@
-#include
-#include
-
int main()
{
- __asm_sfence();
- assert(0);
+ __asm__ volatile("sfence");
return 0;
}
diff --git a/regression/cbmc-library/__asm_sfence/test.desc b/regression/cbmc-library/__asm_sfence/test.desc
index 9542d988e8d5..0a30650280ed 100644
--- a/regression/cbmc-library/__asm_sfence/test.desc
+++ b/regression/cbmc-library/__asm_sfence/test.desc
@@ -1,8 +1,12 @@
-KNOWNBUG
+CORE gcc-only
main.c
---pointer-check --bounds-check
+--show-goto-functions
^EXIT=0$
^SIGNAL=0$
-^VERIFICATION SUCCESSFUL$
+^ *FENCE WW$
--
+^ *FENCE .*R
^warning: ignoring
+--
+sfence orders stores before later stores, but it does not order stores
+before later loads, nor loads before later accesses.
diff --git a/regression/cbmc-library/__asm_stmxcsr/main.c b/regression/cbmc-library/__asm_stmxcsr/main.c
new file mode 100644
index 000000000000..bf8b120380ae
--- /dev/null
+++ b/regression/cbmc-library/__asm_stmxcsr/main.c
@@ -0,0 +1,13 @@
+#include
+#include
+
+int main()
+{
+ unsigned csr;
+ __asm__("stmxcsr %0" : "=m"(csr));
+ assert((csr & 0x6000) == 0);
+ fesetround(FE_UPWARD);
+ __asm__("stmxcsr %0" : "=m"(csr));
+ assert((csr & 0x6000) == 0x4000);
+ return 0;
+}
diff --git a/regression/cbmc-library/__asm_stmxcsr/test.desc b/regression/cbmc-library/__asm_stmxcsr/test.desc
new file mode 100644
index 000000000000..28345e3383da
--- /dev/null
+++ b/regression/cbmc-library/__asm_stmxcsr/test.desc
@@ -0,0 +1,8 @@
+CORE gcc-only
+main.c
+--pointer-check --bounds-check
+^EXIT=0$
+^SIGNAL=0$
+^VERIFICATION SUCCESSFUL$
+--
+^warning: ignoring
diff --git a/regression/cbmc/asm_aarch64/main.c b/regression/cbmc/asm_aarch64/main.c
new file mode 100644
index 000000000000..432d19b3eea1
--- /dev/null
+++ b/regression/cbmc/asm_aarch64/main.c
@@ -0,0 +1,497 @@
+// AArch64 inline assembly that is interpreted exactly. The expected values were
+// observed with an emulator.
+
+// aarch64 ldxr/add/stxr set a bit (atomic_set llsc)
+void asm_case_0(unsigned long long *io)
+{
+ unsigned int m = (unsigned int)io[0];
+ unsigned int v = (unsigned int)io[1];
+ unsigned int tmp = (unsigned int)io[2];
+ unsigned int res = (unsigned int)io[3];
+ __asm__ __volatile__(
+ "1: ldxr %w0, [%2]\n"
+ " orr %w0, %w0, %w3\n"
+ " stxr %w1, %w0, [%2]\n"
+ " cbnz %w1, 1b\n"
+ : "=&r"(tmp), "=&r"(res)
+ : "r"(&m), "r"(v)
+ : "memory");
+ io[0] = (unsigned long long)m;
+ io[1] = (unsigned long long)v;
+ io[2] = (unsigned long long)tmp;
+ io[3] = (unsigned long long)res;
+}
+
+// aarch64 ldadd is an atomic add (LSE)
+void asm_case_1(unsigned long long *io)
+{
+ unsigned int m = (unsigned int)io[0];
+ unsigned int v = (unsigned int)io[1];
+ unsigned int old = (unsigned int)io[2];
+ __asm__ __volatile__("ldadd %w2, %w0, [%1]"
+ : "=r"(old)
+ : "r"(&m), "r"(v)
+ : "memory");
+ io[0] = (unsigned long long)m;
+ io[1] = (unsigned long long)v;
+ io[2] = (unsigned long long)old;
+}
+
+// aarch64 ldset ORs into memory and returns the old value (LSE)
+void asm_case_2(unsigned long long *io)
+{
+ unsigned long m = (unsigned long)io[0];
+ unsigned long v = (unsigned long)io[1];
+ unsigned long old = (unsigned long)io[2];
+ __asm__ __volatile__("ldset %2, %0, [%1]"
+ : "=r"(old)
+ : "r"(&m), "r"(v)
+ : "memory");
+ io[0] = (unsigned long long)m;
+ io[1] = (unsigned long long)v;
+ io[2] = (unsigned long long)old;
+}
+
+// aarch64 swp exchanges a register and memory (LSE)
+void asm_case_3(unsigned long long *io)
+{
+ unsigned int m = (unsigned int)io[0];
+ unsigned int v = (unsigned int)io[1];
+ unsigned int old = (unsigned int)io[2];
+ __asm__ __volatile__("swp %w2, %w0, [%1]"
+ : "=r"(old)
+ : "r"(&m), "r"(v)
+ : "memory");
+ io[0] = (unsigned long long)m;
+ io[1] = (unsigned long long)v;
+ io[2] = (unsigned long long)old;
+}
+
+// aarch64 cas compares and swaps (LSE)
+void asm_case_4(unsigned long long *io)
+{
+ unsigned int m = (unsigned int)io[0];
+ unsigned int cmp = (unsigned int)io[1];
+ unsigned int newv = (unsigned int)io[2];
+ __asm__ __volatile__("cas %w0, %w2, [%1]"
+ : "+r"(cmp)
+ : "r"(&m), "r"(newv)
+ : "memory");
+ io[0] = (unsigned long long)m;
+ io[1] = (unsigned long long)cmp;
+ io[2] = (unsigned long long)newv;
+}
+
+// aarch64 add then subtract
+void asm_case_5(unsigned long long *io)
+{
+ unsigned long r = (unsigned long)io[0];
+ unsigned long a = (unsigned long)io[1];
+ unsigned long b = (unsigned long)io[2];
+ unsigned long c = (unsigned long)io[3];
+ __asm__("add %0, %1, %2\n sub %0, %0, %3"
+ : "=&r"(r)
+ : "r"(a), "r"(b), "r"(c));
+ io[0] = (unsigned long long)r;
+ io[1] = (unsigned long long)a;
+ io[2] = (unsigned long long)b;
+ io[3] = (unsigned long long)c;
+}
+
+// aarch64 and with a shifted register
+void asm_case_6(unsigned long long *io)
+{
+ unsigned long r = (unsigned long)io[0];
+ unsigned long a = (unsigned long)io[1];
+ unsigned long b = (unsigned long)io[2];
+ __asm__("and %0, %1, %2, lsl #4" : "=r"(r) : "r"(a), "r"(b));
+ io[0] = (unsigned long long)r;
+ io[1] = (unsigned long long)a;
+ io[2] = (unsigned long long)b;
+}
+
+// aarch64 cmp and cset compute a predicate
+void asm_case_7(unsigned long long *io)
+{
+ unsigned long r = (unsigned long)io[0];
+ unsigned long a = (unsigned long)io[1];
+ unsigned long b = (unsigned long)io[2];
+ __asm__("cmp %1, %2\n cset %0, hi" : "=r"(r) : "r"(a), "r"(b) : "cc");
+ io[0] = (unsigned long long)r;
+ io[1] = (unsigned long long)a;
+ io[2] = (unsigned long long)b;
+}
+
+// aarch64 ldr and str move a word through memory
+void asm_case_8(unsigned long long *io)
+{
+ unsigned int a = (unsigned int)io[0];
+ unsigned int b = (unsigned int)io[1];
+ unsigned int t = (unsigned int)io[2];
+ __asm__ __volatile__("ldr %w0, [%1]\n str %w0, [%2]"
+ : "=&r"(t)
+ : "r"(&a), "r"(&b)
+ : "memory");
+ io[0] = (unsigned long long)a;
+ io[1] = (unsigned long long)b;
+ io[2] = (unsigned long long)t;
+}
+
+// aarch64 eor and orr combine bits
+void asm_case_9(unsigned long long *io)
+{
+ unsigned long r = (unsigned long)io[0];
+ unsigned long a = (unsigned long)io[1];
+ unsigned long b = (unsigned long)io[2];
+ unsigned long c = (unsigned long)io[3];
+ __asm__("eor %0, %1, %2\n orr %0, %0, %3"
+ : "=&r"(r)
+ : "r"(a), "r"(b), "r"(c));
+ io[0] = (unsigned long long)r;
+ io[1] = (unsigned long long)a;
+ io[2] = (unsigned long long)b;
+ io[3] = (unsigned long long)c;
+}
+
+// aarch64 lsl and lsr shift
+void asm_case_10(unsigned long long *io)
+{
+ unsigned long r = (unsigned long)io[0];
+ unsigned long a = (unsigned long)io[1];
+ unsigned long b = (unsigned long)io[2];
+ unsigned long c = (unsigned long)io[3];
+ __asm__("lsl %0, %1, %2\n lsr %0, %0, %3"
+ : "=&r"(r)
+ : "r"(a), "r"(b), "r"(c));
+ io[0] = (unsigned long long)r;
+ io[1] = (unsigned long long)a;
+ io[2] = (unsigned long long)b;
+ io[3] = (unsigned long long)c;
+}
+
+// aarch64 movz/movk build a constant
+void asm_case_11(unsigned long long *io)
+{
+ unsigned long r = (unsigned long)io[0];
+ __asm__("movz %0, #0x1234\n movk %0, #0xabcd, lsl #16" : "=r"(r));
+ io[0] = (unsigned long long)r;
+}
+
+int main(void)
+{
+ {
+ unsigned long long io[4] = {
+ 0xc2a7af9eab79b005ULL, 0xffULL, 0x2ULL, 0x6ffe6666fa72766dULL};
+ asm_case_0(io);
+ __CPROVER_assert(
+ io[0] == 0xab79b0ffULL && io[1] == 0xffULL && io[2] == 0xab79b0ffULL &&
+ io[3] == 0x0ULL,
+ "aarch64 ldxr/add/stxr set a bit (atomic_set llsc), vector 0");
+ }
+ {
+ unsigned long long io[4] = {
+ 0x8000000000000000ULL,
+ 0x8000000000000000ULL,
+ 0xae54d49633f89bafULL,
+ 0xcc5604a33a481fd2ULL};
+ asm_case_0(io);
+ __CPROVER_assert(
+ io[0] == 0x0ULL && io[1] == 0x0ULL && io[2] == 0x0ULL && io[3] == 0x0ULL,
+ "aarch64 ldxr/add/stxr set a bit (atomic_set llsc), vector 1");
+ }
+ {
+ unsigned long long io[4] = {
+ 0xadb4bded0a6cc878ULL,
+ 0x248a141c7ccb3b72ULL,
+ 0xb5d23309b6d2590dULL,
+ 0x33756584e354bbe7ULL};
+ asm_case_0(io);
+ __CPROVER_assert(
+ io[0] == 0x7eeffb7aULL && io[1] == 0x7ccb3b72ULL &&
+ io[2] == 0x7eeffb7aULL && io[3] == 0x0ULL,
+ "aarch64 ldxr/add/stxr set a bit (atomic_set llsc), vector 2");
+ }
+ {
+ unsigned long long io[3] = {
+ 0xc2b9546e0f0220f3ULL, 0xf1d5effedcda26fbULL, 0x7fffffffULL};
+ asm_case_1(io);
+ __CPROVER_assert(
+ io[0] == 0xebdc47eeULL && io[1] == 0xdcda26fbULL && io[2] == 0xf0220f3ULL,
+ "aarch64 ldadd is an atomic add (LSE), vector 0");
+ }
+ {
+ unsigned long long io[3] = {
+ 0xb8d3af23681c2889ULL, 0xc60f8fe0a6269527ULL, 0x32f6ffa49275dc84ULL};
+ asm_case_1(io);
+ __CPROVER_assert(
+ io[0] == 0xe42bdb0ULL && io[1] == 0xa6269527ULL && io[2] == 0x681c2889ULL,
+ "aarch64 ldadd is an atomic add (LSE), vector 1");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x7d7ef810cfc3dc1ULL, 0xbf30555ab55f5d1fULL, 0xb3d8c88ab7063e8dULL};
+ asm_case_1(io);
+ __CPROVER_assert(
+ io[0] == 0xc25b9ae0ULL && io[1] == 0xb55f5d1fULL && io[2] == 0xcfc3dc1ULL,
+ "aarch64 ldadd is an atomic add (LSE), vector 2");
+ }
+ {
+ unsigned long long io[3] = {0xe638bca46bd70d89ULL, 0x2ULL, 0x2ULL};
+ asm_case_2(io);
+ __CPROVER_assert(
+ io[0] == 0xe638bca46bd70d8bULL && io[1] == 0x2ULL &&
+ io[2] == 0xe638bca46bd70d89ULL,
+ "aarch64 ldset ORs into memory and returns the old value (LSE), vector "
+ "0");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x17d967b89f01d73dULL, 0x82974e236f761b03ULL, 0x7fULL};
+ asm_case_2(io);
+ __CPROVER_assert(
+ io[0] == 0x97df6fbbff77df3fULL && io[1] == 0x82974e236f761b03ULL &&
+ io[2] == 0x17d967b89f01d73dULL,
+ "aarch64 ldset ORs into memory and returns the old value (LSE), vector "
+ "1");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x6bcc7b9e0a546c01ULL, 0xffffffffULL, 0x9541937ec1bfc7abULL};
+ asm_case_2(io);
+ __CPROVER_assert(
+ io[0] == 0x6bcc7b9effffffffULL && io[1] == 0xffffffffULL &&
+ io[2] == 0x6bcc7b9e0a546c01ULL,
+ "aarch64 ldset ORs into memory and returns the old value (LSE), vector "
+ "2");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x38b1f9a35c2855baULL, 0x8a0cb201725c5338ULL, 0xb0652c5806a310a8ULL};
+ asm_case_3(io);
+ __CPROVER_assert(
+ io[0] == 0x725c5338ULL && io[1] == 0x725c5338ULL &&
+ io[2] == 0x5c2855baULL,
+ "aarch64 swp exchanges a register and memory (LSE), vector 0");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x808332e379bd40dbULL, 0xe586f6e1167ff12eULL, 0xffULL};
+ asm_case_3(io);
+ __CPROVER_assert(
+ io[0] == 0x167ff12eULL && io[1] == 0x167ff12eULL &&
+ io[2] == 0x79bd40dbULL,
+ "aarch64 swp exchanges a register and memory (LSE), vector 1");
+ }
+ {
+ unsigned long long io[3] = {
+ 0xb7a413d48183cf4eULL, 0x872808db18a381eULL, 0x89c40189aec7d882ULL};
+ asm_case_3(io);
+ __CPROVER_assert(
+ io[0] == 0xb18a381eULL && io[1] == 0xb18a381eULL &&
+ io[2] == 0x8183cf4eULL,
+ "aarch64 swp exchanges a register and memory (LSE), vector 2");
+ }
+ {
+ unsigned long long io[3] = {0x5ULL, 0x5ULL, 0x9ULL};
+ asm_case_4(io);
+ __CPROVER_assert(
+ io[0] == 0x9ULL && io[1] == 0x5ULL && io[2] == 0x9ULL,
+ "aarch64 cas compares and swaps (LSE), vector 0");
+ }
+ {
+ unsigned long long io[3] = {0x5ULL, 0x7ULL, 0x9ULL};
+ asm_case_4(io);
+ __CPROVER_assert(
+ io[0] == 0x5ULL && io[1] == 0x5ULL && io[2] == 0x9ULL,
+ "aarch64 cas compares and swaps (LSE), vector 1");
+ }
+ {
+ unsigned long long io[3] = {0x0ULL, 0x0ULL, 0x1ULL};
+ asm_case_4(io);
+ __CPROVER_assert(
+ io[0] == 0x1ULL && io[1] == 0x0ULL && io[2] == 0x1ULL,
+ "aarch64 cas compares and swaps (LSE), vector 2");
+ }
+ {
+ unsigned long long io[3] = {0xdeadbeefULL, 0xdeadbeefULL, 0x1ULL};
+ asm_case_4(io);
+ __CPROVER_assert(
+ io[0] == 0x1ULL && io[1] == 0xdeadbeefULL && io[2] == 0x1ULL,
+ "aarch64 cas compares and swaps (LSE), vector 3");
+ }
+ {
+ unsigned long long io[4] = {
+ 0x662e0001e6f57713ULL,
+ 0xf1657729e04a52fdULL,
+ 0x4ce92b19fe991d62ULL,
+ 0x2dca9d6ea967229bULL};
+ asm_case_5(io);
+ __CPROVER_assert(
+ io[0] == 0x108404d5357c4dc4ULL && io[1] == 0xf1657729e04a52fdULL &&
+ io[2] == 0x4ce92b19fe991d62ULL && io[3] == 0x2dca9d6ea967229bULL,
+ "aarch64 add then subtract, vector 0");
+ }
+ {
+ unsigned long long io[4] = {
+ 0xe4a66e6e92989e7dULL,
+ 0x350b8f5dcea6fb83ULL,
+ 0xabc0ff65a92da05dULL,
+ 0x4c075f1f0b493f1aULL};
+ asm_case_5(io);
+ __CPROVER_assert(
+ io[0] == 0x94c52fa46c8b5cc6ULL && io[1] == 0x350b8f5dcea6fb83ULL &&
+ io[2] == 0xabc0ff65a92da05dULL && io[3] == 0x4c075f1f0b493f1aULL,
+ "aarch64 add then subtract, vector 1");
+ }
+ {
+ unsigned long long io[4] = {
+ 0x7d9cf46457162242ULL, 0x7fULL, 0x8000000000000000ULL, 0x80000000ULL};
+ asm_case_5(io);
+ __CPROVER_assert(
+ io[0] == 0x7fffffff8000007fULL && io[1] == 0x7fULL &&
+ io[2] == 0x8000000000000000ULL && io[3] == 0x80000000ULL,
+ "aarch64 add then subtract, vector 2");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x71985eb13a883919ULL, 0x7fffffffffffffffULL, 0xc42192ad002d93dcULL};
+ asm_case_6(io);
+ __CPROVER_assert(
+ io[0] == 0x42192ad002d93dc0ULL && io[1] == 0x7fffffffffffffffULL &&
+ io[2] == 0xc42192ad002d93dcULL,
+ "aarch64 and with a shifted register, vector 0");
+ }
+ {
+ unsigned long long io[3] = {
+ 0xffffffffULL, 0x4f18c672fa42fe7cULL, 0x120984629605b44fULL};
+ asm_case_6(io);
+ __CPROVER_assert(
+ io[0] == 0x18462060424470ULL && io[1] == 0x4f18c672fa42fe7cULL &&
+ io[2] == 0x120984629605b44fULL,
+ "aarch64 and with a shifted register, vector 1");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x4eb832d5079a2474ULL, 0x0ULL, 0xfd64b9e5bd0edb5aULL};
+ asm_case_6(io);
+ __CPROVER_assert(
+ io[0] == 0x0ULL && io[1] == 0x0ULL && io[2] == 0xfd64b9e5bd0edb5aULL,
+ "aarch64 and with a shifted register, vector 2");
+ }
+ {
+ unsigned long long io[3] = {
+ 0xd6bc0d35ccec83e4ULL, 0x4fb0d79be3130b3fULL, 0x2d65a8a1cf4ce48eULL};
+ asm_case_7(io);
+ __CPROVER_assert(
+ io[0] == 0x1ULL && io[1] == 0x4fb0d79be3130b3fULL &&
+ io[2] == 0x2d65a8a1cf4ce48eULL,
+ "aarch64 cmp and cset compute a predicate, vector 0");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x0ULL, 0xdae98f58edaf5ddULL, 0xbb5f3ec2488a958cULL};
+ asm_case_7(io);
+ __CPROVER_assert(
+ io[0] == 0x0ULL && io[1] == 0xdae98f58edaf5ddULL &&
+ io[2] == 0xbb5f3ec2488a958cULL,
+ "aarch64 cmp and cset compute a predicate, vector 1");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x8bcb7f1085e0c86ULL, 0x3dc7152f30428c31ULL, 0x12257820e6b755cfULL};
+ asm_case_7(io);
+ __CPROVER_assert(
+ io[0] == 0x1ULL && io[1] == 0x3dc7152f30428c31ULL &&
+ io[2] == 0x12257820e6b755cfULL,
+ "aarch64 cmp and cset compute a predicate, vector 2");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x71f2223ff559a0f9ULL, 0xc447a6fbb54b9c52ULL, 0xf34de6440ce23540ULL};
+ asm_case_8(io);
+ __CPROVER_assert(
+ io[0] == 0xf559a0f9ULL && io[1] == 0xf559a0f9ULL &&
+ io[2] == 0xf559a0f9ULL,
+ "aarch64 ldr and str move a word through memory, vector 0");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x2444fb01d3bdc021ULL, 0xfc66ce02975f8fb3ULL, 0xffULL};
+ asm_case_8(io);
+ __CPROVER_assert(
+ io[0] == 0xd3bdc021ULL && io[1] == 0xd3bdc021ULL &&
+ io[2] == 0xd3bdc021ULL,
+ "aarch64 ldr and str move a word through memory, vector 1");
+ }
+ {
+ unsigned long long io[3] = {
+ 0xca7d4a778a0b60a5ULL, 0x38e3ee6142044392ULL, 0xa67f5e58956140eULL};
+ asm_case_8(io);
+ __CPROVER_assert(
+ io[0] == 0x8a0b60a5ULL && io[1] == 0x8a0b60a5ULL &&
+ io[2] == 0x8a0b60a5ULL,
+ "aarch64 ldr and str move a word through memory, vector 2");
+ }
+ {
+ unsigned long long io[4] = {
+ 0x80000000ULL, 0x85c6eecdbd37c64dULL, 0xe051fbafdcfbbb5eULL, 0x7fULL};
+ asm_case_9(io);
+ __CPROVER_assert(
+ io[0] == 0x6597156261cc7d7fULL && io[1] == 0x85c6eecdbd37c64dULL &&
+ io[2] == 0xe051fbafdcfbbb5eULL && io[3] == 0x7fULL,
+ "aarch64 eor and orr combine bits, vector 0");
+ }
+ {
+ unsigned long long io[4] = {
+ 0x1ULL, 0x7fULL, 0x7e0269edac79ca6eULL, 0xffffffffffffffffULL};
+ asm_case_9(io);
+ __CPROVER_assert(
+ io[0] == 0xffffffffffffffffULL && io[1] == 0x7fULL &&
+ io[2] == 0x7e0269edac79ca6eULL && io[3] == 0xffffffffffffffffULL,
+ "aarch64 eor and orr combine bits, vector 1");
+ }
+ {
+ unsigned long long io[4] = {
+ 0xffULL,
+ 0xfc5d96cc82bdb472ULL,
+ 0x7fffffffffffffffULL,
+ 0x2355aca2a0edf46dULL};
+ asm_case_9(io);
+ __CPROVER_assert(
+ io[0] == 0xa3f7edb3fdefffedULL && io[1] == 0xfc5d96cc82bdb472ULL &&
+ io[2] == 0x7fffffffffffffffULL && io[3] == 0x2355aca2a0edf46dULL,
+ "aarch64 eor and orr combine bits, vector 2");
+ }
+ {
+ unsigned long long io[4] = {0xd29b8652db47179aULL, 0x1ULL, 0x3ULL, 0x3fULL};
+ asm_case_10(io);
+ __CPROVER_assert(
+ io[0] == 0x0ULL && io[1] == 0x1ULL && io[2] == 0x3ULL && io[3] == 0x3fULL,
+ "aarch64 lsl and lsr shift, vector 0");
+ }
+ {
+ unsigned long long io[4] = {
+ 0xd57e59721b5c3f00ULL, 0x8161ebbba4c1b5daULL, 0x1aULL, 0x24ULL};
+ asm_case_10(io);
+ __CPROVER_assert(
+ io[0] == 0xee9306dULL && io[1] == 0x8161ebbba4c1b5daULL &&
+ io[2] == 0x1aULL && io[3] == 0x24ULL,
+ "aarch64 lsl and lsr shift, vector 1");
+ }
+ {
+ unsigned long long io[4] = {
+ 0x5e4c1de7ba5f33e2ULL, 0x870cf5c903adeee9ULL, 0x2dULL, 0x3eULL};
+ asm_case_10(io);
+ __CPROVER_assert(
+ io[0] == 0x2ULL && io[1] == 0x870cf5c903adeee9ULL && io[2] == 0x2dULL &&
+ io[3] == 0x3eULL,
+ "aarch64 lsl and lsr shift, vector 2");
+ }
+ {
+ unsigned long long io[1] = {0x6a08e48807e06ce6ULL};
+ asm_case_11(io);
+ __CPROVER_assert(
+ io[0] == 0xabcd1234ULL, "aarch64 movz/movk build a constant, vector 0");
+ }
+ return 0;
+}
diff --git a/regression/cbmc/asm_aarch64/test.desc b/regression/cbmc/asm_aarch64/test.desc
new file mode 100644
index 000000000000..86f8be6dbc81
--- /dev/null
+++ b/regression/cbmc/asm_aarch64/test.desc
@@ -0,0 +1,15 @@
+CORE gcc-only
+main.c
+--arch arm64 --unwind 2 --no-unwinding-assertions
+^EXIT=0$
+^SIGNAL=0$
+^VERIFICATION SUCCESSFUL$
+--
+^warning: ignoring
+approximating inline assembly
+--
+The AArch64 inline assembly in main.c is interpreted exactly: if CBMC
+approximated any statement, the assertions, which check values observed with
+an emulator, would fail. A store-exclusive may fail spuriously, so the retry
+loops of load-exclusive/store-exclusive sequences are bounded; the executions
+that retry more often reach the same states.
diff --git a/regression/cbmc/asm_aarch64_alu/main.c b/regression/cbmc/asm_aarch64_alu/main.c
new file mode 100644
index 000000000000..af2fd57bb9a6
--- /dev/null
+++ b/regression/cbmc/asm_aarch64_alu/main.c
@@ -0,0 +1,451 @@
+// AArch64 data processing, condition codes, and branches, each compared with
+// a C reference on concrete inputs. All assertions hold on qemu-aarch64.
+typedef unsigned long u64;
+typedef unsigned int u32;
+
+static u64 mk_nzcv(int n, int z, int c, int v)
+{
+ return ((u64)n << 31) | ((u64)z << 30) | ((u64)c << 29) | ((u64)v << 28);
+}
+
+static u64 mask_of(int w)
+{
+ return w == 64 ? ~0ul : ((1ul << w) - 1);
+}
+
+// the flags computed by the last call of ref_add, ref_sub, or ref_logic
+static u64 ref_flags;
+
+// a + b in w bits, with the flags of adds
+static u64 ref_add(u64 a, u64 b, int w)
+{
+ u64 m = mask_of(w);
+ a &= m;
+ b &= m;
+ u64 r = (a + b) & m;
+ int c = r < a;
+ int v = (((a ^ r) & (b ^ r)) >> (w - 1)) & 1;
+ ref_flags = mk_nzcv((r >> (w - 1)) & 1, r == 0, c, v);
+ return r;
+}
+
+// a - b in w bits, with the flags of subs
+static u64 ref_sub(u64 a, u64 b, int w)
+{
+ u64 m = mask_of(w);
+ a &= m;
+ b &= m;
+ u64 r = (a - b) & m;
+ int c = a >= b;
+ int v = (((a ^ b) & (a ^ r)) >> (w - 1)) & 1;
+ ref_flags = mk_nzcv((r >> (w - 1)) & 1, r == 0, c, v);
+ return r;
+}
+
+static u64 ref_logic(u64 r, int w)
+{
+ r &= mask_of(w);
+ ref_flags = mk_nzcv((r >> (w - 1)) & 1, r == 0, 0, 0);
+ return r;
+}
+
+// the flags read by mrs after the last FLAGOP function
+static u64 asm_flags;
+
+// the result and the flags (via mrs) of an instruction
+#define FLAGOP(name, insn) \
+ static u64 name(u64 a, u64 b) \
+ { \
+ u64 r, g; \
+ __asm__(insn "\n mrs %1, nzcv" \
+ : "=&r"(r), "=&r"(g) \
+ : "r"(a), "r"(b) \
+ : "cc"); \
+ asm_flags = g; \
+ return r; \
+ }
+
+FLAGOP(adds_x, "adds %0, %2, %3")
+FLAGOP(adds_w, "adds %w0, %w2, %w3")
+FLAGOP(subs_x, "subs %0, %2, %3")
+FLAGOP(subs_w, "subs %w0, %w2, %w3")
+FLAGOP(adds_x_lsl, "adds %0, %2, %3, lsl #7")
+FLAGOP(subs_w_asr, "subs %w0, %w2, %w3, asr #5")
+FLAGOP(subs_imm, "subs %w0, %w2, #4095")
+FLAGOP(adds_imm_neg, "adds %w0, %w2, #-1")
+FLAGOP(cmp_x, "cmp %2, %3\n mov %0, #0")
+FLAGOP(cmp_w, "cmp %w2, %w3\n mov %0, #0")
+FLAGOP(cmp_imm_neg, "cmp %2, #-5\n mov %0, #0")
+FLAGOP(cmn_x, "cmn %2, %3\n mov %0, #0")
+FLAGOP(cmn_w, "cmn %w2, %w3\n mov %0, #0")
+FLAGOP(negs_x, "negs %0, %3")
+FLAGOP(negs_w, "negs %w0, %w3")
+FLAGOP(ands_w, "ands %w0, %w2, %w3, lsl #3")
+FLAGOP(bics_x, "bics %0, %2, %3")
+FLAGOP(tst_x, "tst %2, %3\n mov %0, #0")
+
+#define CHECKFLAG(name, refexpr) \
+ do \
+ { \
+ u64 r1 = name(a, b); \
+ u64 r2 = refexpr; \
+ __CPROVER_assert(r1 == r2 && asm_flags == ref_flags, #name); \
+ } while(0)
+
+static void check_flags(u64 a, u64 b)
+{
+ CHECKFLAG(adds_x, ref_add(a, b, 64));
+ CHECKFLAG(adds_w, ref_add(a, b, 32));
+ CHECKFLAG(subs_x, ref_sub(a, b, 64));
+ CHECKFLAG(subs_w, ref_sub(a, b, 32));
+ CHECKFLAG(adds_x_lsl, ref_add(a, b << 7, 64));
+ CHECKFLAG(subs_w_asr, ref_sub(a, (u64)(u32)((int)(u32)b >> 5), 32));
+ CHECKFLAG(subs_imm, ref_sub(a, 4095, 32));
+ CHECKFLAG(adds_imm_neg, ref_add(a, 0xffffffff, 32));
+ CHECKFLAG(cmp_x, (ref_sub(a, b, 64), 0));
+ CHECKFLAG(cmp_w, (ref_sub(a, b, 32), 0));
+ CHECKFLAG(cmp_imm_neg, (ref_add(a, 5, 64), 0));
+ CHECKFLAG(cmn_x, (ref_add(a, b, 64), 0));
+ CHECKFLAG(cmn_w, (ref_add(a, b, 32), 0));
+ CHECKFLAG(negs_x, ref_sub(0, b, 64));
+ CHECKFLAG(negs_w, ref_sub(0, b, 32));
+ CHECKFLAG(ands_w, ref_logic(a & (b << 3), 32));
+ CHECKFLAG(bics_x, ref_logic(a & ~b, 64));
+ CHECKFLAG(tst_x, (ref_logic(a & b, 64), 0));
+}
+
+// condition codes: cset after cmp, csetm after cmn, b.cond, and flag outputs
+static int ref_cond(int cc, u64 f)
+{
+ int n = (f >> 31) & 1, z = (f >> 30) & 1, c = (f >> 29) & 1,
+ v = (f >> 28) & 1;
+ switch(cc)
+ {
+ case 0:
+ return z;
+ case 1:
+ return !z;
+ case 2:
+ return c;
+ case 3:
+ return !c;
+ case 4:
+ return n;
+ case 5:
+ return !n;
+ case 6:
+ return v;
+ case 7:
+ return !v;
+ case 8:
+ return c && !z;
+ case 9:
+ return !c || z;
+ case 10:
+ return n == v;
+ case 11:
+ return n != v;
+ case 12:
+ return !z && n == v;
+ default:
+ return z || n != v;
+ }
+}
+
+#define COND(cc, idx) \
+ static u64 cset_##cc(u64 a, u64 b) \
+ { \
+ u64 r; \
+ __asm__("cmp %1, %2\n cset %0, " #cc : "=r"(r) : "r"(a), "r"(b) : "cc"); \
+ return r; \
+ } \
+ static u64 csetm_##cc(u64 a, u64 b) \
+ { \
+ u64 r; \
+ __asm__("cmn %w1, %w2\n csetm %w0, " #cc \
+ : "=r"(r) \
+ : "r"(a), "r"(b) \
+ : "cc"); \
+ return r; \
+ } \
+ static u64 bcc_##cc(u64 a, u64 b) \
+ { \
+ u64 r; \
+ __asm__("subs %0, %1, %2\n mov %0, #1\n b." #cc " 1f\n mov %0, #0\n1:" \
+ : "=&r"(r) \
+ : "r"(a), "r"(b) \
+ : "cc"); \
+ return r; \
+ } \
+ static int flag_output_##cc(u64 a, u64 b) \
+ { \
+ int r; \
+ __asm__("cmp %1, %2" : "=@cc" #cc(r) : "r"(a), "r"(b)); \
+ return r; \
+ } \
+ static void check_cond_##cc(u64 a, u64 b) \
+ { \
+ ref_sub(a, b, 64); \
+ u64 f = ref_flags; \
+ ref_add(a, b, 32); \
+ u64 g = ref_flags; \
+ __CPROVER_assert(cset_##cc(a, b) == (u64)ref_cond(idx, f), "cset " #cc); \
+ __CPROVER_assert( \
+ csetm_##cc(a, b) == (ref_cond(idx, g) ? 0xfffffffful : 0), \
+ "csetm " #cc); \
+ __CPROVER_assert(bcc_##cc(a, b) == (u64)ref_cond(idx, f), "b." #cc); \
+ __CPROVER_assert(flag_output_##cc(a, b) == ref_cond(idx, f), "@cc" #cc); \
+ }
+
+COND(eq, 0)
+COND(ne, 1)
+COND(hs, 2)
+COND(lo, 3)
+COND(mi, 4)
+COND(pl, 5)
+COND(vs, 6)
+COND(vc, 7)
+COND(hi, 8)
+COND(ls, 9)
+COND(ge, 10)
+COND(lt, 11)
+COND(gt, 12)
+COND(le, 13)
+
+static void check_conds(u64 a, u64 b)
+{
+ check_cond_eq(a, b);
+ check_cond_ne(a, b);
+ check_cond_hs(a, b);
+ check_cond_lo(a, b);
+ check_cond_mi(a, b);
+ check_cond_pl(a, b);
+ check_cond_vs(a, b);
+ check_cond_vc(a, b);
+ check_cond_hi(a, b);
+ check_cond_ls(a, b);
+ check_cond_ge(a, b);
+ check_cond_lt(a, b);
+ check_cond_gt(a, b);
+ check_cond_le(a, b);
+}
+
+// conditional selects
+#define CSELOP(name, insn) \
+ static u64 name(u64 a, u64 b, u64 c, u64 d) \
+ { \
+ u64 r; \
+ __asm__("cmp %1, %2\n" insn \
+ : "=&r"(r) \
+ : "r"(a), "r"(b), "r"(c), "r"(d) \
+ : "cc"); \
+ return r; \
+ }
+
+CSELOP(csel_gt, "csel %0, %3, %4, gt")
+CSELOP(csinc_lt, "csinc %0, %3, %4, lt")
+CSELOP(csinv_w_hi, "csinv %w0, %w3, %w4, hi")
+CSELOP(csneg_ls, "csneg %0, %3, %4, ls")
+CSELOP(csneg_w_eq, "csneg %w0, %w3, %w4, eq")
+CSELOP(csinc_nv, "csinc %0, %3, %4, nv")
+CSELOP(csetm_x_vs, "csetm %0, vs")
+
+static void check_csel(u64 a, u64 b, u64 c, u64 d)
+{
+ ref_sub(a, b, 64);
+ u64 f = ref_flags;
+ __CPROVER_assert(csel_gt(a, b, c, d) == (ref_cond(12, f) ? c : d), "csel gt");
+ __CPROVER_assert(
+ csinc_lt(a, b, c, d) == (ref_cond(11, f) ? c : d + 1), "csinc lt");
+ __CPROVER_assert(
+ csinv_w_hi(a, b, c, d) == (u64)(u32)(ref_cond(8, f) ? c : ~d),
+ "csinv w hi");
+ __CPROVER_assert(
+ csneg_ls(a, b, c, d) == (ref_cond(9, f) ? c : -d), "csneg ls");
+ __CPROVER_assert(
+ csneg_w_eq(a, b, c, d) == (u64)(u32)(ref_cond(0, f) ? c : -d),
+ "csneg w eq");
+ __CPROVER_assert(csinc_nv(a, b, c, d) == c, "csinc nv");
+ __CPROVER_assert(
+ csetm_x_vs(a, b, c, d) == (ref_cond(6, f) ? ~0ul : 0), "csetm x vs");
+}
+
+// shifts, rotates, and moves
+#define BINOP(name, insn) \
+ static u64 name(u64 a, u64 b) \
+ { \
+ u64 r; \
+ __asm__(insn : "=&r"(r) : "r"(a), "r"(b)); \
+ return r; \
+ }
+
+BINOP(lsl_x, "lsl %0, %1, %2")
+BINOP(lsr_x, "lsr %0, %1, %2")
+BINOP(asr_x, "asr %0, %1, %2")
+BINOP(ror_x, "ror %0, %1, %2")
+BINOP(lsl_w, "lsl %w0, %w1, %w2")
+BINOP(lsr_w, "lsr %w0, %w1, %w2")
+BINOP(asr_w, "asr %w0, %w1, %w2")
+BINOP(ror_w, "ror %w0, %w1, %w2")
+BINOP(ror_x_imm, "ror %0, %1, #13")
+BINOP(ror_w_imm, "ror %w0, %w1, #31")
+BINOP(asr_x_imm, "asr %0, %1, #63")
+BINOP(asr_w_imm, "asr %w0, %w1, #7")
+BINOP(orn_x, "orn %0, %1, %2, lsr #3")
+BINOP(bic_w, "bic %w0, %w1, %w2, asr #2")
+BINOP(mvn_w, "mvn %w0, %w1")
+BINOP(mvn_x, "mvn %0, %2")
+BINOP(neg_x, "neg %0, %1")
+BINOP(neg_w, "neg %w0, %w1")
+BINOP(movk_x, "mov %0, %1\n movk %0, #0xbeef, lsl #32")
+BINOP(movn_x, "movn %0, #0x1234, lsl #48")
+BINOP(movn_w, "movn %w0, #0x1234, lsl #16")
+BINOP(movz_w, "mov %0, %1\n movz %w0, #0xffff")
+
+static u64 ror64(u64 x, unsigned n)
+{
+ n &= 63;
+ return n ? (x >> n) | (x << (64 - n)) : x;
+}
+
+static u32 ror32(u32 x, unsigned n)
+{
+ n &= 31;
+ return n ? (x >> n) | (x << (32 - n)) : x;
+}
+
+static void check_shifts(u64 a, u64 b)
+{
+ __CPROVER_assert(lsl_x(a, b) == a << (b & 63), "lsl x");
+ __CPROVER_assert(lsr_x(a, b) == a >> (b & 63), "lsr x");
+ __CPROVER_assert(asr_x(a, b) == (u64)((long)a >> (b & 63)), "asr x");
+ __CPROVER_assert(ror_x(a, b) == ror64(a, b), "ror x");
+ __CPROVER_assert(lsl_w(a, b) == (u64)(u32)((u32)a << (b & 31)), "lsl w");
+ __CPROVER_assert(lsr_w(a, b) == (u64)((u32)a >> (b & 31)), "lsr w");
+ __CPROVER_assert(asr_w(a, b) == (u64)(u32)((int)(u32)a >> (b & 31)), "asr w");
+ __CPROVER_assert(ror_w(a, b) == (u64)ror32((u32)a, b), "ror w");
+ __CPROVER_assert(ror_x_imm(a, b) == ror64(a, 13), "ror x imm");
+ __CPROVER_assert(ror_w_imm(a, b) == (u64)ror32((u32)a, 31), "ror w imm");
+ __CPROVER_assert(asr_x_imm(a, b) == (u64)((long)a >> 63), "asr x imm");
+ __CPROVER_assert(
+ asr_w_imm(a, b) == (u64)(u32)((int)(u32)a >> 7), "asr w imm");
+ __CPROVER_assert(orn_x(a, b) == (a | ~(b >> 3)), "orn x");
+ __CPROVER_assert(
+ bic_w(a, b) == (u64)(u32)(a & ~(u32)((int)(u32)b >> 2)), "bic w");
+ __CPROVER_assert(mvn_w(a, b) == (u64)(u32)~a, "mvn w");
+ __CPROVER_assert(mvn_x(a, b) == ~b, "mvn x");
+ __CPROVER_assert(neg_x(a, b) == -a, "neg x");
+ __CPROVER_assert(neg_w(a, b) == (u64)(u32)-a, "neg w");
+ __CPROVER_assert(
+ movk_x(a, b) == ((a & ~0xffff00000000ul) | 0xbeef00000000ul), "movk x");
+ __CPROVER_assert(movn_x(a, b) == ~0x1234000000000000ul, "movn x");
+ __CPROVER_assert(movn_w(a, b) == (u64)(u32)~0x12340000u, "movn w");
+ __CPROVER_assert(movz_w(a, b) == 0xffff, "movz w");
+}
+
+// branches on registers
+#define BRANCH(name, insn) \
+ static u64 name(u64 a) \
+ { \
+ u64 r; \
+ __asm__("mov %0, #1\n " insn " 1f\n mov %0, #0\n1:" : "=&r"(r) : "r"(a)); \
+ return r; \
+ }
+
+BRANCH(cbz_w, "cbz %w1,")
+BRANCH(cbnz_x, "cbnz %1,")
+BRANCH(tbz_63, "tbz %1, #63,")
+BRANCH(tbnz_31, "tbnz %w1, #31,")
+BRANCH(tbnz_40, "tbnz %1, #40,")
+BRANCH(tbz_0, "tbz %w1, #0,")
+
+static void check_branches(u64 a)
+{
+ __CPROVER_assert(cbz_w(a) == ((u32)a == 0), "cbz w");
+ __CPROVER_assert(cbnz_x(a) == (a != 0), "cbnz x");
+ __CPROVER_assert(tbz_63(a) == !((a >> 63) & 1), "tbz 63");
+ __CPROVER_assert(tbnz_31(a) == ((a >> 31) & 1), "tbnz 31");
+ __CPROVER_assert(tbnz_40(a) == ((a >> 40) & 1), "tbnz 40");
+ __CPROVER_assert(tbz_0(a) == !(a & 1), "tbz 0");
+}
+
+// asm goto
+static int lt_signed(u64 a, u64 b)
+{
+ __asm__ goto("cmp %0, %1\n b.lt %l[yes]" : : "r"(a), "r"(b) : "cc" : yes);
+ return 0;
+yes:
+ return 1;
+}
+
+static int classify(u64 a)
+{
+ __asm__ goto("cbz %0, %l[zero]\n tbnz %0, #63, %l[negative]"
+ :
+ : "r"(a)
+ :
+ : zero, negative);
+ return 2;
+zero:
+ return 0;
+negative:
+ return 1;
+}
+
+static void check_asm_goto(u64 a, u64 b)
+{
+ __CPROVER_assert(lt_signed(a, b) == ((long)a < (long)b), "b.lt to C label");
+ __CPROVER_assert(
+ classify(a) == (a == 0 ? 0
+ : (long)a < 0 ? 1
+ : 2),
+ "cbz, tbnz to C labels");
+}
+
+int main(void)
+{
+ // carry, overflow, zero, and negative results in 64 and 32 bits
+ check_flags(5, 7);
+ check_flags(0x7ffffffffffffffful, 1);
+ check_flags(0xfffffffffffffffful, 1);
+ check_flags(0x8000000000000000ul, 0x8000000000000000ul);
+ check_flags(0, 1);
+ check_flags(0, 0);
+ check_flags(1, 0x8000000000000000ul);
+ check_flags(0x7fffffff, 1);
+ check_flags(0xffffffff, 0x80000001);
+ check_flags(0x80000000, 1);
+ check_flags(0xfffffffffffffffbul, 0x00000000fffffffful);
+
+ // each condition holds for some of the inputs and fails for others
+ check_conds(5, 7);
+ check_conds(7, 5);
+ check_conds(5, 5);
+ check_conds(0x8000000000000000ul, 1);
+ check_conds(1, 0x8000000000000000ul);
+ check_conds(0x7fffffff, 1);
+
+ check_csel(5, 7, 0x1234, 0x8000000000000001ul);
+ check_csel(7, 5, 0x1234, 0x8000000000000001ul);
+ check_csel(5, 5, 0x1234, 0x8000000000000001ul);
+ check_csel(0x8000000000000000ul, 1, 0xfffffffffffffffful, 3);
+ check_csel(1, 0x8000000000000000ul, 0xfffffffffffffffful, 3);
+
+ check_shifts(0x8000000000000001ul, 0);
+ check_shifts(0xf0f0f0f08f0f0f0ful, 1);
+ check_shifts(0xf0f0f0f08f0f0f0ful, 31);
+ check_shifts(0x8000000000000001ul, 32);
+ check_shifts(0x7ffffffffffffffful, 63);
+ check_shifts(0x123456789abcdef0ul, 65);
+
+ check_branches(0);
+ check_branches(0x8000000000000000ul);
+ check_branches(0x80000000);
+ check_branches(0x10000000001ul);
+
+ check_asm_goto(0, 1);
+ check_asm_goto(0x8000000000000000ul, 0x7ffffffffffffffful);
+ check_asm_goto(5, 3);
+
+ return 0;
+}
diff --git a/regression/cbmc/asm_aarch64_alu/test.desc b/regression/cbmc/asm_aarch64_alu/test.desc
new file mode 100644
index 000000000000..7e8f73a76f99
--- /dev/null
+++ b/regression/cbmc/asm_aarch64_alu/test.desc
@@ -0,0 +1,17 @@
+CORE gcc-only
+main.c
+--arch arm64
+^EXIT=0$
+^SIGNAL=0$
+^VERIFICATION SUCCESSFUL$
+--
+^warning: ignoring
+approximating inline assembly
+--
+Covers the C and V flags of adds, subs, cmp, cmn, and negs, all condition
+codes (cset, csetm, b.cond, =@cc outputs), the conditional selects, shifts
+and rotates, mvn, movn, movk, movz, neg, cbz, cbnz, tbz, tbnz, and asm goto.
+The assertions compare each statement with a C reference; they all hold when
+main.c is compiled with clang -O1 --target=aarch64-linux-gnu -march=armv8.4-a
+(with __CPROVER_assert defined to check its condition) and run on
+qemu-aarch64 -cpu max.
diff --git a/regression/cbmc/asm_aarch64_clrex_branch/main.c b/regression/cbmc/asm_aarch64_clrex_branch/main.c
new file mode 100644
index 000000000000..325d3e34980e
--- /dev/null
+++ b/regression/cbmc/asm_aarch64_clrex_branch/main.c
@@ -0,0 +1,25 @@
+// Whether the exclusive monitor is cleared depends on the path through the
+// statement: a store-exclusive after a clrex that a branch skips may succeed.
+// "fails" marks properties that are violated on hardware, so CBMC must not
+// prove them.
+unsigned long m = 1;
+
+int main(void)
+{
+ unsigned long t;
+ unsigned int status;
+ unsigned long skip = 1;
+
+ // sets the exclusive monitor
+ __asm__ volatile("ldxr %0, [%1]" : "=r"(t) : "r"(&m) : "memory");
+
+ // the branch skips the clrex
+ __asm__ volatile("cbnz %3, 1f\n clrex\n1: stxr %w0, %2, [%1]"
+ : "=&r"(status)
+ : "r"(&m), "r"(42ul), "r"(skip)
+ : "memory");
+ __CPROVER_assert(status == 1 && m == 1, "skipped clrex"); // fails
+ __CPROVER_assert((status == 0) == (m == 42), "status matches the store");
+
+ return 0;
+}
diff --git a/regression/cbmc/asm_aarch64_clrex_branch/test.desc b/regression/cbmc/asm_aarch64_clrex_branch/test.desc
new file mode 100644
index 000000000000..09e90813e120
--- /dev/null
+++ b/regression/cbmc/asm_aarch64_clrex_branch/test.desc
@@ -0,0 +1,15 @@
+CORE gcc-only
+main.c
+--arch arm64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 21 skipped clrex: FAILURE$
+^\[main\.assertion\.2\] line 22 status matches the store: SUCCESS$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly
+--
+On qemu-aarch64, the store-exclusive succeeds (status 0, m is 42). CBMC takes
+the exclusive monitor to be cleared only if it is cleared on all paths: after
+a label, a store-exclusive succeeds or fails nondeterministically.
diff --git a/regression/cbmc/asm_aarch64_corner_cases/main.c b/regression/cbmc/asm_aarch64_corner_cases/main.c
new file mode 100644
index 000000000000..82c1755f7bf2
--- /dev/null
+++ b/regression/cbmc/asm_aarch64_corner_cases/main.c
@@ -0,0 +1,63 @@
+// Corner cases of the AArch64 instruction semantics. "fails" marks
+// properties that the architecture permits to be violated, so CBMC must not
+// prove them.
+static unsigned long add_ll_sc(unsigned long *p, unsigned long v)
+{
+ unsigned long result;
+ unsigned int status;
+ __asm__ volatile(
+ "1: ldxr %0, [%2]\n"
+ " add %0, %0, %3\n"
+ " stxr %w1, %0, [%2]\n"
+ " cbnz %w1, 1b"
+ : "=&r"(result), "=&r"(status)
+ : "r"(p), "r"(v)
+ : "memory");
+ return result;
+}
+
+int main(void)
+{
+ // msr nzcv writes the flags, mrs nzcv reads them
+ unsigned long a = 0, r;
+ __asm__ volatile("cmp %1, %1\n msr nzcv, xzr\n cset %w0, eq"
+ : "=r"(r)
+ : "r"(a)
+ : "cc");
+ __CPROVER_assert(r == 0, "msr nzcv clears Z");
+ unsigned long flags;
+ __asm__ volatile("cmp %1, %1\n mrs %0, nzcv" : "=r"(flags) : "r"(a) : "cc");
+ __CPROVER_assert(flags == 0x60000000ul, "mrs nzcv after equality");
+
+ // a load-exclusive/store-exclusive loop retries until the store succeeds
+ unsigned long counter = 40;
+ __CPROVER_assert(add_ll_sc(&counter, 2) == 42, "ll/sc result");
+ __CPROVER_assert(counter == 42, "ll/sc stored");
+
+ // a store-exclusive without a load-exclusive in the statement may succeed
+ // (if a load-exclusive executed before) or fail
+ unsigned long m = 1, v = 42;
+ unsigned int res;
+ __asm__ volatile("stxr %w0, %2, [%1]"
+ : "=&r"(res)
+ : "r"(&m), "r"(v)
+ : "memory");
+ __CPROVER_assert(m == 42, "store-exclusive stores"); // fails
+ __CPROVER_assert(m == 1, "store-exclusive fails"); // fails
+ __CPROVER_assert((res == 0) == (m == 42), "status matches the store");
+
+ // after clrex, a store-exclusive fails
+ m = 1;
+ __asm__ volatile("clrex\n stxr %w0, %2, [%1]"
+ : "=&r"(res)
+ : "r"(&m), "r"(v)
+ : "memory");
+ __CPROVER_assert(m == 1 && res == 1, "store-exclusive after clrex");
+
+ // a register variable bound to a register that the template writes
+ register unsigned long x0 __asm__("x0") = 5;
+ __asm__ volatile("add x0, x0, #1" : "+r"(x0));
+ __CPROVER_assert(x0 == 5, "register variable"); // fails
+
+ return 0;
+}
diff --git a/regression/cbmc/asm_aarch64_corner_cases/test.desc b/regression/cbmc/asm_aarch64_corner_cases/test.desc
new file mode 100644
index 000000000000..9d7a25ac97b7
--- /dev/null
+++ b/regression/cbmc/asm_aarch64_corner_cases/test.desc
@@ -0,0 +1,22 @@
+CORE gcc-only
+main.c
+--arch arm64 --unwind 3 --no-unwinding-assertions
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 27 msr nzcv clears Z: SUCCESS$
+^\[main\.assertion\.2\] line 30 mrs nzcv after equality: SUCCESS$
+^\[main\.assertion\.3\] line 34 ll/sc result: SUCCESS$
+^\[main\.assertion\.4\] line 35 ll/sc stored: SUCCESS$
+^\[main\.assertion\.5\] line 45 store-exclusive stores: FAILURE$
+^\[main\.assertion\.6\] line 46 store-exclusive fails: FAILURE$
+^\[main\.assertion\.7\] line 47 status matches the store: SUCCESS$
+^\[main\.assertion\.8\] line 55 store-exclusive after clrex: SUCCESS$
+^\[main\.assertion\.9\] line 60 register variable: FAILURE$
+approximating inline assembly statement at .* line 59 function main: writes register x0, which is neither an operand nor clobbered
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly statement at .* function add_ll_sc
+--
+The properties marked "fails" in main.c may be violated on hardware; all
+others hold on hardware.
diff --git a/regression/cbmc/asm_aarch64_exclusive_abandoned/main.c b/regression/cbmc/asm_aarch64_exclusive_abandoned/main.c
new file mode 100644
index 000000000000..0b71f82382f7
--- /dev/null
+++ b/regression/cbmc/asm_aarch64_exclusive_abandoned/main.c
@@ -0,0 +1,72 @@
+// A branch, a label, or another memory access after a load-exclusive ends the
+// exclusive access region: the load-exclusive is then a plain load, and a
+// store-exclusive after it succeeds or fails nondeterministically without
+// being atomic with the load. "fails" marks properties that the architecture
+// permits to be violated, so CBMC must not prove them.
+
+// Linux arm64 __cmpwait_case_32: the load-exclusive only arms the monitor for
+// wfe, and there is no store-exclusive
+static void cmpwait(volatile void *ptr, unsigned long val)
+{
+ unsigned long tmp;
+ __asm__ volatile(
+ "sevl\n"
+ " wfe\n"
+ " ldxr %w[tmp], %[v]\n"
+ " eor %w[tmp], %w[tmp], %w[val]\n"
+ " cbnz %w[tmp], 1f\n"
+ " wfe\n"
+ "1:"
+ : [tmp] "=&r"(tmp), [v] "+Q"(*(unsigned int *)ptr)
+ : [val] "r"(val));
+}
+
+// Linux arm64 __ll_sc__cmpxchg_case_64, without barriers: the branch to 2f
+// leaves the exclusive access region
+static unsigned long
+cmpxchg(unsigned long *ptr, unsigned long old, unsigned long new)
+{
+ unsigned long tmp, oldval;
+ __asm__ volatile(
+ "1: ldxr %[oldval], %[v]\n"
+ " eor %[tmp], %[oldval], %[old]\n"
+ " cbnz %[tmp], 2f\n"
+ " stxr %w[tmp], %[new], %[v]\n"
+ " cbnz %w[tmp], 1b\n"
+ "2:"
+ : [tmp] "=&r"(tmp), [oldval] "=&r"(oldval), [v] "+Q"(*ptr)
+ : [old] "r"(old), [new] "r"(new)
+ : "memory");
+ return oldval;
+}
+
+unsigned int flag = 3;
+unsigned long m = 5, n = 1;
+
+int main(void)
+{
+ cmpwait(&flag, 3);
+ __CPROVER_assert(flag == 3, "cmpwait does not write");
+
+ __CPROVER_assert(cmpxchg(&m, 5, 7) == 5, "cmpxchg returns the old value");
+ __CPROVER_assert(m == 7, "cmpxchg stores if equal");
+ __CPROVER_assert(cmpxchg(&m, 3, 9) == 7, "cmpxchg returns the value");
+ __CPROVER_assert(m == 7, "cmpxchg does not store if different");
+
+ // a store between the load-exclusive and the store-exclusive
+ unsigned long t;
+ unsigned int status;
+ __asm__ volatile(
+ "ldxr %0, [%2]\n"
+ " str %0, [%3]\n"
+ " stxr %w1, %4, [%2]"
+ : "=&r"(t), "=&r"(status)
+ : "r"(&m), "r"(&n), "r"(8ul)
+ : "memory");
+ __CPROVER_assert(n == 7, "plain store");
+ __CPROVER_assert((status == 0) == (m == 8), "status matches the store");
+ __CPROVER_assert(status == 0, "store-exclusive succeeds"); // fails
+ __CPROVER_assert(status == 1, "store-exclusive fails"); // fails
+
+ return 0;
+}
diff --git a/regression/cbmc/asm_aarch64_exclusive_abandoned/test.desc b/regression/cbmc/asm_aarch64_exclusive_abandoned/test.desc
new file mode 100644
index 000000000000..75a8017a50f1
--- /dev/null
+++ b/regression/cbmc/asm_aarch64_exclusive_abandoned/test.desc
@@ -0,0 +1,21 @@
+CORE gcc-only
+main.c
+--arch arm64 --unwind 2 --no-unwinding-assertions
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 49 cmpwait does not write: SUCCESS$
+^\[main\.assertion\.2\] line 51 cmpxchg returns the old value: SUCCESS$
+^\[main\.assertion\.3\] line 52 cmpxchg stores if equal: SUCCESS$
+^\[main\.assertion\.4\] line 53 cmpxchg returns the value: SUCCESS$
+^\[main\.assertion\.5\] line 54 cmpxchg does not store if different: SUCCESS$
+^\[main\.assertion\.6\] line 66 plain store: SUCCESS$
+^\[main\.assertion\.7\] line 67 status matches the store: SUCCESS$
+^\[main\.assertion\.8\] line 68 store-exclusive succeeds: FAILURE$
+^\[main\.assertion\.9\] line 69 store-exclusive fails: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly
+--
+The statements are interpreted. On qemu-aarch64, the store-exclusive after the
+plain store succeeds; the architecture permits it to fail.
diff --git a/regression/cbmc/asm_aarch64_hint/main.c b/regression/cbmc/asm_aarch64_hint/main.c
new file mode 100644
index 000000000000..96e5714c3a95
--- /dev/null
+++ b/regression/cbmc/asm_aarch64_hint/main.c
@@ -0,0 +1,44 @@
+// hint #imm is a no-op only for the hints that leave the registers unchanged.
+// The pointer authentication hints write x16, x17, or x30, and chkfeat
+// (hint #40) writes x16. "fails" marks properties that the architecture
+// permits to be violated, so CBMC must not prove them.
+int main(void)
+{
+ unsigned long in = 0x0000aaaabbbbccccul, out;
+
+ // pacia1716 signs x17 with the modifier in x16
+ __asm__ volatile("mov x17, %1\n mov x16, #0\n hint #8\n mov %0, x17"
+ : "=r"(out)
+ : "r"(in)
+ : "x16", "x17");
+ __CPROVER_assert(out == in, "pacia1716 leaves x17 unchanged"); // fails
+
+ // pacib1716, with the hint number in an operand
+ __asm__ volatile("mov x17, %1\n mov x16, #0\n hint %2\n mov %0, x17"
+ : "=r"(out)
+ : "r"(in), "i"(10)
+ : "x16", "x17");
+ __CPROVER_assert(out == in, "pacib1716 leaves x17 unchanged"); // fails
+
+ // xpaclri removes the authentication code from x30
+ __asm__ volatile("mov x30, %1\n hint #7\n mov %0, x30"
+ : "=r"(out)
+ : "r"(0x00ff000000001234ul)
+ : "x30");
+ __CPROVER_assert(out == 0x00ff000000001234ul, "xpaclri"); // fails
+
+ // chkfeat x16 clears the bits of the enabled features
+ __asm__ volatile("mov x16, #1\n hint #40\n mov %0, x16"
+ : "=r"(out)
+ :
+ : "x16");
+ __CPROVER_assert(out == 1, "chkfeat leaves x16 unchanged"); // fails
+
+ // nop, csdb, bti c, bti jc
+ __asm__ volatile("mov %0, %1\n hint #0\n hint #20\n hint #34\n hint #0x26"
+ : "=r"(out)
+ : "r"(in));
+ __CPROVER_assert(out == in, "hints without effect");
+
+ return 0;
+}
diff --git a/regression/cbmc/asm_aarch64_hint/test.desc b/regression/cbmc/asm_aarch64_hint/test.desc
new file mode 100644
index 000000000000..a5cdf8714c67
--- /dev/null
+++ b/regression/cbmc/asm_aarch64_hint/test.desc
@@ -0,0 +1,24 @@
+CORE gcc-only
+main.c
+--arch arm64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 14 pacia1716 leaves x17 unchanged: FAILURE$
+^\[main\.assertion\.2\] line 21 pacib1716 leaves x17 unchanged: FAILURE$
+^\[main\.assertion\.3\] line 28 xpaclri: FAILURE$
+^\[main\.assertion\.4\] line 35 chkfeat leaves x16 unchanged: FAILURE$
+^\[main\.assertion\.5\] line 41 hints without effect: SUCCESS$
+approximating inline assembly statement at .* line 10 function main: hint #8 may write registers
+approximating inline assembly statement at .* line 17 function main: hint #10 may write registers
+approximating inline assembly statement at .* line 24 function main: hint #7 may write registers
+approximating inline assembly statement at .* line 31 function main: hint #40 may write registers
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly statement at .* line 38
+--
+The pointer authentication hints and chkfeat are not interpreted, so the
+statements using them are approximated; the other hints of the test are
+no-ops. On qemu-aarch64 (-cpu max), the first three properties are violated;
+qemu does not implement chkfeat, which clears bit 0 of x16 if the guarded
+control stack is enabled.
diff --git a/regression/cbmc/asm_aarch64_immediate_modifier/main.c b/regression/cbmc/asm_aarch64_immediate_modifier/main.c
new file mode 100644
index 000000000000..888638139f8e
--- /dev/null
+++ b/regression/cbmc/asm_aarch64_immediate_modifier/main.c
@@ -0,0 +1,27 @@
+// Operand modifiers of immediate operands: %c prints the constant like a
+// plain reference, and %w and %x print the constant 0 as wzr and xzr. Other
+// modifiers, such as %n, which prints the negated constant, are not
+// interpreted. "fails" marks properties that are violated on hardware, so
+// CBMC must not prove them.
+int main(void)
+{
+ unsigned long a = 100, r;
+
+ __asm__("add %0, %1, #%n2" : "=r"(r) : "r"(a), "i"(5));
+ __CPROVER_assert(r == 105, "add #%n2 adds 5"); // fails
+
+ __asm__("add %0, %1, #%2" : "=r"(r) : "r"(a), "i"(5));
+ __CPROVER_assert(r == 105, "add #%2");
+
+ __asm__("add %0, %1, %c2" : "=r"(r) : "r"(a), "i"(5));
+ __CPROVER_assert(r == 105, "add %c2");
+
+ __asm__("orr %0, %1, %x2" : "=r"(r) : "r"(a), "i"(0));
+ __CPROVER_assert(r == 100, "orr with xzr");
+
+ r = 1;
+ __asm__("mov %w0, %w1" : "+r"(r) : "i"(0));
+ __CPROVER_assert(r == 0, "mov from wzr");
+
+ return 0;
+}
diff --git a/regression/cbmc/asm_aarch64_immediate_modifier/test.desc b/regression/cbmc/asm_aarch64_immediate_modifier/test.desc
new file mode 100644
index 000000000000..3c6b57348687
--- /dev/null
+++ b/regression/cbmc/asm_aarch64_immediate_modifier/test.desc
@@ -0,0 +1,18 @@
+CORE gcc-only
+main.c
+--arch arm64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 11 add #%n2 adds 5: FAILURE$
+^\[main\.assertion\.2\] line 14 add #%2: SUCCESS$
+^\[main\.assertion\.3\] line 17 add %c2: SUCCESS$
+^\[main\.assertion\.4\] line 20 orr with xzr: SUCCESS$
+^\[main\.assertion\.5\] line 24 mov from wzr: SUCCESS$
+approximating inline assembly statement at .* line 10 function main: unsupported operand modifier %n
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly statement at .* line (13|16|19|23)
+--
+With %n, the first statement subtracts 5 on hardware (qemu-aarch64: r is 95);
+CBMC approximates it. The other statements are interpreted.
diff --git a/regression/cbmc/asm_aarch64_memory/main.c b/regression/cbmc/asm_aarch64_memory/main.c
new file mode 100644
index 000000000000..920fa29ec655
--- /dev/null
+++ b/regression/cbmc/asm_aarch64_memory/main.c
@@ -0,0 +1,306 @@
+// AArch64 loads, stores, and atomics, each compared with a C reference on
+// concrete inputs. All assertions hold on qemu-aarch64.
+typedef unsigned long u64;
+typedef unsigned int u32;
+
+u64 buf[2];
+u64 expected[2];
+
+static u64 mask_of(int w)
+{
+ return w == 64 ? ~0ul : ((1ul << w) - 1);
+}
+
+// the size bytes at byte offset off of buf, sign-extended if sgn
+static u64 rd(int off, int size, int sgn)
+{
+ u64 v = buf[off / 8] >> ((off % 8) * 8);
+ if(size < 8)
+ {
+ v &= mask_of(size * 8);
+ if(sgn && ((v >> (size * 8 - 1)) & 1))
+ v |= ~0ul << (size * 8);
+ }
+ return v;
+}
+
+#define LD(name, insn) \
+ static u64 name(u64 *p) \
+ { \
+ u64 r; \
+ __asm__ volatile(insn : "=&r"(r) : "r"(p) : "memory"); \
+ return r; \
+ }
+
+LD(ldrb_w, "mov %0, #-1\n ldrb %w0, [%1, #1]")
+LD(ldrh_w, "mov %0, #-1\n ldrh %w0, [%1, #2]")
+LD(ldrsb_w, "mov %0, #-1\n ldrsb %w0, [%1, #3]")
+LD(ldrsb_x, "ldrsb %0, [%1, #7]")
+LD(ldrsh_w, "mov %0, #-1\n ldrsh %w0, [%1, #6]")
+LD(ldrsh_x, "ldrsh %0, [%1, #4]")
+LD(ldrsw_x, "ldrsw %0, [%1, #4]")
+LD(ldr_w, "mov %0, #-1\n ldr %w0, [%1, #4]")
+LD(ldr_x, "ldr %0, [%1, #8]")
+LD(ldur_x, "add %0, %1, #12\n ldur %0, [%0, #-4]")
+LD(ldurb_w, "add %0, %1, #9\n ldurb %w0, [%0, #-2]")
+LD(ldurh_w, "ldurh %w0, [%1, #3]")
+LD(ldar_w, "ldar %w0, [%1]")
+LD(ldar_x, "ldar %0, [%1]")
+LD(ldarb, "ldarb %w0, [%1]")
+LD(ldarh, "ldarh %w0, [%1]")
+LD(ldapr_x, "ldapr %0, [%1]")
+LD(ldxrb, "ldxrb %w0, [%1]\n clrex")
+LD(ldaxrh, "ldaxrh %w0, [%1]\n clrex")
+
+static void check_loads(void)
+{
+ __CPROVER_assert(ldrb_w(buf) == rd(1, 1, 0), "ldrb w");
+ __CPROVER_assert(ldrh_w(buf) == rd(2, 2, 0), "ldrh w");
+ __CPROVER_assert(ldrsb_w(buf) == (u32)rd(3, 1, 1), "ldrsb w");
+ __CPROVER_assert(ldrsb_x(buf) == rd(7, 1, 1), "ldrsb x");
+ __CPROVER_assert(ldrsh_w(buf) == (u32)rd(6, 2, 1), "ldrsh w");
+ __CPROVER_assert(ldrsh_x(buf) == rd(4, 2, 1), "ldrsh x");
+ __CPROVER_assert(ldrsw_x(buf) == rd(4, 4, 1), "ldrsw x");
+ __CPROVER_assert(ldr_w(buf) == rd(4, 4, 0), "ldr w");
+ __CPROVER_assert(ldr_x(buf) == rd(8, 8, 0), "ldr x");
+ __CPROVER_assert(ldur_x(buf) == rd(8, 8, 0), "ldur x");
+ __CPROVER_assert(ldurb_w(buf) == rd(7, 1, 0), "ldurb w");
+ __CPROVER_assert(ldurh_w(buf) == rd(3, 2, 0), "ldurh w");
+ __CPROVER_assert(ldar_w(buf) == rd(0, 4, 0), "ldar w");
+ __CPROVER_assert(ldar_x(buf + 1) == rd(8, 8, 0), "ldar x");
+ __CPROVER_assert(ldarb(buf) == rd(0, 1, 0), "ldarb");
+ __CPROVER_assert(ldarh(buf + 1) == rd(8, 2, 0), "ldarh");
+ __CPROVER_assert(ldapr_x(buf) == rd(0, 8, 0), "ldapr x");
+ __CPROVER_assert(ldxrb(buf) == rd(0, 1, 0), "ldxrb");
+ __CPROVER_assert(ldaxrh(buf) == rd(0, 2, 0), "ldaxrh");
+}
+
+#define ST(name, insn) \
+ static void name(u64 *p, u64 v) \
+ { \
+ __asm__ volatile(insn : : "r"(p), "r"(v) : "memory"); \
+ }
+
+ST(strb_w, "strb %w1, [%0, #3]")
+ST(strh_w, "strh %w1, [%0, #6]")
+ST(str_w, "str %w1, [%0, #4]")
+ST(str_x, "str %1, [%0, #8]")
+ST(stlr_x, "stlr %1, [%0]")
+ST(stlr_w, "stlr %w1, [%0]")
+ST(stlrb, "stlrb %w1, [%0]")
+ST(stlrh, "stlrh %w1, [%0]")
+ST(stur_w, "stur %w1, [%0, #-4]")
+ST(sturb, "sturb %w1, [%0, #-1]")
+ST(sturh, "sturh %w1, [%0, #5]")
+
+// the st forms of the LSE atomics do not return the old value
+ST(stadd_x, "stadd %1, [%0]")
+ST(staddlb, "staddlb %w1, [%0]")
+ST(stclrh, "stclrh %w1, [%0]")
+ST(steor_w, "steor %w1, [%0]")
+ST(stsetlb, "stsetlb %w1, [%0]")
+ST(stsmaxlh, "stsmaxlh %w1, [%0]")
+ST(stsmin_x, "stsmin %1, [%0]")
+ST(stumaxb, "stumaxb %w1, [%0]")
+ST(stuminl_w, "stuminl %w1, [%0]")
+
+// LSE atomics: the old value is returned, the new value is in memory
+#define LSE(name, insn) \
+ static u64 name(u64 *p, u64 v) \
+ { \
+ u64 old; \
+ __asm__ volatile(insn : "=&r"(old) : "r"(p), "r"(v) : "memory"); \
+ return old; \
+ }
+
+LSE(ldadd_x, "ldadd %2, %0, [%1]")
+LSE(ldaddal_w, "ldaddal %w2, %w0, [%1]")
+LSE(ldaddb, "ldaddb %w2, %w0, [%1]")
+LSE(ldaddlh, "ldaddlh %w2, %w0, [%1]")
+LSE(ldclra_x, "ldclra %2, %0, [%1]")
+LSE(ldclrb, "ldclrb %w2, %w0, [%1]")
+LSE(ldeorlh, "ldeorlh %w2, %w0, [%1]")
+LSE(ldeorab, "ldeorab %w2, %w0, [%1]")
+LSE(ldsetb, "ldsetb %w2, %w0, [%1]")
+LSE(ldseth, "ldseth %w2, %w0, [%1]")
+LSE(ldsmax_x, "ldsmax %2, %0, [%1]")
+LSE(ldsmaxb, "ldsmaxb %w2, %w0, [%1]")
+LSE(ldsminal_w, "ldsminal %w2, %w0, [%1]")
+LSE(ldsminah, "ldsminah %w2, %w0, [%1]")
+LSE(ldumax_w, "ldumax %w2, %w0, [%1]")
+LSE(ldumaxb, "ldumaxb %w2, %w0, [%1]")
+LSE(lduminl_x, "lduminl %2, %0, [%1]")
+LSE(lduminh, "lduminh %w2, %w0, [%1]")
+LSE(swpal_x, "swpal %2, %0, [%1]")
+LSE(swph, "swph %w2, %w0, [%1]")
+LSE(swpb, "swpb %w2, %w0, [%1]")
+
+// cas: Rs is compared with memory and receives the old value
+#define CAS(name, insn) \
+ static u64 name(u64 *p, u64 cmp, u64 nv) \
+ { \
+ __asm__ volatile(insn : "+&r"(cmp) : "r"(p), "r"(nv) : "memory"); \
+ return cmp; \
+ }
+
+CAS(casal_x, "casal %0, %2, [%1]")
+CAS(cas_w, "cas %w0, %w2, [%1]")
+CAS(casab, "casab %w0, %w2, [%1]")
+CAS(caslh, "caslh %w0, %w2, [%1]")
+CAS(casb, "casb %w0, %w2, [%1]")
+
+// replace the size bytes at byte offset off of expected by v
+static void wr(int off, int size, u64 v)
+{
+ u64 m = mask_of(size * 8) << ((off % 8) * 8);
+ expected[off / 8] = (expected[off / 8] & ~m) | ((v << ((off % 8) * 8)) & m);
+}
+
+#define CHECK_STORE(call, off, size, v, msg) \
+ do \
+ { \
+ expected[0] = buf[0]; \
+ expected[1] = buf[1]; \
+ call; \
+ wr(off, size, v); \
+ __CPROVER_assert(buf[0] == expected[0] && buf[1] == expected[1], msg); \
+ } while(0)
+
+static void check_stores(u64 v)
+{
+ CHECK_STORE(strb_w(buf, v), 3, 1, v, "strb w");
+ CHECK_STORE(strh_w(buf, v), 6, 2, v, "strh w");
+ CHECK_STORE(str_w(buf, v), 4, 4, v, "str w");
+ CHECK_STORE(str_x(buf, v), 8, 8, v, "str x");
+ CHECK_STORE(stlr_x(buf + 1, ~v), 8, 8, ~v, "stlr x");
+ CHECK_STORE(stlr_w(buf, v), 0, 4, v, "stlr w");
+ CHECK_STORE(stlrb(buf + 1, v), 8, 1, v, "stlrb");
+ CHECK_STORE(stlrh(buf + 1, ~v), 8, 2, ~v, "stlrh");
+ CHECK_STORE(stur_w(buf + 1, v), 4, 4, v, "stur w");
+ CHECK_STORE(sturb(buf + 1, ~v), 7, 1, ~v, "sturb");
+ CHECK_STORE(sturh(buf, v), 5, 2, v, "sturh");
+}
+
+static u64 smax(u64 a, u64 b, int w)
+{
+ long sa = (long)(a << (64 - w)) >> (64 - w),
+ sb = (long)(b << (64 - w)) >> (64 - w);
+ return sa >= sb ? a : b;
+}
+
+static u64 smin(u64 a, u64 b, int w)
+{
+ long sa = (long)(a << (64 - w)) >> (64 - w),
+ sb = (long)(b << (64 - w)) >> (64 - w);
+ return sa <= sb ? a : b;
+}
+
+// the old value o and the operand a, both of w bits, determine the new value
+// new_value of the w bits of buf[0]
+#define LSECHECK(fn, w, new_value) \
+ do \
+ { \
+ u64 m = mask_of(w); \
+ u64 before = buf[0]; \
+ u64 o = before & m, a = v & m; \
+ u64 got = fn(buf, v); \
+ u64 want = (before & ~m) | (m & (new_value)); \
+ __CPROVER_assert(got == o && buf[0] == want, #fn); \
+ buf[0] = before; \
+ } while(0)
+
+static void check_lse(u64 v)
+{
+ LSECHECK(ldadd_x, 64, o + a);
+ LSECHECK(ldaddal_w, 32, o + a);
+ LSECHECK(ldaddb, 8, o + a);
+ LSECHECK(ldaddlh, 16, o + a);
+ LSECHECK(ldclra_x, 64, o & ~a);
+ LSECHECK(ldclrb, 8, o & ~a);
+ LSECHECK(ldeorlh, 16, o ^ a);
+ LSECHECK(ldeorab, 8, o ^ a);
+ LSECHECK(ldsetb, 8, o | a);
+ LSECHECK(ldseth, 16, o | a);
+ LSECHECK(ldsmax_x, 64, smax(o, a, 64));
+ LSECHECK(ldsmaxb, 8, smax(o, a, 8));
+ LSECHECK(ldsminal_w, 32, smin(o, a, 32));
+ LSECHECK(ldsminah, 16, smin(o, a, 16));
+ LSECHECK(ldumax_w, 32, o >= a ? o : a);
+ LSECHECK(ldumaxb, 8, o >= a ? o : a);
+ LSECHECK(lduminl_x, 64, o <= a ? o : a);
+ LSECHECK(lduminh, 16, o <= a ? o : a);
+ LSECHECK(swpal_x, 64, a);
+ LSECHECK(swph, 16, a);
+ LSECHECK(swpb, 8, a);
+}
+
+#define STCHECK(fn, w, new_value) \
+ do \
+ { \
+ u64 m = mask_of(w); \
+ u64 before = buf[0]; \
+ u64 o = before & m, a = v & m; \
+ fn(buf, v); \
+ __CPROVER_assert(buf[0] == ((before & ~m) | (m & (new_value))), #fn); \
+ buf[0] = before; \
+ } while(0)
+
+static void check_lse_st(u64 v)
+{
+ STCHECK(stadd_x, 64, o + a);
+ STCHECK(staddlb, 8, o + a);
+ STCHECK(stclrh, 16, o & ~a);
+ STCHECK(steor_w, 32, o ^ a);
+ STCHECK(stsetlb, 8, o | a);
+ STCHECK(stsmaxlh, 16, smax(o, a, 16));
+ STCHECK(stsmin_x, 64, smin(o, a, 64));
+ STCHECK(stumaxb, 8, o >= a ? o : a);
+ STCHECK(stuminl_w, 32, o <= a ? o : a);
+}
+
+#define CASCHECK(fn, w) \
+ do \
+ { \
+ u64 m = mask_of(w); \
+ u64 before = buf[0]; \
+ u64 got = fn(buf, c, v); \
+ int eq = (before & m) == (c & m); \
+ u64 want = eq ? (before & ~m) | (v & m) : before; \
+ __CPROVER_assert(got == (before & m) && buf[0] == want, #fn); \
+ buf[0] = before; \
+ } while(0)
+
+static void check_cas(u64 c, u64 v)
+{
+ CASCHECK(casal_x, 64);
+ CASCHECK(cas_w, 32);
+ CASCHECK(casab, 8);
+ CASCHECK(caslh, 16);
+ CASCHECK(casb, 8);
+}
+
+static void run(u64 b0, u64 b1, u64 v, u64 c)
+{
+ buf[0] = b0;
+ buf[1] = b1;
+ check_loads();
+ check_stores(v);
+ buf[0] = b0;
+ buf[1] = b1;
+ check_lse(v);
+ check_lse_st(v);
+ check_cas(c, v);
+}
+
+int main(void)
+{
+ run(0x8182838485868788ul, 0x7f00ff017e02fd03ul, 0x80ff7f0180ff7f01ul, 0);
+ run(
+ 0x0123456789abcdeful,
+ 0xfedcba9876543210ul,
+ 0xfedcba9876543210ul,
+ 0x0123456789abcdeful);
+ run(0x7f7f7f7f7f7f7f7ful, 0x8080808080808080ul, 0x8080808080808080ul, 0);
+ run(0, 0xfffffffffffffffful, 0xfffffffffffffffful, 0x00000000ffffff00ul);
+ return 0;
+}
diff --git a/regression/cbmc/asm_aarch64_memory/test.desc b/regression/cbmc/asm_aarch64_memory/test.desc
new file mode 100644
index 000000000000..2272fcff87b5
--- /dev/null
+++ b/regression/cbmc/asm_aarch64_memory/test.desc
@@ -0,0 +1,16 @@
+CORE gcc-only
+main.c
+--arch arm64
+^EXIT=0$
+^SIGNAL=0$
+^VERIFICATION SUCCESSFUL$
+--
+^warning: ignoring
+approximating inline assembly
+--
+Covers loads (including the sign-extending loads, ldur, ldar, and ldapr),
+stores (including stur and stlr), the LSE atomics in their b, h, and st
+forms, swp, and cas. The assertions compare each statement with a C
+reference; they all hold when main.c is compiled with clang -O1
+--target=aarch64-linux-gnu -march=armv8.4-a (with __CPROVER_assert defined to
+check its condition) and run on qemu-aarch64 -cpu max.
diff --git a/regression/cbmc/asm_aarch64_unknown_address/main.c b/regression/cbmc/asm_aarch64_unknown_address/main.c
new file mode 100644
index 000000000000..5aff28dc3e50
--- /dev/null
+++ b/regression/cbmc/asm_aarch64_unknown_address/main.c
@@ -0,0 +1,63 @@
+int x, y;
+
+int main(void)
+{
+ int *px = &x;
+ unsigned long c, slot;
+
+ // x1 holds the register variable p, but the statement does not
+ // initialise it: the store is approximated
+ x = 0;
+ {
+ register int *p __asm__("x1") = &x;
+ __asm__ volatile("str %w0, [x1]" : : "r"(5), "r"(p) : "memory");
+ }
+ __CPROVER_assert(x == 0, "store via register variable");
+
+ // x1 initialised from an operand before the store: interpreted
+ x = 0;
+ __asm__ volatile("mov x1, %0\n str %w1, [x1]"
+ :
+ : "r"(px), "r"(5)
+ : "x1", "memory");
+ __CPROVER_assert(x == 5, "store via initialised register");
+
+ // a forward branch skips the initialisation
+ x = 0;
+ __asm__ volatile("cbz %2, 1f\n mov x1, %0\n1: str %w1, [x1]"
+ :
+ : "r"(px), "r"(5), "r"(c)
+ : "x1", "memory");
+ __CPROVER_assert(x == 0, "initialisation skipped by forward branch");
+
+ // a backward branch makes x1 unknown at the store
+ x = 0;
+ __asm__ volatile(
+ "mov x1, %0\n1: str %w1, [x1]\n mrs x1, tpidr_el0\n cbnz %2, 1b"
+ :
+ : "r"(px), "r"(5), "r"(c)
+ : "x1", "memory");
+ __CPROVER_assert(x == 0, "register made unknown by backward branch");
+
+ // an unknown value stored to memory and loaded as an address
+ x = 0;
+ __asm__ volatile(
+ "mrs x1, tpidr_el0\n str x1, [%0]\n ldr x2, [%0]\n str %w1, [x2]"
+ :
+ : "r"(&slot), "r"(5)
+ : "x1", "x2", "memory");
+ __CPROVER_assert(x == 0, "unknown address loaded from memory");
+
+ // a loop with known values only: interpreted
+ y = 0;
+ __asm__ volatile(
+ "mov x1, %0\n mov x3, #3\n"
+ "1: ldr w2, [x1]\n add w2, w2, #1\n str w2, [x1]\n"
+ " subs x3, x3, #1\n b.ne 1b"
+ :
+ : "r"(&y)
+ : "x1", "x2", "x3", "cc", "memory");
+ __CPROVER_assert(y == 3, "loop with known addresses");
+
+ return 0;
+}
diff --git a/regression/cbmc/asm_aarch64_unknown_address/test.desc b/regression/cbmc/asm_aarch64_unknown_address/test.desc
new file mode 100644
index 000000000000..de5d1cfabe7d
--- /dev/null
+++ b/regression/cbmc/asm_aarch64_unknown_address/test.desc
@@ -0,0 +1,25 @@
+CORE gcc-only
+main.c
+--arch arm64 --unwind 4 --no-unwinding-assertions
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 15 store via register variable: FAILURE$
+^\[main\.assertion\.2\] line 23 store via initialised register: SUCCESS$
+^\[main\.assertion\.3\] line 31 initialisation skipped by forward branch: FAILURE$
+^\[main\.assertion\.4\] line 40 register made unknown by backward branch: FAILURE$
+^\[main\.assertion\.5\] line 49 unknown address loaded from memory: FAILURE$
+^\[main\.assertion\.6\] line 60 loop with known addresses: SUCCESS$
+approximating inline assembly statement at .* line 13 function main: memory access at an address computed from a value that is not modeled
+approximating inline assembly statement at .* line 27 function main: memory access at an address computed from a value that is not modeled
+approximating inline assembly statement at .* line 35 function main: branch with values that are not modeled to code that relies on them
+approximating inline assembly statement at .* line 44 function main: memory access at an address computed from a value that is not modeled
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly statement at .* line (19|53|54) function main
+--
+A memory access at an address that may depend on a value CBMC does not model
+(here: registers the statement does not initialise, or a system register read
+by mrs) would change no object, as CBMC cannot relate such an address to an
+object. Such statements are approximated. The assertions that fail may be violated
+on hardware (x or y is then changed); the others hold on hardware.
diff --git a/regression/cbmc/asm_arm/main.c b/regression/cbmc/asm_arm/main.c
new file mode 100644
index 000000000000..5ff309c8f15e
--- /dev/null
+++ b/regression/cbmc/asm_arm/main.c
@@ -0,0 +1,437 @@
+// AArch32 inline assembly (A32 code) that is interpreted exactly. The expected
+// values were observed with an emulator.
+
+// arm ldrex/orr/strex set a bit (atomic set)
+void asm_case_0(unsigned long long *io)
+{
+ unsigned int m = (unsigned int)io[0];
+ unsigned int v = (unsigned int)io[1];
+ unsigned int tmp = (unsigned int)io[2];
+ unsigned int res = (unsigned int)io[3];
+ __asm__ __volatile__(
+ "1: ldrex %0, [%2]\n"
+ " orr %0, %0, %3\n"
+ " strex %1, %0, [%2]\n"
+ " teq %1, #0\n"
+ " bne 1b\n"
+ : "=&r"(tmp), "=&r"(res)
+ : "r"(&m), "r"(v)
+ : "cc", "memory");
+ io[0] = (unsigned long long)m;
+ io[1] = (unsigned long long)v;
+ io[2] = (unsigned long long)tmp;
+ io[3] = (unsigned long long)res;
+}
+
+// arm add and subtract
+void asm_case_1(unsigned long long *io)
+{
+ unsigned int r = (unsigned int)io[0];
+ unsigned int a = (unsigned int)io[1];
+ unsigned int b = (unsigned int)io[2];
+ unsigned int c = (unsigned int)io[3];
+ __asm__("add %0, %1, %2\n sub %0, %0, %3"
+ : "=&r"(r)
+ : "r"(a), "r"(b), "r"(c));
+ io[0] = (unsigned long long)r;
+ io[1] = (unsigned long long)a;
+ io[2] = (unsigned long long)b;
+ io[3] = (unsigned long long)c;
+}
+
+// arm adds/adc is a 64-bit add
+void asm_case_2(unsigned long long *io)
+{
+ unsigned int lo = (unsigned int)io[0];
+ unsigned int hi = (unsigned int)io[1];
+ unsigned int alo = (unsigned int)io[2];
+ unsigned int ahi = (unsigned int)io[3];
+ unsigned int blo = (unsigned int)io[4];
+ unsigned int bhi = (unsigned int)io[5];
+ __asm__("adds %0, %2, %4\n adc %1, %3, %5"
+ : "=&r"(lo), "=&r"(hi)
+ : "r"(alo), "r"(ahi), "r"(blo), "r"(bhi)
+ : "cc");
+ io[0] = (unsigned long long)lo;
+ io[1] = (unsigned long long)hi;
+ io[2] = (unsigned long long)alo;
+ io[3] = (unsigned long long)ahi;
+ io[4] = (unsigned long long)blo;
+ io[5] = (unsigned long long)bhi;
+}
+
+// arm and/orr/eor combine bits
+void asm_case_3(unsigned long long *io)
+{
+ unsigned int r = (unsigned int)io[0];
+ unsigned int a = (unsigned int)io[1];
+ unsigned int b = (unsigned int)io[2];
+ unsigned int c = (unsigned int)io[3];
+ unsigned int d = (unsigned int)io[4];
+ __asm__("and %0, %1, %2\n orr %0, %0, %3\n eor %0, %0, %4"
+ : "=&r"(r)
+ : "r"(a), "r"(b), "r"(c), "r"(d));
+ io[0] = (unsigned long long)r;
+ io[1] = (unsigned long long)a;
+ io[2] = (unsigned long long)b;
+ io[3] = (unsigned long long)c;
+ io[4] = (unsigned long long)d;
+}
+
+// arm cmp and conditional move
+void asm_case_4(unsigned long long *io)
+{
+ unsigned int r = (unsigned int)io[0];
+ unsigned int a = (unsigned int)io[1];
+ unsigned int b = (unsigned int)io[2];
+ __asm__("cmp %1, %2\n movhi %0, #1\n movls %0, #0"
+ : "=r"(r)
+ : "r"(a), "r"(b)
+ : "cc");
+ io[0] = (unsigned long long)r;
+ io[1] = (unsigned long long)a;
+ io[2] = (unsigned long long)b;
+}
+
+// arm ldr and str move a word
+void asm_case_5(unsigned long long *io)
+{
+ unsigned int a = (unsigned int)io[0];
+ unsigned int b = (unsigned int)io[1];
+ unsigned int t = (unsigned int)io[2];
+ __asm__ __volatile__("ldr %0, [%1]\n str %0, [%2]"
+ : "=&r"(t)
+ : "r"(&a), "r"(&b)
+ : "memory");
+ io[0] = (unsigned long long)a;
+ io[1] = (unsigned long long)b;
+ io[2] = (unsigned long long)t;
+}
+
+// arm lsl/lsr shift
+void asm_case_6(unsigned long long *io)
+{
+ unsigned int r = (unsigned int)io[0];
+ unsigned int a = (unsigned int)io[1];
+ unsigned int b = (unsigned int)io[2];
+ unsigned int c = (unsigned int)io[3];
+ __asm__("lsl %0, %1, %2\n lsr %0, %0, %3"
+ : "=&r"(r)
+ : "r"(a), "r"(b), "r"(c));
+ io[0] = (unsigned long long)r;
+ io[1] = (unsigned long long)a;
+ io[2] = (unsigned long long)b;
+ io[3] = (unsigned long long)c;
+}
+
+// arm bic clears bits
+void asm_case_7(unsigned long long *io)
+{
+ unsigned int r = (unsigned int)io[0];
+ unsigned int a = (unsigned int)io[1];
+ unsigned int b = (unsigned int)io[2];
+ __asm__("bic %0, %1, %2" : "=r"(r) : "r"(a), "r"(b));
+ io[0] = (unsigned long long)r;
+ io[1] = (unsigned long long)a;
+ io[2] = (unsigned long long)b;
+}
+
+// arm rsb reverse-subtracts
+void asm_case_8(unsigned long long *io)
+{
+ unsigned int r = (unsigned int)io[0];
+ unsigned int a = (unsigned int)io[1];
+ unsigned int b = (unsigned int)io[2];
+ __asm__("rsb %0, %1, %2" : "=r"(r) : "r"(a), "r"(b));
+ io[0] = (unsigned long long)r;
+ io[1] = (unsigned long long)a;
+ io[2] = (unsigned long long)b;
+}
+
+int main(void)
+{
+ {
+ unsigned long long io[4] = {
+ 0xc2a7af9eab79b005ULL, 0xffULL, 0x2ULL, 0x6ffe6666fa72766dULL};
+ asm_case_0(io);
+ __CPROVER_assert(
+ io[0] == 0xab79b0ffULL && io[1] == 0xffULL && io[2] == 0xab79b0ffULL &&
+ io[3] == 0x0ULL,
+ "arm ldrex/orr/strex set a bit (atomic set), vector 0");
+ }
+ {
+ unsigned long long io[4] = {
+ 0x8000000000000000ULL,
+ 0x8000000000000000ULL,
+ 0xae54d49633f89bafULL,
+ 0xcc5604a33a481fd2ULL};
+ asm_case_0(io);
+ __CPROVER_assert(
+ io[0] == 0x0ULL && io[1] == 0x0ULL && io[2] == 0x0ULL && io[3] == 0x0ULL,
+ "arm ldrex/orr/strex set a bit (atomic set), vector 1");
+ }
+ {
+ unsigned long long io[4] = {
+ 0xadb4bded0a6cc878ULL,
+ 0x248a141c7ccb3b72ULL,
+ 0xb5d23309b6d2590dULL,
+ 0x33756584e354bbe7ULL};
+ asm_case_0(io);
+ __CPROVER_assert(
+ io[0] == 0x7eeffb7aULL && io[1] == 0x7ccb3b72ULL &&
+ io[2] == 0x7eeffb7aULL && io[3] == 0x0ULL,
+ "arm ldrex/orr/strex set a bit (atomic set), vector 2");
+ }
+ {
+ unsigned long long io[4] = {
+ 0xc2b9546e0f0220f3ULL,
+ 0xf1d5effedcda26fbULL,
+ 0x7fffffffULL,
+ 0xb8d3af23681c2889ULL};
+ asm_case_1(io);
+ __CPROVER_assert(
+ io[0] == 0xf4bdfe71ULL && io[1] == 0xdcda26fbULL &&
+ io[2] == 0x7fffffffULL && io[3] == 0x681c2889ULL,
+ "arm add and subtract, vector 0");
+ }
+ {
+ unsigned long long io[4] = {
+ 0xc60f8fe0a6269527ULL,
+ 0x32f6ffa49275dc84ULL,
+ 0x7d7ef810cfc3dc1ULL,
+ 0xbf30555ab55f5d1fULL};
+ asm_case_1(io);
+ __CPROVER_assert(
+ io[0] == 0xea12bd26ULL && io[1] == 0x9275dc84ULL &&
+ io[2] == 0xcfc3dc1ULL && io[3] == 0xb55f5d1fULL,
+ "arm add and subtract, vector 1");
+ }
+ {
+ unsigned long long io[4] = {
+ 0xb3d8c88ab7063e8dULL,
+ 0xefe90ebcbcb30e03ULL,
+ 0xffffffffULL,
+ 0xed25d1989c8d81a9ULL};
+ asm_case_1(io);
+ __CPROVER_assert(
+ io[0] == 0x20258c59ULL && io[1] == 0xbcb30e03ULL &&
+ io[2] == 0xffffffffULL && io[3] == 0x9c8d81a9ULL,
+ "arm add and subtract, vector 2");
+ }
+ {
+ unsigned long long io[6] = {
+ 0xe638bca46bd70d89ULL,
+ 0x2ULL,
+ 0x2ULL,
+ 0x17d967b89f01d73dULL,
+ 0x82974e236f761b03ULL,
+ 0x7fULL};
+ asm_case_2(io);
+ __CPROVER_assert(
+ io[0] == 0x6f761b05ULL && io[1] == 0x9f01d7bcULL && io[2] == 0x2ULL &&
+ io[3] == 0x9f01d73dULL && io[4] == 0x6f761b03ULL && io[5] == 0x7fULL,
+ "arm adds/adc is a 64-bit add, vector 0");
+ }
+ {
+ unsigned long long io[6] = {
+ 0x6bcc7b9e0a546c01ULL,
+ 0xffffffffULL,
+ 0x9541937ec1bfc7abULL,
+ 0x80000000ULL,
+ 0xef512c58c835156fULL,
+ 0xe685acb714786e96ULL};
+ asm_case_2(io);
+ __CPROVER_assert(
+ io[0] == 0x89f4dd1aULL && io[1] == 0x94786e97ULL &&
+ io[2] == 0xc1bfc7abULL && io[3] == 0x80000000ULL &&
+ io[4] == 0xc835156fULL && io[5] == 0x14786e96ULL,
+ "arm adds/adc is a 64-bit add, vector 1");
+ }
+ {
+ unsigned long long io[6] = {
+ 0x8644a1e6d2818addULL,
+ 0xef828079a78b6244ULL,
+ 0x4a13b9da9f4e53bbULL,
+ 0x19ec4413125e5cfbULL,
+ 0xa51bdbee16d2b9aeULL,
+ 0xffULL};
+ asm_case_2(io);
+ __CPROVER_assert(
+ io[0] == 0xb6210d69ULL && io[1] == 0x125e5dfaULL &&
+ io[2] == 0x9f4e53bbULL && io[3] == 0x125e5cfbULL &&
+ io[4] == 0x16d2b9aeULL && io[5] == 0xffULL,
+ "arm adds/adc is a 64-bit add, vector 2");
+ }
+ {
+ unsigned long long io[5] = {
+ 0x38b1f9a35c2855baULL,
+ 0x8a0cb201725c5338ULL,
+ 0xb0652c5806a310a8ULL,
+ 0x808332e379bd40dbULL,
+ 0xe586f6e1167ff12eULL};
+ asm_case_3(io);
+ __CPROVER_assert(
+ io[0] == 0x6dc2a1d5ULL && io[1] == 0x725c5338ULL &&
+ io[2] == 0x6a310a8ULL && io[3] == 0x79bd40dbULL &&
+ io[4] == 0x167ff12eULL,
+ "arm and/orr/eor combine bits, vector 0");
+ }
+ {
+ unsigned long long io[5] = {
+ 0xffULL,
+ 0xb7a413d48183cf4eULL,
+ 0x872808db18a381eULL,
+ 0x89c40189aec7d882ULL,
+ 0x3ab70d7151d4fc15ULL};
+ asm_case_3(io);
+ __CPROVER_assert(
+ io[0] == 0xfe13249bULL && io[1] == 0x8183cf4eULL &&
+ io[2] == 0xb18a381eULL && io[3] == 0xaec7d882ULL &&
+ io[4] == 0x51d4fc15ULL,
+ "arm and/orr/eor combine bits, vector 1");
+ }
+ {
+ unsigned long long io[5] = {
+ 0x3785478d706735ULL,
+ 0xffULL,
+ 0x7fULL,
+ 0x62fbfdc8c535c306ULL,
+ 0x7fffffffULL};
+ asm_case_3(io);
+ __CPROVER_assert(
+ io[0] == 0xbaca3c80ULL && io[1] == 0xffULL && io[2] == 0x7fULL &&
+ io[3] == 0xc535c306ULL && io[4] == 0x7fffffffULL,
+ "arm and/orr/eor combine bits, vector 2");
+ }
+ {
+ unsigned long long io[3] = {
+ 0xb8004092f9e22b92ULL, 0xab4d6d46b16b38cbULL, 0xc5cc618b30146b25ULL};
+ asm_case_4(io);
+ __CPROVER_assert(
+ io[0] == 0x1ULL && io[1] == 0xb16b38cbULL && io[2] == 0x30146b25ULL,
+ "arm cmp and conditional move, vector 0");
+ }
+ {
+ unsigned long long io[3] = {
+ 0xc662866ad84cf328ULL, 0x8000000000000000ULL, 0xffffffffffffffffULL};
+ asm_case_4(io);
+ __CPROVER_assert(
+ io[0] == 0x0ULL && io[1] == 0x0ULL && io[2] == 0xffffffffULL,
+ "arm cmp and conditional move, vector 1");
+ }
+ {
+ unsigned long long io[3] = {
+ 0xd85ee36626a12990ULL, 0x816ffbff11966d8bULL, 0x0ULL};
+ asm_case_4(io);
+ __CPROVER_assert(
+ io[0] == 0x1ULL && io[1] == 0x11966d8bULL && io[2] == 0x0ULL,
+ "arm cmp and conditional move, vector 2");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x662e0001e6f57713ULL, 0xf1657729e04a52fdULL, 0x4ce92b19fe991d62ULL};
+ asm_case_5(io);
+ __CPROVER_assert(
+ io[0] == 0xe6f57713ULL && io[1] == 0xe6f57713ULL &&
+ io[2] == 0xe6f57713ULL,
+ "arm ldr and str move a word, vector 0");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x2dca9d6ea967229bULL, 0xe4a66e6e92989e7dULL, 0x350b8f5dcea6fb83ULL};
+ asm_case_5(io);
+ __CPROVER_assert(
+ io[0] == 0xa967229bULL && io[1] == 0xa967229bULL &&
+ io[2] == 0xa967229bULL,
+ "arm ldr and str move a word, vector 1");
+ }
+ {
+ unsigned long long io[3] = {
+ 0xabc0ff65a92da05dULL, 0x4c075f1f0b493f1aULL, 0x7d9cf46457162242ULL};
+ asm_case_5(io);
+ __CPROVER_assert(
+ io[0] == 0xa92da05dULL && io[1] == 0xa92da05dULL &&
+ io[2] == 0xa92da05dULL,
+ "arm ldr and str move a word, vector 2");
+ }
+ {
+ unsigned long long io[4] = {
+ 0x71985eb13a883919ULL, 0x7fffffffffffffffULL, 0x1cULL, 0x1fULL};
+ asm_case_6(io);
+ __CPROVER_assert(
+ io[0] == 0x1ULL && io[1] == 0xffffffffULL && io[2] == 0x1cULL &&
+ io[3] == 0x1fULL,
+ "arm lsl/lsr shift, vector 0");
+ }
+ {
+ unsigned long long io[4] = {
+ 0x4f18c672fa42fe7cULL, 0x120984629605b44fULL, 0x14ULL, 0x0ULL};
+ asm_case_6(io);
+ __CPROVER_assert(
+ io[0] == 0x44f00000ULL && io[1] == 0x9605b44fULL && io[2] == 0x14ULL &&
+ io[3] == 0x0ULL,
+ "arm lsl/lsr shift, vector 1");
+ }
+ {
+ unsigned long long io[4] = {
+ 0xfd64b9e5bd0edb5aULL, 0x25406c9bc1a6364ULL, 0x0ULL, 0x12ULL};
+ asm_case_6(io);
+ __CPROVER_assert(
+ io[0] == 0x2f06ULL && io[1] == 0xbc1a6364ULL && io[2] == 0x0ULL &&
+ io[3] == 0x12ULL,
+ "arm lsl/lsr shift, vector 2");
+ }
+ {
+ unsigned long long io[3] = {
+ 0xd6bc0d35ccec83e4ULL, 0x4fb0d79be3130b3fULL, 0x2d65a8a1cf4ce48eULL};
+ asm_case_7(io);
+ __CPROVER_assert(
+ io[0] == 0x20130b31ULL && io[1] == 0xe3130b3fULL &&
+ io[2] == 0xcf4ce48eULL,
+ "arm bic clears bits, vector 0");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x0ULL, 0xdae98f58edaf5ddULL, 0xbb5f3ec2488a958cULL};
+ asm_case_7(io);
+ __CPROVER_assert(
+ io[0] == 0x86506051ULL && io[1] == 0x8edaf5ddULL &&
+ io[2] == 0x488a958cULL,
+ "arm bic clears bits, vector 1");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x8bcb7f1085e0c86ULL, 0x3dc7152f30428c31ULL, 0x12257820e6b755cfULL};
+ asm_case_7(io);
+ __CPROVER_assert(
+ io[0] == 0x10408830ULL && io[1] == 0x30428c31ULL &&
+ io[2] == 0xe6b755cfULL,
+ "arm bic clears bits, vector 2");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x71f2223ff559a0f9ULL, 0xc447a6fbb54b9c52ULL, 0xf34de6440ce23540ULL};
+ asm_case_8(io);
+ __CPROVER_assert(
+ io[0] == 0x579698eeULL && io[1] == 0xb54b9c52ULL && io[2] == 0xce23540ULL,
+ "arm rsb reverse-subtracts, vector 0");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x2444fb01d3bdc021ULL, 0xfc66ce02975f8fb3ULL, 0xffULL};
+ asm_case_8(io);
+ __CPROVER_assert(
+ io[0] == 0x68a0714cULL && io[1] == 0x975f8fb3ULL && io[2] == 0xffULL,
+ "arm rsb reverse-subtracts, vector 1");
+ }
+ {
+ unsigned long long io[3] = {
+ 0xca7d4a778a0b60a5ULL, 0x38e3ee6142044392ULL, 0xa67f5e58956140eULL};
+ asm_case_8(io);
+ __CPROVER_assert(
+ io[0] == 0x4751d07cULL && io[1] == 0x42044392ULL &&
+ io[2] == 0x8956140eULL,
+ "arm rsb reverse-subtracts, vector 2");
+ }
+ return 0;
+}
diff --git a/regression/cbmc/asm_arm/test.desc b/regression/cbmc/asm_arm/test.desc
new file mode 100644
index 000000000000..0f22d6f1ab4a
--- /dev/null
+++ b/regression/cbmc/asm_arm/test.desc
@@ -0,0 +1,16 @@
+CORE gcc-only no-new-smt
+main.c
+--arch arm --unwind 2 --no-unwinding-assertions
+^EXIT=0$
+^SIGNAL=0$
+^VERIFICATION SUCCESSFUL$
+--
+^warning: ignoring
+approximating inline assembly
+--
+The AArch32 inline assembly in main.c is interpreted exactly: if CBMC
+approximated any statement, the assertions, which check values observed with
+an emulator (Unicorn, and qemu-arm for A32 code compiled by GCC and Clang),
+would fail. A store-exclusive may fail spuriously, so the retry loops of
+load-exclusive/store-exclusive sequences are bounded; the executions that
+retry more often reach the same states.
diff --git a/regression/cbmc/asm_arm_corner_cases/main.c b/regression/cbmc/asm_arm_corner_cases/main.c
new file mode 100644
index 000000000000..a816a5fef946
--- /dev/null
+++ b/regression/cbmc/asm_arm_corner_cases/main.c
@@ -0,0 +1,246 @@
+// Corner cases of the AArch32 instruction semantics. "fails" marks
+// properties that the architecture permits to be violated, so CBMC must not
+// prove them.
+static unsigned add_ll_sc(unsigned *p, unsigned v)
+{
+ unsigned result, status;
+ __asm__ volatile(
+ "1: ldrex %0, [%2]\n"
+ " add %0, %0, %3\n"
+ " strex %1, %0, [%2]\n"
+ " teq %1, #0\n"
+ " bne 1b"
+ : "=&r"(result), "=&r"(status)
+ : "r"(p), "r"(v)
+ : "cc", "memory");
+ return result;
+}
+
+int x;
+
+int main(void)
+{
+ unsigned a = 0, r, zero = 0;
+
+ // msr writes the flags, mrs reads them
+ r = 0;
+ __asm__ volatile("cmp %1, %1\n msr APSR_nzcvq, %2\n it eq\n moveq %0, #1"
+ : "+r"(r)
+ : "r"(a), "r"(zero)
+ : "cc");
+ __CPROVER_assert(r == 0, "msr clears Z");
+ unsigned flags;
+ __asm__ volatile("cmp %1, %1\n mrs %0, APSR" : "=r"(flags) : "r"(a) : "cc");
+ __CPROVER_assert((flags & 0xf0000000u) == 0x60000000u, "mrs after equality");
+
+ // an IT block of four instructions
+ r = 0;
+ __asm__ volatile(
+ "cmp %1, #0\n itttt eq\n addeq %0, %0, #1\n addeq %0, %0, #1\n"
+ " addeq %0, %0, #1\n addeq %0, %0, #1"
+ : "+r"(r)
+ : "r"(a)
+ : "cc");
+ __CPROVER_assert(r == 4, "it block");
+
+ // a load-exclusive/store-exclusive loop retries until the store succeeds
+ unsigned counter = 40;
+ __CPROVER_assert(add_ll_sc(&counter, 2) == 42, "ll/sc result");
+ __CPROVER_assert(counter == 42, "ll/sc stored");
+
+ // a store-exclusive without a load-exclusive in the statement may succeed
+ // (if a load-exclusive executed before) or fail
+ unsigned m = 1, v = 42, res;
+ __asm__ volatile("strex %0, %2, [%1]"
+ : "=&r"(res)
+ : "r"(&m), "r"(v)
+ : "memory");
+ __CPROVER_assert(m == 42, "store-exclusive stores"); // fails
+ __CPROVER_assert(m == 1, "store-exclusive fails"); // fails
+ __CPROVER_assert((res == 0) == (m == 42), "status matches the store");
+
+ // after clrex, a store-exclusive fails
+ m = 1;
+ __asm__ volatile("clrex\n strex %0, %2, [%1]"
+ : "=&r"(res)
+ : "r"(&m), "r"(v)
+ : "memory");
+ __CPROVER_assert(m == 1 && res == 1, "store-exclusive after clrex");
+
+ // a conditional store-exclusive followed by another one is not modeled: if
+ // the first does not execute, the monitor stays set for the second one
+ m = 1;
+ __asm__ volatile(
+ "ldrex %0, [%1]\n cmp %3, #0\n ite eq\n strexeq %0, %2, [%1]\n"
+ " strexne %0, %2, [%1]"
+ : "=&r"(res)
+ : "r"(&m), "r"(v), "r"(v)
+ : "cc", "memory");
+ __CPROVER_assert(res == 1, "conditional store-exclusive"); // fails
+ __CPROVER_assert(
+ (res == 0) == (m == 42), "status matches the conditional store");
+
+ // shifts by register use the bottom byte of the register
+ unsigned s32 = 32, s33 = 0x121, s40 = 40, big = 0x80000001u;
+ unsigned lsl, lsr, asr, ror;
+ __asm__("lsl %0, %4, %5\n lsr %1, %4, %6\n asr %2, %4, %7\n ror %3, %4, %5"
+ : "=&r"(lsl), "=&r"(lsr), "=&r"(asr), "=&r"(ror)
+ : "r"(big), "r"(s32), "r"(s33), "r"(s40));
+ __CPROVER_assert(
+ lsl == 0 && lsr == 0 && asr == 0xffffffffu && ror == big, "shifts");
+
+ // 64-bit subtraction with borrow
+ unsigned lo, hi;
+ __asm__("subs %0, %2, %4\n sbc %1, %3, %5"
+ : "=&r"(lo), "=&r"(hi)
+ : "r"(0u), "r"(1u), "r"(1u), "r"(0u)
+ : "cc");
+ __CPROVER_assert(lo == 0xffffffffu && hi == 0, "subs/sbc");
+
+ // bit and byte manipulation
+ unsigned w = 0x12345680u, rv16, rb, cz, sb, uh, mw;
+ __asm__(
+ "rev16 %0, %6\n rbit %1, %6\n clz %2, %7\n sxtb %3, %6\n"
+ " uxth %4, %6\n movw %5, #0xbeef\n movt %5, #0xdead"
+ : "=&r"(rv16), "=&r"(rb), "=&r"(cz), "=&r"(sb), "=&r"(uh), "=&r"(mw)
+ : "r"(w), "r"(0u));
+ __CPROVER_assert(
+ rv16 == 0x34128056u && rb == 0x016a2c48u && cz == 32, "rev16 rbit clz");
+ __CPROVER_assert(
+ sb == 0xffffff80u && uh == 0x5680u && mw == 0xdeadbeefu, "ext movw");
+
+ // loads and stores of bytes and halfwords
+ unsigned char bytes[4] = {0x80, 0x7f, 0x01, 0x02};
+ unsigned lb, lh;
+ __asm__ volatile("ldrsb %0, [%2]\n ldrh %1, [%2, #2]\n strb %1, [%2, #1]"
+ : "=&r"(lb), "=&r"(lh)
+ : "r"(bytes)
+ : "memory");
+ __CPROVER_assert(
+ lb == 0xffffff80u && lh == 0x0201u && bytes[1] == 1, "ldrsb ldrh strb");
+
+ // reading pc is not modeled
+ __asm__ volatile("mov %0, pc" : "=r"(r));
+ __CPROVER_assert(r == 0, "pc"); // fails
+
+ // In divided syntax (GCC's default for inline assembly in A32 code and
+ // for Thumb-1 targets), Thumb-1 add sets the flags; in A32 code and in
+ // unified syntax, it does not.
+ __asm__("cmp %1, %1\n add %0, %1, #1\n beq 1f\n mov %0, #7\n1:"
+ : "=&r"(r)
+ : "r"(a)
+ : "cc");
+ __CPROVER_assert(r == 1, "flags after add in divided syntax"); // fails
+ __asm__(
+ ".syntax unified\n cmp %1, %1\n add %0, %1, #1\n beq 1f\n"
+ " mov %0, #7\n1:"
+ : "=&r"(r)
+ : "r"(a)
+ : "cc");
+ __CPROVER_assert(r == 1, "flags after add in unified syntax");
+
+ // a register variable held in a register that the statement does not
+ // initialise
+ x = 0;
+ {
+ register int *p __asm__("r1") = &x;
+ __asm__ volatile("str %0, [r1]" : : "r"(5), "r"(p) : "memory");
+ }
+ __CPROVER_assert(x == 0, "store via register variable"); // fails
+
+ // a register variable bound to a register that the template writes
+ register unsigned r0 __asm__("r0") = 5;
+ __asm__ volatile("add r0, r0, #1" : "+r"(r0));
+ __CPROVER_assert(r0 == 5, "register variable"); // fails
+
+ // A change of the processor mode banks r8 to r14, to which the compiler
+ // may allocate operands: here the mov writes r8_fiq, not the operand.
+ {
+ register unsigned banked __asm__("r8") = 1;
+ unsigned fiq = 0x11, svc = 0x13;
+ __asm__ volatile("msr cpsr_c, %1\n mov %0, #2\n msr cpsr_c, %2"
+ : "+r"(banked)
+ : "r"(fiq), "r"(svc));
+ __CPROVER_assert(banked == 2, "operand after mode change"); // fails
+ }
+ // operands used before the mode changes are modeled
+ __asm__ volatile("mov %0, #1\n msr cpsr_c, %1"
+ : "=&r"(r)
+ : "r"(0x13)
+ : "memory");
+ __CPROVER_assert(r == 1, "operand before mode change");
+
+ // ARMv8-M Baseline is a Thumb-1 target, for which GCC uses divided syntax,
+ // but it has movw and ldrex: the add may still be adds.
+ unsigned t;
+ __asm__("cmp %1, %1\n movw %0, #1\n add %0, %0, #1\n beq 1f\n mov %0, #7\n1:"
+ : "=&l"(r)
+ : "l"(0)
+ : "cc");
+ __CPROVER_assert(r == 2, "flags after add with movw"); // fails
+ __asm__(
+ "cmp %2, %2\n add %0, %2, #1\n beq 1f\n mov %0, #7\n"
+ "1: ldrex %1, [%3]"
+ : "=&l"(r), "=&l"(t)
+ : "l"(0), "l"(&x)
+ : "memory", "cc");
+ __CPROVER_assert(r == 1, "flags after add with ldrex"); // fails
+ // with explicit divided syntax, Thumb-2 code uses the Thumb-1 forms too,
+ // and the assembler accepts clz and rsbs
+ __asm__(
+ ".syntax divided\n cmp %1, %1\n clz %0, %1\n add %0, %0, #1\n"
+ " beq 1f\n mov %0, #7\n1:\n.syntax unified"
+ : "=&l"(r)
+ : "l"(0)
+ : "cc");
+ __CPROVER_assert(r == 33, "flags after add with clz"); // fails
+ __asm__(
+ ".syntax divided\n rsbs %1, %2, #0\n cmp %2, %2\n add %0, %2, #1\n"
+ " beq 1f\n mov %0, #7\n1:\n.syntax unified"
+ : "=&l"(r), "=&l"(t)
+ : "l"(0)
+ : "cc");
+ __CPROVER_assert(r == 1, "flags after add with rsbs"); // fails
+
+ // in divided syntax for Thumb-1 targets, movs is adds with #0, which
+ // clears C
+ __asm__("cmp %1, %1\n movs %0, %2\n bcs 1f\n mov %0, #7\n1:"
+ : "=&l"(r)
+ : "l"(0), "l"(5)
+ : "cc");
+ __CPROVER_assert(r == 5, "carry after movs"); // fails
+
+ // Invalidating a data cache line without cleaning it may discard a dirty
+ // line, so that memory reverts to an older value, unlike cleaning and
+ // invalidating it.
+ x = 1;
+ __asm__ volatile("mcr p15, 0, %0, c7, c6, 1" : : "r"(&x) : "memory");
+ __CPROVER_assert(x == 1, "invalidate data cache line"); // fails
+ x = 1;
+ __asm__ volatile("mcr p15, 0, %0, c7, c14, 1" : : "r"(&x) : "memory");
+ __CPROVER_assert(x == 1, "clean and invalidate data cache line");
+
+ // A conditional load-exclusive is not modeled: if it does not execute, the
+ // monitor stays clear, so that the store-exclusive fails.
+ m = 1;
+ __asm__ volatile(
+ "clrex\n cmp %3, #0\n it eq\n ldrexeq %0, [%2]\n strex %1, %4, [%2]"
+ : "+&r"(r), "=&r"(res)
+ : "r"(&m), "r"(1), "r"(2)
+ : "memory", "cc");
+ __CPROVER_assert(m == 1 && res == 1, "conditional load-exclusive");
+ // compare and swap with a conditional store-exclusive, which is the only
+ // store-exclusive of the statement
+ m = 1;
+ __asm__ volatile(
+ "ldrex %1, [%2]\n mov %0, #0\n teq %1, %3\n it eq\n"
+ " strexeq %0, %4, [%2]"
+ : "=&r"(res), "=&r"(r)
+ : "r"(&m), "r"(1), "r"(42)
+ : "memory", "cc");
+ __CPROVER_assert(r == 1, "compare and swap reads");
+ __CPROVER_assert((res == 0) == (m == 42), "compare and swap status");
+ __CPROVER_assert(m == 42, "compare and swap stores"); // fails
+
+ return 0;
+}
diff --git a/regression/cbmc/asm_arm_corner_cases/test.desc b/regression/cbmc/asm_arm_corner_cases/test.desc
new file mode 100644
index 000000000000..26861023d6d5
--- /dev/null
+++ b/regression/cbmc/asm_arm_corner_cases/test.desc
@@ -0,0 +1,64 @@
+CORE gcc-only no-new-smt
+main.c
+--arch arm --unwind 3 --no-unwinding-assertions
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 31 msr clears Z: SUCCESS$
+^\[main\.assertion\.2\] line 34 mrs after equality: SUCCESS$
+^\[main\.assertion\.3\] line 44 it block: SUCCESS$
+^\[main\.assertion\.4\] line 48 ll/sc result: SUCCESS$
+^\[main\.assertion\.5\] line 49 ll/sc stored: SUCCESS$
+^\[main\.assertion\.6\] line 58 store-exclusive stores: FAILURE$
+^\[main\.assertion\.7\] line 59 store-exclusive fails: FAILURE$
+^\[main\.assertion\.8\] line 60 status matches the store: SUCCESS$
+^\[main\.assertion\.9\] line 68 store-exclusive after clrex: SUCCESS$
+^\[main\.assertion\.10\] line 79 conditional store-exclusive: FAILURE$
+^\[main\.assertion\.11\] line 80 status matches the conditional store: FAILURE$
+^\[main\.assertion\.12\] line 89 shifts: SUCCESS$
+^\[main\.assertion\.13\] line 98 subs/sbc: SUCCESS$
+^\[main\.assertion\.14\] line 107 rev16 rbit clz: SUCCESS$
+^\[main\.assertion\.15\] line 109 ext movw: SUCCESS$
+^\[main\.assertion\.16\] line 119 ldrsb ldrh strb: SUCCESS$
+^\[main\.assertion\.17\] line 124 pc: FAILURE$
+^\[main\.assertion\.18\] line 133 flags after add in divided syntax: FAILURE$
+^\[main\.assertion\.19\] line 140 flags after add in unified syntax: SUCCESS$
+^\[main\.assertion\.20\] line 149 store via register variable: FAILURE$
+^\[main\.assertion\.21\] line 154 register variable: FAILURE$
+^\[main\.assertion\.22\] line 164 operand after mode change: FAILURE$
+^\[main\.assertion\.23\] line 171 operand before mode change: SUCCESS$
+^\[main\.assertion\.24\] line 180 flags after add with movw: FAILURE$
+^\[main\.assertion\.25\] line 187 flags after add with ldrex: FAILURE$
+^\[main\.assertion\.26\] line 196 flags after add with clz: FAILURE$
+^\[main\.assertion\.27\] line 203 flags after add with rsbs: FAILURE$
+^\[main\.assertion\.28\] line 211 carry after movs: FAILURE$
+^\[main\.assertion\.29\] line 218 invalidate data cache line: FAILURE$
+^\[main\.assertion\.30\] line 221 clean and invalidate data cache line: SUCCESS$
+^\[main\.assertion\.31\] line 231 conditional load-exclusive: FAILURE$
+^\[main\.assertion\.32\] line 241 compare and swap reads: SUCCESS$
+^\[main\.assertion\.33\] line 242 compare and swap status: SUCCESS$
+^\[main\.assertion\.34\] line 243 compare and swap stores: FAILURE$
+approximating inline assembly statement at .* line 73 function main: conditional load-exclusive, or conditional store-exclusive and another store-exclusive
+approximating inline assembly statement at .* line 123 function main: unsupported operands of mov
+approximating inline assembly statement at .* line 147 function main: memory access at an address computed from a value that is not modeled
+approximating inline assembly statement at .* line 153 function main: writes register r0, which is neither an operand nor clobbered
+approximating inline assembly statement at .* line 161 function main: msr of the processor mode, which banks r8 to r14, and operands used after it
+approximating inline assembly statement at .* line 217 function main: cache invalidation without cleaning
+approximating inline assembly statement at .* line 226 function main: conditional load-exclusive, or conditional store-exclusive and another store-exclusive
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly statement at .* function add_ll_sc
+approximating inline assembly statement at .* line (167|176|181|190|197|207|220|235)
+--
+The properties marked "fails" in main.c may be violated on hardware; all
+others hold on hardware. The results were validated with qemu-arm, for A32
+and T32 code compiled by GCC and Clang (from "operand after mode change" on,
+by GCC only, as Clang does not support divided syntax). "flags after add in
+divided syntax" holds in A32 and T32 code, but is violated for Thumb-1
+targets, where the add sets the flags. So are "flags after add with movw" and
+"flags after add with ldrex", as validated with code compiled by GCC for
+-march=armv8-m.base, while "flags after add with clz" and "flags after add
+with rsbs" are violated in T32 code. "carry after movs" is violated in code
+compiled by GCC for -march=armv5t or -march=armv8-m.base. "status matches the
+conditional store" and "conditional load-exclusive" hold on hardware, but the
+statements are approximated.
diff --git a/regression/cbmc/asm_arm_exclusive_abandoned/main.c b/regression/cbmc/asm_arm_exclusive_abandoned/main.c
new file mode 100644
index 000000000000..b0219b8a29c9
--- /dev/null
+++ b/regression/cbmc/asm_arm_exclusive_abandoned/main.c
@@ -0,0 +1,60 @@
+// A branch, a label, or another memory access after a load-exclusive ends the
+// exclusive access region: the load-exclusive is then a plain load, and a
+// store-exclusive after it succeeds or fails nondeterministically without
+// being atomic with the load. "fails" marks properties that the architecture
+// permits to be violated, so CBMC must not prove them.
+
+#ifndef __CPROVER__
+# include
+# define __CPROVER_assert(c, m) assert(c)
+#endif
+
+// Linux arm arch_atomic_fetch_add_unless: the branch to 2f leaves the
+// exclusive access region
+static int fetch_add_unless(int *counter, int a, int u)
+{
+ int oldval, newval;
+ unsigned int tmp;
+ __asm__ volatile(
+ "1: ldrex %0, [%4]\n"
+ " teq %0, %5\n"
+ " beq 2f\n"
+ " add %1, %0, %6\n"
+ " strex %2, %1, [%4]\n"
+ " teq %2, #0\n"
+ " bne 1b\n"
+ "2:"
+ : "=&r"(oldval), "=&r"(newval), "=&r"(tmp), "+Qo"(*counter)
+ : "r"(counter), "r"(u), "r"(a)
+ : "cc");
+ return oldval;
+}
+
+int m = 5, n = 1;
+
+int main(void)
+{
+ __CPROVER_assert(fetch_add_unless(&m, 2, 0) == 5, "returns the old value");
+ __CPROVER_assert(m == 7, "adds unless equal");
+ __CPROVER_assert(fetch_add_unless(&m, 2, 7) == 7, "returns the value");
+ __CPROVER_assert(m == 7, "does not add if equal");
+
+ // a store between the load-exclusive and the store-exclusive
+ int t;
+ unsigned int status;
+ __asm__ volatile(
+ "ldrex %0, [%2]\n"
+ " str %0, [%3]\n"
+ " strex %1, %4, [%2]"
+ : "=&r"(t), "=&r"(status)
+ : "r"(&m), "r"(&n), "r"(8)
+ : "memory");
+ __CPROVER_assert(n == 7, "plain store");
+ __CPROVER_assert((status == 0) == (m == 8), "status matches the store");
+#ifdef __CPROVER__
+ __CPROVER_assert(status == 0, "store-exclusive succeeds"); // fails
+ __CPROVER_assert(status == 1, "store-exclusive fails"); // fails
+#endif
+
+ return 0;
+}
diff --git a/regression/cbmc/asm_arm_exclusive_abandoned/test.desc b/regression/cbmc/asm_arm_exclusive_abandoned/test.desc
new file mode 100644
index 000000000000..54aa844916e1
--- /dev/null
+++ b/regression/cbmc/asm_arm_exclusive_abandoned/test.desc
@@ -0,0 +1,22 @@
+CORE gcc-only no-new-smt
+main.c
+--arch arm --unwind 2 --no-unwinding-assertions
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 37 returns the old value: SUCCESS$
+^\[main\.assertion\.2\] line 38 adds unless equal: SUCCESS$
+^\[main\.assertion\.3\] line 39 returns the value: SUCCESS$
+^\[main\.assertion\.4\] line 40 does not add if equal: SUCCESS$
+^\[main\.assertion\.5\] line 52 plain store: SUCCESS$
+^\[main\.assertion\.6\] line 53 status matches the store: SUCCESS$
+^\[main\.assertion\.7\] line 55 store-exclusive succeeds: FAILURE$
+^\[main\.assertion\.8\] line 56 store-exclusive fails: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly
+--
+The statements are interpreted. The program, built without __CPROVER__ by
+arm-linux-gnueabihf-gcc -O2 (-marm and -mthumb), passes on qemu-arm, where the
+store-exclusive after the plain store succeeds; the architecture permits it to
+fail.
diff --git a/regression/cbmc/asm_arm_instructions/main.c b/regression/cbmc/asm_arm_instructions/main.c
new file mode 100644
index 000000000000..84acfb9e47b4
--- /dev/null
+++ b/regression/cbmc/asm_arm_instructions/main.c
@@ -0,0 +1,154 @@
+// Instructions and forms of the AArch32 semantics, each case in a function of
+// its own; a target attribute marks cases that exist in one instruction set
+// only. "fails" marks properties that the architecture permits to be
+// violated, so CBMC must not prove them.
+
+unsigned nondet_unsigned(void);
+
+// The width qualifiers .w and .n exist in unified syntax only, so that the
+// add does not set the flags.
+__attribute__((target("thumb"), noinline)) static void width_qualifiers(void)
+{
+ unsigned x = nondet_unsigned(), r;
+ __asm__("cmp %1, %1\n add.w %0, %1, #1\n beq.n 1f\n mov %0, #7\n1:"
+ : "=&r"(r)
+ : "r"(x)
+ : "cc");
+ __CPROVER_assert(r == x + 1, "add.w");
+}
+
+// The shifts with an s suffix set C to the last bit shifted out, which is 0
+// for lsl and lsr by more than 32; a shift by 0 keeps C.
+static void shift_carry(void)
+{
+ unsigned x = nondet_unsigned(), r;
+ _Bool c;
+ __asm__("lsls %0, %2, #1" : "=r"(r), "=@cccs"(c) : "r"(x));
+ __CPROVER_assert(c == x >> 31, "lsls #1");
+ __asm__("lsrs %0, %2, #32" : "=r"(r), "=@cccs"(c) : "r"(x));
+ __CPROVER_assert(c == x >> 31, "lsrs #32");
+ __asm__("asrs %0, %2, #3" : "=r"(r), "=@cccs"(c) : "r"(x));
+ __CPROVER_assert(c == ((x >> 2) & 1), "asrs #3");
+ __asm__("rors %0, %2, #1" : "=r"(r), "=@cccs"(c) : "r"(x));
+ __CPROVER_assert(c == (x & 1), "rors #1");
+ // shifts by a register use its bottom byte
+ __asm__("lsls %0, %2, %3" : "=r"(r), "=@cccs"(c) : "r"(x), "r"(0x120));
+ __CPROVER_assert(c == (x & 1), "lsls by 32");
+ __asm__("lsrs %0, %2, %3" : "=r"(r), "=@cccs"(c) : "r"(x), "r"(33));
+ __CPROVER_assert(!c && r == 0, "lsrs by 33");
+ __asm__("asrs %0, %2, %3" : "=r"(r), "=@cccs"(c) : "r"(x), "r"(200));
+ __CPROVER_assert(c == x >> 31, "asrs by 200");
+ __asm__("rors %0, %2, %3" : "=r"(r), "=@cccs"(c) : "r"(x), "r"(64));
+ __CPROVER_assert(c == x >> 31 && r == x, "rors by 64");
+ __asm__("cmp %2, %2\n lsrs %0, %2, %3"
+ : "=r"(r), "=@cccs"(c)
+ : "r"(x), "r"(0x100));
+ __CPROVER_assert(c && r == x, "lsrs by 0");
+}
+
+// cbz and cbnz (T32 only) branch if a register is zero, or is not zero
+__attribute__((target("thumb"), noinline)) static void compare_and_branch(void)
+{
+ unsigned x = nondet_unsigned(), r, s;
+ __asm__(
+ "mov %0, #1\n cbz %2, 1f\n mov %0, #2\n1: mov %1, #1\n"
+ " cbnz %2, 2f\n mov %1, #2\n2:"
+ : "=&l"(r), "=&l"(s)
+ : "l"(x));
+ __CPROVER_assert(r == (x == 0 ? 1 : 2), "cbz");
+ __CPROVER_assert(s == (x == 0 ? 2 : 1), "cbnz");
+}
+
+// in divided syntax (A32 only), the s suffix may follow the condition
+__attribute__((target("arm"), noinline)) static void divided_suffixes(void)
+{
+ unsigned x = nondet_unsigned(), r = 0;
+ _Bool z;
+ __asm__("cmp %2, #0\n addeqs %0, %0, #1" : "+r"(r), "=@cceq"(z) : "r"(x));
+ __CPROVER_assert(r == (x == 0) && !z, "addeqs");
+}
+
+static void data_processing(void)
+{
+ unsigned x = nondet_unsigned(), y = nondet_unsigned(), mv, ng, sh, ub;
+ __asm__("mvn %0, %4\n neg %1, %4\n sxth %2, %4\n uxtb %3, %4"
+ : "=&r"(mv), "=&r"(ng), "=&r"(sh), "=&r"(ub)
+ : "r"(x));
+ __CPROVER_assert(mv == ~x && ng == -x, "mvn neg");
+ __CPROVER_assert(
+ sh == (unsigned)(int)(short)x && ub == (x & 0xff), "sxth uxtb");
+ _Bool z, c, n;
+ __asm__("cmn %3, %4"
+ : "=@cceq"(z), "=@cccs"(c), "=@ccmi"(n)
+ : "r"(x), "r"(y));
+ __CPROVER_assert(
+ z == (x + y == 0) && c == (x + y < x) && n == (x + y) >> 31, "cmn");
+ __asm__("tst %2, %3" : "=@cceq"(z), "=@ccmi"(n) : "r"(x), "r"(y));
+ __CPROVER_assert(z == ((x & y) == 0) && n == (x & y) >> 31, "tst");
+}
+
+// loads and stores with immediate and register offsets, and preload hints
+static void loads_and_stores(void)
+{
+ short h[3] = {-2, 0x1234, 0};
+ unsigned char b[4] = {1, 2, 3, 4};
+ unsigned v, w;
+ __asm__ volatile(
+ "ldrsh %0, [%2]\n strh %0, [%2, #4]\n ldrb %1, [%3, %4]\n"
+ " strb %1, [%3, %5]"
+ : "=&r"(v), "=&r"(w)
+ : "r"(h), "r"(b), "r"(2), "r"(0)
+ : "memory");
+ __CPROVER_assert(v == 0xfffffffeu && h[2] == -2, "ldrsh strh");
+ __CPROVER_assert(w == 3 && b[0] == 3 && b[1] == 2, "register offsets");
+ // preload hints do not access memory
+ __asm__ volatile("pld [%0]\n pld [%0, #64]" : : "r"(b));
+ __CPROVER_assert(b[0] == 3, "pld");
+}
+
+// exclusive loads and stores of bytes and halfwords
+static void exclusive_bytes_and_halfwords(void)
+{
+ unsigned char b = 0xff;
+ unsigned short h = 0xffff;
+ unsigned v, s, w, t;
+ __asm__ volatile(
+ "ldrexb %0, [%4]\n add %0, %0, #1\n strexb %1, %0, [%4]\n"
+ " ldrexh %2, [%5]\n add %2, %2, #2\n strexh %3, %2, [%5]"
+ : "=&r"(v), "=&r"(s), "=&r"(w), "=&r"(t)
+ : "r"(&b), "r"(&h)
+ : "memory");
+ __CPROVER_assert(v == 0x100 && w == 0x10001, "ldrexb ldrexh");
+ __CPROVER_assert((s == 0) == (b == 0), "strexb");
+ __CPROVER_assert((t == 0) == (h == 1), "strexh");
+ // the store-exclusive may fail spuriously
+ __CPROVER_assert(b == 0, "strexb stores"); // fails
+}
+
+// asm goto jumps to a label of its label list
+static int is_nonzero(unsigned x)
+{
+ __asm__ goto("cmp %0, #0\n beq %l[zero]" : : "r"(x) : "cc" : zero);
+ return 1;
+zero:
+ return 0;
+}
+
+static void asm_goto(void)
+{
+ unsigned x = nondet_unsigned();
+ __CPROVER_assert(is_nonzero(x) == (x != 0), "asm goto");
+}
+
+int main(void)
+{
+ width_qualifiers();
+ shift_carry();
+ compare_and_branch();
+ divided_suffixes();
+ data_processing();
+ loads_and_stores();
+ exclusive_bytes_and_halfwords();
+ asm_goto();
+ return 0;
+}
diff --git a/regression/cbmc/asm_arm_instructions/test.desc b/regression/cbmc/asm_arm_instructions/test.desc
new file mode 100644
index 000000000000..8162ffdfe756
--- /dev/null
+++ b/regression/cbmc/asm_arm_instructions/test.desc
@@ -0,0 +1,40 @@
+CORE gcc-only no-new-smt
+main.c
+--arch arm
+^EXIT=10$
+^SIGNAL=0$
+^\[width_qualifiers\.assertion\.1\] line 17 add\.w: SUCCESS$
+^\[shift_carry\.assertion\.1\] line 27 lsls #1: SUCCESS$
+^\[shift_carry\.assertion\.2\] line 29 lsrs #32: SUCCESS$
+^\[shift_carry\.assertion\.3\] line 31 asrs #3: SUCCESS$
+^\[shift_carry\.assertion\.4\] line 33 rors #1: SUCCESS$
+^\[shift_carry\.assertion\.5\] line 36 lsls by 32: SUCCESS$
+^\[shift_carry\.assertion\.6\] line 38 lsrs by 33: SUCCESS$
+^\[shift_carry\.assertion\.7\] line 40 asrs by 200: SUCCESS$
+^\[shift_carry\.assertion\.8\] line 42 rors by 64: SUCCESS$
+^\[shift_carry\.assertion\.9\] line 46 lsrs by 0: SUCCESS$
+^\[compare_and_branch\.assertion\.1\] line 58 cbz: SUCCESS$
+^\[compare_and_branch\.assertion\.2\] line 59 cbnz: SUCCESS$
+^\[divided_suffixes\.assertion\.1\] line 68 addeqs: SUCCESS$
+^\[data_processing\.assertion\.1\] line 77 mvn neg: SUCCESS$
+^\[data_processing\.assertion\.2\] line 78 sxth uxtb: SUCCESS$
+^\[data_processing\.assertion\.3\] line 84 cmn: SUCCESS$
+^\[data_processing\.assertion\.4\] line 87 tst: SUCCESS$
+^\[loads_and_stores\.assertion\.1\] line 102 ldrsh strh: SUCCESS$
+^\[loads_and_stores\.assertion\.2\] line 103 register offsets: SUCCESS$
+^\[loads_and_stores\.assertion\.3\] line 106 pld: SUCCESS$
+^\[exclusive_bytes_and_halfwords\.assertion\.1\] line 121 ldrexb ldrexh: SUCCESS$
+^\[exclusive_bytes_and_halfwords\.assertion\.2\] line 122 strexb: SUCCESS$
+^\[exclusive_bytes_and_halfwords\.assertion\.3\] line 123 strexh: SUCCESS$
+^\[exclusive_bytes_and_halfwords\.assertion\.4\] line 125 strexb stores: FAILURE$
+^\[asm_goto\.assertion\.1\] line 140 asm goto: SUCCESS$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly statement
+--
+The properties marked "fails" in main.c may be violated on hardware; all
+others hold on hardware. The results were validated with qemu-arm, for code
+compiled by GCC for A32 and T32 (-marm and -mthumb, with the functions that
+have a target attribute in that instruction set only), with nondet_unsigned
+returning each of eight values (0, 1, 41, 0x7fffffff, 0x80000001, ...) first.
diff --git a/regression/cbmc/asm_arm_system/fences.desc b/regression/cbmc/asm_arm_system/fences.desc
new file mode 100644
index 000000000000..3bce46579efd
--- /dev/null
+++ b/regression/cbmc/asm_arm_system/fences.desc
@@ -0,0 +1,12 @@
+CORE gcc-only
+main.c
+--arch arm --show-goto-functions
+^EXIT=0$
+^SIGNAL=0$
+^ *FENCE WW RR RW WR$
+^ *FENCE$
+--
+^warning: ignoring
+--
+The CP15 forms of dmb and dsb are full fences, and that of isb is an empty
+fence.
diff --git a/regression/cbmc/asm_arm_system/main.c b/regression/cbmc/asm_arm_system/main.c
new file mode 100644
index 000000000000..84374a3cec69
--- /dev/null
+++ b/regression/cbmc/asm_arm_system/main.c
@@ -0,0 +1,96 @@
+// System instructions of the AArch32 semantics: the special registers, the
+// interrupt masks, and the coprocessor operations. "fails" marks properties
+// that the architecture permits to be violated, so CBMC must not prove them.
+
+unsigned x;
+
+int main(void)
+{
+ unsigned r = 0, t;
+ _Bool z;
+
+ // the interrupt masks have no effect on the modeled state
+ __asm__ volatile("cmp %1, %1\n cpsid i\n cpsie if"
+ : "=@cceq"(z)
+ : "r"(x)
+ : "memory");
+ __CPROVER_assert(z, "cpsid and cpsie");
+ // with a mode operand, cps changes the processor mode
+ __asm__ volatile("cpsid i, #0x13");
+
+ // msr of the control field keeps the flags, msr of cpsr_fc sets them
+ __asm__ volatile("cmp %2, %2\n mrs %1, cpsr\n msr cpsr_c, %1"
+ : "=@cceq"(z), "=&r"(t)
+ : "r"(x)
+ : "memory");
+ __CPROVER_assert(z, "msr cpsr_c");
+ __asm__ volatile("cmp %1, %1\n msr cpsr_c, %2"
+ : "=@cceq"(z)
+ : "r"(x), "r"(0x10));
+ __CPROVER_assert(z, "msr cpsr_c of user mode");
+ __asm__ volatile(
+ "mrs %0, cpsr\n bic %0, %0, #0xf0000000\n"
+ " orr %0, %0, #0x40000000\n msr cpsr_fc, %0"
+ : "=&r"(t), "=@cceq"(z));
+ __CPROVER_assert(z, "msr cpsr_fc");
+ // a statement that changes the mode must not name r8 to r14, which are
+ // banked
+ __asm__ volatile("mrs %0, cpsr\n msr cpsr_c, %0\n mov r8, #1"
+ : "=&r"(t)
+ :
+ : "r8");
+ // other special registers are not modeled
+ __asm__ volatile("mrs %0, spsr" : "=r"(r));
+ __CPROVER_assert(r == 0, "mrs spsr"); // fails
+ __asm__ volatile("msr spsr_c, %0" : : "r"(0));
+
+ // the value of a coprocessor register is not modeled
+ __asm__ volatile("mrc p15, 0, %0, c13, c0, 3" : "=r"(r));
+ __CPROVER_assert(r == 0, "mrc"); // fails
+
+ // CP15 barriers (DMB, DSB, ISB)
+ x = 1;
+ __asm__ volatile(
+ "mcr p15, 0, %0, c7, c10, 5\n mcr p15, 0, %0, c7, c10, 4\n"
+ " mcr p15, 0, %0, c7, c5, 4"
+ :
+ : "r"(0)
+ : "memory");
+ __CPROVER_assert(x == 1, "CP15 barriers");
+ // Cleaning data caches, cleaning and invalidating them, and invalidating
+ // instruction caches and branch predictors do not change memory.
+ __asm__ volatile(
+ "mcr p15, 0, %0, c7, c10, 1\n mcr p15, 0, %0, c7, c14, 1\n"
+ " mcr p15, 0, %0, c7, c11, 1\n mcr p15, 0, %0, c7, c5, 0\n"
+ " mcr p15, 0, %0, c7, c5, 6"
+ :
+ : "r"(&x)
+ : "memory");
+ __CPROVER_assert(x == 1, "cache clean");
+ // Invalidating data or unified caches without cleaning them may discard
+ // dirty lines, so that memory reverts to older values.
+ __asm__ volatile("mcr p15, 0, %0, c7, c6, 2" : : "r"(0) : "memory");
+ __CPROVER_assert(x == 1, "invalidate by set/way"); // fails
+ x = 1;
+ __asm__ volatile("mcr p15, 0, %0, c7, c7, 0" : : "r"(0) : "memory");
+ __CPROVER_assert(x == 1, "invalidate both caches"); // fails
+ x = 1;
+ // other c7 operations, such as address translation, are not modeled
+ __asm__ volatile("mcr p15, 0, %0, c7, c8, 0" : : "r"(0));
+ // TLB maintenance does not change memory
+ __asm__ volatile("mcr p15, 0, %0, c8, c7, 0" : : "r"(0) : "memory");
+ __CPROVER_assert(x == 1, "TLB maintenance");
+ // nor do the thread ID registers
+ __asm__ volatile("mrc p15, 0, %0, c13, c0, 2\n mcr p15, 0, %0, c13, c0, 2"
+ : "=&r"(t)
+ :
+ : "memory");
+ __CPROVER_assert(x == 1, "thread ID");
+ // other CP15 registers, other values of opc1, and other coprocessors are
+ // not modeled
+ __asm__ volatile("mcr p15, 0, %0, c1, c0, 0" : : "r"(0));
+ __asm__ volatile("mcr p15, 2, %0, c0, c0, 0" : : "r"(0));
+ __asm__ volatile("mcr p14, 0, %0, c0, c5, 0" : : "r"(0));
+
+ return 0;
+}
diff --git a/regression/cbmc/asm_arm_system/test.desc b/regression/cbmc/asm_arm_system/test.desc
new file mode 100644
index 000000000000..5e685030d23b
--- /dev/null
+++ b/regression/cbmc/asm_arm_system/test.desc
@@ -0,0 +1,35 @@
+CORE gcc-only no-new-smt
+main.c
+--arch arm
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 17 cpsid and cpsie: SUCCESS$
+^\[main\.assertion\.2\] line 26 msr cpsr_c: SUCCESS$
+^\[main\.assertion\.3\] line 30 msr cpsr_c of user mode: SUCCESS$
+^\[main\.assertion\.4\] line 35 msr cpsr_fc: SUCCESS$
+^\[main\.assertion\.5\] line 44 mrs spsr: FAILURE$
+^\[main\.assertion\.6\] line 49 mrc: FAILURE$
+^\[main\.assertion\.7\] line 59 CP15 barriers: SUCCESS$
+^\[main\.assertion\.8\] line 69 cache clean: SUCCESS$
+^\[main\.assertion\.9\] line 73 invalidate by set/way: FAILURE$
+^\[main\.assertion\.10\] line 76 invalidate both caches: FAILURE$
+^\[main\.assertion\.11\] line 82 TLB maintenance: SUCCESS$
+^\[main\.assertion\.12\] line 88 thread ID: SUCCESS$
+approximating inline assembly statement at .* line 19 function main: unsupported operands of cpsid
+approximating inline assembly statement at .* line 38 function main: msr of the processor mode with named registers r8 to r14
+approximating inline assembly statement at .* line 45 function main: unsupported special register spsr_c
+approximating inline assembly statement at .* line 72 function main: cache invalidation without cleaning
+approximating inline assembly statement at .* line 75 function main: cache invalidation without cleaning
+approximating inline assembly statement at .* line 79 function main: unsupported coprocessor operation
+approximating inline assembly statement at .* line 91 function main: unsupported coprocessor operation
+approximating inline assembly statement at .* line 92 function main: unsupported coprocessor operation
+approximating inline assembly statement at .* line 93 function main: unsupported coprocessor operation
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly statement at .* line (13|22|27|31|43|48|53|62|81|84)
+--
+The properties marked "fails" in main.c may be violated on hardware; all
+others hold on hardware. The results were validated with qemu-arm in user
+mode, for A32 and T32 code compiled by GCC, where the privileged instructions
+(including the CP15 cache and TLB maintenance) were skipped.
diff --git a/regression/cbmc/asm_block_comment/main.c b/regression/cbmc/asm_block_comment/main.c
new file mode 100644
index 000000000000..16c5be200072
--- /dev/null
+++ b/regression/cbmc/asm_block_comment/main.c
@@ -0,0 +1,20 @@
+// In MSVC-style blocks (and asm blocks in braces), ';' starts a comment that
+// extends to the end of the line, as in MASM: it does not separate
+// instructions.
+int main(void)
+{
+ int x = 0;
+ // the comment names x and an instruction that is not modeled
+ __asm { mfence ; mov x, 1 }
+ __CPROVER_assert(x == 0, "x unchanged");
+
+ int y = 0;
+ // the comment ends at the end of the line
+ __asm {
+ mfence ; a comment
+ mov y, 1
+ }
+ __CPROVER_assert(y == 0, "y unchanged"); // fails
+
+ return 0;
+}
diff --git a/regression/cbmc/asm_block_comment/test.desc b/regression/cbmc/asm_block_comment/test.desc
new file mode 100644
index 000000000000..940c82274fa5
--- /dev/null
+++ b/regression/cbmc/asm_block_comment/test.desc
@@ -0,0 +1,17 @@
+CORE
+main.c
+
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 9 x unchanged: SUCCESS$
+^\[main\.assertion\.2\] line 17 y unchanged: FAILURE$
+^approximating inline assembly statement at file main\.c line 13 function main: instruction 'mov' is not modeled, so the local variables it names,
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly statement at file main\.c line 8
+--
+In MASM syntax, which MSVC-style blocks use, ';' starts a comment. The
+comment in the first block was taken as an instruction (mov x, 1), so the
+block was approximated and x changed. The second block shows that the
+comment ends at the end of the line.
diff --git a/regression/cbmc/asm_block_function_body/main.c b/regression/cbmc/asm_block_function_body/main.c
new file mode 100644
index 000000000000..3d35230d94c1
--- /dev/null
+++ b/regression/cbmc/asm_block_function_body/main.c
@@ -0,0 +1,23 @@
+#ifdef __GNUC__
+// an asm block as a function body, as with Apple gcc -fasm-blocks: like an
+// MSVC block, it uses Intel syntax and names the parameters
+// clang-format off
+void asm test(int *p)
+{
+ mov eax, p
+ mov dword ptr [eax], 1
+}
+// clang-format on
+#endif
+
+int main()
+{
+ int x = 0;
+#ifdef __GNUC__
+ test(&x);
+#else
+ x = 1;
+#endif
+ __CPROVER_assert(x == 0, "x unchanged"); // fails
+ return 0;
+}
diff --git a/regression/cbmc/asm_block_function_body/test.desc b/regression/cbmc/asm_block_function_body/test.desc
new file mode 100644
index 000000000000..c19dc6878910
--- /dev/null
+++ b/regression/cbmc/asm_block_function_body/test.desc
@@ -0,0 +1,17 @@
+CORE
+main.c
+--arch riscv64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 21 x unchanged: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+Invariant check failed
+--
+An asm block that forms a function body (as with gcc -fasm-blocks) used to
+have no assembler flavor, which made remove_asm fail an invariant. Such a
+block uses Intel syntax and names C variables, as an MSVC block does, so it
+has the msc flavor: the parameter p it names is treated as an operand of
+pointer type, and x, which p points to, may be changed. As gcc-style basic
+assembly, the statement did not change x.
diff --git a/regression/cbmc/asm_block_gcc/main.c b/regression/cbmc/asm_block_gcc/main.c
new file mode 100644
index 000000000000..869e366e0853
--- /dev/null
+++ b/regression/cbmc/asm_block_gcc/main.c
@@ -0,0 +1,22 @@
+int main(void)
+{
+ // an Intel-syntax block (gcc and clang -fasm-blocks, MSVC) names the
+ // variables it accesses
+ int x = 0;
+ __asm { mov x, 1 }
+ __CPROVER_assert(x == 0, "x unchanged"); // fails
+
+ int y = 0;
+#ifdef __GNUC__
+ __asm__ { mov y, 1 }
+#else
+ y = 1;
+#endif
+ __CPROVER_assert(y == 0, "y unchanged"); // fails
+
+ int z = 0;
+ __asm { mov eax, 1 }
+ __CPROVER_assert(z == 0, "z unchanged"); // holds
+
+ return 0;
+}
diff --git a/regression/cbmc/asm_block_gcc/test.desc b/regression/cbmc/asm_block_gcc/test.desc
new file mode 100644
index 000000000000..3620f75960b7
--- /dev/null
+++ b/regression/cbmc/asm_block_gcc/test.desc
@@ -0,0 +1,19 @@
+CORE
+main.c
+
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 7 x unchanged: FAILURE$
+^\[main\.assertion\.2\] line 15 y unchanged: FAILURE$
+^\[main\.assertion\.3\] line 19 z unchanged: SUCCESS$
+^approximating inline assembly statement at file main\.c line 6 function main: instruction 'mov' is not modeled, so the local variables it names,
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+Invariant check failed
+--
+In gcc mode, an asm block in braces (as with gcc and clang -fasm-blocks) was
+treated as gcc-style basic assembly, which only changes objects of static
+lifetime and address-taken local variables, so x and y kept their values.
+Such a block uses Intel syntax and names C variables, as an MSVC block does,
+so it now has the msc flavor and changes the local variables it names.
diff --git a/regression/cbmc/asm_i386_64bit_instructions/main.i b/regression/cbmc/asm_i386_64bit_instructions/main.i
new file mode 100644
index 000000000000..3e7ab1234c5a
--- /dev/null
+++ b/regression/cbmc/asm_i386_64bit_instructions/main.i
@@ -0,0 +1,12 @@
+// 64-bit instructions are not valid in 32-bit mode: they are not modeled.
+int main(void)
+{
+ unsigned a = 5, d = 0;
+ __asm__("cltq" : "+a"(a));
+ __asm__("cqto" : "+a"(a), "=d"(d));
+ __asm__("movzbq %b1,%0" : "=r"(d) : "q"(a));
+ unsigned b = 1;
+ __asm__("addl $1,%0" : "+r"(b) : : "cc");
+ __CPROVER_assert(b == 2, "32-bit instruction");
+ return 0;
+}
diff --git a/regression/cbmc/asm_i386_64bit_instructions/test.desc b/regression/cbmc/asm_i386_64bit_instructions/test.desc
new file mode 100644
index 000000000000..ac7a1905de95
--- /dev/null
+++ b/regression/cbmc/asm_i386_64bit_instructions/test.desc
@@ -0,0 +1,16 @@
+CORE gcc-only
+main.i
+--arch i386
+^EXIT=0$
+^SIGNAL=0$
+approximating inline assembly statement at .* line 5 function main: 64-bit instruction in 32-bit mode
+approximating inline assembly statement at .* line 6 function main: 64-bit instruction in 32-bit mode
+approximating inline assembly statement at .* line 7 function main: 64-bit instruction in 32-bit mode
+^\[main\.assertion\.1\] line 10 32-bit instruction: SUCCESS$
+^VERIFICATION SUCCESSFUL$
+--
+^warning: ignoring
+approximating inline assembly statement at .* line 9
+--
+Instructions with 64-bit operands are not valid in 32-bit mode, and are not
+modeled; the 32-bit addl is.
diff --git a/regression/cbmc/asm_operand_side_effects/main.c b/regression/cbmc/asm_operand_side_effects/main.c
new file mode 100644
index 000000000000..9888f50e4651
--- /dev/null
+++ b/regression/cbmc/asm_operand_side_effects/main.c
@@ -0,0 +1,26 @@
+int calls;
+
+int *f(int *p)
+{
+ ++calls;
+ return p;
+}
+
+int g(void)
+{
+ ++calls;
+ return 1;
+}
+
+int main()
+{
+ int m = 0;
+#ifdef __GNUC__
+ // the operands are evaluated exactly once, before the statement
+ __asm__ volatile("mfence" : "=m"(*f(&m)) : "r"(g()));
+#else
+ calls = 2;
+#endif
+ __CPROVER_assert(calls == 2, "operands are evaluated");
+ return 0;
+}
diff --git a/regression/cbmc/asm_operand_side_effects/test.desc b/regression/cbmc/asm_operand_side_effects/test.desc
new file mode 100644
index 000000000000..c08be45bac8f
--- /dev/null
+++ b/regression/cbmc/asm_operand_side_effects/test.desc
@@ -0,0 +1,11 @@
+CORE
+main.c
+--arch riscv64
+^EXIT=0$
+^SIGNAL=0$
+^VERIFICATION SUCCESSFUL$
+--
+^warning: ignoring
+--
+Side effects in the operands of gcc-style inline assembly used to be lost, as
+the operands were only kept in the (later removed) assembly statement.
diff --git a/regression/cbmc/asm_unknown_address/main.i b/regression/cbmc/asm_unknown_address/main.i
new file mode 100644
index 000000000000..307b81dcb50e
--- /dev/null
+++ b/regression/cbmc/asm_unknown_address/main.i
@@ -0,0 +1,63 @@
+int x, y;
+
+int main(void)
+{
+ int *px = &x;
+ unsigned long c, slot;
+
+ // rcx holds the register variable p, but the statement does not
+ // initialise it: the store is approximated
+ x = 0;
+ {
+ register int *p __asm__("rcx") = &x;
+ __asm__ volatile("movl %0, (%%rcx)" : : "r"(5), "r"(p) : "memory");
+ }
+ __CPROVER_assert(x == 0, "store via register variable");
+
+ // rcx initialised from an operand before the store: interpreted
+ x = 0;
+ __asm__ volatile("mov %0, %%rcx\n movl %1, (%%rcx)"
+ :
+ : "r"(px), "r"(5)
+ : "rcx", "memory");
+ __CPROVER_assert(x == 5, "store via initialised register");
+
+ // a forward branch skips the initialisation
+ x = 0;
+ __asm__ volatile("test %2, %2\n jz 1f\n mov %0, %%rcx\n1: movl %1, (%%rcx)"
+ :
+ : "r"(px), "r"(5), "r"(c)
+ : "rcx", "cc", "memory");
+ __CPROVER_assert(x == 0, "initialisation skipped by forward branch");
+
+ // a backward branch makes rcx unknown at the store
+ x = 0;
+ __asm__ volatile(
+ "mov %0, %%rcx\n1: movl %1, (%%rcx)\n rdtsc\n mov %%rax, %%rcx\n"
+ " test %2, %2\n jnz 1b"
+ :
+ : "r"(px), "r"(5), "r"(c)
+ : "rax", "rcx", "rdx", "cc", "memory");
+ __CPROVER_assert(x == 0, "register made unknown by backward branch");
+
+ // an unknown value stored to memory and loaded as an address
+ x = 0;
+ __asm__ volatile(
+ "rdtsc\n mov %%rax, (%0)\n mov (%0), %%rcx\n movl %1, (%%rcx)"
+ :
+ : "r"(&slot), "r"(5)
+ : "rax", "rcx", "rdx", "memory");
+ __CPROVER_assert(x == 0, "unknown address loaded from memory");
+
+ // a loop with known values only: interpreted
+ y = 0;
+ __asm__ volatile(
+ "mov %0, %%rcx\n mov $3, %%edx\n"
+ "1: addl $1, (%%rcx)\n dec %%edx\n jnz 1b"
+ :
+ : "r"(&y)
+ : "rcx", "rdx", "cc", "memory");
+ __CPROVER_assert(y == 3, "loop with known addresses");
+
+ return 0;
+}
diff --git a/regression/cbmc/asm_unknown_address/test.desc b/regression/cbmc/asm_unknown_address/test.desc
new file mode 100644
index 000000000000..3c7911a5df3d
--- /dev/null
+++ b/regression/cbmc/asm_unknown_address/test.desc
@@ -0,0 +1,25 @@
+CORE gcc-only
+main.i
+--arch x86_64 --unwind 4 --no-unwinding-assertions
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 15 store via register variable: FAILURE$
+^\[main\.assertion\.2\] line 23 store via initialised register: SUCCESS$
+^\[main\.assertion\.3\] line 31 initialisation skipped by forward branch: FAILURE$
+^\[main\.assertion\.4\] line 41 register made unknown by backward branch: FAILURE$
+^\[main\.assertion\.5\] line 50 unknown address loaded from memory: FAILURE$
+^\[main\.assertion\.6\] line 60 loop with known addresses: SUCCESS$
+approximating inline assembly statement at .* line 13 function main: memory access at an address computed from a value that is not modeled
+approximating inline assembly statement at .* line 27 function main: memory access at an address computed from a value that is not modeled
+approximating inline assembly statement at .* line 35 function main: branch with values that are not modeled to code that relies on them
+approximating inline assembly statement at .* line 45 function main: memory access at an address computed from a value that is not modeled
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly statement at .* line (19|54) function main
+--
+A memory access at an address that may depend on a value CBMC does not model
+(here: registers the statement does not initialise, or the result of rdtsc)
+would change no object, as CBMC cannot relate such an address to an object.
+Such statements are approximated. The assertions that fail may be violated
+on hardware (x or y is then changed); the others hold on hardware.
diff --git a/regression/cbmc/asm_x86/main.i b/regression/cbmc/asm_x86/main.i
new file mode 100644
index 000000000000..adbb5e869138
--- /dev/null
+++ b/regression/cbmc/asm_x86/main.i
@@ -0,0 +1,454 @@
+// x86 inline assembly that is interpreted exactly. The expected values were
+// observed on x86-64 hardware.
+
+// x86 lock-or sets a bit (atomic_set_long)
+void asm_case_0(unsigned long long *io)
+{
+ unsigned long m = (unsigned long)io[0];
+ unsigned long v = (unsigned long)io[1];
+ __asm__ __volatile__("lock; orq %1,%0" : "+m"(m) : "ir"(v) : "cc");
+ io[0] = (unsigned long long)m;
+ io[1] = (unsigned long long)v;
+}
+
+// x86 lock-and clears bits (atomic_clear_long)
+void asm_case_1(unsigned long long *io)
+{
+ unsigned long m = (unsigned long)io[0];
+ unsigned long v = (unsigned long)io[1];
+ __asm__ __volatile__("lock; andq %1,%0" : "+m"(m) : "ir"(v) : "cc");
+ io[0] = (unsigned long long)m;
+ io[1] = (unsigned long long)v;
+}
+
+// x86 lock-add (atomic_add_long)
+void asm_case_2(unsigned long long *io)
+{
+ unsigned long m = (unsigned long)io[0];
+ unsigned long v = (unsigned long)io[1];
+ __asm__ __volatile__("lock; addq %1,%0" : "+m"(m) : "ir"(v) : "cc");
+ io[0] = (unsigned long long)m;
+ io[1] = (unsigned long long)v;
+}
+
+// x86 lock-xadd returns the previous value (atomic_fetchadd)
+void asm_case_3(unsigned long long *io)
+{
+ unsigned int v = (unsigned int)io[0];
+ unsigned int m = (unsigned int)io[1];
+ __asm__ __volatile__("lock; xaddl %0,%1" : "+r"(v), "+m"(m) : : "cc");
+ io[0] = (unsigned long long)v;
+ io[1] = (unsigned long long)m;
+}
+
+// x86 lock-bts sets a bit and reports its old value
+void asm_case_4(unsigned long long *io)
+{
+ unsigned int m = (unsigned int)io[0];
+ unsigned int c = (unsigned int)io[1];
+ unsigned int n = (unsigned int)io[2];
+ __asm__ __volatile__("lock; btsl %2,%0"
+ : "+m"(m), "=@ccc"(c)
+ : "Ir"(n)
+ : "cc");
+ io[0] = (unsigned long long)m;
+ io[1] = (unsigned long long)c;
+ io[2] = (unsigned long long)n;
+}
+
+// x86 bsr finds the highest set bit
+void asm_case_5(unsigned long long *io)
+{
+ unsigned long r = (unsigned long)io[0];
+ unsigned long v = (unsigned long)io[1];
+ __asm__("bsrq %1,%0" : "=r"(r) : "rm"(v));
+ io[0] = (unsigned long long)r;
+ io[1] = (unsigned long long)v;
+}
+
+// x86 add sets the carry flag
+void asm_case_6(unsigned long long *io)
+{
+ unsigned int a = (unsigned int)io[0];
+ unsigned int c = (unsigned int)io[1];
+ unsigned int b = (unsigned int)io[2];
+ __asm__("addl %2,%0; setc %b1" : "+r"(a), "+q"(c) : "r"(b) : "cc");
+ io[0] = (unsigned long long)a;
+ io[1] = (unsigned long long)c;
+ io[2] = (unsigned long long)b;
+}
+
+// x86 shld-free shift: shr by cl
+void asm_case_7(unsigned long long *io)
+{
+ unsigned long a = (unsigned long)io[0];
+ unsigned long n = (unsigned long)io[1];
+ __asm__("shrq %%cl,%0" : "+r"(a) : "c"(n) : "cc");
+ io[0] = (unsigned long long)a;
+ io[1] = (unsigned long long)n;
+}
+
+// x86 bswap reverses byte order
+void asm_case_8(unsigned long long *io)
+{
+ unsigned long a = (unsigned long)io[0];
+ __asm__("bswapq %0" : "+r"(a));
+ io[0] = (unsigned long long)a;
+}
+
+// x86 cmpxchg exchanges on a match (atomic_cmpset)
+void asm_case_9(unsigned long long *io)
+{
+ unsigned int ok = (unsigned int)io[0];
+ unsigned int m = (unsigned int)io[1];
+ unsigned int expected = (unsigned int)io[2];
+ unsigned int desired = (unsigned int)io[3];
+ __asm__ __volatile__("lock; cmpxchgl %3,%1"
+ : "=@ccz"(ok), "+m"(m), "+a"(expected)
+ : "r"(desired)
+ : "cc");
+ io[0] = (unsigned long long)ok;
+ io[1] = (unsigned long long)m;
+ io[2] = (unsigned long long)expected;
+ io[3] = (unsigned long long)desired;
+}
+
+// x86 xchg swaps a register and memory
+void asm_case_10(unsigned long long *io)
+{
+ unsigned int r = (unsigned int)io[0];
+ unsigned int m = (unsigned int)io[1];
+ __asm__ __volatile__("xchgl %0,%1" : "+r"(r), "+m"(m));
+ io[0] = (unsigned long long)r;
+ io[1] = (unsigned long long)m;
+}
+
+// x86 and masks and sets the sign flag
+void asm_case_11(unsigned long long *io)
+{
+ unsigned int a = (unsigned int)io[0];
+ unsigned int s = (unsigned int)io[1];
+ unsigned int b = (unsigned int)io[2];
+ __asm__("andl %2,%0; sets %b1" : "+r"(a), "+q"(s) : "r"(b) : "cc");
+ io[0] = (unsigned long long)a;
+ io[1] = (unsigned long long)s;
+ io[2] = (unsigned long long)b;
+}
+
+// x86 memory operand with a struct field offset
+void asm_case_12(unsigned long long *io)
+{
+ unsigned int lo = (unsigned int)io[0];
+ unsigned int hi = (unsigned int)io[1];
+ unsigned int v = (unsigned int)io[2];
+ struct
+ {
+ unsigned int lo, hi;
+ } s = {(unsigned)io[0], (unsigned)io[1]};
+ __asm__ __volatile__("orl %1,%0" : "+m"(s.hi) : "r"(v) : "cc");
+ io[0] = (unsigned long long)lo;
+ io[1] = (unsigned long long)hi;
+ io[2] = (unsigned long long)v;
+}
+
+int main(void)
+{
+ {
+ unsigned long long io[2] = {0xc2a7af9eab79b005ULL, 0xffULL};
+ asm_case_0(io);
+ __CPROVER_assert(
+ io[0] == 0xc2a7af9eab79b0ffULL && io[1] == 0xffULL,
+ "x86 lock-or sets a bit (atomic_set_long), vector 0");
+ }
+ {
+ unsigned long long io[2] = {0x2ULL, 0x6ffe6666fa72766dULL};
+ asm_case_0(io);
+ __CPROVER_assert(
+ io[0] == 0x6ffe6666fa72766fULL && io[1] == 0x6ffe6666fa72766dULL,
+ "x86 lock-or sets a bit (atomic_set_long), vector 1");
+ }
+ {
+ unsigned long long io[2] = {0x8000000000000000ULL, 0x8000000000000000ULL};
+ asm_case_0(io);
+ __CPROVER_assert(
+ io[0] == 0x8000000000000000ULL && io[1] == 0x8000000000000000ULL,
+ "x86 lock-or sets a bit (atomic_set_long), vector 2");
+ }
+ {
+ unsigned long long io[2] = {0xc2b9546e0f0220f3ULL, 0xf1d5effedcda26fbULL};
+ asm_case_1(io);
+ __CPROVER_assert(
+ io[0] == 0xc091446e0c0220f3ULL && io[1] == 0xf1d5effedcda26fbULL,
+ "x86 lock-and clears bits (atomic_clear_long), vector 0");
+ }
+ {
+ unsigned long long io[2] = {0x7fffffffULL, 0xb8d3af23681c2889ULL};
+ asm_case_1(io);
+ __CPROVER_assert(
+ io[0] == 0x681c2889ULL && io[1] == 0xb8d3af23681c2889ULL,
+ "x86 lock-and clears bits (atomic_clear_long), vector 1");
+ }
+ {
+ unsigned long long io[2] = {0xc60f8fe0a6269527ULL, 0x32f6ffa49275dc84ULL};
+ asm_case_1(io);
+ __CPROVER_assert(
+ io[0] == 0x2068fa082249404ULL && io[1] == 0x32f6ffa49275dc84ULL,
+ "x86 lock-and clears bits (atomic_clear_long), vector 2");
+ }
+ {
+ unsigned long long io[2] = {0xe638bca46bd70d89ULL, 0x2ULL};
+ asm_case_2(io);
+ __CPROVER_assert(
+ io[0] == 0xe638bca46bd70d8bULL && io[1] == 0x2ULL,
+ "x86 lock-add (atomic_add_long), vector 0");
+ }
+ {
+ unsigned long long io[2] = {0x2ULL, 0x17d967b89f01d73dULL};
+ asm_case_2(io);
+ __CPROVER_assert(
+ io[0] == 0x17d967b89f01d73fULL && io[1] == 0x17d967b89f01d73dULL,
+ "x86 lock-add (atomic_add_long), vector 1");
+ }
+ {
+ unsigned long long io[2] = {0x82974e236f761b03ULL, 0x7fULL};
+ asm_case_2(io);
+ __CPROVER_assert(
+ io[0] == 0x82974e236f761b82ULL && io[1] == 0x7fULL,
+ "x86 lock-add (atomic_add_long), vector 2");
+ }
+ {
+ unsigned long long io[2] = {0x38b1f9a35c2855baULL, 0x8a0cb201725c5338ULL};
+ asm_case_3(io);
+ __CPROVER_assert(
+ io[0] == 0x725c5338ULL && io[1] == 0xce84a8f2ULL,
+ "x86 lock-xadd returns the previous value (atomic_fetchadd), vector 0");
+ }
+ {
+ unsigned long long io[2] = {0xb0652c5806a310a8ULL, 0x808332e379bd40dbULL};
+ asm_case_3(io);
+ __CPROVER_assert(
+ io[0] == 0x79bd40dbULL && io[1] == 0x80605183ULL,
+ "x86 lock-xadd returns the previous value (atomic_fetchadd), vector 1");
+ }
+ {
+ unsigned long long io[2] = {0xe586f6e1167ff12eULL, 0xffULL};
+ asm_case_3(io);
+ __CPROVER_assert(
+ io[0] == 0xffULL && io[1] == 0x167ff22dULL,
+ "x86 lock-xadd returns the previous value (atomic_fetchadd), vector 2");
+ }
+ {
+ unsigned long long io[3] = {
+ 0xb8004092f9e22b92ULL, 0xab4d6d46b16b38cbULL, 0x5ULL};
+ asm_case_4(io);
+ __CPROVER_assert(
+ io[0] == 0xf9e22bb2ULL && io[1] == 0x0ULL && io[2] == 0x5ULL,
+ "x86 lock-bts sets a bit and reports its old value, vector 0");
+ }
+ {
+ unsigned long long io[3] = {
+ 0xc662866ad84cf328ULL, 0x8000000000000000ULL, 0x1fULL};
+ asm_case_4(io);
+ __CPROVER_assert(
+ io[0] == 0xd84cf328ULL && io[1] == 0x1ULL && io[2] == 0x1fULL,
+ "x86 lock-bts sets a bit and reports its old value, vector 1");
+ }
+ {
+ unsigned long long io[3] = {
+ 0xd85ee36626a12990ULL, 0x816ffbff11966d8bULL, 0x0ULL};
+ asm_case_4(io);
+ __CPROVER_assert(
+ io[0] == 0x26a12991ULL && io[1] == 0x0ULL && io[2] == 0x0ULL,
+ "x86 lock-bts sets a bit and reports its old value, vector 2");
+ }
+ {
+ unsigned long long io[2] = {0x0ULL, 0x1ULL};
+ asm_case_5(io);
+ __CPROVER_assert(
+ io[0] == 0x0ULL && io[1] == 0x1ULL,
+ "x86 bsr finds the highest set bit, vector 0");
+ }
+ {
+ unsigned long long io[2] = {0x0ULL, 0x8000000000000000ULL};
+ asm_case_5(io);
+ __CPROVER_assert(
+ io[0] == 0x3fULL && io[1] == 0x8000000000000000ULL,
+ "x86 bsr finds the highest set bit, vector 1");
+ }
+ {
+ unsigned long long io[2] = {0x0ULL, 0x1234ULL};
+ asm_case_5(io);
+ __CPROVER_assert(
+ io[0] == 0xcULL && io[1] == 0x1234ULL,
+ "x86 bsr finds the highest set bit, vector 2");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x71985eb13a883919ULL, 0x7fffffffffffffffULL, 0xc42192ad002d93dcULL};
+ asm_case_6(io);
+ __CPROVER_assert(
+ io[0] == 0x3ab5ccf5ULL && io[1] == 0xffffff00ULL && io[2] == 0x2d93dcULL,
+ "x86 add sets the carry flag, vector 0");
+ }
+ {
+ unsigned long long io[3] = {
+ 0xffffffffULL, 0x4f18c672fa42fe7cULL, 0x120984629605b44fULL};
+ asm_case_6(io);
+ __CPROVER_assert(
+ io[0] == 0x9605b44eULL && io[1] == 0xfa42fe01ULL &&
+ io[2] == 0x9605b44fULL,
+ "x86 add sets the carry flag, vector 1");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x4eb832d5079a2474ULL, 0x0ULL, 0xfd64b9e5bd0edb5aULL};
+ asm_case_6(io);
+ __CPROVER_assert(
+ io[0] == 0xc4a8ffceULL && io[1] == 0x0ULL && io[2] == 0xbd0edb5aULL,
+ "x86 add sets the carry flag, vector 2");
+ }
+ {
+ unsigned long long io[2] = {0xd6bc0d35ccec83e4ULL, 0x3fULL};
+ asm_case_7(io);
+ __CPROVER_assert(
+ io[0] == 0x1ULL && io[1] == 0x3fULL,
+ "x86 shld-free shift: shr by cl, vector 0");
+ }
+ {
+ unsigned long long io[2] = {0x2d65a8a1cf4ce48eULL, 0x0ULL};
+ asm_case_7(io);
+ __CPROVER_assert(
+ io[0] == 0x2d65a8a1cf4ce48eULL && io[1] == 0x0ULL,
+ "x86 shld-free shift: shr by cl, vector 1");
+ }
+ {
+ unsigned long long io[2] = {0xdae98f58edaf5ddULL, 0xcULL};
+ asm_case_7(io);
+ __CPROVER_assert(
+ io[0] == 0xdae98f58edafULL && io[1] == 0xcULL,
+ "x86 shld-free shift: shr by cl, vector 2");
+ }
+ {
+ unsigned long long io[1] = {0x71f2223ff559a0f9ULL};
+ asm_case_8(io);
+ __CPROVER_assert(
+ io[0] == 0xf9a059f53f22f271ULL,
+ "x86 bswap reverses byte order, vector 0");
+ }
+ {
+ unsigned long long io[1] = {0xc447a6fbb54b9c52ULL};
+ asm_case_8(io);
+ __CPROVER_assert(
+ io[0] == 0x529c4bb5fba647c4ULL,
+ "x86 bswap reverses byte order, vector 1");
+ }
+ {
+ unsigned long long io[1] = {0xf34de6440ce23540ULL};
+ asm_case_8(io);
+ __CPROVER_assert(
+ io[0] == 0x4035e20c44e64df3ULL,
+ "x86 bswap reverses byte order, vector 2");
+ }
+ {
+ unsigned long long io[4] = {
+ 0x80000000ULL, 0x85c6eecdbd37c64dULL, 0xe051fbafdcfbbb5eULL, 0x7fULL};
+ asm_case_9(io);
+ __CPROVER_assert(
+ io[0] == 0x0ULL && io[1] == 0xbd37c64dULL && io[2] == 0xbd37c64dULL &&
+ io[3] == 0x7fULL,
+ "x86 cmpxchg exchanges on a match (atomic_cmpset), vector 0");
+ }
+ {
+ unsigned long long io[4] = {
+ 0x1ULL, 0x7fULL, 0x7e0269edac79ca6eULL, 0xffffffffffffffffULL};
+ asm_case_9(io);
+ __CPROVER_assert(
+ io[0] == 0x0ULL && io[1] == 0x7fULL && io[2] == 0x7fULL &&
+ io[3] == 0xffffffffULL,
+ "x86 cmpxchg exchanges on a match (atomic_cmpset), vector 1");
+ }
+ {
+ unsigned long long io[4] = {
+ 0xffULL,
+ 0xfc5d96cc82bdb472ULL,
+ 0x7fffffffffffffffULL,
+ 0x2355aca2a0edf46dULL};
+ asm_case_9(io);
+ __CPROVER_assert(
+ io[0] == 0x0ULL && io[1] == 0x82bdb472ULL && io[2] == 0x82bdb472ULL &&
+ io[3] == 0xa0edf46dULL,
+ "x86 cmpxchg exchanges on a match (atomic_cmpset), vector 2");
+ }
+ {
+ unsigned long long io[2] = {0xd29b8652db47179aULL, 0x1ULL};
+ asm_case_10(io);
+ __CPROVER_assert(
+ io[0] == 0x1ULL && io[1] == 0xdb47179aULL,
+ "x86 xchg swaps a register and memory, vector 0");
+ }
+ {
+ unsigned long long io[2] = {0x2a12166c6984b1c3ULL, 0xffffffffffffffffULL};
+ asm_case_10(io);
+ __CPROVER_assert(
+ io[0] == 0xffffffffULL && io[1] == 0x6984b1c3ULL,
+ "x86 xchg swaps a register and memory, vector 1");
+ }
+ {
+ unsigned long long io[2] = {0xd57e59721b5c3f00ULL, 0x8161ebbba4c1b5daULL};
+ asm_case_10(io);
+ __CPROVER_assert(
+ io[0] == 0xa4c1b5daULL && io[1] == 0x1b5c3f00ULL,
+ "x86 xchg swaps a register and memory, vector 2");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x6a08e48807e06ce6ULL, 0x80000000ULL, 0x715314e0aeb3b1f3ULL};
+ asm_case_11(io);
+ __CPROVER_assert(
+ io[0] == 0x6a020e2ULL && io[1] == 0x80000000ULL && io[2] == 0xaeb3b1f3ULL,
+ "x86 and masks and sets the sign flag, vector 0");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x2dac4420420cf8f6ULL, 0x8000000000000000ULL, 0xffffffffffffffffULL};
+ asm_case_11(io);
+ __CPROVER_assert(
+ io[0] == 0x420cf8f6ULL && io[1] == 0x0ULL && io[2] == 0xffffffffULL,
+ "x86 and masks and sets the sign flag, vector 1");
+ }
+ {
+ unsigned long long io[3] = {
+ 0xff19d85a9e12e041ULL, 0x7fULL, 0x7fffffffffffffffULL};
+ asm_case_11(io);
+ __CPROVER_assert(
+ io[0] == 0x9e12e041ULL && io[1] == 0x1ULL && io[2] == 0xffffffffULL,
+ "x86 and masks and sets the sign flag, vector 2");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x67474bfe2512affeULL, 0x0ULL, 0xd9b9851fa699e71cULL};
+ asm_case_12(io);
+ __CPROVER_assert(
+ io[0] == 0x2512affeULL && io[1] == 0x0ULL && io[2] == 0xa699e71cULL,
+ "x86 memory operand with a struct field offset, vector 0");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x32c9fc24972fa9f7ULL, 0x6a5b23a370b4c97bULL, 0x80000000ULL};
+ asm_case_12(io);
+ __CPROVER_assert(
+ io[0] == 0x972fa9f7ULL && io[1] == 0x70b4c97bULL &&
+ io[2] == 0x80000000ULL,
+ "x86 memory operand with a struct field offset, vector 1");
+ }
+ {
+ unsigned long long io[3] = {
+ 0x9c5bc4ac7a21b60aULL, 0x63ce12b6f4f5b47cULL, 0xd2f4c3415c4eb1fdULL};
+ asm_case_12(io);
+ __CPROVER_assert(
+ io[0] == 0x7a21b60aULL && io[1] == 0xf4f5b47cULL &&
+ io[2] == 0x5c4eb1fdULL,
+ "x86 memory operand with a struct field offset, vector 2");
+ }
+ return 0;
+}
diff --git a/regression/cbmc/asm_x86/test.desc b/regression/cbmc/asm_x86/test.desc
new file mode 100644
index 000000000000..6aa55e0b613a
--- /dev/null
+++ b/regression/cbmc/asm_x86/test.desc
@@ -0,0 +1,13 @@
+CORE gcc-only no-new-smt
+main.i
+--arch x86_64
+^EXIT=0$
+^SIGNAL=0$
+^VERIFICATION SUCCESSFUL$
+--
+^warning: ignoring
+approximating inline assembly
+--
+The x86 inline assembly in main.c is interpreted exactly: if CBMC approximated
+any statement (assigning nondeterministic values to its outputs), the
+assertions, which check values observed on x86-64 hardware, would fail.
diff --git a/regression/cbmc/asm_x86_corner_cases/main.i b/regression/cbmc/asm_x86_corner_cases/main.i
new file mode 100644
index 000000000000..4477e46467c8
--- /dev/null
+++ b/regression/cbmc/asm_x86_corner_cases/main.i
@@ -0,0 +1,62 @@
+// Corner cases of the x86 instruction semantics. "fails" marks properties
+// that hardware violates (or that the architecture leaves undefined), so CBMC
+// must not prove them.
+
+// asm goto that is interpreted: the statement jumps if and only if v is 0
+static int branch(int v)
+{
+ __asm__ goto("testl %0,%0; jz %l[zero]" : : "r"(v) : "cc" : zero);
+ return 0;
+zero:
+ return 1;
+}
+
+int main(void)
+{
+ // cmpxchgl in 64-bit mode writes only one of accumulator and destination;
+ // a 32-bit register write zero-extends, so the other keeps its upper half
+ unsigned long long rax = 0x1234567800000005ull; // eax == 5
+ unsigned mem = 5;
+ __asm__ __volatile__("lock cmpxchgl %2,%1"
+ : "+a"(rax), "+m"(mem)
+ : "r"(7u)
+ : "cc", "memory");
+ __CPROVER_assert(mem == 7, "cmpxchg stored");
+ __CPROVER_assert(rax == 0x1234567800000005ull, "accumulator unchanged");
+ unsigned long long acc = 1;
+ unsigned long long dst = 0xabcdef0000000002ull;
+ __asm__("cmpxchgl %k2,%k1" : "+a"(acc), "+r"(dst) : "r"(9ull) : "cc");
+ __CPROVER_assert(acc == 2, "accumulator loaded");
+ __CPROVER_assert(dst == 0xabcdef0000000002ull, "destination unchanged");
+
+ // bsf with a zero source: the destination is undefined (Intel) or unchanged
+ // (AMD), including the bits above a 32-bit destination
+ unsigned long long r = 0xdead000000000000ull;
+ unsigned zero = 0;
+ __asm__("bsfl %1,%k0" : "+r"(r) : "rm"(zero) : "cc");
+ __CPROVER_assert((r >> 32) == 0, "bsf zero-extends"); // fails
+
+ // shifts by more than the width and address arithmetic that wraps are
+ // defined on the hardware: no undefined-behaviour checks apply
+ unsigned x = 1;
+ __asm__("shlb $20,%b0" : "+q"(x) : : "cc");
+ __CPROVER_assert((x & 0xff) == 0, "shlb by 20");
+ unsigned long long a, b = 0x4000000000000000ull;
+ __asm__("leaq (,%1,4),%0" : "=r"(a) : "r"(b));
+ __CPROVER_assert(a == 0, "lea wraps");
+
+ // an array operand in a register is the address of its first element
+ char buf[4] = {0};
+ __asm__ volatile("movb $1,(%0)" : : "r"(buf) : "memory");
+ __CPROVER_assert(buf[0] == 1, "array operand");
+
+ // a register variable bound to a register that the template writes
+ register unsigned long long reg asm("rax") = 5;
+ __asm__ volatile("addq $1, %%rax" : "+r"(reg));
+ __CPROVER_assert(reg == 5, "register variable"); // fails
+
+ __CPROVER_assert(branch(0) == 1, "asm goto jumps");
+ __CPROVER_assert(branch(5) == 0, "asm goto falls through");
+
+ return 0;
+}
diff --git a/regression/cbmc/asm_x86_corner_cases/test.desc b/regression/cbmc/asm_x86_corner_cases/test.desc
new file mode 100644
index 000000000000..caa607db193e
--- /dev/null
+++ b/regression/cbmc/asm_x86_corner_cases/test.desc
@@ -0,0 +1,26 @@
+CORE gcc-only
+main.i
+--arch x86_64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 24 cmpxchg stored: SUCCESS$
+^\[main\.assertion\.2\] line 25 accumulator unchanged: SUCCESS$
+^\[main\.assertion\.3\] line 29 accumulator loaded: SUCCESS$
+^\[main\.assertion\.4\] line 30 destination unchanged: SUCCESS$
+^\[main\.assertion\.5\] line 37 bsf zero-extends: FAILURE$
+^\[main\.assertion\.6\] line 43 shlb by 20: SUCCESS$
+^\[main\.assertion\.7\] line 46 lea wraps: SUCCESS$
+^\[main\.assertion\.8\] line 51 array operand: SUCCESS$
+^\[main\.assertion\.9\] line 56 register variable: FAILURE$
+^\[main\.assertion\.10\] line 58 asm goto jumps: SUCCESS$
+^\[main\.assertion\.11\] line 59 asm goto falls through: SUCCESS$
+approximating inline assembly statement at .* line 55 function main: writes register rax, which is neither an operand nor clobbered
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+undefined-shift.*: FAILURE$
+overflow.*: FAILURE$
+approximating inline assembly statement at .* function branch
+--
+The properties marked "fails" in main.c are violated on hardware (or left
+undefined by the architecture); all others hold on hardware.
diff --git a/regression/cbmc/asm_x86_immediate_syntax/main.i b/regression/cbmc/asm_x86_immediate_syntax/main.i
new file mode 100644
index 000000000000..faeedf5650be
--- /dev/null
+++ b/regression/cbmc/asm_x86_immediate_syntax/main.i
@@ -0,0 +1,53 @@
+// Immediates and displacements as GCC prints and GAS reads them: %n prints
+// the negated constant; 0x, 0b, and a leading 0 select hexadecimal, binary,
+// and octal. The expected values were observed on x86-64 hardware.
+
+__attribute__((noinline)) int negated(int x)
+{
+ __asm__("addl $%n1, %0" : "+r"(x) : "i"(5) : "cc");
+ return x;
+}
+
+__attribute__((noinline)) int octal(int x)
+{
+ __asm__("addl $010, %0" : "+r"(x) : : "cc");
+ return x;
+}
+
+__attribute__((noinline)) int binary_and_hex(int x)
+{
+ __asm__("addl $0b101, %0\n\t"
+ "addl $0x10, %0\n\t"
+ "addl $0, %0"
+ : "+r"(x)
+ :
+ : "cc");
+ return x;
+}
+
+__attribute__((noinline)) int octal_displacement(const int *p)
+{
+ int r;
+ __asm__("movl 010(%1), %0" : "=r"(r) : "r"(p), "m"(*(const int(*)[8])p));
+ return r;
+}
+
+__attribute__((noinline)) int negated_displacement(const int *p)
+{
+ int r;
+ __asm__("movl %n2(%1), %0"
+ : "=r"(r)
+ : "r"(p), "i"(-12), "m"(*(const int(*)[8])p));
+ return r;
+}
+
+int main(void)
+{
+ int a[8] = {0, 1, 2, 3, 4, 5, 6, 7};
+ __CPROVER_assert(negated(0) == -5, "%n negates");
+ __CPROVER_assert(octal(0) == 8, "$010 is octal");
+ __CPROVER_assert(binary_and_hex(0) == 21, "0b and 0x");
+ __CPROVER_assert(octal_displacement(a) == 2, "010(%1) is octal");
+ __CPROVER_assert(negated_displacement(a) == 3, "%n in a displacement");
+ return 0;
+}
diff --git a/regression/cbmc/asm_x86_immediate_syntax/test.desc b/regression/cbmc/asm_x86_immediate_syntax/test.desc
new file mode 100644
index 000000000000..0e5852cca370
--- /dev/null
+++ b/regression/cbmc/asm_x86_immediate_syntax/test.desc
@@ -0,0 +1,18 @@
+CORE gcc-only
+main.i
+--arch x86_64
+^EXIT=0$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 47 %n negates: SUCCESS$
+^\[main\.assertion\.2\] line 48 \$010 is octal: SUCCESS$
+^\[main\.assertion\.3\] line 49 0b and 0x: SUCCESS$
+^\[main\.assertion\.4\] line 50 010\(%1\) is octal: SUCCESS$
+^\[main\.assertion\.5\] line 51 %n in a displacement: SUCCESS$
+^VERIFICATION SUCCESSFUL$
+--
+^warning: ignoring
+approximating inline assembly
+--
+GCC prints %n of a constant as the negated constant, and GAS reads numbers
+with a leading 0 as octal, 0b as binary, and 0x as hexadecimal. The expected
+values were observed on x86-64 hardware.
diff --git a/regression/cbmc/asm_x86_narrow_inputs/main.i b/regression/cbmc/asm_x86_narrow_inputs/main.i
new file mode 100644
index 000000000000..358f66ed7f8b
--- /dev/null
+++ b/regression/cbmc/asm_x86_narrow_inputs/main.i
@@ -0,0 +1,84 @@
+// An input operand narrower than its register leaves the bits of the register
+// above its width unspecified. These bits are read here through an output tied
+// to the input, by the name of the register, by %h, by an instruction that
+// reads the register implicitly, and as an output that shares the register.
+// "fails" marks properties that do not hold for all register allocations;
+// "violated" those that GCC 13 -O0 or -O2 code violates on x86-64.
+
+__attribute__((noinline)) int tied(signed char c)
+{
+ int r;
+ __asm__("addl $1, %0" : "=r"(r) : "0"(c) : "cc");
+ return r;
+}
+
+__attribute__((noinline)) unsigned long tied_int(long y)
+{
+ unsigned long r;
+ __asm__("addq $0, %0" : "=r"(r) : "0"((int)y) : "cc");
+ return r;
+}
+
+__attribute__((noinline)) unsigned long by_name(long y)
+{
+ unsigned long r;
+ __asm__("movq %%rdx, %0" : "=r"(r) : "d"((int)y));
+ return r;
+}
+
+__attribute__((noinline)) unsigned by_name_char(int y)
+{
+ unsigned r;
+ __asm__("movl %%ecx, %0" : "=r"(r) : "c"((signed char)y));
+ return r;
+}
+
+__attribute__((noinline)) unsigned high_byte(unsigned x)
+{
+ unsigned char r;
+ __asm__("movb %h1, %0" : "=q"(r) : "Q"((unsigned char)x));
+ return r;
+}
+
+__attribute__((noinline)) int implicit(int x)
+{
+ int r;
+ // cwtl reads %ax
+ __asm__("cwtl" : "=a"(r) : "a"((unsigned char)x));
+ return r;
+}
+
+__attribute__((noinline)) unsigned shared_output(unsigned x)
+{
+ unsigned r;
+ __asm__("" : "=r"(r) : "0"((unsigned char)x));
+ return r;
+}
+
+__attribute__((noinline)) unsigned char narrow(void)
+{
+ unsigned char r;
+ __asm__("addb $1, %b0" : "=q"(r) : "0"((unsigned char)5) : "cc");
+ return r;
+}
+
+int main(void)
+{
+ // fails, violated at -O2: GCC sign-extends c
+ __CPROVER_assert(tied(-1) == 256, "tied char input zero-extended");
+ // fails, violated at -O0: GCC sign-extends the int
+ __CPROVER_assert(tied_int(-1) == 0xffffffffUL, "tied int input zero-extended");
+ // fails: GCC need not zero-extend
+ __CPROVER_assert(by_name(-1) == 0xffffffffUL, "fixed int input zero-extended");
+ // fails, violated
+ __CPROVER_assert(by_name_char(-1) == 0xffu, "fixed char input zero-extended");
+ // fails, violated: %h reads bits 8 to 15 of the register
+ __CPROVER_assert(high_byte(0x1234) == 0, "high byte of a char is 0");
+ // fails: GCC need not zero-extend
+ __CPROVER_assert(implicit(0x80) == 0x80, "cwtl of a zero-extended char");
+ // fails, violated
+ __CPROVER_assert(shared_output(0x1ff) == 0xff, "output zero-extended");
+ // holds: no bits above the width of the input are read
+ __CPROVER_assert(narrow() == 6, "narrow access");
+ return 0;
+}
diff --git a/regression/cbmc/asm_x86_narrow_inputs/test.desc b/regression/cbmc/asm_x86_narrow_inputs/test.desc
new file mode 100644
index 000000000000..55a7e1c267b2
--- /dev/null
+++ b/regression/cbmc/asm_x86_narrow_inputs/test.desc
@@ -0,0 +1,22 @@
+CORE gcc-only no-new-smt
+main.i
+--arch x86_64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 68 tied char input zero-extended: FAILURE$
+^\[main\.assertion\.2\] line 70 tied int input zero-extended: FAILURE$
+^\[main\.assertion\.3\] line 72 fixed int input zero-extended: FAILURE$
+^\[main\.assertion\.4\] line 74 fixed char input zero-extended: FAILURE$
+^\[main\.assertion\.5\] line 76 high byte of a char is 0: FAILURE$
+^\[main\.assertion\.6\] line 78 cwtl of a zero-extended char: FAILURE$
+^\[main\.assertion\.7\] line 80 output zero-extended: FAILURE$
+^\[main\.assertion\.8\] line 82 narrow access: SUCCESS$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly
+--
+The bits of a register above the width of the input operand it holds are
+unspecified. Where they may be read, they are nondeterministic, so that CBMC
+does not prove the properties that depend on them; where they are not read,
+the input is zero-extended, which keeps a constant input constant.
diff --git a/regression/cbmc/asm_x86_operand_contract/main.i b/regression/cbmc/asm_x86_operand_contract/main.i
new file mode 100644
index 000000000000..a19907c02d27
--- /dev/null
+++ b/regression/cbmc/asm_x86_operand_contract/main.i
@@ -0,0 +1,73 @@
+// GCC assumes that the instructions read all inputs before they write an
+// output without the earlyclobber modifier '&', and that they do not change
+// input-only operands: it may allocate such an output to the register of an
+// input or of the address of a memory operand, and two inputs with the same
+// value to one register. Statements that do not meet these assumptions are
+// approximated. "violated" marks properties that the native code of GCC 13 on
+// x86-64 violates at -O2.
+
+__attribute__((noinline)) int no_earlyclobber(int c)
+{
+ int a, b;
+ __asm__("movl $1, %0\n\tmovl %2, %1" : "=r"(a), "=r"(b) : "r"(c));
+ return b + 0 * a;
+}
+
+__attribute__((noinline)) int earlyclobber(int c)
+{
+ int a, b;
+ __asm__("movl $1, %0\n\tmovl %2, %1" : "=&r"(a), "=r"(b) : "r"(c));
+ return b + 0 * a;
+}
+
+__attribute__((noinline)) int input_write(int x)
+{
+ int a;
+ __asm__("incl %1\n\tmovl %2, %0" : "=r"(a) : "r"(x), "r"(x) : "cc");
+ return a;
+}
+
+__attribute__((noinline)) long shared_address(long *p)
+{
+ long r;
+ __asm__("addq $8, %0\n\tmovq %2, %1" : "+r"(p), "=r"(r) : "m"(*p) : "cc");
+ return r;
+}
+
+__attribute__((noinline)) int other_width(int x, long *p)
+{
+ // x and the address of *p differ in width, so GCC does not give them one
+ // register
+ long r;
+ __asm__("addl $8, %0\n\tmovq %2, %1" : "+r"(x), "=r"(r) : "m"(*p) : "cc");
+ return x + (int)r;
+}
+
+__attribute__((noinline)) int loop(int x)
+{
+ int a;
+ __asm__("movl $2, %%ecx\n"
+ "1: movl %1, %%eax\n\t"
+ "movl %%eax, %0\n\t"
+ "decl %%ecx\n\t"
+ "jnz 1b"
+ : "=r"(a)
+ : "r"(x)
+ : "eax", "ecx", "cc");
+ return a;
+}
+
+int main(void)
+{
+ long v[2] = {1, 2}, w[2] = {1, 2};
+ // violated
+ __CPROVER_assert(no_earlyclobber(7) == 7, "no earlyclobber");
+ __CPROVER_assert(earlyclobber(7) == 7, "earlyclobber");
+ // violated
+ __CPROVER_assert(input_write(7) == 7, "input write");
+ // violated
+ __CPROVER_assert(shared_address(v) == 1, "address in the output register");
+ __CPROVER_assert(other_width(1, w) == 10, "different widths");
+ __CPROVER_assert(loop(3) == 3, "read after write in a loop");
+ return 0;
+}
diff --git a/regression/cbmc/asm_x86_operand_contract/test.desc b/regression/cbmc/asm_x86_operand_contract/test.desc
new file mode 100644
index 000000000000..07830ffcb663
--- /dev/null
+++ b/regression/cbmc/asm_x86_operand_contract/test.desc
@@ -0,0 +1,27 @@
+CORE gcc-only
+main.i
+--arch x86_64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 64 no earlyclobber: FAILURE$
+^\[main\.assertion\.2\] line 65 earlyclobber: SUCCESS$
+^\[main\.assertion\.3\] line 67 input write: FAILURE$
+^\[main\.assertion\.4\] line 69 address in the output register: FAILURE$
+^\[main\.assertion\.5\] line 70 different widths: SUCCESS$
+^\[main\.assertion\.6\] line 71 read after write in a loop: FAILURE$
+approximating inline assembly statement at .* line 12 function no_earlyclobber: output operand %0, which has no earlyclobber modifier '&', is written before input operand %2 is read
+approximating inline assembly statement at .* line 26 function input_write: writes input operand %1
+approximating inline assembly statement at .* line 33 function shared_address: output operand %0, which has no earlyclobber modifier '&', is written before the address of memory operand %2 is read
+approximating inline assembly statement at .* line 49 function loop: output operand %0, which has no earlyclobber modifier '&', is written before input operand %1 is read
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly statement at .* function (earlyclobber|other_width)
+--
+GCC may allocate an output without '&' to the register of an input or of the
+address of a memory operand, and two inputs with the same value to one
+register; with -O2, GCC 13 on x86-64 does so in no_earlyclobber, input_write,
+and shared_address, whose native code violates assertions 1, 3, and 4. Such statements are
+approximated. An output that holds its own value when the statement starts
+shares a register only with an operand of the same value, and hence of the
+same width; the statement in other_width is interpreted.
diff --git a/regression/cbmc/asm_x86_operand_size/main.i b/regression/cbmc/asm_x86_operand_size/main.i
new file mode 100644
index 000000000000..815c6b5d2a04
--- /dev/null
+++ b/regression/cbmc/asm_x86_operand_size/main.i
@@ -0,0 +1,57 @@
+// The operand size of an x86 instruction is given by its suffix or by a
+// register operand other than the count of a shift; GAS does not see the C
+// type of a memory operand. The statements without a suffix and without such
+// a register operand are approximated. The expected values of the others were
+// observed on x86-64 hardware.
+
+__attribute__((noinline)) unsigned shl_mem(unsigned x, unsigned char n)
+{
+ __asm__("shl %%cl, %0" : "+m"(x) : "c"(n) : "cc");
+ return x;
+}
+
+__attribute__((noinline)) unsigned long add_mem(unsigned long x)
+{
+ __asm__("add $1, %0" : "+m"(x) : : "cc");
+ return x;
+}
+
+__attribute__((noinline)) unsigned movzx_mem(unsigned short s)
+{
+ unsigned r;
+ __asm__("movzx %1, %0" : "=r"(r) : "m"(s));
+ return r;
+}
+
+__attribute__((noinline)) unsigned shll_mem(unsigned x, unsigned char n)
+{
+ __asm__("shll %%cl, %0" : "+m"(x) : "c"(n) : "cc");
+ return x;
+}
+
+__attribute__((noinline)) unsigned shl_reg(unsigned x, unsigned char n)
+{
+ __asm__("shl %%cl, %0" : "+r"(x) : "c"(n) : "cc");
+ return x;
+}
+
+__attribute__((noinline)) unsigned long add_reg_mem(unsigned long x)
+{
+ unsigned long one = 1;
+ __asm__("add %1, %0" : "+m"(x) : "r"(one) : "cc");
+ return x;
+}
+
+int main(void)
+{
+ // the size of the count register (8 bits)
+ __CPROVER_assert(shl_mem(0x101, 4) == 0x110, "shl as 8-bit");
+ // the size of the C type (64 bits); GAS uses 32 bits
+ __CPROVER_assert(add_mem(0xffffffffUL) == 0x100000000UL, "add as 64-bit");
+ // the size of the C type (16 bits); GAS uses 8 bits
+ __CPROVER_assert(movzx_mem(0x1234) == 0x1234, "movzx as 16-bit");
+ __CPROVER_assert(shll_mem(0x101, 4) == 0x1010, "shll");
+ __CPROVER_assert(shl_reg(0x101, 4) == 0x1010, "shl of a register");
+ __CPROVER_assert(add_reg_mem(0xffffffffUL) == 0x100000000UL, "add register");
+ return 0;
+}
diff --git a/regression/cbmc/asm_x86_operand_size/test.desc b/regression/cbmc/asm_x86_operand_size/test.desc
new file mode 100644
index 000000000000..06a49b94043e
--- /dev/null
+++ b/regression/cbmc/asm_x86_operand_size/test.desc
@@ -0,0 +1,25 @@
+CORE gcc-only
+main.i
+--arch x86_64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 48 shl as 8-bit: FAILURE$
+^\[main\.assertion\.2\] line 50 add as 64-bit: FAILURE$
+^\[main\.assertion\.3\] line 52 movzx as 16-bit: FAILURE$
+^\[main\.assertion\.4\] line 53 shll: SUCCESS$
+^\[main\.assertion\.5\] line 54 shl of a register: SUCCESS$
+^\[main\.assertion\.6\] line 55 add register: SUCCESS$
+approximating inline assembly statement at .* line 9 function shl_mem: no suffix or register operand gives the operand size
+approximating inline assembly statement at .* line 15 function add_mem: no suffix or register operand gives the operand size
+approximating inline assembly statement at .* line 22 function movzx_mem: no suffix or register operand gives the operand sizes of movzx
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly statement at .* function (shll_mem|shl_reg|add_reg_mem)
+--
+Without a suffix, the operand size is that of a register operand (other than
+the count of a shift). GAS does not see the C type of a memory operand: for
+the statements without a suffix and without such a register operand it uses
+a default size (32 bits for shl and add, 8 bits for the source of movzx),
+which hardware shows to differ from the C type (the first three assertions
+fail on x86-64 hardware). These statements are approximated.
diff --git a/regression/cbmc/asm_x86_output_addresses/main.c b/regression/cbmc/asm_x86_output_addresses/main.c
new file mode 100644
index 000000000000..6a86cb994fa9
--- /dev/null
+++ b/regression/cbmc/asm_x86_output_addresses/main.c
@@ -0,0 +1,15 @@
+// The address of the output arr[idx] depends on the output idx. GCC at -O0
+// computes it after writing idx (arr[1] = 9), GCC at -O2 and Clang compute it
+// before (arr[0] = 9), so both behaviours must be possible.
+int main(void)
+{
+ int idx = 0, arr[2] = {0, 0};
+ asm("movl $1, %0; movl $9, %1" : "=&r"(idx), "=r"(arr[idx]));
+ __CPROVER_assert(
+ !(idx == 1 && arr[0] == 9 && arr[1] == 0),
+ "address before writing idx"); // fails
+ __CPROVER_assert(
+ !(idx == 1 && arr[0] == 0 && arr[1] == 9),
+ "address after writing idx"); // fails
+ return 0;
+}
diff --git a/regression/cbmc/asm_x86_output_addresses/test.desc b/regression/cbmc/asm_x86_output_addresses/test.desc
new file mode 100644
index 000000000000..3621751b39a2
--- /dev/null
+++ b/regression/cbmc/asm_x86_output_addresses/test.desc
@@ -0,0 +1,16 @@
+CORE gcc-only
+main.c
+--arch x86_64
+^EXIT=10$
+^SIGNAL=0$
+^approximating inline assembly statement at file main\.c line 7 function main: the address of output operand 1 depends on the program state, and compilers differ in whether they compute it before or after writing the other outputs, so
+^\[main\.assertion\.1\] line 8 address before writing idx: FAILURE$
+^\[main\.assertion\.2\] line 11 address after writing idx: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+The address of the register output arr[idx] depends on the output idx. GCC
+at -O0 computes it after writing idx, GCC at -O2 and Clang before. The
+interpretation of x86 instructions used to compute it after writing the
+other outputs only, which excluded the behaviour of the other compilers.
diff --git a/regression/cbmc/asm_x86_register_operand_constant/main.i b/regression/cbmc/asm_x86_register_operand_constant/main.i
new file mode 100644
index 000000000000..6af95ec46d01
--- /dev/null
+++ b/regression/cbmc/asm_x86_register_operand_constant/main.i
@@ -0,0 +1,50 @@
+// A 32-bit input operand whose value is a known constant stays a constant in
+// the register that holds it (the upper bits of the register are zero, as no
+// reference reads them). Merging the value into a register with
+// nondeterministic upper bits would make the lock word written by xchg
+// symbolic, so that both spin loops would unwind to the unwinding bound.
+typedef int word_t;
+typedef struct
+{
+ word_t locked;
+} spin_t;
+
+static inline void spin_lock(spin_t *sl)
+{
+ word_t lock_val = 1;
+ asm volatile(
+ "1:\n"
+ "xchg %[locked], %[lv]\n"
+ "test %[lv], %[lv]\n"
+ "jz 3f\n"
+ "2:\n"
+ "pause\n"
+ "cmpl $0, %[locked]\n"
+ "jnz 2b\n"
+ "jmp 1b\n"
+ "3:\n"
+ : [locked] "=m"(sl->locked), [lv] "=q"(lock_val)
+ : "[lv]"(lock_val)
+ : "memory");
+}
+
+static inline void spin_unlock(spin_t *sl)
+{
+ word_t v = 0;
+ asm volatile("xchg %[locked], %[ulv]\n"
+ : [locked] "=m"(sl->locked), [ulv] "=q"(v)
+ : "[ulv]"(v)
+ : "memory");
+}
+
+int main(void)
+{
+ spin_t s;
+ s.locked = 0;
+ for(int i = 0; i < 4; i++)
+ {
+ spin_lock(&s);
+ spin_unlock(&s);
+ }
+ return 0;
+}
diff --git a/regression/cbmc/asm_x86_register_operand_constant/test.desc b/regression/cbmc/asm_x86_register_operand_constant/test.desc
new file mode 100644
index 000000000000..cedfe113e55b
--- /dev/null
+++ b/regression/cbmc/asm_x86_register_operand_constant/test.desc
@@ -0,0 +1,13 @@
+CORE gcc-only
+main.i
+--arch x86_64 --unwind 5 --unwinding-assertions
+^EXIT=0$
+^SIGNAL=0$
+^VERIFICATION SUCCESSFUL$
+--
+^warning: ignoring
+approximating inline assembly
+--
+The outer loop is fully unwound (five iterations cover the four lock/unlock
+pairs), so a failing unwinding assertion could only come from a spin loop that
+symbolic execution cannot resolve because the lock word is not a constant.
diff --git a/regression/cbmc/asm_x86_register_or_memory/main.i b/regression/cbmc/asm_x86_register_or_memory/main.i
new file mode 100644
index 000000000000..df8753f6b5b8
--- /dev/null
+++ b/regression/cbmc/asm_x86_register_or_memory/main.i
@@ -0,0 +1,65 @@
+// For an operand whose constraint allows a register and memory ("rm", "g"),
+// GCC chooses either (GCC 13 chooses a register for a global at -O0 and the
+// global's memory at -O2), and the effect of the instructions may depend on
+// the choice. "fails" marks properties that do not hold for all choices.
+
+long gx = -1;
+long gy = -1;
+
+struct s
+{
+ unsigned word, next;
+};
+
+__attribute__((noinline)) int bit(struct s *p, unsigned nr)
+{
+ unsigned char r;
+ __asm__("btl %2, %1\n\tsetc %0" : "=q"(r) : "rm"(p->word), "r"(nr) : "cc");
+ return r;
+}
+
+__attribute__((noinline)) int rvalue(int x)
+{
+ int r;
+ __asm__("movl %1, %0" : "=r"(r) : "rm"(x + 1));
+ return r;
+}
+
+__attribute__((noinline)) unsigned long immediate(unsigned long x)
+{
+ __asm__("addq %1, %0" : "+r"(x) : "g"(-1) : "cc");
+ return x;
+}
+
+int main(void)
+{
+ // a 32-bit write clears the upper half of a register, not of memory
+ __asm__("movl $1, %k0" : "+rm"(gx));
+ // fails
+ __CPROVER_assert(gx == 1, "register");
+ // fails
+ __CPROVER_assert(gx == 0xffffffff00000001L, "memory");
+ __CPROVER_assert(gx == 1 || gx == 0xffffffff00000001L, "register or memory");
+
+ // in memory, the bit offset addresses a bit string beyond the operand
+ static struct s v = {0, 1};
+ int b = bit(&v, 32);
+ // fails
+ __CPROVER_assert(b == 0, "bit 0 of word");
+ // fails
+ __CPROVER_assert(b == 1, "bit 0 of next");
+ __CPROVER_assert(b == 0 || b == 1, "bit of word or next");
+
+ __CPROVER_assert(rvalue(1) == 2, "rvalue in register or memory");
+
+ // as an immediate, -1 is sign-extended to 64 bits; the register would not
+ // even be accepted by the assembler (addq %edx, %rax)
+ // fails
+ __CPROVER_assert(immediate(1) == 0x100000000UL, "immediate zero-extended");
+
+ // GCC may choose any alternative
+ __asm__("movl $1, %k0" : "+r,m"(gy));
+ // fails
+ __CPROVER_assert(gy == 1, "first alternative");
+ return 0;
+}
diff --git a/regression/cbmc/asm_x86_register_or_memory/test.desc b/regression/cbmc/asm_x86_register_or_memory/test.desc
new file mode 100644
index 000000000000..be874051b9be
--- /dev/null
+++ b/regression/cbmc/asm_x86_register_or_memory/test.desc
@@ -0,0 +1,27 @@
+CORE gcc-only
+main.i
+--arch x86_64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 39 register: FAILURE$
+^\[main\.assertion\.2\] line 41 memory: FAILURE$
+^\[main\.assertion\.3\] line 42 register or memory: SUCCESS$
+^\[main\.assertion\.4\] line 48 bit 0 of word: FAILURE$
+^\[main\.assertion\.5\] line 50 bit 0 of next: FAILURE$
+^\[main\.assertion\.6\] line 51 bit of word or next: SUCCESS$
+^\[main\.assertion\.7\] line 53 rvalue in register or memory: SUCCESS$
+^\[main\.assertion\.8\] line 58 immediate zero-extended: FAILURE$
+^\[main\.assertion\.9\] line 63 first alternative: FAILURE$
+approximating inline assembly statement at .* line 30 function immediate: operand %1, which may be an immediate, is read at a width other than its own
+approximating inline assembly statement at .* line 61 function main: constraint "\+r,m" with several alternatives
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly statement at .* line (17|25|36)
+--
+Both the register and the memory alternative of "rm" operands are modelled:
+the native code of GCC 13 on x86-64 violates assertions 1 and 4 at -O2 (in
+memory) and assertions 2 and 5 at -O0 (in a register). Where the semantics
+would differ for an immediate (assertion 8, violated by the native code), or
+the constraint has several alternatives (assertion 9, violated at -O2), the
+statement is approximated.
diff --git a/regression/cbmc/asm_x86_section_references/main.i b/regression/cbmc/asm_x86_section_references/main.i
new file mode 100644
index 000000000000..3e8edeb72608
--- /dev/null
+++ b/regression/cbmc/asm_x86_section_references/main.i
@@ -0,0 +1,103 @@
+// Data in another section that refers to a label of the statement records the
+// label for run-time patching or as the continuation of a fault, so that the
+// statement may jump where its instructions do not show.
+
+struct static_key
+{
+ int enabled;
+} key;
+
+// Linux x86 arch_static_branch_jump: the kernel patches the jmp recorded in
+// __jump_table into a nop when the key is disabled (and back)
+static inline __attribute__((always_inline)) _Bool
+arch_static_branch_jump(struct static_key *const k, const _Bool branch)
+{
+ __asm__ goto("1:"
+ "jmp %l[l_yes]\n\t"
+ ".pushsection __jump_table, \"aw\" \n\t"
+ ".balign 8\n\t"
+ ".long 1b - . \n\t"
+ ".long %l[l_yes] - . \n\t"
+ ".quad %c0 + %c1 - . \n\t"
+ ".popsection \n\t"
+ :
+ : "i"(k), "i"(branch)
+ :
+ : l_yes);
+ return 0;
+l_yes:
+ return 1;
+}
+
+// Linux x86 __put_user_goto: a fault of the store continues at the label
+// recorded in __ex_table
+static int put_user(int x, int *p)
+{
+ __asm__ goto("1: movl %0,%1\n"
+ ".pushsection \"__ex_table\",\"a\"\n"
+ ".balign 4\n"
+ ".long (1b) - .\n"
+ ".long (%l2) - .\n"
+ ".long 3\n"
+ ".popsection\n"
+ :
+ : "r"(x), "m"(*p)
+ :
+ : efault);
+ return 0;
+efault:
+ return -14;
+}
+
+unsigned long bits[2];
+
+// Linux x86-64 set_bit with LOCK_PREFIX: .smp_locks records the lock prefix,
+// which the kernel replaces by a no-op prefix on a single processor
+static inline void set_bit(long nr, volatile unsigned long *addr)
+{
+ __asm__ volatile(".pushsection .smp_locks,\"a\"\n"
+ ".balign 4\n"
+ ".long 671f - .\n"
+ ".popsection\n"
+ "671:"
+ "\n\tlock; "
+ "btsq %1,%0"
+ :
+ : "m"(*(volatile long *)addr), "Ir"(nr)
+ : "memory");
+}
+
+int main(void)
+{
+ if(!arch_static_branch_jump(&key, 0))
+ __CPROVER_assert(0, "jump label disabled path");
+ else
+ __CPROVER_assert(0, "jump label enabled path");
+
+ int y = 0;
+ __CPROVER_assert(put_user(5, &y) == 0, "store does not fault");
+
+ set_bit(70, bits);
+ __CPROVER_assert(bits[1] == 64, "bit 70 set");
+
+ // a discarded annotation that refers to a label
+ int r;
+ __asm__("999:\n\t"
+ ".pushsection .discard.retpoline_safe\n\t"
+ ".long 999b\n\t"
+ ".popsection\n\t"
+ "movl %1, %0"
+ : "=r"(r)
+ : "r"(7));
+ __CPROVER_assert(r == 7, "discarded annotation");
+
+ // data in another section that refers to no label
+ __asm__(".pushsection .rodata\n\t"
+ ".long 42\n\t"
+ ".popsection\n\t"
+ "movl $8, %0"
+ : "=r"(r));
+ __CPROVER_assert(r == 8, "data without label references");
+
+ return 0;
+}
diff --git a/regression/cbmc/asm_x86_section_references/test.desc b/regression/cbmc/asm_x86_section_references/test.desc
new file mode 100644
index 000000000000..d76563cf58b8
--- /dev/null
+++ b/regression/cbmc/asm_x86_section_references/test.desc
@@ -0,0 +1,25 @@
+CORE gcc-only
+main.i
+--arch x86_64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 73 jump label disabled path: FAILURE$
+^\[main\.assertion\.2\] line 75 jump label enabled path: FAILURE$
+^\[main\.assertion\.3\] line 78 store does not fault: FAILURE$
+^\[main\.assertion\.4\] line 81 bit 70 set: SUCCESS$
+^\[main\.assertion\.5\] line 92 discarded annotation: SUCCESS$
+^\[main\.assertion\.6\] line 100 data without label references: SUCCESS$
+approximating inline assembly statement at .* line 15 function arch_static_branch_jump: data in section __jump_table refers to label 1b
+approximating inline assembly statement at .* line 36 function put_user: data in section __ex_table refers to label 1b
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly statement at .* function (set_bit|main)
+--
+Data in another section that refers to a label of the statement records the
+label for run-time patching (jump tables) or as the continuation of a fault
+(exception tables), so these statements are approximated: on hardware, the
+static branch goes either way, depending on how the kernel patched it, and
+the store may fault. The lock prefixes recorded in .smp_locks, discarded
+sections, and data that refers to no label do not change the effect of the
+statement, which is interpreted.
diff --git a/regression/cbmc/asm_x86_segment_store/main.i b/regression/cbmc/asm_x86_segment_store/main.i
new file mode 100644
index 000000000000..eb3c944c8f14
--- /dev/null
+++ b/regression/cbmc/asm_x86_segment_store/main.i
@@ -0,0 +1,31 @@
+// Segment-relative accesses: the segment base is not modeled. With a base of
+// zero, as for %gs in x86-64 Linux user space, a store to a C lvalue writes
+// it.
+
+int x;
+int y;
+
+int main(void)
+{
+ x = 0;
+ __asm__ volatile("movl $5, %%gs:%0" : "=m"(x));
+ // fails: x is 5 on x86-64 Linux
+ __CPROVER_assert(x == 0, "segment store dropped");
+
+ // a store that, by GCC's operand contract, changes no C object
+ y = 0;
+ __asm__ volatile("movl $5, %%gs:0x10" : : : "cc");
+ __CPROVER_assert(y == 0, "store to the environment");
+
+ // the "memory" clobber declares that the store may change any object
+ __asm__ volatile("movl $5, %%gs:0x10" : : : "memory");
+ // fails
+ __CPROVER_assert(y == 0, "store to memory");
+
+ // a load is nondeterministic
+ int r;
+ __asm__ volatile("movl %%fs:0x28, %0" : "=r"(r));
+ // fails
+ __CPROVER_assert(r == 0, "segment load");
+ return 0;
+}
diff --git a/regression/cbmc/asm_x86_segment_store/test.desc b/regression/cbmc/asm_x86_segment_store/test.desc
new file mode 100644
index 000000000000..63b87d910fae
--- /dev/null
+++ b/regression/cbmc/asm_x86_segment_store/test.desc
@@ -0,0 +1,21 @@
+CORE gcc-only
+main.i
+--arch x86_64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 13 segment store dropped: FAILURE$
+^\[main\.assertion\.2\] line 18 store to the environment: SUCCESS$
+^\[main\.assertion\.3\] line 23 store to memory: FAILURE$
+^\[main\.assertion\.4\] line 29 segment load: FAILURE$
+approximating inline assembly statement at .* line 11 function main: segment-relative store to a C object
+approximating inline assembly statement at .* line 21 function main: segment-relative store, which may change a C object
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly statement at .* line (17|26) function main
+--
+The segment base is not modeled: a segment-relative store to a C lvalue, or
+one in a statement that declares that it may change memory, is approximated
+(compiled with GCC 13 for x86-64 Linux, the first statement sets x to 5).
+Other segment-relative stores change no C object, and loads are
+nondeterministic.
diff --git a/regression/cbmc/asm_x86_shift_carry/main.i b/regression/cbmc/asm_x86_shift_carry/main.i
new file mode 100644
index 000000000000..89ccf2c4b247
--- /dev/null
+++ b/regression/cbmc/asm_x86_shift_carry/main.i
@@ -0,0 +1,38 @@
+// The Intel SDM leaves CF undefined for shl and shr of 8- and 16-bit operands
+// by a count of their width or more. The Intel Xeon Platinum 8124M that
+// these tests were run on sets it to the last bit shifted out, but CBMC must
+// not prove that. For sar by the width, CF is the sign bit.
+
+__attribute__((noinline)) unsigned char carry(unsigned char x, unsigned char n)
+{
+ unsigned char c;
+ __asm__("shlb %%cl, %1" : "=@ccc"(c), "+q"(x) : "c"(n));
+ return c;
+}
+
+__attribute__((noinline)) unsigned char
+carry_shr(unsigned short x, unsigned char n)
+{
+ unsigned char c;
+ __asm__("shrw %%cl, %1" : "=@ccc"(c), "+r"(x) : "c"(n));
+ return c;
+}
+
+__attribute__((noinline)) unsigned char
+carry_sar(unsigned char x, unsigned char n)
+{
+ unsigned char c;
+ __asm__("sarb %%cl, %1" : "=@ccc"(c), "+q"(x) : "c"(n));
+ return c;
+}
+
+int main(void)
+{
+ // fails: undefined
+ __CPROVER_assert(carry(1, 8) == 1, "shlb by 8");
+ __CPROVER_assert(carry(1, 7) == 0, "shlb by 7");
+ // fails: undefined
+ __CPROVER_assert(carry_shr(0x8000, 16) == 1, "shrw by 16");
+ __CPROVER_assert(carry_sar(0x80, 8) == 1, "sarb by 8");
+ return 0;
+}
diff --git a/regression/cbmc/asm_x86_shift_carry/test.desc b/regression/cbmc/asm_x86_shift_carry/test.desc
new file mode 100644
index 000000000000..d0cd5578a67a
--- /dev/null
+++ b/regression/cbmc/asm_x86_shift_carry/test.desc
@@ -0,0 +1,17 @@
+CORE gcc-only
+main.i
+--arch x86_64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 32 shlb by 8: FAILURE$
+^\[main\.assertion\.2\] line 33 shlb by 7: SUCCESS$
+^\[main\.assertion\.3\] line 35 shrw by 16: FAILURE$
+^\[main\.assertion\.4\] line 36 sarb by 8: SUCCESS$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+approximating inline assembly
+--
+The Intel SDM leaves CF undefined for shl and shr by a count of the operand
+width or more, which CBMC models as nondeterministic. All four assertions
+hold on the Intel Xeon Platinum 8124M that they were run on.
diff --git a/regression/cbmc/havoc_object_string_literal/main.c b/regression/cbmc/havoc_object_string_literal/main.c
new file mode 100644
index 000000000000..29961ad08735
--- /dev/null
+++ b/regression/cbmc/havoc_object_string_literal/main.c
@@ -0,0 +1,36 @@
+#include
+
+int main()
+{
+ char *s = "abc";
+ int i = 0;
+ _Bool c;
+ char *p = c ? s : (char *)&i;
+
+ // String literals cannot be modified, havocking them has no effect.
+ __CPROVER_havoc_object(s);
+ __CPROVER_assert(s[0] == 'a', "string literal unchanged");
+
+ __CPROVER_havoc_object(p);
+ __CPROVER_assert(i == 0, "i may be havocked"); // should fail
+
+ // A pointer into the middle of a string literal.
+ __CPROVER_havoc_object(s + 1);
+ __CPROVER_assert(s[1] == 'b', "offset into string literal unchanged");
+
+ // Wide and Unicode string literals are arrays rather than string constants.
+ const wchar_t *w = L"abc";
+ __CPROVER_havoc_object((void *)w);
+ __CPROVER_assert(w[0] == L'a', "wide string literal unchanged");
+
+ const unsigned short *u16 = (const unsigned short *)u"abc";
+ __CPROVER_havoc_object((void *)u16);
+ __CPROVER_assert(u16[0] == 'a', "UTF-16 string literal unchanged");
+
+ const unsigned int *u32 = (const unsigned int *)U"abc";
+ __CPROVER_havoc_object((void *)(u32 + 1));
+ __CPROVER_assert(
+ u32[1] == 'b', "offset into UTF-32 string literal unchanged");
+
+ return 0;
+}
diff --git a/regression/cbmc/havoc_object_string_literal/test.desc b/regression/cbmc/havoc_object_string_literal/test.desc
new file mode 100644
index 000000000000..9279523e0b41
--- /dev/null
+++ b/regression/cbmc/havoc_object_string_literal/test.desc
@@ -0,0 +1,23 @@
+CORE
+main.c
+--no-pointer-check
+^EXIT=10$
+^SIGNAL=0$
+^\[main.assertion.1\] line 12 string literal unchanged: SUCCESS$
+^\[main.assertion.2\] line 15 i may be havocked: FAILURE$
+^\[main.assertion.3\] line 19 offset into string literal unchanged: SUCCESS$
+^\[main.assertion.4\] line 24 wide string literal unchanged: SUCCESS$
+^\[main.assertion.5\] line 28 UTF-16 string literal unchanged: SUCCESS$
+^\[main.assertion.6\] line 32 offset into UTF-32 string literal unchanged: SUCCESS$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+Invariant check failed
+--
+__CPROVER_havoc_object applied to a pointer to a string literal used to fail
+an invariant in symbolic execution ("Attempted to symex havoc applied to
+unsupported expression"). Narrow string literals are string constants, while
+wide and Unicode string literals, and the part of a literal that a pointer
+into its middle refers to, are arrays. The test uses --no-pointer-check, as
+reading a wide string literal currently gives a spurious pointer check failure,
+and makes the SMT2 back-end fail an invariant in convert_address_of_rec.
diff --git a/regression/cbmc/inline_asm_approximation/main.c b/regression/cbmc/inline_asm_approximation/main.c
new file mode 100644
index 000000000000..1ebb476be39d
--- /dev/null
+++ b/regression/cbmc/inline_asm_approximation/main.c
@@ -0,0 +1,72 @@
+// Statements containing an instruction that is not modeled are approximated
+// conservatively by the remove_asm pass (see
+// doc/cprover-manual/modeling-inline-asm.md): everything they may change is
+// assigned a nondeterministic value. Each assertion marked "fails" would hold
+// if the statement were dropped.
+#include
+
+int global;
+int *global_pointer;
+
+struct S
+{
+ int *p;
+ int v;
+};
+
+static void clobber(void)
+{
+ // pointee is not a local variable of this function
+ asm volatile("incl global" ::: "memory");
+}
+
+int main(void)
+{
+ int x = 0, y = 1;
+ // output operands
+ asm volatile("xchg %0,%1" : "+r"(x), "+r"(y));
+ __CPROVER_assert(x == 0, "output operand"); // fails
+
+ // a recognized mfence combined with an unrecognized addl: the recognized
+ // instruction does not hide the unrecognized one
+ int z = 0;
+ asm volatile("mfence; addl $1, %0" : "+m"(z));
+ __CPROVER_assert(z == 0, "mixed statement"); // fails
+
+ // the object a pointer operand points to, also via an array that decays
+ // to a pointer, a cast to an integer type, or a struct member
+ int a[2] = {0, 0};
+ asm volatile("movl $1, (%0)" : : "r"(a));
+ __CPROVER_assert(a[0] == 0, "array operand"); // fails
+ int h = 0;
+ asm volatile("movl $1, (%0)" : : "r"((uintptr_t)&h));
+ __CPROVER_assert(h == 0, "pointer cast to integer"); // fails
+ int k = 0;
+ struct S s = {&k, 0};
+ asm volatile("movl $1, (%0)" : : "m"(s));
+ __CPROVER_assert(k == 0, "pointer member"); // fails
+ __CPROVER_assert(s.v == 0, "input operand"); // holds
+
+ // "memory" clobber: objects of static lifetime and what they point to
+ int pointee = 0;
+ global = 0;
+ global_pointer = &pointee;
+ clobber();
+ __CPROVER_assert(global == 0, "static object"); // fails
+ __CPROVER_assert(pointee == 0, "pointee of static pointer"); // fails
+
+ // "memory" clobber: address-taken local variables
+ int unaddressed = 0, addressed = 0;
+ int *escape = &addressed;
+ asm volatile("cli" ::: "memory");
+ __CPROVER_assert(addressed == 0, "address-taken local"); // fails
+ __CPROVER_assert(unaddressed == 0, "local without address"); // holds
+
+ // an empty statement is a compiler barrier only
+ int barrier = 0;
+ int *barrier_address = &barrier;
+ asm volatile("" ::: "memory");
+ __CPROVER_assert(barrier == 0, "empty statement"); // holds
+
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_approximation/test.desc b/regression/cbmc/inline_asm_approximation/test.desc
new file mode 100644
index 000000000000..a2e6da68e940
--- /dev/null
+++ b/regression/cbmc/inline_asm_approximation/test.desc
@@ -0,0 +1,29 @@
+CORE gcc-only
+main.c
+--arch riscv64 --no-standard-checks
+^EXIT=10$
+^SIGNAL=0$
+approximating inline assembly statement at .* line 27 function main: instruction 'xchg' is not modeled, so its output operands and the rounding mode are assigned nondeterministic values
+approximating inline assembly statement at .* line 33 function main: instruction 'addl' is not modeled
+^\[main\.assertion\.1\] line 28 output operand: FAILURE$
+^\[main\.assertion\.2\] line 34 mixed statement: FAILURE$
+^\[main\.assertion\.3\] line 40 array operand: FAILURE$
+^\[main\.assertion\.4\] line 43 pointer cast to integer: FAILURE$
+^\[main\.assertion\.5\] line 47 pointer member: FAILURE$
+^\[main\.assertion\.6\] line 48 input operand: SUCCESS$
+^\[main\.assertion\.7\] line 55 static object: FAILURE$
+^\[main\.assertion\.8\] line 56 pointee of static pointer: FAILURE$
+^\[main\.assertion\.9\] line 62 address-taken local: FAILURE$
+^\[main\.assertion\.10\] line 63 local without address: SUCCESS$
+^\[main\.assertion\.11\] line 69 empty statement: SUCCESS$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+dropping inline assembly
+--
+Inline-assembly statements containing an unrecognized instruction are
+approximated conservatively by the remove_asm pass: output operands, the
+objects pointer operands point to, with a "memory" clobber the objects of
+static lifetime, what these point to, and address-taken local variables, and
+the rounding mode are assigned nondeterministic values.
+See GitHub issue diffblue/cbmc#6374.
diff --git a/regression/cbmc/inline_asm_approximation_msvc/main.i b/regression/cbmc/inline_asm_approximation_msvc/main.i
new file mode 100644
index 000000000000..060d5aef323d
--- /dev/null
+++ b/regression/cbmc/inline_asm_approximation_msvc/main.i
@@ -0,0 +1,23 @@
+// MSVC-style __asm blocks have no operand interface: a block containing an
+// instruction that is not modeled may change any variable it names (with
+// shadowing, any local variable of that name) and any memory it can reach.
+int global;
+
+int main(void)
+{
+ int z = 0;
+ __asm { nop }
+ __CPROVER_assert(z == 0, "nop is modeled"); // holds
+
+ int x = 0, y = 0;
+ {
+ int x = 0;
+ __asm { mov x, 1 }
+ __CPROVER_assert(x == 0, "inner x"); // fails
+ }
+ __CPROVER_assert(x == 0, "outer x"); // fails
+ __CPROVER_assert(y == 0, "y is not named"); // holds
+ __CPROVER_assert(global == 0, "static object"); // fails
+
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_approximation_msvc/test.desc b/regression/cbmc/inline_asm_approximation_msvc/test.desc
new file mode 100644
index 000000000000..3c376555afc2
--- /dev/null
+++ b/regression/cbmc/inline_asm_approximation_msvc/test.desc
@@ -0,0 +1,19 @@
+CORE
+main.i
+--winx64
+^EXIT=10$
+^SIGNAL=0$
+approximating inline assembly statement at .* line 15 function main: instruction 'mov' is not modeled, so the local variables it names, all objects of static lifetime
+^\[main\.assertion\.1\] line 10 nop is modeled: SUCCESS$
+^\[main\.assertion\.2\] line 16 inner x: FAILURE$
+^\[main\.assertion\.3\] line 18 outer x: FAILURE$
+^\[main\.assertion\.4\] line 19 y is not named: SUCCESS$
+^\[main\.assertion\.5\] line 20 static object: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+MSVC-style inline assembly that is not modeled is approximated conservatively.
+The input is preprocessed (main.i), and the test uses no instruction that is
+modeled by a library function (as fstcw is, see cbmc-library/__asm_fstcw), as
+preprocessing for --winx64 fails on a non-x86 host.
diff --git a/regression/cbmc/inline_asm_approximation_rounding_mode/main.c b/regression/cbmc/inline_asm_approximation_rounding_mode/main.c
new file mode 100644
index 000000000000..83392ca9ed8a
--- /dev/null
+++ b/regression/cbmc/inline_asm_approximation_rounding_mode/main.c
@@ -0,0 +1,10 @@
+int main(void)
+{
+ float f = 1.0f;
+ char state[512] = {0};
+ // fxrstor is not modeled, and may change the rounding mode
+ asm volatile("fxrstor %0" : : "m"(state));
+ // holds with round-to-nearest only
+ __CPROVER_assert(f / 3.0f == 0x1.555556p-2f, "rounding mode");
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_approximation_rounding_mode/test.desc b/regression/cbmc/inline_asm_approximation_rounding_mode/test.desc
new file mode 100644
index 000000000000..d06b0b037f54
--- /dev/null
+++ b/regression/cbmc/inline_asm_approximation_rounding_mode/test.desc
@@ -0,0 +1,13 @@
+CORE gcc-only no-new-smt
+main.c
+--arch riscv64
+^EXIT=10$
+^SIGNAL=0$
+approximating inline assembly statement at .* line 6 function main: instruction 'fxrstor' is not modeled, so the objects reachable from its operands
+^\[main\.assertion\.1\] line 8 rounding mode: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+Inline assembly that is not modeled may change the floating-point rounding
+mode.
diff --git a/regression/cbmc/inline_asm_fldcw_sse/main.i b/regression/cbmc/inline_asm_fldcw_sse/main.i
new file mode 100644
index 000000000000..43af28e0d6f2
--- /dev/null
+++ b/regression/cbmc/inline_asm_fldcw_sse/main.i
@@ -0,0 +1,15 @@
+// preprocessed input, as preprocessing for --arch x86_64 fails on hosts of
+// other architectures
+int main(void)
+{
+ unsigned short cw;
+ __asm__ volatile("fnstcw %0" : "=m"(cw));
+ // x87: round downwards
+ cw = (unsigned short)((cw & ~0x0c00) | 0x0400);
+ __asm__ volatile("fldcw %0" : : "m"(cw));
+ volatile float f = 1.0f;
+ float q = f / 3.0f;
+ // float arithmetic uses SSE on x86-64, which still rounds to nearest
+ __CPROVER_assert(q == 0x1.555554p-2f, "rounded downwards"); // fails
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_fldcw_sse/test.desc b/regression/cbmc/inline_asm_fldcw_sse/test.desc
new file mode 100644
index 000000000000..e12543fa45f9
--- /dev/null
+++ b/regression/cbmc/inline_asm_fldcw_sse/test.desc
@@ -0,0 +1,13 @@
+KNOWNBUG no-new-smt
+main.i
+--arch x86_64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 13 rounded downwards: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+CBMC has a single rounding mode, which fldcw sets. On x86-64, float
+arithmetic uses SSE, whose rounding mode is in MXCSR and not changed by
+fldcw: compiled with gcc, the assertion fails, but CBMC proves it.
diff --git a/regression/cbmc/inline_asm_fpu_control_word/main.c b/regression/cbmc/inline_asm_fpu_control_word/main.c
new file mode 100644
index 000000000000..6376ec6cb520
--- /dev/null
+++ b/regression/cbmc/inline_asm_fpu_control_word/main.c
@@ -0,0 +1,22 @@
+int main(void)
+{
+ unsigned short old, new = 3 << 10;
+
+ // fldcw uses its own operand (%1), not the first operand of the statement
+ asm volatile("fstcw %0; fldcw %1" : "=m"(old) : "m"(new));
+ __CPROVER_assert((old >> 10 & 3) == 0, "initial rounding mode");
+
+ asm volatile("fstcw %0" : "=m"(old));
+ __CPROVER_assert((old >> 10 & 3) == 3, "rounding mode set by fldcw");
+
+ // the other bits of the control word (the exception masks and the
+ // precision control) are not modeled: on x86 hardware, the control word
+ // is 0x037f initially
+ asm volatile("fnstcw %0" : "=m"(old));
+ __CPROVER_assert((old >> 10 & 3) == 3, "fnstcw rounding mode");
+ __CPROVER_assert((old & 0x0300) == 0, "precision control"); // fails
+ asm volatile("fstcw %0" : "=m"(old));
+ __CPROVER_assert((old & 0x003f) == 0, "exception masks"); // fails
+
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_fpu_control_word/test.desc b/regression/cbmc/inline_asm_fpu_control_word/test.desc
new file mode 100644
index 000000000000..d0f4dd2cd1d7
--- /dev/null
+++ b/regression/cbmc/inline_asm_fpu_control_word/test.desc
@@ -0,0 +1,19 @@
+CORE gcc-only
+main.c
+--arch riscv64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 7 initial rounding mode: SUCCESS$
+^\[main\.assertion\.2\] line 10 rounding mode set by fldcw: SUCCESS$
+^\[main\.assertion\.3\] line 16 fnstcw rounding mode: SUCCESS$
+^\[main\.assertion\.4\] line 17 precision control: FAILURE$
+^\[main\.assertion\.5\] line 19 exception masks: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+inline assembly statement at
+--
+The x87 control word instructions are modeled using the operand they refer
+to. fstcw and fnstcw store the rounding mode in bits 10 and 11; they used to
+store 0 in the other bits, which made CBMC prove properties that fail on
+hardware.
diff --git a/regression/cbmc/inline_asm_goto/main.c b/regression/cbmc/inline_asm_goto/main.c
new file mode 100644
index 000000000000..e26ac4e336a1
--- /dev/null
+++ b/regression/cbmc/inline_asm_goto/main.c
@@ -0,0 +1,38 @@
+// asm goto: a statement that is not modeled may jump to any of its labels, or
+// fall through; a modeled statement (none of the modeled instructions jumps)
+// falls through.
+static int branch(int *p)
+{
+ // not modeled: may jump to either label or fall through
+ asm goto("testl %0, %0; jz %l[zero]; jmp %l[nonzero]"
+ :
+ : "r"(*p)
+ :
+ : zero, nonzero);
+ return 0;
+zero:
+ return 1;
+nonzero:
+ return 2;
+}
+
+static int modeled(void)
+{
+ // modeled, and does not jump
+ asm goto("nop" : : : : out);
+ return 0;
+out:
+ return 1;
+}
+
+int main(void)
+{
+ int x = 0;
+ int r = branch(&x);
+ __CPROVER_assert(r != 0, "falls through"); // fails
+ __CPROVER_assert(r != 1, "jumps to zero"); // fails
+ __CPROVER_assert(r != 2, "jumps to nonzero"); // fails
+ __CPROVER_assert(r >= 0 && r <= 2, "one of the labels"); // holds
+ __CPROVER_assert(modeled() == 0, "modeled statement does not jump"); // holds
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_goto/test.desc b/regression/cbmc/inline_asm_goto/test.desc
new file mode 100644
index 000000000000..697082078e1a
--- /dev/null
+++ b/regression/cbmc/inline_asm_goto/test.desc
@@ -0,0 +1,18 @@
+CORE gcc-only
+main.c
+--arch riscv64 --validate-goto-model --validate-ssa-equation
+^EXIT=10$
+^SIGNAL=0$
+approximating inline assembly statement at .* function branch: instruction 'testl' is not modeled, so the choice of the label it jumps to \(if any\)
+^\[main\.assertion\.1\] line 32 falls through: FAILURE$
+^\[main\.assertion\.2\] line 33 jumps to zero: FAILURE$
+^\[main\.assertion\.3\] line 34 jumps to nonzero: FAILURE$
+^\[main\.assertion\.4\] line 35 one of the labels: SUCCESS$
+^\[main\.assertion\.5\] line 36 modeled statement does not jump: SUCCESS$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+goto conversion dispatches on a variable that the translation of the asm goto
+statement sets to the label it jumps to: nondeterministically for a statement
+that is approximated, and to "no jump" for a modeled one.
diff --git a/regression/cbmc/inline_asm_goto_scope/main.c b/regression/cbmc/inline_asm_goto_scope/main.c
new file mode 100644
index 000000000000..a4f091f74681
--- /dev/null
+++ b/regression/cbmc/inline_asm_goto_scope/main.c
@@ -0,0 +1,53 @@
+// asm goto statements that leave a block with local variables, jumping
+// forwards and backwards. Whether they jump depends on a nondeterministic
+// value, so they may jump to their label or fall through, whether or not
+// their instructions are modeled.
+int nondet_int(void);
+
+int *q;
+
+static int forward(void)
+{
+ int r = 0;
+ {
+ int c = nondet_int();
+ int y = 5;
+ int *p = &y;
+ q = p;
+ asm goto("testl %0, %0; jz %l[out]" : : "r"(c), "r"(p) : : out);
+ r = 1;
+ }
+ return r;
+out:
+ __CPROVER_assert(r == 0, "forward: jumped before r = 1"); // holds
+ // the jump has left the block, which ends the lifetime of y
+ r = *q; // fails: dead object
+ return 2;
+}
+
+static int backward(void)
+{
+ int n = 0;
+again:
+{
+ int z = nondet_int();
+ ++n;
+ if(n < 3)
+ asm goto("testl %0, %0; jnz %l[again]" : : "r"(z) : : again);
+}
+ return n;
+}
+
+int main(void)
+{
+ int r = forward();
+ __CPROVER_assert(r != 1, "forward: falls through"); // fails
+ __CPROVER_assert(r != 2, "forward: jumps out of the block"); // fails
+ __CPROVER_assert(r == 1 || r == 2, "forward: one of the two"); // holds
+
+ int n = backward();
+ __CPROVER_assert(n != 1, "backward: falls through"); // fails
+ __CPROVER_assert(n != 3, "backward: jumps back twice"); // fails
+ __CPROVER_assert(n >= 1 && n <= 3, "backward: at most twice"); // holds
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_goto_scope/test.desc b/regression/cbmc/inline_asm_goto_scope/test.desc
new file mode 100644
index 000000000000..079ac343e738
--- /dev/null
+++ b/regression/cbmc/inline_asm_goto_scope/test.desc
@@ -0,0 +1,21 @@
+CORE gcc-only
+main.c
+--arch riscv64 --validate-goto-model --validate-ssa-equation
+^EXIT=10$
+^SIGNAL=0$
+^\[forward\.assertion\.1\] line 22 forward: jumped before r = 1: SUCCESS$
+^\[forward\.pointer_dereference\.\d+\] line 24 dereference failure: dead object in \*q: FAILURE$
+^\[main\.assertion\.1\] line 44 forward: falls through: FAILURE$
+^\[main\.assertion\.2\] line 45 forward: jumps out of the block: FAILURE$
+^\[main\.assertion\.3\] line 46 forward: one of the two: SUCCESS$
+^\[main\.assertion\.4\] line 49 backward: falls through: FAILURE$
+^\[main\.assertion\.5\] line 50 backward: jumps back twice: FAILURE$
+^\[main\.assertion\.6\] line 51 backward: at most twice: SUCCESS$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+goto conversion lowers the jumps of an asm goto statement to ordinary gotos,
+so that leaving a block ends the lifetime of its local variables (here y, which
+a dereference after the forward jump finds dead) and a jump back to a label
+before the statement forms a loop (here taken at most twice).
diff --git a/regression/cbmc/inline_asm_hash/arm.desc b/regression/cbmc/inline_asm_hash/arm.desc
new file mode 100644
index 000000000000..58ca477e1929
--- /dev/null
+++ b/regression/cbmc/inline_asm_hash/arm.desc
@@ -0,0 +1,14 @@
+CORE
+comments.i
+--arch arm64 --show-goto-functions
+^EXIT=0$
+^SIGNAL=0$
+^approximating inline assembly statement at file comments\.i line 3 function main: unsupported immediate in 'mfence # x86 comment'
+^approximating inline assembly statement at file comments\.i line 4 function main: unsupported immediate in 'sync # Power comment'
+--
+^warning: ignoring
+^ *CALL __asm_mfence\(\)$
+^ *FENCE
+--
+On AArch64, '#' after the start of a statement introduces an immediate, so
+the instructions have operands they do not take and are not modeled.
diff --git a/regression/cbmc/inline_asm_hash/barriers.desc b/regression/cbmc/inline_asm_hash/barriers.desc
new file mode 100644
index 000000000000..e5772b4b3666
--- /dev/null
+++ b/regression/cbmc/inline_asm_hash/barriers.desc
@@ -0,0 +1,16 @@
+CORE
+barriers.i
+--arch arm64 --show-goto-functions
+^EXIT=0$
+^SIGNAL=0$
+^ *FENCE RR RW$
+^ *FENCE WW$
+^ *FENCE WW RR RW WR$
+^ *FENCE$
+^approximating inline assembly statement at file barriers\.i line 8 function main: unsupported option of dmb in 'dmb #0x0'
+--
+^warning: ignoring
+^approximating inline assembly statement at file barriers\.i line [4-7] function
+--
+The immediate forms of the options of dmb, dsb, and isb are modeled like the
+named options; reserved options are not modeled.
diff --git a/regression/cbmc/inline_asm_hash/barriers.i b/regression/cbmc/inline_asm_hash/barriers.i
new file mode 100644
index 000000000000..5040c5121613
--- /dev/null
+++ b/regression/cbmc/inline_asm_hash/barriers.i
@@ -0,0 +1,10 @@
+// the immediate forms of the barrier options
+int main(void)
+{
+ asm volatile("dmb #9"); // ishld
+ asm volatile("dmb #10"); // ishst
+ asm volatile("dsb #15"); // sy
+ asm volatile("isb #15"); // sy
+ asm volatile("dmb #0x0"); // reserved
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_hash/comments.i b/regression/cbmc/inline_asm_hash/comments.i
new file mode 100644
index 000000000000..4b0407899d10
--- /dev/null
+++ b/regression/cbmc/inline_asm_hash/comments.i
@@ -0,0 +1,6 @@
+int main(void)
+{
+ asm volatile("mfence # x86 comment");
+ asm volatile("sync # Power comment");
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_hash/main.i b/regression/cbmc/inline_asm_hash/main.i
new file mode 100644
index 000000000000..1795bba4ad69
--- /dev/null
+++ b/regression/cbmc/inline_asm_hash/main.i
@@ -0,0 +1,18 @@
+// preprocessed input, as preprocessing for --arch arm64 may fail on hosts
+// of other architectures
+int main(void)
+{
+ // on ARM and AArch64, '#' introduces an immediate: the store must not be
+ // lost in a comment
+ int v = 0;
+ int *p = &v;
+ asm volatile("dmb #11; str %w1, [%0]" : : "r"(p), "r"(1) : "memory");
+ __CPROVER_assert(v == 0, "v unchanged"); // fails
+
+ // at the start of a statement, '#' starts a comment
+ int w = 0;
+ asm volatile("nop; # str %w1, [%0]" : : "r"(&w), "r"(1));
+ __CPROVER_assert(w == 0, "w unchanged"); // holds
+
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_hash/ppc.desc b/regression/cbmc/inline_asm_hash/ppc.desc
new file mode 100644
index 000000000000..0d9a45115f62
--- /dev/null
+++ b/regression/cbmc/inline_asm_hash/ppc.desc
@@ -0,0 +1,12 @@
+CORE
+comments.i
+--arch ppc64le --show-goto-functions
+^EXIT=0$
+^SIGNAL=0$
+^ *CALL __asm_mfence\(\)$
+^ *FENCE WW RR RW WR$
+--
+^warning: ignoring
+inline assembly statement at
+--
+On Power, '#' starts a comment anywhere on a line.
diff --git a/regression/cbmc/inline_asm_hash/test.desc b/regression/cbmc/inline_asm_hash/test.desc
new file mode 100644
index 000000000000..056ed474498e
--- /dev/null
+++ b/regression/cbmc/inline_asm_hash/test.desc
@@ -0,0 +1,14 @@
+CORE
+main.i
+--arch arm64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 10 v unchanged: FAILURE$
+^\[main\.assertion\.2\] line 15 w unchanged: SUCCESS$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+On AArch64, '#' starts a comment only at the start of a line or statement.
+The assembler lexer of remove_asm treated it as a comment anywhere, so the
+store after "dmb #11" was lost and the statement was modeled as a fence.
diff --git a/regression/cbmc/inline_asm_hash/x86.desc b/regression/cbmc/inline_asm_hash/x86.desc
new file mode 100644
index 000000000000..66368e9bf781
--- /dev/null
+++ b/regression/cbmc/inline_asm_hash/x86.desc
@@ -0,0 +1,12 @@
+CORE
+comments.i
+--arch x86_64 --show-goto-functions
+^EXIT=0$
+^SIGNAL=0$
+^ *CALL __asm_mfence\(\)$
+^ *FENCE WW RR RW WR$
+--
+^warning: ignoring
+inline assembly statement at
+--
+On x86, '#' starts a comment anywhere on a line.
diff --git a/regression/cbmc/inline_asm_havoc_reachable/full-slice.desc b/regression/cbmc/inline_asm_havoc_reachable/full-slice.desc
new file mode 100644
index 000000000000..e77e32e0c028
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable/full-slice.desc
@@ -0,0 +1,20 @@
+CORE gcc-only
+main.c
+--arch riscv64 --full-slice
+^EXIT=10$
+^SIGNAL=0$
+^not slicing, as the program contains inline assembly that may change any reachable object$
+^\[main\.assertion\.1\] line 37 heap object reachable from static: FAILURE$
+^\[main\.assertion\.2\] line 38 pointer in static: FAILURE$
+^\[main\.assertion\.3\] line 39 pointers are restricted to known values: SUCCESS$
+^\[main\.assertion\.4\] line 50 escaped local of caller: FAILURE$
+^\[main\.assertion\.5\] line 51 local without address: SUCCESS$
+^\[main\.assertion\.6\] line 59 heap object reachable from operand: FAILURE$
+^\[main\.assertion\.7\] line 60 unreachable heap object: SUCCESS$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+dereference failure.*: FAILURE$
+--
+The full slicer does not know which objects havoc_reachable changes and
+reads, and therefore does not slice such programs.
diff --git a/regression/cbmc/inline_asm_havoc_reachable/main.c b/regression/cbmc/inline_asm_havoc_reachable/main.c
new file mode 100644
index 000000000000..beefb179756d
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable/main.c
@@ -0,0 +1,63 @@
+// Inline assembly that is not modeled may change all objects it can reach:
+// with a "memory" clobber, everything reachable from objects of static
+// lifetime and from address-taken local variables of all functions on the
+// call stack, and otherwise everything reachable from its pointer operands.
+#include
+
+struct node
+{
+ int data;
+ struct node *next;
+};
+
+struct node *head;
+
+static void clobber(void)
+{
+ asm volatile("cli" ::: "memory");
+}
+
+static struct node *new_node(int data, struct node *next)
+{
+ struct node *n = malloc(sizeof(struct node));
+ __CPROVER_assume(n != NULL);
+ n->data = data;
+ n->next = next;
+ return n;
+}
+
+int main(void)
+{
+ struct node *b = new_node(2, NULL);
+ struct node *a = new_node(1, b);
+
+ // reachable from an object of static lifetime, two levels deep
+ head = a;
+ clobber();
+ __CPROVER_assert(b->data == 2, "heap object reachable from static"); // fails
+ __CPROVER_assert(head == a, "pointer in static"); // fails
+ __CPROVER_assert(
+ head == a || head == b || head == NULL,
+ "pointers are restricted to known values"); // holds
+ if(head != NULL)
+ head->data = 0; // no dereference failure
+
+ // a local variable of a caller whose address is taken
+ int local = 0;
+ int *alias = &local;
+ int unaddressed = 0;
+ clobber();
+ __CPROVER_assert(local == 0, "escaped local of caller"); // fails
+ __CPROVER_assert(unaddressed == 0, "local without address"); // holds
+
+ // without a "memory" clobber: only what the operands reach
+ head = NULL;
+ struct node *d = new_node(4, NULL);
+ struct node *c = new_node(3, d);
+ struct node *e = new_node(5, NULL);
+ asm volatile("foo %0" : : "r"(c));
+ __CPROVER_assert(d->data == 4, "heap object reachable from operand"); // fails
+ __CPROVER_assert(e->data == 5, "unreachable heap object"); // holds
+
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_havoc_reachable/paths-lifo.desc b/regression/cbmc/inline_asm_havoc_reachable/paths-lifo.desc
new file mode 100644
index 000000000000..dfb9aeb499bf
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable/paths-lifo.desc
@@ -0,0 +1,20 @@
+CORE gcc-only
+main.c
+--arch riscv64 --paths lifo
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 37 heap object reachable from static: FAILURE$
+^\[main\.assertion\.2\] line 38 pointer in static: FAILURE$
+^\[main\.assertion\.3\] line 39 pointers are restricted to known values: SUCCESS$
+^\[main\.assertion\.4\] line 50 escaped local of caller: FAILURE$
+^\[main\.assertion\.5\] line 51 local without address: SUCCESS$
+^\[main\.assertion\.6\] line 59 heap object reachable from operand: FAILURE$
+^\[main\.assertion\.7\] line 60 unreachable heap object: SUCCESS$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+dereference failure.*: FAILURE$
+--
+With path exploration, symbolic execution executes the havoc_reachable
+statement once per path, with the value set of that path, and gives the same
+verdicts as test.desc.
diff --git a/regression/cbmc/inline_asm_havoc_reachable/test.desc b/regression/cbmc/inline_asm_havoc_reachable/test.desc
new file mode 100644
index 000000000000..1efb572af131
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable/test.desc
@@ -0,0 +1,24 @@
+CORE gcc-only
+main.c
+--arch riscv64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 37 heap object reachable from static: FAILURE$
+^\[main\.assertion\.2\] line 38 pointer in static: FAILURE$
+^\[main\.assertion\.3\] line 39 pointers are restricted to known values: SUCCESS$
+^\[main\.assertion\.4\] line 50 escaped local of caller: FAILURE$
+^\[main\.assertion\.5\] line 51 local without address: SUCCESS$
+^\[main\.assertion\.6\] line 59 heap object reachable from operand: FAILURE$
+^\[main\.assertion\.7\] line 60 unreachable heap object: SUCCESS$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+dereference failure.*: FAILURE$
+--
+The havoc_reachable statement that the approximation of inline assembly emits
+is implemented in symbolic execution using the value set: all objects
+reachable from the pointer operands (and, with a "memory" clobber, from the
+objects of static lifetime and the address-taken local variables of all
+functions on the call stack) are assigned nondeterministic values. The
+pointers stored in these objects are restricted to previously stored pointers,
+the addresses of the reachable objects, and null.
diff --git a/regression/cbmc/inline_asm_havoc_reachable_alignment/main.c b/regression/cbmc/inline_asm_havoc_reachable_alignment/main.c
new file mode 100644
index 000000000000..fbc303066c66
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable_alignment/main.c
@@ -0,0 +1,59 @@
+// Inline assembly that is not modeled may move the pointers it can reach
+// within the objects they point into. In an object whose members are at
+// positions that are multiples of their alignment, the pointers are assumed to
+// point to positions that are multiples of the alignment of their target type
+// (relative to the start of the object). In an object that contains a packed
+// struct or a type with a reduced alignment, any position is considered.
+
+// arrays of structs that contain pointers, which two statements change
+#define NODES(T) \
+ struct T \
+ { \
+ struct T *next; \
+ int data; \
+ } T##s[30];
+NODES(t0)
+NODES(t1)
+NODES(t2)
+NODES(t3)
+NODES(t4)
+NODES(t5)
+NODES(t6)
+NODES(t7)
+
+int a[4];
+int *gp = &a[0];
+
+struct __attribute__((packed)) packed
+{
+ char c;
+ int x;
+} ps[4];
+int *pp = &ps[0].x;
+
+typedef int int2 __attribute__((aligned(2)));
+struct reduced
+{
+ short s;
+ int2 x;
+} rs[4];
+int2 *rp = &rs[0].x;
+
+int main(void)
+{
+ t0s[0].next = &t0s[1];
+ t1s[0].next = &t1s[1];
+ t2s[0].next = &t2s[1];
+ t3s[0].next = &t3s[1];
+ t4s[0].next = &t4s[1];
+ t5s[0].next = &t5s[1];
+ t6s[0].next = &t6s[1];
+ t7s[0].next = &t7s[1];
+ asm volatile("cli" ::: "memory");
+ asm volatile("cli" ::: "memory");
+ __CPROVER_assert(t0s[0].next != &t0s[2], "array of structs"); // fails
+ __CPROVER_assert(gp != (int *)((char *)a + 1), "misaligned"); // holds
+ __CPROVER_assert(pp != &ps[1].x, "packed struct"); // fails
+ __CPROVER_assert(rp != &rs[2].x, "reduced alignment"); // fails
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_havoc_reachable_alignment/test.desc b/regression/cbmc/inline_asm_havoc_reachable_alignment/test.desc
new file mode 100644
index 000000000000..9d46d7a369cd
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable_alignment/test.desc
@@ -0,0 +1,23 @@
+CORE gcc-only
+main.c
+--arch riscv64
+^EXIT=10$
+^SIGNAL=0$
+^approximating inline assembly statement at file main\.c line 52 function main: .*; other objects are assumed to be unchanged, and the pointers stored in the changed objects are assumed to be null, previous values of such pointers, or pointers into the reachable objects at offsets that are multiples of the alignment of their target type and at which an object of that type fits$
+^\[main\.assertion\.1\] line 54 array of structs: FAILURE$
+^\[main\.assertion\.2\] line 55 misaligned: SUCCESS$
+^\[main\.assertion\.3\] line 56 packed struct: FAILURE$
+^\[main\.assertion\.4\] line 57 reduced alignment: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+A pointer that the havoc_reachable statement moves within an object points to
+the start of the object plus a nondeterministic position. In objects whose
+members are at multiples of their alignment, the position is a multiple of the
+alignment of the pointer's target type: the statement is assumed not to store
+misaligned pointers (assertion 2 holds by this assumption). In arrays of
+packed structs, and of structs with a member whose alignment is reduced, the
+position is arbitrary. With a nondeterministic offset added to the candidate
+pointer and an assumption that the target type fits, cbmc took 20 s on this
+program instead of 1 s.
diff --git a/regression/cbmc/inline_asm_havoc_reachable_array/main.c b/regression/cbmc/inline_asm_havoc_reachable_array/main.c
new file mode 100644
index 000000000000..054674b76621
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable_array/main.c
@@ -0,0 +1,14 @@
+// Inline assembly that is not modeled may change each of the 4096 pointers in
+// a two-dimensional array of static lifetime. Symbolic execution assigns them
+// one by one, and must not expand the whole array for each of them.
+int x;
+int *a[64][64];
+
+int main(void)
+{
+ a[0][0] = &x;
+ asm volatile("cli" ::: "memory");
+ __CPROVER_assert(a[1][1] == 0, "element changed"); // fails
+ __CPROVER_assert(a[0][0] == &x, "first element changed"); // fails
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_havoc_reachable_array/test.desc b/regression/cbmc/inline_asm_havoc_reachable_array/test.desc
new file mode 100644
index 000000000000..1b8525ee77dd
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable_array/test.desc
@@ -0,0 +1,19 @@
+CORE gcc-only no-new-smt
+main.c
+--arch riscv64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 11 element changed: FAILURE$
+^\[main\.assertion\.2\] line 12 first element changed: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+The havoc_reachable statement assigns each pointer in the array separately.
+Assigning a[i][j] as an element of the whole array, rather than assigning the
+symbol that field-sensitive symbolic execution has for it, expanded all 4096
+elements for each of the 4096 assignments: symbolic execution took 80 s
+instead of 0.5 s. The test thus checks a time bound when run with a timeout
+(test.pl -t).
+The test is not run with the new SMT backend (no-new-smt), with which it took
+57 s (z3), as the solver is not what it checks.
diff --git a/regression/cbmc/inline_asm_havoc_reachable_budget/main.c b/regression/cbmc/inline_asm_havoc_reachable_budget/main.c
new file mode 100644
index 000000000000..b9160a547cac
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable_budget/main.c
@@ -0,0 +1,44 @@
+// Inline assembly that is not modeled changes 17 arrays of 63 nodes. Each of
+// the 1071 pointers has 64 candidate values (the previous values of the 63
+// pointers of its type, and the start of the array), 68544 in total. Symbolic
+// execution restricts pointers to their candidates only up to 65536 candidate
+// values in total; the remaining pointers are assigned arbitrary values.
+
+#define NODES(T) \
+ struct T \
+ { \
+ struct T *next; \
+ int data; \
+ } T##s[63];
+NODES(t00)
+NODES(t01)
+NODES(t02)
+NODES(t03)
+NODES(t04)
+NODES(t05)
+NODES(t06)
+NODES(t07)
+NODES(t08)
+NODES(t09)
+NODES(t10)
+NODES(t11)
+NODES(t12)
+NODES(t13)
+NODES(t14)
+NODES(t15)
+NODES(t16)
+
+int main(void)
+{
+ asm volatile("cli" ::: "memory");
+ __CPROVER_assert(
+ t00s[62].next == 0 || __CPROVER_same_object(t00s[62].next, t00s),
+ "first array"); // holds
+ __CPROVER_assert(
+ t16s[0].next == 0 || __CPROVER_same_object(t16s[0].next, t16s),
+ "within the bound"); // holds
+ __CPROVER_assert(
+ t16s[62].next == 0 || __CPROVER_same_object(t16s[62].next, t16s),
+ "beyond the bound"); // fails
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_havoc_reachable_budget/test.desc b/regression/cbmc/inline_asm_havoc_reachable_budget/test.desc
new file mode 100644
index 000000000000..8e109fe085fc
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable_budget/test.desc
@@ -0,0 +1,20 @@
+CORE gcc-only no-new-smt
+main.c
+--arch riscv64
+^EXIT=10$
+^SIGNAL=0$
+^havocking the objects reachable at file main\.c line 33 function main: the pointers have more than 65536 possible values in total, and some are assigned arbitrary values$
+^\[main\.assertion\.1\] line 34 first array: SUCCESS$
+^\[main\.assertion\.2\] line 37 within the bound: SUCCESS$
+^\[main\.assertion\.3\] line 40 beyond the bound: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+The objects of static lifetime are visited in the order of their names, and so
+are their pointers. 16 arrays (1008 pointers with 64 candidates each) and the
+first 16 elements of t16s are restricted to their candidates, which keeps them
+within the array of their type (or null). The other 47 pointers of t16s are
+assigned arbitrary values, which may point anywhere.
+The test is not run with the new SMT backend (no-new-smt), with which it took
+31 s (z3).
diff --git a/regression/cbmc/inline_asm_havoc_reachable_candidates/main.c b/regression/cbmc/inline_asm_havoc_reachable_candidates/main.c
new file mode 100644
index 000000000000..f1fc020bd2b4
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable_candidates/main.c
@@ -0,0 +1,97 @@
+// A pointer stored in an object that unmodeled inline assembly may change
+// has more candidate values (here: 70 objects of type int, and its own
+// previous value) than symbolic execution restricts pointers to. It must
+// then be assigned an arbitrary value, not one of the first candidates only.
+
+int g00;
+int g01;
+int g02;
+int g03;
+int g04;
+int g05;
+int g06;
+int g07;
+int g08;
+int g09;
+int g10;
+int g11;
+int g12;
+int g13;
+int g14;
+int g15;
+int g16;
+int g17;
+int g18;
+int g19;
+int g20;
+int g21;
+int g22;
+int g23;
+int g24;
+int g25;
+int g26;
+int g27;
+int g28;
+int g29;
+int g30;
+int g31;
+int g32;
+int g33;
+int g34;
+int g35;
+int g36;
+int g37;
+int g38;
+int g39;
+int g40;
+int g41;
+int g42;
+int g43;
+int g44;
+int g45;
+int g46;
+int g47;
+int g48;
+int g49;
+int g50;
+int g51;
+int g52;
+int g53;
+int g54;
+int g55;
+int g56;
+int g57;
+int g58;
+int g59;
+int g60;
+int g61;
+int g62;
+int g63;
+int g64;
+int g65;
+int g66;
+int g67;
+int g68;
+int g69;
+int *p = &g00;
+
+// a pointer with few candidates remains restricted to them
+struct S
+{
+ int x;
+} s1, s2;
+struct S *sp = &s1;
+
+int main(void)
+{
+ asm volatile("cli" ::: "memory");
+ __CPROVER_assert(p != &g69, "many candidates");
+ __CPROVER_assert(p == &g00, "previous value");
+ __CPROVER_assert(sp == &s1 || sp == &s2 || sp == 0, "few candidates");
+ // the assembly may have stored &g69 in p, although no pointer pointed to
+ // g69 before
+ g69 = 0;
+ *p = 1;
+ __CPROVER_assert(g69 == 0, "write through the pointer");
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_havoc_reachable_candidates/test.desc b/regression/cbmc/inline_asm_havoc_reachable_candidates/test.desc
new file mode 100644
index 000000000000..4c9128cd1902
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable_candidates/test.desc
@@ -0,0 +1,22 @@
+CORE gcc-only
+main.c
+--arch riscv64
+^EXIT=10$
+^SIGNAL=0$
+^havocking the objects reachable at file main\.c line 87 function main: some pointers have more than 64 possible values, and are assigned arbitrary values$
+^\[main\.assertion\.1\] line 88 many candidates: FAILURE$
+^\[main\.assertion\.2\] line 89 previous value: FAILURE$
+^\[main\.assertion\.3\] line 90 few candidates: SUCCESS$
+^\[main\.assertion\.4\] line 95 write through the pointer: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+Symbolic execution restricts each pointer stored in an object that the
+havoc_reachable statement changes to its candidate values (previous values,
+addresses of reachable objects, null) only if there are at most 64 of them.
+Restricting p to the first 64 candidates would exclude &g69, which the
+assembly may store in p.
+The value set of such an arbitrary pointer must contain the reachable objects
+of a compatible type, such as g69, even if no pointer pointed to them:
+otherwise, writing through p cannot change g69.
diff --git a/regression/cbmc/inline_asm_havoc_reachable_errno/main.c b/regression/cbmc/inline_asm_havoc_reachable_errno/main.c
new file mode 100644
index 000000000000..0a89985bd47c
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable_errno/main.c
@@ -0,0 +1,25 @@
+#include
+#include
+
+// errno and the result of getpwnam are objects of the C library model whose
+// names start with __CPROVER_, but they are memory that the program can
+// access: a statement that may access arbitrary memory may change them, for
+// example by calling a function that sets errno.
+int main(void)
+{
+ errno = 0;
+ struct passwd *pw = getpwnam("root");
+ __CPROVER_assume(pw != 0);
+ unsigned uid = pw->pw_uid;
+
+ int r;
+ __asm__ volatile("nonexistent_instruction %0" : "=r"(r));
+ __CPROVER_assert(errno == 0, "errno unchanged by operands only");
+ __CPROVER_assert(pw->pw_uid == uid, "passwd unchanged by operands only");
+
+ __asm__ volatile("nonexistent_instruction" ::: "memory");
+ __CPROVER_assert(errno == 0, "errno unchanged"); // fails
+ __CPROVER_assert(pw->pw_uid == uid, "passwd unchanged"); // fails
+
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_havoc_reachable_errno/test.desc b/regression/cbmc/inline_asm_havoc_reachable_errno/test.desc
new file mode 100644
index 000000000000..5654e90d8efc
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable_errno/test.desc
@@ -0,0 +1,17 @@
+CORE gcc-only
+main.c
+--arch riscv64 --no-standard-checks
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 17 errno unchanged by operands only: SUCCESS$
+^\[main\.assertion\.2\] line 18 passwd unchanged by operands only: SUCCESS$
+^\[main\.assertion\.3\] line 21 errno unchanged: FAILURE$
+^\[main\.assertion\.4\] line 22 passwd unchanged: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+A statement with a "memory" clobber may change all objects of static lifetime,
+including __CPROVER_errno, which models errno, and __CPROVER_passwd, which
+getpwnam returns. Both were treated as internal objects of CPROVER and kept
+their values.
diff --git a/regression/cbmc/inline_asm_havoc_reachable_offsets/main.c b/regression/cbmc/inline_asm_havoc_reachable_offsets/main.c
new file mode 100644
index 000000000000..a9e67b158556
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable_offsets/main.c
@@ -0,0 +1,36 @@
+// Inline assembly that is not modeled may store in the pointers it can reach
+// a pointer to any position within the objects it can reach (pointer
+// arithmetic, interior pointers), as long as an object of the pointer's
+// target type fits at that position.
+int a[4];
+int *gp = &a[0];
+
+struct S
+{
+ int x;
+ int y;
+} s;
+int *ip = &s.x;
+
+struct node
+{
+ int data;
+ struct node *next;
+} n1, n2;
+struct node *head = &n1;
+
+int main(void)
+{
+ n1.next = &n2;
+ asm volatile("cli" ::: "memory");
+ __CPROVER_assert(gp != &a[1], "pointer arithmetic"); // fails
+ __CPROVER_assert(ip != &s.y, "interior pointer"); // fails
+ __CPROVER_assert(gp != &a[3] + 1, "one past the end"); // holds (assumed)
+ if(gp != 0)
+ *gp = 1; // no dereference failure
+ __CPROVER_assert(
+ head == 0 || head == &n1 || head == &n2, "start of a node"); // holds
+ if(head != 0)
+ head->data = 1; // no dereference failure
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_havoc_reachable_offsets/test.desc b/regression/cbmc/inline_asm_havoc_reachable_offsets/test.desc
new file mode 100644
index 000000000000..e555513a5191
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable_offsets/test.desc
@@ -0,0 +1,21 @@
+CORE gcc-only
+main.c
+--arch riscv64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 26 pointer arithmetic: FAILURE$
+^\[main\.assertion\.2\] line 27 interior pointer: FAILURE$
+^\[main\.assertion\.3\] line 28 one past the end: SUCCESS$
+^\[main\.assertion\.4\] line 31 start of a node: SUCCESS$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+dereference failure.*: FAILURE$
+--
+Each pointer stored in an object that the havoc_reachable statement changes
+may, after the statement, point to any position within the objects its
+candidate values point into, provided that an object of the pointer's target
+type fits there and that the position is a multiple of the alignment of that
+type. The statement is assumed not to store other pointers, such as
+pointers one past the end of an object. Pointers to objects of exactly the
+pointer's target type, such as head, keep pointing to the start of objects.
diff --git a/regression/cbmc/inline_asm_havoc_reachable_union/main.c b/regression/cbmc/inline_asm_havoc_reachable_union/main.c
new file mode 100644
index 000000000000..7be153377800
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable_union/main.c
@@ -0,0 +1,59 @@
+// Inline assembly that is not modeled may store any reachable object's address
+// in the pointers stored in unions and in large arrays, which symbolic
+// execution does not split into their pointers. Writing through these pointers
+// after the statement may thus change y, although no pointer pointed to y.
+int x, y;
+
+union U
+{
+ int *p;
+ char *q;
+ long l;
+} u;
+
+struct S
+{
+ int a;
+ union U u;
+} s;
+
+union V
+{
+ struct
+ {
+ int *p;
+ } st;
+ long l;
+} v;
+
+int *big[100];
+union U unions[100];
+
+int main(void)
+{
+ u.p = &x;
+ s.u.p = &x;
+ v.st.p = &x;
+ big[5] = &x;
+ unions[7].p = &x;
+ asm volatile("cli" ::: "memory");
+ y = 0;
+ *u.p = 1;
+ __CPROVER_assert(y == 0, "first member of a union"); // fails
+ y = 0;
+ *u.q = 1;
+ __CPROVER_assert(y == 0, "second member of a union"); // fails
+ y = 0;
+ *s.u.p = 1;
+ __CPROVER_assert(y == 0, "union in a struct"); // fails
+ y = 0;
+ *v.st.p = 1;
+ __CPROVER_assert(y == 0, "struct in a union"); // fails
+ y = 0;
+ *big[3] = 1;
+ __CPROVER_assert(y == 0, "large array"); // fails
+ y = 0;
+ *unions[9].p = 1;
+ __CPROVER_assert(y == 0, "large array of unions"); // fails
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_havoc_reachable_union/test.desc b/regression/cbmc/inline_asm_havoc_reachable_union/test.desc
new file mode 100644
index 000000000000..507ad777911e
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable_union/test.desc
@@ -0,0 +1,24 @@
+CORE gcc-only
+main.c
+--arch riscv64 --no-standard-checks
+^EXIT=10$
+^SIGNAL=0$
+^havocking the objects reachable at file main\.c line 39 function main: some pointers are stored in unions or in arrays of more than 64 elements \(or of a size that is not constant\), and are assigned arbitrary values$
+^\[main\.assertion\.1\] line 42 first member of a union: FAILURE$
+^\[main\.assertion\.2\] line 45 second member of a union: FAILURE$
+^\[main\.assertion\.3\] line 48 union in a struct: FAILURE$
+^\[main\.assertion\.4\] line 51 struct in a union: FAILURE$
+^\[main\.assertion\.5\] line 54 large array: FAILURE$
+^\[main\.assertion\.6\] line 57 large array of unions: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+The havoc_reachable statement assigns unions, and arrays of more than 64
+elements, a nondeterministic value as a whole. The value set of a pointer in a
+union then had only unknown targets (and that of an element of a large array
+kept its previous targets), so that dereferencing it after the statement could
+not yield y, and the assertions held. The pointers in these objects are now
+assigned arbitrary values, whose value set contains all reachable objects of a
+compatible type. Without --no-standard-checks, the pointer checks of these
+dereferences fail.
diff --git a/regression/cbmc/inline_asm_havoc_reachable_unknown/main.c b/regression/cbmc/inline_asm_havoc_reachable_unknown/main.c
new file mode 100644
index 000000000000..56d16e4532bc
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable_unknown/main.c
@@ -0,0 +1,54 @@
+// Inline assembly that is not modeled may change memory whose address CBMC
+// does not know: the objects behind pointers whose targets are unknown (here:
+// returned by functions without a body), and memory at integer addresses (such
+// as memory-mapped registers).
+void *malloc(__CPROVER_size_t);
+int *ext(void);
+
+struct node
+{
+ struct node *next;
+ int data;
+};
+struct node *ext_node(void);
+
+const int *ext_const(void);
+
+int *gp;
+struct node *gn;
+const int *cp;
+
+#define REG (*(volatile int *)0x1000)
+
+int main(void)
+{
+ gp = ext();
+ gn = ext_node();
+ cp = ext_const();
+ int v = *cp;
+ int *lp = ext(); // its address is not taken
+ *gp = 1;
+ gn->data = 2;
+ *lp = 3;
+ REG = 4;
+ asm volatile("cli" ::: "memory");
+ __CPROVER_assert(*gp == 1, "unknown target of a static"); // fails
+ __CPROVER_assert(gn->data == 2, "unknown target, struct"); // fails
+ __CPROVER_assert(*lp == 3, "unknown target of a local"); // holds
+ __CPROVER_assert(REG == 4, "integer address, memory clobber"); // fails
+ __CPROVER_assert(*cp == v, "unknown target of a const pointer"); // fails
+
+ *gp = 1;
+ asm volatile("cli" : : "r"(gp + 1));
+ __CPROVER_assert(*gp == 1, "unknown target of an operand"); // fails
+
+ REG = 4;
+ asm volatile("cli" : : "r"((int *)0x2000));
+ __CPROVER_assert(REG == 4, "integer address of an operand"); // fails
+
+ // a pointer read from memory that was not written
+ int **pp = malloc(sizeof(int *));
+ __CPROVER_assume(pp != 0);
+ asm volatile("cli" : : "r"(*pp));
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_havoc_reachable_unknown/test.desc b/regression/cbmc/inline_asm_havoc_reachable_unknown/test.desc
new file mode 100644
index 000000000000..ac49b1ccd3d4
--- /dev/null
+++ b/regression/cbmc/inline_asm_havoc_reachable_unknown/test.desc
@@ -0,0 +1,30 @@
+CORE gcc-only
+main.c
+--arch riscv64 --no-standard-checks
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 35 unknown target of a static: FAILURE$
+^\[main\.assertion\.2\] line 36 unknown target, struct: FAILURE$
+^\[main\.assertion\.3\] line 37 unknown target of a local: SUCCESS$
+^\[main\.assertion\.4\] line 38 integer address, memory clobber: FAILURE$
+^\[main\.assertion\.5\] line 39 unknown target of a const pointer: FAILURE$
+^\[main\.assertion\.6\] line 43 unknown target of an operand: FAILURE$
+^\[main\.assertion\.7\] line 47 integer address of an operand: FAILURE$
+^havocking the objects reachable at file main\.c line 52 function main: some pointers have targets that are not known, and the objects they point to keep their values$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+^havocking the objects reachable at file main\.c line (34|42|46) function main: some pointers have targets
+--
+Dereferencing a pointer variable whose target is not known (gp, gn, cp, lp:
+the results of functions without a body) yields the variable's failed object
+(gp$object, ...). The havoc_reachable statement changes the failed objects of
+the reachable pointer variables (assertions 1, 2, 5, 6), also if the pointer
+points to a constant type (the object it points to need not be constant), and
+__CPROVER_memory, which models memory at integer addresses, with a "memory"
+clobber or if an operand may point to an integer address (assertions 4, 7).
+lp is a local variable whose address is not taken, which the statement cannot
+reach (assertion 3). The operand *pp is read from memory that was not
+written: it is no variable, and has no failed object, so a warning is
+printed. Without --no-standard-checks, the dereferences of these pointers fail
+the pointer checks.
diff --git a/regression/cbmc/inline_asm_lock_prefix/main.c b/regression/cbmc/inline_asm_lock_prefix/main.c
new file mode 100644
index 000000000000..ecba417207a9
--- /dev/null
+++ b/regression/cbmc/inline_asm_lock_prefix/main.c
@@ -0,0 +1,14 @@
+int main(void)
+{
+ // a lock prefix on a line of its own, followed by an instruction that
+ // takes it
+ int x = 0;
+ asm volatile("lock\n\tincl %0" : "+m"(x));
+ __CPROVER_assert(x == 0, "locked increment"); // fails
+
+ // the lock prefix is not modeled, even if the instruction it prefixes is
+ // (which makes the instruction invalid)
+ asm volatile("lock; nop");
+
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_lock_prefix/test.desc b/regression/cbmc/inline_asm_lock_prefix/test.desc
new file mode 100644
index 000000000000..d908a0333a53
--- /dev/null
+++ b/regression/cbmc/inline_asm_lock_prefix/test.desc
@@ -0,0 +1,16 @@
+CORE gcc-only
+main.c
+--arch riscv64
+^EXIT=10$
+^SIGNAL=0$
+^approximating inline assembly statement at file main\.c line 6 function main: instruction 'lock' is not modeled
+^approximating inline assembly statement at file main\.c line 11 function main: instruction 'lock' is not modeled
+^\[main\.assertion\.1\] line 7 locked increment: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+A lock prefix used to start an atomic section with a full fence around the
+instruction it prefixes, which was translated only if it was modeled. As
+none of the instructions that take a lock prefix is modeled, the prefix is
+now not modeled either.
diff --git a/regression/cbmc/inline_asm_modeled_outputs/main.c b/regression/cbmc/inline_asm_modeled_outputs/main.c
new file mode 100644
index 000000000000..bcbffe99b161
--- /dev/null
+++ b/regression/cbmc/inline_asm_modeled_outputs/main.c
@@ -0,0 +1,43 @@
+int main(void)
+{
+ // an empty template with an output tied to an input, as in the Linux
+ // kernel's RELOC_HIDE and OPTIMIZER_HIDE_VAR: the output receives the value
+ // of the input
+ int x = 0, y = 5;
+ asm("" : "=r"(x) : "0"(y));
+ __CPROVER_assert(x == 5, "tied output");
+
+ // the same, with a symbolic name
+ int a = 0, b = 6;
+ asm("" : [out] "=r"(a) : "[out]"(b));
+ __CPROVER_assert(a == 6, "tied named output");
+
+ // an output that is not tied to an input receives the contents of a
+ // register that CBMC does not know
+ int z = 0, w = 7;
+ asm volatile("nop" : "=a"(z) : "a"(w));
+ __CPROVER_assert(z == 0, "untied output"); // fails
+
+ // an in-out operand keeps its value
+ int c = 8;
+ asm("" : "+r"(c));
+ __CPROVER_assert(c == 8, "in-out operand");
+
+ // all inputs are read before any output is written
+ int p = 1, q = 2;
+ asm("" : "=r"(p), "=r"(q) : "0"(q), "1"(p));
+ __CPROVER_assert(p == 2 && q == 1, "swapped");
+
+ // the upper bits of a wider output are not known
+ long long wide = 0;
+ int narrow = 3;
+ asm("" : "=r"(wide) : "0"(narrow));
+ __CPROVER_assert(wide == 3, "wider output"); // fails
+
+ // an output that a modeled instruction writes is not changed otherwise
+ unsigned short cw;
+ asm volatile("fnstcw %0" : "=m"(cw));
+ __CPROVER_assert((cw >> 10 & 3) == 0, "written by fnstcw");
+
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_modeled_outputs/test.desc b/regression/cbmc/inline_asm_modeled_outputs/test.desc
new file mode 100644
index 000000000000..b3abc072b589
--- /dev/null
+++ b/regression/cbmc/inline_asm_modeled_outputs/test.desc
@@ -0,0 +1,22 @@
+CORE gcc-only
+main.c
+
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 8 tied output: SUCCESS$
+^\[main\.assertion\.2\] line 13 tied named output: SUCCESS$
+^\[main\.assertion\.3\] line 19 untied output: FAILURE$
+^\[main\.assertion\.4\] line 24 in-out operand: SUCCESS$
+^\[main\.assertion\.5\] line 29 swapped: SUCCESS$
+^\[main\.assertion\.6\] line 35 wider output: FAILURE$
+^\[main\.assertion\.7\] line 40 written by fnstcw: SUCCESS$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+inline assembly statement at
+--
+The instructions of these statements are all modeled. Their outputs with an
+"=" constraint are written by the statement: an output tied to an input by a
+matching constraint receives the value of the input (if the types have the
+same width), any other output receives a nondeterministic value unless a
+modeled instruction writes it. These outputs used to keep their values.
diff --git a/regression/cbmc/inline_asm_modeling/main.c b/regression/cbmc/inline_asm_modeling/main.c
new file mode 100644
index 000000000000..1569359e1b44
--- /dev/null
+++ b/regression/cbmc/inline_asm_modeling/main.c
@@ -0,0 +1,44 @@
+// Each statement below is processed independently by the remove_asm pass.
+// This test pins the documented translation of each statement (see
+// doc/cprover-manual/modeling-inline-asm.md) via the goto program shown by
+// --show-goto-functions.
+int main(void)
+{
+ unsigned short cw = 0;
+
+ // x86 fences: calls of the __asm_* fence helpers
+ asm volatile("mfence");
+ asm volatile("lfence");
+ asm volatile("sfence");
+
+ // ARM barriers: dmb/dsb are full fences unless the option restricts them
+ // to loads (ld) or stores (st); a dsb with a st option also orders stores
+ // before later loads; isb is an empty fence
+ asm volatile("dmb");
+ asm volatile("dsb sy");
+ asm volatile("dmb ish" ::: "memory");
+ asm volatile("dmb ishld");
+ asm volatile("dsb ishld");
+ asm volatile("dmb ishst");
+ asm volatile("dsb ishst");
+ asm volatile("isb");
+
+ // Power barriers: sync is a full fence, lwsync does not order stores
+ // before later loads, eieio orders stores, isync is an empty fence
+ asm volatile("sync");
+ asm volatile("hwsync");
+ asm volatile("lwsync");
+ asm volatile("eieio");
+ asm volatile("isync");
+
+ // instructions without effect on the C program state
+ asm volatile("nop");
+ asm volatile("pause");
+ asm volatile("yield");
+
+ // x86 FP control word: writes/reads __CPROVER_rounding_mode via the operand
+ asm volatile("fstcw %0" : "=m"(cw));
+ asm volatile("fldcw %0" : : "m"(cw));
+
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_modeling/test.desc b/regression/cbmc/inline_asm_modeling/test.desc
new file mode 100644
index 000000000000..b40d42fad46c
--- /dev/null
+++ b/regression/cbmc/inline_asm_modeling/test.desc
@@ -0,0 +1,46 @@
+CORE gcc-only
+main.c
+--arch riscv64 --show-goto-functions
+activate-multi-line-match
+^EXIT=0$
+^SIGNAL=0$
+^ *// \d+ file main\.c line 10 function main\n *CALL __asm_mfence\(\)\n *// \d+ file main\.c line (?!10 )
+^ *// \d+ file main\.c line 11 function main\n *CALL __asm_lfence\(\)\n *// \d+ file main\.c line (?!11 )
+^ *// \d+ file main\.c line 12 function main\n *CALL __asm_sfence\(\)\n *// \d+ file main\.c line (?!12 )
+^ *// \d+ file main\.c line 17 function main\n *FENCE WW RR RW WR\n *// \d+ file main\.c line (?!17 )
+^ *// \d+ file main\.c line 18 function main\n *FENCE WW RR RW WR\n *// \d+ file main\.c line (?!18 )
+^ *// \d+ file main\.c line 19 function main\n *FENCE WW RR RW WR\n *// \d+ file main\.c line (?!19 )
+^ *// \d+ file main\.c line 20 function main\n *FENCE RR RW\n *// \d+ file main\.c line (?!20 )
+^ *// \d+ file main\.c line 21 function main\n *FENCE RR RW\n *// \d+ file main\.c line (?!21 )
+^ *// \d+ file main\.c line 22 function main\n *FENCE WW\n *// \d+ file main\.c line (?!22 )
+^ *// \d+ file main\.c line 23 function main\n *FENCE WW WR\n *// \d+ file main\.c line (?!23 )
+^ *// \d+ file main\.c line 24 function main\n *FENCE\n *// \d+ file main\.c line (?!24 )
+^ *// \d+ file main\.c line 28 function main\n *FENCE WW RR RW WR\n *// \d+ file main\.c line (?!28 )
+^ *// \d+ file main\.c line 29 function main\n *FENCE WW RR RW WR\n *// \d+ file main\.c line (?!29 )
+^ *// \d+ file main\.c line 30 function main\n *FENCE WW RR RW\n *// \d+ file main\.c line (?!30 )
+^ *// \d+ file main\.c line 31 function main\n *FENCE WW\n *// \d+ file main\.c line (?!31 )
+^ *// \d+ file main\.c line 32 function main\n *FENCE\n *// \d+ file main\.c line (?!32 )
+^ *// \d+ file main\.c line 40 function main\n *CALL __asm_fstcw\(cast\(address_of\(main::1::cw\), empty\*\)\)\n *// \d+ file main\.c line (?!40 )
+^ *// \d+ file main\.c line 41 function main\n *CALL __asm_fldcw\(cast\(address_of\(main::1::cw\), empty\*\)\)\n *// \d+ file main\.c line (?!41 )
+^__asm_lfence /\* __asm_lfence \*/\n *// [^\n]*\n *FENCE RR RW\n *// [^\n]*\n *END_FUNCTION$
+^__asm_mfence /\* __asm_mfence \*/\n *// [^\n]*\n *FENCE WW RR RW WR\n *// [^\n]*\n *END_FUNCTION$
+^__asm_sfence /\* __asm_sfence \*/\n *// [^\n]*\n *FENCE WW\n *// [^\n]*\n *END_FUNCTION$
+--
+^warning: ignoring
+inline assembly statement at
+ATOMIC_BEGIN
+file main\.c line 35 function main
+file main\.c line 36 function main
+file main\.c line 37 function main
+--
+Pins the documented translation of each recognized inline-assembly statement
+by the remove_asm pass (see doc/cprover-manual/modeling-inline-asm.md): each
+pattern requires the statement on the given line to be translated into
+exactly the given instruction. x86 mfence/lfence/sfence -> __asm_* fence
+helper calls, which are full, RR RW, and WW fences, respectively; ARM
+dmb/dsb and Power sync/hwsync -> full fence (WW RR RW WR); ARM dmb/dsb with
+an ld option -> RR RW, dmb with an st option -> WW, dsb with an st option ->
+WW WR; Power lwsync -> fence without WR; Power eieio -> WW; ARM isb / Power
+isync -> empty fence; nop/pause/yield -> nothing; fstcw/fldcw ->
+__CPROVER_rounding_mode helpers called with the address of the operand. See
+GitHub issue diffblue/cbmc#6374.
diff --git a/regression/cbmc/inline_asm_msvc_names/main.i b/regression/cbmc/inline_asm_msvc_names/main.i
new file mode 100644
index 000000000000..0c357954f3af
--- /dev/null
+++ b/regression/cbmc/inline_asm_msvc_names/main.i
@@ -0,0 +1,32 @@
+// preprocessed input, as preprocessing for --winx64 fails on non-x86 hosts
+struct S
+{
+ int a;
+ int b;
+};
+
+unsigned short cw = 0x1234;
+
+int main(void)
+{
+ // the operand of fnstcw is a variable whose name is unique
+ unsigned short own;
+ __asm { fnstcw own }
+ __CPROVER_assert((own >> 10 & 3) == 0, "unique name"); // holds
+
+ // the operand of fnstcw names the global cw, but there is also a local cw,
+ // which is not in scope here
+ {
+ unsigned short cw = 0;
+ (void)cw;
+ }
+ __asm { fnstcw cw }
+ __CPROVER_assert(cw == 0x1234, "global cw unchanged"); // fails
+
+ // a member access names the variable that contains the member
+ struct S s = {0, 0};
+ __asm { mov s.a, 1 }
+ __CPROVER_assert(s.a == 0, "member written"); // fails
+
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_msvc_names/test.desc b/regression/cbmc/inline_asm_msvc_names/test.desc
new file mode 100644
index 000000000000..3fbd963b055b
--- /dev/null
+++ b/regression/cbmc/inline_asm_msvc_names/test.desc
@@ -0,0 +1,18 @@
+CORE
+main.i
+--winx64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 15 unique name: SUCCESS$
+^\[main\.assertion\.2\] line 24 global cw unchanged: FAILURE$
+^\[main\.assertion\.3\] line 29 member written: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+dead object.*: FAILURE$
+--
+The operand of fnstcw was looked up preferring a unique local variable over
+a global one, even when the local variable is not in scope, so a dead object
+was written and the global cw was not. If several variables have the name,
+the block is now approximated. The scanner of remove_asm returns s.a as one
+identifier, whose lookup failed, so s was not changed; now s.a names s.
diff --git a/regression/cbmc/inline_asm_mxcsr/main.c b/regression/cbmc/inline_asm_mxcsr/main.c
new file mode 100644
index 000000000000..8afc7b4294bd
--- /dev/null
+++ b/regression/cbmc/inline_asm_mxcsr/main.c
@@ -0,0 +1,23 @@
+int main(void)
+{
+ unsigned csr;
+
+ // the rounding mode is bits 13 and 14 of the SSE control register
+ asm volatile("stmxcsr %0" : "=m"(csr));
+ __CPROVER_assert((csr >> 13 & 3) == 0, "initial rounding mode");
+
+ // round downwards
+ csr = (csr & ~0x6000u) | 0x2000u;
+ asm volatile("ldmxcsr %0" : : "m"(csr));
+ float f = 1.0f;
+ __CPROVER_assert(f / 3.0f == 0x1.555554p-2f, "rounded downwards");
+
+ unsigned again;
+ asm volatile("stmxcsr %0" : "=m"(again));
+ __CPROVER_assert((again >> 13 & 3) == 1, "rounding mode set by ldmxcsr");
+
+ // the other bits are not modeled
+ __CPROVER_assert(again == csr, "other bits"); // fails
+
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_mxcsr/test.desc b/regression/cbmc/inline_asm_mxcsr/test.desc
new file mode 100644
index 000000000000..92913b97cce6
--- /dev/null
+++ b/regression/cbmc/inline_asm_mxcsr/test.desc
@@ -0,0 +1,16 @@
+CORE gcc-only no-new-smt
+main.c
+--arch riscv64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 7 initial rounding mode: SUCCESS$
+^\[main\.assertion\.2\] line 13 rounded downwards: SUCCESS$
+^\[main\.assertion\.3\] line 17 rounding mode set by ldmxcsr: SUCCESS$
+^\[main\.assertion\.4\] line 20 other bits: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+inline assembly statement at
+--
+stmxcsr and ldmxcsr read and set the rounding mode (bits 13 and 14 of the
+SSE control and status register); the other bits are not modeled.
diff --git a/regression/cbmc/inline_asm_output_addresses/main.c b/regression/cbmc/inline_asm_output_addresses/main.c
new file mode 100644
index 000000000000..e5c7fea21e66
--- /dev/null
+++ b/regression/cbmc/inline_asm_output_addresses/main.c
@@ -0,0 +1,23 @@
+// The address of the output arr[idx] depends on the output idx. GCC at -O0
+// computes it after writing idx (arr[1] = 9), GCC at -O2 and Clang compute it
+// before (arr[0] = 9), so both behaviours must be possible.
+int main(void)
+{
+ // all instructions modeled (none)
+ int idx = 0, arr[2] = {0, 0};
+ asm("" : "=r"(idx), "=r"(arr[idx]) : "0"(1), "1"(9));
+ __CPROVER_assert(
+ !(idx == 1 && arr[0] == 9 && arr[1] == 0),
+ "address before writing idx"); // fails
+ __CPROVER_assert(
+ !(idx == 1 && arr[0] == 0 && arr[1] == 9),
+ "address after writing idx"); // fails
+
+ // an instruction that is not modeled
+ int j = 0, brr[2] = {0, 0};
+ asm("unknown_instruction" : "=r"(j), "=r"(brr[j]));
+ __CPROVER_assert(brr[1] == 0, "address after writing j"); // fails
+ __CPROVER_assert(brr[0] == 0, "address before writing j"); // fails
+
+ return 0;
+}
diff --git a/regression/cbmc/inline_asm_output_addresses/test.desc b/regression/cbmc/inline_asm_output_addresses/test.desc
new file mode 100644
index 000000000000..e8aaf6999a28
--- /dev/null
+++ b/regression/cbmc/inline_asm_output_addresses/test.desc
@@ -0,0 +1,20 @@
+CORE gcc-only
+main.c
+--arch riscv64
+^EXIT=10$
+^SIGNAL=0$
+^approximating inline assembly statement at file main\.c line 8 function main: the address of an output operand depends on the program state, and compilers differ in whether they compute it before or after writing the other outputs, so
+^\[main\.assertion\.1\] line 9 address before writing idx: FAILURE$
+^\[main\.assertion\.2\] line 12 address after writing idx: FAILURE$
+^\[main\.assertion\.3\] line 19 address after writing j: FAILURE$
+^\[main\.assertion\.4\] line 20 address before writing j: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+The address of an output such as arr[idx] depends on another output, idx.
+GCC at -O0 computes it after writing idx, GCC at -O2 and Clang before. A
+statement whose instructions are all modeled used to write the output at the
+address before the statement only, and the approximation of a statement with
+an instruction that is not modeled used to change only the object at that
+address; both excluded the behaviour of GCC at -O0.
diff --git a/regression/goto-analyzer/dependence-graph-havoc-reachable/main.c b/regression/goto-analyzer/dependence-graph-havoc-reachable/main.c
new file mode 100644
index 000000000000..4cfaf3c84531
--- /dev/null
+++ b/regression/goto-analyzer/dependence-graph-havoc-reachable/main.c
@@ -0,0 +1,15 @@
+int x;
+int *p;
+
+int main()
+{
+ p = &x;
+ x = 1;
+#ifdef __GNUC__
+ asm volatile("cli" ::: "memory");
+#else
+ __asm { cli }
+#endif
+ int out = *p;
+ return out;
+}
diff --git a/regression/goto-analyzer/dependence-graph-havoc-reachable/test.desc b/regression/goto-analyzer/dependence-graph-havoc-reachable/test.desc
new file mode 100644
index 000000000000..2b8f05d32ad1
--- /dev/null
+++ b/regression/goto-analyzer/dependence-graph-havoc-reachable/test.desc
@@ -0,0 +1,15 @@
+CORE
+main.c
+--arch riscv64 --dependence-graph --show
+activate-multi-line-match
+^EXIT=0$
+^SIGNAL=0$
+\/\/ ([0-9]+).*\n.*p := address_of\(x\)\n(.*\n)*Data dependencies: ([0-9]+,)*\1(,[0-9]+)*\n\n.*\n.*HAVOC_REACHABLE \(arbitrary memory\)
+\/\/ ([0-9]+).*\n.*HAVOC_REACHABLE \(arbitrary memory\)\n(.*\n)*Data dependencies: ([0-9]+,)*\1(,[0-9]+)*\n\n.*\n.*out := \*p
+--
+^warning: ignoring
+--
+The havoc_reachable statement emitted for the inline assembly reads the
+pointers stored in the objects reachable from the objects of static lifetime
+(here, p), and writes the objects it reaches (here, x, which out is then read
+from).
diff --git a/regression/goto-analyzer/havoc-object-domains/constants.desc b/regression/goto-analyzer/havoc-object-domains/constants.desc
new file mode 100644
index 000000000000..88fcee47df75
--- /dev/null
+++ b/regression/goto-analyzer/havoc-object-domains/constants.desc
@@ -0,0 +1,14 @@
+CORE
+main.c
+--verify --constants
+^EXIT=0$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 7 pointee: UNKNOWN$
+^\[main\.assertion\.2\] line 8 local without address: SUCCESS$
+^\[main\.assertion\.3\] line 17 conditional pointee: UNKNOWN$
+--
+^warning: ignoring
+--
+__CPROVER_havoc_object, which the approximation of inline assembly uses for
+the objects its pointer operands point to, may change any of the objects of
+static lifetime and the address-taken variables. The domain ignored it.
diff --git a/regression/goto-analyzer/havoc-object-domains/intervals.desc b/regression/goto-analyzer/havoc-object-domains/intervals.desc
new file mode 100644
index 000000000000..8065eb977b0f
--- /dev/null
+++ b/regression/goto-analyzer/havoc-object-domains/intervals.desc
@@ -0,0 +1,13 @@
+CORE
+main.c
+--verify --intervals
+^EXIT=0$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 7 pointee: UNKNOWN$
+^\[main\.assertion\.3\] line 17 conditional pointee: UNKNOWN$
+--
+^warning: ignoring
+--
+__CPROVER_havoc_object, which the approximation of inline assembly uses for
+the objects its pointer operands point to, may change any variable. The
+domain ignored it.
diff --git a/regression/goto-analyzer/havoc-object-domains/main.c b/regression/goto-analyzer/havoc-object-domains/main.c
new file mode 100644
index 000000000000..d79880f1cdd7
--- /dev/null
+++ b/regression/goto-analyzer/havoc-object-domains/main.c
@@ -0,0 +1,20 @@
+int main()
+{
+ int k = 0;
+ int *p = &k;
+ int unaddressed = 0;
+ __CPROVER_havoc_object(p);
+ __CPROVER_assert(k <= 0, "pointee"); // may change
+ __CPROVER_assert(unaddressed <= 0, "local without address"); // unchanged
+
+ // as in the approximation of inline assembly, which skips null pointers
+ int j = 0;
+ int *q = &j;
+ if(q == 0)
+ goto skip;
+ __CPROVER_havoc_object(q);
+skip:
+ __CPROVER_assert(j <= 0, "conditional pointee"); // may change
+
+ return 0;
+}
diff --git a/regression/goto-analyzer/havoc-object-domains/vsd.desc b/regression/goto-analyzer/havoc-object-domains/vsd.desc
new file mode 100644
index 000000000000..6af36cb8f77d
--- /dev/null
+++ b/regression/goto-analyzer/havoc-object-domains/vsd.desc
@@ -0,0 +1,13 @@
+CORE
+main.c
+--verify --vsd
+^EXIT=0$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 7 pointee: UNKNOWN$
+^\[main\.assertion\.3\] line 17 conditional pointee: UNKNOWN$
+--
+^warning: ignoring
+--
+__CPROVER_havoc_object, which the approximation of inline assembly uses for
+the objects its pointer operands point to, may change any variable. The
+domain ignored it.
diff --git a/regression/goto-analyzer/havoc-reachable-domains/constants.desc b/regression/goto-analyzer/havoc-reachable-domains/constants.desc
new file mode 100644
index 000000000000..bb808ac8431d
--- /dev/null
+++ b/regression/goto-analyzer/havoc-reachable-domains/constants.desc
@@ -0,0 +1,13 @@
+CORE
+main.c
+--arch riscv64 --verify --constants
+^EXIT=0$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 14 global: UNKNOWN$
+^\[main\.assertion\.2\] line 15 address-taken local: UNKNOWN$
+^\[main\.assertion\.3\] line 16 local without address: SUCCESS$
+--
+^warning: ignoring
+--
+Inline assembly with a "memory" clobber may change the objects of static
+lifetime and the address-taken local variables (the havoc_reachable statement).
diff --git a/regression/goto-analyzer/havoc-reachable-domains/intervals.desc b/regression/goto-analyzer/havoc-reachable-domains/intervals.desc
new file mode 100644
index 000000000000..7a948a207074
--- /dev/null
+++ b/regression/goto-analyzer/havoc-reachable-domains/intervals.desc
@@ -0,0 +1,12 @@
+CORE
+main.c
+--arch riscv64 --verify --intervals
+^EXIT=0$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 14 global: UNKNOWN$
+^\[main\.assertion\.2\] line 15 address-taken local: UNKNOWN$
+--
+^warning: ignoring
+--
+Inline assembly with a "memory" clobber may change the objects of static
+lifetime and the address-taken local variables (the havoc_reachable statement).
diff --git a/regression/goto-analyzer/havoc-reachable-domains/main.c b/regression/goto-analyzer/havoc-reachable-domains/main.c
new file mode 100644
index 000000000000..41ec0626fcdf
--- /dev/null
+++ b/regression/goto-analyzer/havoc-reachable-domains/main.c
@@ -0,0 +1,18 @@
+int global;
+
+int main()
+{
+ global = 1;
+ int local = 2;
+ int *p = &local;
+ int unaddressed = 3;
+#ifdef __GNUC__
+ asm volatile("cli" ::: "memory");
+#else
+ __asm { cli }
+#endif
+ __CPROVER_assert(global == 1, "global"); // may change
+ __CPROVER_assert(local == 2, "address-taken local"); // may change
+ __CPROVER_assert(unaddressed == 3, "local without address"); // unchanged
+ return 0;
+}
diff --git a/regression/goto-analyzer/havoc-reachable-domains/vsd.desc b/regression/goto-analyzer/havoc-reachable-domains/vsd.desc
new file mode 100644
index 000000000000..10c6c3cbfe74
--- /dev/null
+++ b/regression/goto-analyzer/havoc-reachable-domains/vsd.desc
@@ -0,0 +1,12 @@
+CORE
+main.c
+--arch riscv64 --verify --vsd
+^EXIT=0$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 14 global: UNKNOWN$
+^\[main\.assertion\.2\] line 15 address-taken local: UNKNOWN$
+--
+^warning: ignoring
+--
+Inline assembly with a "memory" clobber may change the objects of static
+lifetime and the address-taken local variables (the havoc_reachable statement).
diff --git a/regression/goto-analyzer/havoc-reachable-three-way-merge/main.c b/regression/goto-analyzer/havoc-reachable-three-way-merge/main.c
new file mode 100644
index 000000000000..4a3559602ad2
--- /dev/null
+++ b/regression/goto-analyzer/havoc-reachable-three-way-merge/main.c
@@ -0,0 +1,48 @@
+int g;
+int h;
+int *p;
+
+static void clobber(void)
+{
+#ifdef __GNUC__
+ asm volatile("cli" ::: "memory");
+#else
+ __asm { cli }
+#endif
+}
+
+static void write_through_pointer(void)
+{
+ *p = 3;
+}
+
+static void set_h(void)
+{
+ h = 2;
+}
+
+int main()
+{
+ g = 1;
+ clobber();
+ __CPROVER_assert(g == 1, "known at the entry of the callee"); // may change
+
+ g = 2;
+ clobber();
+ __CPROVER_assert(g == 2, "unknown at the entry of the callee"); // may change
+
+ int local = 3;
+ int *alias = &local;
+ clobber();
+ __CPROVER_assert(local == 3, "address-taken local"); // may change
+
+ g = 4;
+ write_through_pointer();
+ __CPROVER_assert(g == 4, "written through a pointer"); // may change
+
+ g = 5;
+ set_h();
+ __CPROVER_assert(g == 5, "not changed by the callee"); // unchanged
+ __CPROVER_assert(h == 2, "assigned by the callee"); // unchanged
+ return 0;
+}
diff --git a/regression/goto-analyzer/havoc-reachable-three-way-merge/test.desc b/regression/goto-analyzer/havoc-reachable-three-way-merge/test.desc
new file mode 100644
index 000000000000..268fdef722ac
--- /dev/null
+++ b/regression/goto-analyzer/havoc-reachable-three-way-merge/test.desc
@@ -0,0 +1,19 @@
+CORE
+main.c
+--arch riscv64 --verify --vsd --three-way-merge
+^EXIT=0$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 28 known at the entry of the callee: UNKNOWN$
+^\[main\.assertion\.2\] line 32 unknown at the entry of the callee: UNKNOWN$
+^\[main\.assertion\.3\] line 37 address-taken local: UNKNOWN$
+^\[main\.assertion\.4\] line 41 written through a pointer: UNKNOWN$
+^\[main\.assertion\.5\] line 45 not changed by the callee: SUCCESS$
+^\[main\.assertion\.6\] line 46 assigned by the callee: SUCCESS$
+--
+^warning: ignoring
+--
+The inline assembly in clobber (and the write through a pointer that the
+domain does not know) makes the variable-sensitivity domain forget all values.
+The three-way merge at the return of the function must propagate this to the
+caller, also for variables that are unknown at the entry of the function (the
+two calls of clobber are merged at its entry, so g is unknown there).
diff --git a/regression/goto-analyzer/variable-sensitivity-havoc-merge/main.c b/regression/goto-analyzer/variable-sensitivity-havoc-merge/main.c
new file mode 100644
index 000000000000..075036b371c7
--- /dev/null
+++ b/regression/goto-analyzer/variable-sensitivity-havoc-merge/main.c
@@ -0,0 +1,14 @@
+void opaque(int *);
+int nondet(void);
+
+int main()
+{
+ int m = 0;
+ if(nondet())
+ goto done;
+ // a call of a function without a body havocs the environment
+ opaque(&m);
+done:
+ __CPROVER_assert(m == 0, "conditional call"); // may fail
+ return 0;
+}
diff --git a/regression/goto-analyzer/variable-sensitivity-havoc-merge/test.desc b/regression/goto-analyzer/variable-sensitivity-havoc-merge/test.desc
new file mode 100644
index 000000000000..9c3b1313ee5f
--- /dev/null
+++ b/regression/goto-analyzer/variable-sensitivity-havoc-merge/test.desc
@@ -0,0 +1,13 @@
+CORE
+main.c
+--verify --vsd
+^EXIT=0$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 12 conditional call: UNKNOWN$
+--
+^warning: ignoring
+--
+Havocking the environment, as for a call of a function without a body,
+removed all variables from it. A merge only merges the variables that both
+environments have, so when the path that jumps over the call reached the
+label first, m kept the value 0, and the assertion was reported as holding.
diff --git a/regression/goto-cc-cbmc/CMakeLists.txt b/regression/goto-cc-cbmc/CMakeLists.txt
index 53647ddb692f..8a64f81da49a 100644
--- a/regression/goto-cc-cbmc/CMakeLists.txt
+++ b/regression/goto-cc-cbmc/CMakeLists.txt
@@ -4,6 +4,13 @@ else()
set(is_windows false)
endif()
+if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
+ set(gcc_only -X gcc-only)
+else()
+ set(gcc_only "")
+endif()
+
add_test_pl_tests(
"${CMAKE_CURRENT_SOURCE_DIR}/chain.sh $ $ ${is_windows}"
+ ${gcc_only}
)
diff --git a/regression/goto-cc-cbmc/Makefile b/regression/goto-cc-cbmc/Makefile
index 10734a47ff28..758398d8ee53 100644
--- a/regression/goto-cc-cbmc/Makefile
+++ b/regression/goto-cc-cbmc/Makefile
@@ -6,16 +6,18 @@ include ../../src/common
ifeq ($(BUILD_ENV_),MSVC)
exe=../../../src/goto-cc/goto-cl
is_windows=true
+ GCC_ONLY = -X gcc-only
else
exe=../../../src/goto-cc/goto-cc
is_windows=false
+ GCC_ONLY =
endif
test:
- @../test.pl -e -p -c '../chain.sh $(exe) ../../../src/cbmc/cbmc $(is_windows)'
+ @../test.pl -e -p -c '../chain.sh $(exe) ../../../src/cbmc/cbmc $(is_windows)' $(GCC_ONLY)
tests.log:
- @../test.pl -e -p -c '../chain.sh $(exe) ../../../src/cbmc/cbmc $(is_windows)'
+ @../test.pl -e -p -c '../chain.sh $(exe) ../../../src/cbmc/cbmc $(is_windows)' $(GCC_ONLY)
clean:
$(RM) tests.log
diff --git a/regression/goto-cc-cbmc/asm_intel_syntax/directive.desc b/regression/goto-cc-cbmc/asm_intel_syntax/directive.desc
new file mode 100644
index 000000000000..e2cac7720ef5
--- /dev/null
+++ b/regression/goto-cc-cbmc/asm_intel_syntax/directive.desc
@@ -0,0 +1,13 @@
+CORE gcc-only
+directive.i
+--arch x86_64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 18 AT&T syntax: FAILURE$
+approximating inline assembly statement at .* line 7 function directive: '\.intel_syntax noprefix' switches to Intel syntax
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+A template that switches to Intel syntax (in which the statement subtracts b
+from a) is approximated.
diff --git a/regression/goto-cc-cbmc/asm_intel_syntax/directive.i b/regression/goto-cc-cbmc/asm_intel_syntax/directive.i
new file mode 100644
index 000000000000..8af05f92e696
--- /dev/null
+++ b/regression/goto-cc-cbmc/asm_intel_syntax/directive.i
@@ -0,0 +1,20 @@
+// Inline assembly that switches to Intel syntax with .intel_syntax: the
+// destination is the first operand.
+
+__attribute__((noinline)) int directive(int a, int b)
+{
+ // a -= b
+ __asm__(".intel_syntax noprefix\n\t"
+ "sub %0, %1\n\t"
+ ".att_syntax prefix"
+ : "+r"(a), "+r"(b)
+ :
+ : "cc");
+ return a;
+}
+
+int main(void)
+{
+ __CPROVER_assert(directive(10, 3) == 10, "AT&T syntax");
+ return 0;
+}
diff --git a/regression/goto-cc-cbmc/asm_intel_syntax/main.i b/regression/goto-cc-cbmc/asm_intel_syntax/main.i
new file mode 100644
index 000000000000..187d0a4853a3
--- /dev/null
+++ b/regression/goto-cc-cbmc/asm_intel_syntax/main.i
@@ -0,0 +1,15 @@
+// Inline assembly in Intel syntax, selected with -masm=intel: the destination
+// is the first operand.
+
+__attribute__((noinline)) int masm(int a, int b)
+{
+ // with -masm=intel: a -= b; in AT&T syntax: b -= a
+ __asm__("sub %0, %1" : "+r"(a), "+r"(b) : : "cc");
+ return a;
+}
+
+int main(void)
+{
+ __CPROVER_assert(masm(10, 3) == 10, "AT&T syntax");
+ return 0;
+}
diff --git a/regression/goto-cc-cbmc/asm_intel_syntax/test.desc b/regression/goto-cc-cbmc/asm_intel_syntax/test.desc
new file mode 100644
index 000000000000..315cb38db161
--- /dev/null
+++ b/regression/goto-cc-cbmc/asm_intel_syntax/test.desc
@@ -0,0 +1,15 @@
+CORE gcc-only
+main.i
+-masm=intel _ --arch x86_64
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 13 AT&T syntax: FAILURE$
+approximating inline assembly statement at .* line 7 function masm: the template is in the intel dialect \(-masm=intel\)
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+goto-cc records the dialect that -masm selects on the statement, so that cbmc
+does not read an Intel-syntax template as AT&T syntax (in which the statement
+would leave a unchanged; with -masm=intel, it subtracts b from a). Such
+statements are approximated.
diff --git a/regression/goto-cc-cbmc/chain.sh b/regression/goto-cc-cbmc/chain.sh
index 61ec544401e8..243923017f33 100755
--- a/regression/goto-cc-cbmc/chain.sh
+++ b/regression/goto-cc-cbmc/chain.sh
@@ -11,10 +11,18 @@ name=${*:$#}
base_name=${name%.c}
base_name=${base_name%.cpp}
+# options before " _ " are passed to goto-cc, the others to cbmc
+goto_cc_options=""
+if [[ "$options" == *" _ "* ]]
+then
+ goto_cc_options="${options%%" _ "*}"
+ options="${options#*" _ "}"
+fi
+
if [[ "${is_windows}" == "true" ]]; then
- "${goto_cc}" "${name}" "/Fe${base_name}.gb"
+ "${goto_cc}" ${goto_cc_options} "${name}" "/Fe${base_name}.gb"
else
- "${goto_cc}" "${name}" -o "${base_name}.gb"
+ "${goto_cc}" ${goto_cc_options} "${name}" -o "${base_name}.gb"
fi
"${cbmc}" "${base_name}.gb" ${options}
diff --git a/regression/goto-cc-file-local/asm-goto/main.c b/regression/goto-cc-file-local/asm-goto/main.c
new file mode 100644
index 000000000000..586c59b6b3d1
--- /dev/null
+++ b/regression/goto-cc-file-local/asm-goto/main.c
@@ -0,0 +1,46 @@
+// asm goto in a file-local (static) function, as in the Linux kernel's
+// arch_static_branch (static keys). goto-cc --export-file-local-symbols
+// renames the symbols of such functions, which must include the variable that
+// selects the label the asm goto jumps to: else symbolic execution fails to
+// find it, or the jump is lost and control always reaches "return 0".
+struct static_key
+{
+ int enabled;
+};
+
+#ifdef __GNUC__
+# define ALWAYS_INLINE __attribute__((always_inline))
+#else
+# define ALWAYS_INLINE
+#endif
+
+static inline ALWAYS_INLINE _Bool
+arch_static_branch(struct static_key *const key, const _Bool branch)
+{
+#ifdef __GNUC__
+ asm goto(
+ "1: jmp %l[l_yes]\n\t"
+ ".pushsection __jump_table, \"aw\"\n\t"
+ ".long 1b - . \n\t"
+ ".popsection\n\t"
+ :
+ : "i"(key), "i"(branch)
+ :
+ : l_yes);
+ return 0;
+l_yes:
+ return 1;
+#else
+ return branch;
+#endif
+}
+
+struct static_key kc;
+
+int main(void)
+{
+ int x = arch_static_branch(&kc, 1) ? 1 : 0;
+ __CPROVER_assert(x <= 1, "x"); // holds
+ __CPROVER_assert(x == 0, "jump is not taken"); // fails: the jump may be taken
+ return 0;
+}
diff --git a/regression/goto-cc-file-local/asm-goto/test.desc b/regression/goto-cc-file-local/asm-goto/test.desc
new file mode 100644
index 000000000000..39026da38cc8
--- /dev/null
+++ b/regression/goto-cc-file-local/asm-goto/test.desc
@@ -0,0 +1,17 @@
+CORE
+main.c
+compile-and-link assertion-check
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 43 x: SUCCESS$
+^\[main\.assertion\.2\] line 44 jump is not taken: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+^\*\*\*\* WARNING: no body for function
+--
+goto-cc --export-file-local-symbols renames the symbols of file-local
+functions. The variable that selects the label an asm goto jumps to must be
+renamed with them, else symbolic execution fails to find it (and crashes), or
+the dispatch on it is lost, in which case the jump is never taken and the
+second assertion holds.
diff --git a/regression/goto-cc-goto-analyzer/dependence-graph-havoc-reachable-heap/main.c b/regression/goto-cc-goto-analyzer/dependence-graph-havoc-reachable-heap/main.c
new file mode 100644
index 000000000000..7a0a22507812
--- /dev/null
+++ b/regression/goto-cc-goto-analyzer/dependence-graph-havoc-reachable-heap/main.c
@@ -0,0 +1,23 @@
+#include
+
+struct node
+{
+ struct node *next;
+ int data;
+};
+
+struct node *head;
+
+int main()
+{
+ struct node *a = malloc(sizeof(struct node));
+ a->next = malloc(sizeof(struct node));
+ head = a;
+#ifdef __GNUC__
+ asm volatile("unmodeled_instruction" ::: "memory");
+#else
+ __asm { unmodeled_instruction }
+#endif
+ struct node *second = head->next;
+ return second != 0;
+}
diff --git a/regression/goto-cc-goto-analyzer/dependence-graph-havoc-reachable-heap/test.desc b/regression/goto-cc-goto-analyzer/dependence-graph-havoc-reachable-heap/test.desc
new file mode 100644
index 000000000000..76408ec9ae74
--- /dev/null
+++ b/regression/goto-cc-goto-analyzer/dependence-graph-havoc-reachable-heap/test.desc
@@ -0,0 +1,17 @@
+CORE
+main.c
+--dependence-graph --show
+activate-multi-line-match
+^EXIT=0$
+^SIGNAL=0$
+\/\/ ([0-9]+).*\n.*head := main::1::a\n(.*\n)*Data dependencies: ([0-9]+,)*\1(,[0-9]+)*\n\n.*\n.*HAVOC_REACHABLE \(arbitrary memory\)
+\/\/ ([0-9]+).*\n.*HAVOC_REACHABLE \(arbitrary memory\)\n(.*\n)*Data dependencies: ([0-9]+,)*\1(,[0-9]+)*\n\n.*\n.*second := \*head\.next
+--
+^warning: ignoring
+--
+The flow-insensitive value sets that the reaching definitions use contain
+references to the value sets of other variables, which the havoc_reachable
+statement must not take as objects of their own: following them made
+goto-analyzer recurse until the stack overflowed. The statement reads head and
+the pointers in the heap objects it reaches, and writes the objects it
+reaches.
diff --git a/regression/goto-instrument/CMakeLists.txt b/regression/goto-instrument/CMakeLists.txt
index f7f08694f6aa..758628d7a21f 100644
--- a/regression/goto-instrument/CMakeLists.txt
+++ b/regression/goto-instrument/CMakeLists.txt
@@ -4,6 +4,13 @@ else()
set(is_windows false)
endif()
+if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
+ set(gcc_only -X gcc-only)
+else()
+ set(gcc_only "")
+endif()
+
add_test_pl_tests(
"${CMAKE_CURRENT_SOURCE_DIR}/chain.sh $ $ $ ${is_windows}"
+ ${gcc_only}
)
diff --git a/regression/goto-instrument/Makefile b/regression/goto-instrument/Makefile
index 6712245e3324..dc9ce6e27f05 100644
--- a/regression/goto-instrument/Makefile
+++ b/regression/goto-instrument/Makefile
@@ -6,16 +6,18 @@ include ../../src/common
ifeq ($(BUILD_ENV_),MSVC)
exe=../../../src/goto-cc/goto-cl
is_windows=true
+ GCC_ONLY = -X gcc-only
else
exe=../../../src/goto-cc/goto-cc
is_windows=false
+ GCC_ONLY =
endif
test:
- @../test.pl -e -p -c '../chain.sh $(exe) ../../../src/goto-instrument/goto-instrument ../../../src/cbmc/cbmc $(is_windows)'
+ @../test.pl -e -p -c '../chain.sh $(exe) ../../../src/goto-instrument/goto-instrument ../../../src/cbmc/cbmc $(is_windows)' $(GCC_ONLY)
tests.log:
- @../test.pl -e -p -c '../chain.sh $(exe) ../../../src/goto-instrument/goto-instrument ../../../src/cbmc/cbmc $(is_windows)'
+ @../test.pl -e -p -c '../chain.sh $(exe) ../../../src/goto-instrument/goto-instrument ../../../src/cbmc/cbmc $(is_windows)' $(GCC_ONLY)
clean:
$(RM) tests.log
diff --git a/regression/goto-instrument/dump-asm-goto/main.c b/regression/goto-instrument/dump-asm-goto/main.c
new file mode 100644
index 000000000000..2486b26f44a8
--- /dev/null
+++ b/regression/goto-instrument/dump-asm-goto/main.c
@@ -0,0 +1,32 @@
+// asm goto statements in the output of --dump-c, which is compiled again and
+// checked: the jumps of the statements must be preserved, and the variable that
+// selects the label in the goto program must say "no jump" when the statement
+// falls through.
+int nondet_int(void);
+
+static int modeled(void)
+{
+ // modeled, and does not jump
+ asm goto("nop" : : : : out);
+ return 0;
+out:
+ return 1;
+}
+
+static int approximated(int c)
+{
+ // not modeled: may jump to zero or fall through
+ asm goto("testl %0, %0; jz %l[zero]" : : "r"(c) : : zero);
+ return 0;
+zero:
+ return 1;
+}
+
+int main(void)
+{
+ __CPROVER_assert(modeled() == 0, "modeled statement does not jump"); // holds
+ int r = approximated(nondet_int());
+ __CPROVER_assert(r == 0, "falls through"); // fails
+ __CPROVER_assert(r == 1, "jumps"); // fails
+ return 0;
+}
diff --git a/regression/goto-instrument/dump-asm-goto/test.desc b/regression/goto-instrument/dump-asm-goto/test.desc
new file mode 100644
index 000000000000..2c96337fc468
--- /dev/null
+++ b/regression/goto-instrument/dump-asm-goto/test.desc
@@ -0,0 +1,22 @@
+CORE gcc-only
+main.c
+--dump-c _ --validate-goto-model --validate-ssa-equation
+^EXIT=10$
+^SIGNAL=0$
+^\s*asm goto\("nop" *: *: *: *: out\);$
+^\s*asm goto\("testl %0, %0; jz %l\[zero\]" *: *: "r"\(c\) *: *: zero\);$
+^\s*tmp_asm_goto_target = 0u;$
+^\[main\.assertion\.1\] .*: SUCCESS$
+^\[main\.assertion\.2\] .*: FAILURE$
+^\[main\.assertion\.3\] .*: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+^\s*asm\("nop"
+--
+--dump-c prints asm goto statements with their labels, and assigns 0 ("no
+jump") to the variable that selects the label in the goto program before the
+statement, as in C the statement jumps to the label directly. The output is
+compiled again and checked: the modeled statement, which does not jump, still
+does not jump (assertion 1), and the approximated one may still jump or fall
+through (assertions 2 and 3).
diff --git a/regression/goto-instrument/isr-havoc-reachable/main.c b/regression/goto-instrument/isr-havoc-reachable/main.c
new file mode 100644
index 000000000000..0f12a67b446e
--- /dev/null
+++ b/regression/goto-instrument/isr-havoc-reachable/main.c
@@ -0,0 +1,18 @@
+int flag;
+
+void isr(void)
+{
+#ifdef __GNUC__
+ asm volatile("unmodeled_instruction" ::: "memory");
+#else
+ __asm { unmodeled_instruction }
+#endif
+}
+
+int main()
+{
+ flag = 0;
+ int v = flag;
+ __CPROVER_assert(v == 0, "the handler may run before the read");
+ return 0;
+}
diff --git a/regression/goto-instrument/isr-havoc-reachable/test.desc b/regression/goto-instrument/isr-havoc-reachable/test.desc
new file mode 100644
index 000000000000..0d767be9683f
--- /dev/null
+++ b/regression/goto-instrument/isr-havoc-reachable/test.desc
@@ -0,0 +1,12 @@
+CORE
+main.c
+--isr isr
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.assertion\.1\] line 16 the handler may run before the read: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+The inline assembly in the interrupt handler may write flag (it has a "memory"
+clobber), so the handler may run before main reads flag.
diff --git a/regression/goto-instrument/race-check-havoc-reachable/main.c b/regression/goto-instrument/race-check-havoc-reachable/main.c
new file mode 100644
index 000000000000..ec84e5e66a5c
--- /dev/null
+++ b/regression/goto-instrument/race-check-havoc-reachable/main.c
@@ -0,0 +1,18 @@
+int g;
+
+static void thread(void)
+{
+#ifdef __GNUC__
+ asm volatile("unmodeled_instruction" ::: "memory");
+#else
+ __asm { unmodeled_instruction }
+#endif
+}
+
+int main()
+{
+__CPROVER_ASYNC_1:
+ thread();
+ g = 1;
+ return 0;
+}
diff --git a/regression/goto-instrument/race-check-havoc-reachable/test.desc b/regression/goto-instrument/race-check-havoc-reachable/test.desc
new file mode 100644
index 000000000000..84aeb70bda7f
--- /dev/null
+++ b/regression/goto-instrument/race-check-havoc-reachable/test.desc
@@ -0,0 +1,14 @@
+CORE
+main.c
+--race-check
+^EXIT=10$
+^SIGNAL=0$
+^\[main\.1\] line 16 W/W data race on g: FAILURE$
+^\[thread\.1\] line 6 W/W data race on g: FAILURE$
+^VERIFICATION FAILED$
+--
+^warning: ignoring
+--
+The inline assembly in the thread may write g (it has a "memory" clobber), as
+may main: the race check must consider the objects that the havoc_reachable
+statement changes.
diff --git a/regression/goto-instrument/show-value-sets-havoc-reachable-offset/main.c b/regression/goto-instrument/show-value-sets-havoc-reachable-offset/main.c
new file mode 100644
index 000000000000..28bbc134f5b1
--- /dev/null
+++ b/regression/goto-instrument/show-value-sets-havoc-reachable-offset/main.c
@@ -0,0 +1,15 @@
+int a[2];
+int *pa = a;
+int x;
+int *px = &x;
+
+int main()
+{
+#ifdef __GNUC__
+ asm volatile("unmodeled_instruction" ::: "memory");
+#else
+ __asm { unmodeled_instruction }
+#endif
+ int *r = pa;
+ return 0;
+}
diff --git a/regression/goto-instrument/show-value-sets-havoc-reachable-offset/test.desc b/regression/goto-instrument/show-value-sets-havoc-reachable-offset/test.desc
new file mode 100644
index 000000000000..5a1a8afedee1
--- /dev/null
+++ b/regression/goto-instrument/show-value-sets-havoc-reachable-offset/test.desc
@@ -0,0 +1,15 @@
+CORE
+main.c
+--show-value-sets
+activate-multi-line-match
+^EXIT=0$
+^SIGNAL=0$
+\npa = \{[^}]*
+\npx = \{[^}]*
+--
+^warning: ignoring
+\npx = \{[^}]*
+--
+The inline assembly may store in pa and px a pointer to any position within a
+(an array larger than the pointers' target type), so the offset becomes
+unknown. Only one int fits in x, so pointers to x keep offset 0.
diff --git a/regression/goto-instrument/show-value-sets-havoc-reachable-unknown/main.c b/regression/goto-instrument/show-value-sets-havoc-reachable-unknown/main.c
new file mode 100644
index 000000000000..d18fb2306524
--- /dev/null
+++ b/regression/goto-instrument/show-value-sets-havoc-reachable-unknown/main.c
@@ -0,0 +1,92 @@
+// More than 64 reachable objects of a compatible type: the value-set
+// analyses give a pointer that the inline assembly may change an unknown
+// target instead of listing all of them.
+int g00;
+int g01;
+int g02;
+int g03;
+int g04;
+int g05;
+int g06;
+int g07;
+int g08;
+int g09;
+int g10;
+int g11;
+int g12;
+int g13;
+int g14;
+int g15;
+int g16;
+int g17;
+int g18;
+int g19;
+int g20;
+int g21;
+int g22;
+int g23;
+int g24;
+int g25;
+int g26;
+int g27;
+int g28;
+int g29;
+int g30;
+int g31;
+int g32;
+int g33;
+int g34;
+int g35;
+int g36;
+int g37;
+int g38;
+int g39;
+int g40;
+int g41;
+int g42;
+int g43;
+int g44;
+int g45;
+int g46;
+int g47;
+int g48;
+int g49;
+int g50;
+int g51;
+int g52;
+int g53;
+int g54;
+int g55;
+int g56;
+int g57;
+int g58;
+int g59;
+int g60;
+int g61;
+int g62;
+int g63;
+int g64;
+int g65;
+int g66;
+int g67;
+int g68;
+int g69;
+int *p = &g00;
+
+struct S
+{
+ int x;
+} s1, s2;
+struct S *sp = &s1;
+
+int main()
+{
+#ifdef __GNUC__
+ asm volatile("unmodeled_instruction" ::: "memory");
+#else
+ __asm { unmodeled_instruction }
+#endif
+ int *r = p;
+ struct S *t = sp;
+ return 0;
+}
diff --git a/regression/goto-instrument/show-value-sets-havoc-reachable-unknown/test.desc b/regression/goto-instrument/show-value-sets-havoc-reachable-unknown/test.desc
new file mode 100644
index 000000000000..d41c40abee95
--- /dev/null
+++ b/regression/goto-instrument/show-value-sets-havoc-reachable-unknown/test.desc
@@ -0,0 +1,16 @@
+CORE
+main.c
+--show-value-sets
+activate-multi-line-match
+^EXIT=0$
+^SIGNAL=0$
+\nmain::1::r = \{ , unknown \}
+\nmain::1::t = \{ , ,\s* \}
+--
+^warning: ignoring
+\nmain::1::r = \{[^}]*
+\nq = \{[^}]*
+--
+^warning: ignoring
+--
+q is null until the inline assembly, which may store in q the address of any
+object reachable from the objects of static lifetime (x, through p) or whose
+address is stored in a variable (y, through local_p).
diff --git a/regression/validate-trace-xml-schema/check.py b/regression/validate-trace-xml-schema/check.py
index 674dec2e91c9..5e55936819ae 100644
--- a/regression/validate-trace-xml-schema/check.py
+++ b/regression/validate-trace-xml-schema/check.py
@@ -25,11 +25,17 @@
['xml-interface1', 'test.desc'],
['xml-interface1', 'test_wrong_flag.desc'],
# these want --show-goto-functions instead of producing a trace
+ ['asm_arm_system', 'fences.desc'],
['declaration-goto', 'no-duplicate-decl.desc'],
['destructors', 'compound_literal.desc'],
['destructors', 'enter_lexical_block.desc'],
['enum_is_in_range', 'enum_test3-simplified.desc'],
['enum_is_in_range', 'format.desc'],
+ ['inline_asm_hash', 'arm.desc'],
+ ['inline_asm_hash', 'barriers.desc'],
+ ['inline_asm_hash', 'ppc.desc'],
+ ['inline_asm_hash', 'x86.desc'],
+ ['inline_asm_modeling', 'test.desc'],
['integer-assignments1', 'integer-typecheck.desc'],
['r_w_ok9', 'simplify.desc'],
['rational1', 'typecheck.desc'],
diff --git a/src/analyses/constant_propagator.cpp b/src/analyses/constant_propagator.cpp
index e0ffc9e87f27..2b87a5422e88 100644
--- a/src/analyses/constant_propagator.cpp
+++ b/src/analyses/constant_propagator.cpp
@@ -191,6 +191,19 @@ void constant_propagator_domaint::transform(
{
values.set_to_top(from->dead_symbol());
}
+ else if(
+ from->is_other() &&
+ (from->get_other().get_statement() == ID_havoc_object ||
+ from->get_other().get_statement() == ID_havoc_reachable))
+ {
+ // the objects that the pointer points to, or that are reachable from
+ // pointers, may be any of the objects of static lifetime and the
+ // address-taken variables, as the domain does not track pointers
+ if(have_dirty)
+ values.set_dirty_to_top(cp->dirty, ns);
+ else
+ values.set_to_top();
+ }
else if(from->is_function_call())
{
const exprt &function = from->call_function();
diff --git a/src/analyses/dependence_graph.cpp b/src/analyses/dependence_graph.cpp
index efb7c6ae8fca..0c22ae8cc585 100644
--- a/src/analyses/dependence_graph.cpp
+++ b/src/analyses/dependence_graph.cpp
@@ -165,6 +165,7 @@ void dep_graph_domaint::data_dependencies(
value_setst &value_sets=
dep_graph.reaching_definitions().get_value_sets();
rw_range_set_value_sett rw_set(ns, value_sets, message_handler);
+ rw_set.set_dirty(dep_graph.reaching_definitions().get_is_dirty());
goto_rw(function_to, to, rw_set);
for(const auto &read_object_entry : rw_set.get_r_set())
diff --git a/src/analyses/dirty.cpp b/src/analyses/dirty.cpp
index 941d6b79622a..7c425615c590 100644
--- a/src/analyses/dirty.cpp
+++ b/src/analyses/dirty.cpp
@@ -42,7 +42,8 @@ void dirtyt::search_other(const goto_programt::instructiont &instruction)
statement == ID_expression || statement == ID_array_set ||
statement == ID_array_equal || statement == ID_array_copy ||
statement == ID_array_replace || statement == ID_havoc_object ||
- statement == ID_input || statement == ID_output)
+ statement == ID_havoc_reachable || statement == ID_input ||
+ statement == ID_output)
{
for(const auto &op : code.operands())
find_dirty(op);
diff --git a/src/analyses/goto_rw.cpp b/src/analyses/goto_rw.cpp
index 1416236f84a7..01844bb6d3a3 100644
--- a/src/analyses/goto_rw.cpp
+++ b/src/analyses/goto_rw.cpp
@@ -19,12 +19,17 @@ Date: April 2010
#include
#include
#include
+#include
#include
#include
#include
+#include
+#include "dirty.h"
+
+#include
#include
range_domain_baset::~range_domain_baset()
@@ -673,6 +678,78 @@ void rw_range_sett::get_array_objects(
mode, ode.root_object(), range_spect::unknown(), range_spect::unknown());
}
+void rw_range_sett::get_havoc_reachable_objects(
+ const irep_idt &function,
+ goto_programt::const_targett target,
+ const codet &code)
+{
+ for(const auto &op : code.operands())
+ {
+ get_objects_rec(function, target, get_modet::READ, op);
+ get_array_objects(function, target, get_modet::LHS_W, op);
+ }
+
+ if(code.get_bool(ID_arbitrary_memory))
+ {
+ for(const auto &object : static_lifetime_objects(ns.get_symbol_table(), ns))
+ {
+ get_objects_rec(function, target, get_modet::LHS_W, object);
+ }
+ }
+}
+
+void rw_range_set_value_sett::get_havoc_reachable_objects(
+ const irep_idt &_function,
+ goto_programt::const_targett _target,
+ const codet &code)
+{
+ function = _function;
+ target = _target;
+
+ std::vector roots;
+ if(code.get_bool(ID_arbitrary_memory))
+ {
+ for(const auto &object : static_lifetime_objects(ns.get_symbol_table(), ns))
+ {
+ roots.push_back(object);
+ }
+ if(dirty != nullptr)
+ {
+ std::vector locals;
+ for(const auto &identifier : dirty->get_dirty_ids())
+ {
+ const symbolt *symbol = nullptr;
+ if(!ns.lookup(identifier, symbol) && !symbol->is_static_lifetime)
+ locals.push_back(identifier);
+ }
+ std::sort(
+ locals.begin(),
+ locals.end(),
+ [](const irep_idt &a, const irep_idt &b)
+ { return id2string(a) < id2string(b); });
+ for(const auto &identifier : locals)
+ roots.push_back(ns.lookup(identifier).symbol_expr());
+ }
+ }
+
+ const reachable_objectst reachable = find_reachable_objects(
+ code.operands(),
+ roots,
+ [this](const exprt &pointer)
+ { return value_sets.get_values(function, target, pointer); },
+ ns);
+
+ for(const auto &op : code.operands())
+ get_objects_rec(get_modet::READ, op);
+ for(const auto &pointer : reachable.pointers)
+ get_objects_rec(get_modet::READ, pointer);
+ for(const auto &object : reachable.objects)
+ {
+ if(object.id() == ID_symbol)
+ get_objects_rec(get_modet::LHS_W, object);
+ }
+}
+
void rw_range_set_value_sett::get_objects_dereference(
get_modet mode,
const dereference_exprt &deref,
@@ -840,6 +917,10 @@ static void goto_rw_other(
rw_set.get_array_objects(
function, target, rw_range_sett::get_modet::LHS_W, code.op0());
}
+ else if(statement == ID_havoc_reachable)
+ {
+ rw_set.get_havoc_reachable_objects(function, target, code);
+ }
}
static void goto_rw(
diff --git a/src/analyses/goto_rw.h b/src/analyses/goto_rw.h
index 35bd191771bc..619481b8743e 100644
--- a/src/analyses/goto_rw.h
+++ b/src/analyses/goto_rw.h
@@ -257,6 +257,18 @@ class rw_range_sett
get_modet,
const exprt &);
+ /// Adds the objects that the `havoc_reachable` statement \p code reads and
+ /// writes. Without a pointer analysis, these are the operands (read), the
+ /// objects they point to (written), and, if \p code has the
+ /// `arbitrary_memory` flag, the objects of static lifetime (written).
+ /// \param function: Function containing \p target
+ /// \param target: The instruction \p code belongs to
+ /// \param code: The `havoc_reachable` statement
+ virtual void get_havoc_reachable_objects(
+ const irep_idt &function,
+ goto_programt::const_targett target,
+ const codet &code);
+
void output(std::ostream &out) const;
protected:
@@ -362,6 +374,7 @@ inline std::ostream &operator << (
return out;
}
+class dirtyt;
class value_setst;
class rw_range_set_value_sett:public rw_range_sett
@@ -410,8 +423,30 @@ class rw_range_set_value_sett:public rw_range_sett
rw_range_sett::get_array_objects(_function, _target, mode, pointer);
}
+ /// Adds the objects that the `havoc_reachable` statement \p code reads and
+ /// writes, using the value sets: the operands and the pointers stored in
+ /// reachable objects are read, and all reachable objects that are symbols
+ /// are written. With the `arbitrary_memory` flag, the objects of static
+ /// lifetime and, if set via \ref set_dirty, the address-taken local
+ /// variables of all functions are additional roots.
+ /// \param function: Function containing \p target
+ /// \param target: The instruction \p code belongs to
+ /// \param code: The `havoc_reachable` statement
+ void get_havoc_reachable_objects(
+ const irep_idt &function,
+ goto_programt::const_targett target,
+ const codet &code) override;
+
+ /// Sets the address-taken local variables that `havoc_reachable` with the
+ /// `arbitrary_memory` flag may change.
+ void set_dirty(const dirtyt &_dirty)
+ {
+ dirty = &_dirty;
+ }
+
protected:
value_setst &value_sets;
+ const dirtyt *dirty = nullptr;
irep_idt function;
goto_programt::const_targett target;
diff --git a/src/analyses/interval_domain.cpp b/src/analyses/interval_domain.cpp
index dcc22a1ac34d..4a95d1b18960 100644
--- a/src/analyses/interval_domain.cpp
+++ b/src/analyses/interval_domain.cpp
@@ -127,6 +127,13 @@ void interval_domaint::transform(
case SKIP: // No action required
break;
case OTHER:
+ // the objects that the pointer points to, or that are reachable from
+ // pointers, may be any of the variables, as the domain does not track
+ // pointers
+ if(
+ instruction.get_other().get_statement() == ID_havoc_object ||
+ instruction.get_other().get_statement() == ID_havoc_reachable)
+ make_top();
#if 0
DATA_INVARIANT(false, "Unclear what is a safe over-approximation of OTHER");
#endif
diff --git a/src/analyses/invariant_set.cpp b/src/analyses/invariant_set.cpp
index 26eca1aef504..aa4d33899120 100644
--- a/src/analyses/invariant_set.cpp
+++ b/src/analyses/invariant_set.cpp
@@ -1081,6 +1081,12 @@ void invariant_sett::apply_code(const codet &code)
// may be a disaster
make_true();
}
+ else if(statement == ID_havoc_object || statement == ID_havoc_reachable)
+ {
+ // the objects that the pointer points to, or that are reachable from
+ // pointers, may be any of the objects
+ make_true();
+ }
else if(statement==ID_cpp_delete ||
statement==ID_cpp_delete_array)
{
diff --git a/src/analyses/reaching_definitions.cpp b/src/analyses/reaching_definitions.cpp
index c5d4adeb2097..2c3a4ffaf2e5 100644
--- a/src/analyses/reaching_definitions.cpp
+++ b/src/analyses/reaching_definitions.cpp
@@ -310,6 +310,7 @@ void rd_range_domaint::transform_assign(
reaching_definitions_analysist &rd)
{
rw_range_set_value_sett rw_set(ns, rd.get_value_sets(), message_handler);
+ rw_set.set_dirty(rd.get_is_dirty());
goto_rw(function_to, to, rw_set);
const bool is_must_alias=rw_set.get_w_set().size()==1;
diff --git a/src/analyses/variable-sensitivity/abstract_environment.cpp b/src/analyses/variable-sensitivity/abstract_environment.cpp
index c5fa3a3f6fde..36c605b4e509 100644
--- a/src/analyses/variable-sensitivity/abstract_environment.cpp
+++ b/src/analyses/variable-sensitivity/abstract_environment.cpp
@@ -366,9 +366,15 @@ bool abstract_environmentt::merge(
if(env.bottom)
return false;
+ bool modified = false;
+ if(env.havocked && !havocked)
+ {
+ havocked = true;
+ modified = true;
+ }
+
// For each element in the intersection of map and env.map merge
// If the result of the merge is top, remove from the map
- bool modified = false;
for(const auto &entry : env.map.get_delta_view(map))
{
auto merge_result = abstract_objectt::merge(
@@ -384,7 +390,20 @@ bool abstract_environmentt::merge(
void abstract_environmentt::havoc(const std::string &havoc_string)
{
// TODO(tkiley): error reporting
- make_top();
+ // Each value becomes top. The values are replaced rather than removed (as
+ // make_top does), as merge only merges the values of the variables that
+ // both environments have: a variable removed here would keep the value it
+ // has in an environment that this one is merged with.
+ bottom = false;
+ std::vector> tops;
+ for(const auto &entry : map.get_view())
+ {
+ if(!entry.second->is_top())
+ tops.emplace_back(entry.first, entry.second->make_top());
+ }
+ for(auto &top : tops)
+ map.replace(top.first, std::move(top.second));
+ havocked = true;
}
void abstract_environmentt::make_top()
@@ -398,6 +417,7 @@ void abstract_environmentt::make_bottom()
{
map.clear();
bottom = true;
+ havocked = false;
}
bool abstract_environmentt::is_bottom() const
diff --git a/src/analyses/variable-sensitivity/abstract_environment.h b/src/analyses/variable-sensitivity/abstract_environment.h
index f1785e917df4..d0d0e6861c5e 100644
--- a/src/analyses/variable-sensitivity/abstract_environment.h
+++ b/src/analyses/variable-sensitivity/abstract_environment.h
@@ -201,6 +201,22 @@ class abstract_environmentt
/// Set the domain to top (i.e. everything)
void make_top();
+ /// Gets whether \ref havoc was applied since the last call of
+ /// \ref clear_havocked (at the entry of a function), on any path. Variables
+ /// that are not in the map at the entry of the function may then have
+ /// changed as well.
+ bool has_been_havocked() const
+ {
+ return havocked;
+ }
+
+ /// Forget about past applications of \ref havoc, see
+ /// \ref has_been_havocked.
+ void clear_havocked()
+ {
+ havocked = false;
+ }
+
/// Set the domain to top (i.e. no possible states / unreachable)
void make_bottom();
@@ -250,6 +266,9 @@ class abstract_environmentt
protected:
bool bottom;
+ /// See \ref has_been_havocked.
+ bool havocked = false;
+
// We may need to break out more of these cases into these
virtual abstract_object_pointert
eval_expression(const exprt &e, const namespacet &ns) const;
diff --git a/src/analyses/variable-sensitivity/variable_sensitivity_domain.cpp b/src/analyses/variable-sensitivity/variable_sensitivity_domain.cpp
index 10efb04e147f..5a684ddaec39 100644
--- a/src/analyses/variable-sensitivity/variable_sensitivity_domain.cpp
+++ b/src/analyses/variable-sensitivity/variable_sensitivity_domain.cpp
@@ -154,6 +154,14 @@ void variable_sensitivity_domaint::transform(
throw "exceptions not handled";
case OTHER:
+ // the objects that the pointer points to, or that are reachable from
+ // pointers, may be any of the variables
+ if(
+ instruction.get_other().get_statement() == ID_havoc_object ||
+ instruction.get_other().get_statement() == ID_havoc_reachable)
+ {
+ abstract_state.havoc(id2string(instruction.get_other().get_statement()));
+ }
// throw "other";
break;
@@ -355,7 +363,10 @@ void variable_sensitivity_domaint::transform_function_call(
}
else
{
- // we have an actual call
+ // we have an actual call: record whether the callee havocs anything
+ // (see merge_three_way_function_return)
+ abstract_state.clear_havocked();
+
const symbolt &symbol = ns.lookup(function_id);
const code_typet &code_type = to_code_type(symbol.type);
const code_typet::parameterst &declaration_parameters =
@@ -427,6 +438,11 @@ void variable_sensitivity_domaint::merge_three_way_function_return(
const variable_sensitivity_domaint &cast_function_end =
static_cast(function_end);
+ // The function may have changed any variable, including those that are
+ // not in the map at its entry (and thus not among the modified symbols).
+ if(cast_function_end.abstract_state.has_been_havocked())
+ abstract_state.havoc("havoc in a called function");
+
const std::vector &modified_symbol_names =
abstract_environmentt::modified_symbols(
cast_function_start.abstract_state, cast_function_end.abstract_state);
diff --git a/src/ansi-c/c_typecheck_code.cpp b/src/ansi-c/c_typecheck_code.cpp
index 1736855a7041..f94b875ccfca 100644
--- a/src/ansi-c/c_typecheck_code.cpp
+++ b/src/ansi-c/c_typecheck_code.cpp
@@ -172,6 +172,11 @@ void c_typecheck_baset::typecheck_asm(code_asmt &code)
auto &code_asm_gcc = to_code_asm_gcc(code);
+ // the dialect of the template (as selected by -masm), which remove_asm
+ // needs when it processes the statement, possibly in another tool
+ if(!config.ansi_c.asm_dialect.empty())
+ code_asm_gcc.set(ID_asm_dialect, config.ansi_c.asm_dialect);
+
typecheck_expr(code_asm_gcc.asm_text());
// the operands are lists of expressions
diff --git a/src/ansi-c/expr2c.cpp b/src/ansi-c/expr2c.cpp
index 6305d6f6b005..ea155d24e271 100644
--- a/src/ansi-c/expr2c.cpp
+++ b/src/ansi-c/expr2c.cpp
@@ -2590,9 +2590,13 @@ std::string expr2ct::convert_code_asm(
if(src.get_flavor()==ID_gcc)
{
- if(src.operands().size()==5)
+ // The optional sixth operand, the variable that selects the label an asm
+ // goto statement jumps to, is internal to CBMC: in C, the statement jumps
+ // to the label directly.
+ if(src.operands().size() == 5 || src.operands().size() == 6)
{
- dest+="asm(";
+ const exprt &labels = src.operands()[4];
+ dest += labels.operands().empty() ? "asm(" : "asm goto(";
dest+=convert(src.op0());
if(!src.operands()[1].operands().empty() ||
!src.operands()[2].operands().empty() ||
@@ -2642,6 +2646,20 @@ std::string expr2ct::convert_code_asm(
else
dest+=convert(*it);
}
+
+ // labels (asm goto)
+ if(!labels.operands().empty())
+ {
+ dest += " : ";
+ bool first = true;
+ for(const auto &label : labels.operands())
+ {
+ if(!first)
+ dest += ", ";
+ first = false;
+ dest += clean_identifier(label.get(ID_identifier));
+ }
+ }
}
dest+=");";
}
diff --git a/src/ansi-c/goto-conversion/goto_asm.cpp b/src/ansi-c/goto-conversion/goto_asm.cpp
index 9b85ff0cb7f0..ded1708d9d05 100644
--- a/src/ansi-c/goto-conversion/goto_asm.cpp
+++ b/src/ansi-c/goto-conversion/goto_asm.cpp
@@ -11,8 +11,103 @@ Author: Daniel Kroening, kroening@kroening.com
#include "goto_convert_class.h"
-void goto_convertt::convert_asm(const code_asmt &code, goto_programt &dest)
+#include
+#include
+#include
+
+#include "destructor.h"
+
+#include
+
+void goto_convertt::convert_asm(
+ const code_asmt &code,
+ goto_programt &dest,
+ const irep_idt &mode)
{
- // copy as OTHER
- copy(code, OTHER, dest);
+ if(code.get_flavor() != ID_gcc || code.operands().size() != 5)
+ {
+ // copy as OTHER
+ copy(code, OTHER, dest);
+ return;
+ }
+
+ // The operands of a gcc-style statement are evaluated before the statement
+ // executes; side effects in them (function calls, assignments, statement
+ // expressions) need to be made explicit so that the lowering of the
+ // statement only sees side-effect free expressions.
+ code_asm_gcct new_code = to_code_asm_gcc(code);
+ clean_expr_resultt side_effects;
+ for(auto *list : {&new_code.outputs(), &new_code.inputs()})
+ {
+ for(auto &operand : list->operands())
+ {
+ if(operand.operands().size() != 2)
+ continue;
+ exprt &expr = to_binary_expr(operand).op1();
+ if(needs_cleaning(expr))
+ side_effects.add(clean_expr(expr, mode));
+ }
+ }
+ dest.destructive_append(side_effects.side_effects);
+
+ // asm goto: the translation of the statement (see remove_asm) records the
+ // label it jumps to in a variable, which convert_asm_goto_dispatch then
+ // dispatches on
+ std::vector labels;
+ for(const auto &label : new_code.labels().operands())
+ labels.push_back(label.get(ID_identifier));
+
+ std::optional goto_target;
+ if(!labels.empty())
+ {
+ goto_target = new_tmp_symbol(
+ unsigned_int_type(),
+ "asm_goto_target",
+ dest,
+ code.source_location(),
+ mode)
+ .symbol_expr();
+ new_code.add_goto_target(*goto_target);
+ }
+
+ copy(new_code, OTHER, dest);
+
+ // the operands are no longer needed once the statement has executed
+ destruct_locals(side_effects.temporaries, dest, ns);
+
+ if(!goto_target.has_value())
+ return;
+
+ convert_asm_goto_dispatch(
+ *goto_target, labels, code.source_location(), dest, mode);
+}
+
+void goto_convertt::convert_asm_goto_dispatch(
+ const symbol_exprt &goto_target,
+ const std::vector &labels,
+ const source_locationt &source_location,
+ goto_programt &dest,
+ const irep_idt &mode)
+{
+ // goto_target == i + 1 selects labels[i], and 0 means that the statement
+ // does not jump (see the documentation of this function)
+ code_deadt dead{goto_target};
+ dead.add_source_location() = source_location;
+ for(std::size_t i = 0; i < labels.size(); ++i)
+ {
+ code_gotot code_goto{labels[i]};
+ code_goto.add_source_location() = source_location;
+
+ code_blockt then_case{{dead, std::move(code_goto)}};
+ then_case.add_source_location() = source_location;
+
+ code_ifthenelset if_goto{
+ equal_exprt{goto_target, from_integer(i + 1, goto_target.type())},
+ std::move(then_case)};
+ if_goto.add_source_location() = source_location;
+
+ convert(if_goto, dest, mode);
+ }
+
+ dest.add(goto_programt::make_dead(goto_target, source_location));
}
diff --git a/src/ansi-c/goto-conversion/goto_convert.cpp b/src/ansi-c/goto-conversion/goto_convert.cpp
index 036358751981..b934edefa568 100644
--- a/src/ansi-c/goto-conversion/goto_convert.cpp
+++ b/src/ansi-c/goto-conversion/goto_convert.cpp
@@ -694,7 +694,7 @@ void goto_convertt::convert(
else if(statement == ID_CPROVER_try_finally)
convert_CPROVER_try_finally(code, dest, mode);
else if(statement == ID_asm)
- convert_asm(to_code_asm(code), dest);
+ convert_asm(to_code_asm(code), dest, mode);
else if(statement == ID_static_assert)
{
// C23 allows static_assert without message
diff --git a/src/ansi-c/goto-conversion/goto_convert_class.h b/src/ansi-c/goto-conversion/goto_convert_class.h
index 1d2a29aca3ec..053352cb1cf1 100644
--- a/src/ansi-c/goto-conversion/goto_convert_class.h
+++ b/src/ansi-c/goto-conversion/goto_convert_class.h
@@ -357,7 +357,28 @@ class goto_convertt : public messaget
const codet &code,
goto_programt &dest,
const irep_idt &mode);
- void convert_asm(const code_asmt &code, goto_programt &dest);
+ void
+ convert_asm(const code_asmt &code, goto_programt &dest, const irep_idt &mode);
+
+ /// Appends to \p dest the jumps of an `asm goto` statement, which follow
+ /// the OTHER instruction of the statement. The translation of the statement
+ /// (see remove_asm) sets \p goto_target to the 1-based index in \p labels of
+ /// the label it jumps to, or to 0 if it does not jump (see
+ /// \ref code_asm_gcct::goto_target). For each label i, this emits an
+ /// ordinary goto to `labels[i]`, guarded by `goto_target == i + 1`, which is
+ /// converted like any other goto, so that leaving scopes is handled as
+ /// usual. \p goto_target is dead on all paths that leave the dispatch.
+ /// \param goto_target: The variable that selects the label
+ /// \param labels: The labels of the statement, in the order they are listed
+ /// \param source_location: The location of the statement
+ /// \param [out] dest: The program to append to
+ /// \param mode: The language mode
+ void convert_asm_goto_dispatch(
+ const symbol_exprt &goto_target,
+ const std::vector &labels,
+ const source_locationt &source_location,
+ goto_programt &dest,
+ const irep_idt &mode);
void convert(const codet &code, goto_programt &dest, const irep_idt &mode);
diff --git a/src/ansi-c/library/x86_assembler.c b/src/ansi-c/library/x86_assembler.c
index 9b70e185c3b6..e330aabc5a4a 100644
--- a/src/ansi-c/library/x86_assembler.c
+++ b/src/ansi-c/library/x86_assembler.c
@@ -2,21 +2,29 @@
/* FUNCTION: __asm_fnstcw */
extern int __CPROVER_rounding_mode;
+unsigned short __VERIFIER_nondet_unsigned_short(void);
void __asm_fnstcw(void *dest)
{
- // the rounding mode is bits 10 and 11 in the control word
- *(unsigned short *)dest = __CPROVER_rounding_mode << 10;
+ // The rounding mode is bits 10 and 11 in the control word. The other bits
+ // (exception masks, precision control) are not modeled.
+ unsigned int other = __VERIFIER_nondet_unsigned_short() & ~0x0c00u;
+ unsigned int rounding = (unsigned int)__CPROVER_rounding_mode << 10;
+ *(unsigned short *)dest = (unsigned short)(other | rounding);
}
/* FUNCTION: __asm_fstcw */
extern int __CPROVER_rounding_mode;
+unsigned short __VERIFIER_nondet_unsigned_short(void);
void __asm_fstcw(void *dest)
{
- // the rounding mode is bits 10 and 11 in the control word
- *(unsigned short *)dest = __CPROVER_rounding_mode << 10;
+ // The rounding mode is bits 10 and 11 in the control word. The other bits
+ // (exception masks, precision control) are not modeled.
+ unsigned int other = __VERIFIER_nondet_unsigned_short() & ~0x0c00u;
+ unsigned int rounding = (unsigned int)__CPROVER_rounding_mode << 10;
+ *(unsigned short *)dest = (unsigned short)(other | rounding);
}
/* FUNCTION: __asm_fldcw */
@@ -29,6 +37,31 @@ void __asm_fldcw(void *src)
__CPROVER_rounding_mode = ((*(const unsigned short *)src) >> 10) & 3;
}
+/* FUNCTION: __asm_stmxcsr */
+
+extern int __CPROVER_rounding_mode;
+unsigned int __VERIFIER_nondet_unsigned_int(void);
+
+void __asm_stmxcsr(void *dest)
+{
+ // The rounding mode is bits 13 and 14 in the SSE control and status
+ // register. The other bits (exception flags and masks, flush-to-zero,
+ // denormals-are-zero) are not modeled.
+ *(unsigned int *)dest = (__VERIFIER_nondet_unsigned_int() & ~0x6000u) |
+ ((unsigned int)__CPROVER_rounding_mode << 13);
+}
+
+/* FUNCTION: __asm_ldmxcsr */
+
+extern int __CPROVER_rounding_mode;
+
+void __asm_ldmxcsr(void *src)
+{
+ // the rounding mode is bits 13 and 14 in the SSE control and status
+ // register
+ __CPROVER_rounding_mode = ((*(const unsigned int *)src) >> 13) & 3;
+}
+
/* FUNCTION: __asm_mfence */
void __asm_mfence(void)
@@ -40,12 +73,16 @@ void __asm_mfence(void)
void __asm_sfence(void)
{
- __CPROVER_fence("WWfence", "RRfence", "RWfence", "WRfence");
+ // orders stores before later stores only; stores may still be delayed
+ // beyond later loads
+ __CPROVER_fence("WWfence");
}
/* FUNCTION: __asm_lfence */
void __asm_lfence(void)
{
- __CPROVER_fence("WWfence", "RRfence", "RWfence", "WRfence");
+ // orders loads before later loads and stores only; stores may still be
+ // delayed beyond later loads
+ __CPROVER_fence("RRfence", "RWfence");
}
diff --git a/src/ansi-c/parser.y b/src/ansi-c/parser.y
index 8b5df32cf803..2853d9e47c11 100644
--- a/src/ansi-c/parser.y
+++ b/src/ansi-c/parser.y
@@ -2495,8 +2495,12 @@ compound_statement:
}
| compound_scope '{' TOK_ASM_STRING '}'
{
+ // a block of assembly following the asm keyword, as in
+ // asm int f() { ... } (gcc -fasm-blocks, CodeWarrior): like an
+ // MSVC block, it uses Intel syntax and names C variables
$$=$2;
statement($$, ID_asm);
+ parser_stack($$).set(ID_flavor, ID_msc);
parser_stack($$).set(ID_C_end_location, parser_stack($4).source_location());
mto($$, $3);
PARSER.pop_scope();
@@ -2731,11 +2735,12 @@ gcc_asm_statement:
}
| TOK_GCC_ASM_PAREN volatile_or_goto_opt '{' TOK_ASM_STRING '}'
{
+ // asm { ... } (gcc -fasm-blocks, CodeWarrior): like an MSVC block,
+ // it uses Intel syntax and names C variables
$$=$1;
statement($$, ID_asm);
- parser_stack($$).set(ID_flavor, ID_gcc);
- parser_stack($$).operands().resize(5);
- to_multi_ary_expr(parser_stack($$)).op0()=parser_stack($4);
+ parser_stack($$).set(ID_flavor, ID_msc);
+ mto($$, $4);
}
;
@@ -2884,6 +2889,9 @@ gcc_asm_output:
{
$$=$5;
parser_stack($$).id(ID_gcc_asm_output);
+ // the symbolic name, for %[name] references
+ parser_stack($$).set(
+ ID_identifier, parser_stack($2).get(ID_C_base_name));
parser_stack($$).add_to_operands(std::move(parser_stack($4)), std::move(parser_stack($6)));
}
;
@@ -2921,6 +2929,9 @@ gcc_asm_input:
{
$$=$5;
parser_stack($$).id(ID_gcc_asm_input);
+ // the symbolic name, for %[name] references
+ parser_stack($$).set(
+ ID_identifier, parser_stack($2).get(ID_C_base_name));
parser_stack($$).add_to_operands(std::move(parser_stack($4)), std::move(parser_stack($6)));
}
;
diff --git a/src/assembler/Makefile b/src/assembler/Makefile
index 57638617bddf..b30322441b85 100644
--- a/src/assembler/Makefile
+++ b/src/assembler/Makefile
@@ -1,4 +1,12 @@
-SRC = assembler_lex.yy.cpp \
+SRC = asm_aarch64.cpp \
+ asm_approximation.cpp \
+ asm_arm.cpp \
+ asm_lowering.cpp \
+ asm_operands.cpp \
+ asm_semantics.cpp \
+ asm_template.cpp \
+ asm_x86.cpp \
+ assembler_lex.yy.cpp \
assembler_parser.cpp \
remove_asm.cpp \
# Empty last line
diff --git a/src/assembler/asm_aarch64.cpp b/src/assembler/asm_aarch64.cpp
new file mode 100644
index 000000000000..856a68ed2d6d
--- /dev/null
+++ b/src/assembler/asm_aarch64.cpp
@@ -0,0 +1,1213 @@
+/*******************************************************************\
+
+Module: Inline assembly: AArch64 instruction semantics
+
+Author: Michael Tautschnig
+
+\*******************************************************************/
+
+/// \file
+/// Instruction semantics for AArch64 (A64 instruction set, GNU syntax)
+
+#include "asm_aarch64.h"
+
+#include
+#include
+#include
+#include
+
+#include "asm_arm_flags.h"
+#include "asm_bv_util.h"
+#include "asm_semantics.h"
+
+#include
+#include
+#include