From fd791951d1fa2faf5112dfa461db2dec92da0665 Mon Sep 17 00:00:00 2001 From: ahuoguo Date: Sun, 2 Jun 2024 14:06:11 -0400 Subject: [PATCH 1/2] print full ast --- src/common/error.rs | 35 ++++++++++++++--------------------- src/main.rs | 4 ++-- src/parser/print_visitor.rs | 21 ++++++++++----------- src/parser/tests.rs | 17 +++++++++++------ src/parser/whamm_parser.rs | 2 +- src/verifier/tests.rs | 4 ++-- 6 files changed, 40 insertions(+), 43 deletions(-) diff --git a/src/common/error.rs b/src/common/error.rs index 2986b9fd..248fbabc 100644 --- a/src/common/error.rs +++ b/src/common/error.rs @@ -34,7 +34,7 @@ impl ErrorGen { } pub fn add_error(&mut self, error: WhammError) { - let fatal = error.fatal.clone(); + let fatal = error.fatal; self.errors.push(error); self.inc_errors(); @@ -215,13 +215,9 @@ impl ErrorGen { // See code the following code for why we can do this: // https://github.com/pest-parser/pest/blob/master/pest/src/error.rs#L612 let mut lines = orig_msg.lines(); - if let Some(line) = lines.rfind(|line| { + lines.rfind(|line| { line.as_bytes()[0].is_ascii_digit() - }) { - Some(line.to_string()) - } else { - None - } + }).map(|line| line.to_string()) } else { None }; @@ -285,13 +281,10 @@ pub struct CodeLocation { } impl CodeLocation { pub fn is_span(&self) -> bool { - match self.line_col { - LineColLocation::Span(..) => true, - _ => false - } + matches!(self.line_col, LineColLocation::Span(..)) } pub fn lines_are_defined(&self) -> bool { - !self.line_str.is_none() + self.line_str.is_some() } // report this error to the console, including color highlighting @@ -333,7 +326,7 @@ impl CodeLocation { } } - fn define_lines(&mut self, script: &String) { + fn define_lines(&mut self, script: &str) { match &self.line_col { LineColLocation::Pos((line_no, ..)) => { if let Some(script_line) = script.lines().nth(line_no - 1) { @@ -353,7 +346,7 @@ impl CodeLocation { } } - fn print_numbered_line(&self, l: &usize, line: &String, s: &String, buffer: &mut Buffer) { + fn print_numbered_line(&self, l: &usize, line: &String, s: &str, buffer: &mut Buffer) { let w = s.len(); blue(false, format!("{l:w$} | "), buffer); white(false, format!("{line}\n"), buffer); @@ -381,7 +374,7 @@ impl CodeLocation { fn underline(&self, start_col: &usize) -> String { let mut underline = String::new(); - let mut start_col = start_col.clone(); + let mut start_col = *start_col; let end = match &self.line_col { LineColLocation::Span(_, (_, mut end)) => { let inverted_cols = start_col > end; @@ -415,10 +408,10 @@ impl CodeLocation { INFO_UNDERLINE_CHAR }; - underline.push(u_char.clone()); - if end - &start_col > 1 { - for _ in 2..(&end - &start_col) { - underline.push(u_char.clone()); + underline.push(u_char); + if end - start_col > 1 { + for _ in 2..(end - start_col) { + underline.push(u_char); } underline.push(u_char); } @@ -447,7 +440,7 @@ pub struct WhammError { } impl WhammError { pub fn is_fatal(&self) -> bool { - self.fatal.clone() + self.fatal } /// report this error to the console, including color highlighting @@ -637,7 +630,7 @@ impl ErrorType { let non_separated = f(&rules[l - 1]); let separated = rules .iter() - .take(l.clone() - 1) + .take(l - 1) .map(f) .collect::>() .join(", "); diff --git a/src/main.rs b/src/main.rs index cf859e46..7c714338 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,8 @@ use cli::{Cmd, WhammCli}; use crate::parser::whamm_parser::*; use crate::behavior::builder_visitor::*; use crate::generator::emitters::{Emitter, WasmRewritingEmitter}; -use crate::generator::init_generator::{InitGenerator}; -use crate::generator::instr_generator::{InstrGenerator}; +use crate::generator::init_generator::InitGenerator; +use crate::generator::instr_generator::InstrGenerator; use crate::common::error::ErrorGen; mod cli; diff --git a/src/parser/print_visitor.rs b/src/parser/print_visitor.rs index c691474d..6f53d60d 100644 --- a/src/parser/print_visitor.rs +++ b/src/parser/print_visitor.rs @@ -233,10 +233,8 @@ impl WhammVisitor for AsStrVisitor { self.increase_indent(); s += &format!("{} {}: ", self.get_indent(), name); - s += &format!("("); - for probe in probes.iter() { - self.visit_probe(probe); - } + s += &format!("({}", NL); + s += &probes.iter().map(|probe| self.visit_probe(probe)).collect::(); s += &format!("){}", NL); self.decrease_indent(); } @@ -273,7 +271,12 @@ impl WhammVisitor for AsStrVisitor { s += &format!("{} `predicate`:{}", self.get_indent(), NL); self.increase_indent(); match &probe.predicate { - Some(pred) => s += &format!("{} / {} /{}", self.get_indent(), self.visit_expr(pred), NL), + Some(pred) => { + let expr = self.visit_expr(pred); + expr.split("&&").for_each(|line| { + s += &format!("{}&& {}{}", self.get_indent(), line, NL) + }); + }, None => s += &format!("{} / None /{}", self.get_indent(), NL) } self.decrease_indent(); @@ -369,9 +372,7 @@ impl WhammVisitor for AsStrVisitor { s += &format!("{}(", self.visit_expr(fn_target)); match args { Some(args) => { - for arg in args { - s += &format!("{}, ", self.visit_expr(arg)); - } + s += &args.iter().map(|arg| self.visit_expr(arg)).collect::>().join(", "); }, _ => {} } @@ -445,9 +446,7 @@ impl WhammVisitor for AsStrVisitor { Value::Tuple {ty: _ty, vals} => { let mut s = "".to_string(); s += &format!("("); - for v in vals.iter() { - s += &format!("{}, ", self.visit_expr(v)); - } + s += &vals.iter().map(|arg| self.visit_expr(arg)).collect::>().join(", "); s += &format!(")"); s } diff --git a/src/parser/tests.rs b/src/parser/tests.rs index 092c5cd6..8d910189 100644 --- a/src/parser/tests.rs +++ b/src/parser/tests.rs @@ -47,7 +47,7 @@ const VALID_SCRIPTS: &'static [&'static str] = &[ // Function calls r#" -wasm::call:alt / strpaircmp((arg2, arg3), "record") / { +wasm::call:alt / strcmp((arg2, arg3), "record") / { new_target_fn_name = "redirect_to_fault_injector"; } "#, @@ -56,19 +56,24 @@ wasm::call:alt / target_fn_type == "import" && target_imp_module == "ic0" && target_imp_name == "call_new" && - strpaircmp((arg0, arg1), "bookings") && - strpaircmp((arg2, arg3), "record") + strcmp((arg0, arg1), "bookings") && + strcmp((arg2, arg3), "record") / { new_target_fn_name = "redirect_to_fault_injector"; } "#, - // Statements + // Statements (either assignment or function call) r#" wasm:bytecode:br:before { i = 0; } "#, + r#" + wasm:bytecode:br:before { + call_new(); + } + "#, // Comments r#" @@ -228,8 +233,8 @@ wasm::call:alt / target_fn_type == "import" && target_imp_module == "ic0" && target_imp_name == "call_new" && - strpaircmp((arg0, arg1), "bookings") && - strpaircmp((arg2, arg3), "record") + strcmp((arg0, arg1), "bookings") && + strcmp((arg2, arg3), "record") / { new_target_fn_name = "redirect_to_fault_injector"; } diff --git a/src/parser/whamm_parser.rs b/src/parser/whamm_parser.rs index 4b2af733..51a1bad7 100644 --- a/src/parser/whamm_parser.rs +++ b/src/parser/whamm_parser.rs @@ -6,7 +6,7 @@ use pest::error::{Error, LineColLocation}; use pest::Parser; use pest::iterators::{Pair, Pairs}; -use log::{trace}; +use log::trace; use crate::common::error::{ErrorGen, WhammError}; use crate::parser::types::{DataType, Script, Whamm, Expr, Statement, Value, Location, ProbeSpec, SpecPart}; diff --git a/src/verifier/tests.rs b/src/verifier/tests.rs index 3604d73a..80121c5a 100644 --- a/src/verifier/tests.rs +++ b/src/verifier/tests.rs @@ -51,8 +51,8 @@ wasm::call:alt / target_fn_type == "import" && target_imp_module == "ic0" && target_imp_name == "call_new" && - strpaircmp((arg0, arg1), "bookings") && - strpaircmp((arg2, arg3), "record") + strcmp((arg0, arg1), "bookings") && + strcmp((arg2, arg3), "record") / { new_target_fn_name = "redirect_to_fault_injector"; } From cf655f817d5ffaf886fc7c937758e2f6a669b73f Mon Sep 17 00:00:00 2001 From: ahuoguo Date: Sun, 2 Jun 2024 14:41:59 -0400 Subject: [PATCH 2/2] fix bad naming --- src/parser/print_visitor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser/print_visitor.rs b/src/parser/print_visitor.rs index 6f53d60d..99f56657 100644 --- a/src/parser/print_visitor.rs +++ b/src/parser/print_visitor.rs @@ -446,7 +446,7 @@ impl WhammVisitor for AsStrVisitor { Value::Tuple {ty: _ty, vals} => { let mut s = "".to_string(); s += &format!("("); - s += &vals.iter().map(|arg| self.visit_expr(arg)).collect::>().join(", "); + s += &vals.iter().map(|v| self.visit_expr(v)).collect::>().join(", "); s += &format!(")"); s }