native_library_fuchsia.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #include "base/native_library.h"
  5. #include <fcntl.h>
  6. #include <fuchsia/io/cpp/fidl.h>
  7. #include <lib/fdio/directory.h>
  8. #include <lib/fdio/io.h>
  9. #include <lib/zx/vmo.h>
  10. #include <stdio.h>
  11. #include <zircon/dlfcn.h>
  12. #include <zircon/status.h>
  13. #include <zircon/syscalls.h>
  14. #include "base/base_paths.h"
  15. #include "base/files/file.h"
  16. #include "base/files/file_path.h"
  17. #include "base/fuchsia/fuchsia_logging.h"
  18. #include "base/notreached.h"
  19. #include "base/path_service.h"
  20. #include "base/posix/safe_strerror.h"
  21. #include "base/strings/strcat.h"
  22. #include "base/strings/string_piece.h"
  23. #include "base/strings/stringprintf.h"
  24. #include "base/strings/utf_string_conversions.h"
  25. #include "base/threading/thread_restrictions.h"
  26. #include "base_paths.h"
  27. namespace base {
  28. std::string NativeLibraryLoadError::ToString() const {
  29. return message;
  30. }
  31. NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
  32. const NativeLibraryOptions& options,
  33. NativeLibraryLoadError* error) {
  34. std::vector<base::FilePath::StringType> components =
  35. library_path.GetComponents();
  36. if (components.size() != 1u) {
  37. NOTREACHED() << "library_path is a path, should be a filename: "
  38. << library_path.MaybeAsASCII();
  39. return nullptr;
  40. }
  41. FilePath computed_path;
  42. base::PathService::Get(DIR_ASSETS, &computed_path);
  43. computed_path = computed_path.AppendASCII("lib").Append(components[0]);
  44. // Use fdio_open_fd (a Fuchsia-specific API) here so we can pass the
  45. // appropriate FS rights flags to request executability.
  46. // TODO(crbug.com/1018538): Teach base::File about FLAG_WIN_EXECUTE on
  47. // Fuchsia, and then use it here instead of using fdio_open_fd() directly.
  48. base::ScopedFD fd;
  49. zx_status_t status = fdio_open_fd(
  50. computed_path.value().c_str(),
  51. static_cast<uint32_t>(fuchsia::io::OpenFlags::RIGHT_READABLE |
  52. fuchsia::io::OpenFlags::RIGHT_EXECUTABLE),
  53. base::ScopedFD::Receiver(fd).get());
  54. if (status != ZX_OK) {
  55. if (error) {
  56. error->message =
  57. base::StringPrintf("fdio_open_fd: %s", zx_status_get_string(status));
  58. }
  59. return nullptr;
  60. }
  61. zx::vmo vmo;
  62. status = fdio_get_vmo_exec(fd.get(), vmo.reset_and_get_address());
  63. if (status != ZX_OK) {
  64. if (error) {
  65. error->message = base::StringPrintf("fdio_get_vmo_exec: %s",
  66. zx_status_get_string(status));
  67. }
  68. return nullptr;
  69. }
  70. NativeLibrary result = dlopen_vmo(vmo.get(), RTLD_LAZY | RTLD_LOCAL);
  71. return result;
  72. }
  73. void UnloadNativeLibrary(NativeLibrary library) {
  74. // dlclose() is a no-op on Fuchsia, so do nothing here.
  75. }
  76. void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
  77. StringPiece name) {
  78. return dlsym(library, name.data());
  79. }
  80. std::string GetNativeLibraryName(StringPiece name) {
  81. return StrCat({"lib", name, ".so"});
  82. }
  83. std::string GetLoadableModuleName(StringPiece name) {
  84. return GetNativeLibraryName(name);
  85. }
  86. } // namespace base