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