Example #1
0
def instantiate_spectest_module(store: Store) -> ModuleInstance:
    logger = logging.getLogger("wasm.tools.fixtures.modules.spectest")

    def spectest__print_i32(store, arg):
        logger.debug('print_i32: %s', arg)
        return store, []

    def spectest__print_i64(store, arg):
        logger.debug('print_i64: %s', arg)
        return store, []

    def spectest__print_f32(store, arg):
        logger.debug('print_f32: %s', arg)
        return store, []

    def spectest__print_f64(store, arg):
        logger.debug('print_f64: %s', arg)
        return store, []

    def spectest__print_i32_f32(store, arg):
        logger.debug('print_i32_f32: %s', arg)
        return store, []

    def spectest__print_f64_f64(store, arg):
        logger.debug('print_f64_f64: %s', arg)
        return store, []

    def spectest__print(store, arg):
        logger.debug('print: %s', arg)
        return store, []

    store.allocate_host_function(FunctionType((ValType.i32, ), ()),
                                 spectest__print_i32)
    store.allocate_host_function(FunctionType((ValType.i64, ), ()),
                                 spectest__print_i64)
    store.allocate_host_function(FunctionType((ValType.f32, ), ()),
                                 spectest__print_f32)
    store.allocate_host_function(FunctionType((ValType.f64, ), ()),
                                 spectest__print_f64)
    store.allocate_host_function(FunctionType((ValType.i32, ValType.f32), ()),
                                 spectest__print_i32_f32)
    store.allocate_host_function(FunctionType((ValType.f64, ValType.f64), ()),
                                 spectest__print_f64_f64)
    store.allocate_host_function(FunctionType((), ()), spectest__print)

    # min:1,max:2 required by import.wast:
    store.allocate_memory(MemoryType(UInt32(1), UInt32(2)))

    # 666 required by import.wast
    store.allocate_global(GlobalType(Mutability.const, ValType.i32),
                          UInt32(666))

    store.allocate_global(GlobalType(Mutability.const, ValType.f32),
                          Float32(0.0))
    store.allocate_global(GlobalType(Mutability.const, ValType.f64),
                          Float64(0.0))
    store.allocate_table(
        TableType(
            Limits(UInt32(10), UInt32(20)),
            FunctionAddress))  # max was 30, changed to 20 for import.wast
    moduleinst = ModuleInstance(
        types=(
            FunctionType((ValType.i32, ), ()),
            FunctionType((ValType.i64, ), ()),
            FunctionType((ValType.f32, ), ()),
            FunctionType((ValType.f64, ), ()),
            FunctionType((ValType.i32, ValType.f32), ()),
            FunctionType((ValType.f64, ValType.f64), ()),
            FunctionType((), ()),
        ),
        func_addrs=tuple(FunctionAddress(idx) for idx in range(7)),
        table_addrs=(TableAddress(0), ),
        memory_addrs=(MemoryAddress(0), ),
        global_addrs=(GlobalAddress(0), GlobalAddress(1)),
        exports=(
            ExportInstance("print_i32", FunctionAddress(0)),
            ExportInstance("print_i64", FunctionAddress(1)),
            ExportInstance("print_f32", FunctionAddress(2)),
            ExportInstance("print_f64", FunctionAddress(3)),
            ExportInstance("print_i32_f32", FunctionAddress(4)),
            ExportInstance("print_f64_f64", FunctionAddress(5)),
            ExportInstance("print", FunctionAddress(6)),
            ExportInstance("memory", MemoryAddress(0)),
            ExportInstance("global_i32", GlobalAddress(0)),
            ExportInstance("global_f32", GlobalAddress(1)),
            ExportInstance("global_f64", GlobalAddress(2)),
            ExportInstance("table", TableAddress(0)),
        ),
    )
    return moduleinst
Example #2
0
    def instantiate_module(
        self,
        module: Module,
    ) -> Tuple[ModuleInstance, Optional[Tuple[TValue, ...]]]:
        """
        Instantiate a Web Assembly module into this runtime environment.
        """
        # Ensure the module is valid
        try:
            module_import_types, module_export_types = validate_module(module)
        except ValidationError as err:
            raise InvalidModule from err

        # Gather all of the addresses for the external types for this module.
        all_import_addresses = self._get_import_addresses(module.imports)

        # Validate all of the addresses are known to the store.
        for address in all_import_addresses:
            try:
                self.store.validate_address(address)
            except ValidationError as err:
                raise InvalidModule from err

        # Gather the types for each of the values referenced by the addresses
        # of the module imports.
        store_import_types = tuple(
            self.store.get_type_for_address(address)
            for address in all_import_addresses)
        if len(module_import_types) != len(store_import_types):
            # TODO: This validation may be superfluous as both of these values
            # are generated from `module.imports` and the generation process
            # **should** give strong guarantees that the resulting list is the
            # same length as `module.imports`.
            raise Unlinkable(
                f"Mismatched number of import types: {len(module_import_types)} != "
                f"{len(store_import_types)}")

        # Ensure that the module's internal types for it's imports match the
        # types found in the store.
        for module_type, store_type in zip(module_import_types,
                                           store_import_types):
            try:
                validate_external_type_match(store_type, module_type)
            except ValidationError as err:
                raise Unlinkable from err

        global_addresses = GlobalAddress.filter(all_import_addresses)
        global_values = _initialize_globals(self.store, module,
                                            global_addresses)

        module_instance = self.store.allocate_module(module,
                                                     all_import_addresses,
                                                     global_values)

        element_segment_offsets = _compute_table_offsets(
            self.store,
            module.elem,
            module_instance,
        )
        data_segment_offsets = _compute_data_offsets(
            self.store,
            module.data,
            module_instance,
        )

        for offset, element_segment in zip(element_segment_offsets,
                                           module.elem):
            for idx, function_idx in enumerate(element_segment.init):
                function_address = module_instance.func_addrs[function_idx]
                table_address = module_instance.table_addrs[
                    element_segment.table_idx]
                table_instance = self.store.tables[table_address]
                table_instance.elem[offset + idx] = function_address

        for offset, data_segment in zip(data_segment_offsets, module.data):
            memory_address = module_instance.memory_addrs[
                data_segment.memory_idx]
            memory_instance = self.store.mems[memory_address]
            data_length = len(data_segment.init)
            memory_instance.data[offset:offset +
                                 data_length] = data_segment.init

        result: Optional[Tuple[TValue, ...]]

        if module.start is not None:
            function_address = module_instance.func_addrs[
                module.start.function_idx]
            result = self.invoke_function(function_address)
        else:
            result = None

        return module_instance, result
Example #3
0
def instantiate_test_module(store):
    def test__func(store, arg):
        pass

    def test__func_i32(store, arg):
        pass

    def test__func_f32(store, arg):
        pass

    def test__func__i32(store, arg):
        pass

    def test__func__f32(store, arg):
        pass

    def test__func_i32_i32(store, arg):
        pass

    def test__func_i64_i64(store, arg):
        pass

    store.allocate_host_function(FunctionType((), ()), test__func)
    store.allocate_host_function(FunctionType((ValType.i32, ), ()),
                                 test__func_i32)
    store.allocate_host_function(FunctionType((ValType.f32, ), ()),
                                 test__func_f32)
    store.allocate_host_function(FunctionType((), (ValType.i32, )),
                                 test__func__i32)
    store.allocate_host_function(FunctionType((), (ValType.f32, )),
                                 test__func__f32)
    store.allocate_host_function(
        FunctionType((ValType.i32, ), (ValType.i32, )), test__func_i32_i32)
    store.allocate_host_function(
        FunctionType((ValType.i64, ), (ValType.i64, )), test__func_i64_i64)

    store.allocate_memory(MemoryType(1, None))
    store.allocate_global(GlobalType(Mutability.const, ValType.i32),
                          UInt32(666))
    store.allocate_global(GlobalType(Mutability.const, ValType.f32),
                          Float32(0.0))
    store.allocate_table(TableType(Limits(10, None), FunctionAddress))
    moduleinst = ModuleInstance(
        types=(
            FunctionType((), ()),
            FunctionType((ValType.i32, ), ()),
            FunctionType((ValType.f32, ), ()),
            FunctionType((), (ValType.i32, )),
            FunctionType((), (ValType.f32, )),
            FunctionType((ValType.i32, ), (ValType.i32, )),
            FunctionType((ValType.i64, ), (ValType.i64, )),
        ),
        func_addrs=tuple(FunctionAddress(idx) for idx in range(7)),
        table_addrs=(TableAddress(0), ),
        memory_addrs=(MemoryAddress(0), ),
        global_addrs=(GlobalAddress(0), GlobalAddress(1)),
        exports=(
            ExportInstance("func", FunctionAddress(0)),
            ExportInstance("func_i32", FunctionAddress(1)),
            ExportInstance("func_f32", FunctionAddress(2)),
            ExportInstance("func__i32", FunctionAddress(3)),
            ExportInstance("func__f32", FunctionAddress(4)),
            ExportInstance("func__i32_i32", FunctionAddress(5)),
            ExportInstance("func__i64_i64", FunctionAddress(6)),
            ExportInstance("memory-2-inf", MemoryAddress(0)),
            ExportInstance("global-i32", GlobalAddress(0)),
            ExportInstance("global-f32", GlobalAddress(1)),
            ExportInstance("table-10-inf", TableAddress(0)),
        ),
    )
    return moduleinst
Example #4
0
def instantiate_test_module(store: Store) -> ModuleInstance:
    def test__func(config: Configuration, args: Tuple[TValue, ...]) -> Tuple[TValue, ...]:
        return tuple()

    def test__func_i32(config: Configuration, args: Tuple[TValue, ...]) -> Tuple[TValue, ...]:
        return tuple()

    def test__func_f32(config: Configuration, args: Tuple[TValue, ...]) -> Tuple[TValue, ...]:
        return tuple()

    def test__func__i32(config: Configuration, args: Tuple[TValue, ...]) -> Tuple[TValue, ...]:
        return tuple()

    def test__func__f32(config: Configuration, args: Tuple[TValue, ...]) -> Tuple[TValue, ...]:
        return tuple()

    def test__func_i32_i32(config: Configuration, args: Tuple[TValue, ...]) -> Tuple[TValue, ...]:
        return tuple()

    def test__func_i64_i64(config: Configuration, args: Tuple[TValue, ...]) -> Tuple[TValue, ...]:
        return tuple()

    store.allocate_host_function(FunctionType((), ()), test__func)
    store.allocate_host_function(FunctionType((ValType.i32,), ()), test__func_i32)
    store.allocate_host_function(FunctionType((ValType.f32,), ()), test__func_f32)
    store.allocate_host_function(FunctionType((), (ValType.i32,)), test__func__i32)
    store.allocate_host_function(FunctionType((), (ValType.f32,)), test__func__f32)
    store.allocate_host_function(
        FunctionType((ValType.i32,), (ValType.i32,)),
        test__func_i32_i32,
    )
    store.allocate_host_function(
        FunctionType((ValType.i64,), (ValType.i64,)),
        test__func_i64_i64,
    )

    store.allocate_memory(MemoryType(numpy.uint32(1), None))
    store.allocate_global(GlobalType(Mutability.const, ValType.i32), numpy.uint32(666))
    store.allocate_global(GlobalType(Mutability.const, ValType.f32), numpy.float32(0.0))
    store.allocate_table(TableType(Limits(numpy.uint32(10), None), FunctionAddress))
    moduleinst = ModuleInstance(
        types=(
            FunctionType((), ()),
            FunctionType((ValType.i32,), ()),
            FunctionType((ValType.f32,), ()),
            FunctionType((), (ValType.i32,)),
            FunctionType((), (ValType.f32,)),
            FunctionType((ValType.i32,), (ValType.i32,)),
            FunctionType((ValType.i64,), (ValType.i64,)),
        ),
        func_addrs=tuple(FunctionAddress(idx) for idx in range(7)),
        table_addrs=(TableAddress(0),),
        memory_addrs=(MemoryAddress(0),),
        global_addrs=(GlobalAddress(0), GlobalAddress(1)),
        exports=(
            ExportInstance("func", FunctionAddress(0)),
            ExportInstance("func_i32", FunctionAddress(1)),
            ExportInstance("func_f32", FunctionAddress(2)),
            ExportInstance("func__i32", FunctionAddress(3)),
            ExportInstance("func__f32", FunctionAddress(4)),
            ExportInstance("func__i32_i32", FunctionAddress(5)),
            ExportInstance("func__i64_i64", FunctionAddress(6)),
            ExportInstance("memory-2-inf", MemoryAddress(0)),
            ExportInstance("global-i32", GlobalAddress(0)),
            ExportInstance("global-f32", GlobalAddress(1)),
            ExportInstance("table-10-inf", TableAddress(0)),
        ),
    )
    return moduleinst