Part II — Intermediate Development · Chapter 10 · Lesson 4 of 6 · 9 min read
Lesson 4: Interpreter/VM Differential Testing, Fuzzing, and Conformance
Lesson level: Advanced intermediate
Purpose: Verify that PeaLang programs behave identically on both execution engines, probe code with generated and hostile inputs, and check behavior against the written language specification.
Learning objectives
By the end of this lesson, you should be able to:
- explain why two engines must agree and what “agree” means;
- run one program on the tree-walking interpreter and the VM and compare results;
- classify differences as defects, permitted variation, or test flaws;
- design a deterministic, bounded fuzzing campaign;
- turn a fuzzing failure into a minimal regression test; and
- use conformance tests to anchor behavior to the specification.
Release note: command names, options, and output formats in this lesson are illustrative. Verify them against the installed v0.63 release and its built-in help before relying on them.
4.1 Two engines, one language
PeaLang programs can run on a tree-walking interpreter or on a portable stack-based virtual machine that executes PEAC bytecode. The language promises the same observable behavior from both. A program that prints one result on one engine and a different result on the other has found a defect in an engine, in the compiler, or in the test.
Because the host chooses an engine, disagreement is a real production risk: a rule validated on one engine could behave differently when deployed on the other.
4.2 What “same behavior” means
Define the comparison explicitly before running it. For deterministic programs the observable behavior includes:
- the returned value and its type;
- printed output, in order;
- whether an error occurred and its category;
- the error message text and reason code, where the specification defines them; and
- side effects granted through capabilities, such as logged records.
Some details are not required to match, such as internal timing, memory use, or the exact wording of an implementation-specific diagnostic. List these exclusions in the test configuration rather than discovering them by accident.
4.3 A differential run
A differential test runs the same source on both engines with the same inputs and limits, then compares the observable results.
pea run rules.pea
pea compile rules.pea -o rules.peac
pea vm-run rules.peac
Capture output and exit status from each run into separate files and compare them. Use fixed inputs and a fixed seed so the only difference between runs is the engine.
4.4 Classifying a difference
When outputs differ, do not assume the VM is wrong. Classify the difference first.
- Engine defect: the specification defines the result and one engine departs from it.
- Compiler defect: the bytecode does not preserve the meaning of the source.
- Permitted variation: the specification leaves the detail unspecified, such as message wording.
- Test flaw: nondeterminism, differing limits, or differing capabilities in the two runs.
Only the first two are defects to fix. The last two are test-design work: tighten the comparison or remove the variation.
4.5 What to compare in the order-discount project
Run every case from the Chapter 9 suite, A through K, on both engines. Compare the returned map, the reason code, and the error category for the invalid cases. Include precedence cases such as a gold customer at the bulk threshold, since evaluation order is where engines most often diverge.
case A standard qty 9 interpreter: NONE vm: NONE
case C gold qty 10 interpreter: GOLD_BULK vm: GOLD_BULK
case J gold qty 0 interpreter: VALIDATION_ERROR vm: VALIDATION_ERROR
The data above is synthetic. A real run records every case and flags any mismatch.
4.6 Areas where engines tend to diverge
- short-circuit evaluation and the order in which operands are evaluated;
- closures capturing loop variables and shadowed names;
- integer and floating-point arithmetic at limits, including division and remainder signs;
- string interpolation and value-to-text conversion;
- exceptions raised inside nested calls and finally blocks;
- module initialization order and repeated imports; and
- resource limits being reached at different points.
Use this list to choose focused programs. A small program that exercises one of these areas is more useful than a large program that exercises all of them.
4.7 What fuzzing is
Fuzzing feeds a program large numbers of generated inputs to find failures that hand-written cases miss. For a rules module, the goal is to confirm that no input causes a crash, a hang, an inconsistent result, or a violated invariant. It complements boundary testing: boundaries test the cases you thought of, and fuzzing tests the ones you did not.
4.8 Generating inputs safely
- Seeded: derive every input from a recorded seed so a failure can be reproduced exactly.
- Bounded: cap the number of cases, input size, nesting depth, and run time.
- Structure-aware: generate maps with the expected keys, then mutate them: missing keys, wrong types, null, huge numbers, empty and very long strings.
- Synthetic: never use real customer data as a seed corpus.
- Contained: run under default-deny capabilities and resource limits, so a bad input cannot touch files or the network.
pea fuzz rules.pea --entry decide --seed 7 --cases 500 --max-time 60
4.9 Properties to check
A fuzzing campaign needs a pass/fail rule for every generated input. Express it as a property that must always hold.
| Property | Example for the discount rules |
|---|---|
| Never crashes | Any input returns a decision or a documented VALIDATION_ERROR, never an unhandled error |
| Result is complete | Every result contains a reason code and a discount amount |
| Invariant holds | The discount is never negative and never exceeds the order subtotal |
| Deterministic | The same input returns the same result on repeated calls |
| Engines agree | Interpreter and VM return the same result for the same input |
The last property combines fuzzing with differential testing: generate inputs, run both engines, and report the first disagreement.
4.10 From failure to regression test
- Record the seed and the exact failing input.
- Shrink the input to the smallest one that still fails, removing fields and reducing values step by step.
- Identify the cause and fix it in the rule, validator, or engine.
- Add the minimal input as a named, deterministic regression test.
- Keep the seed with the test so the original campaign can be replayed.
test "missing tier field returns VALIDATION_ERROR (fuzz seed 7, case 212)" {
let result = rules.decide({"quantity": 10})
assert result["reason"] == "VALIDATION_ERROR"
}
4.11 Limits of fuzzing
- It finds crashes and violated properties, not wrong business policy.
- Coverage of the input space is probabilistic; a clean run is not a proof.
- Weak properties produce clean runs. If the only property is “does not crash”, wrong answers pass.
- Runs must be bounded and reproducible; an open-ended campaign is not a CI gate.
4.12 Conformance testing
A conformance suite checks the implementation against the written specification, independent of any one program. Each test states the specified behavior, the input, and the expected result, and runs on every engine and host.
- Organize tests by specification section: lexical rules, expressions, scope, control flow, errors, standard library, and portable-value boundaries.
- Give each test an identifier that traces to the specification clause it checks.
- Run the suite on both engines and on every supported host configuration.
- Treat a failing conformance test as a release blocker, and an unexplained skip as a defect in the suite.
The supplied v0.63 materials describe a specification-verification command and conformance gates. Use them as the starting point, and extend with project-specific cases rather than replacing them.
4.13 Conformance versus project tests
| Aspect | Project test | Conformance test |
|---|---|---|
| Question answered | Does my rule do what the business wants? | Does the engine do what the language specifies? |
| Owner | Application team | Language and tooling maintainers |
| Input | Domain data | Minimal language constructs |
| Failure means | Policy or rule defect | Engine, compiler, or specification defect |
| Runs on | The deployed engine | Every engine and host |
4.14 Common mistakes
| Mistake | Consequence | Correction |
|---|---|---|
| Comparing printed text that includes timing or addresses | False differences | Define and normalize the compared fields |
| Running the two engines with different limits | Spurious divergence | Use identical limits and capabilities |
| Unseeded fuzzing | Failures cannot be reproduced | Record the seed with every failure |
| A fuzz property that only checks for crashes | Wrong results pass | Add result-shape and invariant properties |
| Assuming the VM is the reference | Real interpreter defects ignored | Compare both against the specification |
| Fixing a bug without a regression test | The bug returns | Add the minimal input as a named test |
4.15 Diagnostic workflow
- Reproduce the difference with the smallest program and fixed inputs.
- Confirm both runs used identical limits, capabilities, and seed.
- Normalize the compared fields and rerun to remove permitted variation.
- Check the specification clause for the disputed behavior.
- Disassemble the bytecode for the failing construct and compare it with the source meaning.
- Decide whether the interpreter, compiler, VM, or test is wrong, then fix and add a regression test.
4.16 Guided practice
- List five observable properties two engines must share for a rules function.
- Explain why differing message wording might be permitted variation.
- Write two properties that a fuzzing campaign for the discount rules should check.
- Describe how you would shrink a failing map with six fields.
- Name two reasons a differential run could differ for a correct engine.
- Map three of the Chapter 9 cases to specification areas such as precedence and validation.
4.17 Hands-on lab: Differential and fuzz testing of the order-discount rules
Goal: Show that the Chapter 9 rules behave identically on both engines and survive a bounded fuzzing campaign.
- Run cases A–K on the interpreter and record the results in a table.
- Compile the project and run the same cases on the VM with identical limits.
- Compare the tables and classify any difference using the four categories.
- Write three properties for the rules: result completeness, non-negative discount, and no unhandled error.
- Run a seeded, bounded fuzz campaign and record the seed, case count, and time limit.
- Add a deliberate defect on a scratch copy, confirm the campaign finds it, and shrink the failing input.
- Add the minimal input as a regression test and remove the defect.
- Select five conformance-style tests for precedence and short-circuit behavior and run them on both engines.
Deliverable: The comparison table, the property definitions, the seed and campaign settings, the shrunk failing input and its regression test, and a short note on what the campaign cannot prove.
4.18 Review questions
- Why must two engines produce the same observable behavior?
- What should a differential comparison include and exclude?
- Name the four classifications for a difference.
- Why must fuzzing be seeded and bounded?
- What is a property, and why is “does not crash” insufficient?
- How does shrinking help?
- How does a conformance test differ from a project test?
- Why is the VM not automatically the reference?
4.19 Applied challenge
Design a verification plan for a new rules module that will be deployed on both engines. Specify the differential comparison and its exclusions, three fuzz properties, the campaign bounds and seed policy, the conformance cases you would add, and the rule for deciding which difference blocks a release.
Lesson summary
PeaLang promises the same behavior on its interpreter and VM, so differential testing compares them on identical inputs and limits and classifies every difference. Seeded, bounded, structure-aware fuzzing finds failures that hand-written cases miss, and every failure becomes a minimal regression test. Conformance tests tie behavior to the specification and run on every engine and host. None of these proves correctness, so they supplement, rather than replace, project tests.
Transition to Lesson 5
The next lesson covers performance gates and regression prevention: measuring speed and resource use repeatably, setting thresholds, and stopping slowdowns from reaching a release.
Lesson status: Ready for review.