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

Preserve the order of sons when splitting ternary node #1850

Merged
merged 5 commits into from
Apr 25, 2023

Conversation

Troublor
Copy link
Contributor

@Troublor Troublor commented Apr 20, 2023

Fix #1846

The order of sons of node matters.
Node with NodeType.IF relies on the order of the sons to determine which son is the true branch and which is the false branch.

However, the _split_ternary_node function during AST parsing does not preserve the son order of the father node.

for father in node.fathers:
father.remove_son(node)
father.add_son(condition_node.underlying_node)
condition_node.underlying_node.add_father(father)

When the father node is a node with multiple sons, and the index of the son to remove is not -1, the order of sones in the father node will be changed after remove_son and add_son.
As a result, the issue in #1846 occurs.

This PR adds a new utility method, replace_son, in Node class to preserve the order of sones when replacing a son.
So that #1846 can be fixed.

Two bug-revealing test cases are also added.

@Troublor
Copy link
Contributor Author

The parser tests fail due to this test case in ast-parsing test:

1 + 2 == 3 ? 4 + 5 == 6 ? int8(0) : -1 : -2;

It seems that the expected CFG is wrong due to the same issue #1846.
@0xalpharush Could you spare some time to look into the test case and confirm that the expected CFG is wrong?

@0xalpharush
Copy link
Member

@Troublor I haven't had a chance to investigate this, but your change likely requires updating some of the AST parsing tests which generate CFGs (they're not necessarily correct). If you would make your changes against dev, that would make this easier to merge in the future

@Troublor Troublor changed the base branch from master to dev April 21, 2023 16:00
@Troublor
Copy link
Contributor Author

@Troublor I haven't had a chance to investigate this, but your change likely requires updating some of the AST parsing tests which generate CFGs (they're not necessarily correct). If you would make your changes against dev, that would make this easier to merge in the future

@0xalpharush I have changed the base branch to dev. But the tests still fail due to the same reason (incorrect expected CFGs). Am I supposed to update the expected CFG in ast parsing tests myself?

@0xalpharush
Copy link
Member

You can delete the old artifacts for the failing : rm tests/e2e/solc_parsing/test_data/expected/conditional-all*

Then to regenerate the artifacts:

make dev
source ./env/bin/activate
python tests/e2e/solc_parsing/test_ast_parsing.py --generate

Then, add the updated artifacts to git.

At first glance, I think the update is right. The current CFG seems to incorrect point 14 - True -> 16, 14 - False -> 26 and that's correctly reversed in the new CFG.

@Troublor
Copy link
Contributor Author

You can delete the old artifacts for the failing : rm tests/e2e/solc_parsing/test_data/expected/conditional-all*

Then to regenerate the artifacts:

make dev
source ./env/bin/activate
python tests/e2e/solc_parsing/test_ast_parsing.py --generate

Then, add the updated artifacts to git.

At first glance, I think the update is right. The current CFG seems to incorrect point 14 - True -> 16, 14 - False -> 26 and that's correctly reversed in the new CFG.

Thanks for the guideline. I have updated the test artifacts, and the tests seem to be all passing.

@montyly
Copy link
Member

montyly commented Apr 25, 2023

Great catch and fix @Troublor. Nice addition to tests/unit/slithir/test_ssa_generation.py too. Thanks for the contribution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants