tests/rtdl: add basic rtdl tests
This commit is contained in:
parent
774bf3fa90
commit
6568a3c323
|
@ -4,7 +4,6 @@
|
|||
#include <dlfcn.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
#ifdef USE_HOST_LIBC
|
||||
#define LDSO_PATTERN "ld-linux-"
|
||||
#define LIBFOO "libnative-foo.so"
|
||||
|
@ -59,33 +58,32 @@ static int callback(struct dl_phdr_info *info, size_t size, void *data) {
|
|||
|
||||
int main() {
|
||||
struct result found = { 0 };
|
||||
printf("---\n");
|
||||
assert(!dl_iterate_phdr(callback, &found));
|
||||
assert(found.found_ldso == 1);
|
||||
assert(found.found_self == 1);
|
||||
assert(found.found_foo == 0);
|
||||
assert(found.found_bar == 0);
|
||||
printf("---\n");
|
||||
|
||||
memset(&found, 0, sizeof(found));
|
||||
void *bar = dlopen(LIBBAR, RTLD_LOCAL | RTLD_NOW);
|
||||
assert(bar);
|
||||
printf("---\n");
|
||||
assert(!dl_iterate_phdr(callback, &found));
|
||||
assert(found.found_ldso == 1);
|
||||
assert(found.found_self == 1);
|
||||
assert(found.found_foo == 1); // Since bar depends on foo.
|
||||
assert(found.found_bar == 1);
|
||||
assert(found.found_foo == 1); // Since bar depends on foo.
|
||||
printf("---\n");
|
||||
|
||||
memset(&found, 0, sizeof(found));
|
||||
void *foo = dlopen(LIBFOO, RTLD_GLOBAL | RTLD_NOW);
|
||||
assert(foo);
|
||||
printf("---\n");
|
||||
assert(!dl_iterate_phdr(callback, &found));
|
||||
assert(found.found_ldso == 1);
|
||||
assert(found.found_self == 1);
|
||||
assert(found.found_foo == 1);
|
||||
assert(found.found_foo > 0);
|
||||
assert(found.found_bar == 1);
|
||||
printf("---\n");
|
||||
|
||||
dlclose(bar);
|
||||
dlclose(foo);
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
rtdl_test_cases = [
|
||||
'dl_iterate_phdr',
|
||||
'noload-promote',
|
||||
'rtld_next',
|
||||
'soname',
|
||||
'preinit',
|
||||
'scope1',
|
||||
'scope2',
|
||||
'scope3',
|
||||
'scope4',
|
||||
]
|
||||
|
||||
foreach test_name : rtdl_test_cases
|
||||
|
|
1
tests/rtdl/noload-promote/libfoo.c
Normal file
1
tests/rtdl/noload-promote/libfoo.c
Normal file
|
@ -0,0 +1 @@
|
|||
void foo() {}
|
5
tests/rtdl/noload-promote/meson.build
Normal file
5
tests/rtdl/noload-promote/meson.build
Normal file
|
@ -0,0 +1,5 @@
|
|||
libfoo = shared_library('foo', 'libfoo.c')
|
||||
test_depends = [libfoo]
|
||||
|
||||
libfoo_native = shared_library('native-foo', 'libfoo.c', native: true)
|
||||
test_native_depends = [libfoo_native]
|
22
tests/rtdl/noload-promote/test.c
Normal file
22
tests/rtdl/noload-promote/test.c
Normal file
|
@ -0,0 +1,22 @@
|
|||
#include <dlfcn.h>
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef USE_HOST_LIBC
|
||||
#define LIBFOO "libnative-foo.so"
|
||||
#else
|
||||
#define LIBFOO "libfoo.so"
|
||||
#endif
|
||||
|
||||
int main() {
|
||||
void *foo = dlopen(LIBFOO, RTLD_LOCAL | RTLD_NOW);
|
||||
assert(foo);
|
||||
|
||||
assert(dlsym(RTLD_DEFAULT, "foo") == NULL);
|
||||
|
||||
// Opening a library with RTLD_NOLOAD | RTLD_GLOBAL should promote it to the global scope.
|
||||
assert(dlopen(LIBFOO, RTLD_NOLOAD | RTLD_GLOBAL | RTLD_NOW) == foo);
|
||||
assert(dlsym(RTLD_DEFAULT, "foo") != NULL);
|
||||
|
||||
assert(dlopen("does-not-exist.so.1337", RTLD_NOLOAD | RTLD_GLOBAL | RTLD_NOW) == NULL);
|
||||
}
|
14
tests/rtdl/scope1/libbar.c
Normal file
14
tests/rtdl/scope1/libbar.c
Normal file
|
@ -0,0 +1,14 @@
|
|||
char *foo(void);
|
||||
char *foo_global(void);
|
||||
|
||||
char *bar() {
|
||||
return "bar";
|
||||
}
|
||||
|
||||
char *bar_calls_foo() {
|
||||
return foo();
|
||||
}
|
||||
|
||||
char *bar_calls_foo_global() {
|
||||
return foo_global();
|
||||
}
|
9
tests/rtdl/scope1/libfoo.c
Normal file
9
tests/rtdl/scope1/libfoo.c
Normal file
|
@ -0,0 +1,9 @@
|
|||
char *foo() {
|
||||
return "foo";
|
||||
}
|
||||
|
||||
char global[] = "foo global";
|
||||
|
||||
char *foo_global() {
|
||||
return global;
|
||||
}
|
7
tests/rtdl/scope1/meson.build
Normal file
7
tests/rtdl/scope1/meson.build
Normal file
|
@ -0,0 +1,7 @@
|
|||
libfoo = shared_library('foo', 'libfoo.c')
|
||||
libbar = shared_library('bar', 'libbar.c', build_rpath: test_rpath, link_with: libfoo)
|
||||
test_depends = [libfoo, libbar]
|
||||
|
||||
libfoo_native = shared_library('native-foo', 'libfoo.c', native: true)
|
||||
libbar_native = shared_library('native-bar', 'libbar.c', build_rpath: test_rpath, link_with: libfoo_native, native: true)
|
||||
test_native_depends = [libfoo_native, libbar_native]
|
90
tests/rtdl/scope1/test.c
Normal file
90
tests/rtdl/scope1/test.c
Normal file
|
@ -0,0 +1,90 @@
|
|||
#include <stddef.h>
|
||||
#include <dlfcn.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef USE_HOST_LIBC
|
||||
#define LIBFOO "libnative-foo.so"
|
||||
#define LIBBAR "libnative-bar.so"
|
||||
#else
|
||||
#define LIBFOO "libfoo.so"
|
||||
#define LIBBAR "libbar.so"
|
||||
#endif
|
||||
|
||||
typedef char *strfn(void);
|
||||
|
||||
int main() {
|
||||
// We haven't dlopen'd these libs yet, so symbol resolution should fail.
|
||||
assert(dlsym(RTLD_DEFAULT, "foo") == NULL);
|
||||
assert(dlsym(RTLD_DEFAULT, "bar") == NULL);
|
||||
|
||||
assert(!dlopen(LIBFOO, RTLD_NOLOAD));
|
||||
assert(!dlopen(LIBBAR, RTLD_NOLOAD));
|
||||
|
||||
void *foo_handle = dlopen(LIBFOO, RTLD_LOCAL | RTLD_NOW);
|
||||
assert(foo_handle);
|
||||
assert(dlopen(LIBFOO, RTLD_NOLOAD | RTLD_NOW));
|
||||
|
||||
strfn *foo_sym = dlsym(foo_handle, "foo");
|
||||
assert(foo_sym);
|
||||
assert(foo_sym());
|
||||
assert(!strcmp(foo_sym(), "foo"));
|
||||
|
||||
strfn *foo_global_sym = dlsym(foo_handle, "foo_global");
|
||||
assert(foo_global_sym);
|
||||
assert(foo_global_sym());
|
||||
assert(!strcmp(foo_global_sym(), "foo global"));
|
||||
|
||||
assert(dlsym(foo_handle, "doesnotexist") == NULL);
|
||||
|
||||
// Nested opening should work
|
||||
assert(dlopen(LIBFOO, RTLD_LOCAL | RTLD_NOW) == foo_handle);
|
||||
assert(dlopen(LIBFOO, RTLD_NOLOAD | RTLD_NOW));
|
||||
|
||||
// Since we've loaded the same library twice, the addresses should be the same
|
||||
assert(dlsym(foo_handle, "foo") == foo_sym);
|
||||
assert(dlsym(foo_handle, "foo_global") == foo_global_sym);
|
||||
|
||||
// libfoo was opened with RTLD_LOCAL, so we shouldn't be able to lookup
|
||||
// its symbols in the global namespace.
|
||||
assert(dlsym(RTLD_DEFAULT, "foo") == NULL);
|
||||
|
||||
{
|
||||
void *bar_handle = dlopen(LIBBAR, RTLD_GLOBAL | RTLD_NOW);
|
||||
assert(bar_handle);
|
||||
assert(dlopen(LIBBAR, RTLD_NOLOAD | RTLD_NOW));
|
||||
|
||||
strfn *bar_sym = dlsym(bar_handle, "bar");
|
||||
assert(bar_sym);
|
||||
assert(bar_sym());
|
||||
assert(!strcmp(bar_sym(), "bar"));
|
||||
|
||||
strfn *bar_calls_foo_sym = dlsym(bar_handle, "bar_calls_foo");
|
||||
assert(bar_calls_foo_sym);
|
||||
assert(bar_calls_foo_sym());
|
||||
assert(!strcmp(bar_calls_foo_sym(), "foo"));
|
||||
|
||||
strfn *bar_calls_foo_global_sym = dlsym(bar_handle, "bar_calls_foo_global");
|
||||
assert(bar_calls_foo_global_sym);
|
||||
assert(bar_calls_foo_global_sym());
|
||||
assert(!strcmp(bar_calls_foo_global_sym(), "foo global"));
|
||||
|
||||
// libbar was opened with RTLD_GLOBAL, so we can find symbols by
|
||||
// searching in the global scope.
|
||||
strfn *new_bar_sym = dlsym(RTLD_DEFAULT, "bar");
|
||||
assert(new_bar_sym);
|
||||
assert(new_bar_sym == bar_sym);
|
||||
|
||||
// Note that we loaded libbar with RTLD_GLOBAL, which should pull
|
||||
// in libfoo's symbols globally too.
|
||||
strfn *new_foo_sym = dlsym(RTLD_DEFAULT, "foo");
|
||||
assert(new_foo_sym);
|
||||
assert(new_foo_sym == foo_sym);
|
||||
|
||||
assert(dlclose(bar_handle) == 0);
|
||||
}
|
||||
|
||||
assert(dlclose(foo_handle) == 0);
|
||||
assert(dlclose(foo_handle) == 0);
|
||||
}
|
5
tests/rtdl/scope2/libbar.c
Normal file
5
tests/rtdl/scope2/libbar.c
Normal file
|
@ -0,0 +1,5 @@
|
|||
char *foo_baz_conflict(void);
|
||||
|
||||
char *bar_calls_foo_baz_conflict() {
|
||||
return foo_baz_conflict();
|
||||
}
|
3
tests/rtdl/scope2/libbaz.c
Normal file
3
tests/rtdl/scope2/libbaz.c
Normal file
|
@ -0,0 +1,3 @@
|
|||
char *foo_baz_conflict() {
|
||||
return "resolved to baz";
|
||||
}
|
3
tests/rtdl/scope2/libfoo.c
Normal file
3
tests/rtdl/scope2/libfoo.c
Normal file
|
@ -0,0 +1,3 @@
|
|||
char *foo_baz_conflict() {
|
||||
return "resolved to foo";
|
||||
}
|
9
tests/rtdl/scope2/meson.build
Normal file
9
tests/rtdl/scope2/meson.build
Normal file
|
@ -0,0 +1,9 @@
|
|||
libfoo = shared_library('foo', 'libfoo.c')
|
||||
libbar = shared_library('bar', 'libbar.c', build_rpath: test_rpath, link_with: libfoo)
|
||||
libbaz = shared_library('baz', 'libbaz.c')
|
||||
test_depends = [libfoo, libbar, libbaz]
|
||||
|
||||
libfoo_native = shared_library('native-foo', 'libfoo.c', native: true)
|
||||
libbar_native = shared_library('native-bar', 'libbar.c', build_rpath: test_rpath, link_with: libfoo_native, native: true)
|
||||
libbaz_native = shared_library('native-baz', 'libbaz.c', native: true)
|
||||
test_native_depends = [libfoo_native, libbar_native, libbaz_native]
|
36
tests/rtdl/scope2/test.c
Normal file
36
tests/rtdl/scope2/test.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
#include <stddef.h>
|
||||
#include <dlfcn.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef USE_HOST_LIBC
|
||||
#define LIBBAR "libnative-bar.so"
|
||||
#define LIBBAZ "libnative-baz.so"
|
||||
#else
|
||||
#define LIBBAR "libbar.so"
|
||||
#define LIBBAZ "libbaz.so"
|
||||
#endif
|
||||
|
||||
typedef char *strfn(void);
|
||||
|
||||
int main() {
|
||||
void *baz = dlopen(LIBBAZ, RTLD_LAZY | RTLD_GLOBAL);
|
||||
assert(baz);
|
||||
|
||||
// At this point, baz is loaded in the global scope. When we load bar locally,
|
||||
// there is a relocation to `foo_baz_conflict` which is defined in both
|
||||
// foo (which is a dependency of bar), and baz. In this case baz should win
|
||||
// since we search the global scope first.
|
||||
|
||||
void *bar = dlopen(LIBBAR, RTLD_LAZY | RTLD_LOCAL);
|
||||
assert(bar);
|
||||
|
||||
strfn *bfn = dlsym(bar, "bar_calls_foo_baz_conflict");
|
||||
assert(!strcmp(bfn(), "resolved to baz"));
|
||||
|
||||
// TODO: Test RTLD_DEEPBIND and DT_SYMBOLIC once we implement it.
|
||||
|
||||
dlclose(bar);
|
||||
dlclose(baz);
|
||||
}
|
8
tests/rtdl/scope3/libbar.c
Normal file
8
tests/rtdl/scope3/libbar.c
Normal file
|
@ -0,0 +1,8 @@
|
|||
int g = 1;
|
||||
|
||||
int call_foo();
|
||||
|
||||
int call_bar() {
|
||||
return call_foo();
|
||||
}
|
||||
|
7
tests/rtdl/scope3/libbaz.c
Normal file
7
tests/rtdl/scope3/libbaz.c
Normal file
|
@ -0,0 +1,7 @@
|
|||
int g = 2;
|
||||
|
||||
int call_foo();
|
||||
|
||||
int call_baz() {
|
||||
return call_foo();
|
||||
}
|
5
tests/rtdl/scope3/libfoo.c
Normal file
5
tests/rtdl/scope3/libfoo.c
Normal file
|
@ -0,0 +1,5 @@
|
|||
int g = 0;
|
||||
|
||||
int call_foo() {
|
||||
return g;
|
||||
}
|
9
tests/rtdl/scope3/meson.build
Normal file
9
tests/rtdl/scope3/meson.build
Normal file
|
@ -0,0 +1,9 @@
|
|||
libfoo = shared_library('foo', 'libfoo.c')
|
||||
libbar = shared_library('bar', 'libbar.c', build_rpath: test_rpath, link_with: libfoo)
|
||||
libbaz = shared_library('baz', 'libbaz.c', build_rpath: test_rpath, link_with: libfoo)
|
||||
test_depends = [libfoo, libbar, libbaz]
|
||||
|
||||
libfoo_native = shared_library('native-foo', 'libfoo.c', native: true)
|
||||
libbar_native = shared_library('native-bar', 'libbar.c', build_rpath: test_rpath, link_with: libfoo_native, native: true)
|
||||
libbaz_native = shared_library('native-baz', 'libbaz.c', build_rpath: test_rpath, link_with: libfoo_native, native: true)
|
||||
test_native_depends = [libfoo_native, libbar_native, libbaz_native]
|
39
tests/rtdl/scope3/test.c
Normal file
39
tests/rtdl/scope3/test.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
#include <dlfcn.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
#ifdef USE_HOST_LIBC
|
||||
#define LIBBAR "libnative-bar.so"
|
||||
#define LIBBAZ "libnative-baz.so"
|
||||
#else
|
||||
#define LIBBAR "libbar.so"
|
||||
#define LIBBAZ "libbaz.so"
|
||||
#endif
|
||||
|
||||
int main() {
|
||||
// In this test, we have bar -> foo and baz -> foo (where -> means 'depends on').
|
||||
// All three objects contain a definition of a symbol g. Bar calls into foo to retrieve
|
||||
// what foo thinks g is, but since bar appears earlier in the scope than foo, bar's copy
|
||||
// of g wins.
|
||||
//
|
||||
// Next, we load baz, which is identical to bar. When baz calls into foo to retrieve g,
|
||||
// foo still sees bar's definition of g, so bar's copy of g wins.
|
||||
//
|
||||
// Swapping the load order of bar and baz should therefore change the value of g which
|
||||
// foo sees. This behaviour is why dlmopen exists. If we ever implement that, we should
|
||||
// write a similar test and assert that the calls return different results.
|
||||
|
||||
void *libbar = dlopen(LIBBAR, RTLD_LAZY | RTLD_LOCAL);
|
||||
int (*call_bar)(void) = dlsym(libbar, "call_bar");
|
||||
printf("call_bar: %d\n", call_bar());
|
||||
assert(call_bar() == 1);
|
||||
|
||||
void *libbaz = dlopen(LIBBAZ, RTLD_LAZY | RTLD_LOCAL);
|
||||
int (*call_baz)(void) = dlsym(libbaz, "call_baz");
|
||||
printf("call_baz: %d\n", call_baz());
|
||||
assert(call_baz() == 1);
|
||||
|
||||
|
||||
dlclose(libbar);
|
||||
dlclose(libbaz);
|
||||
}
|
3
tests/rtdl/scope4/libbar.c
Normal file
3
tests/rtdl/scope4/libbar.c
Normal file
|
@ -0,0 +1,3 @@
|
|||
// Bar needs to have a relocation against foo in order to set DT_NEEDED.
|
||||
void foo(void);
|
||||
void bar() { foo(); }
|
1
tests/rtdl/scope4/libbaz.c
Normal file
1
tests/rtdl/scope4/libbaz.c
Normal file
|
@ -0,0 +1 @@
|
|||
void baz() {}
|
3
tests/rtdl/scope4/libfoo.c
Normal file
3
tests/rtdl/scope4/libfoo.c
Normal file
|
@ -0,0 +1,3 @@
|
|||
// Foo needs to have a relocation against baz in order to set DT_NEEDED.
|
||||
void baz(void);
|
||||
void foo() { baz(); }
|
9
tests/rtdl/scope4/meson.build
Normal file
9
tests/rtdl/scope4/meson.build
Normal file
|
@ -0,0 +1,9 @@
|
|||
libbaz = shared_library('baz', 'libbaz.c')
|
||||
libfoo = shared_library('foo', 'libfoo.c', build_rpath: test_rpath, link_with: libbaz)
|
||||
libbar = shared_library('bar', 'libbar.c', build_rpath: test_rpath, link_with: libfoo)
|
||||
test_depends = [libfoo, libbar, libbaz]
|
||||
|
||||
libbaz_native = shared_library('native-baz', 'libbaz.c', native: true)
|
||||
libfoo_native = shared_library('native-foo', 'libfoo.c', build_rpath: test_rpath, link_with: libbaz_native, native: true)
|
||||
libbar_native = shared_library('native-bar', 'libbar.c', build_rpath: test_rpath, link_with: libfoo_native, native: true)
|
||||
test_native_depends = [libfoo_native, libbar_native, libbaz_native]
|
36
tests/rtdl/scope4/test.c
Normal file
36
tests/rtdl/scope4/test.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
#include <dlfcn.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
#ifdef USE_HOST_LIBC
|
||||
#define LIBFOO "libnative-foo.so"
|
||||
#define LIBBAR "libnative-bar.so"
|
||||
#define LIBBAZ "libnative-baz.so"
|
||||
#else
|
||||
#define LIBFOO "libfoo.so"
|
||||
#define LIBBAR "libbar.so"
|
||||
#define LIBBAZ "libbaz.so"
|
||||
#endif
|
||||
|
||||
int main() {
|
||||
// In this test, we have foo -> baz, bar -> foo (where '->' means 'depends on').
|
||||
// We first load foo with RTLD_LOCAL, and then load bar with RTLD_GLOBAL.
|
||||
// This should bring foo and bar into the global scope.
|
||||
|
||||
void *foo = dlopen(LIBFOO, RTLD_LOCAL | RTLD_NOW);
|
||||
assert(foo);
|
||||
assert(dlsym(foo, "foo"));
|
||||
assert(dlsym(foo, "baz"));
|
||||
assert(!dlsym(RTLD_DEFAULT, "foo"));
|
||||
assert(!dlsym(RTLD_DEFAULT, "baz"));
|
||||
|
||||
void *bar = dlopen(LIBBAR, RTLD_GLOBAL | RTLD_NOW);
|
||||
assert(bar);
|
||||
assert(dlsym(bar, "bar"));
|
||||
assert(dlsym(RTLD_DEFAULT, "bar"));
|
||||
assert(dlsym(RTLD_DEFAULT, "foo"));
|
||||
assert(dlsym(RTLD_DEFAULT, "baz"));
|
||||
|
||||
dlclose(foo);
|
||||
dlclose(bar);
|
||||
}
|
Loading…
Reference in a new issue