Commit 64e40a70 authored by Avi Drissman's avatar Avi Drissman Committed by Commit Bot

Add Rosetta utilities.

New macOS capabilities, more utilities in Chromium.

Bug: 1145672, 1145792, 1142017
Change-Id: Ibd10501429aba40aacb4db7c751385428115941d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2518258
Commit-Queue: Avi Drissman <avi@chromium.org>
Reviewed-by: default avatarMark Mentovai <mark@chromium.org>
Auto-Submit: Avi Drissman <avi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#824329}
parent 2750d7d4
...@@ -1081,6 +1081,8 @@ component("base") { ...@@ -1081,6 +1081,8 @@ component("base") {
"mac/objc_release_properties.mm", "mac/objc_release_properties.mm",
"mac/os_crash_dumps.cc", "mac/os_crash_dumps.cc",
"mac/os_crash_dumps.h", "mac/os_crash_dumps.h",
"mac/rosetta.h",
"mac/rosetta.mm",
"mac/scoped_aedesc.h", "mac/scoped_aedesc.h",
"mac/scoped_authorizationref.h", "mac/scoped_authorizationref.h",
"mac/scoped_authorizationref.mm", "mac/scoped_authorizationref.mm",
......
...@@ -9,8 +9,6 @@ ...@@ -9,8 +9,6 @@
#include <errno.h> #include <errno.h>
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <sys/utsname.h> #include <sys/utsname.h>
#include <sys/xattr.h> #include <sys/xattr.h>
...@@ -19,6 +17,7 @@ ...@@ -19,6 +17,7 @@
#include "base/mac/bundle_locations.h" #include "base/mac/bundle_locations.h"
#include "base/mac/foundation_util.h" #include "base/mac/foundation_util.h"
#include "base/mac/mac_logging.h" #include "base/mac/mac_logging.h"
#include "base/mac/rosetta.h"
#include "base/mac/scoped_cftyperef.h" #include "base/mac/scoped_cftyperef.h"
#include "base/mac/scoped_ioobject.h" #include "base/mac/scoped_ioobject.h"
#include "base/mac/scoped_nsobject.h" #include "base/mac/scoped_nsobject.h"
...@@ -353,19 +352,6 @@ int MacOSVersion() { ...@@ -353,19 +352,6 @@ int MacOSVersion() {
} // namespace internal } // namespace internal
#if defined(ARCH_CPU_X86_64)
namespace {
// https://developer.apple.com/documentation/apple_silicon/about_the_rosetta_translation_environment#3616845
bool ProcessIsTranslated() {
int ret = 0;
size_t size = sizeof(ret);
if (sysctlbyname("sysctl.proc_translated", &ret, &size, nullptr, 0) == -1)
return false;
return ret;
}
} // namespace
#endif // ARCH_CPU_X86_64
CPUType GetCPUType() { CPUType GetCPUType() {
#if defined(ARCH_CPU_ARM64) #if defined(ARCH_CPU_ARM64)
return CPUType::kArm; return CPUType::kArm;
......
// Copyright 2020 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.
#ifndef BASE_MAC_ROSETTA_H_
#define BASE_MAC_ROSETTA_H_
#include <vector>
#include "base/base_export.h"
#include "build/build_config.h"
namespace base {
class FilePath;
namespace mac {
#if defined(ARCH_CPU_X86_64)
// Returns true if the current process is being translated by Rosetta.
bool ProcessIsTranslated();
#endif // ARCH_CPU_X86_64
#if defined(ARCH_CPU_ARM64)
// Returns true if Rosetta is installed and available to translate x86_64 code.
BASE_EXPORT bool IsRosettaInstalled();
#endif // ARCH_CPU_ARM64
// Requests an ahead-of-time translation of the binaries with paths given in
// `binaries`. Returns the success value (true == success, false == failure)
// indicated by the underlying call.
//
// Observed behavior about Rosetta AOT translation:
// - If a binary was already translated, it will not be translated again.
// - The call blocks and waits for the completion of the translation. Do not
// call this on the main thread.
BASE_EXPORT bool RequestRosettaAheadOfTimeTranslation(
const std::vector<FilePath>& binaries);
} // namespace mac
} // namespace base
#endif // BASE_MAC_ROSETTA_H_
// Copyright 2020 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/mac/rosetta.h"
#include <CoreFoundation/CoreFoundation.h>
#include <dlfcn.h>
#include <mach/machine.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include "base/files/file_path.h"
#include "base/mac/sdk_forward_declarations.h"
#include "base/threading/scoped_blocking_call.h"
#include "build/build_config.h"
namespace base {
namespace mac {
#if defined(ARCH_CPU_X86_64)
// https://developer.apple.com/documentation/apple_silicon/about_the_rosetta_translation_environment#3616845
bool ProcessIsTranslated() {
int ret = 0;
size_t size = sizeof(ret);
if (sysctlbyname("sysctl.proc_translated", &ret, &size, nullptr, 0) == -1)
return false;
return ret;
}
#endif // ARCH_CPU_X86_64
#if defined(ARCH_CPU_ARM64)
bool IsRosettaInstalled() {
// Chromium currently requires the 10.15 SDK, but code compiled for Arm must
// be compiled against at least the 11.0 SDK and will run on at least macOS
// 11.0, so this is safe. __builtin_available doesn't work for 11.0 yet;
// https://crbug.com/1115294
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
return CFBundleIsArchitectureLoadable(CPU_TYPE_X86_64);
#pragma clang diagnostic pop
}
#endif // ARCH_CPU_ARM64
bool RequestRosettaAheadOfTimeTranslation(
const std::vector<FilePath>& binaries) {
#if defined(ARCH_CPU_X86_64)
if (!ProcessIsTranslated())
return false;
#endif // ARCH_CPU_X86_64
// It's unclear if this is a BOOL or Boolean or bool. It's returning 0 or 1
// in w0, so just grab the result as an int and convert here.
// int oah_translate_binaries(const char*[] paths, int npaths)
using oah_translate_binaries_t = int (*)(const char*[], int);
static auto oah_translate_binaries = []() {
void* liboah = dlopen("/usr/lib/liboah.dylib", RTLD_LAZY | RTLD_LOCAL);
if (!liboah)
return static_cast<oah_translate_binaries_t>(nullptr);
return reinterpret_cast<oah_translate_binaries_t>(
dlsym(liboah, "oah_translate_binaries"));
}();
if (!oah_translate_binaries)
return false;
std::vector<std::string> paths;
std::vector<const char*> path_strs;
paths.reserve(binaries.size());
path_strs.reserve(paths.size());
for (const auto& binary : binaries) {
paths.push_back(FilePath::GetHFSDecomposedForm(binary.value()));
path_strs.push_back(paths.back().c_str());
}
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);
return oah_translate_binaries(path_strs.data(), path_strs.size());
}
} // namespace mac
} // namespace base
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