wasmtime_cranelift/debug/transform/
synthetic.rs

1use gimli::write::{
2    AttributeValue, LineProgram, Reference, StringTable, Unit, UnitEntryId, UnitId, UnitTable,
3};
4use wasmtime_environ::StaticModuleIndex;
5use wasmtime_versioned_export_macros::versioned_stringify_ident;
6
7use crate::debug::{Compilation, ModuleMemoryOffset};
8
9/// Internal Wasm utility types DIEs such as WebAssemblyPtr and WasmtimeVMContext.
10///
11/// For unwrapping Wasm pointer, the WasmtimeVMContext has the `set()` method
12/// that allows to control current Wasm memory to inspect.
13/// Notice that "wasmtime_set_vmctx_memory" is an external/builtin subprogram
14/// that is not part of Wasm code.
15///
16/// This CU is currently per-module since VMContext memory structure is per-module;
17/// some of the contained types could be made global (per-Compilation).
18pub struct ModuleSyntheticUnit {
19    unit_id: UnitId,
20    vmctx_ptr_die_id: UnitEntryId,
21    wasm_ptr_die_id: UnitEntryId,
22}
23
24macro_rules! add_tag {
25    ($unit:ident, $parent_id:ident, $tag:expr => $die:ident as $die_id:ident { $($a:path = $v:expr),* }) => {
26        let $die_id = $unit.add($parent_id, $tag);
27        let $die = $unit.get_mut($die_id);
28        $( $die.set($a, $v); )*
29    };
30}
31
32impl ModuleSyntheticUnit {
33    pub fn new(
34        module: StaticModuleIndex,
35        compilation: &Compilation<'_>,
36        encoding: gimli::Encoding,
37        out_units: &mut UnitTable,
38        out_strings: &mut StringTable,
39    ) -> Self {
40        let unit_id = Self::create_unit(encoding, out_units, out_strings);
41        let unit = out_units.get_mut(unit_id);
42        let vmctx_ptr_die_id = Self::create_vmctx_ptr_die(module, compilation, unit, out_strings);
43        let wasm_ptr_die_id = Self::create_wasm_ptr_die(unit, out_strings);
44
45        Self {
46            unit_id,
47            vmctx_ptr_die_id,
48            wasm_ptr_die_id,
49        }
50    }
51
52    pub fn vmctx_ptr_die_ref(&self) -> Reference {
53        Reference::Entry(self.unit_id, self.vmctx_ptr_die_id)
54    }
55
56    pub fn wasm_ptr_die_ref(&self) -> Reference {
57        Reference::Entry(self.unit_id, self.wasm_ptr_die_id)
58    }
59
60    fn create_unit(
61        encoding: gimli::Encoding,
62        out_units: &mut UnitTable,
63        out_strings: &mut StringTable,
64    ) -> UnitId {
65        let unit_id = out_units.add(Unit::new(encoding, LineProgram::none()));
66        let unit = out_units.get_mut(unit_id);
67        let unit_die = unit.get_mut(unit.root());
68        unit_die.set(
69            gimli::DW_AT_name,
70            AttributeValue::StringRef(out_strings.add("WasmtimeModuleSyntheticUnit")),
71        );
72        unit_id
73    }
74
75    fn create_vmctx_ptr_die(
76        module: StaticModuleIndex,
77        compilation: &Compilation<'_>,
78        unit: &mut Unit,
79        out_strings: &mut StringTable,
80    ) -> UnitEntryId {
81        // Build DW_TAG_base_type for Wasm byte:
82        //  .. DW_AT_name = u8
83        //  .. DW_AT_encoding = DW_ATE_unsigned
84        //  .. DW_AT_byte_size = 1
85        let root_id = unit.root();
86        add_tag!(unit, root_id, gimli::DW_TAG_base_type => memory_byte_die as memory_byte_die_id {
87            gimli::DW_AT_name = AttributeValue::StringRef(out_strings.add("u8")),
88            gimli::DW_AT_encoding = AttributeValue::Encoding(gimli::DW_ATE_unsigned),
89            gimli::DW_AT_byte_size = AttributeValue::Data1(1)
90        });
91
92        // Build DW_TAG_pointer_type that references Wasm bytes:
93        //  .. DW_AT_name = "u8*"
94        //  .. DW_AT_type = <memory_byte_die>
95        add_tag!(unit, root_id, gimli::DW_TAG_pointer_type => memory_bytes_die as memory_bytes_die_id {
96            gimli::DW_AT_name = AttributeValue::StringRef(out_strings.add("u8*")),
97            gimli::DW_AT_type = AttributeValue::UnitRef(memory_byte_die_id)
98        });
99
100        // Create artificial VMContext type and its reference for convenience viewing
101        // its fields (such as memory ref) in a debugger. Build DW_TAG_structure_type:
102        //   .. DW_AT_name = "WasmtimeVMContext"
103        let vmctx_die_id = unit.add(root_id, gimli::DW_TAG_structure_type);
104        let vmctx_die = unit.get_mut(vmctx_die_id);
105        vmctx_die.set(
106            gimli::DW_AT_name,
107            AttributeValue::StringRef(out_strings.add("WasmtimeVMContext")),
108        );
109
110        // TODO multiple memories
111        match compilation.module_memory_offsets[module] {
112            ModuleMemoryOffset::Defined(memory_offset) => {
113                // The context has defined memory: extend the WasmtimeVMContext size
114                // past the "memory" field.
115                const MEMORY_FIELD_SIZE_PLUS_PADDING: u32 = 8;
116                vmctx_die.set(
117                    gimli::DW_AT_byte_size,
118                    AttributeValue::Data4(memory_offset + MEMORY_FIELD_SIZE_PLUS_PADDING),
119                );
120
121                // Define the "memory" field which is a direct pointer to allocated Wasm memory.
122                // Build DW_TAG_member:
123                //  .. DW_AT_name = "memory"
124                //  .. DW_AT_type = <memory_bytes_die>
125                //  .. DW_AT_data_member_location = `memory_offset`
126                add_tag!(unit, vmctx_die_id, gimli::DW_TAG_member => m_die as m_die_id {
127                    gimli::DW_AT_name = AttributeValue::StringRef(out_strings.add("memory")),
128                    gimli::DW_AT_type = AttributeValue::UnitRef(memory_bytes_die_id),
129                    gimli::DW_AT_data_member_location = AttributeValue::Udata(memory_offset as u64)
130                });
131            }
132            ModuleMemoryOffset::Imported { .. } => {
133                // TODO implement convenience pointer to and additional types for VMMemoryImport.
134            }
135            ModuleMemoryOffset::None => (),
136        }
137
138        // Build DW_TAG_pointer_type for `WasmtimeVMContext*`:
139        //  .. DW_AT_name = "WasmtimeVMContext*"
140        //  .. DW_AT_type = <vmctx_die>
141        add_tag!(unit, root_id, gimli::DW_TAG_pointer_type => vmctx_ptr_die as vmctx_ptr_die_id {
142            gimli::DW_AT_name = AttributeValue::StringRef(out_strings.add("WasmtimeVMContext*")),
143            gimli::DW_AT_type = AttributeValue::UnitRef(vmctx_die_id)
144        });
145
146        // Build vmctx_die's DW_TAG_subprogram for `set` method:
147        //  .. DW_AT_linkage_name = "wasmtime_set_vmctx_memory"
148        //  .. DW_AT_name = "set"
149        //  .. DW_TAG_formal_parameter
150        //  ..  .. DW_AT_type = <vmctx_ptr_die>
151        //  ..  .. DW_AT_artificial = 1
152        add_tag!(unit, vmctx_die_id, gimli::DW_TAG_subprogram => vmctx_set as vmctx_set_id {
153            gimli::DW_AT_linkage_name = AttributeValue::StringRef(out_strings.add(versioned_stringify_ident!(wasmtime_set_vmctx_memory))),
154            gimli::DW_AT_name = AttributeValue::StringRef(out_strings.add("set"))
155        });
156        add_tag!(unit, vmctx_set_id, gimli::DW_TAG_formal_parameter => vmctx_set_this_param as vmctx_set_this_param_id {
157            gimli::DW_AT_type = AttributeValue::UnitRef(vmctx_ptr_die_id),
158            gimli::DW_AT_artificial = AttributeValue::Flag(true)
159        });
160
161        vmctx_ptr_die_id
162    }
163
164    fn create_wasm_ptr_die(unit: &mut Unit, out_strings: &mut StringTable) -> UnitEntryId {
165        // Build DW_TAG_base_type for generic `WebAssemblyPtr`.
166        //  .. DW_AT_name = "WebAssemblyPtr"
167        //  .. DW_AT_byte_size = 4
168        //  .. DW_AT_encoding = DW_ATE_unsigned
169        const WASM_PTR_LEN: u8 = 4;
170        let root_id = unit.root();
171        add_tag!(unit, root_id, gimli::DW_TAG_base_type => wp_die as wp_die_id {
172            gimli::DW_AT_name = AttributeValue::StringRef(out_strings.add("WebAssemblyPtr")),
173            gimli::DW_AT_byte_size = AttributeValue::Data1(WASM_PTR_LEN),
174            gimli::DW_AT_encoding = AttributeValue::Encoding(gimli::DW_ATE_unsigned)
175        });
176
177        wp_die_id
178    }
179}