[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: camlSys__entry vs. FreeBSD kmod

On 11 Jul 2012, at 12:52, PALI Gabor Janos wrote:

> On Wed, Jul 11, 2012 at 12:37:43PM +0100, Anil Madhavapeddy wrote:
>> So Sys__entry is the native code entry point for the Sys module, which
>> will execute any top-level phrases. Looking at stdlib/sys.mlp, we have:
>> external get_config: unit -> string * int * bool = "caml_sys_get_config"
>> external get_argv: unit -> string * string array = "caml_sys_get_argv"
> Yeah, but in my current code, caml_sys_get_argv() is not called at all
> (because I do not have the "(executable_name,argv) = get_argv() line"),
> and the modified stdlib instance lives without it happily.
> Maybe that is the problem?  I did not see any use of having the argv stuff
> in the kernel, as the code running there will not be able to work it anyways.

Ah, your bug is just below that.  Your modified code looks like this:

> external get_config: unit -> string * int = "caml_sys_get_config"
> (*external get_argv: unit -> string * string array = "caml_sys_get_argv"*)
> (*let (executable_name, argv) = get_argv()*)
> let (os_type, word_size) = get_config()

Note the call to caml_sys_get_config, which in your modified version is:

> CAMLprim value caml_sys_get_config(value unit)
> {
> #if 0
>   CAMLparam0 ();   /* unit is unused */
>   CAMLlocal2 (result, ostype);
>   ostype = caml_copy_string(OCAML_OS_TYPE);
>   result = caml_alloc_small (2, 0);
>   Field(result, 0) = ostype;
>   Field(result, 1) = Val_long (8 * sizeof(value));
>   CAMLreturn (result);
> #else
>   return 0;
> #endif
> }

The function has to return a well-formed OCaml heap value that matches
the type of the external function declaration (in this case, a string*int).
Your modified version returns 0, which will cause heap corruption.  Just
restore the original caml_sys_get_config, and define OCAML_OS_TYPE as 
"kFreeBSD" or something (it's just for informational reasons).

For additional reading btw, it's worth going through this short 6-post
blog series about how the OCaml heap works:

Robert is correct that you should be wary of stack overflows as they can
be hard to spot without guard pages, but at this stage (module initialisation),
the stack usage will be very small indeed.




Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.