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

Select ctypes primitives at runtime #757

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

benozol
Copy link

@benozol benozol commented Oct 30, 2023

(Follow-up to #753)

Executing a program that uses ocaml-ctypes on a different platform from where the OCaml code was compiled can result in memory errors due to the wrong pointer arithmetic. For example, when a program is compiled on a 64-bit machine to OCaml bytecode and the bytecode is then compiled to 32-bit WebAssembly code, the execution of the Wasm program fails because ocaml-types uses 64-bit pointer arithmetic on a 32-bit architecture.

This PR makes the C data model selectable during compilation or runtime using an environment variable, i.e., CTYPES_DATA_MODEL=ILP32 to execute a program on 32-bit WebAssembly.

I can add other data models to this PR, but compiling OCaml bytecode to Wasm32 is I think the only way to trigger the failure, and Wasm32 uses ILP32.

Executing a program that uses `ocaml-ctypes` on a different platform from where
the OCaml code was compiled can result in memory errors due to the wrong pointer
arithmetic. For example, when a program is compiled on a 64-bit machine to OCaml
bytecode and the bytecode is subsequently compiled to 32-bit WebAssembly (Wasm)
code, `ocaml-types` uses 64-bit pointer arithmetic on a 32-bit architecture.

This commit makes the C data model selectable using an environment variable,
i.e., `CTYPES_DATA_MODEL=ILP32` for 32-bit Wasm. The data model can be selected
during compilation or during runtime.
| Short -> 2
| Int -> 4
| Long -> 4
| Llong -> 8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is only 4 (remember, the data bus is 32 bits, so it is no problem for the CPU to split a 64 bit lookup into two 32 bit lookups at 4-aligned addresses).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, wasm has an 8-alignment for long long. Compare e.g. with this page: https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/64bitPorting/transition/transition.html. This detail seems to be not fully standardized.

@yallop
Copy link
Owner

yallop commented Aug 23, 2024

Thank you for the proposal, @benozol.

I think the configure-time option is a fine idea, and I'd be happy to merge a PR that provided that option.

I'm less convinced about the option to select the sizes at runtime, since it seems likely to block inlining of the size and alignment information, which will introduce a performance cost for all programs that use ctypes.

@benozol
Copy link
Author

benozol commented Sep 10, 2024

Good point about the increased performance cost from the runtime configuration, will remove it. The drawback of the implemented compile-time configuration is that it works only at the compile-time of ocaml-ctypes (when running gen_c_primitives.exe) and not at the compile-time of the downstream code, which makes it hard to use. An alternative might be to add a separate package ocaml-ctypes-ilp32, but I'm not sure how much room should be given to this.

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.

3 participants