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

Size and align of size_t and pointer based on Sys.word_size #753

Closed
wants to merge 1 commit into from

Conversation

benozol
Copy link

@benozol benozol commented Sep 4, 2023

Executing a program that uses ocaml-ctypes on a platform where the word size differs from the word size of the platform where the program was originally compiled results in memory errors due to the wrong pointer arithmetic. We ran into this issue after first compiling an OCaml program on a 64-bit platform to OCaml bytecode and then compiling the OCaml bytecode to 32-bit WebAssembly (in our case using wasicaml but the same issue is expected using wasm_of_ocaml).

This PR uses Sys.word_size / 8 for the size and align of size_t and pointers, which resolves the issue (the program runs as bytecode and as Wasm).

My questions are 1) if this is valid for all supported platforms and 2) if ctypes_primitives.h requires corresponding modifications.

@yallop
Copy link
Owner

yallop commented Sep 11, 2023

Is there something special about size_t that means it should be treated differently to other integer types? int, long, etc., can vary in size from platform to platform, too.

In any case, I think we should retrieve the size of these types from the C implementation, not use OCaml's Sys.word_size.

@benozol
Copy link
Author

benozol commented Sep 11, 2023

Agreed, I didn't realize just how few assumptions can be made about the bit-sizes of most C types.

I am going to close this PR and open an issue to continue the discussion.

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

Successfully merging this pull request may close these issues.

2 participants