From fb7c969ce89912b80657b5bce3cd17edd81468bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Fri, 25 Mar 2022 20:03:26 +0100 Subject: [PATCH] Tests for assembly instructions allowed with mutable/view/pure functions --- .../inline_assembly_instructions_allowed.sol | 92 +++++++++++++++++++ ...ine_assembly_instructions_allowed_pure.sol | 90 ++++++++++++++++++ ...ine_assembly_instructions_allowed_view.sol | 91 ++++++++++++++++++ ...nline_assembly_instructions_disallowed.sol | 28 ++++++ ..._assembly_instructions_disallowed_pure.sol | 80 ++++++++++++++++ ..._assembly_instructions_disallowed_view.sol | 38 ++++++++ 6 files changed, 419 insertions(+) create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_allowed.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_allowed_pure.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_allowed_view.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_disallowed.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_disallowed_pure.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_disallowed_view.sol diff --git a/test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_allowed.sol b/test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_allowed.sol new file mode 100644 index 000000000..8b634c219 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_allowed.sol @@ -0,0 +1,92 @@ +contract C { + function f() public { + assembly { + stop() + pop(add(0, 1)) + pop(sub(0, 1)) + pop(mul(0, 1)) + pop(div(0, 1)) + pop(sdiv(0, 1)) + pop(mod(0, 1)) + pop(smod(0, 1)) + pop(exp(0, 1)) + pop(not(0)) + pop(lt(0, 1)) + pop(gt(0, 1)) + pop(slt(0, 1)) + pop(sgt(0, 1)) + pop(eq(0, 1)) + pop(iszero(0)) + pop(and(0, 1)) + pop(or(0, 1)) + pop(xor(0, 1)) + pop(byte(0, 1)) + pop(shl(0, 1)) + pop(shr(0, 1)) + pop(sar(0, 1)) + pop(addmod(0, 1, 2)) + pop(mulmod(0, 1, 2)) + pop(signextend(0, 1)) + pop(keccak256(0, 1)) + pop(0) + pop(mload(0)) + mstore(0, 1) + mstore8(0, 1) + pop(sload(0)) + sstore(0, 1) + pop(gas()) + pop(address()) + pop(balance(0)) + pop(selfbalance()) + pop(caller()) + pop(callvalue()) + pop(calldataload(0)) + pop(calldatasize()) + calldatacopy(0, 1, 2) + pop(codesize()) + codecopy(0, 1, 2) + pop(extcodesize(0)) + extcodecopy(0, 1, 2, 3) + pop(returndatasize()) + returndatacopy(0, 1, 2) + pop(extcodehash(0)) + pop(create(0, 1, 2)) + pop(create2(0, 1, 2, 3)) + pop(call(0, 1, 2, 3, 4, 5, 6)) + pop(callcode(0, 1, 2, 3, 4, 5, 6)) + pop(delegatecall(0, 1, 2, 3, 4, 5)) + pop(staticcall(0, 1, 2, 3, 4, 5)) + return(0, 1) + revert(0, 1) + selfdestruct(0) + invalid() + log0(0, 1) + log1(0, 1, 2) + log2(0, 1, 2, 3) + log3(0, 1, 2, 3, 4) + log4(0, 1, 2, 3, 4, 5) + pop(chainid()) + pop(basefee()) + pop(origin()) + pop(gasprice()) + pop(blockhash(0)) + pop(coinbase()) + pop(timestamp()) + pop(number()) + pop(difficulty()) + pop(gaslimit()) + + // NOTE: msize() is allowed only with optimizer disabled + //pop(msize()) + //pop(pc()) + } + } +} +// ==== +// EVMVersion: >=london +// ---- +// Warning 5740: (89-1716): Unreachable code. +// Warning 5740: (1729-1741): Unreachable code. +// Warning 5740: (1754-1769): Unreachable code. +// Warning 5740: (1782-1791): Unreachable code. +// Warning 5740: (1804-2215): Unreachable code. diff --git a/test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_allowed_pure.sol b/test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_allowed_pure.sol new file mode 100644 index 000000000..85d7b6300 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_allowed_pure.sol @@ -0,0 +1,90 @@ +contract C { + function f() public pure { + assembly { + stop() + pop(add(0, 1)) + pop(sub(0, 1)) + pop(mul(0, 1)) + pop(div(0, 1)) + pop(sdiv(0, 1)) + pop(mod(0, 1)) + pop(smod(0, 1)) + pop(exp(0, 1)) + pop(not(0)) + pop(lt(0, 1)) + pop(gt(0, 1)) + pop(slt(0, 1)) + pop(sgt(0, 1)) + pop(eq(0, 1)) + pop(iszero(0)) + pop(and(0, 1)) + pop(or(0, 1)) + pop(xor(0, 1)) + pop(byte(0, 1)) + pop(shl(0, 1)) + pop(shr(0, 1)) + pop(sar(0, 1)) + pop(addmod(0, 1, 2)) + pop(mulmod(0, 1, 2)) + pop(signextend(0, 1)) + pop(keccak256(0, 1)) + pop(0) + pop(mload(0)) + mstore(0, 1) + mstore8(0, 1) + //pop(sload(0)) + //sstore(0, 1) + //pop(gas()) + //pop(address()) + //pop(balance(0)) + //pop(selfbalance()) + //pop(caller()) + //pop(callvalue()) + pop(calldataload(0)) + pop(calldatasize()) + calldatacopy(0, 1, 2) + pop(codesize()) + codecopy(0, 1, 2) + //pop(extcodesize(0)) + //extcodecopy(0, 1, 2, 3) + pop(returndatasize()) + returndatacopy(0, 1, 2) + //pop(extcodehash(0)) + //pop(create(0, 1, 2)) + //pop(create2(0, 1, 2, 3)) + //pop(call(0, 1, 2, 3, 4, 5, 6)) + //pop(callcode(0, 1, 2, 3, 4, 5, 6)) + //pop(delegatecall(0, 1, 2, 3, 4, 5)) + //pop(staticcall(0, 1, 2, 3, 4, 5)) + return(0, 1) + revert(0, 1) + //selfdestruct(0) + invalid() + //log0(0, 1) + //log1(0, 1, 2) + //log2(0, 1, 2, 3) + //log3(0, 1, 2, 3, 4) + //log4(0, 1, 2, 3, 4, 5) + //pop(chainid()) + //pop(basefee()) + //pop(origin()) + //pop(gasprice()) + //pop(blockhash(0)) + //pop(coinbase()) + //pop(timestamp()) + //pop(number()) + //pop(difficulty()) + //pop(gaslimit()) + + // NOTE: msize() is allowed only with optimizer disabled + //pop(msize()) + //pop(pc()) + } + } +} +// ==== +// EVMVersion: >=london +// ---- +// Warning 5740: (94-1755): Unreachable code. +// Warning 5740: (1768-1780): Unreachable code. +// Warning 5740: (1823-1832): Unreachable code. diff --git a/test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_allowed_view.sol b/test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_allowed_view.sol new file mode 100644 index 000000000..af30bc21a --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_allowed_view.sol @@ -0,0 +1,91 @@ +contract C { + function f() public view { + assembly { + stop() + pop(add(0, 1)) + pop(sub(0, 1)) + pop(mul(0, 1)) + pop(div(0, 1)) + pop(sdiv(0, 1)) + pop(mod(0, 1)) + pop(smod(0, 1)) + pop(exp(0, 1)) + pop(not(0)) + pop(lt(0, 1)) + pop(gt(0, 1)) + pop(slt(0, 1)) + pop(sgt(0, 1)) + pop(eq(0, 1)) + pop(iszero(0)) + pop(and(0, 1)) + pop(or(0, 1)) + pop(xor(0, 1)) + pop(byte(0, 1)) + pop(shl(0, 1)) + pop(shr(0, 1)) + pop(sar(0, 1)) + pop(addmod(0, 1, 2)) + pop(mulmod(0, 1, 2)) + pop(signextend(0, 1)) + pop(keccak256(0, 1)) + pop(0) + pop(mload(0)) + mstore(0, 1) + mstore8(0, 1) + pop(sload(0)) + //sstore(0, 1) + pop(gas()) + pop(address()) + pop(balance(0)) + pop(selfbalance()) + pop(caller()) + pop(callvalue()) + pop(calldataload(0)) + pop(calldatasize()) + calldatacopy(0, 1, 2) + pop(codesize()) + codecopy(0, 1, 2) + pop(extcodesize(0)) + extcodecopy(0, 1, 2, 3) + pop(returndatasize()) + returndatacopy(0, 1, 2) + pop(extcodehash(0)) + //pop(create(0, 1, 2)) + //pop(create2(0, 1, 2, 3)) + //pop(call(0, 1, 2, 3, 4, 5, 6)) + //pop(callcode(0, 1, 2, 3, 4, 5, 6)) + //pop(delegatecall(0, 1, 2, 3, 4, 5)) + pop(staticcall(0, 1, 2, 3, 4, 5)) + return(0, 1) + revert(0, 1) + //selfdestruct(0) + invalid() + //log0(0, 1) + //log1(0, 1, 2) + //log2(0, 1, 2, 3) + //log3(0, 1, 2, 3, 4) + //log4(0, 1, 2, 3, 4, 5) + pop(chainid()) + pop(basefee()) + pop(origin()) + pop(gasprice()) + pop(blockhash(0)) + pop(coinbase()) + pop(timestamp()) + pop(number()) + pop(difficulty()) + pop(gaslimit()) + + // NOTE: msize() is allowed only with optimizer disabled + //pop(msize()) + //pop(pc()) + } + } +} +// ==== +// EVMVersion: >=london +// ---- +// Warning 5740: (94-1733): Unreachable code. +// Warning 5740: (1746-1758): Unreachable code. +// Warning 5740: (1801-1810): Unreachable code. +// Warning 5740: (1978-2244): Unreachable code. diff --git a/test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_disallowed.sol b/test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_disallowed.sol new file mode 100644 index 000000000..992a51ad2 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_disallowed.sol @@ -0,0 +1,28 @@ +contract C { + function f() public { + assembly { + datasize(0) + dataoffset(0) + datacopy(0, 1, 2) + setimmutable(0, "x", 1) + loadimmutable("x") + linkersymbol("x") + memoryguard(0) + verbatim_1i_1o(hex"600202", 0) + + pop(msize()) + pop(pc()) + } + } +} +// ---- +// SyntaxError 6553: (47-362): The msize instruction cannot be used when the Yul optimizer is activated because it can change its semantics. Either disable the Yul optimizer or do not use the instruction. +// DeclarationError 4619: (70-78): Function "datasize" not found. +// DeclarationError 4619: (94-104): Function "dataoffset" not found. +// DeclarationError 4619: (120-128): Function "datacopy" not found. +// DeclarationError 4619: (150-162): Function "setimmutable" not found. +// DeclarationError 4619: (186-199): Function "loadimmutable" not found. +// DeclarationError 4619: (217-229): Function "linkersymbol" not found. +// DeclarationError 4619: (247-258): Function "memoryguard" not found. +// DeclarationError 4619: (274-288): Function "verbatim_1i_1o" not found. +// SyntaxError 2450: (347-349): PC instruction is a low-level EVM feature. Because of that PC is disallowed in strict assembly. diff --git a/test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_disallowed_pure.sol b/test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_disallowed_pure.sol new file mode 100644 index 000000000..f9a652fdb --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_disallowed_pure.sol @@ -0,0 +1,80 @@ +contract C { + function f() public pure { + assembly { + pop(sload(0)) + sstore(0, 1) + pop(gas()) + pop(address()) + pop(balance(0)) + pop(selfbalance()) + pop(caller()) + pop(callvalue()) + pop(extcodesize(0)) + extcodecopy(0, 1, 2, 3) + pop(extcodehash(0)) + pop(create(0, 1, 2)) + pop(create2(0, 1, 2, 3)) + pop(call(0, 1, 2, 3, 4, 5, 6)) + pop(callcode(0, 1, 2, 3, 4, 5, 6)) + pop(delegatecall(0, 1, 2, 3, 4, 5)) + pop(staticcall(0, 1, 2, 3, 4, 5)) + selfdestruct(0) + log0(0, 1) + log1(0, 1, 2) + log2(0, 1, 2, 3) + log3(0, 1, 2, 3, 4) + log4(0, 1, 2, 3, 4, 5) + pop(chainid()) + pop(basefee()) + pop(origin()) + pop(gasprice()) + pop(blockhash(0)) + pop(coinbase()) + pop(timestamp()) + pop(number()) + pop(difficulty()) + pop(gaslimit()) + + // These two are disallowed too but the error suppresses other errors. + //pop(msize()) + //pop(pc()) + } + } +} +// ==== +// EVMVersion: >=london +// ---- +// Warning 5740: (672-1083): Unreachable code. +// TypeError 2527: (79-87): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 8961: (101-113): Function cannot be declared as pure because this expression (potentially) modifies the state. +// TypeError 2527: (130-135): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 2527: (153-162): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 2527: (180-190): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 2527: (208-221): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 2527: (239-247): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 2527: (265-276): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 2527: (294-308): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 2527: (322-345): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 2527: (362-376): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 8961: (394-409): Function cannot be declared as pure because this expression (potentially) modifies the state. +// TypeError 8961: (427-446): Function cannot be declared as pure because this expression (potentially) modifies the state. +// TypeError 8961: (464-489): Function cannot be declared as pure because this expression (potentially) modifies the state. +// TypeError 8961: (507-536): Function cannot be declared as pure because this expression (potentially) modifies the state. +// TypeError 8961: (554-584): Function cannot be declared as pure because this expression (potentially) modifies the state. +// TypeError 2527: (602-630): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 8961: (644-659): Function cannot be declared as pure because this expression (potentially) modifies the state. +// TypeError 8961: (672-682): Function cannot be declared as pure because this expression (potentially) modifies the state. +// TypeError 8961: (695-708): Function cannot be declared as pure because this expression (potentially) modifies the state. +// TypeError 8961: (721-737): Function cannot be declared as pure because this expression (potentially) modifies the state. +// TypeError 8961: (750-769): Function cannot be declared as pure because this expression (potentially) modifies the state. +// TypeError 8961: (782-804): Function cannot be declared as pure because this expression (potentially) modifies the state. +// TypeError 2527: (821-830): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 2527: (848-857): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 2527: (875-883): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 2527: (901-911): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 2527: (929-941): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 2527: (959-969): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 2527: (987-998): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 2527: (1016-1024): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 2527: (1042-1054): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". +// TypeError 2527: (1072-1082): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". diff --git a/test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_disallowed_view.sol b/test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_disallowed_view.sol new file mode 100644 index 000000000..5daf91742 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/inline_assembly_instructions_disallowed_view.sol @@ -0,0 +1,38 @@ +contract C { + function f() public view { + assembly { + sstore(0, 1) + pop(create(0, 1, 2)) + pop(create2(0, 1, 2, 3)) + pop(call(0, 1, 2, 3, 4, 5, 6)) + pop(callcode(0, 1, 2, 3, 4, 5, 6)) + pop(delegatecall(0, 1, 2, 3, 4, 5)) + selfdestruct(0) + log0(0, 1) + log1(0, 1, 2) + log2(0, 1, 2, 3) + log3(0, 1, 2, 3, 4) + log4(0, 1, 2, 3, 4, 5) + + // These two are disallowed too but the error suppresses other errors. + //pop(msize()) + //pop(pc()) + } + } +} +// ==== +// EVMVersion: >=london +// ---- +// Warning 5740: (336-468): Unreachable code. +// TypeError 8961: (75-87): Function cannot be declared as view because this expression (potentially) modifies the state. +// TypeError 8961: (104-119): Function cannot be declared as view because this expression (potentially) modifies the state. +// TypeError 8961: (137-156): Function cannot be declared as view because this expression (potentially) modifies the state. +// TypeError 8961: (174-199): Function cannot be declared as view because this expression (potentially) modifies the state. +// TypeError 8961: (217-246): Function cannot be declared as view because this expression (potentially) modifies the state. +// TypeError 8961: (264-294): Function cannot be declared as view because this expression (potentially) modifies the state. +// TypeError 8961: (308-323): Function cannot be declared as view because this expression (potentially) modifies the state. +// TypeError 8961: (336-346): Function cannot be declared as view because this expression (potentially) modifies the state. +// TypeError 8961: (359-372): Function cannot be declared as view because this expression (potentially) modifies the state. +// TypeError 8961: (385-401): Function cannot be declared as view because this expression (potentially) modifies the state. +// TypeError 8961: (414-433): Function cannot be declared as view because this expression (potentially) modifies the state. +// TypeError 8961: (446-468): Function cannot be declared as view because this expression (potentially) modifies the state.