Memcheck: runtime typechecking for Ocaml

Warning: This is a legacy page that is only displayed for historical reasons. I stopped developing Memcheck in 2007 because Olmar development stopped then and Memcheck was only developed for Olmar. In contrast to Olmar there exist no alternatives to Memcheck. In 2016 I updated the sources to compile with camlp5 and OCaml 4.03.

Memcheck can check at runtime whether a given value has a certain type. This is useful

Memcheck is very similar to SafeUnmarshal. I wrote Memcheck as a substitute for SafeUnmarshal when I could not longer live with the deficiencies, performance problems and the seemingly dead status of SafeUnmarshal (see this message and this message on the ocaml mailing list).

Strictly speaking Memcheck (and SafeUnmarshal) only checks if the memory representation of a value agrees with a given type. The memory representation for, e.g. 0 and None are the same, therefore checking None against the type int will succeed. Similarly a tuple can be considered as a record or an array.

Page navigation

Status

Memcheck is just good enough to be used in the regression test of
Olmar. If you want to use it for something else you might have to do some nontrivial extensions. I am a bit reluctent to invest more time than absolutely needed into this, because Memcheck will fall into oblivion when the problems with SafeUnmarshal are resolved.

Please drop me a line at hendrik@askra.de if you are interested in Memcheck and it doesn't work out of the box. I will then try to help out.

Download / Installation

  1. install camlp5

  2. download the last cvs snapshot: memcheck-2016-10-14.tgz (should compile with camlp5 6.16 and OCaml 4.03.0)

  3. compilation: make all

Usage

  1. generate the runtime type descriptions from the source:
    echo "open Memcheck;;" > type_descr.ml
    camlp5o path_to_memcheck/generate_type_descr.cmo type_def.ml >> type_descr.ml
    
    This will generate a constant name_type_descr for every type definition found. For parametric type definitions it will be a function taking a type description argument for each parameter type.

  2. compile the runtime type descriptions:
    ocamlc -I path_to_memcheck -c type_descr.ml
    

  3. use it like
    open Memcheck;;
    check [Channel stdout; Verbose_trace] some_value (<type_name>_type_descr arguments ...)
    

Comparison with SafeUnmarshal

Memcheck was partly inspired by
SafeUnmarshal and shares a lot of idias with it. The most important differances are

Technology

The most important point is that Memcheck needs unique tags or identifications for type expressions that occor during runtime type checking. Because

Memcheck does the following to ensure that every type that occurs during runtime type checking is associated with precisely one tag:

The remainder is straightforward: Every memory block that is checked is registered in a hash table with the tag of its type. To detect sharing and cycles this hash is consulted. One only has to use structural equality in this hash (such that equal memory blocks at different addresses can have different types, like for instance for the two lists [0] and [None]).

Type-constructor applications are expanded when they are met during runtime type checking. Type-constructor applications are always boxed in a reference cell, which gets updated once the application is performed. Therefore, for instance, to check an int list, the list type constructor needs only to be applied twice: first at top level and then inside the list type constructor.

Known Problems

Contact

Please contact me at
hendrik@askra.de with anything Memcheck related.


last changed on 14 Oct 2016 by Hendrik