Coverage for examples/memory.py: 5%

56 statements  

« prev     ^ index     » next       coverage.py v7.11.3, created at 2025-12-01 19:40 +0000

1# An example of how to interact with wasm memory. 

2# 

3# Here a small wasm module is used to show how memory is initialized, how to 

4# read and write memory through the `Memory` object, and how wasm functions 

5# can trap when dealing with out-of-bounds addresses. 

6 

7from wasmtime import Store, Module, Instance, Trap, MemoryType, Memory, Limits, WasmtimeError 

8 

9def run(): 

10 # Create our `Store` context and then compile a module and create an 

11 # instance from the compiled module all in one go. 

12 store = Store() 

13 module = Module.from_file(store.engine, "examples/memory.wat") 

14 instance = Instance(store, module, []) 

15 

16 # Load up our exports from the instance 

17 exports = instance.exports(store) 

18 memory = exports["memory"] 

19 size_fn = exports["size"] 

20 load_fn = exports["load"] 

21 store_fn = exports["store"] 

22 

23 print("Checking memory...") 

24 assert(memory.size(store) == 2) 

25 assert(memory.data_len(store) == 0x20000) 

26 

27 # Note that usage of `data_ptr` is unsafe! This is a raw C pointer which is not 

28 # bounds checked at all. We checked our `data_len` above but you'll want to be 

29 # very careful when accessing data through `data_ptr()` 

30 assert(memory.data_ptr(store)[0] == 0) 

31 assert(memory.data_ptr(store)[0x1000] == 1) 

32 assert(memory.data_ptr(store)[0x1003] == 4) 

33 

34 assert(size_fn(store) == 2) 

35 assert(load_fn(store, 0) == 0) 

36 assert(load_fn(store, 0x1000) == 1) 

37 assert(load_fn(store, 0x1003) == 4) 

38 assert(load_fn(store, 0x1ffff) == 0) 

39 

40 

41 def assert_traps(func): 

42 try: 

43 func() 

44 assert(False) 

45 except Trap: 

46 pass 

47 except WasmtimeError: 

48 pass 

49 

50 

51 # out of bounds trap 

52 assert_traps(lambda: load_fn(store, 0x20000)) 

53 

54 print("Mutating memory...") 

55 memory.data_ptr(store)[0x1003] = 5 

56 store_fn(store, 0x1002, 6) 

57 # out of bounds trap 

58 assert_traps(lambda: store_fn(store, 0x20000, 0)) 

59 

60 assert(memory.data_ptr(store)[0x1002] == 6) 

61 assert(memory.data_ptr(store)[0x1003] == 5) 

62 assert(load_fn(store, 0x1002) == 6) 

63 assert(load_fn(store, 0x1003) == 5) 

64 

65 # Grow memory. 

66 print("Growing memory...") 

67 assert(memory.grow(store, 1)) 

68 assert(memory.size(store) == 3) 

69 assert(memory.data_len(store) == 0x30000) 

70 

71 assert(load_fn(store, 0x20000) == 0) 

72 store_fn(store, 0x20000, 0) 

73 assert_traps(lambda: load_fn(store, 0x30000)) 

74 assert_traps(lambda: store_fn(store, 0x30000, 0)) 

75 

76 # Memory can fail to grow 

77 assert_traps(lambda: memory.grow(store, 1)) 

78 assert(memory.grow(store, 0)) 

79 

80 print("Creating stand-alone memory...") 

81 memorytype = MemoryType(Limits(5, 5)) 

82 memory2 = Memory(store, memorytype) 

83 assert(memory2.size(store) == 5) 

84 assert_traps(lambda: memory2.grow(store, 1)) 

85 assert(memory2.grow(store, 0)) 

86 

87if __name__ == '__main__': 

88 run()