WASM runtime switch to import memory (#4737)

* WASM runtime switch to import memory

Up to now runtimes have exported their memory. To unify it with
sandboxing, this pr switches runtimes to import memory as well.

From a functional perspective, exporting/importing memory makes no
difference to the runtime.

To provide backwards compatibility, WASM exported memory is still supported.

* Revert debug stuff

* Revert some stuff
This commit is contained in:
Bastian Köcher
2020-01-28 09:36:57 +01:00
committed by GitHub
parent 5c8743510e
commit 793a1eb053
15 changed files with 603 additions and 428 deletions
@@ -14,17 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use wasm_builder_runner::{build_current_project_with_rustflags, WasmBuilderSource};
use wasm_builder_runner::WasmBuilder;
fn main() {
build_current_project_with_rustflags(
"wasm_binary.rs",
WasmBuilderSource::CratesOrPath {
path: "../../../utils/wasm-builder",
version: "1.0.9",
},
// This instructs LLD to export __heap_base as a global variable, which is used by the
// external memory allocator.
"-Clink-arg=--export=__heap_base",
);
WasmBuilder::new()
.with_current_project()
.with_wasm_builder_from_crates_or_path("1.0.9", "../../../utils/wasm-builder")
.export_heap_base()
.import_memory()
.build()
}
@@ -26,6 +26,11 @@ extern "C" {
fn yet_another_missing_external();
}
#[cfg(not(feature = "std"))]
/// Mutable static variables should be always observed to have
/// the initialized value at the start of a runtime call.
static mut MUTABLE_STATIC: u64 = 32;
sp_core::wasm_export_functions! {
fn test_calling_missing_external() {
unsafe { missing_external() }
@@ -217,6 +222,41 @@ sp_core::wasm_export_functions! {
fn test_sp_allocator_compiles() {
sp_allocator::FreeingBumpHeapAllocator::new(0);
}
fn returns_mutable_static() -> u64 {
unsafe {
MUTABLE_STATIC += 1;
MUTABLE_STATIC
}
}
fn allocates_huge_stack_array(trap: bool) -> Vec<u8> {
// Allocate a stack frame that is approx. 75% of the stack (assuming it is 1MB).
// This will just decrease (stacks in wasm32-u-u grow downwards) the stack
// pointer. This won't trap on the current compilers.
let mut data = [0u8; 1024 * 768];
// Then make sure we actually write something to it.
//
// If:
// 1. the stack area is placed at the beginning of the linear memory space, and
// 2. the stack pointer points to out-of-bounds area, and
// 3. a write is performed around the current stack pointer.
//
// then a trap should happen.
//
for (i, v) in data.iter_mut().enumerate() {
*v = i as u8; // deliberate truncation
}
if trap {
// There is a small chance of this to be pulled up in theory. In practice
// the probability of that is rather low.
panic!()
}
data.to_vec()
}
}
#[cfg(not(feature = "std"))]