Skip to content

bugc: store a memory string's bytes when assigning it to storage - #355

Closed
gnidan wants to merge 3 commits into
mainfrom
bugc-string-to-storage
Closed

gnidan wants to merge 3 commits into
mainfrom
bugc-string-to-storage

Conversation

@gnidan

@gnidan gnidan commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Assigning a memory string or bytes to a storage variable stored the value's memory address in the slot, not its bytes. For example, with storage { [0] s: string; }, both s = m; and s = "hello"; left a pointer such as 0xe0 in slot 0. A storage write took every value as a word, and a string or bytes value is a reference to its length and data in memory.

Code generation now recognizes a storage write whose value is a memory reference to a string or dynamic bytes, and stores the referenced bytes as Solidity encodes them:

  • Up to 31 bytes: the data, left-aligned, in the slot itself, with length * 2 in the low byte.
  • 32 bytes or more: length * 2 + 1 in the slot, and the data in the slots from keccak256(slot).

The bytes after the end of the data, in its last word, are stored as zero. (Memory past the end of the data can hold other bytes: on main, a slice copies a whole word, so its last word holds the source's next bytes.) This holds for any storage write of such a value, such as a mapping value (u[7] = m;), which the test also covers.

A slice's IR type now carries bytes as its origin (it was synthetic), so a slice assigned straight to storage is recognized too.

The new test stores strings and bytes of 0, 5, 31, 32 and 70 bytes at levels 0 to 3 and reads every slot back. A new local-variable soundness case runs a program with locals live across the copy loop. Bytecode and debug info for every program in examples/ are unchanged at all levels.

gnidan added a commit that referenced this pull request Oct 7, 2026
@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-10-07 03:12 UTC

gnidan added a commit that referenced this pull request Oct 7, 2026
@gnidan
gnidan force-pushed the bugc-string-to-storage branch from f9cbf37 to 8fe2d51 Compare October 7, 2026 01:42
gnidan added 3 commits October 6, 2026 22:00
A storage write took its value as a word, so a memory string or bytes
reference wrote its memory address into the slot. Code generation now
encodes the referenced bytes as Solidity does: inline with length * 2
up to 31 bytes, else length * 2 + 1 in the slot and the data from
keccak256(slot), with the last word's trailing bytes cleared. A slice
now carries the bytes type, so its value is recognized too.
@gnidan
gnidan force-pushed the bugc-string-to-storage branch from 8fe2d51 to 82f5e0a Compare October 7, 2026 02:05
gnidan added a commit that referenced this pull request Oct 7, 2026
* bugc: compile `!` as a logical not

* bugc: link the changelog entry to #353

* bugc: end each source range at its last token

* bugc: link the changelog entry to #362

* bugc: add block.prevrandao

* bugc: link the changelog entries to #363

* bugc: hash several value-type words with keccak256

* bugc: link the changelog entries to #365

* bugc: slice a fixed-size bytes value by its bytes

* bugc: link the changelog entry to #357

* bugc: hash the data of dynamic bytes and strings

* bugc: link the changelog entry to #360

* bugc: give a caller's call JUMP the invoke's arguments

* bugc: list variables before a function's first statement

* bugc: store a memory string's bytes when assigning it to storage

A storage write took its value as a word, so a memory string or bytes
reference wrote its memory address into the slot. Code generation now
encodes the referenced bytes as Solidity does: inline with length * 2
up to 31 bytes, else length * 2 + 1 in the slot and the data from
keccak256(slot), with the last word's trailing bytes cleared. A slice
now carries the bytes type, so its value is recognized too.

* bugc: link the changelog entry to #355

* bugc: say why the bytes test slices without naming the copy width

* bugc: copy all of a slice's bytes

irgen built a slice's data as a read and a write, and a read or write
moves at most one word, so only the first 32 bytes were copied. Add a
copy IR instruction (CALLDATACOPY from calldata, MCOPY from memory) and
emit it for the slice's data.

* bugc: link the changelog entries to #359

* bugc: copy a struct read from storage into memory

* bugc: link the changelog entries to #361

* bugc: lay storage arrays out as Solidity does

* bugc: add push to dynamic arrays in storage
@gnidan

gnidan commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

Included in #368.

@gnidan gnidan closed this Oct 7, 2026
@gnidan
gnidan deleted the bugc-string-to-storage branch October 7, 2026 03:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant