caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] memory corruption using C stub
@ 2017-06-10 15:33 Alexey Egorov
  2017-06-10 15:40 ` Viet Le
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Alexey Egorov @ 2017-06-10 15:33 UTC (permalink / raw)
  To: caml-list

Hello,

I have an OCaml application with some C code which (I believe) is the
reason of some random crashes.

Here is the code - https://pastebin.com/FVtLphZu
This function reads file at given offset, divides data into chunks and
compute checksums and compression ratio:
  external compute_data_props
    : string -> int -> int -> int -> (int * int * float) list =
"compute_data_props"

The problem is, after some calls to this stub, application is crashing
at random places in OCaml code.
I can't figure out what's going wrong, but replacing this stub with
dummy function (which does nothing but returns some predefined list)
eliminates the problem.

What can we do to debug it? We are using OCaml 4.04.1 and Windows 10,
and there is no other C stubs in our codebase.

Thanks!

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] memory corruption using C stub
  2017-06-10 15:33 [Caml-list] memory corruption using C stub Alexey Egorov
@ 2017-06-10 15:40 ` Viet Le
  2017-06-10 17:00   ` Alexey Egorov
  2017-06-10 15:44 ` Daniel Bünzli
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 13+ messages in thread
From: Viet Le @ 2017-06-10 15:40 UTC (permalink / raw)
  To: Alexey Egorov, caml-list

[-- Attachment #1: Type: text/plain, Size: 1188 bytes --]

Do you have stack frame to look at?


On Sat, 10 Jun 2017 at 16:33, Alexey Egorov <alex.only.d@gmail.com> wrote:

> Hello,
>
> I have an OCaml application with some C code which (I believe) is the
> reason of some random crashes.
>
> Here is the code - https://pastebin.com/FVtLphZu
> This function reads file at given offset, divides data into chunks and
> compute checksums and compression ratio:
>   external compute_data_props
>     : string -> int -> int -> int -> (int * int * float) list =
> "compute_data_props"
>
> The problem is, after some calls to this stub, application is crashing
> at random places in OCaml code.
> I can't figure out what's going wrong, but replacing this stub with
> dummy function (which does nothing but returns some predefined list)
> eliminates the problem.
>
> What can we do to debug it? We are using OCaml 4.04.1 and Windows 10,
> and there is no other C stubs in our codebase.
>
> Thanks!
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
-- 
Kind regards,
Viet

[-- Attachment #2: Type: text/html, Size: 2017 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] memory corruption using C stub
  2017-06-10 15:33 [Caml-list] memory corruption using C stub Alexey Egorov
  2017-06-10 15:40 ` Viet Le
@ 2017-06-10 15:44 ` Daniel Bünzli
  2017-06-10 15:50   ` Daniel Bünzli
  2017-06-10 16:01   ` Daniel Bünzli
  2017-06-11 11:37 ` Alexey Egorov
  2017-06-11 18:49 ` Alexey Egorov
  3 siblings, 2 replies; 13+ messages in thread
From: Daniel Bünzli @ 2017-06-10 15:44 UTC (permalink / raw)
  To: caml-list, Alexey Egorov

My C FFI may be a bit rusty and I only had a quick look but I think that on line 75 you need to put that caml_copy_double into a CAMLLocal variable since you are allocating afterwards on line 77.

See Rule 3 in http://caml.inria.fr/pub/docs/manual-ocaml/intfc.html

Best,

Daniel

P.S. A quick way of checking if you do everything correctly is to call Gc.full_major () after the call to C usually this crashes immediately if you did something wrong.



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] memory corruption using C stub
  2017-06-10 15:44 ` Daniel Bünzli
@ 2017-06-10 15:50   ` Daniel Bünzli
  2017-06-10 16:01   ` Daniel Bünzli
  1 sibling, 0 replies; 13+ messages in thread
From: Daniel Bünzli @ 2017-06-10 15:50 UTC (permalink / raw)
  To: Alexey Egorov, caml-list

On 10 June 2017 at 16:44:50, Daniel Bünzli (daniel.buenzli@erratique.ch) wrote:

> My C FFI may be a bit rusty and I only had a quick look but I think that on line 75 you need to  
> put that caml_copy_double into a CAMLLocal variable since you are allocating afterwards  
> on line 77.

(Or rearrange your code so that caml_copy_double is the last allocation you perform in the fun.)

D

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] memory corruption using C stub
  2017-06-10 15:44 ` Daniel Bünzli
  2017-06-10 15:50   ` Daniel Bünzli
@ 2017-06-10 16:01   ` Daniel Bünzli
  1 sibling, 0 replies; 13+ messages in thread
From: Daniel Bünzli @ 2017-06-10 16:01 UTC (permalink / raw)
  To: Alexey Egorov, caml-list


On 10 June 2017 at 16:44:50, Daniel Bünzli (daniel.buenzli@erratique.ch) wrote:
> My C FFI may be a bit rusty

It is. In fact forget what I said it's the first argument of Store_field you need to make sure to keep a root on. 

D



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] memory corruption using C stub
  2017-06-10 15:40 ` Viet Le
@ 2017-06-10 17:00   ` Alexey Egorov
  0 siblings, 0 replies; 13+ messages in thread
From: Alexey Egorov @ 2017-06-10 17:00 UTC (permalink / raw)
  To: Viet Le; +Cc: caml-list

Stack traces is not very helpful; it crashes at random places in OCaml code:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 4204.0xb4c]
0x0000000000414fac in camlSource__fun_6516 ()
(gdb)
(gdb) bt
#0  0x0000000000414fac in camlSource__fun_6516 ()
#1  0x0000000000000000 in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)


2017-06-10 20:40 GMT+05:00 Viet Le <vietlq85@gmail.com>:
> Do you have stack frame to look at?
>
>
> On Sat, 10 Jun 2017 at 16:33, Alexey Egorov <alex.only.d@gmail.com> wrote:
>>
>> Hello,
>>
>> I have an OCaml application with some C code which (I believe) is the
>> reason of some random crashes.
>>
>> Here is the code - https://pastebin.com/FVtLphZu
>> This function reads file at given offset, divides data into chunks and
>> compute checksums and compression ratio:
>>   external compute_data_props
>>     : string -> int -> int -> int -> (int * int * float) list =
>> "compute_data_props"
>>
>> The problem is, after some calls to this stub, application is crashing
>> at random places in OCaml code.
>> I can't figure out what's going wrong, but replacing this stub with
>> dummy function (which does nothing but returns some predefined list)
>> eliminates the problem.
>>
>> What can we do to debug it? We are using OCaml 4.04.1 and Windows 10,
>> and there is no other C stubs in our codebase.
>>
>> Thanks!
>>
>> --
>> Caml-list mailing list.  Subscription management and archives:
>> https://sympa.inria.fr/sympa/arc/caml-list
>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
> --
> Kind regards,
> Viet

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] memory corruption using C stub
  2017-06-10 15:33 [Caml-list] memory corruption using C stub Alexey Egorov
  2017-06-10 15:40 ` Viet Le
  2017-06-10 15:44 ` Daniel Bünzli
@ 2017-06-11 11:37 ` Alexey Egorov
  2017-06-11 11:45   ` David Allsopp
  2017-06-11 18:49 ` Alexey Egorov
  3 siblings, 1 reply; 13+ messages in thread
From: Alexey Egorov @ 2017-06-11 11:37 UTC (permalink / raw)
  To: caml-list

OK, I'm minimized this function as much as possible and it's still
crashing - https://pastebin.com/MZ0Qkh9B
Now I'm thinking that is compiler's bug on windows...

2017-06-10 20:33 GMT+05:00 Alexey Egorov <alex.only.d@gmail.com>:
> Hello,
>
> I have an OCaml application with some C code which (I believe) is the
> reason of some random crashes.
>
> Here is the code - https://pastebin.com/FVtLphZu
> This function reads file at given offset, divides data into chunks and
> compute checksums and compression ratio:
>   external compute_data_props
>     : string -> int -> int -> int -> (int * int * float) list =
> "compute_data_props"
>
> The problem is, after some calls to this stub, application is crashing
> at random places in OCaml code.
> I can't figure out what's going wrong, but replacing this stub with
> dummy function (which does nothing but returns some predefined list)
> eliminates the problem.
>
> What can we do to debug it? We are using OCaml 4.04.1 and Windows 10,
> and there is no other C stubs in our codebase.
>
> Thanks!

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] memory corruption using C stub
  2017-06-11 11:37 ` Alexey Egorov
@ 2017-06-11 11:45   ` David Allsopp
  2017-06-11 11:54     ` Alexey Egorov
  0 siblings, 1 reply; 13+ messages in thread
From: David Allsopp @ 2017-06-11 11:45 UTC (permalink / raw)
  To: Alexey Egorov; +Cc: caml-list

> On 11 Jun 2017, at 12:38, Alexey Egorov <alex.only.d@gmail.com> wrote:
> 
> OK, I'm minimized this function as much as possible and it's still
> crashing - https://pastebin.com/MZ0Qkh9B
> Now I'm thinking that is compiler's bug on windows...

Are you able to post the OCaml side of your minimised code? Which port of OCaml are you using?


David


> 2017-06-10 20:33 GMT+05:00 Alexey Egorov <alex.only.d@gmail.com>:
>> Hello,
>> 
>> I have an OCaml application with some C code which (I believe) is the
>> reason of some random crashes.
>> 
>> Here is the code - https://pastebin.com/FVtLphZu
>> This function reads file at given offset, divides data into chunks and
>> compute checksums and compression ratio:
>>  external compute_data_props
>>    : string -> int -> int -> int -> (int * int * float) list =
>> "compute_data_props"
>> 
>> The problem is, after some calls to this stub, application is crashing
>> at random places in OCaml code.
>> I can't figure out what's going wrong, but replacing this stub with
>> dummy function (which does nothing but returns some predefined list)
>> eliminates the problem.
>> 
>> What can we do to debug it? We are using OCaml 4.04.1 and Windows 10,
>> and there is no other C stubs in our codebase.
>> 
>> Thanks!
> 
> -- 
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] memory corruption using C stub
  2017-06-11 11:45   ` David Allsopp
@ 2017-06-11 11:54     ` Alexey Egorov
  2017-06-11 12:07       ` David Allsopp
  0 siblings, 1 reply; 13+ messages in thread
From: Alexey Egorov @ 2017-06-11 11:54 UTC (permalink / raw)
  To: David Allsopp; +Cc: caml-list

OCaml side is a complex server-side application; unfortunately, it's
closed-source and when I'm trying to minimize OCaml side too it stops
crashing...
I'm using fdopen's opam repository
(https://github.com/fdopen/opam-repository-mingw) with compiler's
versions from 4.03 to 4.05-beta3.


2017-06-11 16:45 GMT+05:00 David Allsopp <dra-news@metastack.com>:
>> On 11 Jun 2017, at 12:38, Alexey Egorov <alex.only.d@gmail.com> wrote:
>>
>> OK, I'm minimized this function as much as possible and it's still
>> crashing - https://pastebin.com/MZ0Qkh9B
>> Now I'm thinking that is compiler's bug on windows...
>
> Are you able to post the OCaml side of your minimised code? Which port of OCaml are you using?
>
>
> David
>
>
>> 2017-06-10 20:33 GMT+05:00 Alexey Egorov <alex.only.d@gmail.com>:
>>> Hello,
>>>
>>> I have an OCaml application with some C code which (I believe) is the
>>> reason of some random crashes.
>>>
>>> Here is the code - https://pastebin.com/FVtLphZu
>>> This function reads file at given offset, divides data into chunks and
>>> compute checksums and compression ratio:
>>>  external compute_data_props
>>>    : string -> int -> int -> int -> (int * int * float) list =
>>> "compute_data_props"
>>>
>>> The problem is, after some calls to this stub, application is crashing
>>> at random places in OCaml code.
>>> I can't figure out what's going wrong, but replacing this stub with
>>> dummy function (which does nothing but returns some predefined list)
>>> eliminates the problem.
>>>
>>> What can we do to debug it? We are using OCaml 4.04.1 and Windows 10,
>>> and there is no other C stubs in our codebase.
>>>
>>> Thanks!
>>
>> --
>> Caml-list mailing list.  Subscription management and archives:
>> https://sympa.inria.fr/sympa/arc/caml-list
>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>> Bug reports: http://caml.inria.fr/bin/caml-bugs

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] memory corruption using C stub
  2017-06-11 11:54     ` Alexey Egorov
@ 2017-06-11 12:07       ` David Allsopp
  2017-06-11 15:26         ` Alexey Egorov
  0 siblings, 1 reply; 13+ messages in thread
From: David Allsopp @ 2017-06-11 12:07 UTC (permalink / raw)
  To: Alexey Egorov; +Cc: David Allsopp, caml-list

On 11 Jun 2017, at 12:54, Alexey Egorov <alex.only.d@gmail.com> wrote:
> 
> OCaml side is a complex server-side application; unfortunately, it's
> closed-source and when I'm trying to minimize OCaml side too it stops
> crashing...

Ah well! Are there other packages you are using which themselves have C stubs? If minimising the OCaml application is also eliminating the problem, then it could be that it's just the allocations in your (correct) C stub which are revealing the crash elsewhere. Unfortunately, incorrect C stub can include OCaml unix functions...

