-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ARM64 macOS variadic arguments not passed properly in ctypes #92892
Comments
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. |
To expand on my previous comment, the following code works as expected: import ctypes
from ctypes import util
libc = ctypes.CDLL(util.find_library("c"))
libc.printf.argtypes = [ctypes.c_char_p]. # <--- argument type spec
libc.printf(b"hello %d world\n", 128_000_000) 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. |
I'm not sure how to properly document this requirement, probably a new subsection in the "Calling Functions" section? |
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. |
For anyone coming to this page in the future, here is the Apple official documentation: (Read the section named “Update Code that Passes Arguments to Variadic Functions”) |
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. |
Something like that, although I'd prefer a new heading about calling variadic functions instead of inserting a warning about macOS. |
Note that this is a duplicate of #87046 |
…ntation On some platforms, and in particular macOS/arm64, the calling convention for variadic arguments is different from the regular calling convention. Add a section to the documentation to document this.
I've created a PR that adds a section about this to the ctypes documentation. |
#99529) On some platforms, and in particular macOS/arm64, the calling convention for variadic arguments is different from the regular calling convention. Add a section to the documentation to document this.
…ntation (pythonGH-99529) On some platforms, and in particular macOS/arm64, the calling convention for variadic arguments is different from the regular calling convention. Add a section to the documentation to document this. (cherry picked from commit bc3a11d) Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com>
…ntation (pythonGH-99529) On some platforms, and in particular macOS/arm64, the calling convention for variadic arguments is different from the regular calling convention. Add a section to the documentation to document this. (cherry picked from commit bc3a11d) Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com>
GH-99529) On some platforms, and in particular macOS/arm64, the calling convention for variadic arguments is different from the regular calling convention. Add a section to the documentation to document this. (cherry picked from commit bc3a11d) Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com>
GH-99529) On some platforms, and in particular macOS/arm64, the calling convention for variadic arguments is different from the regular calling convention. Add a section to the documentation to document this. (cherry picked from commit bc3a11d) Co-authored-by: Ronald Oussoren <ronaldoussoren@mac.com>
* write to helper log from helper & children, resolve to absolute path so that cwd changes don't break * more detailed logging around forks/pids, and exit/semaphore messaging * `union semun` is defined differently on Linux & macOS... even though they're effectively the same, reflect accurately On macOS/arm64, it's critical to define the final argument to semctl() as variadic via ctypes (ie: don't define it), because the calling convention differs between fixed and variadic arguments. python/cpython#92892
Bug report
Using ctypes with variadic functions on ARM64 macOS machines seems to improperly pass the arguments, leading to truncation.
Minimal repro:
This happens regardless of casting it to a
ctypes.c_int
explicitly or not.On my regular machine (in this case an x64 Windows machine) it works as expected:
Your environment
I do not personally have a macOS machine, but I got a few others who did have a machine test for me. Their versions were as follows:
Machine 1:
Python 3.10.1, macOS 12.3.1 (21E258)
Machine 2:
Machine 3:
Linked PRs
The text was updated successfully, but these errors were encountered: