diff options
author | 2022-01-18 11:38:39 -0800 | |
---|---|---|
committer | 2022-01-18 11:38:39 -0800 | |
commit | d3e17e7f7402ddc41272784cd5dd202eb6f7e4cd (patch) | |
tree | fc1c8bb83db842cbe7ad575a1b4c39dd4d163a5c /Python/pywarpx/_libwarpx.py | |
parent | 988cb01e64eb5677e3ac1f4c0d70dcf3c4b2ea3b (diff) | |
download | WarpX-d3e17e7f7402ddc41272784cd5dd202eb6f7e4cd.tar.gz WarpX-d3e17e7f7402ddc41272784cd5dd202eb6f7e4cd.tar.zst WarpX-d3e17e7f7402ddc41272784cd5dd202eb6f7e4cd.zip |
Python: Fix UB in Inputs Passing (#2726)
Trying to fix the macOS PyPy 3.7 error seen in https://github.com/conda-forge/warpx-feedstock/issues/37
Testing in https://github.com/conda-forge/warpx-feedstock/pull/38
After googling for a while, the original implementation was likely based on https://code.activestate.com/lists/python-list/704158, which contains bugs.
1) Bug: `create_string_buffer`
Allocating new, null-terminated char arrays with `ctypes.create_string_buffer` does lead to scrambled arrays in pypy3.7.
As far as I can see, this [should have also worked](https://docs.python.org/3/library/ctypes.html), but maybe there is a bug in the upstream implementation or the original code created some kind of use-after-free on a temporary while the new implementation just shares the existing byte address.
This leads to errors such as the ones here:
https://github.com/conda-forge/warpx-feedstock/pull/38#issuecomment-1010160519
The call `argvC[i] = ctypes.c_char_p(enc_arg)` is equivalent in creating a `NULL`-terminated char array.
2) Bug: Last Argv Argument
The last argument in the array of char arrays `argv` in ANSII C needs to be a plain `NULL` ptr.
Before this PR, this has been allocated but never initialized, leading to undefined behavior (read as: crashes).
Reference: https://stackoverflow.com/a/39096006/2719194
3) Cleanup: there is a pre-defined `ctypes.c_char_p` we can use for pointer of char.
Diffstat (limited to 'Python/pywarpx/_libwarpx.py')
-rwxr-xr-x | Python/pywarpx/_libwarpx.py | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/Python/pywarpx/_libwarpx.py b/Python/pywarpx/_libwarpx.py index 9de822739..105bfc37f 100755 --- a/Python/pywarpx/_libwarpx.py +++ b/Python/pywarpx/_libwarpx.py @@ -38,7 +38,7 @@ else: _path_libc = _find_library('c') _libc = ctypes.CDLL(_path_libc) _LP_c_int = ctypes.POINTER(ctypes.c_int) -_LP_c_char = ctypes.POINTER(ctypes.c_char) +_LP_c_char = ctypes.c_char_p class LibWarpX(): @@ -397,10 +397,15 @@ class LibWarpX(): def amrex_init(self, argv, mpi_comm=None): # --- Construct the ctype list of strings to pass in argc = len(argv) + + # note: +1 since there is an extra char-string array element, + # that ANSII C requires to be a simple NULL entry + # https://stackoverflow.com/a/39096006/2719194 argvC = (_LP_c_char * (argc+1))() for i, arg in enumerate(argv): enc_arg = arg.encode('utf-8') - argvC[i] = ctypes.create_string_buffer(enc_arg) + argvC[i] = _LP_c_char(enc_arg) + argvC[argc] = _LP_c_char(b"\0") # +1 element must be NULL if mpi_comm is None or MPI is None: self.libwarpx_so.amrex_init(argc, argvC) |