-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This pull-request, still in-progress, adds a minimal fuzzer to the test-suite and patches up some bogus crashes and panics that were found almost immediately. Ouch. * Division by zero is caught. * Issues with the backtick operator were caught. * Issues with null-operands were caught * Though I kinda feel the parser is at fault here.
- Loading branch information
Showing
11 changed files
with
169 additions
and
2 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
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,67 @@ | ||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/skx/monkey/evaluator" | ||
"github.com/skx/monkey/lexer" | ||
"github.com/skx/monkey/object" | ||
"github.com/skx/monkey/parser" | ||
) | ||
|
||
// FuzzMonkey runs the fuzz-testing against our parser and interpreter. | ||
func FuzzMonkey(f *testing.F) { | ||
|
||
// Known errors we might see | ||
known := []string{ | ||
"as integer", | ||
"divide by zero", | ||
"null operand", | ||
"could not parse", | ||
"exceeded", | ||
"expected assign", | ||
"expected next token", | ||
"impossible", | ||
"nested ternary expressions are illegal", | ||
"no prefix parse function", | ||
} | ||
|
||
f.Fuzz(func(t *testing.T, input []byte) { | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) | ||
defer cancel() | ||
|
||
env := object.NewEnvironment() | ||
l := lexer.New(string(input)) | ||
p := parser.New(l) | ||
|
||
program := p.ParseProgram() | ||
falsePositive := false | ||
|
||
// No errors? Then execute | ||
if len(p.Errors()) == 0 { | ||
|
||
evaluator.EvalContext(ctx, program, env) | ||
return | ||
} | ||
|
||
for _, msg := range p.Errors() { | ||
for _, ignored := range known { | ||
if strings.Contains(msg, ignored) { | ||
falsePositive = true | ||
} | ||
} | ||
|
||
} | ||
|
||
if !falsePositive { | ||
t.Fatalf("error running input: '%s': %v", input, p.Errors()) | ||
} | ||
}) | ||
} |
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
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,2 @@ | ||
go test fuzz v1 | ||
[]byte("0%00") |
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,2 @@ | ||
go test fuzz v1 | ||
[]byte("`0`") |
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,2 @@ | ||
go test fuzz v1 | ||
[]byte("0b=") |
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,2 @@ | ||
go test fuzz v1 | ||
[]byte("\xe0.0A)000") |
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,2 @@ | ||
go test fuzz v1 | ||
[]byte("7\xdc%00000000000\"00000000000000") |
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,2 @@ | ||
go test fuzz v1 | ||
[]byte("``") |
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,2 @@ | ||
go test fuzz v1 | ||
[]byte("-") |