New submission from Ayappan <ayappa...@gmail.com>:

There seems to be a behavioral issue with ctypes in AIX. 
The specific symptom is that passing structures containing arrays to a C 
function by value appears to be broken.
Consider the below program.,

#!/usr/bin/env python3

from ctypes import *

libc = CDLL('libc.a(shr_64.o)')


class MemchrArgsHack(Structure):
    _fields_ = [("s", c_char_p), ("c", c_ulong), ("n", c_ulong)]


memchr_args_hack = MemchrArgsHack()
memchr_args_hack.s = b"abcdef"
memchr_args_hack.c = ord('d')
memchr_args_hack.n = 7


class MemchrArgsHack2(Structure):
    _fields_ = [("s", c_char_p), ("c_n", c_ulong * 2)]


memchr_args_hack2 = MemchrArgsHack2()
memchr_args_hack2.s = b"abcdef"
memchr_args_hack2.c_n[0] = ord('d')
memchr_args_hack2.c_n[1] = 7

print(
    CFUNCTYPE(c_char_p, c_char_p, c_uint, c_ulong,
              c_void_p)(('memchr', libc))(b"abcdef", c_uint(ord('d')),
                                          c_ulong(7), None))
print(
    CFUNCTYPE(c_char_p, MemchrArgsHack,
              c_void_p)(('memchr', libc))(memchr_args_hack, None))
print(
    CFUNCTYPE(c_char_p, MemchrArgsHack2,
              c_void_p)(('memchr', libc))(memchr_args_hack2, None))

This one uses memchr from the C library and passing it structures that would 
map to the registers that correspond to the arguments of memchr. This works for 
the first structure type in the reproducer; however, using the second structure 
type (which should be treated the same way) does not work.

In the failing case, the last register that should be used for the structure is 
not populated from the structure. The reproducer passes an extra argument so 
that the register is instead populated from that argument for the failing case 
(instead of working because the register still contains the correct value from 
a previous call).

The output should be the same for all three calls, but we get:
b'def'
b'def'
None

The last line is None, because memchr got 0 (the None passed on the call) for 
its length argument.

----------
components: ctypes
messages: 355632
nosy: Ayappan
priority: normal
severity: normal
status: open
title: Issue with ctypes in AIX
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue38628>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to