From 0ec35296184c0629993ced7b01205d3de19f4c09 Mon Sep 17 00:00:00 2001 From: "g. nicholas d'andrea" Date: Tue, 10 Nov 2026 19:06:48 -0500 Subject: [PATCH 1/6] format: add virtual regions for computed values --- .../format/src/types/pointer/pointer.test.ts | 4 + packages/format/src/types/pointer/pointer.ts | 22 +++- packages/pointers/src/cursor.ts | 5 +- .../pointers/src/dereference/index.test.ts | 104 ++++++++++++++++++ packages/pointers/src/dereference/region.ts | 51 ++++++++- packages/pointers/src/read.test.ts | 19 ++++ packages/pointers/src/read.ts | 9 ++ packages/pointers/test/test-cases.ts | 38 +++++++ packages/web/docs/concepts/pointers.mdx | 18 +++ .../docs/core-schemas/pointers/regions.mdx | 31 +++++- .../making-regions-concrete.mdx | 10 ++ .../testing/test-cases/test-cases.mdx | 2 + .../testing/test-cases/virtual-regions.mdx | 27 +++++ packages/web/spec/pointer/concepts.mdx | 8 +- .../spec/pointer/region/location/virtual.mdx | 11 ++ .../spec/pointer/region/scheme/fragment.mdx | 11 ++ packages/web/src/schemas.ts | 3 +- schemas/pointer.schema.yaml | 23 ++++ schemas/pointer/region.schema.yaml | 10 ++ schemas/pointer/region/virtual.schema.yaml | 46 ++++++++ schemas/pointer/scheme/fragment.schema.yaml | 66 +++++++++++ 21 files changed, 509 insertions(+), 9 deletions(-) create mode 100644 packages/web/docs/implementation-guides/pointers/testing/test-cases/virtual-regions.mdx create mode 100644 packages/web/spec/pointer/region/location/virtual.mdx create mode 100644 packages/web/spec/pointer/region/scheme/fragment.mdx create mode 100644 schemas/pointer/region/virtual.schema.yaml create mode 100644 schemas/pointer/scheme/fragment.schema.yaml diff --git a/packages/format/src/types/pointer/pointer.test.ts b/packages/format/src/types/pointer/pointer.test.ts index 27211c9e9b..eb124e4895 100644 --- a/packages/format/src/types/pointer/pointer.test.ts +++ b/packages/format/src/types/pointer/pointer.test.ts @@ -160,6 +160,10 @@ testSchemaGuards("ethdebug/format/pointer", [ schema: "schema:ethdebug/format/pointer/region/code", guard: Pointer.Region.isCode, }, + { + schema: "schema:ethdebug/format/pointer/region/virtual", + guard: Pointer.Region.isVirtual, + }, { schema: "schema:ethdebug/format/pointer/collection/group", guard: Pointer.Collection.isGroup, diff --git a/packages/format/src/types/pointer/pointer.ts b/packages/format/src/types/pointer/pointer.ts index f5bc859c89..baf15b387d 100644 --- a/packages/format/src/types/pointer/pointer.ts +++ b/packages/format/src/types/pointer/pointer.ts @@ -15,7 +15,8 @@ export namespace Pointer { | Region.Calldata | Region.Returndata | Region.Transient - | Region.Code; + | Region.Code + | Region.Virtual; export const isRegion = (value: unknown): value is Region => [ @@ -26,6 +27,7 @@ export namespace Pointer { Region.isReturndata, Region.isTransient, Region.isCode, + Region.isVirtual, ].some((guard) => guard(value)); export namespace Region { @@ -71,6 +73,10 @@ export namespace Pointer { export type Code = Base & Scheme.Slice & { location: "code" }; export const isCode = (value: unknown): value is Code => isBase(value) && Scheme.isSlice(value) && value.location === "code"; + + export type Virtual = Base & Scheme.Fragment & { location: "virtual" }; + export const isVirtual = (value: unknown): value is Virtual => + isBase(value) && Scheme.isFragment(value) && value.location === "virtual"; } export namespace Scheme { @@ -99,6 +105,20 @@ export namespace Pointer { isExpression(value.offset) && "length" in value && isExpression(value.length); + + export interface Fragment { + expression: Expression; + offset?: Expression; + length?: Expression; + } + + export const isFragment = (value: unknown): value is Fragment => + !!value && + typeof value === "object" && + "expression" in value && + isExpression(value.expression) && + (!("offset" in value) || isExpression(value.offset)) && + (!("length" in value) || isExpression(value.length)); } export type Collection = diff --git a/packages/pointers/src/cursor.ts b/packages/pointers/src/cursor.ts index 56be57cd6c..53b6e925be 100644 --- a/packages/pointers/src/cursor.ts +++ b/packages/pointers/src/cursor.ts @@ -30,10 +30,11 @@ export namespace Cursor { /** * A Pointer region where all dynamic expressions have been replaced with - * concrete bytes values. + * concrete bytes values. A virtual region's `expression` becomes the + * bytes of its value. */ export type Region = { - [K in keyof R]: K extends "slot" | "offset" | "length" + [K in keyof R]: K extends "slot" | "offset" | "length" | "expression" ? R[K] extends Pointer.Expression ? Data : R[K] extends Pointer.Expression | undefined diff --git a/packages/pointers/src/dereference/index.test.ts b/packages/pointers/src/dereference/index.test.ts index b6feb1c83d..bb7b525585 100644 --- a/packages/pointers/src/dereference/index.test.ts +++ b/packages/pointers/src/dereference/index.test.ts @@ -893,6 +893,110 @@ describe("dereference", () => { expect(regions.lookup["my-slot-value"]).toBeDefined(); }); + describe("virtual regions", () => { + // `a` is on the stack, which reads as 0x11223344 + const a: Pointer.Region = { name: "a", location: "stack", slot: 2 }; + + const view = async (...group: Pointer[]) => + await (await dereference({ group: [a, ...group] })).view(state); + + const x: Pointer.Region = { + name: "x", + location: "virtual", + expression: { "~wordsized": { "~sum": [{ "~read": "a" }, 1] } }, + }; + + it("defaults to the whole value of the expression", async () => { + const { regions, read } = await view(x); + const value = Data.fromNumber(0x11223345).resizeTo(32); + + expect(regions.lookup["x"]).toEqual({ + name: "x", + location: "virtual", + expression: value, + offset: Data.fromUint(0n), + length: Data.fromNumber(32), + }); + expect(await read(regions.lookup["x"])).toEqual(value); + }); + + it("reads the fragment at an explicit offset and length", async () => { + const { regions, read } = await view( + { ...x, offset: 29, length: 2 }, + { + name: "rest", + location: "virtual", + expression: "0xdeadbeef", + offset: 1, + }, + ); + + expect(await read(regions.lookup["x"])).toEqual(Data.fromHex("0x2233")); + expect(await read(regions.lookup["rest"])).toEqual( + Data.fromHex("0xadbeef"), + ); + expect(regions.lookup["rest"].length).toEqual(Data.fromNumber(3)); + }); + + it("allows lookups of offset and length from a later region", async () => { + const { regions } = await view( + { + name: "tag", + location: "virtual", + expression: "0xdeadbeef", + offset: 1, + }, + { + location: "memory", + offset: { ".offset": "tag" }, + length: { ".length": "tag" }, + }, + ); + + expect(regions[2]).toEqual({ + location: "memory", + offset: Data.fromNumber(1), + length: Data.fromNumber(3), + }); + }); + + it("allows reading a virtual region in another expression", async () => { + const { regions, read } = await view(x, { + name: "y", + location: "virtual", + expression: { "~concat": ["0x00", { "~read": "x" }] }, + length: 2, + }); + + expect(await read(regions.lookup["y"])).toEqual(Data.fromHex("0x0000")); + expect(regions.lookup["y"]).toHaveProperty("expression.length", 33); + }); + + it("rejects a lookup of slot", async () => { + await expect( + view(x, { location: "storage", slot: { ".slot": "x" } }), + ).rejects.toThrow("Region named x does not have slot needed by lookup"); + }); + + it("rejects an expression with no width", async () => { + await expect( + view({ location: "virtual", expression: { "~sum": [1, 2] } }), + ).rejects.toThrow("evaluates to the integer 3, which has no byte width"); + }); + + it("rejects a fragment outside the value", async () => { + for (const fragment of [ + { offset: 5 }, + { offset: 2, length: 3 }, + { length: 5 }, + ]) { + await expect( + view({ location: "virtual", expression: "0xdeadbeef", ...fragment }), + ).rejects.toThrow("extends outside the 4 bytes"); + } + }); + }); + describe("names that start with $", () => { it("resolves a region named $this, not the current region", async () => { const pointer: Pointer = { diff --git a/packages/pointers/src/dereference/region.ts b/packages/pointers/src/dereference/region.ts index 8c855a37c2..a2d11c66d3 100644 --- a/packages/pointers/src/dereference/region.ts +++ b/packages/pointers/src/dereference/region.ts @@ -1,6 +1,6 @@ import { Pointer } from "@ethdebug/format"; import type { Cursor } from "#cursor"; -import type { Data } from "#data"; +import { Data } from "#data"; import { evaluate, Value, type EvaluateOptions } from "#evaluate"; /** @@ -86,12 +86,61 @@ export async function evaluateRegion( } } + if (Pointer.Region.isVirtual(region)) { + const fragment = await evaluateFragment( + region, + evaluatedProperties, + options, + ); + return fragment as Cursor.Region; + } + return { ...region, ...evaluatedProperties, } as Cursor.Region; } +/** + * Evaluate a virtual region's `expression` to the bytes it addresses into, + * and fill in the default `offset` and `length` so that later regions may + * look them up + */ +async function evaluateFragment( + region: Pointer.Region.Virtual, + { offset, length }: { offset?: Data; length?: Data }, + options: EvaluateOptions, +): Promise> { + const value = await evaluate(region.expression, options); + if (Value.isInteger(value)) { + throw new Error( + [ + `Expression of virtual region (${JSON.stringify(region.expression)}) `, + `evaluates to the integer ${value.value}, which has no byte `, + `width; give it a width with ~wordsized or ~sizedN`, + ].join(""), + ); + } + + const width = BigInt(value.data.length); + const start = offset?.asUint() ?? 0n; + const end = length ? start + length.asUint() : width; + if (start > width || end > width) { + const fields = length ? `${start}, length ${length.asUint()}` : start; + throw new Error( + `Fragment of virtual region (offset ${fields}) extends outside ` + + `the ${width} bytes of its expression's value`, + ); + } + + return { + ...region, + expression: value.data, + offset: Data.fromUint(start), + length: Data.fromUint(end - start), + }; +} + /** * Detect a stack region and modify its `slot` expression to include the * appropriate sum or difference based on the machine stack length change diff --git a/packages/pointers/src/read.test.ts b/packages/pointers/src/read.test.ts index 5824ed3911..467bfe5761 100644 --- a/packages/pointers/src/read.test.ts +++ b/packages/pointers/src/read.test.ts @@ -455,4 +455,23 @@ describe("read (segment carry)", () => { }); expect(result).toEqual(bytes([0xa2, 2], [0xa3, 2])); }); + + it("reads the fragment of a virtual region", async () => { + const expression = Data.fromHex("0xdeadbeef"); + + expect( + await read( + { + location: "virtual", + expression, + offset: Data.fromNumber(1), + length: Data.fromNumber(2), + }, + options, + ), + ).toEqual(Data.fromHex("0xadbe")); + expect(await read({ location: "virtual", expression }, options)).toEqual( + expression, + ); + }); }); diff --git a/packages/pointers/src/read.ts b/packages/pointers/src/read.ts index 05784be47b..e38bb028c5 100644 --- a/packages/pointers/src/read.ts +++ b/packages/pointers/src/read.ts @@ -84,6 +84,15 @@ export async function read( return await state.code.read({ slice: { offset, length } }); } + case "virtual": { + const { offset = 0n, length } = withPropertiesAsUints( + ["offset", "length"], + region, + ); + const end = length === undefined ? undefined : Number(offset + length); + + return Data.fromBytes(region.expression.subarray(Number(offset), end)); + } } } diff --git a/packages/pointers/test/test-cases.ts b/packages/pointers/test/test-cases.ts index 8e7d3942f6..32d439286a 100644 --- a/packages/pointers/test/test-cases.ts +++ b/packages/pointers/test/test-cases.ts @@ -237,6 +237,43 @@ const uint256ArrayMemoryTest: ObserveTraceTest = { }, }; +/** + * the computed values example: `x` (`a + 1`) and `tag` (the first 4 bytes + * of the hash of `a`) exist only as virtual regions computed from `a` + */ +const virtualRegionsTest: ObserveTraceTest<{ x: number; tag: string }> = { + pointer: findExamplePointer("virtual-operand"), + compileOptions: singleSourceCompilation({ + path: "VirtualRegions.sol", + contractName: "VirtualRegions", + content: `contract VirtualRegions { + uint256 a; + + constructor() { + a = 2; + } + } + `, + }), + + expectedValues: [ + { x: 1, tag: "0x290decd9" }, + { x: 3, tag: "0x405787fa" }, + ], + + async observe({ regions, read }) { + const x = Number((await read(regions.lookup["x"])).asUint()); + + const tag = (await read(regions.lookup["tag"])).toHex(); + + return { x, tag }; + }, + + equals(a, b) { + return a.x === b.x && a.tag === b.tag; + }, +}; + /** * collection of descriptions of tests that compile+deploy Solidity code, * then step through the machine trace of that code's execution, watching @@ -251,4 +288,5 @@ export const observeTraceTests = { "string storage": stringStorageTest, "string storage (multi-slot)": multiSlotStringStorageTest, "uint256[] memory": uint256ArrayMemoryTest, + "virtual regions": virtualRegionsTest, }; diff --git a/packages/web/docs/concepts/pointers.mdx b/packages/web/docs/concepts/pointers.mdx index 0bab12659c..c50a89ade5 100644 --- a/packages/web/docs/concepts/pointers.mdx +++ b/packages/web/docs/concepts/pointers.mdx @@ -186,6 +186,24 @@ optional offset and length for packed values: }`} +**Fragment-based regions** (virtual) hold the bytes of a computed value, +for data that no EVM location stores (e.g., a variable the optimizer removed). +Optional offset and length select part of the value: + + + {`{ + "location": "virtual", + "expression": { + "~keccak256": [{ "~wordsized": 1 }] + }, + "length": 4 +}`} + + Regions can be **named** for reference elsewhere in the pointer: sourceFile.getFunction("evaluateRegion")} /> + +A virtual region also has an `expression`. The implementation evaluates it +last, keeps its bytes as the concrete region's `expression`, and fills in the +default `offset` and `length`, so that later regions can look them up: + + sourceFile.getFunction("evaluateFragment")} +/> diff --git a/packages/web/docs/implementation-guides/pointers/testing/test-cases/test-cases.mdx b/packages/web/docs/implementation-guides/pointers/testing/test-cases/test-cases.mdx index 71729817dc..c2a2bd083e 100644 --- a/packages/web/docs/implementation-guides/pointers/testing/test-cases/test-cases.mdx +++ b/packages/web/docs/implementation-guides/pointers/testing/test-cases/test-cases.mdx @@ -23,6 +23,8 @@ Test cases are aggregated into the `observeTraceTests` variable: "/docs/implementation-guides/pointers/testing/test-cases/string-storage/", uint256ArrayMemoryTest: "/docs/implementation-guides/pointers/testing/test-cases/uint256-array-memory/", + virtualRegionsTest: + "/docs/implementation-guides/pointers/testing/test-cases/virtual-regions/", }} /> diff --git a/packages/web/docs/implementation-guides/pointers/testing/test-cases/virtual-regions.mdx b/packages/web/docs/implementation-guides/pointers/testing/test-cases/virtual-regions.mdx new file mode 100644 index 0000000000..7ab06b3378 --- /dev/null +++ b/packages/web/docs/implementation-guides/pointers/testing/test-cases/virtual-regions.mdx @@ -0,0 +1,27 @@ +--- +sidebar_position: 4 +sidebar_label: virtual regions +--- + +import CodeListing from "@site/src/components/CodeListing"; +import TestedPointer from "./TestedPointer"; + +# Test case: virtual regions + +A virtual region holds a computed value instead of stored bytes. This test +computes two values from a `uint256` in storage: `x`, the value plus one, +and `tag`, the first 4 bytes of the value's hash. + +## Test source + + + sourceFile.getVariableStatement("virtualRegionsTest") + } +/> + +## Tested pointer + + diff --git a/packages/web/spec/pointer/concepts.mdx b/packages/web/spec/pointer/concepts.mdx index a3cc05030e..ed312b7c6a 100644 --- a/packages/web/spec/pointer/concepts.mdx +++ b/packages/web/spec/pointer/concepts.mdx @@ -277,11 +277,15 @@ abstractions, but instead it defines the concept of an **addressing scheme**. Each location-specific region schema is defined as an extension of the schema for a particular addressing scheme. -This format currently defines two such addressing schemes: +This format currently defines three such addressing schemes: **ethdebug/format/pointer/scheme/slice** and **ethdebug/format/pointer/scheme/segment**, for addressing ranges within a single continuous byte array and addressing a slot or collection of slots -in a word-arranged locaton (respectively). +in a word-arranged locaton (respectively), and +**ethdebug/format/pointer/scheme/fragment**, which supplies its own byte +space (the value of an expression) and addresses a range within it. The +`virtual` location uses the fragment scheme for values that are computed +instead of stored. Within a slot, the segment scheme numbers bytes from the most significant byte: byte `0` is the first byte of the slot's big-endian word. diff --git a/packages/web/spec/pointer/region/location/virtual.mdx b/packages/web/spec/pointer/region/location/virtual.mdx new file mode 100644 index 0000000000..9026269c55 --- /dev/null +++ b/packages/web/spec/pointer/region/location/virtual.mdx @@ -0,0 +1,11 @@ +--- +sidebar_position: 8 +--- + +import SchemaViewer from "@site/src/components/SchemaViewer"; + +# `virtual` + + diff --git a/packages/web/spec/pointer/region/scheme/fragment.mdx b/packages/web/spec/pointer/region/scheme/fragment.mdx new file mode 100644 index 0000000000..b43c18462f --- /dev/null +++ b/packages/web/spec/pointer/region/scheme/fragment.mdx @@ -0,0 +1,11 @@ +--- +sidebar_position: 3 +--- + +import SchemaViewer from "@site/src/components/SchemaViewer"; + +# `fragment` + + diff --git a/packages/web/src/schemas.ts b/packages/web/src/schemas.ts index 2a82d08f0b..02cf5873e1 100644 --- a/packages/web/src/schemas.ts +++ b/packages/web/src/schemas.ts @@ -92,6 +92,7 @@ const pointerSchemaIndex: SchemaIndex = { "returndata", "transient", "code", + "virtual", ] .map((location) => ({ [`schema:ethdebug/format/pointer/region/${location}`]: { @@ -100,7 +101,7 @@ const pointerSchemaIndex: SchemaIndex = { })) .reduce((a, b) => ({ ...a, ...b }), {}), - ...["slice", "segment"] + ...["slice", "segment", "fragment"] .map((scheme) => ({ [`schema:ethdebug/format/pointer/scheme/${scheme}`]: { href: `/spec/pointer/region/scheme/${scheme}`, diff --git a/schemas/pointer.schema.yaml b/schemas/pointer.schema.yaml index ad9cf49727..404c5f1723 100644 --- a/schemas/pointer.schema.yaml +++ b/schemas/pointer.schema.yaml @@ -297,3 +297,26 @@ examples: slot: "start-slot" offset: 0 length: "string-length" + + - # example computed values: `x = a + 1` after the optimizer removed `x`, + # and `tag`, the first 4 bytes of the hash of `a`, where `a` is a + # `uint256` in storage slot 0 + group: + - name: "virtual-operand" + location: storage + slot: 0 + + - name: "x" + location: virtual + expression: + ~wordsized: + ~sum: + - ~read: "virtual-operand" + - 1 + + - name: "tag" + location: virtual + expression: + ~keccak256: + - ~read: "virtual-operand" + length: 4 diff --git a/schemas/pointer/region.schema.yaml b/schemas/pointer/region.schema.yaml index e2f7f5e705..364bf404e9 100644 --- a/schemas/pointer/region.schema.yaml +++ b/schemas/pointer/region.schema.yaml @@ -76,6 +76,15 @@ allOf: then: $ref: "schema:ethdebug/format/pointer/region/code" + - if: + required: + - location + properties: + location: + const: virtual + then: + $ref: "schema:ethdebug/format/pointer/region/virtual" + $defs: Location: type: string @@ -87,6 +96,7 @@ $defs: - returndata - transient - code + - virtual unevaluatedProperties: false diff --git a/schemas/pointer/region/virtual.schema.yaml b/schemas/pointer/region/virtual.schema.yaml new file mode 100644 index 0000000000..9802f6651f --- /dev/null +++ b/schemas/pointer/region/virtual.schema.yaml @@ -0,0 +1,46 @@ +$schema: "https://json-schema.org/draft/2020-12/schema" +$id: "schema:ethdebug/format/pointer/region/virtual" + +title: ethdebug/format/pointer/region/virtual +description: | + A schema for representing a region of data that is not stored in any EVM + data location, but is computed from the machine state: the bytes of an + expression's value. + + A compiler may use a virtual region for a value that has no place in the + machine state of its own, e.g., a variable the optimizer removed whose + value can still be computed from other values that remain. + + This schema is constructed by extending the base region schema + and the schema for the fragment addressing scheme. +type: object +allOf: + - title: '{ "location": "virtual" }' # note: whitespace chars are \255 (nbsp) + properties: + location: + const: virtual + + required: + - location + - $ref: "schema:ethdebug/format/pointer/region/base" + - $ref: "schema:ethdebug/format/pointer/scheme/fragment" + +unevaluatedProperties: false + +examples: + # `x = a + 1` after the optimizer removed `x`, where a region named `a` + # comes before this one + - name: x + location: virtual + expression: + ~wordsized: + ~sum: + - ~read: a + - 1 + # the first 4 bytes of a hash + - name: tag + location: virtual + expression: + ~keccak256: + - ~read: a + length: 4 diff --git a/schemas/pointer/scheme/fragment.schema.yaml b/schemas/pointer/scheme/fragment.schema.yaml new file mode 100644 index 0000000000..3e4dcf7c46 --- /dev/null +++ b/schemas/pointer/scheme/fragment.schema.yaml @@ -0,0 +1,66 @@ +$schema: "https://json-schema.org/draft/2020-12/schema" +$id: "schema:ethdebug/format/pointer/scheme/fragment" + +title: ethdebug/format/pointer/scheme/fragment +description: | + An addressing scheme for pointing to a range of bytes inside the value of + an expression. + + The other addressing schemes address bytes in a byte space that the data + location already has. This scheme supplies the byte space itself: the + bytes of the value that `expression` evaluates to. It then addresses a + range of bytes in that value by `offset` and `length`, as the slice + scheme does. + + A fragment that extends outside the value's bytes (i.e., where `offset` + plus `length` exceeds the value's width) is invalid. + +type: object + +properties: + expression: + description: | + The expression whose value supplies the bytes for this fragment. + + This expression **must** be width-bearing (bytes), the same rule as + for the operands of `~concat` and `~keccak256`. A bare integer (a JSON + number, an odd-digit hex literal, `~wordsize`, an arithmetic result, + or a lookup) is **not** valid here: give it a width first with + `~sizedN` or `~wordsized`. + + This expression is evaluated against the same machine state as the + other regions of the pointer. It may refer only to regions that come + before this region. + $ref: "schema:ethdebug/format/pointer/expression" + offset: + description: | + The index of the byte (starting from zero) in the value's bytes where + the fragment begins. + + This field is **optional**. If unspecified, it has the default value + of `0`, indicating that the fragment begins at the first byte of the + value. + $ref: "schema:ethdebug/format/pointer/expression" + default: 0 + length: + description: | + The length of the fragment in number of bytes. + + This field is **optional**. If unspecified, its default value + indicates that the fragment ends at the end of the value: the value's + width minus `offset`. + $ref: "schema:ethdebug/format/pointer/expression" + +required: + - expression + +examples: + - expression: + ~wordsized: 1 + - expression: + ~keccak256: + - ~wordsized: 0 + length: 4 + - expression: "0xdeadbeef" + offset: 2 + length: 2 From 60fd38d89326705de05f24b26fd16eb164f259dc Mon Sep 17 00:00:00 2001 From: "g. nicholas d'andrea" Date: Tue, 10 Nov 2026 19:07:11 -0500 Subject: [PATCH 2/6] Add changelog entries for virtual regions --- CHANGELOG.md | 15 +++++++++++++++ packages/format/CHANGELOG.md | 7 +++++++ packages/pointers/CHANGELOG.md | 7 +++++++ 3 files changed, 29 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 519bf11bde..431488ff29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,21 @@ The sections do not signal obligations; the prefixes do. ## Unreleased +### Added + +- A region with `"location": "virtual"` holds the bytes of a computed value, + e.g., a variable the optimizer removed. It uses the new `fragment` + addressing scheme: a width-bearing `expression` supplies the bytes, and + optional `offset` and `length` select a range of them. + - Schemas: **ethdebug/format/pointer/region/virtual**, + **ethdebug/format/pointer/scheme/fragment**, + **ethdebug/format/pointer/region** + - Producers: optional: a pointer may describe a value that no data location + stores as a virtual region. + - Consumers: required: a region may have `"location": "virtual"`. Its bytes + are the fragment of its `expression`'s value, which a consumer must + evaluate. A fragment outside the value's bytes is invalid. + ### Changed - The members of a `define` no longer depend on their order, since JSON diff --git a/packages/format/CHANGELOG.md b/packages/format/CHANGELOG.md index 58918d31d2..b9f3817cac 100644 --- a/packages/format/CHANGELOG.md +++ b/packages/format/CHANGELOG.md @@ -6,6 +6,13 @@ tracked in the root [`CHANGELOG.md`](../../CHANGELOG.md). ## Unreleased +### Added + +- The **ethdebug/format/pointer/region/virtual** and + **ethdebug/format/pointer/scheme/fragment** schemas, with the types + `Pointer.Region.Virtual` and `Pointer.Scheme.Fragment` and their guards + `Pointer.Region.isVirtual` and `Pointer.Scheme.isFragment`. + ### Changed - `schemas` parses each schema from YAML on first access instead of at diff --git a/packages/pointers/CHANGELOG.md b/packages/pointers/CHANGELOG.md index bf9623c77c..86a1227f7d 100644 --- a/packages/pointers/CHANGELOG.md +++ b/packages/pointers/CHANGELOG.md @@ -6,6 +6,13 @@ itself are tracked in the root [`CHANGELOG.md`](../../CHANGELOG.md). ## Unreleased +### Added + +- `dereference` and `read` support virtual regions. A concrete virtual + region keeps the bytes of its `expression` and has `offset` and `length` + filled in with their defaults. An expression with no width, or a fragment + outside the value, throws. + ### Changed - `dereference` evaluates the members of a scope's `define` in dependency From 5aa8a7b12c3b8ed3c595a59095834f363b53c3e2 Mon Sep 17 00:00:00 2001 From: "g. nicholas d'andrea" Date: Tue, 10 Nov 2026 19:08:05 -0500 Subject: [PATCH 3/6] Link the virtual regions changelog entries to #411 --- CHANGELOG.md | 3 ++- packages/format/CHANGELOG.md | 3 ++- packages/pointers/CHANGELOG.md | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 431488ff29..be55167952 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,7 +58,7 @@ The sections do not signal obligations; the prefixes do. - A region with `"location": "virtual"` holds the bytes of a computed value, e.g., a variable the optimizer removed. It uses the new `fragment` addressing scheme: a width-bearing `expression` supplies the bytes, and - optional `offset` and `length` select a range of them. + optional `offset` and `length` select a range of them ([#411]). - Schemas: **ethdebug/format/pointer/region/virtual**, **ethdebug/format/pointer/scheme/fragment**, **ethdebug/format/pointer/region** @@ -778,3 +778,4 @@ First published version of the specification. [#402]: https://github.com/ethdebug/format/pull/402 [#404]: https://github.com/ethdebug/format/pull/404 [#408]: https://github.com/ethdebug/format/pull/408 +[#411]: https://github.com/ethdebug/format/pull/411 diff --git a/packages/format/CHANGELOG.md b/packages/format/CHANGELOG.md index b9f3817cac..0241128a54 100644 --- a/packages/format/CHANGELOG.md +++ b/packages/format/CHANGELOG.md @@ -11,7 +11,7 @@ tracked in the root [`CHANGELOG.md`](../../CHANGELOG.md). - The **ethdebug/format/pointer/region/virtual** and **ethdebug/format/pointer/scheme/fragment** schemas, with the types `Pointer.Region.Virtual` and `Pointer.Scheme.Fragment` and their guards - `Pointer.Region.isVirtual` and `Pointer.Scheme.isFragment`. + `Pointer.Region.isVirtual` and `Pointer.Scheme.isFragment` ([#411]). ### Changed @@ -163,6 +163,7 @@ First publication. [#323]: https://github.com/ethdebug/format/pull/323 [#325]: https://github.com/ethdebug/format/pull/325 [#373]: https://github.com/ethdebug/format/pull/373 +[#411]: https://github.com/ethdebug/format/pull/411 [`0ef2f37`]: https://github.com/ethdebug/format/commit/0ef2f37 [`10ab103`]: https://github.com/ethdebug/format/commit/10ab103 [`21e532e`]: https://github.com/ethdebug/format/commit/21e532e diff --git a/packages/pointers/CHANGELOG.md b/packages/pointers/CHANGELOG.md index 86a1227f7d..ef4a7e46b5 100644 --- a/packages/pointers/CHANGELOG.md +++ b/packages/pointers/CHANGELOG.md @@ -11,7 +11,7 @@ itself are tracked in the root [`CHANGELOG.md`](../../CHANGELOG.md). - `dereference` and `read` support virtual regions. A concrete virtual region keeps the bytes of its `expression` and has `offset` and `length` filled in with their defaults. An expression with no width, or a fragment - outside the value, throws. + outside the value, throws ([#411]). ### Changed @@ -162,3 +162,4 @@ First publication. [#323]: https://github.com/ethdebug/format/pull/323 [#325]: https://github.com/ethdebug/format/pull/325 [#408]: https://github.com/ethdebug/format/pull/408 +[#411]: https://github.com/ethdebug/format/pull/411 From 112599a8310b27d899db4813fa2f3099ff073b10 Mon Sep 17 00:00:00 2001 From: "g. nicholas d'andrea" Date: Tue, 10 Nov 2026 19:47:08 -0500 Subject: [PATCH 4/6] format: let pointer collections carry a name Any collection may have a name. A collection's name scopes the regions inside it in the result of the pointer, and a named list also scopes each element by its index. Expressions do not see collection names. The reference implementation gives each region a `path`: the names of the named collections that contain it, outermost first, with the element index after each named list's name. --- .../format/src/types/pointer/pointer.test.ts | 35 ++++ packages/format/src/types/pointer/pointer.ts | 49 +++--- packages/pointers/src/cursor.ts | 12 ++ packages/pointers/src/dereference/generate.ts | 15 ++ .../pointers/src/dereference/index.test.ts | 156 ++++++++++++++++++ packages/pointers/src/dereference/memo.ts | 35 +++- packages/pointers/src/dereference/process.ts | 28 +++- packages/pointers/src/dereference/region.ts | 2 +- packages/pointers/src/read.ts | 24 +-- packages/pointers/test/test-cases.ts | 63 +++++++ .../core-schemas/pointers/collections.mdx | 24 +++ .../dereference-logic/generating-regions.mdx | 14 +- .../pointers/types/cursors.mdx | 6 + schemas/pointer.schema.yaml | 42 +++++ schemas/pointer/collection.schema.yaml | 30 ++++ .../collection/conditional.schema.yaml | 3 + schemas/pointer/collection/group.schema.yaml | 13 ++ schemas/pointer/collection/list.schema.yaml | 16 ++ .../pointer/collection/reference.schema.yaml | 9 + schemas/pointer/collection/scope.schema.yaml | 3 + .../pointer/collection/templates.schema.yaml | 3 + 21 files changed, 540 insertions(+), 42 deletions(-) diff --git a/packages/format/src/types/pointer/pointer.test.ts b/packages/format/src/types/pointer/pointer.test.ts index eb124e4895..b25ac3ea2b 100644 --- a/packages/format/src/types/pointer/pointer.test.ts +++ b/packages/format/src/types/pointer/pointer.test.ts @@ -172,6 +172,22 @@ testSchemaGuards("ethdebug/format/pointer", [ schema: "schema:ethdebug/format/pointer/collection/list", guard: Pointer.Collection.isList, }, + { + schema: "schema:ethdebug/format/pointer/collection/conditional", + guard: Pointer.Collection.isConditional, + }, + { + schema: "schema:ethdebug/format/pointer/collection/scope", + guard: Pointer.Collection.isScope, + }, + { + schema: "schema:ethdebug/format/pointer/collection/reference", + guard: Pointer.Collection.isReference, + }, + { + schema: "schema:ethdebug/format/pointer/collection/templates", + guard: Pointer.Collection.isTemplates, + }, { schema: "schema:ethdebug/format/pointer", guard: isPointer, @@ -181,3 +197,22 @@ testSchemaGuards("ethdebug/format/pointer", [ guard: Pointer.isTemplate, }, ] as const); + +describe("collection names", () => { + const collections = [ + { group: [{ location: "stack", slot: 0 }] }, + { list: { count: 1, each: "i", is: { location: "stack", slot: "i" } } }, + { if: 1, then: { location: "stack", slot: 0 } }, + { define: { a: 0 }, in: { location: "stack", slot: "a" } }, + { template: "t" }, + { templates: {}, in: { location: "stack", slot: 0 } }, + ]; + + it.each(collections)("accepts an identifier name on %j", (collection) => { + expect(Pointer.isCollection({ name: "c", ...collection })).toBe(true); + }); + + it.each(collections)("rejects a non-identifier name on %j", (collection) => { + expect(Pointer.isCollection({ name: "0c", ...collection })).toBe(false); + }); +}); diff --git a/packages/format/src/types/pointer/pointer.ts b/packages/format/src/types/pointer/pointer.ts index baf15b387d..f4e15ef847 100644 --- a/packages/format/src/types/pointer/pointer.ts +++ b/packages/format/src/types/pointer/pointer.ts @@ -140,19 +140,33 @@ export namespace Pointer { ].some((guard) => guard(value)); export namespace Collection { - export interface Group { - group: Pointer[]; + /** + * Any collection may have a name, which scopes the regions inside it + * in the result of the pointer + */ + export interface Base { + name?: Identifier; } - export const isGroup = (value: unknown): value is Group => + export const isBase = (value: unknown): value is Base => !!value && typeof value === "object" && - Object.keys(value).length === 1 && + (!("name" in value) || isIdentifier(value.name)); + + const hasOnlyKeys = (value: object, keys: string[]) => + Object.keys(value).every((key) => key === "name" || keys.includes(key)); + + export interface Group extends Base { + group: Pointer[]; + } + export const isGroup = (value: unknown): value is Group => + isBase(value) && + hasOnlyKeys(value, ["group"]) && "group" in value && Array.isArray(value.group) && value.group.length >= 1 && value.group.every(isPointer); - export interface List { + export interface List extends Base { list: { count: Expression; each: Identifier; @@ -160,9 +174,8 @@ export namespace Pointer { }; } export const isList = (value: unknown): value is List => - !!value && - typeof value === "object" && - Object.keys(value).length === 1 && + isBase(value) && + hasOnlyKeys(value, ["list"]) && "list" in value && !!value.list && typeof value.list === "object" && @@ -174,21 +187,20 @@ export namespace Pointer { "is" in value.list && isPointer(value.list.is); - export interface Conditional { + export interface Conditional extends Base { if: Expression; then: Pointer; else?: Pointer; } export const isConditional = (value: unknown): value is Conditional => - !!value && - typeof value === "object" && + isBase(value) && "if" in value && isExpression(value.if) && "then" in value && isPointer(value.then) && (!("else" in value) || isPointer(value.else)); - export interface Scope { + export interface Scope extends Base { define: { [identifier: string]: Expression; }; @@ -196,8 +208,7 @@ export namespace Pointer { } export const isScope = (value: unknown): value is Scope => - !!value && - typeof value === "object" && + isBase(value) && "define" in value && typeof value.define === "object" && !!value.define && @@ -205,14 +216,13 @@ export namespace Pointer { "in" in value && isPointer(value.in); - export interface Reference { + export interface Reference extends Base { template: string; yields?: Record; } export const isReference = (value: unknown): value is Reference => - !!value && - typeof value === "object" && + isBase(value) && "template" in value && typeof value.template === "string" && !!value.template && @@ -223,7 +233,7 @@ export namespace Pointer { ([k, v]) => isIdentifier(k) && isIdentifier(v), ))); - export interface Templates { + export interface Templates extends Base { templates: { [identifier: string]: Pointer.Template; }; @@ -231,8 +241,7 @@ export namespace Pointer { } export const isTemplates = (value: unknown): value is Templates => - !!value && - typeof value === "object" && + isBase(value) && "templates" in value && typeof value.templates === "object" && !!value.templates && diff --git a/packages/pointers/src/cursor.ts b/packages/pointers/src/cursor.ts index 53b6e925be..74e04599f1 100644 --- a/packages/pointers/src/cursor.ts +++ b/packages/pointers/src/cursor.ts @@ -41,8 +41,20 @@ export namespace Cursor { ? Data | undefined : R[K] : R[K]; + } & { + /** + * The names of the named collections that contain this region, + * outermost first. A named list adds its name and then the index of + * the element. Absent when no named collection contains the region. + */ + path?: Cursor.Path; }; + /** + * A list of collection names and named list element indexes + */ + export type Path = (string | number)[]; + /** * A collection of concrete regions. * diff --git a/packages/pointers/src/dereference/generate.ts b/packages/pointers/src/dereference/generate.ts index 80fea15093..7644a3ea0c 100644 --- a/packages/pointers/src/dereference/generate.ts +++ b/packages/pointers/src/dereference/generate.ts @@ -38,6 +38,9 @@ export async function* generateRegions( // Stack of template definitions for inline templates const templatesStack: Array = []; + // Names of the enclosing named collections (and named list indexes) + const path: Cursor.Path = []; + const stack: Memo[] = [Memo.dereferencePointer(pointer)]; while (stack.length > 0) { const memo: Memo = stack.pop() as Memo; @@ -72,6 +75,10 @@ export async function* generateRegions( } } + if (path.length > 0) { + region = { ...region, path: [...path] }; + } + yield region; result = await process.next(); } @@ -121,6 +128,14 @@ export async function* generateRegions( templatesStack.pop(); break; } + case "push-path": { + path.push(memo.segment); + break; + } + case "pop-path": { + path.pop(); + break; + } } // add new memos to the stack in reverse order diff --git a/packages/pointers/src/dereference/index.test.ts b/packages/pointers/src/dereference/index.test.ts index bb7b525585..42eb6e7421 100644 --- a/packages/pointers/src/dereference/index.test.ts +++ b/packages/pointers/src/dereference/index.test.ts @@ -1064,4 +1064,160 @@ describe("dereference", () => { ); }); }); + + describe("collection names", () => { + const slot = (value: Pointer.Expression) => + ({ location: "stack", slot: value }) as const; + + // the name and path of each output region + const pathsOf = async (pointer: Pointer) => { + const { regions } = await (await dereference(pointer)).view(state); + return regions.map(({ name, path }) => ({ name, path })); + }; + + it("adds the name of a group to the path of its regions", async () => { + expect( + await pathsOf({ + name: "g", + group: [ + { name: "a", ...slot(0) }, + { name: "b", ...slot(1) }, + ], + }), + ).toEqual([ + { name: "a", path: ["g"] }, + { name: "b", path: ["g"] }, + ]); + }); + + it("adds nested names outermost first", async () => { + expect( + await pathsOf({ + name: "outer", + group: [ + { name: "a", ...slot(0) }, + { name: "inner", group: [{ name: "b", ...slot(1) }] }, + ], + }), + ).toEqual([ + { name: "a", path: ["outer"] }, + { name: "b", path: ["outer", "inner"] }, + ]); + }); + + it("adds the element index after the name of a list", async () => { + expect( + await pathsOf({ + name: "items", + list: { + count: 2, + each: "i", + is: { name: "elem", group: [{ name: "a", ...slot("i") }] }, + }, + }), + ).toEqual([ + { name: "a", path: ["items", 0, "elem"] }, + { name: "a", path: ["items", 1, "elem"] }, + ]); + }); + + it("adds the element index when the list's `is` has no name", async () => { + expect( + await pathsOf({ + name: "items", + list: { count: 2, each: "i", is: { name: "a", ...slot("i") } }, + }), + ).toEqual([ + { name: "a", path: ["items", 0] }, + { name: "a", path: ["items", 1] }, + ]); + }); + + it("adds nothing for an unnamed list", async () => { + expect( + await pathsOf({ + list: { count: 2, each: "i", is: { name: "a", ...slot("i") } }, + }), + ).toEqual([ + { name: "a", path: undefined }, + { name: "a", path: undefined }, + ]); + }); + + it("adds the names of a conditional, a scope and templates", async () => { + expect( + await pathsOf({ + name: "t", + templates: {}, + in: { + name: "s", + define: { v: 0 }, + in: { name: "c", if: 1, then: { name: "a", ...slot("v") } }, + }, + }), + ).toEqual([{ name: "a", path: ["t", "s", "c"] }]); + }); + + it("tells apart two uses of one template by named references", async () => { + const { examples } = schemas["schema:ethdebug/format/pointer"] as { + examples: Pointer[]; + }; + const example = examples.find((pointer) => + JSON.stringify(pointer).includes("line-storage-contract-variable-slot"), + )!; + + const { regions } = await (await dereference(example)).view(state); + + expect([...regions]).toMatchObject([ + { name: "x", path: ["a"], slot: Data.fromNumber(0) }, + { name: "y", path: ["a"], slot: Data.fromNumber(0) }, + { name: "x", path: ["b"], slot: Data.fromNumber(1) }, + { name: "y", path: ["b"], slot: Data.fromNumber(1) }, + ]); + + // region-name lookup ignores paths: the last "x" is b's + expect(regions.named("x")).toHaveLength(2); + expect(regions.lookup["x"]).toMatchObject({ slot: Data.fromNumber(1) }); + }); + + it("does not rename collection names with yields", async () => { + const templates: Pointer.Templates = { + t: { expect: [], for: { name: "inner", group: [slot(0)] } }, + }; + const cursor = await dereference( + { name: "ref", template: "t", yields: { inner: "renamed" } }, + { templates }, + ); + const { regions } = await cursor.view(state); + expect(regions[0].path).toEqual(["ref", "inner"]); + }); + + it("hides collection names from expressions", async () => { + const cursor = await dereference({ + group: [ + { name: "g", group: [{ name: "a", ...slot(0) }] }, + { location: "memory", offset: { "~read": "g" }, length: 1 }, + ], + }); + await expect(cursor.view(state)).rejects.toThrow("Region not found: g"); + }); + + it("leaves region-name references within a named collection", async () => { + const { regions } = await ( + await dereference({ + name: "g", + group: [ + { name: "a", location: "memory", offset: 0x40, length: 0x20 }, + { + name: "b", + location: "memory", + offset: { ".offset": "a" }, + length: 1, + }, + ], + }) + ).view(state); + expect(regions.lookup["b"].offset).toEqual(Data.fromNumber(0x40)); + }); + }); }); diff --git a/packages/pointers/src/dereference/memo.ts b/packages/pointers/src/dereference/memo.ts index 03633b6868..00d80e7cc1 100644 --- a/packages/pointers/src/dereference/memo.ts +++ b/packages/pointers/src/dereference/memo.ts @@ -13,7 +13,9 @@ export type Memo = | Memo.PushRegionRenames | Memo.PopRegionRenames | Memo.PushTemplates - | Memo.PopTemplates; + | Memo.PopTemplates + | Memo.PushPath + | Memo.PopPath; export namespace Memo { /** @@ -163,4 +165,35 @@ export namespace Memo { export const popTemplates = (): PopTemplates => ({ kind: "pop-templates", }); + + /** + * A request to push a segment onto the path that output regions carry: + * a collection name, or the index of an element of a named list + */ + export interface PushPath { + kind: "push-path"; + segment: string | number; + } + + /** + * Initialize a PushPath memo + */ + export const pushPath = (segment: string | number): PushPath => ({ + kind: "push-path", + segment, + }); + + /** + * A request to pop the last segment from the path + */ + export interface PopPath { + kind: "pop-path"; + } + + /** + * Initialize a PopPath memo + */ + export const popPath = (): PopPath => ({ + kind: "pop-path", + }); } diff --git a/packages/pointers/src/dereference/process.ts b/packages/pointers/src/dereference/process.ts index fc9d6b7bd7..62f4200cc5 100644 --- a/packages/pointers/src/dereference/process.ts +++ b/packages/pointers/src/dereference/process.ts @@ -38,7 +38,20 @@ export async function* processPointer( } const collection = pointer; + const memos = yield* processCollection(collection, options); + // a collection's name scopes the regions inside it in the output + if (typeof collection.name === "string") { + return [Memo.pushPath(collection.name), ...memos, Memo.popPath()]; + } + + return memos; +} + +async function* processCollection( + collection: Pointer.Collection, + options: ProcessOptions, +): Process { if (Pointer.Collection.isGroup(collection)) { return yield* processGroup(collection, options); } @@ -63,7 +76,7 @@ export async function* processPointer( return yield* processTemplates(collection, options); } - console.error("%s", JSON.stringify(pointer, undefined, 2)); + console.error("%s", JSON.stringify(collection, undefined, 2)); throw new Error("Unexpected unknown kind of pointer"); } @@ -97,7 +110,7 @@ async function* processList( collection: Pointer.Collection.List, options: ProcessOptions, ): Process { - const { list } = collection; + const { name, list } = collection; const { count: countExpression, each, is } = list; const count = Value.toInteger(await evaluate(countExpression, options)); @@ -110,7 +123,16 @@ async function* processList( }), ); - memos.push(Memo.dereferencePointer(is)); + // a named list scopes each element apart by its index + if (typeof name === "string") { + memos.push( + Memo.pushPath(Number(index)), + Memo.dereferencePointer(is), + Memo.popPath(), + ); + } else { + memos.push(Memo.dereferencePointer(is)); + } } return memos; diff --git a/packages/pointers/src/dereference/region.ts b/packages/pointers/src/dereference/region.ts index a2d11c66d3..c28675b5d8 100644 --- a/packages/pointers/src/dereference/region.ts +++ b/packages/pointers/src/dereference/region.ts @@ -59,7 +59,7 @@ export async function evaluateRegion( ...options, regions: { ...options.regions, - "~this": partialRegion, + "~this": partialRegion as Cursor.Region, }, }); diff --git a/packages/pointers/src/read.ts b/packages/pointers/src/read.ts index e38bb028c5..a7c31cc8b7 100644 --- a/packages/pointers/src/read.ts +++ b/packages/pointers/src/read.ts @@ -1,4 +1,3 @@ -import { Pointer } from "@ethdebug/format"; import type { Machine } from "#machine"; import { Data } from "#data"; import type { Cursor } from "#cursor"; @@ -154,23 +153,18 @@ function slotAfter(slot: Data, carry: bigint): Data { return Data.fromUint(slot.asUint() + carry).padUntilAtLeast(slot.length); } -type DataProperties = { - [K in keyof Cursor.Region & - ("slot" | "offset" | "length")]: Cursor.Region[K]; -}; - -type PickDataPropertiesAsUints< - R extends Pointer.Region, - U extends keyof DataProperties, +type DataPropertiesAsUints< + T extends Cursor.Region, + U extends keyof T & ("slot" | "offset" | "length"), > = { - [K in U]: undefined extends Cursor.Region[K] ? bigint | undefined : bigint; + [K in U]: undefined extends T[K] ? bigint | undefined : bigint; }; function withPropertiesAsUints< - R extends Pointer.Region, - U extends keyof DataProperties, ->(uintKeys: U[], region: Cursor.Region): PickDataPropertiesAsUints { - const result: Partial> = {}; + T extends Cursor.Region, + U extends keyof T & ("slot" | "offset" | "length"), +>(uintKeys: U[], region: T): DataPropertiesAsUints { + const result: Partial> = {}; for (const key of uintKeys) { const data: Data | undefined = region[key] as Data | undefined; if (typeof data !== "undefined") { @@ -178,5 +172,5 @@ function withPropertiesAsUints< } } - return result as PickDataPropertiesAsUints; + return result as DataPropertiesAsUints; } diff --git a/packages/pointers/test/test-cases.ts b/packages/pointers/test/test-cases.ts index 32d439286a..91299de59c 100644 --- a/packages/pointers/test/test-cases.ts +++ b/packages/pointers/test/test-cases.ts @@ -274,6 +274,68 @@ const virtualRegionsTest: ObserveTraceTest<{ x: number; tag: string }> = { }, }; +/** + * the `struct Line { Point a; Point b; }` example uses one template for + * both members, so both give regions named "x" and "y"; each region's + * path tells which member it belongs to + */ +const namedReferencesTest: ObserveTraceTest<{ + a: { x: number; y: number }; + b: { x: number; y: number }; +}> = { + pointer: findExamplePointer("line-storage-contract-variable-slot"), + compileOptions: singleSourceCompilation({ + path: "LineStorage.sol", + contractName: "LineStorage", + content: `contract LineStorage { + Line line; + + constructor() { + line = Line({ a: Point({ x: 1, y: 2 }), b: Point({ x: 3, y: 4 }) }); + line.b.x = 5; + } + } + + struct Point { + uint8 x; + uint8 y; + } + + struct Line { + Point a; + Point b; + } + `, + }), + + expectedValues: [ + { a: { x: 0, y: 0 }, b: { x: 0, y: 0 } }, + { a: { x: 1, y: 2 }, b: { x: 3, y: 4 } }, + { a: { x: 1, y: 2 }, b: { x: 5, y: 4 } }, + ], + + async observe({ regions, read }) { + const field = async (member: string, name: string) => { + const region = regions.find( + (region) => + region.name === name && + region.path?.length === 1 && + region.path[0] === member, + )!; + return Number((await read(region)).asUint()); + }; + + return { + a: { x: await field("a", "x"), y: await field("a", "y") }, + b: { x: await field("b", "x"), y: await field("b", "y") }, + }; + }, + + equals(a, b) { + return JSON.stringify(a) === JSON.stringify(b); + }, +}; + /** * collection of descriptions of tests that compile+deploy Solidity code, * then step through the machine trace of that code's execution, watching @@ -289,4 +351,5 @@ export const observeTraceTests = { "string storage (multi-slot)": multiSlotStringStorageTest, "uint256[] memory": uint256ArrayMemoryTest, "virtual regions": virtualRegionsTest, + "struct storage (named references)": namedReferencesTest, }; diff --git a/packages/web/docs/core-schemas/pointers/collections.mdx b/packages/web/docs/core-schemas/pointers/collections.mdx index 24bc9abb12..86713ef102 100644 --- a/packages/web/docs/core-schemas/pointers/collections.mdx +++ b/packages/web/docs/core-schemas/pointers/collections.mdx @@ -136,6 +136,30 @@ Pointers within collections can include a `name` property. Named regions are tracked during resolution and can be referenced using the `.slot`, `.offset`, and `.length` syntax in expressions. +## Collection names + +Any collection can also have a `name`. A collection's name scopes the regions +inside it in the result of the pointer. This tells apart regions that have the +same name, such as the regions of two references to one template: + +```yaml +group: + - define: { "point-slot": 0 } + in: { name: "a", template: "point" } + - define: { "point-slot": 1 } + in: { name: "b", template: "point" } +``` + +Both references give regions named `x` and `y`. The regions of the first +reference are in the scope `a`, and the regions of the second are in the +scope `b`. A named list also scopes each element by its index, so the regions +of one element stay together. + +Collection names affect only the result. Expressions do not see them: `~read`, +`.offset`, `.length` and `.slot` refer to region names only. A consumer that +ignores collection names gets the same regions, with the same names. The +`yields` of a reference renames regions only, never collections. + ## Learn more - [Regions](./regions) for simple pointer definitions diff --git a/packages/web/docs/implementation-guides/pointers/dereference-logic/generating-regions.mdx b/packages/web/docs/implementation-guides/pointers/dereference-logic/generating-regions.mdx index 37cb80559a..7e5a009405 100644 --- a/packages/web/docs/implementation-guides/pointers/dereference-logic/generating-regions.mdx +++ b/packages/web/docs/implementation-guides/pointers/dereference-logic/generating-regions.mdx @@ -39,8 +39,8 @@ this function's `while()` loop operates on the stack, memos for saving new named regions or updating variable values may appear and then get handled appropriately. -For reference, see the `memo.ts` module for type definitions for each of the -three types of memo and their corresponding helper constructor functions. +For reference, see the `memo.ts` module for type definitions for each +kind of memo and its corresponding helper constructor function.
See the `memo.ts` module @@ -239,3 +239,13 @@ enclosing scope. If every remaining member fails in turn, they form a cycle. sourcePath="src/dereference/process.ts" extract={(sourceFile) => sourceFile.getFunction("processScope")} /> + +### Collection names + +`processPointer()` handles a collection's `name` for every kind of collection +at once: it surrounds the collection's memos with a `PushPath` memo for the +name and a `PopPath` memo after them. `generateRegions()` keeps the current +path and copies it onto each region it yields, when the path is not empty. +A named list also surrounds each element with a `PushPath` memo for the +element's index (see `processList()` above). Paths never reach the saved +regions or variables, so expressions do not see collection names. diff --git a/packages/web/docs/implementation-guides/pointers/types/cursors.mdx b/packages/web/docs/implementation-guides/pointers/types/cursors.mdx index f0d24bd66f..83315d13b5 100644 --- a/packages/web/docs/implementation-guides/pointers/types/cursors.mdx +++ b/packages/web/docs/implementation-guides/pointers/types/cursors.mdx @@ -46,6 +46,12 @@ can reference internal variables and other regions, a `Cursor.Region` represents the runtime result of evaluating all these expressions: a specific range of bytes in a specific data location. +A `Cursor.Region` also has a `path` when named collections contain it: the +names of those collections, outermost first. A named list adds its name and +then the index of the element, so a list named `items` whose `is` is a group +named `elem` gives the paths `["items", 0, "elem"]`, `["items", 1, "elem"]`, +and so on. Regions that no named collection contains have no `path`. + The full listing of `namespace Cursor` follows: Date: Tue, 10 Nov 2026 19:47:12 -0500 Subject: [PATCH 5/6] Add changelog entries for collection names --- CHANGELOG.md | 19 +++++++++++++++++++ packages/format/CHANGELOG.md | 3 +++ packages/pointers/CHANGELOG.md | 5 +++++ 3 files changed, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index be55167952..52ea52846e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,25 @@ The sections do not signal obligations; the prefixes do. are the fragment of its `expression`'s value, which a consumer must evaluate. A fragment outside the value's bytes is invalid. +- Any pointer collection may have a `name`. A collection's name scopes the + regions inside it in the result of the pointer, and a named list also scopes + each element by its index. This tells apart regions with the same name, such + as those of two references to one template, without `yields`. Expressions do + not see collection names. + - Schemas: **ethdebug/format/pointer/collection**, + **ethdebug/format/pointer/collection/group**, + **ethdebug/format/pointer/collection/list**, + **ethdebug/format/pointer/collection/conditional**, + **ethdebug/format/pointer/collection/scope**, + **ethdebug/format/pointer/collection/reference**, + **ethdebug/format/pointer/collection/templates** + - Producers: optional: a pointer may name its collections to scope their + regions. + - Consumers: required: accept a `name` on any collection. The collection + schemas are closed objects (`additionalProperties: false`), so the previous + version's schemas reject it. A consumer may ignore the name: the regions + and their names do not change. + ### Changed - The members of a `define` no longer depend on their order, since JSON diff --git a/packages/format/CHANGELOG.md b/packages/format/CHANGELOG.md index 0241128a54..98b25a0b51 100644 --- a/packages/format/CHANGELOG.md +++ b/packages/format/CHANGELOG.md @@ -12,6 +12,9 @@ tracked in the root [`CHANGELOG.md`](../../CHANGELOG.md). **ethdebug/format/pointer/scheme/fragment** schemas, with the types `Pointer.Region.Virtual` and `Pointer.Scheme.Fragment` and their guards `Pointer.Region.isVirtual` and `Pointer.Scheme.isFragment` ([#411]). +- `Pointer.Collection.Base`, the optional `name` of every kind of + collection, with its guard `Pointer.Collection.isBase`. Each collection + guard accepts a `name` that is an identifier. ### Changed diff --git a/packages/pointers/CHANGELOG.md b/packages/pointers/CHANGELOG.md index ef4a7e46b5..ab1ea96074 100644 --- a/packages/pointers/CHANGELOG.md +++ b/packages/pointers/CHANGELOG.md @@ -12,6 +12,11 @@ itself are tracked in the root [`CHANGELOG.md`](../../CHANGELOG.md). region keeps the bytes of its `expression` and has `offset` and `length` filled in with their defaults. An expression with no width, or a fragment outside the value, throws ([#411]). +- `Cursor.Region` has an optional `path`: the names of the named collections + that contain the region, outermost first. A named list adds its name and + then the element index. `dereference` omits `path` when no named + collection contains the region, so pointers without collection names give + the same regions as before. ### Changed From de9fc9601572d05a4d4ff1c8a54bdecc2e63ae7a Mon Sep 17 00:00:00 2001 From: "g. nicholas d'andrea" Date: Tue, 10 Nov 2026 19:47:42 -0500 Subject: [PATCH 6/6] Link the collection names changelog entries to #412 --- CHANGELOG.md | 3 ++- packages/format/CHANGELOG.md | 3 ++- packages/pointers/CHANGELOG.md | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52ea52846e..56f08693f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,7 +72,7 @@ The sections do not signal obligations; the prefixes do. regions inside it in the result of the pointer, and a named list also scopes each element by its index. This tells apart regions with the same name, such as those of two references to one template, without `yields`. Expressions do - not see collection names. + not see collection names ([#412]). - Schemas: **ethdebug/format/pointer/collection**, **ethdebug/format/pointer/collection/group**, **ethdebug/format/pointer/collection/list**, @@ -798,3 +798,4 @@ First published version of the specification. [#404]: https://github.com/ethdebug/format/pull/404 [#408]: https://github.com/ethdebug/format/pull/408 [#411]: https://github.com/ethdebug/format/pull/411 +[#412]: https://github.com/ethdebug/format/pull/412 diff --git a/packages/format/CHANGELOG.md b/packages/format/CHANGELOG.md index 98b25a0b51..71aa3ac8fb 100644 --- a/packages/format/CHANGELOG.md +++ b/packages/format/CHANGELOG.md @@ -14,7 +14,7 @@ tracked in the root [`CHANGELOG.md`](../../CHANGELOG.md). `Pointer.Region.isVirtual` and `Pointer.Scheme.isFragment` ([#411]). - `Pointer.Collection.Base`, the optional `name` of every kind of collection, with its guard `Pointer.Collection.isBase`. Each collection - guard accepts a `name` that is an identifier. + guard accepts a `name` that is an identifier ([#412]). ### Changed @@ -167,6 +167,7 @@ First publication. [#325]: https://github.com/ethdebug/format/pull/325 [#373]: https://github.com/ethdebug/format/pull/373 [#411]: https://github.com/ethdebug/format/pull/411 +[#412]: https://github.com/ethdebug/format/pull/412 [`0ef2f37`]: https://github.com/ethdebug/format/commit/0ef2f37 [`10ab103`]: https://github.com/ethdebug/format/commit/10ab103 [`21e532e`]: https://github.com/ethdebug/format/commit/21e532e diff --git a/packages/pointers/CHANGELOG.md b/packages/pointers/CHANGELOG.md index ab1ea96074..906346784e 100644 --- a/packages/pointers/CHANGELOG.md +++ b/packages/pointers/CHANGELOG.md @@ -16,7 +16,7 @@ itself are tracked in the root [`CHANGELOG.md`](../../CHANGELOG.md). that contain the region, outermost first. A named list adds its name and then the element index. `dereference` omits `path` when no named collection contains the region, so pointers without collection names give - the same regions as before. + the same regions as before ([#412]). ### Changed @@ -168,3 +168,4 @@ First publication. [#325]: https://github.com/ethdebug/format/pull/325 [#408]: https://github.com/ethdebug/format/pull/408 [#411]: https://github.com/ethdebug/format/pull/411 +[#412]: https://github.com/ethdebug/format/pull/412