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