Skip to content
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

Cross-architecture: Why does wrapped structure has _pack_ set to True #38

Closed
blueforesticarus opened this issue Feb 12, 2018 · 3 comments

Comments

@blueforesticarus
Copy link

When running the following through clang2py

#define PACK __attribute__((packed,aligned(2))) 
typedef struct {
    long a;
    short b;
    char c;
    double d;
} PACK test;

I get this as ouput

class struct_c__SA_test(ctypes.Structure):
    _pack_ = True # source:True
    _fields_ = [
    ('a', ctypes.c_int32),
    ('b', ctypes.c_int16),
    ('c', ctypes.c_ubyte),
    ('d', ctypes.c_double),
    ('PADDING_0', ctypes.c_ubyte),
     ]
test = struct_c__SA_test

In the ctypes docs is says that pack should be a number (of bytes). When run through the original h2xml/xml2py the pack attribute is set to 2, as expected. Shouldn't this be the behavior of ctypeslib2 as well?

@fpf3
Copy link

fpf3 commented Aug 1, 2018

Seconding this. I have found a similar issue. Furthermore, if I am attempting to create an unpacked structure, it does not set _pack_ = False, as it should.

It seems that that clang2py has knowledge of how they are configured in the header file, and ignores it. You get something like:

class myStruct(Structure):
    _pack_ = True # source:False
    _fields_ = ...

I can only assume what "source:" refers to is the .h file.

@trolldbois
Copy link
Owner

trolldbois commented Aug 1, 2018

Short version: It's the result of the cross-architecture ability of ctypeslib.

My recollection is that pack and alignment are different attributes returned by clang.
In ctypeslib, pack will always be True to ensure that python ctypes fields are actually in the correct index.
If the alignment provided by tells clang says a field is not packed next the the previous one, a Padding field will be inserted in-between.

Given that ctypeslib is aware of multi-architecture issues, it's a requirement.
You just have to trust that the code & alignment, issued for the platform chosen by clang is correct.

You cannot ask a python 2.7 ctypes lib on a x86 arch to know how to pack a structure for a arm 128 arch.

E.g: the python source file can be used on any other system with possibly a different architecture for the python binary, and still work for the targeted arch. You can read x64 memory bytes from x86, and vice-versa.

@trolldbois trolldbois changed the title Wrapped structure has _pack_ set to True Cross-architecture: Why does wrapped structure has _pack_ set to True Aug 1, 2018
@jpvolkmann
Copy link

My understanding is that pack expects an integer number, not a boolean expression (according https://docs.python.org/2/library/ctypes.html#structure-union-alignment-and-byte-order ).
Because Python casts "True" to the integer 1 this works for ctypes library. Setting it to False will lead to a integer of 0 which is not valid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants