Skip to content

Commit

Permalink
Merge pull request #14371 from ethereum/overrideMagicType_mobileType
Browse files Browse the repository at this point in the history
Disallow the use of `MagicType` in complex expressions
  • Loading branch information
cameel committed Jul 18, 2023
2 parents 4c4410e + f3fc190 commit 2c82873
Show file tree
Hide file tree
Showing 16 changed files with 124 additions and 4 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Compiler Features:


Bugfixes:
* Code Generator: Disallow complex expressions whose results are types, built-ins, modules or some unassignable functions. The legacy code generation pipeline would not actually evaluate them, discarding any side-effects they might have.
* Code Generator: Fix not entirely deterministic order of functions in unoptimized Yul output. The choice of C++ compiler in some cases would result in different (but equivalent) bytecode (especially from native binaries vs emscripten binaries)
* Commandline Interface: Fix internal error when using ``--stop-after parsing`` and requesting some of the outputs that require full analysis or compilation.
* Commandline Interface: It is no longer possible to specify both ``--optimize-yul`` and ``--no-optimize-yul`` at the same time.
Expand Down
2 changes: 2 additions & 0 deletions libsolidity/ast/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1694,6 +1694,8 @@ class MagicType: public Type

Type const* typeArgument() const;

Type const* mobileType() const override { return nullptr; }

protected:
std::vector<std::tuple<std::string, Type const*>> makeStackItems() const override { return {}; }
private:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
==== Source: A ====
contract C {
}
==== Source: B ====
import "A" as M;

contract C {
function f() public pure returns (bool) {
bool flag;
((flag = true) ? M : M).C;
return flag;
}
}
// ----
// f() -> true
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
contract C {
function f() public pure returns (bool){
bool flag;
((flag = true) ? (1, 2, 3) : (3, 2, 1));
return flag;
}
}
// ----
// f() -> true
4 changes: 2 additions & 2 deletions test/libsolidity/smtCheckerTests/special/msg_parens_1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ contract C {
// SMTIgnoreOS: macos
// SMTIgnoreCex: yes
// ----
// Warning 6328: (46-71): CHC: Assertion violation happens here.
// Warning 6328: (75-113): CHC: Assertion violation happens here.
// TypeError 9717: (90-93): Invalid mobile type in true expression.
// TypeError 3703: (96-99): Invalid mobile type in false expression.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
==== Source: A ====
function f() pure returns (uint) {
return 42;
}
==== Source: B ====
function f() pure returns (uint) {
return 24;
}
==== Source: C ====
import "A" as A;
import "B" as B;

contract C {
function f(bool b) public pure returns (uint) {
return (b ? A : B).f();
}
}
// ----
// TypeError 1080: (C:116-125): True expression's type module "A" does not match false expression's type module "B".
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
contract C {
function f() public {
[(1, 2, 3), (4, 5, 6)];
}
}
// ----
// TypeError 9656: (47-69): Unable to deduce nameable type for array elements. Try adding explicit type conversion for the first element.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ contract C {
}
}
// ----
// TypeError 9656: (47-52): Unable to deduce nameable type for array elements. Try adding explicit type conversion for the first element.
// TypeError 9563: (48-51): Invalid mobile type.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ contract C {
}
}
// ----
// TypeError 9656: (47-56): Unable to deduce nameable type for array elements. Try adding explicit type conversion for the first element.
// TypeError 9563: (48-55): Invalid mobile type.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
contract C {
function max(bool isUint) pure public returns (uint8) {
return (isUint ? type(uint8) : type(int8)).max;
}
}
// ----
// TypeError 9717: (98-109): Invalid mobile type in true expression.
// TypeError 3703: (112-122): Invalid mobile type in false expression.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
contract A {
function f() public {}
}

contract B {
function g() public {}
}

contract C {
function ab(bool getA) pure public returns (bytes memory) {
return (getA ? type(A) : type(B)).runtimeCode;
}
}
// ----
// TypeError 9717: (186-193): Invalid mobile type in true expression.
// TypeError 3703: (196-203): Invalid mobile type in false expression.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
contract C {
function max() public pure returns (uint8) {
return (type(uint8)).max;
}
}
// ----
6 changes: 6 additions & 0 deletions test/libsolidity/syntaxTests/metaTypes/type_max.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
contract C {
function max() public pure returns (uint8) {
return type(uint8).max;
}
}
// ----
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
contract C {
function max(bool isUint) public returns (uint8) {
return (isUint ? type(uint8) : type(int8)).max;
}
}
// ----
// TypeError 9717: (93-104): Invalid mobile type in true expression.
// TypeError 3703: (107-117): Invalid mobile type in false expression.
9 changes: 9 additions & 0 deletions test/libsolidity/syntaxTests/metaTypes/type_runtimecode.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
contract A {
}

contract C {
function f() public pure returns (bytes memory) {
return type(A).runtimeCode;
}
}
// ----
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
contract A {
}

contract B {
}

contract C {
function f(bool getA) public returns (bytes memory) {
return (getA ? type(A) : type(B)).runtimeCode;
}
}
// ----
// TypeError 9717: (126-133): Invalid mobile type in true expression.
// TypeError 3703: (136-143): Invalid mobile type in false expression.

0 comments on commit 2c82873

Please sign in to comment.