Skip to content

Commit 9f48b74

Browse files
committed
Do not use named function labels if function names are not unique.
1 parent 863a0d3 commit 9f48b74

File tree

18 files changed

+429
-7
lines changed

18 files changed

+429
-7
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Bugfixes:
1919
* Commandline Interface: Don't return zero exit code when writing linked files to disk fails.
2020
* SMTChecker: Fix internal error in magic type access (``block``, ``msg``, ``tx``).
2121
* TypeChecker: Fix internal error when using user defined value types in public library functions.
22+
* Yul Assembler: Fix internal error when function names are not unique.
2223
* Yul IR Generator: Do not output empty switches/if-bodies for empty contracts.
2324

2425

libyul/backends/evm/AsmCodeGen.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ void CodeGenerator::assemble(
5252
builtinContext,
5353
_optimizeStackAllocation,
5454
_identifierAccessCodeGen,
55-
_useNamedLabelsForFunctions
55+
_useNamedLabelsForFunctions ?
56+
CodeTransform::UseNamedLabels::YesAndForceUnique :
57+
CodeTransform::UseNamedLabels::Never
5658
);
5759
transform(_parsedData);
5860
if (!transform.stackErrors().empty())

libyul/backends/evm/EVMCodeTransform.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ CodeTransform::CodeTransform(
5252
EVMDialect const& _dialect,
5353
BuiltinContext& _builtinContext,
5454
ExternalIdentifierAccess::CodeGenerator _identifierAccessCodeGen,
55-
bool _useNamedLabelsForFunctions,
55+
UseNamedLabels _useNamedLabelsForFunctions,
5656
shared_ptr<Context> _context,
5757
vector<TypedName> _delayedReturnVariables,
5858
optional<AbstractAssembly::LabelID> _functionExitLabel
@@ -405,8 +405,12 @@ void CodeTransform::operator()(FunctionDefinition const& _function)
405405
if (!m_allowStackOpt)
406406
subTransform.setupReturnVariablesAndFunctionExit();
407407

408+
subTransform.m_assignedNamedLabels = move(m_assignedNamedLabels);
409+
408410
subTransform(_function.body);
409411

412+
m_assignedNamedLabels = move(subTransform.m_assignedNamedLabels);
413+
410414
m_assembly.setSourceLocation(originLocationOf(_function));
411415
if (!subTransform.m_stackErrors.empty())
412416
{
@@ -585,8 +589,16 @@ void CodeTransform::createFunctionEntryID(FunctionDefinition const& _function)
585589
if (_function.debugData)
586590
astID = _function.debugData->astID;
587591

592+
bool nameAlreadySeen = !m_assignedNamedLabels.insert(_function.name).second;
593+
594+
if (m_useNamedLabelsForFunctions == UseNamedLabels::YesAndForceUnique)
595+
yulAssert(!nameAlreadySeen);
596+
588597
m_context->functionEntryIDs[&scopeFunction] =
589-
m_useNamedLabelsForFunctions ?
598+
(
599+
m_useNamedLabelsForFunctions != UseNamedLabels::Never &&
600+
!nameAlreadySeen
601+
) ?
590602
m_assembly.namedLabel(
591603
_function.name.str(),
592604
_function.parameters.size(),

libyul/backends/evm/EVMCodeTransform.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ struct CodeTransformContext
6464
class CodeTransform
6565
{
6666
public:
67+
/// Use named labels for functions 1) Yes and check that the names are unique
68+
/// 2) For none of the functions 3) for the first function of each name.
69+
enum class UseNamedLabels { YesAndForceUnique, Never, ForFirstFunctionOfEachName };
70+
6771
/// Create the code transformer.
6872
/// @param _identifierAccessCodeGen used to generate code for identifiers external to the inline assembly
6973
/// As a side-effect of its construction, translates the Yul code and appends it to the
@@ -78,7 +82,7 @@ class CodeTransform
7882
BuiltinContext& _builtinContext,
7983
bool _allowStackOpt = false,
8084
ExternalIdentifierAccess::CodeGenerator const& _identifierAccessCodeGen = {},
81-
bool _useNamedLabelsForFunctions = false
85+
UseNamedLabels _useNamedLabelsForFunctions = UseNamedLabels::Never
8286
): CodeTransform(
8387
_assembly,
8488
_analysisInfo,
@@ -108,7 +112,7 @@ class CodeTransform
108112
EVMDialect const& _dialect,
109113
BuiltinContext& _builtinContext,
110114
ExternalIdentifierAccess::CodeGenerator _identifierAccessCodeGen,
111-
bool _useNamedLabelsForFunctions,
115+
UseNamedLabels _useNamedLabelsForFunctions,
112116
std::shared_ptr<Context> _context,
113117
std::vector<TypedName> _delayedReturnVariables,
114118
std::optional<AbstractAssembly::LabelID> _functionExitLabel
@@ -193,7 +197,8 @@ class CodeTransform
193197
EVMDialect const& m_dialect;
194198
BuiltinContext& m_builtinContext;
195199
bool const m_allowStackOpt = true;
196-
bool const m_useNamedLabelsForFunctions = false;
200+
UseNamedLabels const m_useNamedLabelsForFunctions = UseNamedLabels::Never;
201+
std::set<YulString> m_assignedNamedLabels;
197202
ExternalIdentifierAccess::CodeGenerator m_identifierAccessCodeGen;
198203
std::shared_ptr<Context> m_context;
199204

libyul/backends/evm/EVMObjectCompiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void EVMObjectCompiler::run(Object& _object, bool _optimize)
7272
context,
7373
_optimize,
7474
{},
75-
true /* _useNamedLabelsForFunctions */
75+
CodeTransform::UseNamedLabels::ForFirstFunctionOfEachName
7676
};
7777
transform(*_object.code);
7878
if (!transform.stackErrors().empty())
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--experimental-via-ir --combined-json function-debug-runtime --pretty-json --json-indent 4
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: <SPDX-License>" to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.
2+
--> inline_assembly_function_name_clash/input.sol
3+
4+
Warning: Source file does not specify required compiler version!
5+
--> inline_assembly_function_name_clash/input.sol
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
contract C {
2+
uint x;
3+
modifier m() {
4+
uint t;
5+
assembly {
6+
function f() -> x { x := 8 }
7+
t := f()
8+
}
9+
x = t;
10+
_;
11+
}
12+
function f() m m public returns (uint r) {
13+
assembly { function f() -> x { x := 1 } r := f() }
14+
}
15+
function g() m m public returns (uint r) {
16+
assembly { function f() -> x { x := 2 } r := f() }
17+
}
18+
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
{
2+
"contracts":
3+
{
4+
"inline_assembly_function_name_clash/input.sol:C":
5+
{
6+
"function-debug-runtime":
7+
{
8+
"abi_decode_tuple_":
9+
{
10+
"entryPoint": 216,
11+
"parameterSlots": 2,
12+
"returnSlots": 0
13+
},
14+
"abi_encode_t_uint256_to_t_uint256_fromStack":
15+
{
16+
"entryPoint": 250,
17+
"parameterSlots": 2,
18+
"returnSlots": 0
19+
},
20+
"abi_encode_tuple_t_uint256__to_t_uint256__fromStack":
21+
{
22+
"entryPoint": 265,
23+
"parameterSlots": 2,
24+
"returnSlots": 1
25+
},
26+
"allocate_unbounded":
27+
{
28+
"entryPoint": 196,
29+
"parameterSlots": 0,
30+
"returnSlots": 1
31+
},
32+
"cleanup_t_uint256":
33+
{
34+
"entryPoint": 240,
35+
"parameterSlots": 1,
36+
"returnSlots": 1
37+
},
38+
"convert_t_uint256_to_t_uint256":
39+
{
40+
"entryPoint": 391,
41+
"parameterSlots": 1,
42+
"returnSlots": 1
43+
},
44+
"fun_f_25":
45+
{
46+
"entryPoint": 658,
47+
"id": 25,
48+
"parameterSlots": 0,
49+
"returnSlots": 1
50+
},
51+
"fun_f_25_inner":
52+
{
53+
"entryPoint": 624,
54+
"parameterSlots": 1,
55+
"returnSlots": 1
56+
},
57+
"fun_g_36":
58+
{
59+
"entryPoint": 874,
60+
"id": 36,
61+
"parameterSlots": 0,
62+
"returnSlots": 1
63+
},
64+
"fun_g_36_inner":
65+
{
66+
"entryPoint": 840,
67+
"parameterSlots": 1,
68+
"returnSlots": 1
69+
},
70+
"identity":
71+
{
72+
"entryPoint": 381,
73+
"parameterSlots": 1,
74+
"returnSlots": 1
75+
},
76+
"modifier_m_17":
77+
{
78+
"entryPoint": 470,
79+
"id": 14,
80+
"parameterSlots": 1,
81+
"returnSlots": 1
82+
},
83+
"modifier_m_19":
84+
{
85+
"entryPoint": 547,
86+
"id": 14,
87+
"parameterSlots": 1,
88+
"returnSlots": 1
89+
},
90+
"modifier_m_28":
91+
{
92+
"entryPoint": 686,
93+
"id": 14,
94+
"parameterSlots": 1,
95+
"returnSlots": 1
96+
},
97+
"modifier_m_30":
98+
{
99+
"entryPoint": 763,
100+
"id": 14,
101+
"parameterSlots": 1,
102+
"returnSlots": 1
103+
},
104+
"prepare_store_t_uint256":
105+
{
106+
"entryPoint": 425,
107+
"parameterSlots": 1,
108+
"returnSlots": 1
109+
},
110+
"revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74":
111+
{
112+
"entryPoint": 292,
113+
"parameterSlots": 0,
114+
"returnSlots": 0
115+
},
116+
"revert_error_ca66f745a3ce8ff40e2ccaf1ad45db7774001b90d25810abd9040049be7bf4bb":
117+
{
118+
"entryPoint": 206,
119+
"parameterSlots": 0,
120+
"returnSlots": 0
121+
},
122+
"revert_error_dbdddcbe895c83990c08b3492a0e83918d802a52331272ac6fdb6a7c4aea3b1b":
123+
{
124+
"entryPoint": 211,
125+
"parameterSlots": 0,
126+
"returnSlots": 0
127+
},
128+
"shift_left_0":
129+
{
130+
"entryPoint": 302,
131+
"parameterSlots": 1,
132+
"returnSlots": 1
133+
},
134+
"shift_right_224_unsigned":
135+
{
136+
"entryPoint": 183,
137+
"parameterSlots": 1,
138+
"returnSlots": 1
139+
},
140+
"update_byte_slice_32_shift_0":
141+
{
142+
"entryPoint": 315,
143+
"parameterSlots": 2,
144+
"returnSlots": 1
145+
},
146+
"update_storage_value_offset_0t_uint256_to_t_uint256":
147+
{
148+
"entryPoint": 435,
149+
"parameterSlots": 2,
150+
"returnSlots": 0
151+
},
152+
"usr$f":
153+
{
154+
"entryPoint": 493,
155+
"parameterSlots": 0,
156+
"returnSlots": 1
157+
},
158+
"zero_value_for_split_t_uint256":
159+
{
160+
"entryPoint": 297,
161+
"parameterSlots": 0,
162+
"returnSlots": 1
163+
}
164+
}
165+
}
166+
},
167+
"version": "<VERSION REMOVED>"
168+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--strict-assembly --debug-info none

0 commit comments

Comments
 (0)