blob: 1379438a934b9c9acebe6cb99b07d46a62cd4bae [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
David Dorwin2c7fbbf82021-11-06 21:08:5817#include "base/base_paths.h"
Wez4e861052023-07-12 12:18:5718#include "base/files/file.h"
Kevin Marshall858c03f252018-04-12 19:24:0619#include "base/files/file_path.h"
Wez4e861052023-07-12 12:18:5720#include "base/fuchsia/fuchsia_logging.h"
21#include "base/notreached.h"
Kevin Marshall858c03f252018-04-12 19:24:0622#include "base/path_service.h"
Wez4e861052023-07-12 12:18:5723#include "base/posix/safe_strerror.h"
Jan Wilken Dörrie5db50ac2021-02-15 11:43:1624#include "base/strings/strcat.h"
Wez4e861052023-07-12 12:18:5725#include "base/strings/string_piece.h"
Kevin Marshall858c03f252018-04-12 19:24:0626#include "base/strings/stringprintf.h"
Wez4e861052023-07-12 12:18:5727#include "base/strings/utf_string_conversions.h"
Kevin Marshall858c03f252018-04-12 19:24:0628#include "base/threading/thread_restrictions.h"
David Dorwin636176522022-08-09 22:47:2929#include "base_paths.h"
Kevin Marshall858c03f252018-04-12 19:24:0630
31namespace base {
32
33std::string NativeLibraryLoadError::ToString() const {
34 return message;
35}
36
37NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
38 const NativeLibraryOptions& options,
39 NativeLibraryLoadError* error) {
Kevin Marshallaee8cf22018-09-19 19:24:1840 FilePath computed_path;
Guocheng Wei9b06fa02022-08-23 17:49:5141 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 Fisher689f4ac2019-10-31 18:37:1759
60 // Use fdio_open_fd (a Fuchsia-specific API) here so we can pass the
61 // appropriate FS rights flags to request executability.
David Dorwin636176522022-08-09 22:47:2962 // 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 Fisher689f4ac2019-10-31 18:37:1764 base::ScopedFD fd;
Wez4e861052023-07-12 12:18:5765 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 Marshall858c03f252018-04-12 19:24:0671 if (error) {
Wez4e861052023-07-12 12:18:5772 error->message =
73 base::StringPrintf("fdio_open_fd: %s", zx_status_get_string(status));
Kevin Marshall858c03f252018-04-12 19:24:0674 }
75 return nullptr;
76 }
77
Wez157707d62018-07-10 22:48:4778 zx::vmo vmo;
Wez4e861052023-07-12 12:18:5779 status = fdio_get_vmo_exec(fd.get(), vmo.reset_and_get_address());
80 if (status != ZX_OK) {
Kevin Marshall858c03f252018-04-12 19:24:0681 if (error) {
Drew Fisher689f4ac2019-10-31 18:37:1782 error->message = base::StringPrintf("fdio_get_vmo_exec: %s",
Drew Fisher1247389d2019-04-19 02:03:1883 zx_status_get_string(status));
84 }
85 return nullptr;
86 }
87
Kevin Marshall858c03f252018-04-12 19:24:0688 NativeLibrary result = dlopen_vmo(vmo.get(), RTLD_LAZY | RTLD_LOCAL);
89 return result;
90}
91
92void UnloadNativeLibrary(NativeLibrary library) {
93 // dlclose() is a no-op on Fuchsia, so do nothing here.
94}
95
96void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
David Benjamina9420072023-06-13 14:38:3697 const char* name) {
98 return dlsym(library, name);
Kevin Marshall858c03f252018-04-12 19:24:0699}
100
101std::string GetNativeLibraryName(StringPiece name) {
Jan Wilken Dörrie5db50ac2021-02-15 11:43:16102 return StrCat({"lib", name, ".so"});
Kevin Marshall858c03f252018-04-12 19:24:06103}
104
105std::string GetLoadableModuleName(StringPiece name) {
106 return GetNativeLibraryName(name);
107}
108
109} // namespace base