Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Kevin Marshall | 858c03f25 | 2018-04-12 19:24:06 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/native_library.h" |
| 6 | |
| 7 | #include <fcntl.h> |
Drew Fisher | 689f4ac | 2019-10-31 18:37:17 | [diff] [blame] | 8 | #include <fuchsia/io/cpp/fidl.h> |
| 9 | #include <lib/fdio/directory.h> |
Wez | 5c3c6f15 | 2018-06-09 18:24:02 | [diff] [blame] | 10 | #include <lib/fdio/io.h> |
Wez | 157707d6 | 2018-07-10 22:48:47 | [diff] [blame] | 11 | #include <lib/zx/vmo.h> |
Kevin Marshall | 858c03f25 | 2018-04-12 19:24:06 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <zircon/dlfcn.h> |
| 14 | #include <zircon/status.h> |
| 15 | #include <zircon/syscalls.h> |
| 16 | |
Helmut Januschka | 1dce9dc | 2024-06-11 13:05:35 | [diff] [blame] | 17 | #include <string_view> |
| 18 | |
David Dorwin | 2c7fbbf8 | 2021-11-06 21:08:58 | [diff] [blame] | 19 | #include "base/base_paths.h" |
Wez | 4e86105 | 2023-07-12 12:18:57 | [diff] [blame] | 20 | #include "base/files/file.h" |
Kevin Marshall | 858c03f25 | 2018-04-12 19:24:06 | [diff] [blame] | 21 | #include "base/files/file_path.h" |
Wez | 4e86105 | 2023-07-12 12:18:57 | [diff] [blame] | 22 | #include "base/fuchsia/fuchsia_logging.h" |
| 23 | #include "base/notreached.h" |
Kevin Marshall | 858c03f25 | 2018-04-12 19:24:06 | [diff] [blame] | 24 | #include "base/path_service.h" |
Wez | 4e86105 | 2023-07-12 12:18:57 | [diff] [blame] | 25 | #include "base/posix/safe_strerror.h" |
Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 26 | #include "base/strings/strcat.h" |
Kevin Marshall | 858c03f25 | 2018-04-12 19:24:06 | [diff] [blame] | 27 | #include "base/strings/stringprintf.h" |
Wez | 4e86105 | 2023-07-12 12:18:57 | [diff] [blame] | 28 | #include "base/strings/utf_string_conversions.h" |
Kevin Marshall | 858c03f25 | 2018-04-12 19:24:06 | [diff] [blame] | 29 | #include "base/threading/thread_restrictions.h" |
David Dorwin | 63617652 | 2022-08-09 22:47:29 | [diff] [blame] | 30 | #include "base_paths.h" |
Kevin Marshall | 858c03f25 | 2018-04-12 19:24:06 | [diff] [blame] | 31 | |
| 32 | namespace base { |
| 33 | |
| 34 | std::string NativeLibraryLoadError::ToString() const { |
| 35 | return message; |
| 36 | } |
| 37 | |
| 38 | NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path, |
| 39 | const NativeLibraryOptions& options, |
| 40 | NativeLibraryLoadError* error) { |
Kevin Marshall | aee8cf2 | 2018-09-19 19:24:18 | [diff] [blame] | 41 | FilePath computed_path; |
Guocheng Wei | 9b06fa0 | 2022-08-23 17:49:51 | [diff] [blame] | 42 | FilePath library_root_path = |
| 43 | base::PathService::CheckedGet(DIR_ASSETS).Append("lib"); |
| 44 | if (library_path.IsAbsolute()) { |
| 45 | // See more info in fxbug.dev/105910. |
| 46 | if (!library_root_path.IsParent(library_path)) { |
| 47 | auto error_message = |
| 48 | base::StringPrintf("Absolute library paths must begin with %s", |
| 49 | library_root_path.value().c_str()); |
| 50 | DLOG(ERROR) << error_message; |
| 51 | if (error) { |
| 52 | error->message = std::move(error_message); |
| 53 | } |
| 54 | return nullptr; |
| 55 | } |
| 56 | computed_path = library_path; |
| 57 | } else { |
| 58 | computed_path = library_root_path.Append(library_path); |
| 59 | } |
Drew Fisher | 689f4ac | 2019-10-31 18:37:17 | [diff] [blame] | 60 | |
Brandon Castellano | 853cd2d | 2024-12-19 21:08:20 | [diff] [blame] | 61 | // Use fdio_open3_fd (a Fuchsia-specific API) here so we can pass the |
Drew Fisher | 689f4ac | 2019-10-31 18:37:17 | [diff] [blame] | 62 | // appropriate FS rights flags to request executability. |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 63 | // TODO(crbug.com/40655456): Teach base::File about FLAG_WIN_EXECUTE on |
Brandon Castellano | 853cd2d | 2024-12-19 21:08:20 | [diff] [blame] | 64 | // Fuchsia, and then use it here instead of using fdio_open3_fd() directly. |
Drew Fisher | 689f4ac | 2019-10-31 18:37:17 | [diff] [blame] | 65 | base::ScopedFD fd; |
Brandon Castellano | 853cd2d | 2024-12-19 21:08:20 | [diff] [blame] | 66 | zx_status_t status = |
| 67 | fdio_open3_fd(computed_path.value().c_str(), |
| 68 | static_cast<uint64_t>(fuchsia::io::PERM_READABLE | |
| 69 | fuchsia::io::PERM_EXECUTABLE), |
| 70 | base::ScopedFD::Receiver(fd).get()); |
Wez | 4e86105 | 2023-07-12 12:18:57 | [diff] [blame] | 71 | if (status != ZX_OK) { |
Kevin Marshall | 858c03f25 | 2018-04-12 19:24:06 | [diff] [blame] | 72 | if (error) { |
Wez | 4e86105 | 2023-07-12 12:18:57 | [diff] [blame] | 73 | error->message = |
| 74 | base::StringPrintf("fdio_open_fd: %s", zx_status_get_string(status)); |
Kevin Marshall | 858c03f25 | 2018-04-12 19:24:06 | [diff] [blame] | 75 | } |
| 76 | return nullptr; |
| 77 | } |
| 78 | |
Wez | 157707d6 | 2018-07-10 22:48:47 | [diff] [blame] | 79 | zx::vmo vmo; |
Wez | 4e86105 | 2023-07-12 12:18:57 | [diff] [blame] | 80 | status = fdio_get_vmo_exec(fd.get(), vmo.reset_and_get_address()); |
| 81 | if (status != ZX_OK) { |
Kevin Marshall | 858c03f25 | 2018-04-12 19:24:06 | [diff] [blame] | 82 | if (error) { |
Drew Fisher | 689f4ac | 2019-10-31 18:37:17 | [diff] [blame] | 83 | error->message = base::StringPrintf("fdio_get_vmo_exec: %s", |
Drew Fisher | 1247389d | 2019-04-19 02:03:18 | [diff] [blame] | 84 | zx_status_get_string(status)); |
| 85 | } |
| 86 | return nullptr; |
| 87 | } |
| 88 | |
Kevin Marshall | 858c03f25 | 2018-04-12 19:24:06 | [diff] [blame] | 89 | NativeLibrary result = dlopen_vmo(vmo.get(), RTLD_LAZY | RTLD_LOCAL); |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | void UnloadNativeLibrary(NativeLibrary library) { |
| 94 | // dlclose() is a no-op on Fuchsia, so do nothing here. |
| 95 | } |
| 96 | |
| 97 | void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
David Benjamin | a942007 | 2023-06-13 14:38:36 | [diff] [blame] | 98 | const char* name) { |
| 99 | return dlsym(library, name); |
Kevin Marshall | 858c03f25 | 2018-04-12 19:24:06 | [diff] [blame] | 100 | } |
| 101 | |
Helmut Januschka | 1dce9dc | 2024-06-11 13:05:35 | [diff] [blame] | 102 | std::string GetNativeLibraryName(std::string_view name) { |
Jan Wilken Dörrie | 5db50ac | 2021-02-15 11:43:16 | [diff] [blame] | 103 | return StrCat({"lib", name, ".so"}); |
Kevin Marshall | 858c03f25 | 2018-04-12 19:24:06 | [diff] [blame] | 104 | } |
| 105 | |
Helmut Januschka | 1dce9dc | 2024-06-11 13:05:35 | [diff] [blame] | 106 | std::string GetLoadableModuleName(std::string_view name) { |
Kevin Marshall | 858c03f25 | 2018-04-12 19:24:06 | [diff] [blame] | 107 | return GetNativeLibraryName(name); |
| 108 | } |
| 109 | |
| 110 | } // namespace base |