Clear WASM linear memory on other OSes besides Linux too (#10291)

This commit is contained in:
Koute
2021-11-18 20:16:38 +09:00
committed by GitHub
parent 6e424d74ff
commit 15a0bfb0f6
3 changed files with 91 additions and 11 deletions
@@ -403,10 +403,10 @@ impl InstanceWrapper {
self.memory.data_ptr(ctx)
}
/// Removes physical backing from the allocated linear memory. This leads to returning the
/// memory back to the system. While the memory is zeroed this is considered as a side-effect
/// and is not relied upon. Thus this function acts as a hint.
pub fn decommit(&self, ctx: impl AsContext) {
/// If possible removes physical backing from the allocated linear memory which
/// leads to returning the memory back to the system; this also zeroes the memory
/// as a side-effect.
pub fn decommit(&self, mut ctx: impl AsContextMut) {
if self.memory.data_size(&ctx) == 0 {
return
}
@@ -417,7 +417,7 @@ impl InstanceWrapper {
unsafe {
let ptr = self.memory.data_ptr(&ctx);
let len = self.memory.data_size(ctx);
let len = self.memory.data_size(&ctx);
// Linux handles MADV_DONTNEED reliably. The result is that the given area
// is unmapped and will be zeroed on the next pagefault.
@@ -429,9 +429,15 @@ impl InstanceWrapper {
std::io::Error::last_os_error(),
);
});
} else {
return;
}
}
}
}
// If we're on an unsupported OS or the memory couldn't have been
// decommited for some reason then just manually zero it out.
self.memory.data_mut(ctx.as_context_mut()).fill(0);
}
}
@@ -237,7 +237,7 @@ impl WasmInstance for WasmtimeInstance {
// Signal to the OS that we are done with the linear memory and that it can be
// reclaimed.
instance_wrapper.decommit(&store);
instance_wrapper.decommit(store);
result
},
@@ -415,11 +415,7 @@ pub struct Semantics {
///
/// Primarily this is achieved by not recreating the instance for each call and performing a
/// bare minimum clean up: reapplying the data segments and restoring the values for global
/// variables. The vast majority of the linear memory is not restored, meaning that effects
/// of previous executions on the same [`WasmInstance`] can be observed there.
///
/// This is not a problem for a standard substrate runtime execution because it's up to the
/// runtime itself to make sure that it doesn't involve any non-determinism.
/// variables.
///
/// Since this feature depends on instrumentation, it can be set only if runtime is
/// instantiated using the runtime blob, e.g. using [`create_runtime`].