Commit 858c03f2 authored by Kevin Marshall's avatar Kevin Marshall Committed by Commit Bot

Fuchsia: Add a Fuchsia-specific NativeLibrary impl library.

native_library_fuchsia uses the Fuchsia-specific dlopen_vmo() function
instead of dlopen() as on other POSIX builds.

The library is package-aware, resolving full library paths differently
if the process is running inside a packaged namespace.

Bug: 798847
Change-Id: I1f4e0769e8b94bff65704df4dcfd73c622df13bc
Reviewed-on: https://chromium-review.googlesource.com/1003060Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarWez <wez@chromium.org>
Reviewed-by: default avatarScott Graham <scottmg@chromium.org>
Commit-Queue: Kevin Marshall <kmarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#550302}
parent 18effeae
...@@ -1301,6 +1301,7 @@ jumbo_component("base") { ...@@ -1301,6 +1301,7 @@ jumbo_component("base") {
"debug/stack_trace_posix.cc", "debug/stack_trace_posix.cc",
"message_loop/message_pump_libevent.cc", "message_loop/message_pump_libevent.cc",
"message_loop/message_pump_libevent.h", "message_loop/message_pump_libevent.h",
"native_library_posix.cc",
"posix/unix_domain_socket.cc", "posix/unix_domain_socket.cc",
"posix/unix_domain_socket.h", "posix/unix_domain_socket.h",
"process/kill_posix.cc", "process/kill_posix.cc",
...@@ -1327,6 +1328,7 @@ jumbo_component("base") { ...@@ -1327,6 +1328,7 @@ jumbo_component("base") {
"memory/shared_memory_handle_fuchsia.cc", "memory/shared_memory_handle_fuchsia.cc",
"message_loop/message_pump_fuchsia.cc", "message_loop/message_pump_fuchsia.cc",
"message_loop/message_pump_fuchsia.h", "message_loop/message_pump_fuchsia.h",
"native_library_fuchsia.cc",
"process/kill_fuchsia.cc", "process/kill_fuchsia.cc",
"process/launch_fuchsia.cc", "process/launch_fuchsia.cc",
"process/memory_fuchsia.cc", "process/memory_fuchsia.cc",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/native_library.h"
#include <fcntl.h>
#include <fdio/io.h>
#include <stdio.h>
#include <zircon/dlfcn.h>
#include <zircon/status.h>
#include <zircon/syscalls.h>
#include "base/base_paths_fuchsia.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/fuchsia/fuchsia_logging.h"
#include "base/fuchsia/scoped_zx_handle.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/posix/safe_strerror.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
namespace base {
std::string NativeLibraryLoadError::ToString() const {
return message;
}
NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
const NativeLibraryOptions& options,
NativeLibraryLoadError* error) {
std::vector<base::FilePath::StringType> components;
library_path.GetComponents(&components);
if (components.size() != 1u) {
NOTREACHED() << "library_path is a path, should be a filename: "
<< library_path.MaybeAsASCII();
return nullptr;
}
// Fuchsia libraries must live under the "lib" directory, which may be located
// in /system/lib or /pkg/lib depending on whether the executable is running
// inside a package.
// TODO(https://crbug.com/805057): Remove the non-package codepath when bootfs
// is deprecated.
FilePath computed_path = base::GetPackageRoot();
if (computed_path.empty()) {
CHECK(PathService::Get(DIR_EXE, &computed_path));
}
computed_path = computed_path.AppendASCII("lib").Append(components[0]);
base::File library(computed_path,
base::File::FLAG_OPEN | base::File::FLAG_READ);
if (!library.IsValid()) {
if (error) {
error->message = base::StringPrintf(
"open library: %s",
base::File::ErrorToString(library.error_details()).c_str());
}
return nullptr;
}
base::ScopedZxHandle vmo;
zx_status_t status =
fdio_get_vmo_clone(library.GetPlatformFile(), vmo.receive());
if (status != ZX_OK) {
if (error) {
error->message = base::StringPrintf("fdio_get_vmo_clone: %s",
zx_status_get_string(status));
}
return nullptr;
}
NativeLibrary result = dlopen_vmo(vmo.get(), RTLD_LAZY | RTLD_LOCAL);
return result;
}
void UnloadNativeLibrary(NativeLibrary library) {
// dlclose() is a no-op on Fuchsia, so do nothing here.
}
void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
StringPiece name) {
return dlsym(library, name.data());
}
std::string GetNativeLibraryName(StringPiece name) {
return base::StringPrintf("lib%s.so", name.as_string().c_str());
}
std::string GetLoadableModuleName(StringPiece name) {
return GetNativeLibraryName(name);
}
} // namespace base
...@@ -77,7 +77,12 @@ class TestLibrary { ...@@ -77,7 +77,12 @@ class TestLibrary {
explicit TestLibrary(const NativeLibraryOptions& options) explicit TestLibrary(const NativeLibraryOptions& options)
: library_(nullptr) { : library_(nullptr) {
base::FilePath exe_path; base::FilePath exe_path;
#if !defined(OS_FUCHSIA)
// Libraries do not sit alongside the executable in Fuchsia. NativeLibrary
// is aware of this and is able to resolve library paths correctly.
CHECK(base::PathService::Get(base::DIR_EXE, &exe_path)); CHECK(base::PathService::Get(base::DIR_EXE, &exe_path));
#endif
library_ = LoadNativeLibraryWithOptions( library_ = LoadNativeLibraryWithOptions(
exe_path.AppendASCII(kTestLibraryName), options, nullptr); exe_path.AppendASCII(kTestLibraryName), options, nullptr);
......
...@@ -18,8 +18,6 @@ ...@@ -18,8 +18,6 @@
-FileProxyTest.SetTimes -FileProxyTest.SetTimes
-FileUtilProxyTest.Touch -FileUtilProxyTest.Touch
-FileUtilTest.FileToFILE -FileUtilTest.FileToFILE
-NativeLibraryTest.LoadLibrary
-NativeLibraryTest.LoadLibraryPreferOwnSymbols
-PlatformThreadTest.ThreadPriorityCurrentThread -PlatformThreadTest.ThreadPriorityCurrentThread
-ProcessMemoryDumpTest.CountResidentBytes -ProcessMemoryDumpTest.CountResidentBytes
-ProcessMemoryDumpTest.CountResidentBytesInSharedMemory -ProcessMemoryDumpTest.CountResidentBytesInSharedMemory
...@@ -28,6 +26,11 @@ ...@@ -28,6 +26,11 @@
-SysInfoTest.AmountOfMem -SysInfoTest.AmountOfMem
-SysInfoTest.AmountOfTotalDiskSpace -SysInfoTest.AmountOfTotalDiskSpace
# Remove this filter once the migration to packages is complete.
# See crbug.com/805057.
-NativeLibraryTest.LoadLibrary
-NativeLibraryTest.LoadLibraryPreferOwnSymbols
# These tests are affected by an issue with cloning namespace entries from # These tests are affected by an issue with cloning namespace entries from
# inside a package. See https://crbug.com/826018 # inside a package. See https://crbug.com/826018
-ProcessUtilTest.CloneAlternateDir -ProcessUtilTest.CloneAlternateDir
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment