-
-
Notifications
You must be signed in to change notification settings - Fork 804
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
the use of `set` (which does not guarantee order of its elements) instead of `dict` (which guarantees insertion order) led to instability across runs of the compiler between different versions of python. this commit patches the problem by using a derivation of `dict` to track the call graph instead of `set`.
- Loading branch information
1 parent
f914011
commit 5ae0a07
Showing
3 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import random | ||
import string | ||
|
||
import hypothesis.strategies as st | ||
import pytest | ||
from hypothesis import given, settings | ||
|
||
import vyper.ast as vy_ast | ||
from vyper.compiler.phases import CompilerData | ||
|
||
|
||
# random names for functions | ||
@settings(max_examples=20, deadline=None) | ||
@given( | ||
st.lists( | ||
st.tuples( | ||
st.sampled_from(["@pure", "@view", "@nonpayable", "@payable"]), | ||
st.text(alphabet=string.ascii_lowercase, min_size=1), | ||
), | ||
unique_by=lambda x: x[1], # unique on function name | ||
min_size=1, | ||
max_size=10, | ||
) | ||
) | ||
@pytest.mark.fuzzing | ||
def test_call_graph_stability_fuzz(funcs): | ||
def generate_func_def(mutability, func_name, i): | ||
return f""" | ||
@internal | ||
{mutability} | ||
def {func_name}() -> uint256: | ||
return {i} | ||
""" | ||
|
||
func_defs = "\n".join(generate_func_def(m, s, i) for i, (m, s) in enumerate(funcs)) | ||
|
||
for _ in range(10): | ||
func_names = [f for (_, f) in funcs] | ||
random.shuffle(func_names) | ||
|
||
self_calls = "\n".join(f" self.{f}()" for f in func_names) | ||
code = f""" | ||
{func_defs} | ||
@external | ||
def foo(): | ||
{self_calls} | ||
""" | ||
t = CompilerData(code) | ||
|
||
# check the .called_functions data structure on foo() directly | ||
foo = t.vyper_module_folded.get_children(vy_ast.FunctionDef, filters={"name": "foo"})[0] | ||
foo_t = foo._metadata["type"] | ||
assert [f.name for f in foo_t.called_functions] == func_names | ||
|
||
# now for sanity, ensure the order that the function definitions appear | ||
# in the IR is the same as the order of the calls | ||
sigs = t.function_signatures | ||
del sigs["foo"] | ||
ir = t.ir_runtime | ||
ir_funcs = [] | ||
# search for function labels | ||
for d in ir.args: # currently: (seq ... (seq (label foo ...)) ...) | ||
if d.value == "seq" and d.args[0].value == "label": | ||
r = d.args[0].args[0].value | ||
if isinstance(r, str) and r.startswith("internal"): | ||
ir_funcs.append(r) | ||
assert ir_funcs == [f.internal_function_label for f in sigs.values()] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters