From f809a864b10589cab4dda2cc770f91c70119f967 Mon Sep 17 00:00:00 2001 From: andri lim Date: Wed, 1 May 2019 19:21:46 +0700 Subject: [PATCH] fix calcMemSize bug --- GeneralStateTests.md | 24 +++++++++---------- nimbus/vm/interpreter/opcodes_impl.nim | 9 ++----- nimbus/vm/interpreter/utils/utils_numeric.nim | 10 ++++++++ nimbus/vm/precompiles.nim | 6 ++--- tests/test_generalstate_failing.nim | 13 ---------- 5 files changed, 27 insertions(+), 35 deletions(-) diff --git a/GeneralStateTests.md b/GeneralStateTests.md index b105f208c..7f232b344 100644 --- a/GeneralStateTests.md +++ b/GeneralStateTests.md @@ -1013,13 +1013,13 @@ OK: 95/96 Fail: 0/96 Skip: 1/96 + CallSha256_4.json OK + CallSha256_4_gas99.json OK + CallSha256_5.json OK - modexpRandomInput.json Skip ++ modexpRandomInput.json OK + modexp_0_0_0_20500.json OK + modexp_0_0_0_22000.json OK + modexp_0_0_0_25000.json OK + modexp_0_0_0_35000.json OK ``` -OK: 96/97 Fail: 0/97 Skip: 1/97 +OK: 97/97 Fail: 0/97 Skip: 0/97 ## stQuadraticComplexityTest ```diff Call1MB1024Calldepth.json Skip @@ -1083,7 +1083,7 @@ OK: 0/16 Fail: 0/16 Skip: 16/16 + randomStatetest137.json OK + randomStatetest138.json OK + randomStatetest139.json OK - randomStatetest14.json Skip ++ randomStatetest14.json OK + randomStatetest142.json OK + randomStatetest143.json OK + randomStatetest144.json OK @@ -1357,7 +1357,7 @@ OK: 0/16 Fail: 0/16 Skip: 16/16 + randomStatetest82.json OK + randomStatetest83.json OK + randomStatetest84.json OK - randomStatetest85.json Skip ++ randomStatetest85.json OK + randomStatetest87.json OK + randomStatetest88.json OK + randomStatetest89.json OK @@ -1370,7 +1370,7 @@ OK: 0/16 Fail: 0/16 Skip: 16/16 + randomStatetest97.json OK + randomStatetest98.json OK ``` -OK: 321/327 Fail: 0/327 Skip: 6/327 +OK: 323/327 Fail: 0/327 Skip: 4/327 ## stRandom2 ```diff + 201503110226PYTHON_DUP6.json OK @@ -1834,11 +1834,11 @@ OK: 16/16 Fail: 0/16 Skip: 0/16 + gasPrice0.json OK + makeMoney.json OK push32withoutByte.json Skip - sha3_deja.json Skip ++ sha3_deja.json OK + txCost-sec73.json OK + tx_e1c174e2.json OK ``` -OK: 10/14 Fail: 0/14 Skip: 4/14 +OK: 11/14 Fail: 0/14 Skip: 3/14 ## stStackTests ```diff + shallowStack.json OK @@ -2485,11 +2485,11 @@ OK: 24/24 Fail: 0/24 Skip: 0/24 + ecpairing_one_point_with_g2_zero.json OK + ecpairing_one_point_with_g2_zero_and_g1_invalid.json OK + ecpairing_perturb_g2_by_curve_order.json OK - ecpairing_perturb_g2_by_field_modulus.json Skip - ecpairing_perturb_g2_by_field_modulus_again.json Skip ++ ecpairing_perturb_g2_by_field_modulus.json OK ++ ecpairing_perturb_g2_by_field_modulus_again.json OK + ecpairing_perturb_g2_by_one.json OK + ecpairing_perturb_zeropoint_by_curve_order.json OK - ecpairing_perturb_zeropoint_by_field_modulus.json Skip ++ ecpairing_perturb_zeropoint_by_field_modulus.json OK + ecpairing_perturb_zeropoint_by_one.json OK + ecpairing_three_point_fail_1.json OK + ecpairing_three_point_match_1.json OK @@ -2508,7 +2508,7 @@ OK: 24/24 Fail: 0/24 Skip: 0/24 + pointMulAdd.json OK + pointMulAdd2.json OK ``` -OK: 130/133 Fail: 0/133 Skip: 3/133 +OK: 133/133 Fail: 0/133 Skip: 0/133 ## stZeroKnowledge2 ```diff + ecadd_0-0_0-0_21000_0.json OK @@ -2645,4 +2645,4 @@ OK: 130/133 Fail: 0/133 Skip: 3/133 OK: 130/130 Fail: 0/130 Skip: 0/130 ---TOTAL--- -OK: 2245/2447 Fail: 0/2447 Skip: 202/2447 +OK: 2252/2447 Fail: 0/2447 Skip: 195/2447 diff --git a/nimbus/vm/interpreter/opcodes_impl.nim b/nimbus/vm/interpreter/opcodes_impl.nim index e88707c12..37411270e 100644 --- a/nimbus/vm/interpreter/opcodes_impl.nim +++ b/nimbus/vm/interpreter/opcodes_impl.nim @@ -195,12 +195,7 @@ op byteOp, inline = true, position, value: op sha3, inline = true, startPos, length: ## 0x20, Compute Keccak-256 hash. - let (pos, len) = (startPos.toInt, length.toInt) - - # TODO: - # "randomStatetest14.json", # SHA3 offset - # "sha3_deja.json", # SHA3 startPos - # both test require Uint256 to calculate startpos/offset + let (pos, len) = (startPos.safeInt, length.safeInt) if pos < 0 or len < 0 or pos > 2147483648: raise newException(OutOfBoundsRead, "Out of bounds memory access") @@ -708,7 +703,7 @@ template genCall(callName: untyped, opCode: Op): untyped = else: not computation.vmState.readOnlyStateDb.accountExists(to) - let (memOffset, memLength) = if memInPos + memInLen > memOutPos + memOutLen: + let (memOffset, memLength) = if calcMemSize(memInPos, memInLen) > calcMemSize(memOutPos, memOutLen): (memInPos, memInLen) else: (memOutPos, memOutLen) diff --git a/nimbus/vm/interpreter/utils/utils_numeric.nim b/nimbus/vm/interpreter/utils/utils_numeric.nim index d767bc9db..146b7bd9a 100644 --- a/nimbus/vm/interpreter/utils/utils_numeric.nim +++ b/nimbus/vm/interpreter/utils/utils_numeric.nim @@ -69,3 +69,13 @@ proc rangeToPadded*[T: StUint](x: openarray[byte], first, last: int): T = x.toOpenArray(lo, hi), allowPadding = true ) + +# calculates the memory size required for a step +func calcMemSize*(offset, length: int): int {.inline.} = + if length.isZero: return 0 + result = offset + length + +func safeInt*(x: Uint256): int {.inline.} = + result = x.truncate(int) + if x > high(int32).u256 or result < 0: + result = high(int32) diff --git a/nimbus/vm/precompiles.nim b/nimbus/vm/precompiles.nim index c69c59ff2..ef1bf517b 100644 --- a/nimbus/vm/precompiles.nim +++ b/nimbus/vm/precompiles.nim @@ -211,9 +211,9 @@ proc modExp*(computation: BaseComputation) = template rawMsg: untyped {.dirty.} = computation.msg.data let # lengths Base, Exponent, Modulus - base_len = rawMsg.rangeToPadded[:Uint256](0, 31).truncate(int) - exp_len = rawMsg.rangeToPadded[:Uint256](32, 63).truncate(int) - mod_len = rawMsg.rangeToPadded[:Uint256](64, 95).truncate(int) + base_len = rawMsg.rangeToPadded[:Uint256](0, 31).safeInt + exp_len = rawMsg.rangeToPadded[:Uint256](32, 63).safeInt + mod_len = rawMsg.rangeToPadded[:Uint256](64, 95).safeInt let maxBytes = max(base_len, max(exp_len, mod_len)) diff --git a/tests/test_generalstate_failing.nim b/tests/test_generalstate_failing.nim index 5394f4ef0..ba4379e2c 100644 --- a/tests/test_generalstate_failing.nim +++ b/tests/test_generalstate_failing.nim @@ -13,23 +13,10 @@ # being mostly used for short-term regression prevention. func allowedFailingGeneralStateTest*(folder, name: string): bool = let allowedFailingGeneralStateTests = @[ - # a family of UInt256 truncated to int problems - "randomStatetest14.json", # SHA3 offset - "randomStatetest85.json", # CALL* memoffset - "sha3_deja.json", # SHA3 startPos - # modexp exp_len & friends truncated to int - # and not OOG where it should OOG - "modexpRandomInput.json", - "CreateOOGafterInitCodeReturndataSize.json", "RevertInCreateInInit.json", "modexp.json", - # see precompiles getPoint[G2] - "ecpairing_perturb_g2_by_field_modulus_again.json", - "ecpairing_perturb_zeropoint_by_field_modulus.json", - "ecpairing_perturb_g2_by_field_modulus.json", - # 29/04/2019 "RevertPrecompiledTouch.json", "RevertPrecompiledTouch_storage.json",