Skip to content

goto-symex: zero-initialise heap objects without SSA steps - #9636

Draft
tautschnig wants to merge 2 commits into
diffblue:developfrom
tautschnig:kiro/symex-zero-init-without-steps
Draft

tautschnig wants to merge 2 commits into
diffblue:developfrom
tautschnig:kiro/symex-zero-init-without-steps

Conversation

@tautschnig

Copy link
Copy Markdown
Collaborator

Depends-on: #9635

An object allocated with zero initialisation (calloc,
__CPROVER_allocate(size, 1)) got one assignment step per field (field
sensitivity splits the zero value into the fields, and into the members
of unions), and before the deferral of shared writes one shared-write
event per field as well. With constant propagation, no later step refers
to these generations of the fields: every read takes the propagated zero,
and a merge of paths takes each side's propagated value (merge_names), as
it already does for the generations that goto_statet::apply_condition
makes without an assignment. The steps are only needed by the memory model
once a thread is spawned, and when constant propagation is off. A Lua
table, a zero-initialised object of several hundred fields, thus cost
about 3,900 steps (memory, and conversion time) for values that were
never read from the equation. This builds on the deferral of shared
writes until the first spawn ("goto-symex: record shared writes only once
a thread is spawned").

goto_symext::symex_zero_initialisation now makes the assignment as
symex_assign() would (same L2 generations, propagated values and value
sets), but with the steps recorded aside rather than in the equation. A
step whose value constant propagation holds for the current generation of
its left-hand side, or whose left-hand side is a struct-like aggregate
(read only through its fields), is dropped; any other step goes to the
equation as before (with its shared write). The equation records the
object, guard and position (symex_target_equationt::
deferred_initialisation). When the first thread is spawned,
add_deferred_initialisations() adds, for every field that threads can read
or write (all but struct-like aggregates, union members included), a
shared write and an assignment of zero at that position, before the
deferred shared writes recorded after it (the sequence number orders
those at the same position), so that the threads read the zeros and,
where a write followed before the spawn, that value. The steps use fresh
generations: nothing refers to the dropped ones. Without constant
propagation, and once there are threads, the zero initialisation is
assigned as before.

Not done: skipping the nondeterministic initialisation of an object
allocated without zero initialisation. Leaving its fields at generation 0
is unsound: merge_names takes generation 0 for an invalid target and the
other branch's value, so p = malloc(sizeof(int)); if(c) *p = 1; assert(*p == 1); would pass (test regression/cbmc/
malloc-uninitialised-merge guards against this). Declaring the fields
instead (generation 1, as symex_decl does) crashed under --paths lifo
when done after symex_allocate.

Effect: a C program allocating 100 calloc'd structs of 34 fields in a
loop (--unwind 101): 16,523 -> 12,923 steps, peak memory 106 -> 97 MB. A
Lua front end built on CBMC, whose tables are zero-initialised heap
objects of several hundred fields: on 46 real-world programs, the 40
that verify take 6.86 M instead of 9.55 M steps and 28.5 GB instead of
33.8 GB in all (sum of peak memory), with the same outcomes; symbolic
execution time is within the noise of the host load.
scripts/profile_cbmc.py --auto-large --runs 3 (none of its benchmarks
allocates zeroed memory): the same steps on all ten benchmarks;
pre-solver time -4.2 % to +5.6 % with the base run first and +0.2 % to
+3.4 % with it second, on a host shared with other jobs (the noise of
the run order).

Traces no longer show the zero initialisation of the fields (later
writes to them are shown as before).

Tests: regression/cbmc/zero-initialisation-merge (fields written on one
branch only, an object allocated on one branch only, union members and
pointers; with propagation, without it and with --paths lifo; and that no
step defines the fields' first generation),
regression/cbmc-concurrency/zero-initialisation-before-spawn (a thread
spawned after the allocation reads the zeros, a value written before the
spawn, and one written after it may be missed),
regression/cbmc/malloc-uninitialised-merge (see above), and a unit test
in unit/goto-symex/ssa_equation.cpp (positions and order of the added
steps). Each of the first two fails with a mutant: fields left at
generation 0 (both), no steps at the spawn (the concurrency test), steps
at the spawn's position instead of the allocation's (the concurrency
test). regression/cbmc-library/calloc/program-only_01 checked the type of
calloc's object through its zero initialisation step; it now checks it
through a write to the second element.

zero-initialisation-merge/program-only.desc fails without this change.
regression/cbmc (also with --paths lifo), cbmc-concurrency, cbmc-cpp,
cbmc-cover, cbmc-shadow-memory and cbmc-primitives pass with
--validate-goto-model --validate-ssa-equation; cbmc-library's mmap/test,
mmap/test_02 and munmap/test fail with these options, as on develop.

Co-authored-by: Kiro kiro-agent@users.noreply.github.com

  • Each commit message has a non-empty body, explaining why the change was made.
  • n/a Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • n/a The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • n/a White-space or formatting changes outside the feature-related changed lines are in commits of their own.

tautschnig and others added 2 commits December 3, 2026 17:43
Symbolic execution records a shared-write event for every write to an
object that is not thread-local, heap objects and globals included,
also in programs that never spawn a thread. The events are only used by
the memory model, which runs only if the equation has threads
(postprocess_equation, has_threads), and by traces only then; in a
single-threaded program they are steps that cost time and memory and
are never read. A Lua program writes the fields of every new table, so
that 28 % of its steps were such events (luacheck's
stages/detect_empty_blocks.lua: 147,846 of 524,168).

symex_target_equationt::shared_write now keeps the events recorded
before the first spawn aside (their guard, object, atomic section,
source and the number of steps before them), and spawn() inserts them
where they would have been before it adds the spawn step, so that the
equation of a program that spawns a thread is unchanged: the sorted
--program-only output of each of the 68 tests of
regression/cbmc-concurrency is the same as before (apart from the run
times that one of them prints). Without a spawn they
are dropped. (Simply not recording them before the first spawn would be
unsound for the spawned threads, which read the values written before
it: the new regression test cbmc-concurrency/shared-writes-before-spawn
fails then.)

The effect is mostly on steps and memory. A Lua front end built on CBMC
(luacheck's stages/parse.lua, runs alternated on a quiet host):
1,228,346 to 915,985 steps, symbolic execution 85.4 to 84.1 s (mean of
two runs), peak memory 3.17 to 3.06 GB. scripts/profile_cbmc.py
--auto-large --runs 3: steps unchanged or fewer (array_ops 16,448 to
13,993, matrix 4,148 to 3,435, string_ops 1,950 to 1,737, unions 382 to
311, structs 312 to 207); pre-solver time -14.7 % to +4.8 % with develop
run first and +0.3 % to +4.7 % with it second, on a host shared with
other jobs, i.e. within the noise of the run order.

Tests: unit test in unit/goto-symex/ssa_equation.cpp (the deferred
events and their positions); regression/cbmc-concurrency/
shared-writes-before-spawn (values written before the spawn, on both
branches and twice, read by the thread; a write after the spawn; and a
single-threaded program whose --program-only output has no shared
writes). single-thread.desc fails on develop. regression/cbmc (also with
--paths lifo), cbmc-concurrency, cbmc-cpp, cbmc-cover,
cbmc-shadow-memory and cbmc-primitives pass with --validate-goto-model
--validate-ssa-equation; cbmc-library's mmap/test, mmap/test_02 and
munmap/test fail with these options, as on develop.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
An object allocated with zero initialisation (calloc,
__CPROVER_allocate(size, 1)) got one assignment step per field (field
sensitivity splits the zero value into the fields, and into the members
of unions), and before the deferral of shared writes one shared-write
event per field as well. With constant propagation, no later step refers
to these generations of the fields: every read takes the propagated zero,
and a merge of paths takes each side's propagated value (merge_names), as
it already does for the generations that goto_statet::apply_condition
makes without an assignment. The steps are only needed by the memory model
once a thread is spawned, and when constant propagation is off. A Lua
table, a zero-initialised object of several hundred fields, thus cost
about 3,900 steps (memory, and conversion time) for values that were
never read from the equation. This builds on the deferral of shared
writes until the first spawn ("goto-symex: record shared writes only once
a thread is spawned").

goto_symext::symex_zero_initialisation now makes the assignment as
symex_assign() would (same L2 generations, propagated values and value
sets), but with the steps recorded aside rather than in the equation. A
step whose value constant propagation holds for the current generation of
its left-hand side, or whose left-hand side is a struct-like aggregate
(read only through its fields), is dropped; any other step goes to the
equation as before (with its shared write). The equation records the
object, guard and position (symex_target_equationt::
deferred_initialisation). When the first thread is spawned,
add_deferred_initialisations() adds, for every field that threads can read
or write (all but struct-like aggregates, union members included), a
shared write and an assignment of zero at that position, before the
deferred shared writes recorded after it (the sequence number orders
those at the same position), so that the threads read the zeros and,
where a write followed before the spawn, that value. The steps use fresh
generations: nothing refers to the dropped ones. Without constant
propagation, and once there are threads, the zero initialisation is
assigned as before.

Not done: skipping the nondeterministic initialisation of an object
allocated without zero initialisation. Leaving its fields at generation 0
is unsound: merge_names takes generation 0 for an invalid target and the
other branch's value, so `p = malloc(sizeof(int)); if(c) *p = 1;
assert(*p == 1);` would pass (test regression/cbmc/
malloc-uninitialised-merge guards against this). Declaring the fields
instead (generation 1, as symex_decl does) crashed under --paths lifo
when done after symex_allocate.

Effect: a C program allocating 100 calloc'd structs of 34 fields in a
loop (--unwind 101): 16,523 -> 12,923 steps, peak memory 106 -> 97 MB. A
Lua front end built on CBMC, whose tables are zero-initialised heap
objects of several hundred fields: on 46 real-world programs, the 40
that verify take 6.86 M instead of 9.55 M steps and 28.5 GB instead of
33.8 GB in all (sum of peak memory), with the same outcomes; symbolic
execution time is within the noise of the host load.
scripts/profile_cbmc.py --auto-large --runs 3 (none of its benchmarks
allocates zeroed memory): the same steps on all ten benchmarks;
pre-solver time -4.2 % to +5.6 % with the base run first and +0.2 % to
+3.4 % with it second, on a host shared with other jobs (the noise of
the run order).

Traces no longer show the zero initialisation of the fields (later
writes to them are shown as before).

Tests: regression/cbmc/zero-initialisation-merge (fields written on one
branch only, an object allocated on one branch only, union members and
pointers; with propagation, without it and with --paths lifo; and that no
step defines the fields' first generation),
regression/cbmc-concurrency/zero-initialisation-before-spawn (a thread
spawned after the allocation reads the zeros, a value written before the
spawn, and one written after it may be missed),
regression/cbmc/malloc-uninitialised-merge (see above), and a unit test
in unit/goto-symex/ssa_equation.cpp (positions and order of the added
steps). Each of the first two fails with a mutant: fields left at
generation 0 (both), no steps at the spawn (the concurrency test), steps
at the spawn's position instead of the allocation's (the concurrency
test). regression/cbmc-library/calloc/program-only_01 checked the type of
calloc's object through its zero initialisation step; it now checks it
through a write to the second element.

zero-initialisation-merge/program-only.desc fails without this change.
regression/cbmc (also with --paths lifo), cbmc-concurrency, cbmc-cpp,
cbmc-cover, cbmc-shadow-memory and cbmc-primitives pass with
--validate-goto-model --validate-ssa-equation; cbmc-library's mmap/test,
mmap/test_02 and munmap/test fail with these options, as on develop.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant