Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic teal #2126

Merged
merged 19 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
check back branches for alignment
  • Loading branch information
jannotti committed May 6, 2021
commit fbc896b9cf6c377271481b695d0a674c5141a054
12 changes: 9 additions & 3 deletions data/transactions/logic/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,8 @@ func check(program []byte, params EvalParams) (cost int, err error) {
cx.pc = vlen
cx.EvalParams = params
cx.program = program
cx.branchTargets = make(map[int]bool)
cx.instructionStarts = make(map[int]bool)

for cx.pc < len(cx.program) {
prevpc := cx.pc
Expand Down Expand Up @@ -635,6 +637,7 @@ func (cx *evalContext) step() {
}

func (cx *evalContext) checkStep() error {
cx.instructionStarts[cx.pc] = true
opcode := cx.program[cx.pc]
spec := &opsByOpcode[cx.version][opcode]
if spec.op == nil {
Expand Down Expand Up @@ -666,7 +669,7 @@ func (cx *evalContext) checkStep() error {
if cx.Trace != nil {
fmt.Fprintf(cx.Trace, "%3d %s\n", prevpc, spec.Name)
}
if cx.err == nil && cx.branchTargets != nil {
if cx.err == nil {
for pc := prevpc + 1; pc < cx.pc; pc++ {
if _, ok := cx.branchTargets[pc]; ok {
return fmt.Errorf("branch target %d is not an aligned instruction", pc)
Expand Down Expand Up @@ -1210,8 +1213,11 @@ func checkBranch(cx *evalContext) error {
if err != nil {
return err
}
if cx.branchTargets == nil {
cx.branchTargets = make(map[int]bool)
if target < cx.nextpc {
// If a branch goes backwards, we should have already noted that an instruction began at that location.
if _, ok := cx.instructionStarts[target]; !ok {
return fmt.Errorf("back branch target %d is not an aligned instruction", target)
}
}
cx.branchTargets[target] = true
return nil
Expand Down
16 changes: 16 additions & 0 deletions data/transactions/logic/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2781,6 +2781,22 @@ int 1`, v)
require.Error(t, err)
require.False(t, pass)
isNotPanic(t, err)

// back branches are checked differently, so test misaligned back branch
ops.Program[6] = 0xff // Clobber the two bytes of offset with 0xff 0xff = -1
ops.Program[7] = 0xff // That jumps into the offset itself (pc + 3 -1)
_, err = Check(ops.Program, defaultEvalParams(nil, nil))
require.Error(t, err)
if v < backBranchEnabledVersion {
require.Contains(t, err.Error(), "negative branch")
} else {
require.Contains(t, err.Error(), "back branch")
require.Contains(t, err.Error(), "aligned")
}
pass, err = Eval(ops.Program, defaultEvalParams(nil, nil))
require.Error(t, err)
require.False(t, pass)
isNotPanic(t, err)
})
}
}
Expand Down