detect unchecked malloc failures with memtroll

memtroll is a new project which allows to detect a very common problem in C programs, unchecked malloc(3) failures.

Let’s assume a program like:

char *foo = malloc (6);
strcpy (foo, “hello”);

In most cases this program will work correctly but what happens when the malloc call fails?  The strcpy function will try to write at the address NULL, that is the malloc return value in case of errors.  When a process, under UNIX, tries to access memory that is not accessible, like the NULL address, then the process is terminated with a signal SIGSEGV

memtroll helps to diagnose these errors by iteratively execute the guarded program and make fail a different call to malloc, checking if the program handles correctly this case.

The code repository is available here:
https://gitorious.org/memtroll

Write portable programs using GNU Gnulib

Portability is a desired feature when software is developed.  Many strategies exist to address this problem.  The one I am going to describe is the way GNU Gnulib intends to help developers to write portable programs.

Gnulib is a source repository shared by different projects, it provides a complete framework to write portable programs and help with some tedious tasks (add support for automake/autoconf, i18l, …).

The principal Gnulib goal is to provide a POSIX systems, providing workarounds where it is not completely supported, under these conditions a program can be written transparently from the underlying particular system.  Gnulib components are shared at the source level, in other words, a program chooses which modules to use and those are imported in the program source tree.

To give an example, let’s say a program is using printf(3) with all its fancy format  directives and some of them are not available everywhere; the program can import the `printf-posix’ module and be sure to work in the same way almost everywhere.  The Gnulib include path must be used before any other path, in this way it can fix problems  with functions from the standard library, something like:

gcc -c -I gnulib printf_example.c -o printf_example.o
gcc printf_example.o gnulib/libgnu.a -o printf_example

In the previous example, gnulib modules were imported in the gnulib directory and compiled in the gnulib/libgnu.a static library.

Usually programs using Gnulib provides a `bootstrap’ script which takes care to fetch the specified Gnulib modules.

I have noticed some Gnulib modules are not yet working properly on uClibc and they fail in some cases.  Once these problems are fixed, many programs using Gnulib will be magically available for the OpenRISC platform.