1 use super::AddressTransform;
2 use super::expression::{CompiledExpression, FunctionFrameInfo};
3 use super::utils::append_vmctx_info;
4 use crate::debug::Compilation;
5 use crate::translate::get_vmctx_value_label;
6 use anyhow::{Context, Error};
7 use cranelift_codegen::isa::TargetIsa;
8 use gimli::LineEncoding;
9 use gimli::write;
10 use std::collections::{HashMap, HashSet};
11 use std::path::PathBuf;
12 use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
13 use wasmtime_environ::{
14     DebugInfoData, EntityRef, FunctionMetadata, PrimaryMap, StaticModuleIndex, WasmFileInfo,
15     WasmValType,
16 };
17 
18 const PRODUCER_NAME: &str = "wasmtime";
19 
20 macro_rules! assert_dwarf_str {
21     ($s:expr) => {{
22         let s = $s;
23         if cfg!(debug_assertions) {
24             // Perform check the same way as gimli does it.
25             let bytes: Vec<u8> = s.clone().into();
26             debug_assert!(!bytes.contains(&0), "DWARF string shall not have NULL byte");
27         }
28         s
29     }};
30 }
31 
32 fn generate_line_info(
33     addr_tr: &PrimaryMap<StaticModuleIndex, AddressTransform>,
34     translated: &HashSet<usize>,
35     out_encoding: gimli::Encoding,
36     w: &WasmFileInfo,
37     comp_dir_id: write::StringId,
38     name_id: write::StringId,
39     name: &str,
40 ) -> Result<(write::LineProgram, write::FileId), Error> {
41     let out_comp_dir = write::LineString::StringRef(comp_dir_id);
42     let out_comp_name = write::LineString::StringRef(name_id);
43 
44     let line_encoding = LineEncoding::default();
45 
46     let mut out_program = write::LineProgram::new(
47         out_encoding,
48         line_encoding,
49         out_comp_dir,
50         out_comp_name,
51         None,
52     );
53 
54     let file_index = out_program.add_file(
55         write::LineString::String(name.as_bytes().to_vec()),
56         out_program.default_directory(),
57         None,
58     );
59 
60     let maps = addr_tr.iter().flat_map(|(_, transform)| {
61         transform.map().iter().filter_map(|(_, map)| {
62             if translated.contains(&map.symbol) {
63                 None
64             } else {
65                 Some((map.symbol, map))
66             }
67         })
68     });
69 
70     for (symbol, map) in maps {
71         let base_addr = map.offset;
72         out_program.begin_sequence(Some(write::Address::Symbol {
73             symbol,
74             addend: base_addr as i64,
75         }));
76 
77         // Always emit a row for offset zero - debuggers expect this.
78         out_program.row().address_offset = 0;
79         out_program.row().file = file_index;
80         out_program.row().line = 0; // Special line number for non-user code.
81         out_program.row().discriminator = 1;
82         out_program.row().is_statement = true;
83         out_program.generate_row();
84 
85         let mut is_prologue_end = true;
86         for addr_map in map.addresses.iter() {
87             let address_offset = (addr_map.generated - base_addr) as u64;
88             out_program.row().address_offset = address_offset;
89             let wasm_offset = w.code_section_offset + addr_map.wasm;
90             out_program.row().line = wasm_offset;
91             out_program.row().discriminator = 1;
92             out_program.row().prologue_end = is_prologue_end;
93             out_program.generate_row();
94 
95             is_prologue_end = false;
96         }
97         let end_addr = (base_addr + map.len - 1) as u64;
98         out_program.end_sequence(end_addr);
99     }
100 
101     Ok((out_program, file_index))
102 }
103 
104 fn check_invalid_chars_in_name(s: &str) -> Option<&str> {
105     if s.contains('\x00') { None } else { Some(s) }
106 }
107 
108 fn autogenerate_dwarf_wasm_path(di: &DebugInfoData) -> PathBuf {
109     static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
110     let module_name = di
111         .name_section
112         .module_name
113         .and_then(check_invalid_chars_in_name)
114         .map(|s| s.to_string())
115         .unwrap_or_else(|| format!("<gen-{}>.wasm", NEXT_ID.fetch_add(1, SeqCst)));
116     let path = format!("/<wasm-module>/{module_name}");
117     PathBuf::from(path)
118 }
119 
120 struct WasmTypesDieRefs {
121     i32: write::UnitEntryId,
122     i64: write::UnitEntryId,
123     f32: write::UnitEntryId,
124     f64: write::UnitEntryId,
125 }
126 
127 fn add_wasm_types(
128     unit: &mut write::Unit,
129     root_id: write::UnitEntryId,
130     out_strings: &mut write::StringTable,
131 ) -> WasmTypesDieRefs {
132     macro_rules! def_type {
133         ($id:literal, $size:literal, $enc:path) => {{
134             let die_id = unit.add(root_id, gimli::DW_TAG_base_type);
135             let die = unit.get_mut(die_id);
136             die.set(
137                 gimli::DW_AT_name,
138                 write::AttributeValue::StringRef(out_strings.add($id)),
139             );
140             die.set(gimli::DW_AT_byte_size, write::AttributeValue::Data1($size));
141             die.set(gimli::DW_AT_encoding, write::AttributeValue::Encoding($enc));
142             die_id
143         }};
144     }
145 
146     let i32_die_id = def_type!("i32", 4, gimli::DW_ATE_signed);
147     let i64_die_id = def_type!("i64", 8, gimli::DW_ATE_signed);
148     let f32_die_id = def_type!("f32", 4, gimli::DW_ATE_float);
149     let f64_die_id = def_type!("f64", 8, gimli::DW_ATE_float);
150 
151     WasmTypesDieRefs {
152         i32: i32_die_id,
153         i64: i64_die_id,
154         f32: f32_die_id,
155         f64: f64_die_id,
156     }
157 }
158 
159 fn resolve_var_type(
160     index: usize,
161     wasm_types: &WasmTypesDieRefs,
162     func_meta: &FunctionMetadata,
163 ) -> Option<(write::UnitEntryId, bool)> {
164     let (ty, is_param) = if index < func_meta.params.len() {
165         (func_meta.params[index], true)
166     } else {
167         let mut i = (index - func_meta.params.len()) as u32;
168         let mut j = 0;
169         while j < func_meta.locals.len() && i >= func_meta.locals[j].0 {
170             i -= func_meta.locals[j].0;
171             j += 1;
172         }
173         if j >= func_meta.locals.len() {
174             // Ignore the var index out of bound.
175             return None;
176         }
177         (func_meta.locals[j].1, false)
178     };
179     let type_die_id = match ty {
180         WasmValType::I32 => wasm_types.i32,
181         WasmValType::I64 => wasm_types.i64,
182         WasmValType::F32 => wasm_types.f32,
183         WasmValType::F64 => wasm_types.f64,
184         _ => {
185             // Ignore unsupported types.
186             return None;
187         }
188     };
189     Some((type_die_id, is_param))
190 }
191 
192 fn generate_vars(
193     unit: &mut write::Unit,
194     die_id: write::UnitEntryId,
195     addr_tr: &AddressTransform,
196     frame_info: &FunctionFrameInfo,
197     scope_ranges: &[(u64, u64)],
198     vmctx_ptr_die_ref: write::Reference,
199     wasm_types: &WasmTypesDieRefs,
200     func_meta: &FunctionMetadata,
201     locals_names: Option<&HashMap<u32, &str>>,
202     out_strings: &mut write::StringTable,
203     isa: &dyn TargetIsa,
204 ) -> Result<(), Error> {
205     let vmctx_label = get_vmctx_value_label();
206 
207     // Normalize order of ValueLabelsRanges keys to have reproducible results.
208     let mut vars = frame_info.value_ranges.keys().collect::<Vec<_>>();
209     vars.sort_by(|a, b| a.index().cmp(&b.index()));
210 
211     for label in vars {
212         if label.index() == vmctx_label.index() {
213             append_vmctx_info(
214                 unit,
215                 die_id,
216                 vmctx_ptr_die_ref,
217                 addr_tr,
218                 Some(frame_info),
219                 scope_ranges,
220                 out_strings,
221                 isa,
222             )?;
223         } else {
224             let var_index = label.index();
225             let (type_die_id, is_param) =
226                 if let Some(result) = resolve_var_type(var_index, wasm_types, func_meta) {
227                     result
228                 } else {
229                     // Skipping if type of local cannot be detected.
230                     continue;
231                 };
232 
233             let loc_list_id = {
234                 let locs = CompiledExpression::from_label(*label)
235                     .build_with_locals(scope_ranges, addr_tr, Some(frame_info), isa)
236                     .expressions
237                     .map(|i| {
238                         i.map(|(begin, length, data)| write::Location::StartLength {
239                             begin,
240                             length,
241                             data,
242                         })
243                     })
244                     .collect::<Result<Vec<_>, _>>()?;
245                 unit.locations.add(write::LocationList(locs))
246             };
247 
248             let var_id = unit.add(
249                 die_id,
250                 if is_param {
251                     gimli::DW_TAG_formal_parameter
252                 } else {
253                     gimli::DW_TAG_variable
254                 },
255             );
256             let var = unit.get_mut(var_id);
257 
258             let name_id = match locals_names
259                 .and_then(|m| m.get(&(var_index as u32)))
260                 .and_then(|s| check_invalid_chars_in_name(s))
261             {
262                 Some(n) => out_strings.add(assert_dwarf_str!(n)),
263                 None => out_strings.add(format!("var{var_index}")),
264             };
265 
266             var.set(gimli::DW_AT_name, write::AttributeValue::StringRef(name_id));
267             var.set(
268                 gimli::DW_AT_type,
269                 write::AttributeValue::UnitRef(type_die_id),
270             );
271             var.set(
272                 gimli::DW_AT_location,
273                 write::AttributeValue::LocationListRef(loc_list_id),
274             );
275         }
276     }
277     Ok(())
278 }
279 
280 fn check_invalid_chars_in_path(path: PathBuf) -> Option<PathBuf> {
281     path.clone()
282         .to_str()
283         .and_then(move |s| if s.contains('\x00') { None } else { Some(path) })
284 }
285 
286 /// Generate "simulated" native DWARF for functions lacking WASM-level DWARF.
287 pub fn generate_simulated_dwarf(
288     compilation: &mut Compilation<'_>,
289     addr_tr: &PrimaryMap<StaticModuleIndex, AddressTransform>,
290     translated: &HashSet<usize>,
291     out_encoding: gimli::Encoding,
292     vmctx_ptr_die_refs: &PrimaryMap<StaticModuleIndex, write::Reference>,
293     out_units: &mut write::UnitTable,
294     out_strings: &mut write::StringTable,
295     isa: &dyn TargetIsa,
296 ) -> Result<(), Error> {
297     let (wasm_file, path) = {
298         let di = &compilation.translations.iter().next().unwrap().1.debuginfo;
299         let path = di
300             .wasm_file
301             .path
302             .to_owned()
303             .and_then(check_invalid_chars_in_path)
304             .unwrap_or_else(|| autogenerate_dwarf_wasm_path(di));
305         (&di.wasm_file, path)
306     };
307 
308     let (unit, root_id, file_id) = {
309         let comp_dir_id = out_strings.add(assert_dwarf_str!(
310             path.parent()
311                 .context("path dir")?
312                 .to_str()
313                 .context("path dir encoding")?
314         ));
315         let name = path
316             .file_name()
317             .context("path name")?
318             .to_str()
319             .context("path name encoding")?;
320         let name_id = out_strings.add(assert_dwarf_str!(name));
321 
322         let (out_program, file_id) = generate_line_info(
323             addr_tr,
324             translated,
325             out_encoding,
326             wasm_file,
327             comp_dir_id,
328             name_id,
329             name,
330         )?;
331 
332         let unit_id = out_units.add(write::Unit::new(out_encoding, out_program));
333         let unit = out_units.get_mut(unit_id);
334 
335         let root_id = unit.root();
336         let root = unit.get_mut(root_id);
337 
338         let id = out_strings.add(PRODUCER_NAME);
339         root.set(gimli::DW_AT_producer, write::AttributeValue::StringRef(id));
340         root.set(gimli::DW_AT_name, write::AttributeValue::StringRef(name_id));
341         root.set(
342             gimli::DW_AT_stmt_list,
343             write::AttributeValue::LineProgramRef,
344         );
345         root.set(
346             gimli::DW_AT_comp_dir,
347             write::AttributeValue::StringRef(comp_dir_id),
348         );
349         (unit, root_id, file_id)
350     };
351 
352     let wasm_types = add_wasm_types(unit, root_id, out_strings);
353     let mut unit_ranges = vec![];
354     for (module, index) in compilation.indexes().collect::<Vec<_>>() {
355         let (symbol, _) = compilation.function(module, index);
356         if translated.contains(&symbol) {
357             continue;
358         }
359 
360         let addr_tr = &addr_tr[module];
361         let map = &addr_tr.map()[index];
362         let die_id = unit.add(root_id, gimli::DW_TAG_subprogram);
363         let die = unit.get_mut(die_id);
364         let low_pc = write::Address::Symbol {
365             symbol,
366             addend: map.offset as i64,
367         };
368         let code_length = map.len as u64;
369         die.set(gimli::DW_AT_low_pc, write::AttributeValue::Address(low_pc));
370         die.set(
371             gimli::DW_AT_high_pc,
372             write::AttributeValue::Udata(code_length),
373         );
374         unit_ranges.push(write::Range::StartLength {
375             begin: low_pc,
376             length: code_length,
377         });
378 
379         let translation = &compilation.translations[module];
380         let func_index = translation.module.func_index(index);
381         let di = &translation.debuginfo;
382         let id = match di
383             .name_section
384             .func_names
385             .get(&func_index)
386             .and_then(|s| check_invalid_chars_in_name(s))
387         {
388             Some(n) => out_strings.add(assert_dwarf_str!(n)),
389             None => out_strings.add(format!("wasm-function[{}]", func_index.as_u32())),
390         };
391 
392         die.set(gimli::DW_AT_name, write::AttributeValue::StringRef(id));
393 
394         die.set(
395             gimli::DW_AT_decl_file,
396             write::AttributeValue::FileIndex(Some(file_id)),
397         );
398 
399         let f_start = map.addresses[0].wasm;
400         let wasm_offset = di.wasm_file.code_section_offset + f_start;
401         die.set(
402             gimli::DW_AT_decl_line,
403             write::AttributeValue::Udata(wasm_offset),
404         );
405 
406         let frame_info = compilation.function_frame_info(module, index);
407         let source_range = addr_tr.func_source_range(index);
408         generate_vars(
409             unit,
410             die_id,
411             addr_tr,
412             &frame_info,
413             &[(source_range.0, source_range.1)],
414             vmctx_ptr_die_refs[module],
415             &wasm_types,
416             &di.wasm_file.funcs[index.as_u32() as usize],
417             di.name_section.locals_names.get(&func_index),
418             out_strings,
419             isa,
420         )?;
421     }
422     let unit_ranges_id = unit.ranges.add(write::RangeList(unit_ranges));
423     unit.get_mut(root_id).set(
424         gimli::DW_AT_ranges,
425         write::AttributeValue::RangeListRef(unit_ranges_id),
426     );
427 
428     Ok(())
429 }
430