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

Properly treat utf8-non-encodable yul literals. #11338

Merged
merged 2 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ Compiler Features:


Bugfixes:
* AST: Do not output value of Yul literal if it is not a valid UTF-8 string.


AST Changes:
* Add member `hexValue` for Yul string and hex literals.


### 0.8.4 (2021-04-21)
Expand Down
5 changes: 4 additions & 1 deletion libyul/AsmJsonConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <libyul/AST.h>
#include <libyul/Exceptions.h>
#include <libsolutil/CommonData.h>
#include <libsolutil/UTF8.h>

using namespace std;

Expand Down Expand Up @@ -63,10 +64,12 @@ Json::Value AsmJsonConverter::operator()(Literal const& _node) const
break;
case LiteralKind::String:
ret["kind"] = "string";
ret["hexValue"] = util::toHex(util::asBytes(_node.value.str()));
break;
}
ret["type"] = _node.type.str();
ret["value"] = _node.value.str();
if (util::validateUTF8(_node.value.str()))
ret["value"] = _node.value.str();
return ret;
}

Expand Down
7 changes: 6 additions & 1 deletion libyul/AsmJsonImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ Literal AsmJsonImporter::createLiteral(Json::Value const& _node)
auto lit = createAsmNode<Literal>(_node);
string kind = member(_node, "kind").asString();

lit.value = YulString{member(_node, "value").asString()};
solAssert(member(_node, "hexValue").isString() || member(_node, "value").isString(), "");
if (_node.isMember("hexValue"))
lit.value = YulString{util::asString(util::fromHex(member(_node, "hexValue").asString()))};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make sense to also use util::validateUTF8 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, why? Literals can be invalid utf8

else
lit.value = YulString{member(_node, "value").asString()};

lit.type= YulString{member(_node, "type").asString()};

