|
| 1 | +# Test that native code loaded from a .mpy file is retained after a GC. |
| 2 | + |
| 3 | +try: |
| 4 | + import gc, sys, uio, uos |
| 5 | + |
| 6 | + sys.implementation.mpy |
| 7 | + uio.IOBase |
| 8 | + uos.mount |
| 9 | +except (ImportError, AttributeError): |
| 10 | + print("SKIP") |
| 11 | + raise SystemExit |
| 12 | + |
| 13 | + |
| 14 | +class UserFile(uio.IOBase): |
| 15 | + def __init__(self, data): |
| 16 | + self.data = memoryview(data) |
| 17 | + self.pos = 0 |
| 18 | + |
| 19 | + def readinto(self, buf): |
| 20 | + n = min(len(buf), len(self.data) - self.pos) |
| 21 | + buf[:n] = self.data[self.pos : self.pos + n] |
| 22 | + self.pos += n |
| 23 | + return n |
| 24 | + |
| 25 | + def ioctl(self, req, arg): |
| 26 | + return 0 |
| 27 | + |
| 28 | + |
| 29 | +class UserFS: |
| 30 | + def __init__(self, files): |
| 31 | + self.files = files |
| 32 | + |
| 33 | + def mount(self, readonly, mksfs): |
| 34 | + pass |
| 35 | + |
| 36 | + def umount(self): |
| 37 | + pass |
| 38 | + |
| 39 | + def stat(self, path): |
| 40 | + if path in self.files: |
| 41 | + return (32768, 0, 0, 0, 0, 0, 0, 0, 0, 0) |
| 42 | + raise OSError |
| 43 | + |
| 44 | + def open(self, path, mode): |
| 45 | + return UserFile(self.files[path]) |
| 46 | + |
| 47 | + |
| 48 | +# Pre-compiled examples/natmod/features0 example for various architectures, keyed |
| 49 | +# by the required value of sys.implementation.mpy. |
| 50 | +features0_file_contents = { |
| 51 | + # -march=x64 -mcache-lookup-bc |
| 52 | + 0xB05: b'M\x05\x0b\x1f \x84b\xe9/\x00\x00\x00SH\x8b\x1ds\x00\x00\x00\xbe\x02\x00\x00\x00\xffS\x18\xbf\x01\x00\x00\x00H\x85\xc0u\x0cH\x8bC \xbe\x02\x00\x00\x00[\xff\xe0H\x0f\xaf\xf8H\xff\xc8\xeb\xe6ATUSH\x8b\x1dA\x00\x00\x00H\x8b\x7f\x08L\x8bc(A\xff\xd4H\x8d5\x1f\x00\x00\x00H\x89\xc5H\x8b\x05-\x00\x00\x00\x0f\xb78\xffShH\x89\xefA\xff\xd4H\x8b\x03[]A\\\xc3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x90\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x84@\x12factorial\x10\x00\x00\r \x01"\x9f\x1c\x01\x1e\xff', |
| 53 | + # -march=armv7m |
| 54 | + 0x1605: b"M\x05\x16\x1f \x84\x12\x1a\xe0\x00\x00\x13\xb5\nK\nJ{D\x9cX\x02!\xe3h\x98G\x03F\x01 3\xb9\x02!#i\x01\x93\x02\xb0\xbd\xe8\x10@\x18GXC\x01;\xf4\xe7\x00\xbfj\x00\x00\x00\x00\x00\x00\x00\xf8\xb5\tN\tK~D\xf4X@hgi\xb8G\x05F\x07K\x07I\xf2XyD\x10\x88ck\x98G(F\xb8G h\xf8\xbd6\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x1c\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x01\x84\x00\x12factorial\x10\x00\x00\r<\x01>\x9f8\x01:\xff", |
| 55 | +} |
| 56 | + |
| 57 | +# Populate other armv7m-derived archs based on armv7m. |
| 58 | +for arch in (0x1A05, 0x1E05, 0x2205): |
| 59 | + features0_file_contents[arch] = features0_file_contents[0x1605] |
| 60 | + |
| 61 | +if sys.implementation.mpy not in features0_file_contents: |
| 62 | + print("SKIP") |
| 63 | + raise SystemExit |
| 64 | + |
| 65 | +# These are the test .mpy files. |
| 66 | +user_files = {"/features0.mpy": features0_file_contents[sys.implementation.mpy]} |
| 67 | + |
| 68 | +# Create and mount a user filesystem. |
| 69 | +uos.mount(UserFS(user_files), "/userfs") |
| 70 | +sys.path.append("/userfs") |
| 71 | + |
| 72 | +# Import the native function. |
| 73 | +gc.collect() |
| 74 | +from features0 import factorial |
| 75 | + |
| 76 | +# Free the module that contained the function. |
| 77 | +del sys.modules["features0"] |
| 78 | + |
| 79 | +# Run a GC cycle which should reclaim the module but not the function. |
| 80 | +gc.collect() |
| 81 | + |
| 82 | +# Allocate lots of fragmented memory to overwrite anything that was just freed by the GC. |
| 83 | +for i in range(1000): |
| 84 | + [] |
| 85 | + |
| 86 | +# Run the native function, it should not have been freed or overwritten. |
| 87 | +print(factorial(10)) |
| 88 | + |
| 89 | +# Unmount and undo path addition. |
| 90 | +uos.umount("/userfs") |
| 91 | +sys.path.pop() |
0 commit comments