Spattering Gc.full_major on the OCaml can help try to find corruptions earlier - have you tried that too?

> I'm using fdopen's opam repository
> (https://github.com/fdopen/opam-repository-mingw) with compiler's
> versions from 4.03 to 4.05-beta3.

I presume you're seeing the crash from all versions? Are you able to do a 4.02.3 test, or is that impractical?


David


> 2017-06-11 16:45 GMT+05:00 David Allsopp <dra-news@metastack.com>:
>>> On 11 Jun 2017, at 12:38, Alexey Egorov <alex.only.d@gmail.com> wrote:
>>> 
>>> OK, I'm minimized this function as much as possible and it's still
>>> crashing - https://pastebin.com/MZ0Qkh9B
>>> Now I'm thinking that is compiler's bug on windows...
>> 
>> Are you able to post the OCaml side of your minimised code? Which port of OCaml are you using?
>> 
>> 
>> David
>> 
>> 
>>> 2017-06-10 20:33 GMT+05:00 Alexey Egorov <alex.only.d@gmail.com>:
>>>> Hello,
>>>> 
>>>> I have an OCaml application with some C code which (I believe) is the
>>>> reason of some random crashes.
>>>> 
>>>> Here is the code - https://pastebin.com/FVtLphZu
>>>> This function reads file at given offset, divides data into chunks and
>>>> compute checksums and compression ratio:
>>>> external compute_data_props
>>>>   : string -> int -> int -> int -> (int * int * float) list =
>>>> "compute_data_props"
>>>> 
>>>> The problem is, after some calls to this stub, application is crashing
>>>> at random places in OCaml code.
>>>> I can't figure out what's going wrong, but replacing this stub with
>>>> dummy function (which does nothing but returns some predefined list)
>>>> eliminates the problem.
>>>> 
>>>> What can we do to debug it? We are using OCaml 4.04.1 and Windows 10,
>>>> and there is no other C stubs in our codebase.
>>>> 
>>>> Thanks!
>>> 
>>> --
>>> Caml-list mailing list.  Subscription management and archives:
>>> https://sympa.inria.fr/sympa/arc/caml-list
>>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>>> Bug reports: http://caml.inria.fr/bin/caml-bugs

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] memory corruption using C stub
  2017-06-11 12:07       ` David Allsopp
@ 2017-06-11 15:26         ` Alexey Egorov
  0 siblings, 0 replies; 13+ messages in thread
From: Alexey Egorov @ 2017-06-11 15:26 UTC (permalink / raw)
  To: David Allsopp; +Cc: caml-list

> Are there other packages you are using which themselves have C stubs?
No, except maybe stubs in Array and Unix modules (we are using lots of
Unix.create_process and related functions).
You are right, our C stub is correct - I increased a load on
application and it crashes even without calling this C function...

