crashpad/snapshot/fuchsia/process_snapshot_fuchsia_test.cc
Mark Mentovai cc166d71f4 Use base::size where appropriate, and ArraySize elsewhere
This is a follow-up to c8a016b99d97, following the post-landing
discussion at
https://chromium-review.googlesource.com/c/crashpad/crashpad/+/1393921/5#message-2058541d8c4505d20a990ab7734cd758e437a5f7

base::size, and std::size that will eventually replace it when C++17 is
assured, does not allow the size of non-static data members to be taken
in constant expression context. The remaining uses of ArraySize are in:

minidump/minidump_exception_writer.cc (×1)
minidump/minidump_system_info_writer.cc (×2, also uses base::size)
snapshot/cpu_context.cc (×4, also uses base::size)
util/misc/arraysize_test.cc (×10, of course)

The first of these occurs when initializing a constexpr variable. All
others are in expressions used with static_assert.

Includes:
Update mini_chromium to 737433ebade4d446643c6c07daae02a67e8deccao

f701716d9546 Add Windows ARM64 build target to mini_chromium
87a95a3d6ac2 Remove the arraysize macro
1f7255ead1f7 Placate MSVC in areas of base::size usage
737433ebade4 Add cast

Bug: chromium:837308
Change-Id: I6a5162654461b1bdd9b7b6864d0d71a734bcde19
Reviewed-on: https://chromium-review.googlesource.com/c/1396108
Commit-Queue: Mark Mentovai <mark@chromium.org>
Reviewed-by: Mark Mentovai <mark@chromium.org>
2019-01-04 22:42:57 +00:00

143 lines
4.7 KiB
C++

// Copyright 2018 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "snapshot/fuchsia/memory_map_region_snapshot_fuchsia.h"
#include <dbghelp.h>
#include <zircon/syscalls.h>
#include "base/fuchsia/fuchsia_logging.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "base/strings/stringprintf.h"
#include "gtest/gtest.h"
#include "snapshot/fuchsia/process_snapshot_fuchsia.h"
#include "test/multiprocess_exec.h"
#include "util/fuchsia/scoped_task_suspend.h"
namespace crashpad {
namespace test {
namespace {
constexpr struct {
uint32_t zircon_perm;
size_t pages;
uint32_t minidump_perm;
} kTestMappingPermAndSizes[] = {
// Zircon doesn't currently allow write-only, execute-only, or
// write-execute-only, returning ZX_ERR_INVALID_ARGS on map.
{0, 5, PAGE_NOACCESS},
{ZX_VM_PERM_READ, 6, PAGE_READONLY},
// {ZX_VM_PERM_WRITE, 7, PAGE_WRITECOPY},
// {ZX_VM_PERM_EXECUTE, 8, PAGE_EXECUTE},
{ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, 9, PAGE_READWRITE},
{ZX_VM_PERM_READ | ZX_VM_PERM_EXECUTE, 10, PAGE_EXECUTE_READ},
// {ZX_VM_PERM_WRITE | ZX_VM_PERM_EXECUTE, 11, PAGE_EXECUTE_WRITECOPY},
{ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_PERM_EXECUTE,
12,
PAGE_EXECUTE_READWRITE},
};
CRASHPAD_CHILD_TEST_MAIN(AddressSpaceChildTestMain) {
// Create specifically sized mappings/permissions and write the address in
// our address space to the parent so that the reader can check they're read
// correctly.
for (const auto& t : kTestMappingPermAndSizes) {
zx_handle_t vmo = ZX_HANDLE_INVALID;
const size_t size = t.pages * PAGE_SIZE;
zx_status_t status = zx_vmo_create(size, 0, &vmo);
ZX_CHECK(status == ZX_OK, status) << "zx_vmo_create";
uintptr_t mapping_addr = 0;
status = zx_vmar_map(
zx_vmar_root_self(), t.zircon_perm, 0, vmo, 0, size, &mapping_addr);
ZX_CHECK(status == ZX_OK, status) << "zx_vmar_map";
CheckedWriteFile(StdioFileHandle(StdioStream::kStandardOutput),
&mapping_addr,
sizeof(mapping_addr));
}
CheckedReadFileAtEOF(StdioFileHandle(StdioStream::kStandardInput));
return 0;
}
bool HasSingleMatchingMapping(
const std::vector<const MemoryMapRegionSnapshot*>& memory_map,
uintptr_t address,
size_t size,
uint32_t perm) {
const MemoryMapRegionSnapshot* matching = nullptr;
for (const auto* region : memory_map) {
const MINIDUMP_MEMORY_INFO& mmi = region->AsMinidumpMemoryInfo();
if (mmi.BaseAddress == address) {
if (matching) {
LOG(ERROR) << "multiple mappings matching address";
return false;
}
matching = region;
}
}
if (!matching)
return false;
const MINIDUMP_MEMORY_INFO& matching_mmi = matching->AsMinidumpMemoryInfo();
return matching_mmi.Protect == perm && matching_mmi.RegionSize == size;
}
class AddressSpaceTest : public MultiprocessExec {
public:
AddressSpaceTest() : MultiprocessExec() {
SetChildTestMainFunction("AddressSpaceChildTestMain");
}
~AddressSpaceTest() {}
private:
void MultiprocessParent() override {
uintptr_t test_addresses[base::size(kTestMappingPermAndSizes)];
for (size_t i = 0; i < base::size(test_addresses); ++i) {
ASSERT_TRUE(ReadFileExactly(
ReadPipeHandle(), &test_addresses[i], sizeof(test_addresses[i])));
}
ScopedTaskSuspend suspend(*ChildProcess());
ProcessSnapshotFuchsia process_snapshot;
ASSERT_TRUE(process_snapshot.Initialize(*ChildProcess()));
for (size_t i = 0; i < base::size(test_addresses); ++i) {
const auto& t = kTestMappingPermAndSizes[i];
EXPECT_TRUE(HasSingleMatchingMapping(process_snapshot.MemoryMap(),
test_addresses[i],
t.pages * PAGE_SIZE,
t.minidump_perm))
<< base::StringPrintf(
"index %zu, zircon_perm 0x%x, minidump_perm 0x%x",
i,
t.zircon_perm,
t.minidump_perm);
}
}
DISALLOW_COPY_AND_ASSIGN(AddressSpaceTest);
};
TEST(ProcessSnapshotFuchsiaTest, AddressSpaceMapping) {
AddressSpaceTest test;
test.Run();
}
} // namespace
} // namespace test
} // namespace crashpad