AGENTS.md — how to modify this module
Invariants, conventions and change recipes for contributors — and for AI coding assistants, which load this file automatically when editing the module. Largely derived from the source and verified against it.
Mirrored from subprojects/xcfa/c2xcfa/AGENTS.md — edit it there, not here.
xcfa/c2xcfa — notes for editing this module
Gradle module: :theta-c2xcfa. Turns the C frontend's CProgram into an XCFA. Nearly all of it is FrontendXcfaBuilder.kt, a CStatementVisitorBase<ParamPack, XcfaLocation> over the frontend's statement tree. Utils.kt has the entry point getXcfaFromC. How C objects, cells, base ids and the three memory models are represented is in README.md. Read it before touching assignments, initializers or pointers.
Build and test
./gradlew :theta-c2xcfa:test. Tests compile C snippets withgetXcfaFromC. Every procedure builder gets aCPassesmanager, so a test here exercises the frontend, this module and the C pass pipeline together.TestFrontendXcfaBuildersmoke-builds the numbered programs insrc/test/resources/; add a focused*Test.ktfor new behaviour. The usual shape is a C source string passed togetXcfaFromC(src.byteInputStream(), ParseContext(), false, XcfaProperty(ErrorDetection.ERROR_LOCATION), NullLogger.getInstance()), then assertions on the built XCFA's labels;SwitchWidthTestis a minimal one.- Also run
:theta-c-frontend:testand the canary gate,:theta-xcfa-cli:fixtureTestandcanaryTest(canaries/README.md). A change that alters values rather than what parses needs aSAFE/UNSAFEfixture: a miswritten cell parses fine. - Frontend-side conventions (two-pass parse,
cTypemetadata, static registries): c-frontend AGENTS.md.
Structure
Utils.kt: besidesgetXcfaFromC,getExpressionFromCparses a single C expression against existing variables (used for witness and invariant input).FrontendXcfaBuilder.ktemits one procedure perCFunction; global initialization is the first edge ofmain. Most of the code is about lvalues and aggregates: struct and array copies, bitfield writes, global initialization (initializeGlobalVariable, which initializes only accessed members), and stack storage (alloca,allocateStackStruct/allocateStackArray).CMetaData.kt: source positions attached to edges (used by witnesses).
Invariants / gotchas
- One JVM builds the same program more than once. The memory-model and arithmetic fallbacks and the portfolio re-parse in-process, so keep no static state in the builder.
RepeatedBuildTestbuilds a program twice to guard this. - Initialization writes exactly the cells that accesses read. An array of structs is laid out inline (
a[i].fat celli*unitCount + f, seeExpressionVisitor#rowOf). Its initializer therefore writes each element's units in place (initializeInlineElementscallsinitializeStructUnits) and mints nested-aggregate bases into those cells. It never creates a separate object per element. - A
CAssumewithgetHavocked()present becomesSequenceLabel(havoc x; assume range(x))on the declaration edge. Inlining shares the callee's variables between calls, so this havoc is what gives an uninitialized local a fresh value on every activation. - Re-stamp
cTypeon any lvalue or expression you rebuild. Deriving the type from the SMT sort cannot tellstruct S *fromunsigned int. - Refuse rather than approximate: throw
UnsupportedFrontendElementExceptionfor a construct that cannot be modelled exactly (see "When in doubt, refuse" in the README). ThrowUnsupportedPointerSplitException(as this builder andReferenceEliminationdo) only for whatflatcan express, andRequiresByteAddressedMemoryExceptiononly for whatbytescan. When no memory model was requested, xcfa-cli rebuilds under that model on exactly these exceptions. - Every expression that global-initialization writes go through is registered in
staticObjectBases, a nested struct field's cell expression too (thestorageGivenbranch). The removal of unread writes finds objects by that expression, so for an unregistered one it drops the base write but keeps the writes through it, which then land at an unknown address.GlobalSubObjectBaseTestguards this. - Library functions and intrinsics (
malloc,alloca,pthread_*,__VERIFIER_*, scope ends) are emitted here asInvokeLabels and resolved later by the passes in:theta-xcfa(xcfa/passes). Changing what an intrinsic means usually belongs in the pass, not here. - An lvalue that is not handled must fail with a message (
unhandledLhs), not be dropped or approximated. - Every memory model must keep working (
multi,flat,bytes;ParseContext.memoryModel). Code that reasons about cells has to say which models it supports. - A branch guard reads the value before the post statements of its expression.
visit(CIf)andvisit(CSwitch)put the assume first and callbuildPostStatementon each branch after it. In the other order,switch (i++)compares the incremented value.