Skip to content

Commit

Permalink
asm: fix bug in PC assignment of push 0
Browse files Browse the repository at this point in the history
  • Loading branch information
fjl committed Nov 23, 2024
1 parent 2016511 commit 45222b9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
10 changes: 3 additions & 7 deletions asm/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,8 @@ func (c *Compiler) generateOutput(prog *compilerProg) []byte {

// resolve the op
var op *evm.Op
var size = inst.pushSize
if inst.op == "PUSH" {
if size == 0 && !prog.evm.SupportsPush0() {
size = 1
}
op = prog.evm.PushBySize(size)
op = prog.evm.PushBySize(inst.pushSize)
} else {
op = prog.evm.OpByName(inst.op)
}
Expand All @@ -316,8 +312,8 @@ func (c *Compiler) generateOutput(prog *compilerProg) []byte {

// Add opcode and data padding to output.
output = append(output, op.Code)
if len(inst.data) < size {
output = append(output, make([]byte, size-len(inst.data))...)
if len(inst.data) < inst.pushSize {
output = append(output, make([]byte, inst.pushSize-len(inst.data))...)
}

case inst.op != "":
Expand Down
13 changes: 8 additions & 5 deletions asm/compiler_eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *Compiler) assignInitialPushSizes(e *evaluator, prog *compilerProg) {
c.addError(inst.ast, err)
continue
}
if err := c.assignPushArg(inst, v, true); err != nil {
if err := prog.assignPushArg(inst, v, true); err != nil {
c.addError(inst.ast, err)
continue
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func (c *Compiler) assignArgs(e *evaluator, prog *compilerProg) (inst *instructi
if err != nil {
return inst, err
}
if err := c.assignPushArg(inst, v, false); err != nil {
if err := prog.assignPushArg(inst, v, false); err != nil {
return inst, err
}
}
Expand All @@ -103,7 +103,7 @@ func (c *Compiler) assignArgs(e *evaluator, prog *compilerProg) (inst *instructi
//
// If setSize is true, the pushSize of variable-size "PUSH" instructions will be assigned
// based on the value.
func (c *Compiler) assignPushArg(inst *instruction, v *big.Int, setSize bool) error {
func (prog *compilerProg) assignPushArg(inst *instruction, v *big.Int, setSize bool) error {
if v.Sign() < 0 {
return ecNegativeResult
}
Expand All @@ -115,7 +115,7 @@ func (c *Compiler) assignPushArg(inst *instruction, v *big.Int, setSize bool) er

_, hasExplicitSize := inst.explicitPushSize()
if setSize && !hasExplicitSize {
inst.pushSize = c.autoPushSize(b)
inst.pushSize = prog.autoPushSize(b)
}
if len(b) > inst.pushSize {
if !hasExplicitSize {
Expand All @@ -130,9 +130,12 @@ func (c *Compiler) assignPushArg(inst *instruction, v *big.Int, setSize bool) er
return nil
}

func (c *Compiler) autoPushSize(value []byte) int {
func (prog *compilerProg) autoPushSize(value []byte) int {
if len(value) > 32 {
panic("value too big")
}
if len(value) == 0 && !prog.evm.SupportsPush0() {
return 1
}
return len(value)
}

0 comments on commit 45222b9

Please sign in to comment.