Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
4d1034a
libutils: consolidated libfsigc++, libcoroutine and libq
vitalych Aug 17, 2026
85b813e
libtcg: added tooling to upgrade from qemu releases
vitalych Aug 18, 2026
8874972
libtcg: upgraded to qemu 11.1
vitalych Aug 18, 2026
cb8652c
libcpu: adjusted for the qemu 11.1 tcg api
vitalych Aug 18, 2026
7294e3e
libtcg: upgraded softfloat to qemu 11.1
vitalych Aug 18, 2026
bae2b05
libcpu: initialized the x86 float_status behaviour rules
vitalych Aug 18, 2026
94058db
skills: added a skill to upgrade libtcg
vitalych Aug 18, 2026
274db63
libtcg: upgraded utils to qemu 11.1
vitalych Aug 18, 2026
5b059a5
skills: recorded the utils deviations in the libtcg upgrade skill
vitalych Aug 18, 2026
097a463
libtcg: reclassified memalign.h as hand written
vitalych Aug 18, 2026
c272035
Avoid raw_string_ostream::str() and redundant stringstream copies
vitalych Aug 19, 2026
5b698e2
klee: stripped the aliasing hints that broke module preparation
vitalych Aug 19, 2026
4039791
klee: handled the fp intrinsics of the hard float fast paths
vitalych Aug 19, 2026
90dfd80
libcpu: added porting plan
vitalych Aug 20, 2026
ce76597
claude.md: updated writing style
vitalych Aug 20, 2026
3f16770
libutils: relocated libtcg's platform-independent utils
vitalych Aug 21, 2026
21540f0
libutils: derived the host config macros from one header
vitalych Aug 21, 2026
b3f127f
build: stopped libutils edits from reconfiguring libs2e and tools
vitalych Aug 21, 2026
98a96bd
scripts: shared the qemu upgrade driver between libtcg and libutils
vitalych Aug 21, 2026
6135a76
libutils: vendored the remaining hand-written utilities from QEMU v11…
vitalych Aug 21, 2026
fc26cfa
libutils: split bswap.h into a host half and a target half
vitalych Aug 21, 2026
3a341ed
libutils: imported qht, rcu, thread, xxhash and seqlock from QEMU v11…
vitalych Aug 24, 2026
3dc5bd9
libcpu: moved the CPU caches out of CPUX86State into ArchCPU
vitalych Aug 24, 2026
8e52db4
libcpu: dropped the performance measurements from the tlb plan
vitalych Sep 3, 2026
6bbcc73
libs2ecore: promoted the tlb cache audit to a runtime flag
vitalych Sep 3, 2026
0164b5a
libs2ecore: routed every tlb method through the caches accessor
vitalych Sep 3, 2026
a369979
libs2ecore: stopped inactive states from updating the concrete fast path
vitalych Sep 3, 2026
623aa0f
libs2ecore: stopped snapshotting the CPU caches across state switches
vitalych Sep 3, 2026
a8e3290
libs2ecore: removed the tlb flush from state merging
vitalych Sep 3, 2026
13bf74b
libcpu: moved the execution fields of CPU_COMMON into CPUState
vitalych Sep 3, 2026
03e51a6
libtcg: updated memopidx.h to qemu 11.1
vitalych Oct 10, 2026
1cbc1d0
libcpu,libtcg,tools: removed the static translator
vitalych Oct 10, 2026
6419f47
libcpu: moved the breakpoint flags to cpu/common.h
vitalych Oct 10, 2026
05c4d97
libcpu: renamed cpu_mmu_index to x86_mmu_index
vitalych Oct 10, 2026
684d60e
libcpu: added the upstream names for addresses and memory regions
vitalych Oct 10, 2026
cc5da1b
libcpu,libs2ecore,libs2e: added a tlb flush hook for one mmu index
vitalych Oct 10, 2026
5140ced
libcpu: imported the softmmu from QEMU v11.1.0
vitalych Oct 10, 2026
c9a6e75
libtcg: imported tlb-common.h from QEMU v11.1.0
vitalych Oct 10, 2026
44e97a1
libcpu,libtcg: adapted the imported softmmu headers
vitalych Oct 10, 2026
898b13a
libcpu: added the S2E deviations to cputlb.c
vitalych Oct 10, 2026
9c144ea
libcpu,libtcg,libs2ecore,libs2e: replaced the softmmu with QEMU v11.1…
vitalych Oct 10, 2026
b1b71a8
libcpu,libs2ecore: removed mem_io_vaddr
vitalych Oct 10, 2026
50bacb7
libcpu: added the softmmu replacement plan
vitalych Oct 10, 2026
c291f58
libtcg: optimize the llvm code of translation blocks
vitalych Oct 17, 2026
154de52
libcpu,libtcg,libs2ecore: reduced the interpreted work of the bitcode…
vitalych Oct 17, 2026
cdc1da9
libtcg,libutils: disabled inline assembly in the softfloat bitcode
vitalych Oct 18, 2026
52fb17c
libcpu: fail the build if the helper bitcode contains inline assembly
vitalych Oct 18, 2026
0193c41
klee: print the asm string and function on unsupported inline assembly
vitalych Oct 18, 2026
3adb4fa
libs2e: pass the interrupt window request to the cpu on every kvm_run
vitalych Oct 18, 2026
35f1c4a
libs2ecore: gave print_stacktrace c linkage
vitalych Oct 18, 2026
9993bd6
tools: updated the gdb tlb commands to the new tlb layout
vitalych Oct 18, 2026
13db525
libcpu: updated the porting plans for the landed softmmu
vitalych Oct 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
229 changes: 229 additions & 0 deletions .claude/skills/upgrade-libtcg/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
---
name: upgrade-libtcg
description: Upgrade libtcg and libutils' vendored QEMU code (TCG core, softfloat, host utilities) to a newer QEMU release. Use when asked to move libtcg to a new QEMU version, pull in upstream TCG changes, rebase the TCG fork, or when a QEMU tag is named alongside libtcg/tcg/softfloat/libutils. Covers the normalize-then-merge workflow, the S2E deviations that must survive, and the libcpu fallout.
---

# Upgrading libtcg and libutils to a new QEMU release

Two components carry vendored QEMU code and are upgraded together:

- **libtcg**: a de-QEMU-ified fork of QEMU's TCG (headers moved out of `qemu/`,
`exec/` and `accel/tcg/` into `tcg/utils/`, `tcg/exec/` and `tcg/accel/`),
plus `fpu/` and the target-dependent `utils/{bswap.h,cpu-float.h,log.*}`.
- **libutils**: QEMU's platform-independent utilities under `include/utils/`
and `src/utils/` (upstream `util/`, `include/qemu/`, `host/include/`).

Both trees are reformatted with the S2E `.clang-format`. **`git cherry-pick`
and `patch` never apply**: every line differs because of reformatting alone.

The shared driver lives in `scripts/upgrade/` at the S2E source root; each
component keeps `scripts/upgrade/{BASELINE,mapping.tsv}`. Read
`scripts/upgrade/README.md` first.

## The core idea

Run the upstream file through clang-format with the S2E style *before*
comparing. That collapses the diff to the genuine S2E delta (`libtcg/src/tcg.c`
goes from ~6800 differing lines to ~79), which makes the upgrade an ordinary
three-way merge:

base = clang-formatted upstream at the OLD tag
theirs = clang-formatted upstream at the NEW tag
ours = the current S2E file

Expect most files to merge clean, including the big ones (`tcg.c`, `tcg-op.c`,
`optimize.c`, `tcg-target.c.inc`). Conflicts are almost always one hunk in an
include block or where S2E's `extern "C"` guard meets appended upstream code.

## Steps

### 1. Scope it

cat libtcg/scripts/upgrade/BASELINE libutils/scripts/upgrade/BASELINE
git -C ~/qemu log --oneline <old>..<new> -- tcg/ include/tcg/ util/ include/qemu/ host/include/

**Neither component is uniformly at one version.** The TCG core, `fpu/` and the
vendored utilities track `BASELINE`, but libtcg's hand-written subset (`log.*`,
flagged below the "hand written" marker in its `mapping.tsv`) is still
v8.1.0-era and is not vendored. libutils has no hand-written subsets left, only
pruned copies (`cutils.h`, `osdep.h`, `bswap.h`, `cutils.c`, `osdep.c`,
`oslib-posix.c`: upstream text with sections deleted), which merge like any
other row. `bswap.h` is split between the two components (host half in
libutils, `MemOp`/target-endian half in libtcg), so each side's merge reports
the other half as a conflict in a deleted section. Before assuming a file's
baseline, check it: format the file at several candidate tags and pick the
smallest diff. A file that is stale relative to its neighbors is a bug waiting
to happen: the 11.1 upgrade found `tcg-gvec-desc.h` stranded at an old
descriptor encoding while `simd_desc()` already wrote the new one.

Note deleted, added and renamed files in the upstream range; `merge.sh` reports
them but cannot decide what to do about them.

### 2. Merge, libutils first

cd scripts/upgrade
QEMU_SRC=~/qemu ./merge.sh libutils <old-tag> <new-tag> # dry run
QEMU_SRC=~/qemu ./merge.sh libutils <old-tag> <new-tag> --apply # write back
QEMU_SRC=~/qemu ./merge.sh libtcg <old-tag> <new-tag>
QEMU_SRC=~/qemu ./merge.sh libtcg <old-tag> <new-tag> --apply

libutils goes first because libtcg's merged code may call utility APIs that
only arrive with the libutils merge. Prefer applying a chosen subset by hand
over `--apply` when the upgrade is scoped to part of the tree (e.g. TCG core
but not `fpu/`). Merged output sits in `scripts/upgrade/out/<component>/merged/`.
`--apply` writes conflicted files with markers and lists them; resolve them
before building.

Resolve conflicts by keeping the S2E side of include blocks (the `<tcg/...>` and
`<utils/...>` paths and `// clang-format off` markers), and keeping the upstream
side of everything else, with the `extern "C"` guard moved to wrap the new code.

### 3. Import new files by hand

Files upstream added have no S2E name to merge into. Normalize, then rewrite the
includes to S2E paths and add `extern "C"` guards:

QEMU_SRC=~/qemu ./normalize.sh <new-tag> include/tcg/foo.h > ../../libtcg/include/tcg/foo.h
QEMU_SRC=~/qemu ./normalize.sh <new-tag> include/qemu/bar.h > ../../libutils/include/utils/bar.h

Place by dependency, not by origin: anything that reads no `TARGET_*` or
`CONFIG_SYMBEX` macro belongs in libutils (libtcg is built six times per build
type, libutils once). Add the new row to that component's `mapping.tsv`.

**Make imported headers self-contained.** Upstream relies on `qemu/osdep.h`
being included first everywhere; S2E has no such prelude, and the S2E
clang-format sets `SortIncludes: true`, so it will reorder an include block and
break any header that depends on its position. Host configuration macros
(`CONFIG_INT128`, `CONFIG_MADVISE` and the rest) come from
`utils/config-host.h`, pulled in by `utils/int128.h` and `utils/osdep.h`; never
add `-D` copies to a CMakeLists.

### 4. Build

cd ~/s2e/build/libutils-release && make -j16
cd ~/s2e/build/libs2e-release && make -j16
cd ~/s2e/build/libutils-debug && make -j16
cd ~/s2e/build/libs2e-debug && make -j16

libutils before libs2e, since libs2e links `libutils.a`. Both libs2e trees have
six configurations each: `{i386,x86_64}-{,s2e-,s2e_sp-}softmmu`. Iterate on
`libs2e-release/x86_64-s2e-softmmu/libtcg` alone for a fast loop, but finish on
the full build: the i386 (`TARGET_LONG_BITS=32`) configurations exercise
different paths.

### 5. Verify the S2E patches survived

A clean merge is not a correct merge. Diff each changed file against normalized
upstream at the new tag and confirm the residual is *only* the expected S2E
delta:

diff out/libtcg/new/src/x86_64/tcg-target.c.inc ../../libtcg/src/x86_64/tcg-target.c.inc
diff out/libutils/new/include/utils/bitops.h ../../libutils/include/utils/bitops.h

Patches that must still be there, and that upstream refactors can silently
relocate:

- `s->qemu_ld_helpers` / `s->qemu_st_helpers` (per-context, not the static table)
- the `CONFIG_SYMBEX && CONFIG_SYMBEX_MP` `se_addend` load on the TLB hit path;
check it is anchored to the right `tcg_out_ld`, not merely present
- `tcg_target_force_tb_exit`
- `tcg_prologue_init(TCGContext *s)` taking the context explicitly
- `mutex_t` / `spin_*` in `region.c` and `tb.h` (libutils keeps its own
`mutex.h`/`spinlock.h` instead of `qemu/thread.h`)

### 6. Re-normalize and record

./run-clang-format.sh # from the S2E source root
make -j16 # formatting can reorder includes; rebuild

Then update both `BASELINE` files and any renamed paths in both `mapping.tsv`,
add newly tracked files, and confirm the trees are normalized by merging the
new tag onto itself:

QEMU_SRC=~/qemu ./merge.sh libutils <new-tag> <new-tag> # must be 0 conflicts
QEMU_SRC=~/qemu ./merge.sh libtcg <new-tag> <new-tag> # must be 0 conflicts

### 7. Commit

Per `CLAUDE.md`: `component: short description`, lowercase. Split by phase:
tooling, rename, the libutils merge, the libtcg merge, then each consumer.
Explain *why* in the body, especially for anything that deviates from upstream.

## Deviations that must survive every merge

libutils:

- **`host/generic/atomic128-cas.h`** renames the `new` parameter to `_new`.
These headers are included from C++, where `new` is a keyword.
- **`utils/bitops.h`** wraps `#define BIT(nr)` in `#ifndef BIT`, because
`libcpu/include/cpu/kvm.h` also defines it.
- **`utils/compiler.h`** is vendored; it adds `<stdbool.h>`/`<stddef.h>` and
`#define typeof __typeof__` for C++. **`utils/osdep.h`** is a pruned copy of
upstream's: it redefines `MIN`/`MAX`/`ARRAY_SIZE` for C++ and drops the
`NDEBUG` `#error`.
- **`utils/int128.h` and `utils/osdep.h`** include `<utils/config-host.h>`;
`int128.h` also includes `<utils/compiler.h>`.
- **`src/utils/osdep.c`** uses `fprintf(stderr)` where upstream calls
`error_report()`.
- **`src/utils/cpuinfo-i386.c`** includes `<utils/cpuid.h>`; `CONFIG_CPUID_H`
stays undefined on purpose (see `utils/config-host.h`: the TCG op set must
not grow past what `tcg-llvm.cpp` translates).

libtcg:

- **`float_status.float_rounding_mode` is a plain field, not a bitfield.** S2E
addresses the CPU state by `(offset, size)`; `RR_cpu`/`WR_cpu` need
`offsetof`/`sizeof`, which no bitfield supports.
- **`tcg/utils/cpu-float.h`** holds the float/integer reinterpretation unions
that upstream keeps in `include/qemu/cpu-float.h`. Leaving them in `bswap.h`
creates a cycle: `softfloat-types.h` -> `registerfields.h` -> `bitops.h` ->
`host-utils.h` -> `bswap.h`.
- **`tcg/utils/target-info.h`** defines `target_big_endian()` as
`TARGET_BIG_ENDIAN` under `COMPILING_PER_TARGET`; `TARGET_BIG_ENDIAN` is
defined (0) by libcpu's generated `config-target.h` and libtcg's CMake.
- **`accel/getpc.h`** drops upstream's `#ifndef CONFIG_TCG #error` guard.
- **`exec/helper-head.h.inc`** keeps its `#ifdef TARGET_LONG_BITS` guard instead
of including `exec/target_long.h`.

## The libcpu fallout

libcpu consumes libtcg and libutils and breaks in ways their own builds will
not catch.

**Never replace `RR_cpu`/`WR_cpu` with direct struct access to silence a
compiler error.** Under `CONFIG_SYMBEX` these route through
`g_sqi.regs.read_concrete()` when `fast_concrete_invocation` is false, that is,
when the helper runs as bitcode inside KLEE. Direct access reads the native copy
of the CPU state instead of KLEE's, silently, on exactly the symbolic paths that
matter. If upstream makes a field unaddressable, change the field in libtcg.

**Check for newly required initialization.** Softfloat asserts at runtime when a
NaN propagation rule or default NaN pattern was never set. `cpu_state_reset()`
memsets the CPU state, so anything of that kind must be reapplied there; see
`cpu_init_fp_statuses()` in `libcpu/src/target-i386/helper.c`, mirroring QEMU's
`target/i386/tcg/fpu_helper.c`. This class of bug builds clean and aborts later,
so grep upstream's target code for setters the fork does not call.

**Watch for removed macros and renamed flags**, e.g. the removal of
`TARGET_INSN_START_EXTRA_WORDS` (which made `tcg_gen_insn_start` take a fixed
`INSN_START_WORDS` arguments, callers padding with zeroes), and
`float_flag_input_denormal` -> `float_flag_input_denormal_flushed`.

## Things that change behavior, not just code

Say so explicitly when reporting the upgrade:

- Softfloat changes are guest-visible, and `softfloat.c` is compiled to
`softfloat.bc` and interpreted by KLEE, so symbolic paths see them too.
- `float_status` layout changes do not break compatibility with existing S2E
snapshots. Snapshots are handled by vanilla QEMU.
- `tcg-llvm.cpp` switches over TCG opcodes; if the upstream range removes or
renames any, the LLVM translator needs updating. Check the opcode list diff:
`diff <(git show <old>:include/tcg/tcg-opc.h | grep -oE '^DEF\([a-z0-9_]+') ...`

## Testing

`testsuite/` drives real guest images through `~/s2e/projects/testsuite/*`. The
launch scripts use `BUILD=debug`, so build `libs2e-debug` before running.
`basic4-xmm` is the one most likely to notice softfloat changes. Ask before
running the suite; it is slow, and the user often has it covered.
25 changes: 22 additions & 3 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ S2E is a symbolic execution platform. This repository builds `libs2e.so`, a shar
- `libtcg/` - TCG (Tiny Code Generator) library
- `klee/` - KLEE symbolic execution engine (fork)
- `libvmi/` - Virtual machine introspection
- `libq/` - Utility library
- `libfsigc++/` - Signal/slot library
- `libcoroutine/` - Coroutine support
- `libutils/` - Utility library (QObject/QAPI, signal/slot, coroutines)
- `guest/` - Guest-side tools and libraries
- `tools/` - Offline analysis tools
- `lua/` - Lua headers (for plugin configuration)
Expand Down Expand Up @@ -47,3 +45,24 @@ Examples:
- `libs2e: implemented basic device manager`
- `libcpu: removed unused ymm fields`
- `libs2ecore: allow any sizes for read/writes from/to concrete regions`

## Comments and Documentation
- Write accurate and concise comments. Conciseness does not mean loss of important information and should not make readability worse.
- Comments are accurate first. Verify every claim a comment makes against the code it describes. A comment that promises behavior ("retried later", "cannot happen") must name the mechanism, and the mechanism must exist. Re-derive constants that carry units in their comment (a timeout "in seconds" computed in the wrong unit is a live bug the comment helps find).
- When shortening or rewording a comment, every fact in the old text is either kept or consciously discarded as wrong.
- Do not use "we" in comments. Be specific (e.g., the driver, the caller, etc.).
- Do not overuse semicolons or em-dashes.
- Use doxygen-style comments where appropriate (`///`, `@param`, `@return`). Use proper markers for parameters, etc. Every file starts (after the license header) with a `/// @file` block saying what the module does.
- Comments should be easy to read and sound natural.
- Do not use passive voice when the subject would actually convey additional information.
- In a multi-line comment, do not leave one or two words alone on the last line.
- Do not overuse the word "classic" (e.g., classic cause), "load bearing", "bearing", "seam", "wedged", "latch", "worth stating plainly", "full stop", "carry the argument", "the trap", "isn't just X — it's Y". and other LLMisms.
- Do not use adjectives and other words that bring no value (e.g., honest).
- Minimize jargon and uncommon words. Non-experts should be able to read and understand everything reasonably well.
- When introducing a new term or abbreviation for the first time, define it, especially if it is driver-specific. Common knowledge does not need to be defined (e.g., any standard driver terminology).
- Use plain English in comments and documentation. Do not coin aphorisms or metaphor slogans. A documentation heading must state its point plainly and be understandable before reading the body.
- No punchy fragments for drama. Write complete sentences.
- You are writing technical documentation, not marketing material.
- Some comments are functional and must keep their exact form: formatter on/off markers, and similar tool-consumed comments. Never restyle them.
- Comments justify designs on technical grounds. They do not narrate history, compare against previous implementations, reference external documentation, or paraphrase the code.
- Use American English.
Loading
Loading