diff options
author | Rodrigo Tobar <rtobarc@gmail.com> | 2021-09-29 21:36:02 +0800 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-10-06 12:33:21 +0200 |
commit | 4b091a7cc27ef07ba5c0d46a02454ecbb16f6575 (patch) | |
tree | c07404e935b04559aa890c993ee3b84bb9b6dadd /Tests/LibELF/CMakeLists.txt | |
parent | 669b23ac0a62fa1c9ca13b6307469acf1f0ac34f (diff) | |
download | serenity-4b091a7cc27ef07ba5c0d46a02454ecbb16f6575.zip |
LibELF: Fix dynamic linking of dlopen()-ed libs
Consider the situation where two shared libraries libA and libB, both
depending (as in having a NEEDED dtag) on libC. libA is first
dlopen()-ed, which produces libC to be mapped and linked. When libB is
dlopen()-ed the DynamicLinker would re-map and re-link libC though,
causing any previous references to its old location to be invalid. And
if libA's PLT has been patched to point to libC's symbols, then any
further invocations to libA will cause the code to jump to a virtual
address that isn't mapped anymore, therefore causing a crash. This
situation was reported in #10014, although the setup was more convolved
in the ticket.
This commit fixes the issue by distinguishing between a main program
loading being performed by Loader.so, and a dlopen() call. The main
difference between these two cases is that in the former the
s_globals_objects maps is always empty, while in the latter it might
already contain dependencies for the library being dlopen()-ed. Hence,
when collecting dependencies to map and link, dlopen() should skip those
that are present in the global map to avoid the issue described above.
With this patch the original issue seen in #10014 is gone, with all
python3 modules (so far) loading correctly.
A unit test reproducing a simplified issue is also included in this
commit. The unit test includes the building of two dynamic libraries A
and B with both depending on libline.so (and B also depending on A); the
test then dlopen()s libA, invokes one its function, then does the same
with libB.
Diffstat (limited to 'Tests/LibELF/CMakeLists.txt')
-rw-r--r-- | Tests/LibELF/CMakeLists.txt | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/Tests/LibELF/CMakeLists.txt b/Tests/LibELF/CMakeLists.txt index ab5a9f200a..5e5303be1d 100644 --- a/Tests/LibELF/CMakeLists.txt +++ b/Tests/LibELF/CMakeLists.txt @@ -1,7 +1,21 @@ +macro(add_dlopen_lib NAME FUNCTION) + add_library(${NAME} SHARED Dynlib.cpp) + target_compile_definitions(${NAME} PRIVATE -DFUNCTION=${FUNCTION}) + # LibLine is not special, just an "external" dependency + target_link_libraries(${NAME} LibLine) + # Avoid execution by the test runner + install(TARGETS ${NAME} + DESTINATION usr/Tests/LibELF + PERMISSIONS OWNER_READ GROUP_READ WORLD_READ) +endmacro() +add_dlopen_lib(DynlibA dynliba_function) +add_dlopen_lib(DynlibB dynlibb_function) + set(TEST_SOURCES test-elf.cpp + TestDlOpen.cpp ) foreach(source IN LISTS TEST_SOURCES) - serenity_test("${source}" LibELF) + serenity_test("${source}" LibELF LIBS LibDl) endforeach() |