-
Notifications
You must be signed in to change notification settings - Fork 579
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
Add fuzzer based on honggfuzz #312
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,38 @@ | ||
# Fuzzing | ||
|
||
## Installing `honggfuzz` | ||
|
||
``` | ||
cargo install honggfuzz | ||
``` | ||
|
||
Install [dependencies](https://github.com/rust-fuzz/honggfuzz-rs#dependencies) for your system. | ||
|
||
## Running the fuzzer | ||
|
||
Running the fuzzer is as easy as running in the `fuzz` directory. | ||
|
||
Choose a target: | ||
|
||
These are `[[bin]]` entries in `Cargo.toml`. | ||
List them with `cargo read-manifest | jq '.targets[].name'` from the `fuzz` directory. | ||
|
||
Run the fuzzer: | ||
|
||
```shell | ||
cd fuzz | ||
cargo hfuzz run <target> | ||
``` | ||
|
||
After a panic is found, get a stack trace with: | ||
|
||
```shell | ||
cargo hfuzz run-debug <target> hfuzz_workspace/<target>/*.fuzz | ||
``` | ||
|
||
For example, with the `fuzz_parse_sql` target: | ||
|
||
```shell | ||
cargo hfuzz run fuzz_parse_sql | ||
cargo hfuzz run-debug fuzz_parse_sql hfuzz_workspace/fuzz_parse_sql/*.fuzz | ||
``` |
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,3 @@ | ||
corpus | ||
hfuzz_target | ||
hfuzz_workspace |
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,17 @@ | ||
[package] | ||
name = "fuzz" | ||
version = "0.1.0" | ||
edition = "2018" | ||
publish = false | ||
|
||
[dependencies] | ||
honggfuzz = "0.5.54" | ||
sqlparser = { path = ".." } | ||
|
||
# Prevent this from interfering with workspaces | ||
[workspace] | ||
members = ["."] | ||
|
||
[[bin]] | ||
name = "fuzz_parse_sql" | ||
path = "fuzz_targets/fuzz_parse_sql.rs" |
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,12 @@ | ||
use honggfuzz::fuzz; | ||
use sqlparser::dialect::GenericDialect; | ||
use sqlparser::parser::Parser; | ||
|
||
fn main() { | ||
loop { | ||
fuzz!(|data: String| { | ||
let dialect = GenericDialect {}; | ||
let _ = Parser::parse_sql(&dialect, &data); | ||
}); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -2202,8 +2202,8 @@ impl<'a> Parser<'a> { | |
Ok(Query { | ||
with, | ||
body, | ||
limit, | ||
order_by, | ||
limit, | ||
offset, | ||
fetch, | ||
}) | ||
|
@@ -2358,8 +2358,7 @@ impl<'a> Parser<'a> { | |
]) // This couldn't possibly be a bad idea | ||
})? | ||
.into_iter() | ||
.filter(|i| i.is_some()) | ||
.map(|i| i.unwrap()) | ||
.flatten() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it correct that this is a real bug that was found/fixed with the fuzzer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, just make clippy happy |
||
.collect(); | ||
|
||
lateral_views.push(LateralView { | ||
|
@@ -2414,8 +2413,8 @@ impl<'a> Parser<'a> { | |
top, | ||
projection, | ||
from, | ||
selection, | ||
lateral_views, | ||
selection, | ||
group_by, | ||
cluster_by, | ||
distribute_by, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is pretty cool. Where does the data come from? Is it just random strings? Or does this somehow know how to make valid sql statements?
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 see some related comments here: #211 (comment)
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.
For now it is some random bytes, but can be used to find bugs. Optimization can be continued later, refer to: https://www.cockroachlabs.com/blog/sqlsmith-randomized-sql-testing/
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.
In a past project, we had a harness that could generate random SQL queries and it found many bugs -- such tests are a wonderful way to help database software mature
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.
Filed apache/datafusion#913 for DataFusion. Thanks for the pointer @PsiACE