-
Notifications
You must be signed in to change notification settings - Fork 12
support natspec preconditions #1070
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
base: master
Are you sure you want to change the base?
Conversation
} | ||
|
||
contract PreconditionTest is Test { | ||
/// @custom:kontrol-precondition x <= 7 * 2, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we don't run this test with CSE, this is the only precondition processed.
src/natspec/kdist/natspec-grammar.md
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you consider the following alternatives:
- Using a Solidity parser library (e.g. https://github.com/OpenZeppelin/sgp)
- Implementing the parser using a parser generator (e.g. https://github.com/lark-parser/lark)
With both approaches, you can avoid the burden of kompiling and distributing the parser. With (1), you get support for all the remaining features (x[1][2]
, etc.) out of the box.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't aware of sgp
, but I will look into it. There was a previous attempt at implementing this feature in #662 using Antlr. But we already have the Bison/Flex parsers shipped with K that we could use.
c586e17
to
aa0020d
Compare
aa0020d
to
2276824
Compare
This feature adds support for parsing and enforcing Solidity NatSpec preconditions (marked with
@custom:kontrol-precondition
) during symbolic execution.Define the NatSpec Grammar (natspec-grammar.md)
Created a K syntax module that defines how to parse Solidity expressions, including operators, precedence rules, and symbol annotations.
Set up KDist Build Target
Added an independent
KDist
target for NATSPEC that usesgen_glr_bison_parser
to compile the grammar into a binary parser.Extract Preconditions from Solidity
Modified
solc-to-k
to fetch preconditions from NatSpec devdocs, specifically looking for thecustom:kontrol-precondition
tag.Parse Precondition Strings
When generating the initial proof state (
init_cterm
), parse each precondition string using the compiled parser binary.Convert Kore to KAST
Transform the parser's Kore output into Kast using
kore_to_kast
.Map Operators to K Symbols
Use the
NATSPEC_TO_K_OPERATORS
dictionary to translate Solidity operators (e.g.,SolidityLE
/_<=Exp_
) to their K framework equivalents (e.g.,_<=Int_
).Transform Arguments Recursively
For each argument in the parsed expression:
Create a new KApply with the translated operator and transformed arguments.
Append the transformed preconditions as constraints to init_cterm, ensuring the symbolic execution respects these conditions.