Chris00 / fftw-ocaml

Bindings to FFTW3.
Other
15 stars 5 forks source link

memory corruption bug? #2

Closed jimclune closed 11 years ago

jimclune commented 11 years ago

I want to report a possible memory corruption bug in the ocaml bindings for fftw3, version 0.5.2. Here is a small test case:

module FFT = Fftw3.D

let () = let my_array = Array.create 1000 "fish" in Printf.printf "my_array length: %d\n%!" (Array.length myarray); let = FFT.Genarray.create FFT.float Bigarray.c_layout [|10|] in Printf.printf "my_array length: %d\n%!" (Array.length my_array)

If I compile this with ocamlopt (version 4.00.1) and run it, I get:

my_array length: 1000 my_array length: 0

Note the array length is correctly reported as 1000 prior to the call to FFT.Genarray.create, but incorrectly reported as 0 afterwards, which I interpret as an indication of memory corruption. I also observe that if I call Gc.compact () immediately prior to FFT.Genarray.create, then it reports the correct array length afterwards.

Chris00 commented 11 years ago

I have not had time to look into it yet — I'll do it ASAP.

Chris00 commented 11 years ago

I compiled it to bytecode and native code with OCaml 3.12.1 and the correct result is printed. I'll try with 4.00.1 soon.

Chris00 commented 11 years ago

I tried with 4.00.1 and it works fine too. So I cannot reproduce your behavior. Please give more details.

jimclune commented 11 years ago

I am using Mac OS 10.8.2 and have fftw-3.3.3 installed. I am building with: eval opam config -env ocamlbuild -use-ocamlfind junk.native

I can't think of other details that might be relevant, unless I've managed to install something incorrectly (which is certainly possible). If you can think of other details I should provide, please let me know. Thanks.

Chris00 commented 11 years ago

I have performed the test with almost the same configuration exept that I am on Linux. Note however that Fftw3.D.Genarray.create does not uses the FFTW3 library. Maybe you want to replace fftw_free and fftw_malloc by the usual free and malloc and tell me whether the bug still occurs?

jimclune commented 11 years ago

The bug still occurs if I replace fftw_malloc and fftw_free with malloc and free. However, I think I see what the problem is. In my environment, STDC_VERSION is 199901L. I suspect you have a version that is lower than this. Can you confirm? See http://caml.inria.fr/mantis/view.php?id=5761 and the conditional preprocessing in ocaml 4.00.1 bigarray_stubs.c. It looks like this bug fix has not yet made it into fftw3_stubs.c. If I replace the two occurrences of "num_dims - 1" in fftw3_stubs.c with "num_dims", then the above test case works for me. I have not yet confirmed that this is sufficient for real fftw usage.

Chris00 commented 11 years ago

I have pushed some changes to use the lastest bigarray code. Let me know if it solves the problem.

jimclune commented 11 years ago

Hi, Chris. Thanks for the update. Sorry it took me a while to try it. I still hit the problem. It looks like one spot was missed in fftw3_stubs.c. I think it needs the following change to fftw3_caml_ba_alloc:

147,148c147
<                      sizeof(struct caml_ba_array)
<                      + (num_dims - 1) * sizeof(intnat),
---
>                      SIZEOF_BA_ARRAY + num_dims * sizeof(intnat),
Chris00 commented 11 years ago

You are right. I pushed that change.

Chris00 commented 11 years ago

Please let me know whether it then works for you. I'll then release a new OPAM package.

jimclune commented 11 years ago

Yes, it works for me. Thanks.

Chris00 commented 11 years ago

OPAM package created: https://github.com/OCamlPro/opam-repository/pull/526