crashpad/util/numeric/checked_range_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

306 lines
8.9 KiB
C++

// Copyright 2014 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 "util/numeric/checked_range.h"
#include <stdint.h>
#include <sys/types.h>
#include <limits>
#include "base/format_macros.h"
#include "base/stl_util.h"
#include "base/strings/stringprintf.h"
#include "gtest/gtest.h"
namespace crashpad {
namespace test {
namespace {
TEST(CheckedRange, IsValid) {
static constexpr struct {
uint32_t base;
uint32_t size;
bool valid;
} kUnsignedTestData[] = {
{0, 0, true},
{0, 1, true},
{0, 2, true},
{0, 0x7fffffff, true},
{0, 0x80000000, true},
{0, 0xfffffffe, true},
{0, 0xffffffff, true},
{1, 0, true},
{1, 1, true},
{1, 2, true},
{1, 0x7fffffff, true},
{1, 0x80000000, true},
{1, 0xfffffffe, true},
{1, 0xffffffff, false},
{0x7fffffff, 0, true},
{0x7fffffff, 1, true},
{0x7fffffff, 2, true},
{0x7fffffff, 0x7fffffff, true},
{0x7fffffff, 0x80000000, true},
{0x7fffffff, 0xfffffffe, false},
{0x7fffffff, 0xffffffff, false},
{0x80000000, 0, true},
{0x80000000, 1, true},
{0x80000000, 2, true},
{0x80000000, 0x7fffffff, true},
{0x80000000, 0x80000000, false},
{0x80000000, 0xfffffffe, false},
{0x80000000, 0xffffffff, false},
{0xfffffffe, 0, true},
{0xfffffffe, 1, true},
{0xfffffffe, 2, false},
{0xfffffffe, 0x7fffffff, false},
{0xfffffffe, 0x80000000, false},
{0xfffffffe, 0xfffffffe, false},
{0xfffffffe, 0xffffffff, false},
{0xffffffff, 0, true},
{0xffffffff, 1, false},
{0xffffffff, 2, false},
{0xffffffff, 0x7fffffff, false},
{0xffffffff, 0x80000000, false},
{0xffffffff, 0xfffffffe, false},
{0xffffffff, 0xffffffff, false},
};
for (size_t index = 0; index < base::size(kUnsignedTestData); ++index) {
const auto& testcase = kUnsignedTestData[index];
SCOPED_TRACE(base::StringPrintf("unsigned index %" PRIuS
", base 0x%x, size 0x%x",
index,
testcase.base,
testcase.size));
CheckedRange<uint32_t> range(testcase.base, testcase.size);
EXPECT_EQ(range.IsValid(), testcase.valid);
}
const int32_t kMinInt32 = std::numeric_limits<int32_t>::min();
static constexpr struct {
int32_t base;
uint32_t size;
bool valid;
} kSignedTestData[] = {
{0, 0, true},
{0, 1, true},
{0, 2, true},
{0, 0x7fffffff, true},
{0, 0x80000000, false},
{0, 0xfffffffe, false},
{0, 0xffffffff, false},
{1, 0, true},
{1, 1, true},
{1, 2, true},
{1, 0x7fffffff, false},
{1, 0x80000000, false},
{1, 0xfffffffe, false},
{1, 0xffffffff, false},
{0x7fffffff, 0, true},
{0x7fffffff, 1, false},
{0x7fffffff, 2, false},
{0x7fffffff, 0x7fffffff, false},
{0x7fffffff, 0x80000000, false},
{0x7fffffff, 0xfffffffe, false},
{0x7fffffff, 0xffffffff, false},
{kMinInt32, 0, true},
{kMinInt32, 1, true},
{kMinInt32, 2, true},
{kMinInt32, 0x7fffffff, true},
{kMinInt32, 0x80000000, false},
{kMinInt32, 0xfffffffe, false},
{kMinInt32, 0xffffffff, false},
{-2, 0, true},
{-2, 1, true},
{-2, 2, true},
{-2, 0x7fffffff, true},
{-2, 0x80000000, false},
{-2, 0xfffffffe, false},
{-2, 0xffffffff, false},
{-1, 0, true},
{-1, 1, true},
{-1, 2, true},
{-1, 0x7fffffff, true},
{-1, 0x80000000, false},
{-1, 0xfffffffe, false},
{-1, 0xffffffff, false},
};
for (size_t index = 0; index < base::size(kSignedTestData); ++index) {
const auto& testcase = kSignedTestData[index];
SCOPED_TRACE(base::StringPrintf("signed index %" PRIuS
", base 0x%x, size 0x%x",
index,
testcase.base,
testcase.size));
CheckedRange<int32_t, uint32_t> range(testcase.base, testcase.size);
EXPECT_EQ(range.IsValid(), testcase.valid);
}
}
TEST(CheckedRange, ContainsValue) {
static constexpr struct {
uint32_t value;
bool contains;
} kTestData[] = {
{0, false},
{1, false},
{0x1fff, false},
{0x2000, true},
{0x2001, true},
{0x2ffe, true},
{0x2fff, true},
{0x3000, false},
{0x3001, false},
{0x7fffffff, false},
{0x80000000, false},
{0x80000001, false},
{0x80001fff, false},
{0x80002000, false},
{0x80002001, false},
{0x80002ffe, false},
{0x80002fff, false},
{0x80003000, false},
{0x80003001, false},
{0xffffcfff, false},
{0xffffdfff, false},
{0xffffefff, false},
{0xffffffff, false},
};
CheckedRange<uint32_t> parent_range(0x2000, 0x1000);
ASSERT_TRUE(parent_range.IsValid());
for (size_t index = 0; index < base::size(kTestData); ++index) {
const auto& testcase = kTestData[index];
SCOPED_TRACE(base::StringPrintf(
"index %" PRIuS ", value 0x%x", index, testcase.value));
EXPECT_EQ(parent_range.ContainsValue(testcase.value), testcase.contains);
}
}
TEST(CheckedRange, ContainsRange) {
static constexpr struct {
uint32_t base;
uint32_t size;
bool contains;
} kTestData[] = {
{0, 0, false},
{0, 1, false},
{0x2000, 0x1000, true},
{0, 0x2000, false},
{0x3000, 0x1000, false},
{0x1800, 0x1000, false},
{0x2800, 0x1000, false},
{0x2000, 0x800, true},
{0x2800, 0x800, true},
{0x2400, 0x800, true},
{0x2800, 0, true},
{0x2000, 0xffffdfff, false},
{0x2800, 0xffffd7ff, false},
{0x3000, 0xffffcfff, false},
{0xfffffffe, 1, false},
{0xffffffff, 0, false},
{0x1fff, 0, false},
{0x2000, 0, true},
{0x2001, 0, true},
{0x2fff, 0, true},
{0x3000, 0, true},
{0x3001, 0, false},
{0x1fff, 1, false},
{0x2000, 1, true},
{0x2001, 1, true},
{0x2fff, 1, true},
{0x3000, 1, false},
{0x3001, 1, false},
};
CheckedRange<uint32_t> parent_range(0x2000, 0x1000);
ASSERT_TRUE(parent_range.IsValid());
for (size_t index = 0; index < base::size(kTestData); ++index) {
const auto& testcase = kTestData[index];
SCOPED_TRACE(base::StringPrintf("index %" PRIuS ", base 0x%x, size 0x%x",
index,
testcase.base,
testcase.size));
CheckedRange<uint32_t> child_range(testcase.base, testcase.size);
ASSERT_TRUE(child_range.IsValid());
EXPECT_EQ(parent_range.ContainsRange(child_range), testcase.contains);
}
}
TEST(CheckedRange, OverlapsRange) {
static constexpr struct {
uint32_t base;
uint32_t size;
bool overlaps;
} kTestData[] = {
{0, 0, false},
{0, 1, false},
{0x2000, 0x1000, true},
{0, 0x2000, false},
{0x3000, 0x1000, false},
{0x1800, 0x1000, true},
{0x1800, 0x2000, true},
{0x2800, 0x1000, true},
{0x2000, 0x800, true},
{0x2800, 0x800, true},
{0x2400, 0x800, true},
{0x2800, 0, false},
{0x2000, 0xffffdfff, true},
{0x2800, 0xffffd7ff, true},
{0x3000, 0xffffcfff, false},
{0xfffffffe, 1, false},
{0xffffffff, 0, false},
{0x1fff, 0, false},
{0x2000, 0, false},
{0x2001, 0, false},
{0x2fff, 0, false},
{0x3000, 0, false},
{0x3001, 0, false},
{0x1fff, 1, false},
{0x2000, 1, true},
{0x2001, 1, true},
{0x2fff, 1, true},
{0x3000, 1, false},
{0x3001, 1, false},
};
CheckedRange<uint32_t> first_range(0x2000, 0x1000);
ASSERT_TRUE(first_range.IsValid());
for (size_t index = 0; index < base::size(kTestData); ++index) {
const auto& testcase = kTestData[index];
SCOPED_TRACE(base::StringPrintf("index %" PRIuS ", base 0x%x, size 0x%x",
index,
testcase.base,
testcase.size));
CheckedRange<uint32_t> second_range(testcase.base, testcase.size);
ASSERT_TRUE(second_range.IsValid());
EXPECT_EQ(first_range.OverlapsRange(second_range), testcase.overlaps);
}
}
} // namespace
} // namespace test
} // namespace crashpad