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 "base/base_paths.h"
18 #include "base/files/file.h"
19 #include "base/files/file_path.h"
20 #include "base/fuchsia/fuchsia_logging.h"
21 #include "base/notreached.h"
22 #include "base/path_service.h"
23 #include "base/posix/safe_strerror.h"
24 #include "base/strings/strcat.h"
25 #include "base/strings/string_piece.h"
26 #include "base/strings/stringprintf.h"
27 #include "base/strings/utf_string_conversions.h"
28 #include "base/threading/thread_restrictions.h"
29 #include "base_paths.h"
30
31 namespace base {
32
ToString() const33 std::string NativeLibraryLoadError::ToString() const {
34 return message;
35 }
36
LoadNativeLibraryWithOptions(const FilePath & library_path,const NativeLibraryOptions & options,NativeLibraryLoadError * error)37 NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
38 const NativeLibraryOptions& options,
39 NativeLibraryLoadError* error) {
40 FilePath computed_path;
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 }
59
60 // Use fdio_open_fd (a Fuchsia-specific API) here so we can pass the
61 // appropriate FS rights flags to request executability.
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.
64 base::ScopedFD fd;
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) {
71 if (error) {
72 error->message =
73 base::StringPrintf("fdio_open_fd: %s", zx_status_get_string(status));
74 }
75 return nullptr;
76 }
77
78 zx::vmo vmo;
79 status = fdio_get_vmo_exec(fd.get(), vmo.reset_and_get_address());
80 if (status != ZX_OK) {
81 if (error) {
82 error->message = base::StringPrintf("fdio_get_vmo_exec: %s",
83 zx_status_get_string(status));
84 }
85 return nullptr;
86 }
87
88 NativeLibrary result = dlopen_vmo(vmo.get(), RTLD_LAZY | RTLD_LOCAL);
89 return result;
90 }
91
UnloadNativeLibrary(NativeLibrary library)92 void UnloadNativeLibrary(NativeLibrary library) {
93 // dlclose() is a no-op on Fuchsia, so do nothing here.
94 }
95
GetFunctionPointerFromNativeLibrary(NativeLibrary library,StringPiece name)96 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
97 StringPiece name) {
98 return dlsym(library, name.data());
99 }
100
GetNativeLibraryName(StringPiece name)101 std::string GetNativeLibraryName(StringPiece name) {
102 return StrCat({"lib", name, ".so"});
103 }
104
GetLoadableModuleName(StringPiece name)105 std::string GetLoadableModuleName(StringPiece name) {
106 return GetNativeLibraryName(name);
107 }
108
109 } // namespace base
110