crashpad/snapshot/mac/mach_o_image_segment_reader_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

188 lines
7.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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 "snapshot/mac/mach_o_image_segment_reader.h"
#include <mach-o/loader.h>
#include "base/stl_util.h"
#include "base/strings/stringprintf.h"
#include "gtest/gtest.h"
namespace crashpad {
namespace test {
namespace {
// Most of MachOImageSegmentReader is tested as part of MachOImageReader, which
// depends on MachOImageSegmentReader to provide major portions of its
// functionality. Because MachOImageSegmentReader is difficult to use except by
// a Mach-O load command reader such as MachOImageReader, these portions
// of MachOImageSegmentReader are not tested independently.
//
// The tests here exercise the portions of MachOImageSegmentReader that are
// exposed and independently useful.
TEST(MachOImageSegmentReader, SegmentNameString) {
// The output value should be a string of up to 16 characters, even if the
// input value is not NUL-terminated within 16 characters.
EXPECT_EQ(MachOImageSegmentReader::SegmentNameString("__TEXT"), "__TEXT");
EXPECT_EQ(MachOImageSegmentReader::SegmentNameString("__OVER"), "__OVER");
EXPECT_EQ(MachOImageSegmentReader::SegmentNameString(""), "");
EXPECT_EQ(MachOImageSegmentReader::SegmentNameString("p"), "p");
EXPECT_EQ(MachOImageSegmentReader::SegmentNameString("NoUnderChar"),
"NoUnderChar");
EXPECT_EQ(MachOImageSegmentReader::SegmentNameString("0123456789abcde"),
"0123456789abcde");
EXPECT_EQ(MachOImageSegmentReader::SegmentNameString("0123456789abcdef"),
"0123456789abcdef");
EXPECT_EQ(MachOImageSegmentReader::SegmentNameString("gfedcba9876543210"),
"gfedcba987654321");
EXPECT_EQ(MachOImageSegmentReader::SegmentNameString("hgfedcba9876543210"),
"hgfedcba98765432");
// Segment names defined in <mach-o/loader.h>. All of these should come
// through SegmentNameString() cleanly and without truncation.
static constexpr const char* kSegmentTestData[] = {
SEG_TEXT,
SEG_DATA,
SEG_OBJC,
SEG_ICON,
SEG_LINKEDIT,
SEG_UNIXSTACK,
SEG_IMPORT,
};
for (size_t index = 0; index < base::size(kSegmentTestData); ++index) {
EXPECT_EQ(
MachOImageSegmentReader::SegmentNameString(kSegmentTestData[index]),
kSegmentTestData[index])
<< base::StringPrintf("index %zu", index);
}
}
TEST(MachOImageSegmentReader, SectionNameString) {
// The output value should be a string of up to 16 characters, even if the
// input value is not NUL-terminated within 16 characters.
EXPECT_EQ(MachOImageSegmentReader::SectionNameString("__text"), "__text");
EXPECT_EQ(MachOImageSegmentReader::SectionNameString("__over"), "__over");
EXPECT_EQ(MachOImageSegmentReader::SectionNameString(""), "");
EXPECT_EQ(MachOImageSegmentReader::SectionNameString("p"), "p");
EXPECT_EQ(MachOImageSegmentReader::SectionNameString("NoUnderChar"),
"NoUnderChar");
EXPECT_EQ(MachOImageSegmentReader::SectionNameString("0123456789abcde"),
"0123456789abcde");
EXPECT_EQ(MachOImageSegmentReader::SectionNameString("0123456789abcdef"),
"0123456789abcdef");
EXPECT_EQ(MachOImageSegmentReader::SectionNameString("gfedcba9876543210"),
"gfedcba987654321");
EXPECT_EQ(MachOImageSegmentReader::SectionNameString("hgfedcba9876543210"),
"hgfedcba98765432");
// Section names defined in <mach-o/loader.h>. All of these should come
// through SectionNameString() cleanly and without truncation.
static constexpr const char* kSectionTestData[] = {
SECT_TEXT,
SECT_FVMLIB_INIT0,
SECT_FVMLIB_INIT1,
SECT_DATA,
SECT_BSS,
SECT_COMMON,
SECT_OBJC_SYMBOLS,
SECT_OBJC_MODULES,
SECT_OBJC_STRINGS,
SECT_OBJC_REFS,
SECT_ICON_HEADER,
SECT_ICON_TIFF,
};
for (size_t index = 0; index < base::size(kSectionTestData); ++index) {
EXPECT_EQ(
MachOImageSegmentReader::SectionNameString(kSectionTestData[index]),
kSectionTestData[index])
<< base::StringPrintf("index %zu", index);
}
}
TEST(MachOImageSegmentReader, SegmentAndSectionNameString) {
static constexpr struct {
const char* segment;
const char* section;
const char* output;
} kSegmentAndSectionTestData[] = {
{"segment", "section", "segment,section"},
{"Segment", "Section", "Segment,Section"},
{"SEGMENT", "SECTION", "SEGMENT,SECTION"},
{"__TEXT", "__plain", "__TEXT,__plain"},
{"__TEXT", "poetry", "__TEXT,poetry"},
{"__TEXT", "Prose", "__TEXT,Prose"},
{"__PLAIN", "__text", "__PLAIN,__text"},
{"rich", "__text", "rich,__text"},
{"segment", "", "segment,"},
{"", "section", ",section"},
{"", "", ","},
{"0123456789abcdef", "section", "0123456789abcdef,section"},
{"gfedcba9876543210", "section", "gfedcba987654321,section"},
{"0123456789abcdef", "", "0123456789abcdef,"},
{"gfedcba9876543210", "", "gfedcba987654321,"},
{"segment", "0123456789abcdef", "segment,0123456789abcdef"},
{"segment", "gfedcba9876543210", "segment,gfedcba987654321"},
{"", "0123456789abcdef", ",0123456789abcdef"},
{"", "gfedcba9876543210", ",gfedcba987654321"},
{"0123456789abcdef",
"0123456789abcdef",
"0123456789abcdef,0123456789abcdef"},
{"gfedcba9876543210",
"gfedcba9876543210",
"gfedcba987654321,gfedcba987654321"},
// Sections defined in <mach-o/loader.h>. All of these should come through
// SegmentAndSectionNameString() cleanly and without truncation.
{SEG_TEXT, SECT_TEXT, "__TEXT,__text"},
{SEG_TEXT, SECT_FVMLIB_INIT0, "__TEXT,__fvmlib_init0"},
{SEG_TEXT, SECT_FVMLIB_INIT1, "__TEXT,__fvmlib_init1"},
{SEG_DATA, SECT_DATA, "__DATA,__data"},
{SEG_DATA, SECT_BSS, "__DATA,__bss"},
{SEG_DATA, SECT_COMMON, "__DATA,__common"},
{SEG_OBJC, SECT_OBJC_SYMBOLS, "__OBJC,__symbol_table"},
{SEG_OBJC, SECT_OBJC_MODULES, "__OBJC,__module_info"},
{SEG_OBJC, SECT_OBJC_STRINGS, "__OBJC,__selector_strs"},
{SEG_OBJC, SECT_OBJC_REFS, "__OBJC,__selector_refs"},
{SEG_ICON, SECT_ICON_HEADER, "__ICON,__header"},
{SEG_ICON, SECT_ICON_TIFF, "__ICON,__tiff"},
// These segments dont normally have sections, but the above group tested
// the known segment names for segments that do normally have sections.
// This group does the same for segments that normally dont.
{SEG_LINKEDIT, "", "__LINKEDIT,"},
{SEG_UNIXSTACK, "", "__UNIXSTACK,"},
{SEG_IMPORT, "", "__IMPORT,"},
};
for (size_t index = 0; index < base::size(kSegmentAndSectionTestData);
++index) {
const auto& test = kSegmentAndSectionTestData[index];
EXPECT_EQ(MachOImageSegmentReader::SegmentAndSectionNameString(
test.segment, test.section),
test.output)
<< base::StringPrintf("index %zu, segment %s, section %s",
index,
test.segment,
test.section);
}
}
} // namespace
} // namespace test
} // namespace crashpad