Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
Stack smash(?) on Win64 causes C "float" function to return random results #5008
Original bug ID: 5008
On ocaml-3.11.2 (also verified on ocaml-3.11.1) compiled with Visual Studio 9.0 (i.e., 2008) and Windows SDK 6.0A on Windows 7 in 64-bit mode.
We define a C function with "float" calling convention:
double ml_single_precision_add_float(double a, double b)
external add : t->t->t = "ml_single_precision_add" "ml_single_precision_add_float" "float"
let x = add 1. 2. in ...
within a large OCaml program whose source I cannot disclose, unfortunately.
Then, non-deterministically (approx. 2 out of 10 runs), x will get a value which is not 3. (checked with Printf.printf), although the C function gets the right arguments and computes the right result (checked with printf).
I am not sure I understand fully what happens, but here is a guess.
In amd64nt.asm, caml_call_gc, change:
On my program, this seems to fix the issue.
I've also heard that the stack should be kept aligned on 16-bytes.
Comment author: @xavierleroy
Thanks for the detective work. You are correct that the bottom 32 bytes of stack are scratch space in the Win64 calling conventions. ocamlopt-generated code as well as other functions in amd64nt.asm carefully reserve this space, but caml_call_gc did not. I've applied your fix; it will go in release 3.12.0.