From 6f09971503f9bea177168d1599e925a5b22d18dd Mon Sep 17 00:00:00 2001 From: David 'Digit' Turner Date: Wed, 21 Jul 2010 16:15:28 -0700 Subject: [PATCH] bionic: add a test for dlopen(NULL,...) Change-Id: Iee8b33d1a046a71f5fd934912ee36371c1c9e8e5 --- tests/bionic/libc/Android.mk | 23 +++++++++++++++++- tests/bionic/libc/common/test_dlopen_null.c | 37 +++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tests/bionic/libc/common/test_dlopen_null.c diff --git a/tests/bionic/libc/Android.mk b/tests/bionic/libc/Android.mk index 48748508..10ed2027 100644 --- a/tests/bionic/libc/Android.mk +++ b/tests/bionic/libc/Android.mk @@ -32,10 +32,12 @@ define device-test $(eval LOCAL_MODULE := $(notdir $(file:%.c=%))) \ $(eval LOCAL_MODULE := $(LOCAL_MODULE:%.cpp=%)) \ $(eval LOCAL_CFLAGS += $(EXTRA_CFLAGS)) \ + $(eval LOCAL_LDFLAGS += $(EXTRA_LDLIBS)) \ $(eval LOCAL_MODULE_TAGS := tests) \ $(eval include $(BUILD_EXECUTABLE)) \ ) \ - $(eval EXTRA_CFLAGS :=) + $(eval EXTRA_CFLAGS :=) \ + $(eval EXTRA_LDLIBS :=) endef # same as 'device-test' but builds a host executable instead @@ -82,6 +84,25 @@ EXTRA_CFLAGS := -D_XOPEN_SOURCE=600 -DHOST $(call host-test, $(sources)) $(call device-test, $(sources)) +# The 'test_dlopen_null' tests requires specific linker flags +# +# The -Wl,--export-dynamic ensures that dynamic symbols are +# exported from the executable. +# +# -Wl,-u,foo is used to ensure that symbol "foo" is not +# garbage-collected by the gold linker, since the function +# appears to be unused. +# +sources := common/test_dlopen_null.c \ + +EXTRA_LDLIBS := -ldl -Wl,--export-dynamic -Wl,-u,foo +EXTRA_CFLAGS := -DHOST +$(call host-test, $(sources)) + +EXTRA_LDLIBS := -ldl -Wl,--export-dynamic -Wl,-u,foo +$(call device-test, $(sources)) + + sources := \ common/test_libgen.c \ diff --git a/tests/bionic/libc/common/test_dlopen_null.c b/tests/bionic/libc/common/test_dlopen_null.c new file mode 100644 index 00000000..42b23dd3 --- /dev/null +++ b/tests/bionic/libc/common/test_dlopen_null.c @@ -0,0 +1,37 @@ +#include +#include +#include + +extern int foo(void) +{ + return 42; +} + +int (*func_ptr)(void) = foo; + +int main(void) +{ + void* lib = dlopen(NULL, RTLD_NOW | RTLD_GLOBAL); + void* symbol; + +#if 0 + /* The Gold linker will garbage-collect unused global functions + * even if --Wl,--export-dynamic is used. So use a dummy global + * variable reference here to prevent this. + */ + if (foo() != 42) + return 3; +#endif + + if (lib == NULL) { + fprintf(stderr, "Could not open self-executable with dlopen(NULL) !!: %s\n", dlerror()); + return 1; + } + symbol = dlsym(lib, "foo"); + if (symbol == NULL) { + fprintf(stderr, "Could not lookup symbol inside executable !!: %s\n", dlerror()); + return 2; + } + dlclose(lib); + return 0; +} -- 2.11.0