Skip to content

Reflection schema paths record marshalled field offsets while every accessor reads the managed layout #819

Closed

Description

@nockawa

Residual half of #816, split out of PR #818.

Summary

The schema records a byte offset per field. Every accessor reads the component through the managed layout (*(T*), Span<T>, ref T). Two paths still derive those offsets from Marshal.OffsetOf, which reports the marshalled layout. The two agree for ordinary blittable primitives and diverge for exactly two types:

managed marshalled (default CharSet.Ansi)
bool 1 byte 4 bytes
char 2 bytes 1 byte
struct { char A; char B; int C; }
  managed     A@0  B@2  C@4    sizeof 8
  marshalled  A@0  B@1  C@4    extent 8

Same total size, B recorded one byte off. Every field-addressed path then reads the wrong bytes — index key extraction, WAL field decode, crash recovery, schema evolution, the integrity scanner, the Workbench raw read. Whole-struct copies keep working, which is what makes it quiet.

Where

  • src/Typhon.Engine/Schema/public/DatabaseDefinitions.cs:224 — ReflectComponentSpec, the fallback when a type has no generated spec.
  • src/Typhon.Shell/Schema/AssemblySchemaLoader.cs:170 — typhon check --schema <dll>. It reflects unconditionally and never consults GeneratedSchemaRegistry, even for an assembly that carries a correct generated spec.

Fixed already in #818: the source generator measures offsets against a stack probe with Unsafe.ByteOffset, so a generated spec carries managed offsets.

Why the existing guards do not cover it

  • TYPHON011 errors at compile time — but only for code compiled in a project referencing the analyzer. An externally-built schema assembly is exactly the population it cannot reach.
  • DBComponentDefinition.Build throws when the field extent overruns sizeof(T). That catches the bool shape, whose marshalled extent grows. It does not catch char: extent 8 vs sizeof 8, no overrun, silent.

Who hits it

A schema assembly built without the Typhon analyzer package and loaded at runtime.

Proposed fix

  1. AssemblySchemaLoader should prefer GeneratedSchemaRegistry and reflect only as a fallback. An assembly compiled with the generator already carries its spec with correct managed offsets; the loader just never asks. DatabaseDefinitions.CreateFromAccessor<T> already does exactly this (:174).
  2. Mark spec provenance. ComponentSchemaSpec carries whether its offsets are managed-derived; the generator sets it, ReflectComponentSpec does not. Build then rejects a bool/char field only when the offsets came from reflection — so a generated component keeps working and the unverifiable path is refused rather than trusted.

Neither needs IL emit, which rules out the obvious alternative (DynamicMethod per field) on AOT grounds (#409).

Acceptance criteria

  • A component with two char fields, built through the reflection path, is refused rather than silently mis-offset
  • The same component built through the generated path keeps working, with managed offsets
  • AssemblySchemaLoader returns the generated spec's offsets when the assembly has one
  • Tests fail against the current code and pass after the fix

Activity

  1. self-assigned this
    on Aug 15, 2026
  2. added theissue type on Aug 15, 2026
  3. updated Product Engine, Priority P2-Medium and Estimate S on Aug 15, 2026
  4. added a commit that references this issue on Aug 15, 2026
  5. moved this to Done in Typhonon Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Fields

Priority

P2-Medium

Estimate

S

Area

Schema

Start date

None yet

Target date

None yet

Bug status

None yet

Product

Engine

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions