Reusable VB3 reverse-engineering tools + findings: a decompiler for Visual Basic 3.0 executables. Public at https://github.com/vmartinv/vb3-decompiler.
.mdfiles hold current findings and next steps only, kept succinct. When something is superseded, rewrite it in place. Don't keep retractions, dead ends or history (git has that).- Commit after each validated milestone.
- Never commit VB3,
VBRUN300.DLL, analyzed executables or anything extracted from them (see.gitignore). Findings about them (offsets, opcode meanings) are fine. - Tokens are expensive, wall-clock time is not. Prefer controlled experiments (batch many cases into one generated project, compile, read the result) over long manual analysis; runs may take as long as needed. Keep tool output small (filter/summarize before printing).
- Tests run under pytest:
pytestruns the unit tests (seconds, no IDE),DISPLAY=:99 pytest -m ideevery battery case and sample through the IDE (-k <battery>/-k <sample>to narrow). Add unit tests (tests/test_*.py) for pure logic such as naming or table models.ruff check .must pass (config inpyproject.toml); CI (.github/workflows/ci.yml) runs it and the unit tests. - A refactor (no intended output change) is checked with
tools/snapshot.py: decompile every battery/sample exe before and after (--freeze DIRfirst when builds may run meanwhile, then--work DIR) and--diffthe two snapshots: no differences. - Validate with feature batteries (
tests/batteries/<feature>.py, run bytests/battery.py): one battery per language feature, sweeping its whole range, round-tripped case by case. The VB3 samples are only a sanity check (tests/roundtrip.py): they cover a narrow slice of the language.battery.py --checkvalidates a new battery's source first. - Name unknown opcodes with probes (
tests/probes/<name>.py, run bytests/opprobe.py): one statement per line, unknown ops reported per statement;--semproposes builtin entries. - Builds use
VB.EXE /MAKE(~2 s, no GUI clicks). Its output differs from a GUI "Make EXE" build in one word of RT_RCDATA 1, so compare /MAKE builds only with /MAKE builds. Build from short directory paths (underwork/): from a long one (~90 characters) the IDE compiles some form properties differently. - Target: the decompiled source recompiles to the same p-code and form
resources (see Status). Don't add fitting for exe-only fields (name-table
sizes, init-list/hash orders, pool offsets, line counts): exe byte
identity is not a goal. The one name-length effect that reaches p-code
(object locals' free order) is handled in
src/vb3decompiler/naming.py. - Code layout: the decompiler is the
vb3decompilerpackage insrc/vb3decompiler/(only the decompiler;pyproject.toml,pip install .gives thevb3decompilecommand); the regression tests intests/(batteries, probes and their runners); everything else (analysis, the IDE driver) intools/. Scripts addsrc/(andtools/) tosys.pathand importvb3decompiler.*, so they run without installing; tests/ and tools/ module names must not repeat (the script's own directory wins). Table/record field offsets are named inrecords.py, never raw; per-module state is amodel.Module, per-exe state lives onRuntime/Symbols(no module globals). - Before changing what the decompiler emits for some construct, get
ground truth: hand-write two minimal
.frms that differ only in that construct and compile them directly withtools/vb3ide/compile_project.py, then compare their p-code. - A fix must be validated against the whole battery (
battery.pywith no-k) and the full sample roundtrip, not just the isolated case: several modules share one project's global image, and a change that's correct alone can still cascade into an unrelated module's p-code once bundled. Zero p-code regressions (CODE/FORM/CRASH/DECOFAIL) is non-negotiable. For a pure refactor, also check that the decompiled text of every battery/sample exe is unchanged. - For a sample with known original source, localize a p-code mismatch
by splicing hybrid
.frm/.basfiles (original text with one aspect swapped in from the reconstruction), recompiling each and diffing the p-code (tools/pcode_diff.py). Get the true slot/record <-> name correspondence fromtools/align_source.py, not from file position. - VB.EXE's and VBRUN300's own logic is readable, and often the faster
path once black-box probing stalls on an ordering/emission rule:
decompile the relevant segment with Ghidra instead of guessing further
from input/output pairs. Ghidra is installed (
pacman -S ghidra, officialextrarepo —pacmanis aliased toyayhere, so AUR works too if something's not inextra). It decompiles a raw NE segment cleanly once imported asx86:LE:16:Real Modewith the plain binary loader at base 0 (segment bytes are already one contiguous blob once pulled viane.parse_ne(...)[n].data, src/vb3decompiler/ne.py) — far more legible than manually walking capstone output, which is fine for a short handler body (assrc/vb3decompiler/runtime.pyalready does for VBRUN300.DLL) but not for tracing control flow through a real compiler. Headless recipe:analyzeHeadless <proj-dir> <name> -import <segment.bin> -processor "x86:LE:16:Real Mode" -loader BinaryLoader -loader-baseAddr 0to import + auto-analyze once, then re-run against the saved project with a script to decompile specific addresses:analyzeHeadless <proj-dir> <name> -process <segment.bin> -noanalysis -scriptPath <dir> -postScript <Script>.java— write the script as a JavaGhidraScript(a.pyone needs PyGhidra, which isn't enabled by default here), usingDecompInterface+getFunctionContaining(addr)+getDecompiledFunction().getC()per address of interest. Segment 53 alone (VB.EXE's compile-time symbol table code, ~35KB) took Ghidra ~25s to auto-analyze; whole-binary import wasn't tried. A far call with segment0xFFFFin the raw bytes is an unresolved NE relocation, not a real target — the plainBinaryLoaderimport here doesn't see the NE relocation table at all (it's outside the raw segment blob), so don't assume Ghidra resolved it either; checkSegment.relocsfromne.parse_ne(...)for the real fixup if one of those matters.
The decompiler is complete for the target above: every VB3 language feature has a battery, and all 969 battery cases (20 batteries) and all 22 samples (483 procedures, every form) round-trip to identical p-code and form resources. Work from here is fixing gaps as they turn up (a new exe that doesn't round-trip: reduce it to a battery case first).
OPCODES.md: p-code format;ne.py,runtime.py,symbols.pyinsrc/vb3decompiler/implement it (tools/pcode_disasm.py: the disassembler command line).RESOURCE_FORMAT.md: NE resources / forms.
root/vb/samples/<name>/: VB3 sample projects (.mak/.frm/.bassource + compiled.exe, e.g.calc/calc.exe,mdi/mdinote.exe). Sanity check only.battery/<battery>/<project>/{orig,deco}: battery builds;probe/<probe>/: probe builds.tests/<name>/: generated test projects (source + exe).ide/:VB.EXE,VBRUN300.DLL, VBX/DLLs..wineprefix/(C: =vdrive_mnt/, loop-mountedvdrive.img),proj/,sweep/,sweep_struct/: used bytools/vb3ide/(defaults need no env vars; X display:99).corpus/: aligned sample corpus.
python3 tools/validate.py work/root/vb/samples \
--runtime work/ide/VBRUN300.DLL --vbx-dir work/ide -v
python3 tools/lift_score.py score work/corpus --runtime work/ide/VBRUN300.DLL