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

Mismatch between layout/with-c-layout and clang's reported layout #2

Closed
phronmophobic opened this issue Oct 30, 2022 · 3 comments
Closed

Comments

@phronmophobic
Copy link

I'm trying to use libclang to auto-generate struct layouts for use with coffi. As a sanity check, I wanted to make sure that the size of a struct reported by clang matched mem/size-of. It generally does, but I noticed there's a size difference when a struct includes pointers.

For example:

#include <stdio.h>

typedef struct S1 {
    char c;
    void* p;
} S1;

int main(int n, char** args){
    printf("size: %lu\n", sizeof(S1));
}

The above gives a size of 16, but the same struct definition in coffi gives 9:

(defalias ::S1
  (layout/with-c-layout
   [::mem/struct
    [[:c ::mem/char]
     [:p ::mem/pointer]]]))

(mem/size-of ::S1) ;; 9

It doesn't seem like there's anything wrong with the implementation. The byteAlignment of ValueLayout/ADDRESS is 1. I'm not really sure whether I'm using clang wrong or if there's something else going on. Either way, I thought I'd report it in case there is some improvement to be made.

@phronmophobic
Copy link
Author

I'm also getting a mismatch for the following struct:

typedef struct S2 {
    int i;
    void *data[2];
} S2;

int main(int n, char** args){
    printf("size: %lu\n", sizeof(S2));
}

This prints 24, but coffi/size-of reports 20.

(defalias ::S2
  (layout/with-c-layout
   [::mem/struct
    [[:i ::mem/int]
     [:data [:coffi.mem/array [:coffi.mem/pointer :coffi.mem/void] 2]]]]))

(mem/size-of ::S2) ;; 20

@IGJoshua
Copy link
Owner

Ah, yeah, this was an issue I observed before that I've determined a fix for but it appears I applied it inconsistently. All the value layouts in the JVM-side default to an alignment of 1, which is incorrect, and I corrected this for primitive numbers, but it appears I forgot to do the same for pointers. I believe that the array in your second example should be fixed if I fix the alignment of pointers, but I'll check up on that as well.

@IGJoshua
Copy link
Owner

This is now fixed on the latest develop. I'll be working on a release soon which will include it.

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

No branches or pull requests

2 participants