whamm 0.1.0

A framework for 'Wasm Application Monitoring and Manipulation'
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use crate::api::instrument::Config;
use crate::common::error::ErrorGen;
use crate::emitter::module_emitter::ModuleEmitter;
use crate::emitter::tag_handler::get_tag_for;
use crate::generator::ast::{Probe, Script, WhammParams};
use crate::generator::{create_curr_loc, emit_needed_funcs, GeneratingVisitor};
use crate::lang_features::alloc_vars::wei::UnsharedVarHandler;
use crate::lang_features::libraries::core::io::io_adapter::IOAdapter;
use crate::lang_features::report_vars::LocationData;
use crate::parser::types::{Block, DataType, Expr, Location, Statement, Value, WhammVisitorMut};
use crate::verifier::types::Record;
use log::trace;
use std::collections::{HashMap, HashSet};
use wirm::ir::id::{FunctionID, LocalID};
use wirm::ir::types::DataType as WirmType;
use wirm::Module;

pub struct WeiGenerator<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k, 'l, 'm, 'n> {
    pub emitter: ModuleEmitter<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h>,
    pub io_adapter: &'i mut IOAdapter,
    pub context_name: String,
    pub err: &'j mut ErrorGen,
    pub injected_funcs: &'k mut Vec<FunctionID>,
    pub config: &'l Config,
    pub used_exports_per_lib: HashMap<String, (bool, HashSet<String>)>,
    pub user_lib_modules: HashMap<String, (Option<String>, Module<'m>)>,

    // tracking
    pub curr_script_id: u8,
    pub unshared_var_handler: &'n mut UnsharedVarHandler,
}

impl WeiGenerator<'_, '_, '_, '_, '_, '_, '_, '_, '_, '_, '_, '_, '_, '_> {
    pub fn run(
        &mut self,
        mut ast: Vec<Script>,
        used_bound_funcs: HashSet<(String, String)>,
        used_report_dts: HashSet<DataType>,
        strings_to_emit: Vec<String>,
        has_probe_state_init: bool,
    ) {
        // Reset the symbol table in the emitter just in case
        self.emitter.reset_table();
        self.emitter.setup_module(false, has_probe_state_init);
        emit_needed_funcs(used_bound_funcs, &mut self.emitter, self.injected_funcs);
        self.emitter.emit_strings(strings_to_emit);
        self.visit_ast(&mut ast);

        self.emit_end_func(&ast, used_report_dts);
    }

    fn emit_end_func(&mut self, ast: &[Script], used_report_dts: HashSet<DataType>) {
        self.emitter
            .emit_end_fn(ast, used_report_dts, self.io_adapter, self.err);
    }

    // Visit the AST
    fn visit_ast(&mut self, ast: &mut [Script]) {
        for script in ast.iter_mut() {
            self.visit_script(script);
        }
    }

    fn visit_script(&mut self, script: &mut Script) {
        trace!("Entering: CodeGenerator::visit_script");
        self.enter_named_scope(&script.id.to_string());
        self.set_context_name(script.id.to_string());
        self.curr_script_id = script.id;

        self.set_curr_loc(LocationData::Global {
            script_id: script.id,
        });

        // visit fns
        script.fns.iter_mut().for_each(|f| {
            self.visit_fn(f);
        });
        // inject globals
        self.visit_globals(&script.globals);
        // visit global statements
        self.visit_global_stmts(&mut script.global_stmts);
        // visit probes
        script.probes.iter_mut().for_each(|probe| {
            self.visit_probe(probe);
        });

        trace!("Exiting: CodeGenerator::visit_script");
        self.exit_scope();
    }

