Skip to content

Commit

Permalink
tests/rtld: Add test for breadth-first search order
Browse files Browse the repository at this point in the history
  • Loading branch information
lzcunt committed Feb 2, 2025
1 parent 85a52b8 commit 73c8f83
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/rtld/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ rtld_test_cases = [
'scope3',
'scope4',
'scope5',
'search-order',
'tls_align',
'relr',
'symver',
Expand Down
19 changes: 19 additions & 0 deletions tests/rtld/search-order/libbar.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <assert.h>
#include <dlfcn.h>

#ifdef USE_HOST_LIBC
#define LIBFOO "libnative-foo.so"
#else
#define LIBFOO "libfoo.so"
#endif

int foo(void);

int bar(void) {
return foo() + 0xBABE;
}

[[constructor]] void init(void) {
void *libfoo = dlopen(LIBFOO, RTLD_LOCAL | RTLD_NOW);
assert(libfoo);
}
3 changes: 3 additions & 0 deletions tests/rtld/search-order/libfoo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int foo(void) {
return 0xCAFE0000;
}
37 changes: 37 additions & 0 deletions tests/rtld/search-order/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
patchelf = find_program('patchelf', required: false)

if patchelf.found()
# Overwrite LD_LIBRARY_PATH to make meson not fix our shenanigans
# Otherwise, meson's LD_LIBRARY_PATH makes this test useless because libfoo
# will be found through it instead of failing when libfoo is not found
test_env += ['LD_LIBRARY_PATH=']
test_native_env += ['LD_LIBRARY_PATH=']

libfoo = shared_library('foo', 'libfoo.c')
libbar_unpatched = shared_library('bar-unpatched', 'libbar.c',
dependencies: libc_dep, link_with: libfoo)
libbar = custom_target('patch-libbar',
command: [patchelf,
'--remove-rpath',
'--set-soname', 'libbar.so',
libbar_unpatched,
'--output', '@OUTPUT0@'],
output: ['libbar.so'],
)
test_link_with = [libbar, libfoo]

libfoo_native = shared_library('native-foo', 'libfoo.c', native: true)
libbar_native_unpatched = shared_library('native-bar-unpatched', 'libbar.c',
dependencies: rtlib_deps, link_with: libfoo_native, native: true)
libbar_native = custom_target('patch-libnative-bar',
command: [patchelf,
'--remove-rpath',
'--set-soname', 'libnative-bar.so',
libbar_native_unpatched,
'--output', '@OUTPUT0@'],
output: ['libnative-bar.so'],
)
test_native_link_with = [libbar_native, libfoo_native]
else
test_skipped = true
endif
21 changes: 21 additions & 0 deletions tests/rtld/search-order/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <assert.h>
#include <dlfcn.h>

#ifdef USE_HOST_LIBC
#define LIBBAR "libnative-bar.so"
#else
#define LIBBAR "libbar.so"
#endif

int foo();
int bar();

int main() {
void *libbar = dlopen(LIBBAR, RTLD_LOCAL | RTLD_NOW);
assert(libbar);

assert(foo() == (int) 0xCAFE0000);
assert(bar() == (int) 0xCAFEBABE);

return 0;
}

0 comments on commit 73c8f83

Please sign in to comment.