| 1 |
//! Parsing and econding for code motion mutators. |
| 2 |
use crate::{ |
| 3 |
Error, |
| 4 |
module::map_block_type, |
| 5 |
mutators::{OperatorAndByteOffset, codemotion::ir::parse_context::ParseContext}, |
| 6 |
}; |
| 7 |
use std::ops::Range; |
| 8 |
use wasm_encoder::Function; |
| 9 |
use wasmparser::{BlockType, Operator}; |
| 10 |
|
| 11 |
use self::parse_context::{Ast, Node, State}; |
| 12 |
|
| 13 |
/// Encodes an AST back to a Wasm function |
| 14 |
pub struct AstBuilder; |
| 15 |
pub(crate) mod parse_context; |
| 16 |
|
| 17 |
/// Encodes the Wasm Ast |
| 18 |
pub trait AstWriter { |
| 19 |
/// Encodes a loop node. |
| 20 |
/// |
| 21 |
/// Redefine this if your implementation mutates loops. For example, if your |
| 22 |
/// implementation unrolls the first iteration of a particular loop, override |
| 23 |
/// this method. For the other loops in the Wasm that your implementation is not |
| 24 |
/// mutating, you can call `write_loop_default`. |
| 25 |
fn write_loop<'a>( |
| 26 |
&self, |
| 27 |
ast: &Ast, |
| 28 |
nodeidx: usize, |
| 29 |
body: &[usize], |
| 30 |
newfunc: &mut Function, |
| 31 |
operators: &Vec<OperatorAndByteOffset>, |
| 32 |
input_wasm: &'a [u8], |
| 33 |
ty: &BlockType, |
| 34 |
) -> crate::Result<()> { |
| 35 |
self.write_loop_default(ast, nodeidx, body, newfunc, operators, input_wasm, ty) |
| 36 |
} |
| 37 |
|
| 38 |
/// Default encoding for a loop node |
| 39 |
/// |
| 40 |
/// This function is called by the defaut implementation |
| 41 |
/// of the `write_loop` method |
| 42 |
fn write_loop_default<'a>( |
| 43 |
&self, |
| 44 |
ast: &Ast, |
| 45 |
_nodeidx: usize, |
| 46 |
body: &[usize], |
| 47 |
newfunc: &mut Function, |
| 48 |
operators: &Vec<OperatorAndByteOffset>, |
| 49 |
input_wasm: &'a [u8], |
| 50 |
ty: &BlockType, |
| 51 |
) -> crate::Result<()> { |
| 52 |
newfunc.instructions().loop_(map_block_type(*ty)?); |
| 53 |
for ch in body { |
| 54 |
self.write(ast, *ch, newfunc, operators, input_wasm)?; |
| 55 |
} |
| 56 |
newfunc.instructions().end(); |
| 57 |
Ok(()) |
| 58 |
} |
| 59 |
|
| 60 |
/// Default encoding for a block node |
| 61 |
/// |
| 62 |
/// This function is called by the defaut implementation |
| 63 |
/// of the `write_block` method |
| 64 |
fn write_block_default<'a>( |
| 65 |
&self, |
| 66 |
ast: &Ast, |
| 67 |
_nodeidx: usize, |
| 68 |
body: &[usize], |
| 69 |
newfunc: &mut Function, |
| 70 |
operators: &Vec<OperatorAndByteOffset>, |
| 71 |
input_wasm: &'a [u8], |
| 72 |
ty: &BlockType, |
| 73 |
) -> crate::Result<()> { |
| 74 |
newfunc.instructions().block(map_block_type(*ty)?); |
| 75 |
for ch in body { |
| 76 |
self.write(ast, *ch, newfunc, operators, input_wasm)?; |
| 77 |
} |
| 78 |
newfunc.instructions().end(); |
| 79 |
Ok(()) |
| 80 |
} |
| 81 |
|
| 82 |
/// Encodes a block node. |
| 83 |
/// |
| 84 |
/// Redefine this if your implementation mutates blocks. For example, if your |
| 85 |
/// implementation modifies the internal structure of a particular block, by |
| 86 |
/// for example, inserting some nop operations between its children, override |
| 87 |
/// this method. For the other blocks in the Wasm that your implementation is not |
| 88 |
/// mutating, you can call `write_block_default`. |
| 89 |
/// |
| 90 |
fn write_block<'a>( |
| 91 |
&self, |
| 92 |
ast: &Ast, |
| 93 |
nodeidx: usize, |
| 94 |
body: &[usize], |
| 95 |
newfunc: &mut Function, |
| 96 |
operators: &Vec<OperatorAndByteOffset>, |
| 97 |
input_wasm: &'a [u8], |
| 98 |
ty: &BlockType, |
| 99 |
) -> crate::Result<()> { |
| 100 |
self.write_block_default(ast, nodeidx, body, newfunc, operators, input_wasm, ty) |
| 101 |
} |
| 102 |
|
| 103 |
/// Encodes a if/else node. |
| 104 |
/// |
| 105 |
/// Redefine this if your implementation mutates if/else constructions. For example, if your |
| 106 |
/// implementation modifies a particular if/else, override |
| 107 |
/// this method. For the other if/else constructions in the Wasm that your implementation is not |
| 108 |
/// mutating, you can call `write_if_else_default`. |
| 109 |
fn write_if_else<'a>( |
| 110 |
&self, |
| 111 |
ast: &Ast, |
| 112 |
nodeidx: usize, |
| 113 |
then: &[usize], |
| 114 |
alternative: &Option<Vec<usize>>, |
| 115 |
newfunc: &mut Function, |
| 116 |
operators: &Vec<OperatorAndByteOffset>, |
| 117 |
input_wasm: &'a [u8], |
| 118 |
ty: &BlockType, |
| 119 |
) -> crate::Result<()> { |
| 120 |
self.write_if_else_default( |
| 121 |
ast, |
| 122 |
nodeidx, |
| 123 |
then, |
| 124 |
alternative, |
| 125 |
newfunc, |
| 126 |
operators, |
| 127 |
input_wasm, |
| 128 |
ty, |
| 129 |
) |
| 130 |
} |
| 131 |
|
| 132 |
/// Default encoding for an if-else node |
| 133 |
/// |
| 134 |
/// This function is called by the defaut implementation |
| 135 |
/// of the `write_if_else` method |
| 136 |
fn write_if_else_default<'a>( |
| 137 |
&self, |
| 138 |
ast: &Ast, |
| 139 |
_nodeidx: usize, |
| 140 |
then: &[usize], |
| 141 |
alternative: &Option<Vec<usize>>, |
| 142 |
newfunc: &mut Function, |
| 143 |
operators: &Vec<OperatorAndByteOffset>, |
| 144 |
input_wasm: &'a [u8], |
| 145 |
ty: &BlockType, |
| 146 |
) -> crate::Result<()> { |
| 147 |
newfunc.instructions().if_(map_block_type(*ty)?); |
| 148 |
|
| 149 |
for ch in then { |
| 150 |
self.write(ast, *ch, newfunc, operators, input_wasm)?; |
| 151 |
} |
| 152 |
|
| 153 |
if let Some(alternative) = alternative { |
| 154 |
newfunc.instructions().else_(); |
| 155 |
|
| 156 |
for ch in alternative { |
| 157 |
self.write(ast, *ch, newfunc, operators, input_wasm)?; |
| 158 |
} |
| 159 |
} |
| 160 |
newfunc.instructions().end(); |
| 161 |
Ok(()) |
| 162 |
} |
| 163 |
|
| 164 |
/// Encodes a code node. |
| 165 |
/// |
| 166 |
/// Redefine this if your implementation mutates basic block constructions. For example, if your |
| 167 |
/// implementation replaces the basic block with an unreachable instruction, override |
| 168 |
/// this method. For the other nodes in the Wasm that your implementation is not |
| 169 |
/// mutating, you can call `write_if_else_default`. |
| 170 |
fn write_code<'a>( |
| 171 |
&self, |
| 172 |
_ast: &Ast, |
| 173 |
_nodeidx: usize, |
| 174 |
range: Range<usize>, |
| 175 |
newfunc: &mut Function, |
| 176 |
operators: &Vec<OperatorAndByteOffset>, |
| 177 |
input_wasm: &'a [u8], |
| 178 |
) -> crate::Result<()> { |
| 179 |
let operator_range = (range.start, range.end); |
| 180 |
let bytes_range = ( |
| 181 |
&operators[operator_range.0].1, |
| 182 |
&operators[operator_range.1].1, |
| 183 |
); |
| 184 |
let piece_of_code = &input_wasm[*bytes_range.0..*bytes_range.1]; |
| 185 |
newfunc.raw(piece_of_code.to_vec()); |
| 186 |
Ok(()) |
| 187 |
} |
| 188 |
|
| 189 |
/// Encoding discriminator for the Ast nodes |
| 190 |
/// |
| 191 |
/// It calls the corresponding methods depending on the node type |
| 192 |
fn write<'a>( |
| 193 |
&self, |
| 194 |
ast: &Ast, |
| 195 |
nodeidx: usize, |
| 196 |
newfunc: &mut Function, |
| 197 |
operators: &Vec<OperatorAndByteOffset>, |
| 198 |
input_wasm: &'a [u8], |
| 199 |
) -> crate::Result<()> { |
| 200 |
let node = &ast.get_nodes()[nodeidx]; |
| 201 |
|
| 202 |
match node { |
| 203 |
Node::IfElse { |
| 204 |
consequent, |
| 205 |
alternative, |
| 206 |
ty, |
| 207 |
} => { |
| 208 |
self.write_if_else( |
| 209 |
ast, |
| 210 |
nodeidx, |
| 211 |
consequent, |
| 212 |
alternative, |
| 213 |
newfunc, |
| 214 |
operators, |
| 215 |
input_wasm, |
| 216 |
ty, |
| 217 |
)?; |
| 218 |
} |
| 219 |
Node::Code { range } => { |
| 220 |
self.write_code(ast, nodeidx, range.clone(), newfunc, operators, input_wasm)?; |
| 221 |
} |
| 222 |
Node::Loop { body, ty, range: _ } => { |
| 223 |
self.write_loop(ast, nodeidx, body, newfunc, operators, input_wasm, ty)? |
| 224 |
} |
| 225 |
Node::Block { body, ty } => { |
| 226 |
self.write_block(ast, nodeidx, body, newfunc, operators, input_wasm, ty)? |
| 227 |
} |
| 228 |
Node::Root(body) => { |
| 229 |
for ch in body { |
| 230 |
self.write(ast, *ch, newfunc, operators, input_wasm)?; |
| 231 |
} |
| 232 |
// Closing end |
| 233 |
newfunc.instructions().end(); |
| 234 |
} |
| 235 |
} |
| 236 |
Ok(()) |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
impl AstWriter for Ast { |
| 241 |
/* It has the default implementation */ |
| 242 |
} |
| 243 |
|
| 244 |
impl AstBuilder { |
| 245 |
/// Returns an Ast from the operators collected from the Wasm function |
| 246 |
pub fn build_ast<'a>(&self, operators: &'a [OperatorAndByteOffset]) -> crate::Result<Ast> { |
| 247 |
self.parse(operators) |
| 248 |
} |
| 249 |
|
| 250 |
/// Parsing algorith to construct the Ast |
| 251 |
fn parse<'a>(&self, operators: &'a [OperatorAndByteOffset]) -> crate::Result<Ast> { |
| 252 |
let mut parse_context = ParseContext::default(); |
| 253 |
// Push the first frame, the root |
| 254 |
parse_context.push_frame(State::Root, None, 0); |
| 255 |
|
| 256 |
for (idx, (operator, _)) in operators.iter().enumerate() { |
| 257 |
match operator { |
| 258 |
Operator::If { blockty } => { |
| 259 |
// push current code first |
| 260 |
if !parse_context.current_code_is_empty() { |
| 261 |
parse_context.push_current_code_as_node(); |
| 262 |
} |
| 263 |
parse_context.reset_code_range_at(idx + 1); |
| 264 |
parse_context.push_state(); |
| 265 |
parse_context.push_frame(State::If, Some(*blockty), idx); |
| 266 |
} |
| 267 |
Operator::Else => { |
| 268 |
if !parse_context.current_code_is_empty() { |
| 269 |
parse_context.push_current_code_as_node(); |
| 270 |
} |
| 271 |
parse_context.reset_code_range_at(idx + 1); |
| 272 |
parse_context.push_state(); |
| 273 |
parse_context.push_frame(State::Else, None, idx); |
| 274 |
} |
| 275 |
Operator::Block { blockty } => { |
| 276 |
if !parse_context.current_code_is_empty() { |
| 277 |
parse_context.push_current_code_as_node(); |
| 278 |
} |
| 279 |
parse_context.reset_code_range_at(idx + 1); |
| 280 |
parse_context.push_state(); |
| 281 |
parse_context.push_frame(State::Block, Some(*blockty), idx); |
| 282 |
} |
| 283 |
Operator::Loop { blockty } => { |
| 284 |
if !parse_context.current_code_is_empty() { |
| 285 |
parse_context.push_current_code_as_node(); |
| 286 |
} |
| 287 |
parse_context.reset_code_range_at(idx + 1); |
| 288 |
parse_context.push_state(); |
| 289 |
parse_context.push_frame(State::Loop, Some(*blockty), idx); |
| 290 |
} |
| 291 |
Operator::TryTable { .. } => return Err(Error::no_mutations_applicable()), |
| 292 |
Operator::End => { |
| 293 |
if !parse_context.current_code_is_empty() { |
| 294 |
parse_context.push_current_code_as_node(); |
| 295 |
} |
| 296 |
parse_context.reset_code_range_at(idx + 1); |
| 297 |
|
| 298 |
let (last_frame, ty, frame_start) = parse_context.pop_frame()?; |
| 299 |
match last_frame { |
| 300 |
State::If => { |
| 301 |
let then_branch = parse_context.get_current_parsing(); |
| 302 |
parse_context.pop_state()?; |
| 303 |
// if return type of condition is empty, then the condition is the previous |
| 304 |
parse_context.push_node_to_current_parsing(Node::IfElse { |
| 305 |
consequent: then_branch, |
| 306 |
alternative: None, |
| 307 |
ty: ty.expect("Missing if type"), |
| 308 |
}); |
| 309 |
} |
| 310 |
State::Else => { |
| 311 |
let (last_frame, ty, _if_start) = parse_context.pop_frame()?; |
| 312 |
// Validate parent |
| 313 |
match last_frame { |
| 314 |
State::If => {} |
| 315 |
_ => unreachable!("Invalid parent frame"), |
| 316 |
} |
| 317 |
let else_branch = parse_context.get_current_parsing(); |
| 318 |
let then_branch = parse_context.pop_state()?; |
| 319 |
parse_context.pop_state()?; |
| 320 |
// if return type of condition is empty, then the condition is the previous |
| 321 |
parse_context.push_node_to_current_parsing(Node::IfElse { |
| 322 |
consequent: then_branch, |
| 323 |
alternative: Some(else_branch), |
| 324 |
ty: ty.expect("Missing if type"), |
| 325 |
}); |
| 326 |
} |
| 327 |
State::Loop => { |
| 328 |
let children = parse_context.get_current_parsing(); |
| 329 |
parse_context.pop_state()?; |
| 330 |
|
| 331 |
parse_context.push_node_to_current_parsing(Node::Loop { |
| 332 |
body: children, |
| 333 |
ty: ty.expect("Missing block type for loop"), |
| 334 |
range: frame_start..idx, |
| 335 |
}); |
| 336 |
} |
| 337 |
State::Block => { |
| 338 |
let children = parse_context.get_current_parsing(); |
| 339 |
parse_context.pop_state()?; |
| 340 |
|
| 341 |
parse_context.push_node_to_current_parsing(Node::Block { |
| 342 |
body: children, |
| 343 |
ty: ty.expect("Missing block type for loop"), |
| 344 |
}); |
| 345 |
} |
| 346 |
State::Root => { |
| 347 |
// break |
| 348 |
break; |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
_ => parse_context.append_instruction_to_current_code(), |
| 353 |
} |
| 354 |
} |
| 355 |
Ok(parse_context.finish()) |
| 356 |
} |
| 357 |
} |