Example #1
0
def parse_limits(stream: IO[bytes]) -> Limits:
    """
    Parser for the Limits type
    """
    flag = parse_single_byte(stream)

    if flag == 0x0:
        min_ = parse_u32(stream)
        return Limits(min_, None)
    elif flag == 0x01:
        min_ = parse_u32(stream)
        max_ = parse_u32(stream)
        return Limits(min_, max_)
    else:
        raise InvalidModule(
            "Invalid starting byte for limits type.  Expected starting byte to "
            f"be one of 0x00 or 0x01: Got {hex(flag)}")
Example #2
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 #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 validate_memory_type(memory_type: MemoryType) -> None:
    """
    Validate a MemoryType object.
    """
    limits = Limits(memory_type.min, memory_type.max)
    validate_limits(limits, constants.UINT16_CEIL)
Example #5
0
def test_validate_table_type_success():
    table_type = TableType(Limits(0, 100), FunctionAddress)

    validate_table_type(table_type)
Example #6
0
import pytest

from wasm.datatypes import (
    FunctionAddress,
    Limits,
    TableType,
)
from wasm.exceptions import (
    ValidationError, )
from wasm.validation.tables import (
    validate_table_type, )


def test_validate_table_type_success():
    table_type = TableType(Limits(0, 100), FunctionAddress)

    validate_table_type(table_type)


@pytest.mark.parametrize('table_type,match', (
    (TableType(Limits(10, 9), FunctionAddress), 'Limits'),
    (TableType(Limits(0, 100), None), 'FunctionAddress'),
))
def test_validate_table_type_failures(table_type, match):

    with pytest.raises(ValidationError, match=match):
        validate_table_type(table_type)
Example #7
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

    wasm.alloc_func(store, [[], []], test__func)
    wasm.alloc_func(store, [["i32"], []], test__func_i32)
    wasm.alloc_func(store, [["f32"], []], test__func_f32)
    wasm.alloc_func(store, [[], ["i32"]], test__func__i32)
    wasm.alloc_func(store, [[], ["f32"]], test__func__f32)
    wasm.alloc_func(store, [["i32"], ["i32"]], test__func_i32_i32)
    wasm.alloc_func(store, [["i64"], ["i64"]], test__func_i64_i64)
    wasm.alloc_mem(store, Limits(1, None))
    wasm.alloc_global(store, ["const", "i32"], 666)
    wasm.alloc_global(store, ["const", "f32"], 0.0)
    wasm.alloc_table(store, TableType(Limits(10, None), FuncRef))
    moduleinst = {
        "types": [
            [["i32"], []],
            [["f32"], []],
            [[], ["i32"]],
            [[], ["f32"]],
            [["i32"], ["i32"]],
            [["i64"], ["i64"]],
        ],
        "funcaddrs": [0, 1, 2, 3, 4, 5, 6],
        "tableaddrs": [0],
        "memaddrs": [0],
        "globaladdrs": [0, 1],
        "exports": [
            {"name": "func", "value": ["func", 0]},
            {"name": "func_i32", "value": ["func", 1]},
            {"name": "func_f32", "value": ["func", 2]},
            {"name": "func__i32", "value": ["func", 3]},
            {"name": "func__f32", "value": ["func", 4]},
            {"name": "func__i32_i32", "value": ["func", 5]},
            {"name": "func__i64_i64", "value": ["func", 6]},
            {"name": "memory-2-inf", "value": ["mem", 0]},
            {"name": "global-i32", "value": ["global", 0]},
            {"name": "global-f32", "value": ["global", 1]},
            {"name": "table-10-inf", "value": ["table", 0]},
        ],
    }
    return moduleinst
