Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1613,7 +1613,12 @@ jumbo_component("base") {
"memory/shared_memory_helper.h",
"strings/string16.cc",
]

if (enable_castanets) {
sources += [
"memory/shared_memory_helper.cc",
"memory/shared_memory_helper.h",
]
}
deps += [
"//base/trace_event/etw_manifest:chrome_events_win",
"//base/win:base_win_buildflags",
Expand Down
16 changes: 16 additions & 0 deletions base/memory/castanets_memory_mapping.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#if defined(_WINDOWS)
#include <aclapi.h>
#include <stddef.h>
#include <stdint.h>
#else
#include <sys/mman.h>
#endif

#include "base/memory/castanets_memory_mapping.h"

Expand Down Expand Up @@ -43,8 +49,14 @@ void* CastanetsMemoryMapping::GetMemory() {

void* CastanetsMemoryMapping::MapForSync(int fd) {
CHECK(fd != -1);
#if defined(OS_WIN)
void* memory =
MapViewOfFile(HANDLE(fd), FILE_MAP_READ | FILE_MAP_WRITE,
static_cast<uint64_t>(0) >> 32, static_cast<DWORD> (0), mapped_size_);
#else
void* memory =
mmap(NULL, mapped_size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
#endif
CHECK(memory);
AddMapping(memory);
return memory;
Expand All @@ -53,7 +65,11 @@ void* CastanetsMemoryMapping::MapForSync(int fd) {
void CastanetsMemoryMapping::UnmapForSync(void* memory) {
CHECK(memory);
RemoveMapping(memory);
#if defined(OS_WIN)
UnmapViewOfFile(memory);
#else
munmap(memory, mapped_size_);
#endif
}

} // namespace base
131 changes: 124 additions & 7 deletions base/memory/shared_memory_helper.cc
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@
#include <sys/mman.h>
#include "third_party/ashmem/ashmem.h"
#endif
#endif // defined(CASTANETS)
#if defined(OS_WIN)
#include <aclapi.h>
#include <stddef.h>
#include <stdint.h>
#include "base/allocator/partition_allocator/page_allocator.h"
#include "base/rand_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/unguessable_token.h"
#endif
#endif // defined(CASTANETS)

namespace base {

#if !defined(OS_WIN)
struct ScopedPathUnlinkerTraits {
static const FilePath* InvalidValue() { return nullptr; }

Expand All @@ -35,8 +45,97 @@ struct ScopedPathUnlinkerTraits {
// Unlinks the FilePath when the object is destroyed.
using ScopedPathUnlinker =
ScopedGeneric<const FilePath*, ScopedPathUnlinkerTraits>;
#endif

#if !defined(OS_ANDROID)
#if defined(OS_WIN)

HANDLE CreateFileMappingWithReducedPermissions(SECURITY_ATTRIBUTES* sa,
size_t rounded_size,
LPCWSTR name) {
HANDLE h = CreateFileMapping(INVALID_HANDLE_VALUE, sa, PAGE_READWRITE, 0,
static_cast<DWORD>(rounded_size), name);
if (!h) {
// LogError(CREATE_FILE_MAPPING_FAILURE, GetLastError());
return nullptr;
}

HANDLE dup_handle;
BOOL success = ::DuplicateHandle(
GetCurrentProcess(), h, GetCurrentProcess(), &dup_handle,
FILE_MAP_READ | FILE_MAP_WRITE | SECTION_QUERY, FALSE, 0);
BOOL rv = ::CloseHandle(h);
DCHECK(rv);

if (!success) {
LOG(ERROR)<<" Failure ";
return nullptr;
}
return dup_handle;
}

bool CreateAnonymousSharedMemory(const SharedMemoryCreateOptions& options,
win::ScopedHandle* handle) {
// TODO(crbug.com/210609): NaCl forces us to round up 64k here, wasting 32k
// per mapping on average.
static const size_t kSectionMask = 65536 - 1;
DCHECK(!options.executable);
if (options.size == 0) {
LOG(ERROR) << " Failure ";
return false;
}

// Check maximum accounting for overflow.
if (options.size >
static_cast<size_t>(std::numeric_limits<int>::max()) - kSectionMask) {
LOG(ERROR) << " Failure ";
return false;
}

size_t rounded_size = (options.size + kSectionMask) & ~kSectionMask;
string16 name_ =
options.name_deprecated ? ASCIIToUTF16(*options.name_deprecated) : L"";
SECURITY_ATTRIBUTES sa = {sizeof(sa), nullptr, FALSE};
SECURITY_DESCRIPTOR sd;
ACL dacl;

if (name_.empty()) {
// Add an empty DACL to enforce anonymous read-only sections.
sa.lpSecurityDescriptor = &sd;
if (!InitializeAcl(&dacl, sizeof(dacl), ACL_REVISION)) {
LOG(ERROR) << " Failure ";
return false;
}
if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)) {
LOG(ERROR) << " Failure ";
return false;
}
if (!SetSecurityDescriptorDacl(&sd, TRUE, &dacl, FALSE)) {
LOG(ERROR) << " Failure ";
return false;
}

// Windows ignores DACLs on certain unnamed objects (like shared sections).
// So, we generate a random name when we need to enforce read-only.
uint64_t rand_values[4];
RandBytes(&rand_values, sizeof(rand_values));
name_ = StringPrintf(L"CrSharedMem_%016llx%016llx%016llx%016llx",
rand_values[0], rand_values[1], rand_values[2],
rand_values[3]);
}
DCHECK(!name_.empty());
HANDLE h =
CreateFileMappingWithReducedPermissions(&sa, rounded_size, name_.c_str());
if (h == nullptr) {
// The error is logged within CreateFileMappingWithReducedPermissions().
LOG(ERROR) << " Failure ";
return false;
}
handle->Set(h);

return true;
}
#else
bool CreateAnonymousSharedMemory(const SharedMemoryCreateOptions& options,
ScopedFD* fd,
ScopedFD* readonly_fd,
Expand Down Expand Up @@ -160,7 +259,7 @@ bool PrepareMapFile(ScopedFD fd,

return true;
}

#endif
#elif defined(OS_ANDROID) && defined(CASTANETS)
bool CreateAnonymousSharedMemory(const SharedMemoryCreateOptions& options,
ScopedFD* fd,
Expand Down Expand Up @@ -199,13 +298,30 @@ subtle::PlatformSharedMemoryRegion CreateAnonymousSharedMemoryIfNeeded(
if (region.IsValid())
return region;

#if defined(OS_WIN)
win::ScopedHandle handle;
if (!CreateAnonymousSharedMemory(option, &handle))
return subtle::PlatformSharedMemoryRegion();

subtle::PlatformSharedMemoryRegion::Mode mode =
subtle::PlatformSharedMemoryRegion::Mode::kUnsafe;
if (option.share_read_only) {
mode = subtle::PlatformSharedMemoryRegion::Mode::kReadOnly;
}

region = subtle::PlatformSharedMemoryRegion::Take(std::move(handle), mode,
option.size, guid);

SharedMemoryTracker::GetInstance()->AddHolder(region.Duplicate());
return region;
#else

ScopedFD new_fd;
ScopedFD readonly_fd;
FilePath path;
VLOG(1) << "Create anonymous shared memory for Castanets" << guid;
if (!CreateAnonymousSharedMemory(option, &new_fd, &readonly_fd, &path))
return subtle::PlatformSharedMemoryRegion();

#if !defined(OS_ANDROID)
struct stat stat;
CHECK(!fstat(new_fd.get(), &stat));
Expand All @@ -223,12 +339,13 @@ subtle::PlatformSharedMemoryRegion CreateAnonymousSharedMemoryIfNeeded(
}

region = subtle::PlatformSharedMemoryRegion::Take(
subtle::ScopedFDPair(std::move(new_fd), std::move(readonly_fd)),
mode, option.size, guid);
subtle::ScopedFDPair(std::move(new_fd), std::move(readonly_fd)), mode,
option.size, guid);

SharedMemoryTracker::GetInstance()->AddHolder(region.Duplicate());
return region;
#endif
}
#endif // defined(CASTANETS)
#endif // defined(CASTANETS)

} // namespace base
9 changes: 7 additions & 2 deletions base/memory/shared_memory_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@

#if defined(CASTANETS)
#include "base/memory/platform_shared_memory_region.h"
#endif // defined(CASTANETS)
#endif // defined(CASTANETS)

#include <fcntl.h>

namespace base {

#if !defined(OS_ANDROID)
#if defined(OS_WIN)
bool CreateAnonymousSharedMemory(const SharedMemoryCreateOptions& options,
win::ScopedHandle* handle);
#else
// Makes a temporary file, fdopens it, and then unlinks it. |fd| is populated
// with the opened fd. |readonly_fd| is populated with the opened fd if
// options.share_read_only is true. |path| is populated with the location of
Expand All @@ -33,6 +37,7 @@ bool PrepareMapFile(ScopedFD fd,
ScopedFD readonly_fd,
int* mapped_file,
int* readonly_mapped_file);
#endif
#elif defined(OS_ANDROID) && defined(CASTANETS)
bool CreateAnonymousSharedMemory(const SharedMemoryCreateOptions& options,
ScopedFD* fd,
Expand All @@ -45,7 +50,7 @@ bool CreateAnonymousSharedMemory(const SharedMemoryCreateOptions& options,
subtle::PlatformSharedMemoryRegion BASE_EXPORT
CreateAnonymousSharedMemoryIfNeeded(const UnguessableToken& guid,
const SharedMemoryCreateOptions& option);
#endif // defined(CASTANETS)
#endif // defined(CASTANETS)

} // namespace base

Expand Down
2 changes: 2 additions & 0 deletions base/process/process_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ typedef DWORD ProcessId;
typedef HANDLE UserTokenHandle;
const ProcessHandle kNullProcessHandle = NULL;
const ProcessId kNullProcessId = 0;
const ProcessHandle kCastanetsProcessHandle = NULL;
const ProcessId kCastanetsProcessId = -1;
#elif defined(OS_FUCHSIA)
typedef zx_handle_t ProcessHandle;
typedef zx_koid_t ProcessId;
Expand Down
2 changes: 2 additions & 0 deletions base/win/scoped_handle_verifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ ScopedHandleVerifier* ScopedHandleVerifier::Get() {

bool CloseHandleWrapper(HANDLE handle) {
if (!::CloseHandle(handle))
#if !defined(CASTANETS)
CHECK(false); // CloseHandle failed.
#endif
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion build/config/compiler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ declare_args() {
# warnings with known toolchains. Allow overriding this e.g. for Chromium
# builds on Linux that could use a different version of the compiler.
# With GCC, warnings in no-Chromium code are always not treated as errors.
treat_warnings_as_errors = true
treat_warnings_as_errors = !is_win

# Normally, Android builds are lightly optimized, even for debug builds, to
# keep binary size down. Setting this flag to true disables such optimization
Expand Down
7 changes: 7 additions & 0 deletions cc/paint/paint_typeface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include "build/build_config.h"
#include "third_party/skia/include/core/SkFontMgr.h"
#include "third_party/skia/include/ports/SkFontConfigInterface.h"
#if defined(OS_WIN) && defined(CASTANETS)
#include "third_party/skia/include/ports/SkTypeface_win.h"
#endif

namespace cc {

Expand Down Expand Up @@ -141,7 +144,11 @@ void PaintTypeface::CreateSkTypeface() {
// This is a fallthrough in all cases in FontCache::CreateTypeface, so
// this is done unconditionally. Since we create the typeface upon
// PaintTypeface creation, this should be safe in all cases.
#if defined(OS_WIN) && defined(CASTANETS)
auto fm(SkFontMgr_New_DirectWrite());
#else
auto fm(SkFontMgr::RefDefault());
#endif
sk_typeface_ = fm->legacyMakeTypeface(family_name_.c_str(), font_style_);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion chrome/renderer/chrome_render_frame_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ void ChromeRenderFrameObserver::UpdateBrowserControlsState(
content::BrowserControlsState constraints,
content::BrowserControlsState current,
bool animate) {
#if defined(OS_LINUX)
#if defined(OS_LINUX) || defined(OS_WIN)
return;
#else
render_frame()->GetRenderView()->UpdateBrowserControlsState(constraints,
Expand Down
13 changes: 6 additions & 7 deletions components/services/font/public/cpp/font_service_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
#include <fcntl.h>
#include <stdint.h>
#include <sys/stat.h>
#if !defined(OS_WIN)
#include <unistd.h>
#endif
#endif

#include "base/bind.h"
#include "base/files/file.h"
Expand Down Expand Up @@ -45,15 +47,12 @@ bool FontServiceThread::MatchFamilyName(
bool out_valid = false;
// This proxies to the other thread, which proxies to mojo. Only on the reply
// from mojo do we return from this.
#if defined(CASTANETS)
#if defined(CASTANETS) && !defined(OS_WIN)
SkFontConfigInterface* fc =
SkFontConfigInterface::GetSingletonDirectInterface();
out_valid =
fc->matchFamilyName(family_name,
requested_style,
out_font_identity,
out_family_name,
out_style);
fc->matchFamilyName(family_name, requested_style, out_font_identity,
out_family_name, out_style);
#else
base::WaitableEvent done_event;
task_runner()->PostTask(
Expand Down Expand Up @@ -127,7 +126,7 @@ scoped_refptr<MappedFontFile> FontServiceThread::OpenStream(
const SkFontConfigInterface::FontIdentity& identity) {
DCHECK_NE(GetThreadId(), base::PlatformThread::CurrentId());

#if defined(CASTANETS)
#if defined(CASTANETS) && !defined(OS_WIN)
int result_fd = open(identity.fString.c_str(), O_RDONLY);
base::File stream_file(result_fd);
#else
Expand Down
Loading