    fn visit_probe(&mut self, probe: &mut Probe) {
        self.set_curr_loc(create_curr_loc(self.curr_script_id, probe));

        let (pred_fid, pred_param_str, dynamic_pred) = if let Some(pred) = &mut probe.predicate {
            if probe.metadata.pred_is_dynamic {
                // dynamic analysis of the predicate will go here!
                // See: https://github.com/ejrgilbert/whamm/issues/163

                // for now, we push the dynamic predicate down into the probe body

                (None, "".to_string(), Some(pred))
            } else {
                let mut block = Block {
                    stmts: vec![Statement::Expr {
                        expr: pred.clone(),
                        loc: None,
                    }],
                    results: None,
                    loc: None,
                };
                let (fid, str) = self.emitter.emit_special_func(
                    None,
                    &[],
                    &probe.metadata.pred_args,
                    None,
                    &[WirmType::I32],
                    &mut block,
                    true,
                    &probe.loc,
                    self.err,
                );
                (fid, str, None)
            }
        } else {
            (None, "".to_string(), None)
        };

        // create the probe's $alloc method
        let (alloc_fid, alloc_param_str) = self.unshared_var_handler.emit_alloc_func(
            &mut probe.unshared_to_alloc,
            &probe.metadata.init_args,
            &mut probe.init_logic,
            &mut self.emitter,
            self.err,
        );

        // create any @static evaluation functions
        let mut all_lib_calls = vec![];
        probe
            .static_lib_calls
            .iter()
            .for_each(|(params, lib_call)| {
                let mut block = Block {
                    stmts: vec![Statement::Expr {
                        expr: lib_call.clone(),
                        loc: None,
                    }],
                    results: None,
                    loc: None,
                };

                let ty = if let Expr::ObjCall { results, .. } = lib_call {
                    results.as_ref().unwrap().clone()
                } else {
                    self.err.add_internal_error(
                        "Results of a library call should have been set by the type checker!",
                        lib_call.loc(),
                    );
                    DataType::AssumeGood
                };
                let wirm_ty = match ty.to_wasm_type().first() {
                    Some(ty) => *ty,
                    None => {
                        self.err.add_internal_error(
                            &format!(
                                "Should have been able to convert the type to a Wasm type: {ty}"
                            ),
                            lib_call.loc(),
                        );
                        return;
                    }
                };

                let (fid, s) = self.emitter.emit_special_func(
                    None,
                    &[],
                    params,
                    None,
                    std::slice::from_ref(&wirm_ty),
                    &mut block,
                    true,
                    &probe.loc,
                    self.err,
                );
                all_lib_calls.push((fid, s, wirm_ty));
            });

        // create the probe body function
        let (body_fid, body_param_str) = if let Some(body) = &mut probe.body {
            let alloc_local = if alloc_fid.is_some() {
                Some(LocalID(0))
            } else {
                None
            };
            let (pred, body_block) = match (self.config.no_pred, self.config.no_body) {
                // as normal
                (false, false) => (dynamic_pred, body),
                // empty if statement
                (false, true) => (dynamic_pred, &mut Block::default()),
                // unpredicated body
                (true, false) => (None, body),
                // empty function
                (true, true) => (None, &mut Block::default()),
            };

            // since we're only supporting 'no_bundle' when 'no_body' and 'no_pred' are also true
            // we can simplify this to just not requesting any arguments...shouldn't even have a
            // function body!
            if self.config.no_bundle {
                assert!(pred.is_none());
                assert!(body_block.stmts.is_empty());
            }
            let no_params = WhammParams::default();
            let mut params = probe.metadata.body_args.clone();
            if pred.is_some() {
                // need to request predicate params now that we're pushing down into the body
                params.extend(probe.metadata.pred_args.clone());
            }

            self.emitter.emit_special_func(
                if self.config.no_bundle {
                    None
                } else {
                    alloc_local
                },
                &all_lib_calls,
                if self.config.no_bundle {
                    &no_params
                } else {
                    &params
                },
                pred,
                &[],
                body_block,
                false,
                &probe.loc,
                self.err,
            )
        } else {
            (None, "".to_string())
        };

        let match_rule = self.create_wei_match_rule(
            &probe.rule.to_string(),
            (pred_fid, &pred_param_str),
            (alloc_fid, &alloc_param_str),
            &all_lib_calls,
            &body_param_str,
        );
        if let Some(fid) = body_fid {
            self.emitter.app_wasm.exports.add_export_func_with_tag(
                match_rule,
                fid,
                get_tag_for(&None),
            );
        } else {
            self.err.add_probe_warn(
                "Are you sure you meant to emit a probe with no body?",
                &probe.loc.clone(),
            );
        }
    }

