Skip to content

Review finding: P2MR runtime stack counts items, not bytes #257

Closed

Description

@djh58

Summary

A P2MR input can expand a small initial witness into a much larger runtime stack.

Observed

The P2MR runtime limit checks the number of stack items (1,000), not their total bytes (src/script/interpreter.cpp:1388). OP_DUP copies a whole value, so a 16 KiB initial item duplicated to 1,000 items is ~16 MiB. A 2 MB block can carry ~110 such inputs; with -par workers this is a ~336 MiB memory spike during block connection. Mempool admission gets the same per-transaction pressure.

Impact

Small-memory nodes can be pushed into memory pressure while connecting a valid block.

Suggested fix

Add a byte cap on stack + altstack after each op (1 MiB is a safe compatibility choice), or charge validation weight for bytes copied by P2MR stack ops. Add a test for the OP_DUP/OP_DROP leaf.

Evidence

src/script/interpreter.cpp:37-47,916-926,1388. Scratch test p2mr_stack_dup_runtime_bytes_amplification passes; single-input RSS rose from ~20.5 MiB to ~35.6 MiB.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions