fix(runtime-core) Improve error message when globals are corrupted.

Before this patch:

```
$ cargo run -- run panic_index_oob_all_backends.wasm
Error: ExportNotFound { name: "main" }
```

With this patch:

```sh
$ cargo run -- run panic_index_oob_all_backends.wasm
Error: Can't instantiate module: LinkError([Generic { message: "Trying to read the `0` global that isn\'t properly initialized." }])
```
This commit is contained in:
Ivan Enderlin 2019-11-21 10:57:52 +01:00
parent a9e446b5cd
commit b1f58bded0

View File

@ -77,7 +77,7 @@ impl LocalBacking {
} }
}; };
let mut tables = Self::generate_tables(module); let mut tables = Self::generate_tables(module);
let mut globals = Self::generate_globals(module, imports); let mut globals = Self::generate_globals(module, imports)?;
// Ensure all initializers are valid before running finalizers // Ensure all initializers are valid before running finalizers
Self::validate_memories(module, imports)?; Self::validate_memories(module, imports)?;
@ -443,16 +443,20 @@ impl LocalBacking {
fn generate_globals( fn generate_globals(
module: &ModuleInner, module: &ModuleInner,
imports: &ImportBacking, imports: &ImportBacking,
) -> BoxedMap<LocalGlobalIndex, Global> { ) -> LinkResult<BoxedMap<LocalGlobalIndex, Global>> {
let mut globals = Map::with_capacity(module.info.globals.len()); let mut globals = Map::with_capacity(module.info.globals.len());
for (_, global_init) in module.info.globals.iter() { for (_, global_init) in module.info.globals.iter() {
let value = match &global_init.init { let value = match &global_init.init {
Initializer::Const(value) => value.clone(), Initializer::Const(value) => value.clone(),
Initializer::GetGlobal(import_global_index) => { Initializer::GetGlobal(import_global_index) => {
// Skip if the global hasn't been initialized properly.
if imports.globals.len() <= import_global_index.index() { if imports.globals.len() <= import_global_index.index() {
continue; return Err(vec![LinkError::Generic {
message: format!(
"Trying to read the `{:?}` global that is not properly initialized.",
import_global_index.index()
),
}]);
} }
imports.globals[*import_global_index].get() imports.globals[*import_global_index].get()
@ -468,7 +472,7 @@ impl LocalBacking {
globals.push(global); globals.push(global);
} }
globals.into_boxed_map() Ok(globals.into_boxed_map())
} }
fn finalize_globals( fn finalize_globals(