    fn create_wei_match_rule(
        &self,
        probe_name: &str,
        pred_fid: (Option<u32>, &str),
        alloc: (Option<u32>, &str),
        all_lib_calls: &[(Option<u32>, String, WirmType)],
        body_params: &str,
    ) -> String {
        let pred_part = if let (Some(pred_fid), pred_params) = pred_fid {
            format!("/ {} /", call(pred_fid, pred_params))
        } else {
            "".to_string()
        };
        let alloc_part = if let (Some(alloc_fid), alloc_params) = alloc {
            call(alloc_fid, alloc_params) + ", "
        } else {
            "".to_string()
        };
        let mut lib_calls_part = String::new();
        all_lib_calls
            .iter()
            .for_each(|(fid, params, _)| lib_calls_part += &(call(fid.unwrap(), params) + ", "));

        let body_part = alloc_part + &lib_calls_part + body_params;

        fn call(fid: u32, params: &str) -> String {
            format!("${fid}({params})")
        }

        format!("{probe_name} {pred_part} ({body_part})")
    }
}

impl GeneratingVisitor for WeiGenerator<'_, '_, '_, '_, '_, '_, '_, '_, '_, '_, '_, '_, '_, '_> {
    // TODO -- these are all duplicates, try to factor out
    fn add_internal_error(&mut self, message: &str, loc: &Option<Location>) {
        self.err.add_internal_error(message, loc);
    }
    fn emit_string(&mut self, val: &mut Value) -> bool {
        self.emitter.emit_string(val)
    }

    fn emit_func(&mut self, f: &mut crate::parser::types::Fn) -> Option<FunctionID> {
        self.emitter.emit_fn("TODO", f)
    }

    fn emit_global(
        &mut self,
        name: String,
        ty: DataType,
        value: &Option<Value>,
    ) -> Option<FunctionID> {
        self.emitter.emit_global(name, ty, value, self.err)
    }

    fn emit_report_global(
        &mut self,
        name: String,
        ty: DataType,
        value: &Option<Value>,
    ) -> Option<FunctionID> {
        self.emitter.emit_report_global(name, ty, value, self.err)
    }

    fn link_user_lib(&mut self, lib_name: &str, loc: &Option<Location>) {
        // Perform import now! (we'll be in the right table scope at this point)
        if let Some((used_mem, used_fns)) = self.used_exports_per_lib.get(lib_name) {
            let Some((lib_name_import_override, lib_wasm)) = self.user_lib_modules.get(lib_name)
            else {
                panic!("Could not find wasm module for library '{lib_name}'");
            };
            self.injected_funcs.extend(
                crate::lang_features::libraries::linking::import_lib::link_user_lib(
                    self.emitter.app_wasm,
                    loc,
                    lib_wasm,
                    lib_name.to_string(),
                    lib_name_import_override,
                    *used_mem,
                    used_fns,
                    self.emitter.table,
                ),
            );
        }
    }

    fn add_injected_func(&mut self, fid: FunctionID) {
        self.injected_funcs.push(fid);
    }

    fn get_context_name_mut(&mut self) -> &mut String {
        &mut self.context_name
    }

    fn get_context_name(&self) -> &String {
        &self.context_name
    }

    fn set_curr_loc(&mut self, loc: LocationData) {
        self.emitter.report_vars.curr_location = loc;
    }

    fn enter_named_scope(&mut self, name: &str) {
        self.emitter.table.enter_named_scope(name);
    }

    fn enter_scope(&mut self) {
        self.emitter.enter_scope();
    }

    fn exit_scope(&mut self) {
        self.emitter.exit_scope();
    }
    fn lookup_var_mut(&mut self, name: &str) -> Option<&mut Record> {
        self.emitter.table.lookup_var_mut(name, true)
    }

    fn visit_global_stmts(&mut self, stmts: &mut [Statement]) -> bool {
        // 1. create the emitting_func var, assign in self
        // 2. iterate over stmts and emit them! (will be different for Decl stmts)
        for stmt in stmts.iter_mut() {
            match stmt {
                Statement::Decl { .. } | Statement::UnsharedDecl { .. } => {} // already handled
                Statement::LibImport { lib_name, loc, .. } => {
                    self.link_user_lib(lib_name, loc);
                }
                Statement::Assign { .. } | Statement::Expr { .. } => {
                    // assume this is a valid AST node since we've gone through validation
                    self.emitter.emit_global_stmt(stmt, self.err);
                }
                Statement::UnsharedDeclInit { init, .. } => {
                    self.emitter.emit_global_stmt(init, self.err);
                }
                _ => {
                    self.err.add_unimplemented_error(&format!("We have not added support for this statement type in the script global scope: {stmt:?}"), stmt.loc());
                    return false;
                }
            }
        }
        true
    }
}