Upload upstream chromium 94.0.4606.31
[platform/framework/web/chromium-efl.git] / base / native_library_fuchsia.cc
1 // Copyright 2018 The Chromium Authors. All rights reserved.
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_fuchsia.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
30 namespace base {
31
32 std::string NativeLibraryLoadError::ToString() const {
33   return message;
34 }
35
36 NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
37                                            const NativeLibraryOptions& options,
38                                            NativeLibraryLoadError* error) {
39   std::vector<base::FilePath::StringType> components;
40   library_path.GetComponents(&components);
41   if (components.size() != 1u) {
42     NOTREACHED() << "library_path is a path, should be a filename: "
43                  << library_path.MaybeAsASCII();
44     return nullptr;
45   }
46
47   FilePath computed_path;
48   base::PathService::Get(DIR_SOURCE_ROOT, &computed_path);
49   computed_path = computed_path.AppendASCII("lib").Append(components[0]);
50
51   // Use fdio_open_fd (a Fuchsia-specific API) here so we can pass the
52   // appropriate FS rights flags to request executability.
53   // TODO(1018538): Teach base::File about FLAG_EXECUTE on Fuchsia, and then
54   // use it here instead of using fdio_open_fd() directly.
55   base::ScopedFD fd;
56   zx_status_t status = fdio_open_fd(
57       computed_path.value().c_str(),
58       fuchsia::io::OPEN_RIGHT_READABLE | fuchsia::io::OPEN_RIGHT_EXECUTABLE,
59       base::ScopedFD::Receiver(fd).get());
60   if (status != ZX_OK) {
61     if (error) {
62       error->message =
63           base::StringPrintf("fdio_open_fd: %s", zx_status_get_string(status));
64     }
65     return nullptr;
66   }
67
68   zx::vmo vmo;
69   status = fdio_get_vmo_exec(fd.get(), vmo.reset_and_get_address());
70   if (status != ZX_OK) {
71     if (error) {
72       error->message = base::StringPrintf("fdio_get_vmo_exec: %s",
73                                           zx_status_get_string(status));
74     }
75     return nullptr;
76   }
77
78   NativeLibrary result = dlopen_vmo(vmo.get(), RTLD_LAZY | RTLD_LOCAL);
79   return result;
80 }
81
82 void UnloadNativeLibrary(NativeLibrary library) {
83   // dlclose() is a no-op on Fuchsia, so do nothing here.
84 }
85
86 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
87                                           StringPiece name) {
88   return dlsym(library, name.data());
89 }
90
91 std::string GetNativeLibraryName(StringPiece name) {
92   return StrCat({"lib", name, ".so"});
93 }
94
95 std::string GetLoadableModuleName(StringPiece name) {
96   return GetNativeLibraryName(name);
97 }
98
99 }  // namespace base