Repository navigation
Description
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 consultsGeneratedSchemaRegistry, 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.Buildthrows when the field extent overrunssizeof(T). That catches theboolshape, whose marshalled extent grows. It does not catchchar: extent 8 vssizeof8, no overrun, silent.
Who hits it
A schema assembly built without the Typhon analyzer package and loaded at runtime.
Proposed fix
AssemblySchemaLoadershould preferGeneratedSchemaRegistryand 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).- Mark spec provenance.
ComponentSchemaSpeccarries whether its offsets are managed-derived; the generator sets it,ReflectComponentSpecdoes not.Buildthen rejects abool/charfield 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
charfields, 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
-
AssemblySchemaLoaderreturns the generated spec's offsets when the assembly has one - Tests fail against the current code and pass after the fix
Activity
#819 Reflection schema paths recorded marshalled field offsets while …