if (kind == "number")
Expand Down
1 change: 1 addition & 0 deletions test/libsolidity/ASTJSON/assembly/stringlit.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"src": "58:14:1",
"value":
{
"hexValue": "616263",
"kind": "string",
"nodeType": "YulLiteral",
"src": "67:5:1",
Expand Down
1 change: 1 addition & 0 deletions test/libsolidity/ASTJSON/assembly/stringlit_parseOnly.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"src": "58:14:1",
"value":
{
"hexValue": "616263",
"kind": "string",
"nodeType": "YulLiteral",
"src": "67:5:1",
Expand Down
154 changes: 154 additions & 0 deletions test/libsolidity/ASTJSON/yul_hex_literal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
{
"absolutePath": "a",
"exportedSymbols":
{
"Sample":
[
6
]
},
"id": 7,
"nodeType": "SourceUnit",
"nodes":
[
{
"abstract": false,
"baseContracts": [],
"contractDependencies": [],
"contractKind": "contract",
"fullyImplemented": true,
"id": 6,
"linearizedBaseContracts":
[
6
],
"name": "Sample",
"nameLocation": "9:6:1",
"nodeType": "ContractDefinition",
"nodes":
[
{
"body":
{
"id": 4,
"nodeType": "Block",
"src": "47:167:1",
"statements":
[
{
"AST":
{
"nodeType": "YulBlock",
"src": "66:142:1",
"statements":
[
{
"nodeType": "YulVariableDeclaration",
"src": "80:15:1",
"value":
{
"hexValue": "74657374",
"kind": "string",
"nodeType": "YulLiteral",
"src": "89:6:1",
"type": "",
"value": "test"
},
"variables":
[
{
"name": "a",
"nodeType": "YulTypedName",
"src": "84:1:1",
"type": ""
}
]
},
{
"nodeType": "YulVariableDeclaration",
"src": "108:54:1",
"value":
{
"hexValue": "112233445566778899aabbccddeeff6677889900",
"kind": "string",
"nodeType": "YulLiteral",
"src": "117:45:1",
"type": ""
},
"variables":
[
{
"name": "b",
"nodeType": "YulTypedName",
"src": "112:1:1",
"type": ""
}
]
},
{
"nodeType": "YulVariableDeclaration",
"src": "175:23:1",
"value":
{
"hexValue": "1234abcd",
"kind": "string",
"nodeType": "YulLiteral",
"src": "184:14:1",
"type": ""
},
"variables":
[
{
"name": "c",
"nodeType": "YulTypedName",
"src": "179:1:1",
"type": ""
}
]
}
]
},
"evmVersion": %EVMVERSION%,
"externalReferences": [],
"id": 3,
"nodeType": "InlineAssembly",
"src": "57:151:1"
}
]
},
"functionSelector": "26121ff0",
"id": 5,
"implemented": true,
"kind": "function",
"modifiers": [],
"name": "f",
"nameLocation": "31:1:1",
"nodeType": "FunctionDefinition",
"parameters":
{
"id": 1,
"nodeType": "ParameterList",
"parameters": [],
"src": "32:2:1"
},
"returnParameters":
{
"id": 2,
"nodeType": "ParameterList",
"parameters": [],
"src": "47:0:1"
},
"scope": 6,
"src": "22:192:1",
"stateMutability": "pure",
"virtual": false,
"visibility": "public"
}
],
"scope": 7,
"src": "0:216:1",
"usedErrors": []
}
],
"src": "0:217:1"
}
11 changes: 11 additions & 0 deletions test/libsolidity/ASTJSON/yul_hex_literal.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
contract Sample {
function f() public pure {
assembly {
let a := "test"
let b := hex"112233445566778899aabbccddeeff6677889900"
let c := hex"1234_abcd"
}
}
}

// ----
139 changes: 139 additions & 0 deletions test/libsolidity/ASTJSON/yul_hex_literal_parseOnly.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{
"absolutePath": "a",
"id": 7,
"nodeType": "SourceUnit",
"nodes":
[
{
"abstract": false,
"baseContracts": [],
"contractDependencies": [],
"contractKind": "contract",
"id": 6,
"name": "Sample",
"nameLocation": "9:6:1",
"nodeType": "ContractDefinition",
"nodes":
[
{
"body":
{
"id": 4,
"nodeType": "Block",
"src": "47:167:1",
"statements":
[
{
"AST":
{
"nodeType": "YulBlock",
"src": "66:142:1",
"statements":
[
{
"nodeType": "YulVariableDeclaration",
"src": "80:15:1",
"value":
{
"hexValue": "74657374",
"kind": "string",
"nodeType": "YulLiteral",
"src": "89:6:1",
"type": "",
"value": "test"
},
"variables":
[
{
"name": "a",
"nodeType": "YulTypedName",
"src": "84:1:1",
"type": ""
}
]
},
{
"nodeType": "YulVariableDeclaration",
"src": "108:54:1",
"value":
{
"hexValue": "112233445566778899aabbccddeeff6677889900",
"kind": "string",
"nodeType": "YulLiteral",
"src": "117:45:1",
"type": ""
},
"variables":
[
{
"name": "b",
"nodeType": "YulTypedName",
"src": "112:1:1",
"type": ""
}
]
},
{
"nodeType": "YulVariableDeclaration",
"src": "175:23:1",
"value":
{
"hexValue": "1234abcd",
"kind": "string",
"nodeType": "YulLiteral",
"src": "184:14:1",
"type": ""
},
"variables":
[
{
"name": "c",
"nodeType": "YulTypedName",
"src": "179:1:1",
"type": ""
}
]
}
]
},
"evmVersion": %EVMVERSION%,
"externalReferences": [],
"id": 3,
"nodeType": "InlineAssembly",
"src": "57:151:1"
}
]
},
"id": 5,
"implemented": true,
"kind": "function",
"modifiers": [],
"name": "f",
"nameLocation": "31:1:1",
"nodeType": "FunctionDefinition",
"parameters":
{
"id": 1,
"nodeType": "ParameterList",
"parameters": [],
"src": "32:2:1"
},
"returnParameters":
{
"id": 2,
"nodeType": "ParameterList",
"parameters": [],
"src": "47:0:1"
},
"src": "22:192:1",
"stateMutability": "pure",
"virtual": false,
"visibility": "public"
}
],
"src": "0:216:1",
"usedErrors": []
}
],
"src": "0:217:1"
}