Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rtld: Use breadth-first search order #1238

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 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 All @@ -33,10 +34,23 @@ foreach test_name : rtld_test_cases
test_native_link_with = []
test_native_depends = []
test_additional_link_args = []
test_skipped = false

# Build the needed DSOs for the test. This sets the variables above.
subdir(test_name)

if test_skipped
exec = executable('rtld-' + test_name, ['skipped.c', test_sources],
dependencies: libc_dep,
link_args: test_link_args,
)
test(test_name, exec, suite: 'rtld')
if build_tests_host_libc and not host_libc_excluded_test_cases.contains(test_name)
test(test_name, exec, suite: ['host-libc', 'rtld'])
endif
continue
endif

exec = executable('rtld-' + test_name, [test_name / 'test.c', test_sources],
link_with: test_link_with,
dependencies: libc_dep,
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;
}
3 changes: 3 additions & 0 deletions tests/rtld/skipped.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int main() {
return 77;
}
Loading