Skip to content

Commit 425b331

Browse files
jrudermanbrson
authored andcommitted
fuzzer.rs is now a pseudo-fuzzer that takes an AST and replaces expressions inside it
1 parent b4781bf commit 425b331

File tree

1 file changed

+111
-14
lines changed

1 file changed

+111
-14
lines changed

src/fuzzer/fuzzer.rs

Lines changed: 111 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,127 @@ import std::getopts::optopt;
77
import std::getopts::opt_present;
88
import std::getopts::opt_str;
99
import std::io;
10+
import std::io::stdout;
1011
import std::vec;
1112
import std::ivec;
1213
import std::str;
14+
import std::uint;
1315

14-
import rustc::back::link;
1516
import rustc::syntax::ast;
17+
import rustc::syntax::fold;
18+
import rustc::syntax::walk;
19+
import rustc::syntax::codemap;
20+
import rustc::syntax::print::pprust;
21+
1622
import driver = rustc::driver::rustc; // see https://github.com/graydon/rust/issues/624
23+
import rustc::back::link;
24+
import rustc::driver::rustc::time;
1725
import rustc::driver::session;
1826

27+
/*
28+
// Imports for "the rest of driver::compile_input"
29+
import rustc::metadata::creader;
30+
import rustc::metadata::cstore;
31+
import rustc::syntax::parse::parser;
32+
import rustc::syntax::parse::token;
33+
import rustc::front;
34+
import rustc::front::attr;
35+
import rustc::middle;
36+
import rustc::middle::trans;
37+
import rustc::middle::resolve;
38+
import rustc::middle::ty;
39+
import rustc::middle::typeck;
40+
import rustc::middle::tstate::ck;
41+
import rustc::syntax::print::pp;
42+
import rustc::util::ppaux;
43+
import rustc::lib::llvm;
44+
*/
1945

