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 5 commits
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
67 changes: 32 additions & 35 deletions data/transactions/logic/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1033,11 +1033,22 @@ func (ops *OpStream) assemble(fin io.Reader) error {
ops.trace("%d: no fields\n", ops.sourceLine)
continue
}
// we're going to process opcodes, so fix the Version
// we're about to begin processing opcodes, so fix the Version
if ops.Version == assemblerNoVersion {
ops.Version = AssemblerDefaultVersion
}
opstring := fields[0]

if opstring[len(opstring)-1] == ':' {
ops.createLabel(opstring[:len(opstring)-1])
fields = fields[1:]
if len(fields) == 0 {
// There was a label, not need to ops.trace this
continue
}
opstring = fields[0]
}

spec, ok := OpsByName[ops.Version][opstring]
if !ok {
spec, ok = keywords[opstring]
Expand All @@ -1053,10 +1064,6 @@ func (ops *OpStream) assemble(fin io.Reader) error {
ops.trace("\n")
continue
}
if opstring[len(opstring)-1] == ':' {
ops.createLabel(opstring[:len(opstring)-1])
continue
}
// unknown opcode, let's report a good error if version problem
spec, ok = OpsByName[AssemblerMaxVersion][opstring]
if ok {
Expand Down Expand Up @@ -1148,8 +1155,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 +1383,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 +1451,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 +1609,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 +1629,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
106 changes: 65 additions & 41 deletions data/transactions/logic/assembler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,36 @@ pushint 1000
pushbytes "john"
`

// Keep in mind, only use existing int and byte constants, or else use
// push* instead. The idea is to not cause the *cblocks to change.
const v4Nonsense = `
int 1
pushint 2000
int 0
int 2
divw
callsub stuff
b next
stuff:
retsub
next:
int 1
`

var nonsense = map[uint64]string{
1: v1Nonsense,
2: v1Nonsense + v2Nonsense,
3: v1Nonsense + v2Nonsense + v3Nonsense,
4: v1Nonsense + v2Nonsense + v3Nonsense + v4Nonsense,
}

var compiled = map[uint64]string{
1: "012008b7a60cf8acd19181cf959a12f8acd19181cf951af8acd19181cf15f8acd191810f01020026050212340c68656c6c6f20776f726c6421208dae2087fbba51304eb02b91f656948397a7946390e8cb70fc9ea4d95f92251d024242047465737400320032013202320328292929292a0431003101310231043105310731083109310a310b310c310d310e310f3111311231133114311533000033000133000233000433000533000733000833000933000a33000b33000c33000d33000e33000f3300113300123300133300143300152d2e0102222324252104082209240a220b230c240d250e230f23102311231223132314181b1c2b1716154000032903494",
2: "022008b7a60cf8acd19181cf959a12f8acd19181cf951af8acd19181cf15f8acd191810f01020026050212340c68656c6c6f20776f726c6421208dae2087fbba51304eb02b91f656948397a7946390e8cb70fc9ea4d95f92251d024242047465737400320032013202320328292929292a0431003101310231043105310731083109310a310b310c310d310e310f3111311231133114311533000033000133000233000433000533000733000833000933000a33000b33000c33000d33000e33000f3300113300123300133300143300152d2e0102222324252104082209240a220b230c240d250e230f23102311231223132314181b1c2b171615400003290349483403350222231d4a484848482a50512a63222352410003420000432105602105612105270463484821052b62482b642b65484821052b2106662b21056721072b682b692107210570004848210771004848361c0037001a0031183119311b311d311e311f3120210721051e312131223123312431253126312731283129312a312b312c312d312e312f",
3: "032008b7a60cf8acd19181cf959a12f8acd19181cf951af8acd19181cf15f8acd191810f01020026050212340c68656c6c6f20776f726c6421208dae2087fbba51304eb02b91f656948397a7946390e8cb70fc9ea4d95f92251d024242047465737400320032013202320328292929292a0431003101310231043105310731083109310a310b310c310d310e310f3111311231133114311533000033000133000233000433000533000733000833000933000a33000b33000c33000d33000e33000f3300113300123300133300143300152d2e0102222324252104082209240a220b230c240d250e230f23102311231223132314181b1c2b171615400003290349483403350222231d4a484848482a50512a63222352410003420000432105602105612105270463484821052b62482b642b65484821052b2106662b21056721072b682b692107210570004848210771004848361c0037001a0031183119311b311d311e311f3120210721051e312131223123312431253126312731283129312a312b312c312d312e312f4478222105531421055427042106552105082106564c4d4b02210538212106391c0081e80780046a6f686e",
4: "042008b7a60cf8acd19181cf959a12f8acd19181cf951af8acd19181cf15f8acd191810f01020026050212340c68656c6c6f20776f726c6421208dae2087fbba51304eb02b91f656948397a7946390e8cb70fc9ea4d95f92251d024242047465737400320032013202320328292929292a0431003101310231043105310731083109310a310b310c310d310e310f3111311231133114311533000033000133000233000433000533000733000833000933000a33000b33000c33000d33000e33000f3300113300123300133300143300152d2e0102222324252104082209240a220b230c240d250e230f23102311231223132314181b1c2b171615400003290349483403350222231d4a484848482a50512a63222352410003420000432105602105612105270463484821052b62482b642b65484821052b2106662b21056721072b682b692107210570004848210771004848361c0037001a0031183119311b311d311e311f3120210721051e312131223123312431253126312731283129312a312b312c312d312e312f4478222105531421055427042106552105082106564c4d4b02210538212106391c0081e80780046a6f686e210581d00f210721061f880003420001892105",
}

func pseudoOp(opcode string) bool {
// We don't test every combination of
// intcblock,bytecblock,intc*,bytec*,arg* here. Not all of
Expand All @@ -263,44 +293,33 @@ func TestAssemble(t *testing.T) {
// Run test. It should pass.
//
// This doesn't have to be a sensible program to run, it just has to compile.
for _, spec := range OpSpecs {
// Ensure that we have some basic check of all the ops, except
if !strings.Contains(v1Nonsense+v2Nonsense, spec.Name) &&
!pseudoOp(spec.Name) && spec.Version <= 2 {
t.Errorf("v2 nonsense test should contain op %v", spec.Name)
}
}
// First, we test v2, not AssemblerMaxVersion. A higher version is
// allowed to differ (and must, in the first byte).
ops := testProg(t, v1Nonsense+v2Nonsense, 2)
// check that compilation is stable over time and we assemble to the same bytes this month that we did last month.
expectedBytes, _ := hex.DecodeString("022008b7a60cf8acd19181cf959a12f8acd19181cf951af8acd19181cf15f8acd191810f01020026050212340c68656c6c6f20776f726c6421208dae2087fbba51304eb02b91f656948397a7946390e8cb70fc9ea4d95f92251d024242047465737400320032013202320328292929292a0431003101310231043105310731083109310a310b310c310d310e310f3111311231133114311533000033000133000233000433000533000733000833000933000a33000b33000c33000d33000e33000f3300113300123300133300143300152d2e0102222324252104082209240a220b230c240d250e230f23102311231223132314181b1c2b171615400003290349483403350222231d4a484848482a50512a63222352410003420000432105602105612105270463484821052b62482b642b65484821052b2106662b21056721072b682b692107210570004848210771004848361c0037001a0031183119311b311d311e311f3120210721051e312131223123312431253126312731283129312a312b312c312d312e312f")
if bytes.Compare(expectedBytes, ops.Program) != 0 {
// this print is for convenience if the program has been changed. the hex string can be copy pasted back in as a new expected result.
t.Log(hex.EncodeToString(ops.Program))
}
require.Equal(t, expectedBytes, ops.Program)

// We test v3 here, and compare to AssemblerMaxVersion, with
// the intention that the test breaks the next time
// AssemblerMaxVersion is increased. At that point, we would
// add a new test for v4, and leave behind this test for v3.

for _, spec := range OpSpecs {
// Ensure that we have some basic check of all the ops, except
if !strings.Contains(v1Nonsense+v2Nonsense+v3Nonsense, spec.Name) &&
!pseudoOp(spec.Name) && spec.Version <= 3 {
t.Errorf("v3 nonsense test should contain op %v", spec.Name)
}
}
ops = testProg(t, v1Nonsense+v2Nonsense+v3Nonsense, AssemblerMaxVersion)
// check that compilation is stable over time and we assemble to the same bytes this month that we did last month.
expectedBytes, _ = hex.DecodeString("032008b7a60cf8acd19181cf959a12f8acd19181cf951af8acd19181cf15f8acd191810f01020026050212340c68656c6c6f20776f726c6421208dae2087fbba51304eb02b91f656948397a7946390e8cb70fc9ea4d95f92251d024242047465737400320032013202320328292929292a0431003101310231043105310731083109310a310b310c310d310e310f3111311231133114311533000033000133000233000433000533000733000833000933000a33000b33000c33000d33000e33000f3300113300123300133300143300152d2e0102222324252104082209240a220b230c240d250e230f23102311231223132314181b1c2b171615400003290349483403350222231d4a484848482a50512a63222352410003420000432105602105612105270463484821052b62482b642b65484821052b2106662b21056721072b682b692107210570004848210771004848361c0037001a0031183119311b311d311e311f3120210721051e312131223123312431253126312731283129312a312b312c312d312e312f4478222105531421055427042106552105082106564c4d4b02210538212106391c0081e80780046a6f686e")
if bytes.Compare(expectedBytes, ops.Program) != 0 {
// this print is for convenience if the program has been changed. the hex string can be copy pasted back in as a new expected result.
t.Log(hex.EncodeToString(ops.Program))

t.Parallel()
tsachiherman marked this conversation as resolved.
Show resolved Hide resolved
for v := uint64(2); v <= AssemblerMaxVersion; v++ {
t.Run(fmt.Sprintf("v=%d", v), func(t *testing.T) {
for _, spec := range OpSpecs {
// Make sure our nonsense covers the ops
if !strings.Contains(nonsense[v], spec.Name) &&
!pseudoOp(spec.Name) && spec.Version <= v {
t.Errorf("v%d nonsense test should contain op %v", v, spec.Name)
}
}

ops := testProg(t, nonsense[v], v)
// check that compilation is stable over
// time. we must assemble to the same bytes
// this month that we did last month.
expectedBytes, _ := hex.DecodeString(compiled[v])
if bytes.Compare(expectedBytes, ops.Program) != 0 {
// this print is for convenience if
// the program has been changed. the
// hex string can be copy pasted back
// in as a new expected result.
t.Log(hex.EncodeToString(ops.Program))
}
require.Equal(t, expectedBytes, ops.Program)
})
}
require.Equal(t, expectedBytes, ops.Program)
}

func TestAssembleAlias(t *testing.T) {
Expand Down Expand Up @@ -758,9 +777,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 @@ -1628,8 +1652,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
8 changes: 6 additions & 2 deletions data/transactions/logic/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var opDocByName = map[string]string{
"~": "bitwise invert value X",
"mulw": "A times B out to 128-bit long result as low (top) and high uint64 values on the stack",
"addw": "A plus B out to 128-bit long result as sum (top) and carry-bit uint64 values on the stack",
"divw": "Pop four uint64 values. The deepest two are interpreted as a uint128 dividend (deepest value is high word), the top two are interpreted as a uint128 divisor. Four uint64 values are pushed to the stack. The deepest two are the quotient (deeper value is the high uint64). The top two are the remainder, low bits on top.",
"intcblock": "prepare block of uint64 constants for use by intc",
"intc": "push Ith constant from intcblock to stack",
"intc_0": "push constant 0 from intcblock to stack",
Expand Down Expand Up @@ -111,6 +112,8 @@ var opDocByName = map[string]string{
"asset_holding_get": "read from account specified by Txn.Accounts[A] and asset B holding field X (imm arg) => {0 or 1 (top), value}",
"asset_params_get": "read from asset Txn.ForeignAssets[A] params field X (imm arg) => {0 or 1 (top), value}",
"assert": "immediately fail unless value X is a non-zero number",
"callsub": "branch unconditionally to TARGET, saving the next instruction on the call stack",
"retsub": "pop the top instruction from the call stack and branch to it",
}

// OpDoc returns a description of the op
Expand Down Expand Up @@ -159,6 +162,7 @@ var opDocExtras = map[string]string{
"bytecblock": "`bytecblock` loads the following program bytes into an array of byte-array constants in the evaluator. These constants can be referred to by `bytec` and `bytec_*` which will push the value onto the stack. Subsequent calls to `bytecblock` reset and replace the bytes constants available to the script.",
"*": "Overflow is an error condition which halts execution and fails the transaction. Full precision is available from `mulw`.",
"+": "Overflow is an error condition which halts execution and fails the transaction. Full precision is available from `addw`.",
"/": "`divw` is available to divide the two-element values produced by `mulw` and `addw`.",
"txn": "FirstValidTime causes the program to fail. The field is reserved for future use.",
"gtxn": "for notes on transaction fields available, see `txn`. If this transaction is _i_ in the group, `gtxn i field` is equivalent to `txn field`.",
"gtxns": "for notes on transaction fields available, see `txn`. If top of stack is _i_, `gtxns field` is equivalent to `gtxn _i_ field`. gtxns exists so that _i_ can be calculated, often based on the index of the current transaction.",
Expand Down Expand Up @@ -194,9 +198,9 @@ type OpGroup struct {

// OpGroupList is groupings of ops for documentation purposes.
var OpGroupList = []OpGroup{
{"Arithmetic", []string{"sha256", "keccak256", "sha512_256", "ed25519verify", "+", "-", "/", "*", "<", ">", "<=", ">=", "&&", "||", "==", "!=", "!", "len", "itob", "btoi", "%", "|", "&", "^", "~", "mulw", "addw", "getbit", "setbit", "getbyte", "setbyte", "concat", "substring", "substring3"}},
{"Arithmetic", []string{"sha256", "keccak256", "sha512_256", "ed25519verify", "+", "-", "/", "*", "<", ">", "<=", ">=", "&&", "||", "==", "!=", "!", "len", "itob", "btoi", "%", "|", "&", "^", "~", "mulw", "addw", "divw", "getbit", "setbit", "getbyte", "setbyte", "concat", "substring", "substring3"}},
{"Loading Values", []string{"intcblock", "intc", "intc_0", "intc_1", "intc_2", "intc_3", "pushint", "bytecblock", "bytec", "bytec_0", "bytec_1", "bytec_2", "bytec_3", "pushbytes", "arg", "arg_0", "arg_1", "arg_2", "arg_3", "txn", "gtxn", "txna", "gtxna", "gtxns", "gtxnsa", "global", "load", "store"}},
{"Flow Control", []string{"err", "bnz", "bz", "b", "return", "pop", "dup", "dup2", "dig", "swap", "select", "assert"}},
{"Flow Control", []string{"err", "bnz", "bz", "b", "return", "pop", "dup", "dup2", "dig", "swap", "select", "assert", "callsub", "retsub"}},
{"State Access", []string{"balance", "min_balance", "app_opted_in", "app_local_get", "app_local_get_ex", "app_global_get", "app_global_get_ex", "app_local_put", "app_global_put", "app_local_del", "app_global_del", "asset_holding_get", "asset_params_get"}},
}

Expand Down
2 changes: 1 addition & 1 deletion data/transactions/logic/doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestOpDocs(t *testing.T) {
}
for op, seen := range opsSeen {
if !seen {
t.Errorf("error: doc for op %#v missing", op)
t.Errorf("error: doc for op %#v missing from opDocByName", op)
}
}
}
Expand Down
Loading