> I presume you're seeing the crash from all versions? Are you able to do a 4.02.3 test, or is that impractical?
Yes, app crashes in all versions. I tried 4.02.3 - still crashing :(


2017-06-11 17:07 GMT+05:00 David Allsopp <dra-news@metastack.com>:
> On 11 Jun 2017, at 12:54, Alexey Egorov <alex.only.d@gmail.com> wrote:
>>
>> OCaml side is a complex server-side application; unfortunately, it's
>> closed-source and when I'm trying to minimize OCaml side too it stops
>> crashing...
>
> Ah well! Are there other packages you are using which themselves have C stubs? If minimising the OCaml application is also eliminating the problem, then it could be that it's just the allocations in your (correct) C stub which are revealing the crash elsewhere. Unfortunately, incorrect C stub can include OCaml unix functions...
>
> Spattering Gc.full_major on the OCaml can help try to find corruptions earlier - have you tried that too?
>
>> I'm using fdopen's opam repository
>> (https://github.com/fdopen/opam-repository-mingw) with compiler's
>> versions from 4.03 to 4.05-beta3.
>
> I presume you're seeing the crash from all versions? Are you able to do a 4.02.3 test, or is that impractical?
>
>
> David
>
>
>> 2017-06-11 16:45 GMT+05:00 David Allsopp <dra-news@metastack.com>:
>>>> On 11 Jun 2017, at 12:38, Alexey Egorov <alex.only.d@gmail.com> wrote:
>>>>
>>>> OK, I'm minimized this function as much as possible and it's still
>>>> crashing - https://pastebin.com/MZ0Qkh9B
>>>> Now I'm thinking that is compiler's bug on windows...
>>>
>>> Are you able to post the OCaml side of your minimised code? Which port of OCaml are you using?
>>>
>>>
>>> David
>>>
>>>
>>>> 2017-06-10 20:33 GMT+05:00 Alexey Egorov <alex.only.d@gmail.com>:
>>>>> Hello,
>>>>>
>>>>> I have an OCaml application with some C code which (I believe) is the
>>>>> reason of some random crashes.
>>>>>
>>>>> Here is the code - https://pastebin.com/FVtLphZu
>>>>> This function reads file at given offset, divides data into chunks and
>>>>> compute checksums and compression ratio:
>>>>> external compute_data_props
>>>>>   : string -> int -> int -> int -> (int * int * float) list =
>>>>> "compute_data_props"
>>>>>
>>>>> The problem is, after some calls to this stub, application is crashing
>>>>> at random places in OCaml code.
>>>>> I can't figure out what's going wrong, but replacing this stub with
>>>>> dummy function (which does nothing but returns some predefined list)
>>>>> eliminates the problem.
>>>>>
>>>>> What can we do to debug it? We are using OCaml 4.04.1 and Windows 10,
>>>>> and there is no other C stubs in our codebase.
>>>>>
>>>>> Thanks!
>>>>
>>>> --
>>>> Caml-list mailing list.  Subscription management and archives:
>>>> https://sympa.inria.fr/sympa/arc/caml-list
>>>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>>>> Bug reports: http://caml.inria.fr/bin/caml-bugs

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Caml-list] memory corruption using C stub
  2017-06-10 15:33 [Caml-list] memory corruption using C stub Alexey Egorov
                   ` (2 preceding siblings ...)
  2017-06-11 11:37 ` Alexey Egorov
@ 2017-06-11 18:49 ` Alexey Egorov
  2017-06-11 19:20   ` David Allsopp
  3 siblings, 1 reply; 13+ messages in thread
From: Alexey Egorov @ 2017-06-11 18:49 UTC (permalink / raw)
  To: caml-list

I think I found a reason.
That was just stack overflow from non-tail-recursive List.map, and
OCaml on windows just crashes (C style) instead of throwing
exception...

2017-06-10 20:33 GMT+05:00 Alexey Egorov <alex.only.d@gmail.com>:
> Hello,
>
> I have an OCaml application with some C code which (I believe) is the
> reason of some random crashes.
>
> Here is the code - https://pastebin.com/FVtLphZu
> This function reads file at given offset, divides data into chunks and
> compute checksums and compression ratio:
>   external compute_data_props
>     : string -> int -> int -> int -> (int * int * float) list =
> "compute_data_props"
>
> The problem is, after some calls to this stub, application is crashing
> at random places in OCaml code.
> I can't figure out what's going wrong, but replacing this stub with
> dummy function (which does nothing but returns some predefined list)
> eliminates the problem.
>
> What can we do to debug it? We are using OCaml 4.04.1 and Windows 10,
> and there is no other C stubs in our codebase.
>
> Thanks!

^ permalink raw reply	[flat|nested] 13+ messages in thread

* RE: [Caml-list] memory corruption using C stub
  2017-06-11 18:49 ` Alexey Egorov
@ 2017-06-11 19:20   ` David Allsopp
  0 siblings, 0 replies; 13+ messages in thread
From: David Allsopp @ 2017-06-11 19:20 UTC (permalink / raw)
  To: Alexey Egorov, caml-list

Alexey Egorov wrote:
> I think I found a reason.
> That was just stack overflow from non-tail-recursive List.map, and OCaml
> on windows just crashes (C style) instead of throwing exception...

You'll be pleased to know that will be fixed in 4.06.0! https://github.com/ocaml/ocaml/pull/938. 


David

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2017-06-11 19:20 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-10 15:33 [Caml-list] memory corruption using C stub Alexey Egorov
2017-06-10 15:40 ` Viet Le
2017-06-10 17:00   ` Alexey Egorov
2017-06-10 15:44 ` Daniel Bünzli
2017-06-10 15:50   ` Daniel Bünzli
2017-06-10 16:01   ` Daniel Bünzli
2017-06-11 11:37 ` Alexey Egorov
2017-06-11 11:45   ` David Allsopp
2017-06-11 11:54     ` Alexey Egorov
2017-06-11 12:07       ` David Allsopp
2017-06-11 15:26         ` Alexey Egorov
2017-06-11 18:49 ` Alexey Egorov
2017-06-11 19:20   ` David Allsopp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).