mirror of
https://github.com/kmc7468/cs420.git
synced 2025-12-15 06:58:50 +00:00
fuzz: add seed feature
fuzz: add easy flag (ease csmith)
This commit is contained in:
@@ -13,6 +13,7 @@ import itertools
|
|||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
|
import random
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
REPLACE_DICT = {
|
REPLACE_DICT = {
|
||||||
@@ -96,7 +97,7 @@ def install_csmith(tests_dir):
|
|||||||
|
|
||||||
return bin_path, inc_path
|
return bin_path, inc_path
|
||||||
|
|
||||||
def generate(tests_dir, bin_path):
|
def generate(tests_dir, bin_path, seed=None, easy=False):
|
||||||
"""Feeding options to built Csmith to randomly generate test case.
|
"""Feeding options to built Csmith to randomly generate test case.
|
||||||
|
|
||||||
For generality, I disabled most of the features that are enabled by default.
|
For generality, I disabled most of the features that are enabled by default.
|
||||||
@@ -111,6 +112,15 @@ def generate(tests_dir, bin_path):
|
|||||||
"--no-structs", "--no-unions",
|
"--no-structs", "--no-unions",
|
||||||
"--float", "--strict-float",
|
"--float", "--strict-float",
|
||||||
]
|
]
|
||||||
|
if seed is not None:
|
||||||
|
options += ["--seed", str(seed)]
|
||||||
|
if easy:
|
||||||
|
options += [
|
||||||
|
"--max-block-depth", "2",
|
||||||
|
"--max-block-size", "2",
|
||||||
|
"--max-struct-fields", "3",
|
||||||
|
"--inline-function-prob", "10",
|
||||||
|
]
|
||||||
args = [bin_path] + options
|
args = [bin_path] + options
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -202,7 +212,7 @@ def creduce(tests_dir, fuzz_arg):
|
|||||||
proc.kill()
|
proc.kill()
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
def fuzz(tests_dir, fuzz_arg, num_iter):
|
def fuzz(tests_dir, fuzz_arg, num_iter, easy=False):
|
||||||
global SKIP_TEST
|
global SKIP_TEST
|
||||||
|
|
||||||
csmith_bin, csmith_inc = install_csmith(tests_dir)
|
csmith_bin, csmith_inc = install_csmith(tests_dir)
|
||||||
@@ -217,7 +227,10 @@ def fuzz(tests_dir, fuzz_arg, num_iter):
|
|||||||
skip = 0
|
skip = 0
|
||||||
while True:
|
while True:
|
||||||
print("Test case #{} (skipped: {})".format(i, skip))
|
print("Test case #{} (skipped: {})".format(i, skip))
|
||||||
src = generate(tests_dir, csmith_bin)
|
src = generate(
|
||||||
|
tests_dir, csmith_bin,
|
||||||
|
seed = random.randint(1, 987654321), easy=easy
|
||||||
|
)
|
||||||
with open(os.path.join(tests_dir, "test.c"), 'w') as dst:
|
with open(os.path.join(tests_dir, "test.c"), 'w') as dst:
|
||||||
dst.write(src)
|
dst.write(src)
|
||||||
|
|
||||||
@@ -249,14 +262,18 @@ def fuzz(tests_dir, fuzz_arg, num_iter):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(description='Fuzzing KECC.')
|
parser = argparse.ArgumentParser(description='Fuzzing KECC.')
|
||||||
parser.add_argument('-n', '--num', type=int, help='The number of tests')
|
parser.add_argument('-n', '--num', type=int, help='The number of tests', default=None)
|
||||||
parser.add_argument('-p', '--print', action='store_true', help='Fuzzing C AST printer')
|
parser.add_argument('-p', '--print', action='store_true', help='Fuzzing C AST printer')
|
||||||
parser.add_argument('-i', '--irgen', action='store_true', help='Fuzzing irgen')
|
parser.add_argument('-i', '--irgen', action='store_true', help='Fuzzing irgen')
|
||||||
parser.add_argument('-r', '--reduce', action='store_true', help="Reducing input file")
|
parser.add_argument('-r', '--reduce', action='store_true', help="Reducing input file")
|
||||||
|
parser.add_argument('--skip-build', action='store_true', help="Skipping cargo build")
|
||||||
|
parser.add_argument('--easy', action='store_true', help="Generate more easy code by csmith option")
|
||||||
|
parser.add_argument('--seed', type=int, help="Provide seed of fuzz generation", default=-1)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if args.print and args.irgen:
|
if args.print and args.irgen:
|
||||||
raise Exception("Choose an option used for fuzzing: '--print' or '--irgen', NOT both")
|
raise Exception("Choose an option used for fuzzing: '--print' or '--irgen', NOT both")
|
||||||
|
|
||||||
if args.print:
|
if args.print:
|
||||||
fuzz_arg = "-p"
|
fuzz_arg = "-p"
|
||||||
elif args.irgen:
|
elif args.irgen:
|
||||||
@@ -264,17 +281,26 @@ if __name__ == "__main__":
|
|||||||
else:
|
else:
|
||||||
raise Exception("Specify fuzzing argument")
|
raise Exception("Specify fuzzing argument")
|
||||||
|
|
||||||
|
if args.seed != -1:
|
||||||
|
print('Set seed as', args.seed)
|
||||||
|
random.seed(args.seed)
|
||||||
|
else:
|
||||||
|
print('Use default random seed')
|
||||||
|
|
||||||
tests_dir = os.path.abspath(os.path.dirname(__file__))
|
tests_dir = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
|
||||||
print("Building KECC..")
|
if not args.skip_build:
|
||||||
try:
|
print("Building KECC..")
|
||||||
proc = subprocess.Popen(["cargo", "build", "--release"], cwd=tests_dir)
|
try:
|
||||||
proc.communicate()
|
proc = subprocess.Popen(["cargo", "build", "--release"], cwd=tests_dir)
|
||||||
except subprocess.TimeoutExpired as e:
|
proc.communicate()
|
||||||
proc.kill()
|
except subprocess.TimeoutExpired as e:
|
||||||
raise e
|
proc.kill()
|
||||||
|
raise e
|
||||||
|
else:
|
||||||
|
print("Skip building. You should manually build the binary. Please execute `cargo build --release` to build.")
|
||||||
|
|
||||||
if args.reduce:
|
if args.reduce:
|
||||||
creduce(tests_dir, fuzz_arg)
|
creduce(tests_dir, fuzz_arg)
|
||||||
else:
|
else:
|
||||||
fuzz(tests_dir, fuzz_arg, args.num)
|
fuzz(tests_dir, fuzz_arg, args.num, args.easy)
|
||||||
|
|||||||
Reference in New Issue
Block a user