Skip to content

Commit

Permalink
Python: Fix UB in Inputs Passing (#2726)
Browse files Browse the repository at this point in the history
Trying to fix the macOS PyPy 3.7 error seen in conda-forge/warpx-feedstock#37
Testing in conda-forge/warpx-feedstock#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:
conda-forge/warpx-feedstock#38 (comment)

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.
  • Loading branch information
ax3l authored Jan 18, 2022
1 parent 988cb01 commit d3e17e7
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Python/pywarpx/_libwarpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
_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():
Expand Down Expand Up @@ -397,10 +397,15 @@ def get_nattr_species(self, species_name):
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)
Expand Down

0 comments on commit d3e17e7

Please sign in to comment.