Example #8
0
def instantiate_spectest_module(store):
    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, []

    wasm.alloc_func(store, [["i32"], []], spectest__print_i32)
    wasm.alloc_func(store, [["i64"], []], spectest__print_i64)
    wasm.alloc_func(store, [["f32"], []], spectest__print_f32)
    wasm.alloc_func(store, [["f64"], []], spectest__print_f64)
    wasm.alloc_func(store, [["i32", "f32"], []], spectest__print_i32_f32)
    wasm.alloc_func(store, [["f64", "f64"], []], spectest__print_f64_f64)
    wasm.alloc_func(store, [[], []], spectest__print)
    wasm.alloc_mem(store, Limits(1, 2))  # min:1,max:2 required by import.wast:
    wasm.alloc_global(store, ["const", "i32"], 666)  # 666 required by import.wast
    wasm.alloc_global(store, ["const", "f32"], 0.0)
    wasm.alloc_global(store, ["const", "f64"], 0.0)
    wasm.alloc_table(
        store, TableType(Limits(10, 20), FuncRef)
    )  # max was 30, changed to 20 for import.wast
    moduleinst = {
        "types": [
            [["i32"], []],
            [["i64"], []],
            [["i32"], []],
            [["f64"], []],
            [["i32", "f32"], []],
            [["f64", "f64"], []],
            [[], []],
        ],
        "funcaddrs": [0, 1, 2, 3, 4, 5, 6],
        "tableaddrs": [0],
        "memaddrs": [0],
        "globaladdrs": [0, 1, 2],
        "exports": [
            {"name": "print_i32", "value": ["func", 0]},
            {"name": "print_i64", "value": ["func", 1]},
            {"name": "print_f32", "value": ["func", 2]},
            {"name": "print_f64", "value": ["func", 3]},
            {"name": "print_i32_f32", "value": ["func", 4]},
            {"name": "print_f64_f64", "value": ["func", 5]},
            {"name": "print", "value": ["func", 6]},
            {"name": "memory", "value": ["mem", 0]},
            {"name": "global_i32", "value": ["global", 0]},
            {"name": "global_f32", "value": ["global", 1]},
            {"name": "global_f64", "value": ["global", 2]},
            {"name": "table", "value": ["table", 0]},
        ],
    }
    return moduleinst
Example #9
0
)
from wasm.datatypes import (
    Limits,
)
from wasm.exceptions import (
    ValidationError,
)
from wasm.validation import (
    validate_limits,
)


@pytest.mark.parametrize(
    'limits,upper_bound,expected',
    (
        (Limits(0, None), 10, None),
        (Limits(0, 10), 10, None),
        (Limits(-1, 10), 10, "Limits.min is negative"),
        (Limits(0, 11), 10, "Limits.max exceeds upper bound"),
        (Limits(11, 20), 10, "Limits.min exceeds upper bound"),
        (Limits(10, 9), 10, "Limits.min exceeds Limits.max"),
        (
            Limits(0, constants.UINT32_CEIL),
            constants.UINT32_CEIL,
            "Limits.max is outside of u32 range"
        ),
        (
            Limits(constants.UINT32_CEIL, constants.UINT32_CEIL + 1),
            constants.UINT32_CEIL + 1,
            "Limits.min is outside of u32 range"
        ),
Example #10
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
Example #11
0
def instantiate_spectest_module(store):
    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, []

    wasm.alloc_func(store, FuncType((ValType.i32, ), ()), spectest__print_i32)
    wasm.alloc_func(store, FuncType((ValType.i64, ), ()), spectest__print_i64)
    wasm.alloc_func(store, FuncType((ValType.f32, ), ()), spectest__print_f32)
    wasm.alloc_func(store, FuncType((ValType.f64, ), ()), spectest__print_f64)
    wasm.alloc_func(store, FuncType((ValType.i32, ValType.f32), ()),
                    spectest__print_i32_f32)
    wasm.alloc_func(store, FuncType((ValType.f64, ValType.f64), ()),
                    spectest__print_f64_f64)
    wasm.alloc_func(store, FuncType((), ()), spectest__print)

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

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

    wasm.alloc_global(store, GlobalType(Mutability.const, ValType.f32), 0.0)
    wasm.alloc_global(store, GlobalType(Mutability.const, ValType.f64), 0.0)
    wasm.alloc_table(store, TableType(Limits(
        10, 20), FuncRef))  # max was 30, changed to 20 for import.wast
    moduleinst = {
        "types": [
            FuncType((ValType.i32, ), ()),
            FuncType((ValType.i64, ), ()),
            FuncType((ValType.f32, ), ()),
            FuncType((ValType.f64, ), ()),
            FuncType((ValType.i32, ValType.f32), ()),
            FuncType((ValType.f64, ValType.f64), ()),
            FuncType((), ()),
        ],
        "funcaddrs": [FuncIdx(idx) for idx in range(7)],
        "tableaddrs": [TableIdx(0)],
        "memaddrs": [MemoryIdx(0)],
        "globaladdrs": [GlobalIdx(0), GlobalIdx(1)],
        "exports": [
            Export("print_i32", FuncIdx(0)),
            Export("print_i64", FuncIdx(1)),
            Export("print_f32", FuncIdx(2)),
            Export("print_f64", FuncIdx(3)),
            Export("print_i32_f32", FuncIdx(4)),
            Export("print_f64_f64", FuncIdx(5)),
            Export("print", FuncIdx(6)),
            Export("memory", MemoryIdx(0)),
            Export("global_i32", GlobalIdx(0)),
            Export("global_f32", GlobalIdx(1)),
            Export("global_f64", GlobalIdx(2)),
            Export("table", TableIdx(0)),
        ],
    }
    return moduleinst
Example #12
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

    wasm.alloc_func(store, FuncType((), ()), test__func)
    wasm.alloc_func(store, FuncType((ValType.i32, ), ()), test__func_i32)
    wasm.alloc_func(store, FuncType((ValType.f32, ), ()), test__func_f32)
    wasm.alloc_func(store, FuncType((), (ValType.i32, )), test__func__i32)
    wasm.alloc_func(store, FuncType((), (ValType.f32, )), test__func__f32)
    wasm.alloc_func(store, FuncType((ValType.i32, ), (ValType.i32, )),
                    test__func_i32_i32)
    wasm.alloc_func(store, FuncType((ValType.i64, ), (ValType.i64, )),
                    test__func_i64_i64)
    wasm.alloc_mem(store, MemoryType(1, None))
    wasm.alloc_global(store, GlobalType(Mutability.const, ValType.i32), 666)
    wasm.alloc_global(store, GlobalType(Mutability.const, ValType.f32), 0.0)
    wasm.alloc_table(store, TableType(Limits(10, None), FuncRef))
    moduleinst = {
        "types": [
            FuncType((), ()),
            FuncType((ValType.i32, ), ()),
            FuncType((ValType.f32, ), ()),
            FuncType((), (ValType.i32, )),
            FuncType((), (ValType.f32, )),
            FuncType((ValType.i32, ), (ValType.i32, )),
            FuncType((ValType.i64, ), (ValType.i64, )),
        ],
        "funcaddrs": [FuncIdx(idx) for idx in range(7)],
        "tableaddrs": [TableIdx(0)],
        "memaddrs": [MemoryIdx(0)],
        "globaladdrs": [GlobalIdx(0), GlobalIdx(1)],
        "exports": [
            Export("func", FuncIdx(0)),
            Export("func_i32", FuncIdx(1)),
            Export("func_f32", FuncIdx(2)),
            Export("func__i32", FuncIdx(3)),
            Export("func__f32", FuncIdx(4)),
            Export("func__i32_i32", FuncIdx(5)),
            Export("func__i64_i64", FuncIdx(6)),
            Export("memory-2-inf", MemoryIdx(0)),
            Export("global-i32", GlobalIdx(0)),
            Export("global-f32", GlobalIdx(1)),
            Export("table-10-inf", TableIdx(0)),
        ],
    }
    return moduleinst