Skip to content

Commit

Permalink
smallInt* unhandled cases (#116)
Browse files Browse the repository at this point in the history
* smallIntGetUnsignedArgument handle index error

* split rules with if-then-else
  • Loading branch information
bbyalcinkaya authored Aug 1, 2023
1 parent dd6bbf3 commit 7f54733
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 9 deletions.
87 changes: 87 additions & 0 deletions tests/simple/bad-bounds.wast
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ deployTx(
(import "env" "getCaller" (func $getCaller (param i32)))
(import "env" "isSmartContract" (func $isSmartContract (param i32) (result i32)))
(import "env" "writeEventLog" (func $writeEventLog (param i32 i32 i32 i32 i32)))

(import "env" "smallIntGetUnsignedArgument"
(func $smallIntGetUnsignedArgument (param i32) (result i64))
)

(import "env" "smallIntGetSignedArgument"
(func $smallIntGetSignedArgument (param i32) (result i64))
)

(import "env" "smallIntFinishUnsigned" (func $smallIntFinishUnsigned (param i64)))
(import "env" "smallIntFinishSigned" (func $smallIntFinishSigned (param i64)))

(func (export "memStoreNegativeOffset")
i32.const -1
call $getCaller
Expand All @@ -34,6 +46,21 @@ deployTx(
i32.const 0
call $writeEventLog
)

(func (export "smallIntGetUnsignedArgumentTest")
(call $smallIntGetSignedArgument (i32.const 0)) ;; read the first argument: i
i32.wrap_i64
call $smallIntGetUnsignedArgument ;; read the ith argument
call $smallIntFinishUnsigned ;; return it
)

(func (export "smallIntGetSignedArgumentTest")
(call $smallIntGetSignedArgument (i32.const 0)) ;; read the first argument: i
i32.wrap_i64
call $smallIntGetSignedArgument ;; read the ith argument
call $smallIntFinishSigned ;; return it
)

(func (export "init"))
(memory (;0;) 17)
(export "memory" (memory 0))
Expand Down Expand Up @@ -105,4 +132,64 @@ callTx(
checkExpectStatus(ExecutionFailed)
checkExpectMessage(b"negative numArguments")

;; pass 2 arguments, get 2nd
callTx(
"testCaller"
, "testContract"
, 0, .List
, "smallIntGetUnsignedArgumentTest"
, ListItem(Int2Bytes(1, BE, Signed)) ListItem(Int2Bytes(123, BE, Unsigned))
, 0
, 0
)

checkExpectStatus(OK)
checkExpectOut(ListItem(Int2Bytes(123, BE, Unsigned)))


;; pass 1 argument, get 2nd, should fail
callTx(
"testCaller"
, "testContract"
, 0, .List
, "smallIntGetUnsignedArgumentTest"
, ListItem(Int2Bytes(1, BE, Signed))
, 0
, 0
)

checkExpectStatus(ExecutionFailed)
checkExpectMessage(b"argument index out of range")


;; pass 2 arguments, get -1st, should fail
callTx(
"testCaller"
, "testContract"
, 0, .List
, "smallIntGetUnsignedArgumentTest"
, ListItem(Int2Bytes(-1, BE, Signed)) ListItem(Int2Bytes(123, BE, Unsigned))
, 0
, 0
)

checkExpectStatus(ExecutionFailed)
checkExpectMessage(b"argument index out of range")


;; pass 2 arguments (2nd is too big for u64), get 2nd, should fail
callTx(
"testCaller"
, "testContract"
, 0, .List
, "smallIntGetUnsignedArgumentTest"
, ListItem(Int2Bytes(1, BE, Signed))
ListItem(Int2Bytes(maxUInt64 +Int 5, BE, Unsigned))
, 0
, 0
)

checkExpectStatus(UserError)
checkExpectMessage(b"argument out of range")

setExitCode 0
45 changes: 36 additions & 9 deletions vmhooks/smallIntOps.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,47 @@ require "../elrond-config.md"
module SMALLINTOPS
imports BASEOPS
imports ELROND-CONFIG
imports private LIST-BYTES-EXTENSIONS
// extern long long smallIntGetUnsignedArgument(void *context, int32_t id);
rule <instrs> hostCall("env", "smallIntGetUnsignedArgument", [ i32 .ValTypes ] -> [ i64 .ValTypes ])
=> #returnIfUInt64(Bytes2Int(ARGS[ARG_IDX], BE, Unsigned), "argument out of range") ... </instrs>
<locals> 0 |-> <i32> ARG_IDX </locals>
<callArgs> ARGS </callArgs>
rule [smallIntGetUnsignedArgument]:
<instrs> hostCall("env", "smallIntGetUnsignedArgument", [ i32 .ValTypes ] -> [ i64 .ValTypes ])
=> #returnIfUInt64(Bytes2Int(ARGS[ARG_IDX] orDefault b"", BE, Unsigned), "argument out of range")
...
</instrs>
<locals> 0 |-> <i32> ARG_IDX </locals>
<callArgs> ARGS </callArgs>
requires #validArgIdx(ARG_IDX, ARGS)
rule [smallIntGetUnsignedArgument-ioor]:
<instrs> hostCall("env", "smallIntGetUnsignedArgument", [ i32 .ValTypes ] -> [ i64 .ValTypes ])
=> #throwException(ExecutionFailed, "argument index out of range")
...
</instrs>
<locals> 0 |-> <i32> ARG_IDX </locals>
<callArgs> ARGS </callArgs>
requires notBool #validArgIdx(ARG_IDX, ARGS)
// extern long long smallIntGetSignedArgument(void *context, int32_t id);
rule <instrs> hostCall("env", "smallIntGetSignedArgument", [ i32 .ValTypes ] -> [ i64 .ValTypes ])
=> #returnIfSInt64(Bytes2Int(ARGS[ARG_IDX], BE, Signed), "argument out of range") ... </instrs>
<locals> 0 |-> <i32> ARG_IDX </locals>
<callArgs> ARGS </callArgs>
requires ARG_IDX <Int size(ARGS)
rule [smallIntGetSignedArgument]:
<instrs> hostCall("env", "smallIntGetSignedArgument", [ i32 .ValTypes ] -> [ i64 .ValTypes ])
=> #returnIfSInt64(Bytes2Int(ARGS[ARG_IDX] orDefault b"", BE, Signed), "argument out of range")
...
</instrs>
<locals> 0 |-> <i32> ARG_IDX </locals>
<callArgs> ARGS </callArgs>
requires #validArgIdx(ARG_IDX, ARGS)
rule [smallIntGetSignedArgument-ioor]:
<instrs> hostCall("env", "smallIntGetSignedArgument", [ i32 .ValTypes ] -> [ i64 .ValTypes ])
=> #throwException(ExecutionFailed, "argument index out of range")
...
</instrs>
<locals> 0 |-> <i32> ARG_IDX </locals>
<callArgs> ARGS </callArgs>
requires notBool #validArgIdx(ARG_IDX, ARGS)
// extern void smallIntFinishUnsigned(void* context, long long value);
rule <instrs> hostCall("env", "smallIntFinishUnsigned", [ i64 .ValTypes ] -> [ .ValTypes ])
Expand Down

0 comments on commit 7f54733

Please sign in to comment.