Part III — Advanced Systems · Chapter 12 · Lesson 4 of 7 · 5 min read
Lesson 4: PEAC Format, Versioning, Validation, and Portability
Lesson level: Advanced
Purpose: Understand how compiled code objects are serialized into PEAC files, how versions are recorded, why a loader must validate every file, and what makes compiled artifacts portable.
Release note: section names, header fields, and commands in this lesson are illustrative. Verify them against your installed v0.63 release. PEAC is a preview surface; do not assume permanent compatibility.
Learning objectives
By the end of this lesson, you should be able to:
- Describe the logical layout of a PEAC file.
- Explain format and bytecode version fields and how loaders use them.
- List the validation checks a loader must perform.
- Decide when to ship source and when to ship PEAC.
- Diagnose common load failures.
4.1 Why a file format
Compiling on every run costs time and requires the full front end at runtime. A serialized format lets a build step compile once and lets hosts load ready-to-run code. It also gives release engineering a single artifact to hash and sign.
4.2 Logical layout
| Part | Contents |
|---|---|
| Header | Magic marker, format version, bytecode version, flags |
| Metadata | Source file name, compiler version, optional build information |
| Constant pool | Numbers, strings, names, and nested code objects |
| Code objects | Instructions, arity, local names, line table |
| Integrity data | Optional checksum of the payload |
Treat this as a logical model. The exact byte encoding is defined by your release, not by this book.
4.3 Magic marker and versions
The magic marker lets a loader reject files that are not PEAC at all. The format version describes the container layout. The bytecode version describes the instruction set inside. A loader may support several versions of each, and must refuse anything outside that range.
peac header
magic PEAC
format_version 1
bytecode_version 3
compiler pealang 0.63.0rc2
4.4 Compatibility rules
| Situation | Expected loader behaviour |
|---|---|
| Same versions | Load |
| Older bytecode, still supported | Load, possibly with a warning |
| Newer bytecode than the VM knows | Refuse with a clear version message |
| Unknown format version | Refuse |
| Preview flag set, preview disabled in host | Refuse |
Never "try anyway". Executing instructions the VM does not understand can corrupt state or bypass checks.
4.5 Validation before execution
A PEAC file is input. It may be truncated, corrupted, produced by an older compiler, or deliberately crafted. Before running anything, a loader should check:
- the magic marker and supported versions;
- that every section length fits inside the file;
- that every constant and name index is in range;
- that every jump target is a valid instruction offset;
- that the stack depth never goes negative or exceeds a declared maximum on any path;
- that only known opcodes appear; and
- that the integrity checksum, if present, matches.
4.6 Why stack validation matters
Bytecode that pops from an empty stack, or jumps into the middle of nowhere, could read memory the VM did not intend to expose. Verifying stack depth along every control path before execution turns such files into load errors instead of runtime faults.
4.7 Validation is not trust
A structurally valid file can still contain hostile logic. Validation ensures the VM can run the code safely within its own rules; signatures (Chapter 11) and capability limits (Chapter 14) decide whether you want to run it at all.
4.8 Building and loading PEAC
pea compile src/rules.pea -o build/rules.peac
pea inspect build/rules.peac
pea run build/rules.peac
These illustrative commands compile a module, print its header and sections, and run it on the VM. Confirm exact command names with pea --help in your release.
4.9 Portability
A PEAC file is portable when it depends only on the instruction set and portable values. It stops being portable if it relies on host-specific objects, absolute file paths in metadata that the host uses, or a bytecode version newer than the target VM.
| Factor | Portable choice |
|---|---|
| Values | Only null, boolean, integer, float, string, list, string-keyed map |
| Paths | Relative module names, not absolute paths |
| Versions | Compile for the oldest VM you must support |
| Capabilities | Requested at runtime, never embedded as host objects |
4.10 Source or PEAC?
| Ship source when | Ship PEAC when |
|---|---|
| Hosts run the same release as the build | Startup time matters |
| Readers must review the exact code | The host should not need the compiler |
| Bytecode format may change soon | Artifacts are signed and versioned together |
Many teams ship both: PEAC for execution and source for review and rebuilds. The release manifest (Chapter 24) records the hash of each.
4.11 Reproducible compilation
The same source and compiler version should produce byte-identical PEAC. Avoid embedding timestamps or machine names in metadata, or make them optional, so release hashes stay stable across build machines.
4.12 Worked example
A host running VM bytecode version 2 receives a PEAC file built with bytecode version 3. The loader reports an unsupported version and refuses to run it. The team rebuilds with a compiler setting that targets version 2, re-signs the artifact, and records both the target version and the hash in the release notes.
4.13 Common load failures
| Message | Likely cause | Response |
|---|---|---|
| Not a PEAC file | Wrong file or corruption | Check the path and checksum |
| Unsupported bytecode version | Compiler newer than VM | Rebuild for the target or upgrade the VM |
| Invalid jump target | Corruption or tampering | Rebuild from source; verify signature |
| Stack underflow on path | Malformed code object | Treat as untrusted; do not bypass |
Common mistakes
- Treating a valid PEAC file as trusted: validation checks structure, not intent; load only artifacts from trusted, signed sources.
- Shipping PEAC without its source: keep source and build metadata so artifacts can be rebuilt and audited.
- Ignoring format-version errors: recompile with the matching toolchain instead of forcing a load.
- Assuming PEAC hides logic: bytecode can be disassembled; never embed secrets in it.
- Non-reproducible builds: pin the compiler version and inputs so the same source produces identical artifacts.
4.14 Guided practice
- Compile a module and record its header fields.
- Truncate a copy of the file and observe the loader error.
- Change one byte in the instruction section and observe the result.
- Compile twice and compare hashes.
4.15 Hands-on lab
Build PEAC artifacts for every order_rules module. Run the Chapter 10 differential tests against both the interpreter and the compiled artifacts. Then corrupt one artifact and confirm that loading fails before any rule executes.
Deliverable: the artifact list with hashes, the differential test result, and the loader output for the corrupted file.
4.16 Review questions
- What is the difference between the format version and the bytecode version?
- Why should a loader refuse newer bytecode?
- Name four structural checks a loader performs.
- Why does validation not establish trust?
- What makes a PEAC file non-portable?
4.17 Applied challenge
Write a compatibility policy for a product that supports three VM releases at once. State which bytecode version you compile for, how you test each VM, and when you drop support for the oldest.
Lesson summary
- PEAC serializes code objects with explicit format and bytecode versions.
- Loaders validate structure, indices, jumps, and stack depth before execution.
- Validation protects the VM; signatures and capabilities decide trust.
- Portable artifacts use only portable values and supported versions.
Transition to Lesson 5
With a validated code object loaded, the VM can run it. Lesson 5 follows execution: the stack, call frames, closures, modules, and exceptions.
Lesson status: Ready for review.