20-
fn find_rust_files(&mutable str[] files, str root) {
21-
for (str filename in fs::list_dir(root)) {
22-
if (str::ends_with(filename, ".rs")) {
23-
files += ~[filename];
46+
fn file_contains(&str filename, &str needle) -> bool {
47+
auto r = io::file_reader(filename);
48+
auto contents = str::unsafe_from_bytes(r.read_whole_stream());
49+
ret str::find(contents, needle) != -1;
50+
}
51+
52+
fn find_rust_files(&mutable str[] files, str path) {
53+
if (str::ends_with(path, ".rs")) {
54+
if (file_contains(path, "xfail-stage1")) {
55+
//log_err "Skipping " + path + " because it is marked as xfail-stage1";
56+
} else if (
57+
!str::ends_with(path, "constrained-type.rs") && // https://github.com/graydon/rust/issues/653
58+
str::find(path, "utf8") != -1 && // https://github.com/graydon/rust/issues/654
59+
true) {
60+
//log_err "Skipping " + path + " because of a known bug";
61+
} else {
62+
files += ~[path];
63+
}
64+
} else if (fs::file_is_dir(path) && str::find(path, "compile-fail") == -1) {
65+
for (str p in fs::list_dir(path)) {
66+
find_rust_files(files, p);
67+
}
68+
}
69+
}
70+
71+
fn steal_exprs(&ast::crate crate) -> ast::expr[] {
72+
let @mutable ast::expr[] exprs = @mutable ~[];
73+
// "Stash" cannot be type-parameterized because of https://github.com/graydon/rust/issues/375
74+
fn stash_expr(@mutable ast::expr[] es, &@ast::expr e) { *es += ~[*e]; }
75+
auto v = rec(visit_expr_pre = bind stash_expr(exprs, _) with walk::default_visitor());
76+
walk::walk_crate(v, crate);
77+
*exprs
78+
}
79+
80+
// https://github.com/graydon/rust/issues/652
81+
fn safe_to_replace(ast::expr_ e) -> bool {
82+
alt (e) {
83+
case (ast::expr_if(_, _, _)) { false }
84+
case (ast::expr_block(_)) { false }
85+
case (_) { true }
86+
}
87+
}
88+
89+
// Replace the |i|th expr (in fold order) of |crate| with |newexpr|.
90+
fn replace_expr_in_crate(&ast::crate crate, uint i, ast::expr_ newexpr) -> ast::crate {
91+
let @mutable uint j = @mutable 0u;
92+
fn fold_expr_rep(@mutable uint j_, uint i_, &ast::expr_ newexpr_, &ast::expr_ original, fold::ast_fold fld) -> ast::expr_ {
93+
*j_ += 1u;
94+
if (i_ + 1u == *j_ && safe_to_replace(original)) {
95+
newexpr_
96+
} else {
97+
fold::noop_fold_expr(original, fld)
98+
}
99+
}
100+
auto afp = rec(fold_expr = bind fold_expr_rep(j, i, newexpr, _, _) with *fold::default_ast_fold());
101+
auto af = fold::make_fold(afp);
102+
let @ast::crate crate2 = @af.fold_crate(crate);
103+
fold::dummy_out(af); // work around a leak (https://github.com/graydon/rust/issues/651)
104+
*crate2
105+
}
106+
107+
iter under(uint n) -> uint { let uint i = 0u; while (i < n) { put i; i += 1u; } }
108+
109+
fn devnull() -> io::writer { std::io::string_writer().get_writer() }
110+
111+
fn pp_variants(&ast::crate crate, &session::session sess, &str filename) {
112+
auto exprs = steal_exprs(crate);
113+
auto exprsL = ivec::len(exprs);
114+
if (exprsL < 100u) {
115+
for each (uint i in under(uint::min(exprsL, 20u))) {
116+
log_err "Replacing... " + pprust::expr_to_str(@exprs.(i));
117+
for each (uint j in under(uint::min(exprsL, 5u))) {
118+
log_err "With... " + pprust::expr_to_str(@exprs.(j));
119+
auto crate2 = @replace_expr_in_crate(crate, i, exprs.(j).node);
120+
pprust::print_crate(sess.get_codemap(), crate2, filename, devnull(), pprust::no_ann());
121+
}
24122
}
25123
}
26124
}
27125
28126
fn main(vec[str] args) {
29127
auto files = ~[];
30-
auto root = "/Users/jruderman/code/rust/src/lib/"; // XXX
31-
find_rust_files(files, root); // not using driver::time here because that currently screws with passing-a-mutable-array
128+
auto root = "/Users/jruderman/code/rust/src/"; // XXX
129+
find_rust_files(files, root); // not using time here because that currently screws with passing-a-mutable-array
130+
log_err uint::str(ivec::len(files)) + " files";
32131

33132
auto binary = vec::shift[str](args);
34133
auto binary_dir = fs::dirname(binary);
@@ -50,14 +149,12 @@ fn main(vec[str] args) {
50149
cfg=~[],
51150
test=false);
52151

53-
let session::session sess = driver::build_session(sopts);
54-
55-
log_err ivec::len(files);
56152
for (str file in files) {
57-
log_err file;
58-
// Can't use parse_input here because of https://github.com/graydon/rust/issues/632 :(
59-
//auto crate = driver::parse_input(sess, ~[], file);
60-
//let @ast::crate crate = driver::time(true, "parsing " + file, bind driver::parse_input(sess, ~[], file));
153+
log_err "=== " + file + " ===";
154+
let session::session sess = driver::build_session(sopts);
155+
let @ast::crate crate = time(true, "parsing " + file, bind driver::parse_input(sess, ~[], file));
156+
pprust::print_crate(sess.get_codemap(), crate, file, devnull(), pprust::no_ann());
157+
pp_variants(*crate, sess, file);
61158
}
62159
}
63160

0 commit comments

Comments
 (0)