MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / new

Method new

crates/wasmtime/src/runtime/code_memory.rs:125–282  ·  view source on GitHub ↗

Creates a new `CodeMemory` by taking ownership of the provided `MmapVec`. The returned `CodeMemory` manages the internal `MmapVec` and the `publish` method is used to actually make the memory executable.

(engine: &Engine, mmap: MmapVec)

Source from the content-addressed store, hash-verified

123 /// The returned `CodeMemory` manages the internal `MmapVec` and the
124 /// `publish` method is used to actually make the memory executable.
125 pub fn new(engine: &Engine, mmap: MmapVec) -> Result<Self> {
126 let mmap_data = &*mmap;
127 let header = FileHeader64::<Endianness>::parse(mmap_data)
128 .map_err(obj::ObjectCrateErrorWrapper)
129 .context("failed to parse precompiled artifact as an ELF")?;
130 let endian = header
131 .endian()
132 .context("failed to parse header endianness")?;
133
134 let section_headers = header
135 .section_headers(endian, mmap_data)
136 .context("failed to parse section headers")?;
137 let strings = header
138 .section_strings(endian, mmap_data, section_headers)
139 .context("failed to parse strings table")?;
140 let sections = header
141 .sections(endian, mmap_data)
142 .context("failed to parse sections table")?;
143
144 let mut text = 0..0;
145 let mut unwind = 0..0;
146 let mut enable_branch_protection = None;
147 let mut needs_executable = true;
148 #[cfg(feature = "debug-builtins")]
149 let mut has_native_debug_info = false;
150 let mut trap_data = 0..0;
151 let mut exception_data = 0..0;
152 let mut frame_tables_data = 0..0;
153 let mut wasm_data = 0..0;
154 let mut address_map_data = 0..0;
155 let mut stack_map_data = 0..0;
156 let mut func_name_data = 0..0;
157 let mut info_data = 0..0;
158 let mut wasm_dwarf = 0..0;
159 let mut wasm_bytecode = 0..0;
160 let mut wasm_bytecode_ends = 0..0;
161 for section_header in sections.iter() {
162 let data = section_header
163 .data(endian, mmap_data)
164 .map_err(obj::ObjectCrateErrorWrapper)?;
165 let name = section_name(endian, strings, section_header)?;
166 let range = subslice_range(data, &mmap);
167
168 // Double-check that sections are all aligned properly.
169 let section_align = usize::try_from(section_header.sh_addralign(endian))?;
170 if section_align != 0 && data.len() != 0 {
171 let section_offset = data.as_ptr().addr() - mmap.as_ptr().addr();
172 ensure!(
173 section_offset % section_align == 0,
174 "section {name:?} isn't aligned to {section_align:#x}",
175 );
176 }
177
178 // Check that we don't have any relocations, which would make
179 // loading precompiled Wasm modules slower and also force them to
180 // get paged into memory from disk.
181 //
182 // We avoid using things like Cranelift's `floor`, `ceil`,

Callers

nothing calls this directly

Calls 14

section_nameFunction · 0.85
subslice_rangeFunction · 0.85
reloc_section_targetFunction · 0.85
OkFunction · 0.85
custom_code_memoryMethod · 0.80
parseFunction · 0.50
contextMethod · 0.45
endianMethod · 0.45
iterMethod · 0.45
dataMethod · 0.45
lenMethod · 0.45
addrMethod · 0.45

Tested by

no test coverage detected