Signal handlers sometimes need to know whether the signal occurred
in ocamlopt-generated code, as opposed to runtime or C library code.
Today this determination uses a page table lookup to keep track
of dynamically-loaded modules, plus ad-hoc tests for the main program.
This PR uses the code fragment table instead. That's more reliable,
less ad-hoc, and independent of the page table.
i386nt.asm: add caml_system__code_{begin,end}, ,like in the other ports.
Global roots management has a special case for out-of-heap pointers.
This is not compatible with a future removal of the page table.
However, this is just an optimization that can safely be turned off.
This PR treats out-of-heap pointers like major-heap pointers in
no-naked-pointers mode.
Introduce type Obj.raw_data and functions Obj.raw_field, Obj.set_raw_field to manipulate out-of-heap pointers in no-naked-pointers mode, and more generally all other data that is not a well-formed OCaml value
These primitives are dangerous because they produce naked pointers
outside the OCaml heap, with a risk of "GC pointer confusion".
(After caml_free and a heap extension, the freed memory area can be
reallocated as part of the OCaml heap, causing the naked pointer to
become a bad heap pointer).
These primitives are not used anywhere in the core OCaml system
(in particular they are not exposed via the Obj module).
An OPAM-wide grep shows no uses there either.
This primitive (from runtime/obj.c) is being phased out because
it returns a naked pointer outside the OCaml heap.
Instead, for the test, use a statically-allocated buffer
that is never visible from OCaml.
We know where the code pointers, closure info words and infix headers are,
and can output them directly.
Currently activated in no-naked-pointers mode only, but would work in
the other mode as well.
* Introducing codefrag: a new runtime module to work with code fragments
This module collects all the operations on code fragments performed in
various places of the runtime systems. Applies both to bytecode and
to native code.
The implementation is based on skiplists, so that "lookup fragment by
PC" and "lookup fragment by number" are efficient (logarithmic in the
number of code fragments). "Lookup fragment by digest" remains
linear-time.
The new module also improves the handling of digests: now it is
possible to mark a code fragment as "no digest" i.e. not marshal-able.
* Use the new "codefrag" runtime module for marshaling and for the
debugger interface
Replace the previous handling of code fragments with calls to the
functions provided by the "codefrag" runtime module.
This patch removes casts between struct skiplist * and struct skipcell *, and removes the "layout compatibility" fields in skiplist that were there to enable these casts.
The only algorithmic difference is that caml_skiplist_find is now "stop-at" (see discussion here), as this was slightly easier to write in the no-cast style.
Some OCaml objects contain data that cannot be safely represented
as an OCaml value (type Obj.t). For example, in no-naked-pointers mode,
this is the case for code pointers inside closures, and for the
"custom operations" pointers inside custom blocks.
This PR introduces a type Obj.raw_data (an alias for nativeint)
to encapsulate this data, and functions
Obj.raw_field / Obj.set_raw_field to read and write the "raw" contents
of fields of blocks.
Note: just like it is wrong to access code pointers and custom operations
using Obj.field / Obj.set_field, it is wrong to access regular fields
possibly containing pointers into the OCaml heap using
Obj.raw_field / Obj.set_raw_field. The OCaml heap block can be
reclaimed or moved after its address was captured by Obj.raw_field.
Symmetrically, Obj.set_raw_field on a regular field bypasses the
write barrier of the GC.
We want to start allowing more information in the payload of
[@tailcall] attributes (currently no payload is supported), for
example we could consider using [@tailcall false] to ask the code
generator to disable a tail call.
A first required step in this direction is to use a custom datatype to
represent the tail-call attribute, instead of a boolean. This is
consistent with the other application-site
attributes (inline_attribute, specialise_attribute, local_attribute),
so it makes the code more regular -- but the change itself is
boilerplate-y.