rust/src/fuzzer/fuzzer.rs

400 lines
15 KiB
Rust
Raw Normal View History

2011-07-08 02:16:46 -07:00
use std;
use rustc;
2011-05-20 21:06:05 -04:00
import std::fs;
import std::getopts;
import std::getopts::optopt;
import std::getopts::opt_present;
import std::getopts::opt_str;
2011-08-11 19:14:38 -07:00
import std::io;
import std::io::stdout;
2011-08-15 16:38:23 -07:00
import std::vec;
2011-07-08 02:16:46 -07:00
import std::str;
2011-08-24 16:00:26 -07:00
import std::istr;
import std::uint;
import std::option;
2011-05-20 21:06:05 -04:00
2011-07-08 02:16:46 -07:00
import rustc::syntax::ast;
import rustc::syntax::fold;
2011-07-31 12:23:42 +02:00
import rustc::syntax::visit;
import rustc::syntax::codemap;
import rustc::syntax::parse::parser;
import rustc::syntax::print::pprust;
2011-07-27 14:19:39 +02:00
fn write_file(filename: &str, content: &str) {
2011-08-24 21:26:19 -07:00
io::file_writer(istr::from_estr(filename), [io::create, io::truncate]).write_str(istr::from_estr(content));
2011-07-28 00:29:24 -07:00
// Work around https://github.com/graydon/rust/issues/726
2011-08-24 18:40:57 -07:00
std::run::run_program(~"chmod", [~"644", istr::from_estr(filename)]);
}
2011-07-27 14:19:39 +02:00
fn file_contains(filename: &str, needle: &str) -> bool {
2011-08-24 21:26:19 -07:00
let contents = istr::to_estr(io::read_whole_file_str(istr::from_estr(filename)));
ret str::find(contents, needle) != -1;
}
2011-07-27 14:19:39 +02:00
fn contains(haystack: &str, needle: &str) -> bool {
str::find(haystack, needle) != -1
}
fn find_rust_files(files: &mutable [str], path: str) {
2011-07-27 14:19:39 +02: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-24 16:00:26 -07:00
} else if fs::file_is_dir(istr::from_estr(path))
&& str::find(path, "compile-fail") == -1 {
for p in fs::list_dir(istr::from_estr(path)) {
find_rust_files(files, istr::to_estr(p));
}
}
}
2011-07-27 14:19:39 +02:00
fn safe_to_steal(e: ast::expr_) -> bool {
alt e {
2011-07-27 14:19:39 +02:00
// pretty-printer precedence issues -- https://github.com/graydon/rust/issues/670
ast::expr_unary(_, _) {
false
}
ast::expr_lit(lit) {
alt lit.node {
ast::lit_str(_, _) { true }
ast::lit_char(_) { true }
ast::lit_int(_) { false }
ast::lit_uint(_) { false }
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
}
ast::expr_cast(_, _) { false }
ast::expr_assert(_) { false }
ast::expr_binary(_, _, _) { false }
ast::expr_assign(_, _) { false }
ast::expr_assign_op(_, _, _) { false }
ast::expr_fail(option::none.) {
false
/* https://github.com/graydon/rust/issues/764 */
}
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 }
ast::expr_ret(_) {
false
/* lots of code generation issues, such as https://github.com/graydon/rust/issues/770 */
}
ast::expr_fail(_) { false }
2011-07-27 14:19:39 +02:00
_ {
true
}
}
}
fn steal_exprs(crate: &ast::crate) -> [ast::expr] {
let exprs: @mutable [ast::expr] = @mutable [];
// "Stash" is not type-parameterized because of the need for safe_to_steal
fn stash_expr(es: @mutable [ast::expr], e: &@ast::expr) {
2011-07-27 14:19:39 +02:00
if safe_to_steal(e.node) {
*es += [*e];
2011-07-27 14:19:39 +02:00
} else {/* now my indices are wrong :( */ }
}
let v =
visit::mk_simple_visitor(@{visit_expr: bind stash_expr(exprs, _)
with *visit::default_simple_visitor()});
visit::visit_crate(crate, (), v);;
*exprs
}
// https://github.com/graydon/rust/issues/652
2011-07-27 14:19:39 +02:00
fn safe_to_replace(e: ast::expr_) -> bool {
alt e {
ast::expr_if(_, _, _) { false }
ast::expr_block(_) { false }
_ { true }
}
}
// Replace the |i|th expr (in fold order) of |crate| with |newexpr|.
2011-07-27 14:19:39 +02:00
fn replace_expr_in_crate(crate: &ast::crate, i: uint, newexpr: ast::expr_) ->
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) ->
ast::expr_ {
*j_ += 1u;
if i_ + 1u == *j_ && safe_to_replace(original) {
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, _, _)
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() }
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-24 21:26:19 -07:00
ret istr::to_estr(w.get_str());
2011-07-25 17:10:00 -07:00
}
fn check_variants_of_ast(crate: &ast::crate, codemap: &codemap::codemap,
filename: &str) {
2011-07-28 00:29:24 -07:00
let exprs = steal_exprs(crate);
2011-08-15 16:38:23 -07:00
let exprsL = vec::len(exprs);
if exprsL < 100u {
2011-07-28 00:29:24 -07:00
for each i: uint in under(uint::min(exprsL, 20u)) {
log_err ~"Replacing... " + pprust::expr_to_str(@exprs[i]);
2011-07-28 00:29:24 -07:00
for each j: uint in under(uint::min(exprsL, 5u)) {
log_err ~"With... " + pprust::expr_to_str(@exprs[j]);
let crate2 = @replace_expr_in_crate(crate, i, exprs[j].node);
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,
istr::from_estr(filename),
2011-08-24 21:26:19 -07:00
io::string_reader(~""), _,
pprust::no_ann()));
2011-07-28 00:29:24 -07:00
// 1u would be sane here, but the pretty-printer currently has lots of whitespace and paren issues,
// and https://github.com/graydon/rust/issues/766 is hilarious.
check_roundtrip_convergence(str3, 7u);
//check_whole_compiler(str3);
}
2011-05-20 21:06:05 -04:00
}
}
2011-07-08 02:16:46 -07:00
}
2011-05-20 21:06:05 -04:00
// 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) {
let filename = "test.rs";
write_file(filename, code);
let p =
2011-08-24 18:40:57 -07:00
std::run::program_output(
~"/Users/jruderman/code/rust/build/stage1/rustc",
[~"-c", istr::from_estr(filename)]);
//log_err #ifmt("Status: %d", p.status);
//log_err "Output: " + p.out;
if p.err != ~"" {
if contains(istr::to_estr(p.err), "argument of incompatible type") {
log_err "https://github.com/graydon/rust/issues/769";
} else if contains(istr::to_estr(p.err),
"Cannot create binary operator with two operands of differing type")
{
log_err "https://github.com/graydon/rust/issues/770";
} else if contains(istr::to_estr(p.err), "May only branch on boolean predicates!") {
log_err "https://github.com/graydon/rust/issues/770 or https://github.com/graydon/rust/issues/776";
} else if contains(istr::to_estr(p.err), "Invalid constantexpr cast!") &&
contains(code, "!") {
log_err "https://github.com/graydon/rust/issues/777";
} else if contains(istr::to_estr(p.err),
"Both operands to ICmp instruction are not of the same type!")
&& contains(code, "!") {
log_err "https://github.com/graydon/rust/issues/777 #issuecomment-1678487";
} else if contains(istr::to_estr(p.err), "Ptr must be a pointer to Val type!") &&
contains(code, "!") {
log_err "https://github.com/graydon/rust/issues/779";
} else if contains(istr::to_estr(p.err), "Calling a function with bad signature!") &&
(contains(code, "iter") || contains(code, "range")) {
log_err "https://github.com/graydon/rust/issues/771 - calling an iter fails";
} else if contains(istr::to_estr(p.err), "Calling a function with a bad signature!")
&& contains(code, "empty") {
log_err "https://github.com/graydon/rust/issues/775 - possibly a modification of run-pass/import-glob-crate.rs";
} else if contains(istr::to_estr(p.err), "Invalid type for pointer element!") &&
contains(code, "put") {
log_err "https://github.com/graydon/rust/issues/773 - put put ()";
} else if contains(istr::to_estr(p.err), "pointer being freed was not allocated") &&
contains(istr::to_estr(p.out), "Out of stack space, sorry") {
log_err "https://github.com/graydon/rust/issues/768 + https://github.com/graydon/rust/issues/778"
} else {
log_err ~"Stderr: " + p.err;
fail "Unfamiliar error message";
}
} else if contains(istr::to_estr(p.out), "non-exhaustive match failure") &&
contains(istr::to_estr(p.out), "alias.rs") {
log_err "https://github.com/graydon/rust/issues/772";
} else if contains(istr::to_estr(p.out), "non-exhaustive match failure") &&
contains(istr::to_estr(p.out), "trans.rs") && contains(code, "put") {
log_err "https://github.com/graydon/rust/issues/774";
} else if contains(istr::to_estr(p.out), "Out of stack space, sorry") {
log_err "Possibly a variant of https://github.com/graydon/rust/issues/768";
} else if p.status == 256 {
if !contains(istr::to_estr(p.out), "error:") {
fail "Exited with status 256 without a span-error";
}
} else if p.status == 11 {
log_err "What is this I don't even";
} else if p.status != 0 { fail "Unfamiliar status code"; }
}
2011-07-27 14:19:39 +02:00
fn parse_and_print(code: &str) -> str {
2011-07-28 00:29:24 -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(
istr::from_estr(filename), istr::from_estr(code), [], sess);
ret as_str(bind pprust::print_crate(sess.cm, crate,
istr::from_estr(filename),
2011-08-24 21:26:19 -07:00
io::string_reader(istr::from_estr(code)), _,
2011-07-27 14:19:39 +02:00
pprust::no_ann()));
2011-07-25 17:10:00 -07:00
}
2011-07-28 00:29:24 -07:00
fn content_is_dangerous_to_modify(code: &str) -> bool {
let dangerous_patterns =
["obj", // not safe to steal; https://github.com/graydon/rust/issues/761
2011-07-28 00:29:24 -07:00
"#macro", // not safe to steal things inside of it, because they have a special syntax
"#", // strange representation of the arguments to #ifmt, for example
" be ", // don't want to replace its child with a non-call: "Non-call expression in tail call"
"@"]; // hangs when compiling: https://github.com/graydon/rust/issues/768
2011-07-28 00:29:24 -07:00
for p: str in dangerous_patterns { if contains(code, p) { ret true; } }
ret false;
}
fn content_is_confusing(code: &str) ->
bool { // https://github.com/graydon/rust/issues/671
// https://github.com/graydon/rust/issues/669
// https://github.com/graydon/rust/issues/669
// https://github.com/graydon/rust/issues/669
// crazy rules enforced by parser rather than typechecker?
// more precedence issues
// more precedence issues?
let confusing_patterns =
2011-07-27 14:19:39 +02:00
["#macro", "][]", "][mutable]", "][mutable ]", "self", "spawn",
"bind", "\n\n\n\n\n", // https://github.com/graydon/rust/issues/759
2011-07-28 00:29:24 -07:00
" : ", // https://github.com/graydon/rust/issues/760
"if ret", "alt ret", "if fail", "alt fail"];
2011-07-28 00:29:24 -07:00
for p: str in confusing_patterns { if contains(code, p) { ret true; } }
2011-07-25 17:10:00 -07:00
ret false;
}
2011-07-27 14:19:39 +02:00
fn file_is_confusing(filename: &str) -> bool {
2011-07-25 17:10:00 -07:00
// https://github.com/graydon/rust/issues/674
2011-07-25 17:10:00 -07:00
// something to do with () as a lone pattern
2011-07-26 15:38:48 -07:00
// an issue where -2147483648 gains an
// extra negative sign each time through,
// which i can't reproduce using "rustc
// --pretty normal"???
let confusing_files =
2011-07-27 14:19:39 +02:00
["block-expr-precedence.rs", "nil-pattern.rs",
2011-07-28 00:29:24 -07:00
"syntax-extension-fmt.rs",
"newtype.rs"]; // modifying it hits something like https://github.com/graydon/rust/issues/670
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;
}
2011-07-28 00:29:24 -07:00
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 #ifmt["Converged after %u iterations", i];
2011-07-28 00:29:24 -07:00
} else {
log_err #ifmt["Did not converge after %u iterations!", i];
2011-07-25 17:10:00 -07:00
write_file("round-trip-a.rs", old);
write_file("round-trip-b.rs", new);
2011-08-24 18:40:57 -07:00
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 #ifmt["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-24 21:26:19 -07:00
let s = istr::to_estr(io::read_whole_file_str(istr::from_estr(file)));
2011-07-28 00:29:24 -07:00
if !content_is_confusing(s) {
log_err #ifmt["pp converge: %s", istr::from_estr(file)];
2011-07-28 00:29:24 -07:00
// Change from 7u to 2u when https://github.com/graydon/rust/issues/759 is fixed
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-24 21:26:19 -07:00
let s = istr::to_estr(io::read_whole_file_str(istr::from_estr(file)));
if content_is_dangerous_to_modify(s) || content_is_confusing(s) {
cont;
}
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(
istr::from_estr(file),
istr::from_estr(s), [], sess);
log_err as_str(bind pprust::print_crate(sess.cm, crate,
istr::from_estr(file),
2011-08-24 21:26:19 -07:00
io::string_reader(istr::from_estr(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
fn main(args: [str]) {
2011-08-15 16:38:23 -07:00
if vec::len(args) != 2u {
log_err #ifmt["usage: %s <testdir>", istr::from_estr(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: