blob: 9c58a2f878df7dc6ff8444ee790d4ee3c43db89c [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2018 The Chromium Authors
Kevin Marshall858c03f252018-04-12 19:24:062// 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 Fisher689f4ac2019-10-31 18:37:178#include <fuchsia/io/cpp/fidl.h>
9#include <lib/fdio/directory.h>
Wez5c3c6f152018-06-09 18:24:0210#include <lib/fdio/io.h>
Wez157707d62018-07-10 22:48:4711#include <lib/zx/vmo.h>
Kevin Marshall858c03f252018-04-12 19:24:0612#include <stdio.h>
13#include <zircon/dlfcn.h>
14#include <zircon/status.h>
15#include <zircon/syscalls.h>
16
Helmut Januschka1dce9dc2024-06-11 13:05:3517#include <string_view>
18
David Dorwin2c7fbbf82021-11-06 21:08:5819#include "base/base_paths.h"
Wez4e861052023-07-12 12:18:5720#include "base/files/file.h"
Kevin Marshall858c03f252018-04-12 19:24:0621#include "base/files/file_path.h"
Wez4e861052023-07-12 12:18:5722#include "base/fuchsia/fuchsia_logging.h"
23#include "base/notreached.h"
Kevin Marshall858c03f252018-04-12 19:24:0624#include "base/path_service.h"
Wez4e861052023-07-12 12:18:5725#include "base/posix/safe_strerror.h"
Jan Wilken Dörrie5db50ac2021-02-15 11:43:1626#include "base/strings/strcat.h"
Kevin Marshall858c03f252018-04-12 19:24:0627#include "base/strings/stringprintf.h"
Wez4e861052023-07-12 12:18:5728#include "base/strings/utf_string_conversions.h"
Kevin Marshall858c03f252018-04-12 19:24:0629#include "base/threading/thread_restrictions.h"
David Dorwin636176522022-08-09 22:47:2930#include "base_paths.h"
Kevin Marshall858c03f252018-04-12 19:24:0631
32namespace base {
33
34std::string NativeLibraryLoadError::ToString() const {
35 return message;
36}
37
38NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
39 const NativeLibraryOptions& options,
40 NativeLibraryLoadError* error) {
Kevin Marshallaee8cf22018-09-19 19:24:1841 FilePath computed_path;
Guocheng Wei9b06fa02022-08-23 17:49:5142 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 Fisher689f4ac2019-10-31 18:37:1760
Brandon Castellano853cd2d2024-12-19 21:08:2061 // Use fdio_open3_fd (a Fuchsia-specific API) here so we can pass the
Drew Fisher689f4ac2019-10-31 18:37:1762 // appropriate FS rights flags to request executability.
Alison Gale81f4f2c72024-04-22 19:33:3163 // TODO(crbug.com/40655456): Teach base::File about FLAG_WIN_EXECUTE on
Brandon Castellano853cd2d2024-12-19 21:08:2064 // Fuchsia, and then use it here instead of using fdio_open3_fd() directly.
Drew Fisher689f4ac2019-10-31 18:37:1765 base::ScopedFD fd;
Brandon Castellano853cd2d2024-12-19 21:08:2066 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());
Wez4e861052023-07-12 12:18:5771 if (status != ZX_OK) {
Kevin Marshall858c03f252018-04-12 19:24:0672 if (error) {
Wez4e861052023-07-12 12:18:5773 error->message =
74 base::StringPrintf("fdio_open_fd: %s", zx_status_get_string(status));
Kevin Marshall858c03f252018-04-12 19:24:0675 }
76 return nullptr;
77 }
78
Wez157707d62018-07-10 22:48:4779 zx::vmo vmo;
Wez4e861052023-07-12 12:18:5780 status = fdio_get_vmo_exec(fd.get(), vmo.reset_and_get_address());
81 if (status != ZX_OK) {
Kevin Marshall858c03f252018-04-12 19:24:0682 if (error) {
Drew Fisher689f4ac2019-10-31 18:37:1783 error->message = base::StringPrintf("fdio_get_vmo_exec: %s",
Drew Fisher1247389d2019-04-19 02:03:1884 zx_status_get_string(status));
85 }
86 return nullptr;
87 }
88
Kevin Marshall858c03f252018-04-12 19:24:0689 NativeLibrary result = dlopen_vmo(vmo.get(), RTLD_LAZY | RTLD_LOCAL);
90 return result;
91}
92
93void UnloadNativeLibrary(NativeLibrary library) {
94 // dlclose() is a no-op on Fuchsia, so do nothing here.
95}
96
97void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
David Benjamina9420072023-06-13 14:38:3698 const char* name) {
99 return dlsym(library, name);
Kevin Marshall858c03f252018-04-12 19:24:06100}
101
Helmut Januschka1dce9dc2024-06-11 13:05:35102std::string GetNativeLibraryName(std::string_view name) {
Jan Wilken Dörrie5db50ac2021-02-15 11:43:16103 return StrCat({"lib", name, ".so"});
Kevin Marshall858c03f252018-04-12 19:24:06104}
105
Helmut Januschka1dce9dc2024-06-11 13:05:35106std::string GetLoadableModuleName(std::string_view name) {
Kevin Marshall858c03f252018-04-12 19:24:06107 return GetNativeLibraryName(name);
108}
109
110} // namespace base