-
Notifications
You must be signed in to change notification settings - Fork 95
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
base: master
Are you sure you want to change the base?
Conversation
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 |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
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. |
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 |
(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 becauseocaml-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.