rust/src/fuzzer/fuzzer.rs

432 lines
15 KiB
Rust
Raw Normal View History

2011-07-08 02:16:46 -07:00
use std;
use rustc;
2011-09-12 16:13:28 -07:00
import std::{fs, io, getopts, vec, str, uint, option};
import std::getopts::{optopt, opt_present, opt_str};
2011-08-11 19:14:38 -07:00
import std::io::stdout;
2011-05-20 21:06:05 -04:00
2011-09-12 16:13:28 -07:00
import rustc::syntax::{ast, fold, visit, codemap};
import rustc::syntax::parse::parser;
import rustc::syntax::print::pprust;
fn write_file(filename: str, content: str) {
2011-08-31 15:09:08 -07:00
io::file_writer(filename, [io::create, io::truncate]).write_str(content);
2011-07-28 00:29:24 -07:00
// Work around https://github.com/graydon/rust/issues/726
2011-09-02 15:34:58 -07:00
std::run::run_program("chmod", ["644", filename]);
}
fn file_contains(filename: str, needle: str) -> bool {
2011-08-31 15:09:08 -07:00
let contents = io::read_whole_file_str(filename);
ret str::find(contents, needle) != -1;
}
fn contains(haystack: str, needle: str) -> bool {
str::find(haystack, needle) != -1
2011-07-27 14:19:39 +02:00
}
2011-09-12 12:39:38 +02:00
fn find_rust_files(&files: [str], path: str) {
2011-09-02 15:34:58 -07:00
if str::ends_with(path, ".rs") {
if file_contains(path, "xfail-test") {
//log_err "Skipping " + path + " because it is marked as xfail-test";
} else { files += [path]; }
2011-08-31 15:09:08 -07:00
} else if fs::file_is_dir(path)
2011-09-02 15:34:58 -07:00
&& str::find(path, "compile-fail") == -1 {
2011-08-31 15:09:08 -07:00
for p in fs::list_dir(path) {
find_rust_files(files, p);
2011-08-24 16:00:26 -07:00
}
}
}
fn safe_to_steal_expr(e: @ast::expr) -> bool {
alt e.node {
2011-07-27 14:19:39 +02:00
2011-09-09 22:31:26 -07:00
// https://github.com/graydon/rust/issues/890
2011-07-27 14:19:39 +02:00
ast::expr_lit(lit) {
alt lit.node {
ast::lit_str(_) { true }
2011-07-27 14:19:39 +02:00
ast::lit_char(_) { true }
ast::lit_int(_) { false }
2011-09-09 22:31:26 -07:00
ast::lit_uint(_) { true }
2011-07-27 14:19:39 +02:00
ast::lit_mach_int(_, _) { false }
ast::lit_float(_) { false }
ast::lit_mach_float(_, _) { false }
ast::lit_nil. { true }
ast::lit_bool(_) { true }
}
2011-07-27 14:19:39 +02:00
}
2011-09-09 22:31:26 -07:00
// https://github.com/graydon/rust/issues/890
2011-07-27 14:19:39 +02:00
ast::expr_cast(_, _) { false }
ast::expr_assert(_) { false }
ast::expr_binary(_, _, _) { false }
ast::expr_assign(_, _) { false }
ast::expr_assign_op(_, _, _) { false }
2011-09-09 22:31:26 -07:00
// https://github.com/graydon/rust/issues/764
ast::expr_fail(option::none.) { false }
2011-07-28 00:29:24 -07:00
ast::expr_ret(option::none.) { false }
2011-07-27 14:19:39 +02:00
ast::expr_put(option::none.) { false }
2011-09-09 22:31:26 -07:00
// These prefix-operator keywords are not being parenthesized when in callee positions.
// https://github.com/graydon/rust/issues/891
ast::expr_ret(_) { false }
ast::expr_put(_) { false }
ast::expr_check(_, _) { false }
ast::expr_log(_, _) { false }
2011-09-09 22:31:26 -07:00
_ { true }
}
}
fn safe_to_steal_ty(t: @ast::ty) -> bool {
// Same restrictions
safe_to_replace_ty(t.node)
}
// Not type-parameterized: https://github.com/graydon/rust/issues/898
fn stash_expr_if(c: fn(@ast::expr)->bool, es: @mutable [ast::expr], e: @ast::expr) {
if c(e) {
*es += [*e];
} else {/* now my indices are wrong :( */ }
}
fn stash_ty_if(c: fn(@ast::ty)->bool, es: @mutable [ast::ty], e: @ast::ty) {
if c(e) {
*es += [*e];
} else {/* now my indices are wrong :( */ }
}
type stolen_stuff = {exprs: [ast::expr], tys: [ast::ty]};
fn steal(crate: ast::crate) -> stolen_stuff {
let exprs = @mutable [];
let tys = @mutable [];
let v = visit::mk_simple_visitor(@{
visit_expr: bind stash_expr_if(safe_to_steal_expr, exprs, _),
visit_ty: bind stash_ty_if(safe_to_steal_ty, tys, _)
with *visit::default_simple_visitor()
});
visit::visit_crate(crate, (), v);
{exprs: *exprs, tys: *tys}
}
// https://github.com/graydon/rust/issues/652
fn safe_to_replace_expr(e: ast::expr_) -> bool {
2011-07-27 14:19:39 +02:00
alt e {
ast::expr_if(_, _, _) { false }
ast::expr_block(_) { false }
_ { true }
}
}
fn safe_to_replace_ty(t: ast::ty_) -> bool {
alt t {
ast::ty_infer. { false } // always implicit, always top level
ast::ty_bot. { false } // in source, can only appear as the out type of a function
ast::ty_mac(_) { false }
_ { true }
}
}
// Replace the |i|th expr (in fold order) of |crate| with |newexpr|.
fn replace_expr_in_crate(crate: ast::crate, i: uint, newexpr: ast::expr) ->
2011-07-27 14:19:39 +02:00
ast::crate {
let j: @mutable uint = @mutable 0u;
fn fold_expr_rep(j_: @mutable uint, i_: uint, newexpr_: ast::expr_,
original: ast::expr_, fld: fold::ast_fold) ->
2011-07-27 14:19:39 +02:00
ast::expr_ {
*j_ += 1u;
if i_ + 1u == *j_ && safe_to_replace_expr(original) {
2011-07-27 14:19:39 +02:00
newexpr_
} else { fold::noop_fold_expr(original, fld) }
}
2011-07-27 14:19:39 +02:00
let afp =
{fold_expr: bind fold_expr_rep(j, i, newexpr.node, _, _)
with *fold::default_ast_fold()};
let af = fold::make_fold(afp);
let crate2: @ast::crate = @af.fold_crate(crate);
fold::dummy_out(af); // work around a leak (https://github.com/graydon/rust/issues/651)
*crate2
}
// Replace the |i|th ty (in fold order) of |crate| with |newty|.
fn replace_ty_in_crate(crate: ast::crate, i: uint, newty: ast::ty) ->
ast::crate {
let j: @mutable uint = @mutable 0u;
fn fold_ty_rep(j_: @mutable uint, i_: uint, newty_: ast::ty_,
original: ast::ty_, fld: fold::ast_fold) ->
ast::ty_ {
*j_ += 1u;
if i_ + 1u == *j_ && safe_to_replace_ty(original) {
newty_
} else { fold::noop_fold_ty(original, fld) }
}
let afp =
{fold_ty: bind fold_ty_rep(j, i, newty.node, _, _)
2011-07-27 14:19:39 +02:00
with *fold::default_ast_fold()};
let af = fold::make_fold(afp);
let crate2: @ast::crate = @af.fold_crate(crate);
fold::dummy_out(af); // work around a leak (https://github.com/graydon/rust/issues/651)
*crate2
}
2011-07-27 14:19:39 +02:00
iter under(n: uint) -> uint {
let i: uint = 0u;
while i < n { put i; i += 1u; }
}
2011-08-11 19:14:38 -07:00
fn devnull() -> io::writer { std::io::string_writer().get_writer() }
2011-09-02 15:34:58 -07:00
fn as_str(f: fn(io::writer)) -> str {
2011-08-11 19:14:38 -07:00
let w = std::io::string_writer();
2011-07-25 17:10:00 -07:00
f(w.get_writer());
2011-08-31 15:09:08 -07:00
ret w.get_str();
2011-07-25 17:10:00 -07:00
}
fn check_variants_of_ast(crate: ast::crate, codemap: codemap::codemap,
filename: str) {
let stolen = steal(crate);
check_variants_T(crate, codemap, filename, "expr", stolen.exprs, pprust::expr_to_str, replace_expr_in_crate);
check_variants_T(crate, codemap, filename, "ty", stolen.tys, pprust::ty_to_str, replace_ty_in_crate);
}
fn check_variants_T<T>(
crate: ast::crate,
codemap: codemap::codemap,
filename: str,
thing_label: str,
things: [T],
stringifier: fn(@T) -> str,
replacer: fn(ast::crate, uint, T) -> ast::crate
) {
log_err #fmt("%s contains %u %s objects", filename, vec::len(things), thing_label);
let L = vec::len(things);
if L < 100u {
for each i: uint in under(uint::min(L, 20u)) {
log_err "Replacing... " + stringifier(@things[i]);
for each j: uint in under(uint::min(L, 5u)) {
log_err "With... " + stringifier(@things[j]);
let crate2 = @replacer(crate, i, things[j]);
2011-07-28 00:29:24 -07:00
// It would be best to test the *crate* for stability, but testing the
// string for stability is easier and ok for now.
let str3 =
as_str(bind pprust::print_crate(codemap, crate2,
2011-08-31 15:09:08 -07:00
filename,
2011-09-02 15:34:58 -07:00
io::string_reader(""), _,
pprust::no_ann()));
2011-09-09 22:31:26 -07:00
check_roundtrip_convergence(str3, 1u);
//let file_label = #fmt("buggy_%s_%s_%u_%u.rs", last_part(filename), thing_label, i, j);
//check_whole_compiler(str3, file_label);
}
2011-05-20 21:06:05 -04:00
}
}
2011-07-08 02:16:46 -07:00
}
2011-05-20 21:06:05 -04:00
fn last_part(filename: str) -> str {
let ix = str::rindex(filename, 47u8 /* '/' */);
assert ix >= 0;
str::slice(filename, ix as uint + 1u, str::byte_len(filename) - 3u)
}
tag compile_result { known_bug(str); passed(str); failed(str); }
// We'd find more bugs if we could take an AST here, but
// - that would find many "false positives" or unimportant bugs
// - that would be tricky, requiring use of tasks or serialization or randomness.
// This seems to find plenty of bugs as it is :)
fn check_whole_compiler(code: str, suggested_filename: str) {
2011-09-02 15:34:58 -07:00
let filename = "test.rs";
write_file(filename, code);
alt check_whole_compiler_inner(filename) {
known_bug(s) {
log_err "Ignoring known bug: " + s;
}
failed(s) {
log_err "check_whole_compiler failure: " + s;
write_file(suggested_filename, code);
log_err "Saved as: " + suggested_filename;
}
passed(_) { }
}
}
fn check_whole_compiler_inner(filename: str) -> compile_result {
let p = std::run::program_output(
2011-09-02 15:34:58 -07:00
"/Users/jruderman/code/rust/build/stage1/rustc",
["-c", filename]);
//log_err #fmt("Status: %d", p.status);
2011-09-02 15:34:58 -07:00
if p.err != "" {
if contains(p.err, "May only branch on boolean predicates") {
known_bug("https://github.com/graydon/rust/issues/892")
} else if contains(p.err, "(S->getType()->isPointerTy() && \"Invalid cast\")") {
known_bug("https://github.com/graydon/rust/issues/895")
} else if contains(p.err, "Initializer type must match GlobalVariable type") {
known_bug("https://github.com/graydon/rust/issues/899")
} else if contains(p.err, "(castIsValid(op, S, Ty) && \"Invalid cast!\"), function Create") {
known_bug("https://github.com/graydon/rust/issues/901")
} else {
2011-09-02 15:34:58 -07:00
log_err "Stderr: " + p.err;
failed("Unfamiliar error message")
}
} else if p.status == 256 {
if contains(p.out, "Out of stack space, sorry") {
known_bug("Recursive types - https://github.com/graydon/rust/issues/742")
} else {
log_err "Stdout: " + p.out;
failed("Unfamiliar sudden exit")
}
} else if p.status == 6 {
if contains(p.out, "get_id_ident: can't find item in ext_map") {
known_bug("https://github.com/graydon/rust/issues/876")
} else if contains(p.out, "Assertion !cx.terminated failed") {
known_bug("https://github.com/graydon/rust/issues/893 or https://github.com/graydon/rust/issues/894")
} else if !contains(p.out, "error:") {
log_err "Stdout: " + p.out;
failed("Rejected the input program without a span-error explanation")
} else {
passed("Rejected the input program cleanly")
}
} else if p.status == 11 {
failed("Crashed!?")
} else if p.status == 0 {
passed("Accepted the input program")
} else {
log_err p.status;
log_err "!Stdout: " + p.out;
failed("Unfamiliar status code")
}
}
fn parse_and_print(code: str) -> str {
2011-09-02 15:34:58 -07:00
let filename = "tmp.rs";
let sess = @{cm: codemap::new_codemap(), mutable next_id: 0};
2011-07-28 00:29:24 -07:00
//write_file(filename, code);
2011-08-27 00:43:22 -07:00
let crate = parser::parse_crate_from_source_str(
2011-08-31 15:09:08 -07:00
filename, code, [], sess);
ret as_str(bind pprust::print_crate(sess.cm, crate,
2011-08-31 15:09:08 -07:00
filename,
io::string_reader(code), _,
2011-07-27 14:19:39 +02:00
pprust::no_ann()));
2011-07-25 17:10:00 -07:00
}
fn content_is_dangerous_to_modify(code: str) -> bool {
let dangerous_patterns =
2011-09-09 22:31:26 -07:00
["#macro", // not safe to steal things inside of it, because they have a special syntax
"#", // strange representation of the arguments to #fmt, for example
"tag", // typeck hang: https://github.com/graydon/rust/issues/900
2011-09-09 22:31:26 -07:00
" be "]; // don't want to replace its child with a non-call: "Non-call expression in tail call"
2011-07-28 00:29:24 -07:00
2011-09-02 15:34:58 -07:00
for p: str in dangerous_patterns { if contains(code, p) { ret true; } }
2011-07-28 00:29:24 -07:00
ret false;
}
fn content_is_confusing(code: str) -> bool {
let confusing_patterns =
2011-09-09 22:31:26 -07:00
["self", // crazy rules enforced by parser rather than typechecker?
"spawn", // precedence issues?
"bind", // precedence issues?
"\n\n\n\n\n" // https://github.com/graydon/rust/issues/850
];
2011-07-28 00:29:24 -07:00
2011-09-02 15:34:58 -07:00
for p: str in confusing_patterns { if contains(code, p) { ret true; } }
2011-07-25 17:10:00 -07:00
ret false;
}
fn file_is_confusing(filename: str) -> bool {
2011-09-09 22:31:26 -07:00
let confusing_files = [];
2011-07-25 17:10:00 -07:00
2011-07-28 00:29:24 -07:00
for f in confusing_files { if contains(filename, f) { ret true; } }
2011-07-25 17:10:00 -07:00
ret false;
}
fn check_roundtrip_convergence(code: str, maxIters: uint) {
2011-07-25 17:10:00 -07:00
2011-07-28 00:29:24 -07:00
let i = 0u;
2011-07-27 14:19:39 +02:00
let new = code;
let old = code;
2011-07-25 17:10:00 -07:00
2011-07-28 00:29:24 -07:00
while i < maxIters {
2011-07-25 17:10:00 -07:00
old = new;
2011-07-28 00:29:24 -07:00
if content_is_confusing(old) { ret; }
2011-07-25 17:10:00 -07:00
new = parse_and_print(old);
2011-07-28 00:29:24 -07:00
if old == new { break; }
i += 1u;
2011-07-25 17:10:00 -07:00
}
2011-07-28 00:29:24 -07:00
if old == new {
log_err #fmt["Converged after %u iterations", i];
2011-07-28 00:29:24 -07:00
} else {
log_err #fmt["Did not converge after %u iterations!", i];
2011-09-02 15:34:58 -07:00
write_file("round-trip-a.rs", old);
write_file("round-trip-b.rs", new);
std::run::run_program("diff",
["-w", "-u", "round-trip-a.rs",
"round-trip-b.rs"]);
2011-07-25 17:10:00 -07:00
fail "Mismatch";
}
}
2011-07-28 00:29:24 -07:00
fn check_convergence(files: [str]) {
log_err #fmt["pp convergence tests: %u files", vec::len(files)];
2011-07-28 00:29:24 -07:00
for file in files {
2011-07-27 14:19:39 +02:00
if !file_is_confusing(file) {
2011-08-31 15:09:08 -07:00
let s = io::read_whole_file_str(file);
2011-07-28 00:29:24 -07:00
if !content_is_confusing(s) {
log_err #fmt["pp converge: %s", file];
2011-09-09 22:31:26 -07:00
// Change from 7u to 2u once https://github.com/graydon/rust/issues/850 is fixed
2011-07-28 00:29:24 -07:00
check_roundtrip_convergence(s, 7u);
}
}
2011-07-28 00:29:24 -07:00
}
}
2011-07-25 17:10:00 -07:00
fn check_variants(files: [str]) {
2011-07-28 00:29:24 -07:00
for file in files {
if !file_is_confusing(file) {
2011-08-31 15:09:08 -07:00
let s = io::read_whole_file_str(file);
if content_is_dangerous_to_modify(s) || content_is_confusing(s) {
cont;
}
2011-09-02 15:34:58 -07:00
log_err "check_variants: " + file;
let sess = @{cm: codemap::new_codemap(), mutable next_id: 0};
let crate =
2011-08-27 00:43:22 -07:00
parser::parse_crate_from_source_str(
2011-08-31 15:09:08 -07:00
file,
s, [], sess);
log_err as_str(bind pprust::print_crate(sess.cm, crate,
2011-08-31 15:09:08 -07:00
file,
io::string_reader(s), _,
pprust::no_ann()));
check_variants_of_ast(*crate, sess.cm, file);
2011-07-28 00:29:24 -07:00
}
2011-07-08 02:16:46 -07:00
}
2011-07-27 14:19:39 +02:00
}
2011-07-26 15:38:48 -07:00
2011-09-02 15:34:58 -07:00
fn main(args: [str]) {
2011-08-15 16:38:23 -07:00
if vec::len(args) != 2u {
log_err #fmt["usage: %s <testdir>", args[0]];
2011-07-26 15:38:48 -07:00
ret;
}
let files = [];
let root = args[1];
2011-07-26 15:38:48 -07:00
find_rust_files(files, root);
check_convergence(files);
check_variants(files);
log_err "Fuzzer done";
2011-05-20 21:06:05 -04:00
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End: