diff options
author | Sviatoslav Peleshko <speles@mail.ua> | 2022-02-07 05:42:05 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-02-14 11:42:19 +0100 |
commit | a666140a686cfcc03e960340f8e2c43787947590 (patch) | |
tree | 0e7d43b5de5584a0a243e8898b58fade37c9301e /Userland/Libraries/LibCoredump | |
parent | 334ed9225aae43e84660106e8d81488ca19c0869 (diff) | |
download | serenity-a666140a686cfcc03e960340f8e2c43787947590.zip |
LibCoredump: Respect coredump's LD_LIBRARY_PATH when searching libraries
Previously, we would only resolve libraries from `/usr/lib`, which is
not the only path from which the crashed process could've loaded the
libraries from.
Diffstat (limited to 'Userland/Libraries/LibCoredump')
-rw-r--r-- | Userland/Libraries/LibCoredump/Backtrace.cpp | 8 | ||||
-rw-r--r-- | Userland/Libraries/LibCoredump/Backtrace.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibCoredump/Reader.cpp | 59 | ||||
-rw-r--r-- | Userland/Libraries/LibCoredump/Reader.h | 2 |
4 files changed, 52 insertions, 19 deletions
diff --git a/Userland/Libraries/LibCoredump/Backtrace.cpp b/Userland/Libraries/LibCoredump/Backtrace.cpp index 75eef1417d..6c77903e45 100644 --- a/Userland/Libraries/LibCoredump/Backtrace.cpp +++ b/Userland/Libraries/LibCoredump/Backtrace.cpp @@ -17,11 +17,9 @@ namespace Coredump { -ELFObjectInfo const* Backtrace::object_info_for_region(MemoryRegionInfo const& region) +ELFObjectInfo const* Backtrace::object_info_for_region(Reader const& coredump, MemoryRegionInfo const& region) { - String path = region.object_name(); - if (!path.starts_with('/') && Core::File::looks_like_shared_library(path)) - path = LexicalPath::join("/usr/lib", path).string(); + String path = coredump.resolve_object_path(region.object_name()); auto maybe_ptr = m_debug_info_cache.get(path); if (maybe_ptr.has_value()) @@ -116,7 +114,7 @@ void Backtrace::add_entry(const Reader& coredump, FlatPtr ip) // the PT_LOAD header for the .text segment isn't the first one // in the object file. auto region = coredump.first_region_for_object(object_name); - auto object_info = object_info_for_region(*region); + auto object_info = object_info_for_region(coredump, *region); if (!object_info) { m_entries.append({ ip, object_name, {}, {} }); return; diff --git a/Userland/Libraries/LibCoredump/Backtrace.h b/Userland/Libraries/LibCoredump/Backtrace.h index c0d4c05aa2..bb2db6dcdd 100644 --- a/Userland/Libraries/LibCoredump/Backtrace.h +++ b/Userland/Libraries/LibCoredump/Backtrace.h @@ -45,7 +45,7 @@ public: private: void add_entry(const Reader&, FlatPtr ip); - ELFObjectInfo const* object_info_for_region(MemoryRegionInfo const&); + ELFObjectInfo const* object_info_for_region(Reader const&, MemoryRegionInfo const&); bool m_skip_loader_so { false }; ELF::Core::ThreadInfo m_thread_info; diff --git a/Userland/Libraries/LibCoredump/Reader.cpp b/Userland/Libraries/LibCoredump/Reader.cpp index 3df26d58be..38c5d3ead3 100644 --- a/Userland/Libraries/LibCoredump/Reader.cpp +++ b/Userland/Libraries/LibCoredump/Reader.cpp @@ -8,11 +8,13 @@ #include <AK/HashTable.h> #include <AK/JsonObject.h> #include <AK/JsonValue.h> +#include <AK/LexicalPath.h> #include <LibCompress/Gzip.h> #include <LibCore/File.h> #include <LibCoredump/Reader.h> #include <signal_numbers.h> #include <string.h> +#include <unistd.h> namespace Coredump { @@ -277,13 +279,7 @@ const Reader::LibraryData* Reader::library_containing(FlatPtr address) const return {}; auto name = region->object_name(); - - String path; - if (Core::File::looks_like_shared_library(name)) - path = String::formatted("/usr/lib/{}", name); - else { - path = name; - } + String path = resolve_object_path(name); if (!cached_libs.contains(path)) { auto file_or_error = Core::MappedFile::map(path); @@ -297,6 +293,48 @@ const Reader::LibraryData* Reader::library_containing(FlatPtr address) const return lib_data; } +String Reader::resolve_object_path(StringView name) const +{ + // TODO: There are other places where similar method is implemented or would be useful. + // (e.g. UserspaceEmulator, LibSymbolication, Profiler, and DynamicLinker itself) + // We should consider creating unified implementation in the future. + + if (name.starts_with('/') || !Core::File::looks_like_shared_library(name)) { + return name; + } + + Vector<String> library_search_directories; + + // If LD_LIBRARY_PATH is present, check its folders first + for (auto& environment_variable : process_environment()) { + auto prefix = "LD_LIBRARY_PATH="sv; + if (environment_variable.starts_with(prefix)) { + auto ld_library_path = environment_variable.substring_view(prefix.length()); + + // FIXME: This code won't handle folders with ":" in the name correctly. + for (auto directory : ld_library_path.split_view(':')) { + library_search_directories.append(directory); + } + } + } + + // Add default paths that DynamicLinker uses + library_search_directories.append("/usr/lib/"sv); + library_search_directories.append("/usr/local/lib/"sv); + + // Search for the first readable library file + for (auto& directory : library_search_directories) { + auto full_path = LexicalPath::join(directory, name).string(); + + if (access(full_path.characters(), R_OK) != 0) + continue; + + return full_path; + } + + return name; +} + void Reader::for_each_library(Function<void(LibraryInfo)> func) const { HashTable<String> libraries; @@ -307,12 +345,7 @@ void Reader::for_each_library(Function<void(LibraryInfo)> func) const libraries.set(name); - String path; - if (Core::File::looks_like_shared_library(name)) - path = String::formatted("/usr/lib/{}", name); - else { - path = name; - } + String path = resolve_object_path(name); func(LibraryInfo { name, path, static_cast<FlatPtr>(region.region_start) }); return IterationDecision::Continue; diff --git a/Userland/Libraries/LibCoredump/Reader.h b/Userland/Libraries/LibCoredump/Reader.h index aee369f5e3..7d7f658d0d 100644 --- a/Userland/Libraries/LibCoredump/Reader.h +++ b/Userland/Libraries/LibCoredump/Reader.h @@ -70,6 +70,8 @@ public: }; const LibraryData* library_containing(FlatPtr address) const; + String resolve_object_path(StringView object_name) const; + int process_pid() const; u8 process_termination_signal() const; String process_executable_path() const; |