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
back branches
  • Loading branch information
jannotti committed Apr 27, 2021
commit cdd1a592add1dc9ffddb2f09c96138a1f496e056
50 changes: 20 additions & 30 deletions data/transactions/logic/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,8 +1148,8 @@ func (ops *OpStream) resolveLabels() {
}
// all branch instructions (currently) are opcode byte and 2 offset bytes, and the destination is relative to the next pc as if the branch was a no-op
naturalPc := lr.position + 3
if dest < naturalPc {
ops.errorf("label %v is before reference but only forward jumps are allowed", lr.label)
if ops.Version < backBranchEnabledVersion && dest < naturalPc {
ops.errorf("label %v is a back reference, back jump support was introduced in TEAL v4", lr.label)
tsachiherman marked this conversation as resolved.
Show resolved Hide resolved
continue
}
jump := dest - naturalPc
Expand Down Expand Up @@ -1376,33 +1376,29 @@ func parseIntcblock(program []byte, pc int) (intc []uint64, nextpc int, err erro
return
}

func checkIntConstBlock(cx *evalContext) int {
func checkIntConstBlock(cx *evalContext) error {
pos := cx.pc + 1
numInts, bytesUsed := binary.Uvarint(cx.program[pos:])
if bytesUsed <= 0 {
cx.err = fmt.Errorf("could not decode int const block size at pc=%d", pos)
return 1
return fmt.Errorf("could not decode int const block size at pc=%d", pos)
}
pos += bytesUsed
if numInts > uint64(len(cx.program)) {
cx.err = errTooManyIntc
return 0
return errTooManyIntc
}
//intc = make([]uint64, numInts)
for i := uint64(0); i < numInts; i++ {
if pos >= len(cx.program) {
cx.err = errShortIntcblock
return 0
return errShortIntcblock
}
_, bytesUsed = binary.Uvarint(cx.program[pos:])
if bytesUsed <= 0 {
cx.err = fmt.Errorf("could not decode int const[%d] at pc=%d", i, pos)
return 1
return fmt.Errorf("could not decode int const[%d] at pc=%d", i, pos)
}
pos += bytesUsed
}
cx.nextpc = pos
return 1
return nil
}

var errShortBytecblock = errors.New("bytecblock ran past end of program")
Expand Down Expand Up @@ -1448,44 +1444,38 @@ func parseBytecBlock(program []byte, pc int) (bytec [][]byte, nextpc int, err er
return
}

func checkByteConstBlock(cx *evalContext) int {
func checkByteConstBlock(cx *evalContext) error {
pos := cx.pc + 1
numItems, bytesUsed := binary.Uvarint(cx.program[pos:])
if bytesUsed <= 0 {
cx.err = fmt.Errorf("could not decode []byte const block size at pc=%d", pos)
return 1
return fmt.Errorf("could not decode []byte const block size at pc=%d", pos)
}
pos += bytesUsed
if numItems > uint64(len(cx.program)) {
cx.err = errTooManyItems
return 0
return errTooManyItems
}
//bytec = make([][]byte, numItems)
for i := uint64(0); i < numItems; i++ {
if pos >= len(cx.program) {
cx.err = errShortBytecblock
return 0
return errShortBytecblock
}
itemLen, bytesUsed := binary.Uvarint(cx.program[pos:])
if bytesUsed <= 0 {
cx.err = fmt.Errorf("could not decode []byte const[%d] at pc=%d", i, pos)
return 1
return fmt.Errorf("could not decode []byte const[%d] at pc=%d", i, pos)
}
pos += bytesUsed
if pos >= len(cx.program) {
cx.err = errShortBytecblock
return 0
return errShortBytecblock
}
end := uint64(pos) + itemLen
if end > uint64(len(cx.program)) || end < uint64(pos) {
cx.err = errShortBytecblock
return 0
return errShortBytecblock
}
//bytec[i] = program[pos : pos+int(itemLen)]
pos += int(itemLen)
}
cx.nextpc = pos
return 1
return nil
}

func disIntcblock(dis *disassembleState, spec *OpSpec) (string, error) {
Expand Down Expand Up @@ -1612,9 +1602,9 @@ func disPushInt(dis *disassembleState, spec *OpSpec) (string, error) {
dis.nextpc = pos + bytesUsed
return fmt.Sprintf("%s %d", spec.Name, val), nil
}
func checkPushInt(cx *evalContext) int {
func checkPushInt(cx *evalContext) error {
opPushInt(cx)
return 1
return cx.err
}

func disPushBytes(dis *disassembleState, spec *OpSpec) (string, error) {
Expand All @@ -1632,9 +1622,9 @@ func disPushBytes(dis *disassembleState, spec *OpSpec) (string, error) {
dis.nextpc = int(end)
return fmt.Sprintf("%s 0x%s", spec.Name, hex.EncodeToString(bytes)), nil
}
func checkPushBytes(cx *evalContext) int {
func checkPushBytes(cx *evalContext) error {
opPushBytes(cx)
return 1
return cx.err
Comment on lines -1635 to +1634
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like check* functions needs to be moved to check.go from the assembler

}

// This is also used to disassemble gtxns
Expand Down
13 changes: 9 additions & 4 deletions data/transactions/logic/assembler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,9 +771,14 @@ func TestAssembleRejectNegJump(t *testing.T) {
int 1
bnz wat
int 2`
for v := uint64(1); v <= AssemblerMaxVersion; v++ {
for v := uint64(1); v < backBranchEnabledVersion; v++ {
t.Run(fmt.Sprintf("v=%d", v), func(t *testing.T) {
testProg(t, source, v, expect{3, "label wat is before reference but only forward jumps are allowed"})
testProg(t, source, v, expect{3, "label wat is a back reference..."})
})
}
for v := uint64(backBranchEnabledVersion); v <= AssemblerMaxVersion; v++ {
t.Run(fmt.Sprintf("v=%d", v), func(t *testing.T) {
testProg(t, source, v)
})
}
}
Expand Down Expand Up @@ -1641,8 +1646,8 @@ func TestErrShortBytecblock(t *testing.T) {

var cx evalContext
cx.program = ops.Program
checkIntConstBlock(&cx)
require.Equal(t, cx.err, errShortIntcblock)
err = checkIntConstBlock(&cx)
require.Equal(t, err, errShortIntcblock)
}

func TestBranchAssemblyTypeCheck(t *testing.T) {
Expand Down
Loading