From a3128f89ccb1d24d0ddaec7164d2fa8a101b8ac3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 15 Nov 2024 13:52:04 +1100 Subject: [PATCH] tests: Fix all file ioctl's to support only MP_STREAM_CLOSE. A return value of 0 from Python-level `ioctl()` means success, but if that's returned unconditionally it means that the method supports all ioctl calls, which is not true. Returning 0 without doing anything can potentially lead to a crash, eg for MP_STREAM_SEEK which requires returning a value in the passed-in struct pointer. This commit makes it so that all `ioctl()` methods respond only to MP_STREAM_CLOSE, ie they return -1 (indicating error) for all other ioctl calls. Signed-off-by: Damien George --- tests/micropython/builtin_execfile.py | 4 +++- tests/micropython/import_mpy_invalid.py | 4 +++- tests/micropython/import_mpy_native.py | 4 +++- tests/micropython/import_mpy_native_gc.py | 4 +++- tests/perf_bench/core_import_mpy_multi.py | 4 +++- tests/perf_bench/core_import_mpy_single.py | 4 +++- tests/run-natmodtests.py | 4 +++- tests/run-tests.py | 4 +++- 8 files changed, 24 insertions(+), 8 deletions(-) diff --git a/tests/micropython/builtin_execfile.py b/tests/micropython/builtin_execfile.py index a905521c66..75a867bb94 100644 --- a/tests/micropython/builtin_execfile.py +++ b/tests/micropython/builtin_execfile.py @@ -16,7 +16,9 @@ class File(io.IOBase): self.off = 0 def ioctl(self, request, arg): - return 0 + if request == 4: # MP_STREAM_CLOSE + return 0 + return -1 def readinto(self, buf): buf[:] = memoryview(self.data)[self.off : self.off + len(buf)] diff --git a/tests/micropython/import_mpy_invalid.py b/tests/micropython/import_mpy_invalid.py index 89fdf44839..f928d45c79 100644 --- a/tests/micropython/import_mpy_invalid.py +++ b/tests/micropython/import_mpy_invalid.py @@ -22,7 +22,9 @@ class UserFile(io.IOBase): return n def ioctl(self, req, arg): - return 0 + if req == 4: # MP_STREAM_CLOSE + return 0 + return -1 class UserFS: diff --git a/tests/micropython/import_mpy_native.py b/tests/micropython/import_mpy_native.py index 8f5de25a0b..ac5e724e8d 100644 --- a/tests/micropython/import_mpy_native.py +++ b/tests/micropython/import_mpy_native.py @@ -28,7 +28,9 @@ class UserFile(io.IOBase): return n def ioctl(self, req, arg): - return 0 + if req == 4: # MP_STREAM_CLOSE + return 0 + return -1 class UserFS: diff --git a/tests/micropython/import_mpy_native_gc.py b/tests/micropython/import_mpy_native_gc.py index bdeb612b49..851eb39229 100644 --- a/tests/micropython/import_mpy_native_gc.py +++ b/tests/micropython/import_mpy_native_gc.py @@ -22,7 +22,9 @@ class UserFile(io.IOBase): return n def ioctl(self, req, arg): - return 0 + if req == 4: # MP_STREAM_CLOSE + return 0 + return -1 class UserFS: diff --git a/tests/perf_bench/core_import_mpy_multi.py b/tests/perf_bench/core_import_mpy_multi.py index 33437f9da8..8affa157fa 100644 --- a/tests/perf_bench/core_import_mpy_multi.py +++ b/tests/perf_bench/core_import_mpy_multi.py @@ -31,7 +31,9 @@ class File(io.IOBase): self.off = 0 def ioctl(self, request, arg): - return 0 + if request == 4: # MP_STREAM_CLOSE + return 0 + return -1 def readinto(self, buf): buf[:] = memoryview(file_data)[self.off : self.off + len(buf)] diff --git a/tests/perf_bench/core_import_mpy_single.py b/tests/perf_bench/core_import_mpy_single.py index 18454b8fd5..4d9aa67bf2 100644 --- a/tests/perf_bench/core_import_mpy_single.py +++ b/tests/perf_bench/core_import_mpy_single.py @@ -86,7 +86,9 @@ class File(io.IOBase): self.off = 0 def ioctl(self, request, arg): - return 0 + if request == 4: # MP_STREAM_CLOSE + return 0 + return -1 def readinto(self, buf): buf[:] = memoryview(file_data)[self.off : self.off + len(buf)] diff --git a/tests/run-natmodtests.py b/tests/run-natmodtests.py index f1a2a97469..1fe44bec16 100755 --- a/tests/run-natmodtests.py +++ b/tests/run-natmodtests.py @@ -35,7 +35,9 @@ class __File(io.IOBase): def __init__(self): self.off = 0 def ioctl(self, request, arg): - return 0 + if request == 4: # MP_STREAM_CLOSE + return 0 + return -1 def readinto(self, buf): buf[:] = memoryview(__buf)[self.off:self.off + len(buf)] self.off += len(buf) diff --git a/tests/run-tests.py b/tests/run-tests.py index c72178cd76..a609a1fcbb 100755 --- a/tests/run-tests.py +++ b/tests/run-tests.py @@ -68,7 +68,9 @@ class __File(io.IOBase): sys.modules['__injected_test'].__name__ = '__main__' self.off = 0 def ioctl(self, request, arg): - return 0 + if request == 4: # MP_STREAM_CLOSE + return 0 + return -1 def readinto(self, buf): buf[:] = memoryview(__buf)[self.off:self.off + len(buf)] self.off += len(buf)