Skip to content

bugc: copy all of a slice's bytes - #359

Closed
gnidan wants to merge 2 commits into
mainfrom
bugc-slice-copy
Closed

gnidan wants to merge 2 commits into
mainfrom
bugc-slice-copy

Conversation

@gnidan

@gnidan gnidan commented Oct 7, 2026

Copy link
Copy Markdown
Member

A slice copied only its first 32 bytes. msg.data[4:] with 100 bytes of calldata, or m[1:40] of a longer memory bytes, had the right length but zeros after byte 32. irgen built the slice's data as a read from the source and a write to the new memory, and a read or write moves at most one word.

This adds a copy IR instruction: copy length bytes from source in memory or calldata to memory at offset. It compiles to MCOPY from memory and CALLDATACOPY from calldata, so a slice costs the same code at any length. A slice now emits copy in place of the read and write. The optimizer passes, liveness and memory analysis treat it as a memory write with three operands, and the IR formatter and the bugc-react IR view print it as memory[offset: %d, length: %n] = copy calldata[%s].

The new test slices msg.data and a memory bytes to 0, 31, 32, 33 and 100 bytes at levels 0 to 3, and reads every byte back. A new local-variable soundness case has a 34-byte slice local, whose pointer did not read the program's value before this change. Bytecode and debug info change only for the programs in examples/ that slice.

gnidan added a commit that referenced this pull request Oct 7, 2026
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 2 commits October 6, 2026 22:00
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.
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-slice-copy 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