Add tests for the posix preloader wrappers

This patch introduces some tests for selected posix preloader wrappers.

The tests are autogenerated based on a small configuration files, to make it
easier to add more later on.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
preload/posix: Use #ifdef for stat() and friends

stat(), fstat() and lstat() are often defined as macros instead of functions,
which makes them much harder to override.

They were commented out, and this patch changes that to individual #ifdef, so
they can be enabled on platforms which have them as proper functions.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
preload/posix: Use RTLD_NEXT when available

This patch makes the posix preloader use RTLD_NEXT for finding the original
symbols of the functions we're wrapping, instead of needing
dlopen(LIBC_SONAME). It's a GNU extension, but very very useful.

The change also requires some reorganization, including a special case for
failing the function during its own initialization, which can happen if
dlopen()/dlsym() invoke the functions we are wrapping.

That works as long as those functions can cope with the failures, which is the
case in all tested platforms so far (and we're no worse than we were before
anyway).

Much of the initialization and libc handling is simplified, so the patch
removes more than twice the lines it adds.

The original motivation for this patch, however, is to fix the removal of the
malloc hooks in the latest glibc versions. With it, malloc() and friends no
longer need special treatment. Thanks a lot to Georg Profos for reporting this
issue, along with code samples for reproducing it.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
preload/posix: Do not wrap strdup() if it's a macro

glibc, when _GNU_SOURCE is defined, can have macros for strdup/strndup, so we
need to avoid generating wrappers for them.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
tests: Do not fail if fiu_enable_stack*() returns an error

fiu_enable_stack() and fiu_enable_stack_by_name() are only supported on some
platforms; where they're not, an error will be returned.

This small patch avoid failing the tests on that situation. Is not ideal
because the function could fail when it shouldn't, but until we introduce
other changes needed for detecting when it should and should not work, this
prevents the false negative.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
libfiu: Fail fiu_enable_stack() if we know we can't do it

When the backtrace related functions are not available, fail
fiu_enable_stack(); otherwise the caller may think it's going to work, when
it's not.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
libfiu: Include function boundaries

When checking if the instruction pointer is within a given function
boundaries, we have to include the boundaries themselves too.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
libfiu: Test if backtrace works in fiu_enable_stack()

In fiu_enable_stack(), test the backtrace-related functions to make sure they
work before adding the new failure point.

That way, the caller will know if the function isn't available on the platform
instead of the failure point not working when it should.

We need it because even on platforms where backtrace() is available, it may
not be working properly (because of build options, broken glibc, etc.).

The tester function takes the caller as a parameter to work around compiler
inlining issues that can result in a false negative.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
preload/posix: Remove stat() and friends

These functions were wrapped with #ifdefs in 2dafdf7, but it turns out they're
also often defined as inline functions in headers, which also prevents the
preloading from working correctly.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
libfiu: Fix backtrace_works() in fiu_enable_stack_by_name()

fiu_enable_stack_by_name() calls fiu_enable_stack(), which does the
backtrace_works() check.

For that check to work, fiu_enable_stack() must be on the stack. However, when
fiu_enable_stack_by_name() invokes it, it could be inlined, causing the check
to fail.

This patch adds an independent backtrace_works() check within
fiu_enable_stack_by_name(), so we're sure the caller is on the stack.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
tests/generated: Let generated tests be wrapped around #ifdefs

Some of the functions we want to test are not always included in the posix
preload library, but depend on some precompiler expression being defined.

This patch adds support for that in the test generator, which will be used in
subsequent patches.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
tests: Add generated tests for kill and mmap

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
Make sure we clean the tests when cleaning from the top level Makefile

This patch makes a top-level "make clean" also clean up the tests' binaries
and temporary files.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>