1 // Copyright 2018 The Chromium Authors
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>
8 #include <fuchsia/io/cpp/fidl.h>
9 #include <lib/fdio/directory.h>
10 #include <lib/fdio/io.h>
11 #include <lib/zx/vmo.h>
12 #include <stdio.h>
13 #include <zircon/dlfcn.h>
14 #include <zircon/status.h>
15 #include <zircon/syscalls.h>
16
17 #include <string_view>
18
19 #include "base/base_paths.h"
20 #include "base/files/file.h"
21 #include "base/files/file_path.h"
22 #include "base/fuchsia/fuchsia_logging.h"
23 #include "base/notreached.h"
24 #include "base/path_service.h"
25 #include "base/posix/safe_strerror.h"
26 #include "base/strings/strcat.h"
27 #include "base/strings/stringprintf.h"
28 #include "base/strings/utf_string_conversions.h"
29 #include "base/threading/thread_restrictions.h"
30 #include "base_paths.h"
31
32 namespace base {
33
ToString() const34 std::string NativeLibraryLoadError::ToString() const {
35 return message;
36 }
37
LoadNativeLibraryWithOptions(const FilePath & library_path,const NativeLibraryOptions & options,NativeLibraryLoadError * error)38 NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
39 const NativeLibraryOptions& options,
40 NativeLibraryLoadError* error) {
41 FilePath computed_path;
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 }
60
61 // Use fdio_open_fd (a Fuchsia-specific API) here so we can pass the
62 // appropriate FS rights flags to request executability.
63 // TODO(crbug.com/40655456): Teach base::File about FLAG_WIN_EXECUTE on
64 // Fuchsia, and then use it here instead of using fdio_open_fd() directly.
65 base::ScopedFD fd;
66 zx_status_t status = fdio_open_fd(
67 computed_path.value().c_str(),
68 static_cast<uint32_t>(fuchsia::io::OpenFlags::RIGHT_READABLE |
69 fuchsia::io::OpenFlags::RIGHT_EXECUTABLE),
70 base::ScopedFD::Receiver(fd).get());
71 if (status != ZX_OK) {
72 if (error) {
73 error->message =
74 base::StringPrintf("fdio_open_fd: %s", zx_status_get_string(status));
75 }
76 return nullptr;
77 }
78
79 zx::vmo vmo;
80 status = fdio_get_vmo_exec(fd.get(), vmo.reset_and_get_address());
81 if (status != ZX_OK) {
82 if (error) {
83 error->message = base::StringPrintf("fdio_get_vmo_exec: %s",
84 zx_status_get_string(status));
85 }
86 return nullptr;
87 }
88
89 NativeLibrary result = dlopen_vmo(vmo.get(), RTLD_LAZY | RTLD_LOCAL);
90 return result;
91 }
92
UnloadNativeLibrary(NativeLibrary library)93 void UnloadNativeLibrary(NativeLibrary library) {
94 // dlclose() is a no-op on Fuchsia, so do nothing here.
95 }
96
GetFunctionPointerFromNativeLibrary(NativeLibrary library,const char * name)97 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
98 const char* name) {
99 return dlsym(library, name);
100 }
101
GetNativeLibraryName(std::string_view name)102 std::string GetNativeLibraryName(std::string_view name) {
103 return StrCat({"lib", name, ".so"});
104 }
105
GetLoadableModuleName(std::string_view name)106 std::string GetLoadableModuleName(std::string_view name) {
107 return GetNativeLibraryName(name);
108 }
109
110 } // namespace base
111