diff --git a/src/ctypesgen/ctypedescs.py b/src/ctypesgen/ctypedescs.py index ac4d122b..44c31341 100755 --- a/src/ctypesgen/ctypedescs.py +++ b/src/ctypesgen/ctypedescs.py @@ -228,6 +228,8 @@ def visit(self, visitor): def py_string(self, ignore_can_be_ctype=None): if self.count is None: + # In a function declaration, an empty array is equivalent to a pointer. + # For struct FAMs, the ctypesparser changes count None to ConstantExpressionNode(0). return "POINTER(%s)" % self.base.py_string() if type(self.base) == CtypesArray: return "(%s) * int(%s)" % (self.base.py_string(), self.count.py_string(False)) diff --git a/src/ctypesgen/parser/ctypesparser.py b/src/ctypesgen/parser/ctypesparser.py index 425348f7..f7f88a11 100644 --- a/src/ctypesgen/parser/ctypesparser.py +++ b/src/ctypesgen/parser/ctypesparser.py @@ -85,16 +85,19 @@ def __init__(self, options): self.type_map.update(ctypes_type_map_python_builtin) def make_struct_from_specifier(self, specifier): + + # this method produces the input for the printer + variety = {True: "union", False: "struct"}[specifier.is_union] tag = specifier.tag if specifier.declarations: members = [] for declaration in specifier.declarations: + declarator = declaration.declarator t = self.get_ctypes_type( - declaration.type, declaration.declarator, check_qualifiers=True + declaration.type, declarator, check_qualifiers=True ) - declarator = declaration.declarator if declarator is None: # Anonymous field in nested union/struct (C11/GCC). name = None @@ -103,6 +106,12 @@ def make_struct_from_specifier(self, specifier): declarator = declarator.pointer name = declarator.identifier members.append((name, remove_function_pointer(t))) + + # handle FAM (flexible array member) at end of struct as zero-sized array (see GH issue #219) + _, last_ctype = members[-1] + if isinstance(last_ctype, CtypesArray) and last_ctype.count is None: + last_ctype.count = ConstantExpressionNode(0) + else: members = None diff --git a/src/ctypesgen/printer_python.py b/src/ctypesgen/printer_python.py index 6abf805c..5768aaac 100755 --- a/src/ctypesgen/printer_python.py +++ b/src/ctypesgen/printer_python.py @@ -190,6 +190,9 @@ def print_variable(self, variable): def print_struct(self, struct): + + # input produced by CtypesParser.make_struct_from_specifier() + self._srcinfo(struct) base = {"union": "Union", "struct": "Structure"}[struct.variety] self.file.write(f"class {struct.variety}_{struct.tag} ({base}):") diff --git a/tests/conftest.py b/tests/conftest.py index 09c25a9c..9f3940b4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -38,9 +38,10 @@ def ctypesgen_main(args, echo=True): print(str_args, file=sys.stderr) return ctypesgen.__main__.main(args) -def module_from_code(name, python_code): - # file_spoof = f"__file__ = '{TEST_DIR/'spoof.py'}'\n\n" - # python_code = file_spoof + python_code +def module_from_code(name, python_code, spoof_dir=None): + if spoof_dir: + file_spoof = f"__file__ = {str(spoof_dir/'spoof.py')!r}\n\n" + python_code = file_spoof + python_code module = types.ModuleType(name) exec(python_code, module.__dict__) return module @@ -48,7 +49,7 @@ def module_from_code(name, python_code): COUNTER = 0 -def generate(header=None, args=(), lang="py", cpp=MAIN_CPP, allow_gnuc=False): +def generate(header=None, args=(), lang="py", cpp=MAIN_CPP, allow_gnuc=False, spoof_dir=None): # Windows notes: # - Avoid stdlib tempfiles, they're not usable by anyone except the direct creator, otherwise you'll get permission errors. @@ -80,7 +81,7 @@ def generate(header=None, args=(), lang="py", cpp=MAIN_CPP, allow_gnuc=False): tmp_out.unlink() if lang.startswith("py"): - return module_from_code("tmp_module", content) + return module_from_code("tmp_module", content, spoof_dir) elif lang == "json": return json.loads(content), str(tmp_in) else: diff --git a/tests/testsuite.py b/tests/testsuite.py index 9c3ea31a..fd98043d 100755 --- a/tests/testsuite.py +++ b/tests/testsuite.py @@ -28,6 +28,7 @@ import shutil import math import unittest +import subprocess from contextlib import ( redirect_stdout, redirect_stderr, @@ -894,7 +895,7 @@ def test_macroman_encoding_source(self): self.assertEqual(module.MYSTRING, expected) -class TestEmptyHeader(unittest.TestCase): +class EmptyHeaderTest(unittest.TestCase): """ Test how ctypesgen behaves when no members were found. """ @classmethod @@ -913,9 +914,106 @@ def test_empty_header(self): self.assertFalse(self.outfile.exists()) +class FAMTest(unittest.TestCase): + """ + Test that flexible array members (FAMs) are handled as zero-sized arrays + rather than as pointer types. + See GH issue #219. Thanks to @Natanel-Shitrit. + """ + + @classmethod + def setUpClass(cls): + header_str = """ +#include + +// Header struct with flexible array member +struct msg_header { + uint32_t size; // Total size of the message + uint8_t type; // Type of the message + uint8_t payload[]; // Flexible array member +}; + +// First message type +// Note that nesting a struct with FAM may not conform with the C standard, +// but there are real-world headers in the wild which do this, and compilers +// seem to tolerate it, so it's still good to handle FAMs properly as +// zero-sized arrays to avoid wrong offsets in these cases. +struct msg_type1 { + struct msg_header header; + uint32_t value1; + uint32_t value2; + uint8_t extra[]; // Flexible array member for additional data +}; + +// Second message type +struct msg_type2 { + struct msg_header header; + uint64_t timestamp; + uint8_t extra[]; // Flexible array member for additional data +}; + +// Union to access either just the header or the complete message +union message { + struct msg_header header; + struct msg_type1 type1; + struct msg_type2 type2; +}; + +// A function declaration with empty array syntax, which should be handled +// as pointer (not as zero-sized array) here +void arraytest(int a[]); +""" + + # this is some extra work, but build an actual dummy library so we can test the function + c_str = """\ +#include "test_fam.h"\n +void arraytest(int a[]) { }; +""" + cls.h_path = TMP_DIR/"test_fam.h" + cls.c_path = TMP_DIR/"test_fam.c" + cls.h_path.write_text(header_str) + cls.c_path.write_text(c_str) + libname = "famtest.dll" if sys.platform == "win32" else "libfamtest.so" + cls.libpath = TMP_DIR/libname + subprocess.run(["gcc", "-shared", "-o", str(cls.libpath), str(cls.c_path)], check=True) + cls.module = generate(None, ["-i", cls.h_path, "-l", "famtest", "--compile-libdirs", str(TMP_DIR), "--runtime-libdirs", "."], spoof_dir=TMP_DIR) + + @classmethod + def tearDownClass(cls): + if not CLEANUP_OK: return + cls.h_path.unlink() + cls.c_path.unlink() + cls.libpath.unlink() + + def test_types(self): + # make sure the FAM fields are zero-sized arrays + m = self.module + msg_header_f = dict(m.msg_header._fields_) + msg_type1_f = dict(m.msg_type1._fields_) + msg_type2_f = dict(m.msg_type2._fields_) + self.assertEqual(msg_header_f["payload"], ctypes.c_uint8 * 0) + self.assertEqual(msg_type1_f["extra"], ctypes.c_uint8 * 0) + self.assertEqual(msg_type2_f["extra"], ctypes.c_uint8 * 0) + self.assertEqual(m.arraytest.argtypes, [ctypes.POINTER(ctypes.c_int)]) + + def test_object(self): + payload = "0123456789".encode("ascii") + size = len(payload) + typeval = 1 + + test_data = bytearray(ctypes.c_uint32(size)) + bytearray(ctypes.c_uint8(typeval)) + bytearray(payload) + + obj = self.module.msg_header.from_buffer(test_data) + self.assertEqual(obj.size, size) + self.assertEqual(obj.type, typeval) + + payload_back = (ctypes.c_uint8 * size).from_address(ctypes.addressof(obj.payload)) + self.assertEqual(payload, bytes(payload_back)) + + requires_gcc = unittest.skipUnless(shutil.which("gcc"), reason="Requires GCC") -class TestDefUndef(unittest.TestCase): +class DefUndefTest(unittest.TestCase): """ Test handling of defines/undefines passed to ctypesgen. Checks order, defaults, and overrides of defaults.