Convert fuzzer to istrs. Issue #855

This commit is contained in:
Brian Anderson 2011-08-31 15:09:08 -07:00
parent 81b31429e4
commit 04700066f3

View file

@ -21,30 +21,30 @@ import rustc::syntax::codemap;
import rustc::syntax::parse::parser;
import rustc::syntax::print::pprust;
fn write_file(filename: &str, content: &str) {
io::file_writer(istr::from_estr(filename), [io::create, io::truncate]).write_str(istr::from_estr(content));
fn write_file(filename: &istr, content: &istr) {
io::file_writer(filename, [io::create, io::truncate]).write_str(content);
// Work around https://github.com/graydon/rust/issues/726
std::run::run_program(~"chmod", [~"644", istr::from_estr(filename)]);
std::run::run_program(~"chmod", [~"644", filename]);
}
fn file_contains(filename: &str, needle: &str) -> bool {
let contents = istr::to_estr(io::read_whole_file_str(istr::from_estr(filename)));
ret str::find(contents, needle) != -1;
fn file_contains(filename: &istr, needle: &istr) -> bool {
let contents = io::read_whole_file_str(filename);
ret istr::find(contents, needle) != -1;
}
fn contains(haystack: &str, needle: &str) -> bool {
str::find(haystack, needle) != -1
fn contains(haystack: &istr, needle: &istr) -> bool {
istr::find(haystack, needle) != -1
}
fn find_rust_files(files: &mutable [str], path: str) {
if str::ends_with(path, ".rs") {
if file_contains(path, "xfail-test") {
fn find_rust_files(files: &mutable [istr], path: &istr) {
if istr::ends_with(path, ~".rs") {
if file_contains(path, ~"xfail-test") {
//log_err "Skipping " + path + " because it is marked as xfail-test";
} else { files += [path]; }
} 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));
} else if fs::file_is_dir(path)
&& istr::find(path, ~"compile-fail") == -1 {
for p in fs::list_dir(path) {
find_rust_files(files, p);
}
}
}
@ -151,14 +151,14 @@ iter under(n: uint) -> uint {
fn devnull() -> io::writer { std::io::string_writer().get_writer() }
fn as_str(f: fn(io::writer)) -> str {
fn as_str(f: fn(io::writer)) -> istr {
let w = std::io::string_writer();
f(w.get_writer());
ret istr::to_estr(w.get_str());
ret w.get_str();
}
fn check_variants_of_ast(crate: &ast::crate, codemap: &codemap::codemap,
filename: &str) {
filename: &istr) {
let exprs = steal_exprs(crate);
let exprsL = vec::len(exprs);
if exprsL < 100u {
@ -171,7 +171,7 @@ fn check_variants_of_ast(crate: &ast::crate, codemap: &codemap::codemap,
// string for stability is easier and ok for now.
let str3 =
as_str(bind pprust::print_crate(codemap, crate2,
istr::from_estr(filename),
filename,
io::string_reader(~""), _,
pprust::no_ann()));
// 1u would be sane here, but the pretty-printer currently has lots of whitespace and paren issues,
@ -187,61 +187,61 @@ fn check_variants_of_ast(crate: &ast::crate, codemap: &codemap::codemap,
// - 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";
fn check_whole_compiler(code: &istr) {
let filename = ~"test.rs";
write_file(filename, code);
let p =
std::run::program_output(
~"/Users/jruderman/code/rust/build/stage1/rustc",
[~"-c", istr::from_estr(filename)]);
[~"-c", 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") {
if contains(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")
} else if contains(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!") {
} else if contains(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, "!") {
} else if contains(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, "!") {
} else if contains(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, "!") {
} else if contains(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")) {
} else if contains(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") {
} else if contains(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") {
} else if contains(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") {
} else if contains(p.err, ~"pointer being freed was not allocated") &&
contains(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") {
} else if contains(p.out, ~"non-exhaustive match failure") &&
contains(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") {
} else if contains(p.out, ~"non-exhaustive match failure") &&
contains(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") {
} else if contains(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:") {
if !contains(p.out, ~"error:") {
fail "Exited with status 256 without a span-error";
}
} else if p.status == 11 {
@ -249,31 +249,31 @@ fn check_whole_compiler(code: &str) {
} else if p.status != 0 { fail "Unfamiliar status code"; }
}
fn parse_and_print(code: &str) -> str {
let filename = "tmp.rs";
fn parse_and_print(code: &istr) -> istr {
let filename = ~"tmp.rs";
let sess = @{cm: codemap::new_codemap(), mutable next_id: 0};
//write_file(filename, code);
let crate = parser::parse_crate_from_source_str(
istr::from_estr(filename), istr::from_estr(code), [], sess);
filename, code, [], sess);
ret as_str(bind pprust::print_crate(sess.cm, crate,
istr::from_estr(filename),
io::string_reader(istr::from_estr(code)), _,
filename,
io::string_reader(code), _,
pprust::no_ann()));
}
fn content_is_dangerous_to_modify(code: &str) -> bool {
fn content_is_dangerous_to_modify(code: &istr) -> bool {
let dangerous_patterns =
["obj", // not safe to steal; https://github.com/graydon/rust/issues/761
"#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
[~"obj", // not safe to steal; https://github.com/graydon/rust/issues/761
~"#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
for p: str in dangerous_patterns { if contains(code, p) { ret true; } }
for p: istr in dangerous_patterns { if contains(code, p) { ret true; } }
ret false;
}
fn content_is_confusing(code: &str) ->
fn content_is_confusing(code: &istr) ->
bool { // https://github.com/graydon/rust/issues/671
// https://github.com/graydon/rust/issues/669
// https://github.com/graydon/rust/issues/669
@ -283,16 +283,16 @@ fn content_is_confusing(code: &str) ->
// more precedence issues?
let confusing_patterns =
["#macro", "][]", "][mutable]", "][mutable ]", "self", "spawn",
"bind", "\n\n\n\n\n", // https://github.com/graydon/rust/issues/759
" : ", // https://github.com/graydon/rust/issues/760
"if ret", "alt ret", "if fail", "alt fail"];
[~"#macro", ~"][]", ~"][mutable]", ~"][mutable ]", ~"self", ~"spawn",
~"bind", ~"\n\n\n\n\n", // https://github.com/graydon/rust/issues/759
~" : ", // https://github.com/graydon/rust/issues/760
~"if ret", ~"alt ret", ~"if fail", ~"alt fail"];
for p: str in confusing_patterns { if contains(code, p) { ret true; } }
for p: istr in confusing_patterns { if contains(code, p) { ret true; } }
ret false;
}
fn file_is_confusing(filename: &str) -> bool {
fn file_is_confusing(filename: &istr) -> bool {
// https://github.com/graydon/rust/issues/674
@ -303,16 +303,16 @@ fn file_is_confusing(filename: &str) -> bool {
// which i can't reproduce using "rustc
// --pretty normal"???
let confusing_files =
["block-expr-precedence.rs", "nil-pattern.rs",
"syntax-extension-fmt.rs",
"newtype.rs"]; // modifying it hits something like https://github.com/graydon/rust/issues/670
[~"block-expr-precedence.rs", ~"nil-pattern.rs",
~"syntax-extension-fmt.rs",
~"newtype.rs"]; // modifying it hits something like https://github.com/graydon/rust/issues/670
for f in confusing_files { if contains(filename, f) { ret true; } }
ret false;
}
fn check_roundtrip_convergence(code: &str, maxIters: uint) {
fn check_roundtrip_convergence(code: &istr, maxIters: uint) {
let i = 0u;
let new = code;
@ -330,8 +330,8 @@ fn check_roundtrip_convergence(code: &str, maxIters: uint) {
log_err #ifmt["Converged after %u iterations", i];
} else {
log_err #ifmt["Did not converge after %u iterations!", i];
write_file("round-trip-a.rs", old);
write_file("round-trip-b.rs", new);
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"]);
@ -339,13 +339,13 @@ fn check_roundtrip_convergence(code: &str, maxIters: uint) {
}
}
fn check_convergence(files: &[str]) {
fn check_convergence(files: &[istr]) {
log_err #ifmt["pp convergence tests: %u files", vec::len(files)];
for file in files {
if !file_is_confusing(file) {
let s = istr::to_estr(io::read_whole_file_str(istr::from_estr(file)));
let s = io::read_whole_file_str(file);
if !content_is_confusing(s) {
log_err #ifmt["pp converge: %s", istr::from_estr(file)];
log_err #ifmt["pp converge: %s", file];
// Change from 7u to 2u when https://github.com/graydon/rust/issues/759 is fixed
check_roundtrip_convergence(s, 7u);
}
@ -353,22 +353,22 @@ fn check_convergence(files: &[str]) {
}
}
fn check_variants(files: &[str]) {
fn check_variants(files: &[istr]) {
for file in files {
if !file_is_confusing(file) {
let s = istr::to_estr(io::read_whole_file_str(istr::from_estr(file)));
let s = io::read_whole_file_str(file);
if content_is_dangerous_to_modify(s) || content_is_confusing(s) {
cont;
}
log_err "check_variants: " + file;
log_err ~"check_variants: " + file;
let sess = @{cm: codemap::new_codemap(), mutable next_id: 0};
let crate =
parser::parse_crate_from_source_str(
istr::from_estr(file),
istr::from_estr(s), [], sess);
file,
s, [], sess);
log_err as_str(bind pprust::print_crate(sess.cm, crate,
istr::from_estr(file),
io::string_reader(istr::from_estr(s)), _,
file,
io::string_reader(s), _,
pprust::no_ann()));
check_variants_of_ast(*crate, sess.cm, file);
}
@ -376,8 +376,9 @@ fn check_variants(files: &[str]) {
}
fn main(args: [str]) {
let args = istr::from_estrs(args);
if vec::len(args) != 2u {
log_err #ifmt["usage: %s <testdir>", istr::from_estr(args[0])];
log_err #ifmt["usage: %s <testdir>", args[0]];
ret;
}
let files = [];