crashpad/util/misc/from_pointer_cast_test.cc
Mark Mentovai 984749479f Introduce FromPointerCast<>(), with defined sign/zero-extension behavior
Some of the new Linux/Android tests were failing in 32-bit code where
pointers were being casted via reinterpret_cast<>() to LinuxVMAddress,
an unsigned 64-bit type. The behavior of such casts is
implementation-defined, and in this case, sign-extension was being used
to convert the 32-bit pointers to 64 bits, resulting in very large
(unsigned) LinuxVMAddress values that could not possibly refer to proper
addresses in a 32-bit process’ address space.

The offending reinterpret_cast<>() conversions have been replaced with
the new FromPointerCast<>(), which is careful to do sign-extension when
converting to a signed type, and zero-extension when converting to an
unsigned type like LinuxVMAddress.

Bug: crashpad:30
Test: crashpad_util_test FromPointerCast*:MemoryMap.*:ProcessMemory.*
Change-Id: I6f1408dc63369a8740ecd6015d657e4407a7c271
Reviewed-on: https://chromium-review.googlesource.com/488264
Reviewed-by: Joshua Peraza <jperaza@chromium.org>
2017-04-27 19:42:25 +00:00

191 lines
8.3 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 2017 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/misc/from_pointer_cast.h"
#include <stdlib.h>
#include <sys/types.h>
#include <limits>
#include "build/build_config.h"
#include "gtest/gtest.h"
namespace crashpad {
namespace test {
namespace {
struct SomeType {};
template <typename T>
class FromPointerCastTest : public testing::Test {};
using FromPointerCastTestTypes = testing::Types<void*,
const void*,
volatile void*,
const volatile void*,
SomeType*,
const SomeType*,
volatile SomeType*,
const volatile SomeType*>;
TYPED_TEST_CASE(FromPointerCastTest, FromPointerCastTestTypes);
TYPED_TEST(FromPointerCastTest, ToSigned) {
EXPECT_EQ(FromPointerCast<int64_t>(nullptr), 0);
EXPECT_EQ(FromPointerCast<int64_t>(reinterpret_cast<TypeParam>(1)), 1);
EXPECT_EQ(FromPointerCast<int64_t>(reinterpret_cast<TypeParam>(-1)), -1);
EXPECT_EQ(FromPointerCast<int64_t>(reinterpret_cast<TypeParam>(
std::numeric_limits<uintptr_t>::max())),
static_cast<intptr_t>(std::numeric_limits<uintptr_t>::max()));
EXPECT_EQ(FromPointerCast<int64_t>(reinterpret_cast<TypeParam>(
std::numeric_limits<intptr_t>::min())),
std::numeric_limits<intptr_t>::min());
EXPECT_EQ(FromPointerCast<int64_t>(reinterpret_cast<TypeParam>(
std::numeric_limits<intptr_t>::max())),
std::numeric_limits<intptr_t>::max());
}
TYPED_TEST(FromPointerCastTest, ToUnsigned) {
EXPECT_EQ(FromPointerCast<uint64_t>(nullptr), 0u);
EXPECT_EQ(FromPointerCast<uint64_t>(reinterpret_cast<TypeParam>(1)), 1u);
EXPECT_EQ(FromPointerCast<uint64_t>(reinterpret_cast<TypeParam>(-1)),
static_cast<uintptr_t>(-1));
EXPECT_EQ(FromPointerCast<uint64_t>(reinterpret_cast<TypeParam>(
std::numeric_limits<uintptr_t>::max())),
std::numeric_limits<uintptr_t>::max());
EXPECT_EQ(FromPointerCast<uint64_t>(reinterpret_cast<TypeParam>(
std::numeric_limits<intptr_t>::min())),
static_cast<uintptr_t>(std::numeric_limits<intptr_t>::min()));
EXPECT_EQ(FromPointerCast<uint64_t>(reinterpret_cast<TypeParam>(
std::numeric_limits<intptr_t>::max())),
static_cast<uintptr_t>(std::numeric_limits<intptr_t>::max()));
}
// MatchCV<YourType, CVQualifiedType>::Type adapts YourType to match the
// const/void qualification of CVQualifiedType.
template <typename Base, typename MatchTo>
struct MatchCV {
private:
using NonCVBase = typename std::remove_cv<Base>::type;
public:
using Type = typename std::conditional<
std::is_const<MatchTo>::value,
typename std::conditional<std::is_volatile<MatchTo>::value,
const volatile NonCVBase,
const NonCVBase>::type,
typename std::conditional<std::is_volatile<MatchTo>::value,
volatile NonCVBase,
NonCVBase>::type>::type;
};
#if defined(COMPILER_MSVC) && _MSC_VER < 1910
// gtest under MSVS 2015 (MSC 19.0) doesnt handle EXPECT_EQ(a, b) when a or b
// is a pointer to a volatile type, because it cant figure out how to print
// them.
template <typename T>
typename std::remove_volatile<typename std::remove_pointer<T>::type>::type*
MaybeRemoveVolatile(const T& value) {
return const_cast<typename std::remove_volatile<
typename std::remove_pointer<T>::type>::type*>(value);
}
#else // COMPILER_MSVC && _MSC_VER < 1910
// This isnt a problem in MSVS 2017 (MSC 19.1) or with other compilers.
template <typename T>
T MaybeRemoveVolatile(const T& value) {
return value;
}
#endif // COMPILER_MSVC && _MSC_VER < 1910
TYPED_TEST(FromPointerCastTest, ToPointer) {
using CVSomeType =
typename MatchCV<SomeType,
typename std::remove_pointer<TypeParam>::type>::Type;
EXPECT_EQ(MaybeRemoveVolatile(FromPointerCast<CVSomeType*>(nullptr)),
MaybeRemoveVolatile(static_cast<CVSomeType*>(nullptr)));
EXPECT_EQ(MaybeRemoveVolatile(
FromPointerCast<CVSomeType*>(reinterpret_cast<TypeParam>(1))),
MaybeRemoveVolatile(reinterpret_cast<CVSomeType*>(1)));
EXPECT_EQ(MaybeRemoveVolatile(
FromPointerCast<CVSomeType*>(reinterpret_cast<TypeParam>(-1))),
MaybeRemoveVolatile(reinterpret_cast<CVSomeType*>(-1)));
EXPECT_EQ(
MaybeRemoveVolatile(FromPointerCast<CVSomeType*>(
reinterpret_cast<TypeParam>(std::numeric_limits<uintptr_t>::max()))),
MaybeRemoveVolatile(reinterpret_cast<CVSomeType*>(
std::numeric_limits<uintptr_t>::max())));
EXPECT_EQ(
MaybeRemoveVolatile(FromPointerCast<CVSomeType*>(
reinterpret_cast<TypeParam>(std::numeric_limits<intptr_t>::min()))),
MaybeRemoveVolatile(
reinterpret_cast<CVSomeType*>(std::numeric_limits<intptr_t>::min())));
EXPECT_EQ(
MaybeRemoveVolatile(FromPointerCast<CVSomeType*>(
reinterpret_cast<TypeParam>(std::numeric_limits<intptr_t>::max()))),
MaybeRemoveVolatile(
reinterpret_cast<CVSomeType*>(std::numeric_limits<intptr_t>::max())));
}
TEST(FromPointerCast, FromFunctionPointer) {
// These casts should work with or without the & in &malloc.
EXPECT_NE(FromPointerCast<int64_t>(malloc), 0);
EXPECT_NE(FromPointerCast<int64_t>(&malloc), 0);
EXPECT_NE(FromPointerCast<uint64_t>(malloc), 0u);
EXPECT_NE(FromPointerCast<uint64_t>(&malloc), 0u);
EXPECT_EQ(FromPointerCast<void*>(malloc), reinterpret_cast<void*>(malloc));
EXPECT_EQ(FromPointerCast<void*>(&malloc), reinterpret_cast<void*>(malloc));
EXPECT_EQ(FromPointerCast<const void*>(malloc),
reinterpret_cast<const void*>(malloc));
EXPECT_EQ(FromPointerCast<const void*>(&malloc),
reinterpret_cast<const void*>(malloc));
EXPECT_EQ(MaybeRemoveVolatile(FromPointerCast<volatile void*>(malloc)),
MaybeRemoveVolatile(reinterpret_cast<volatile void*>(malloc)));
EXPECT_EQ(MaybeRemoveVolatile(FromPointerCast<volatile void*>(&malloc)),
MaybeRemoveVolatile(reinterpret_cast<volatile void*>(malloc)));
EXPECT_EQ(
MaybeRemoveVolatile(FromPointerCast<const volatile void*>(malloc)),
MaybeRemoveVolatile(reinterpret_cast<const volatile void*>(malloc)));
EXPECT_EQ(
MaybeRemoveVolatile(FromPointerCast<const volatile void*>(&malloc)),
MaybeRemoveVolatile(reinterpret_cast<const volatile void*>(malloc)));
EXPECT_EQ(FromPointerCast<SomeType*>(malloc),
reinterpret_cast<SomeType*>(malloc));
EXPECT_EQ(FromPointerCast<SomeType*>(&malloc),
reinterpret_cast<SomeType*>(malloc));
EXPECT_EQ(FromPointerCast<const SomeType*>(malloc),
reinterpret_cast<const SomeType*>(malloc));
EXPECT_EQ(FromPointerCast<const SomeType*>(&malloc),
reinterpret_cast<const SomeType*>(malloc));
EXPECT_EQ(MaybeRemoveVolatile(FromPointerCast<volatile SomeType*>(malloc)),
MaybeRemoveVolatile(reinterpret_cast<volatile SomeType*>(malloc)));
EXPECT_EQ(MaybeRemoveVolatile(FromPointerCast<volatile SomeType*>(&malloc)),
MaybeRemoveVolatile(reinterpret_cast<volatile SomeType*>(malloc)));
EXPECT_EQ(
MaybeRemoveVolatile(FromPointerCast<const volatile SomeType*>(malloc)),
MaybeRemoveVolatile(reinterpret_cast<const volatile SomeType*>(malloc)));
EXPECT_EQ(
MaybeRemoveVolatile(FromPointerCast<const volatile SomeType*>(&malloc)),
MaybeRemoveVolatile(reinterpret_cast<const volatile SomeType*>(malloc)));
}
} // namespace
} // namespace test
} // namespace crashpad