You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ARM64 macOS variadic arguments not passed properly in ctypes #92892
For variadic functions on macOS/arm64 you must explicitly annotate the fixed arguments to the variadic function (for printf the format string argument).
The reason for that is that the macOS/arm64 calling convention is different between plain and variadic functions (for the variadic arguments themselves) and types switches between the to based on the number of arguments passed to the function.
This probably needs some documentation.
P.S. Sorry about the vague language, I'll write something more coherent later.
The marked line just before calling printf tells ctypes about the expected types and number of fixed arguments, and that way the ctypes implementation knows that the additional integer argument should be passed using the varargs convention which is different than the argument passing convention for normal functions.
Thanks for the explanation. With your information, I fixed the original downstream issue.
I agree that we need to document this requirement. This is both a portability issue and a security issue, because the wrong value is passed into the external function without being noticed or detected.
I would suggest that we “recommend every variadic function to get its arguments specified”, because specifying the arguments does no harm on other platforms.
.. warning::
When calling variadic functions (such as ``printf``) certain platforms, such as ARM64 for Apple platforms, require specifying ``argtypes`` for the non-variadic parameters. In the case of ``printf`` this would be the ``const char*`` initial parameter.
.. code-block:: python3
libc.printf.argtypes = [ctypes.c_char_p]. # <--- argument type spec
Failure to do this will result in undesirable behavior. Since specifying ``argtypes`` doesn't inhibit portability, it's recommended to do so for all variadic functions.
Activity
ronaldoussoren commentedon May 18, 2022
For variadic functions on macOS/arm64 you must explicitly annotate the fixed arguments to the variadic function (for printf the format string argument).
The reason for that is that the macOS/arm64 calling convention is different between plain and variadic functions (for the variadic arguments themselves) and types switches between the to based on the number of arguments passed to the function.
This probably needs some documentation.
P.S. Sorry about the vague language, I'll write something more coherent later.
ronaldoussoren commentedon May 18, 2022
To expand on my previous comment, the following code works as expected:
The marked line just before calling printf tells ctypes about the expected types and number of fixed arguments, and that way the ctypes implementation knows that the additional integer argument should be passed using the varargs convention which is different than the argument passing convention for normal functions.
ronaldoussoren commentedon May 18, 2022
I'm not sure how to properly document this requirement, probably a new subsection in the "Calling Functions" section?
m13253 commentedon May 18, 2022
Thanks for the explanation. With your information, I fixed the original downstream issue.
I agree that we need to document this requirement. This is both a portability issue and a security issue, because the wrong value is passed into the external function without being noticed or detected.
I would suggest that we “recommend every variadic function to get its arguments specified”, because specifying the arguments does no harm on other platforms.
m13253 commentedon May 18, 2022
For anyone coming to this page in the future, here is the Apple official documentation:
https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Update-Code-that-Passes-Arguments-to-Variadic-Functions
(Read the section named “Update Code that Passes Arguments to Variadic Functions”)
Rapptz commentedon May 18, 2022
Thanks for the explanation and the response, it's greatly appreciated!
Maybe near the bottom of this section https://docs.python.org/3/library/ctypes.html#calling-functions-continued an admonition detailing what needs to be done for ARM64 could be written? Something like...
I think this would give it enough attention.
ronaldoussoren commentedon May 18, 2022
Something like that, although I'd prefer a new heading about calling variadic functions instead of inserting a warning about macOS.
ronaldoussoren commentedon May 22, 2022
Note that this is a duplicate of #87046
pythonGH-92892: Add section about variadic functions to ctypes docume…
ronaldoussoren commentedon Nov 16, 2022
I've created a PR that adds a section about this to the ctypes documentation.
GH-92892: Add section about variadic functions to ctypes documentation (
pythonGH-92892: Add section about variadic functions to ctypes docume…
pythonGH-92892: Add section about variadic functions to ctypes docume…
GH-92892: Add section about variadic functions to ctypes documentation (
GH-92892: Add section about variadic functions to ctypes documentation (
helper: improve logging; per-OS semaphore fixes