mirror of
https://github.com/chromium/crashpad.git
synced 2025-01-14 01:08:01 +08:00
posix: Add ScopedMmap for managing memory-mapped regions
This wraps mmap(), munmap(), and mprotect(). Bug: crashpad:30 Test: crashpad_util_test ScopedMmap.* Change-Id: If14363dfd00e314482cc91e53c7f4e3df737b0d3 Reviewed-on: https://chromium-review.googlesource.com/461361 Reviewed-by: Joshua Peraza <jperaza@chromium.org>
This commit is contained in:
parent
d6837b2b86
commit
96dc950eaf
67
util/posix/scoped_mmap.cc
Normal file
67
util/posix/scoped_mmap.cc
Normal file
@ -0,0 +1,67 @@
|
||||
// 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/posix/scoped_mmap.h"
|
||||
|
||||
#include "base/logging.h"
|
||||
|
||||
namespace crashpad {
|
||||
|
||||
ScopedMmap::ScopedMmap() {}
|
||||
|
||||
ScopedMmap::~ScopedMmap() {
|
||||
Reset();
|
||||
}
|
||||
|
||||
void ScopedMmap::Reset() {
|
||||
ResetAddrLen(MAP_FAILED, 0);
|
||||
}
|
||||
|
||||
void ScopedMmap::ResetAddrLen(void* addr, size_t len) {
|
||||
if (is_valid() && munmap(addr_, len_) != 0) {
|
||||
LOG(ERROR) << "munmap";
|
||||
}
|
||||
|
||||
addr_ = addr;
|
||||
len_ = len;
|
||||
}
|
||||
|
||||
bool ScopedMmap::ResetMmap(void* addr,
|
||||
size_t len,
|
||||
int prot,
|
||||
int flags,
|
||||
int fd,
|
||||
off_t offset) {
|
||||
Reset();
|
||||
|
||||
void* new_addr = mmap(addr, len, prot, flags, fd, offset);
|
||||
if (new_addr == MAP_FAILED) {
|
||||
LOG(ERROR) << "mmap";
|
||||
return false;
|
||||
}
|
||||
|
||||
ResetAddrLen(new_addr, len);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ScopedMmap::Mprotect(int prot) {
|
||||
if (mprotect(addr_, len_, prot) < 0) {
|
||||
LOG(ERROR) << "mprotect";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace crashpad
|
100
util/posix/scoped_mmap.h
Normal file
100
util/posix/scoped_mmap.h
Normal file
@ -0,0 +1,100 @@
|
||||
// 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.
|
||||
|
||||
#ifndef CRASHPAD_UTIL_POSIX_SCOPED_MMAP_H_
|
||||
#define CRASHPAD_UTIL_POSIX_SCOPED_MMAP_H_
|
||||
|
||||
#include "base/macros.h"
|
||||
|
||||
#include <sys/mman.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
namespace crashpad {
|
||||
|
||||
//! \brief Maintains a memory-mapped region created by `mmap()`.
|
||||
//!
|
||||
//! On destruction, any memory-mapped region managed by an object of this class
|
||||
//! will be released by calling `munmap()`.
|
||||
class ScopedMmap {
|
||||
public:
|
||||
ScopedMmap();
|
||||
~ScopedMmap();
|
||||
|
||||
//! \brief Releases the memory-mapped region by calling `munmap()`.
|
||||
//!
|
||||
//! A message will be logged on failure.
|
||||
void Reset();
|
||||
|
||||
//! \brief Releases any existing memory-mapped region and sets the object to
|
||||
//! maintain an already-established mapping.
|
||||
//!
|
||||
//! \param[in] addr The base address of the existing memory-mapped region to
|
||||
//! maintain.
|
||||
//! \param[in] len The size of the existing memory-mapped region to maintain.
|
||||
//!
|
||||
//! A message will be logged on failure to release any existing memory-mapped
|
||||
//! region, but the new mapping will be set regardless.
|
||||
void ResetAddrLen(void* addr, size_t len);
|
||||
|
||||
//! \brief Releases any existing memory-mapped region and establishes a new
|
||||
//! one by calling `mmap()`.
|
||||
//!
|
||||
//! The parameters to this method are passed directly to `mmap()`.
|
||||
//!
|
||||
//! \return `true` on success. `false` on failure, with a message logged. A
|
||||
//! message will also be logged on failure to release any existing
|
||||
//! memory-mapped region, but this will not preclude `mmap()` from being
|
||||
//! called or a new mapping from being established, and if such a call to
|
||||
//! `mmap()` is successful, this method will return `true`.
|
||||
bool ResetMmap(void* addr,
|
||||
size_t len,
|
||||
int prot,
|
||||
int flags,
|
||||
int fd,
|
||||
off_t offset);
|
||||
|
||||
//! \brief Sets the protection of the memory-mapped region by calling
|
||||
//! `mprotect()`.
|
||||
//!
|
||||
//! \a prot is passed directly to `mprotect()`.
|
||||
//!
|
||||
//! \return `true` on success. `false` on failure, with a message logged.
|
||||
bool Mprotect(int prot);
|
||||
|
||||
//! \return Whether this object is managing a valid memory-mapped region.
|
||||
bool is_valid() const { return addr_ != MAP_FAILED; }
|
||||
|
||||
//! \brief Returns the base address of the memory-mapped region.
|
||||
void* addr() const { return addr_; }
|
||||
|
||||
//! \brief Returns the base address of the memory-mapped region, casted to
|
||||
//! a type of the caller’s choosing.
|
||||
template <typename T>
|
||||
T addr_as() const {
|
||||
return reinterpret_cast<T>(addr_);
|
||||
}
|
||||
|
||||
//! \brief Returns the size of the memory-mapped region.
|
||||
size_t len() const { return len_; }
|
||||
|
||||
private:
|
||||
void* addr_ = MAP_FAILED;
|
||||
size_t len_ = 0;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(ScopedMmap);
|
||||
};
|
||||
|
||||
} // namespace crashpad
|
||||
|
||||
#endif // CRASHPAD_UTIL_POSIX_SCOPED_MMAP_H_
|
175
util/posix/scoped_mmap_test.cc
Normal file
175
util/posix/scoped_mmap_test.cc
Normal file
@ -0,0 +1,175 @@
|
||||
// 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/posix/scoped_mmap.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "base/numerics/safe_conversions.h"
|
||||
#include "base/rand_util.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace crashpad {
|
||||
namespace test {
|
||||
namespace {
|
||||
|
||||
bool ScopedMmapResetMmap(ScopedMmap* mapping, size_t len) {
|
||||
return mapping->ResetMmap(
|
||||
nullptr, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
|
||||
}
|
||||
|
||||
// A weird class. This is used to test that memory-mapped regions are freed
|
||||
// as expected by calling munmap(). This is difficult to test well because once
|
||||
// a region has been unmapped, the address space it formerly occupied becomes
|
||||
// eligible for reuse.
|
||||
//
|
||||
// The strategy taken here is that a 64-bit cookie value is written into a
|
||||
// mapped region by SetUp(). While the mapping is active, Check() should
|
||||
// succeed. After the region is unmapped, calling Check() should fail, either
|
||||
// because the region has been unmapped and the address not reused, the address
|
||||
// has been reused but is protected against reading (unlikely), or because the
|
||||
// address has been reused but the cookie value is no longer present there.
|
||||
class TestCookie {
|
||||
public:
|
||||
// A weird constructor for a weird class. The member variable initialization
|
||||
// assures that Check() won’t crash if called on an object that hasn’t had
|
||||
// SetUp() called on it.
|
||||
explicit TestCookie() : address_(&cookie_), cookie_(0) {}
|
||||
|
||||
~TestCookie() {}
|
||||
|
||||
void SetUp(uint64_t* address) {
|
||||
address_ = address, cookie_ = base::RandUint64();
|
||||
*address_ = cookie_;
|
||||
}
|
||||
|
||||
void Check() {
|
||||
if (*address_ != cookie_) {
|
||||
__builtin_trap();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
uint64_t* address_;
|
||||
uint64_t cookie_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(TestCookie);
|
||||
};
|
||||
|
||||
TEST(ScopedMmap, Mmap) {
|
||||
TestCookie cookie;
|
||||
|
||||
ScopedMmap mapping;
|
||||
EXPECT_FALSE(mapping.is_valid());
|
||||
EXPECT_EQ(MAP_FAILED, mapping.addr());
|
||||
EXPECT_EQ(0u, mapping.len());
|
||||
|
||||
mapping.Reset();
|
||||
EXPECT_FALSE(mapping.is_valid());
|
||||
|
||||
const size_t kPageSize = base::checked_cast<size_t>(getpagesize());
|
||||
ASSERT_TRUE(ScopedMmapResetMmap(&mapping, kPageSize));
|
||||
EXPECT_TRUE(mapping.is_valid());
|
||||
EXPECT_NE(MAP_FAILED, mapping.addr());
|
||||
EXPECT_EQ(kPageSize, mapping.len());
|
||||
|
||||
cookie.SetUp(mapping.addr_as<uint64_t*>());
|
||||
cookie.Check();
|
||||
|
||||
mapping.Reset();
|
||||
EXPECT_FALSE(mapping.is_valid());
|
||||
}
|
||||
|
||||
TEST(ScopedMmapDeathTest, Destructor) {
|
||||
TestCookie cookie;
|
||||
{
|
||||
ScopedMmap mapping;
|
||||
|
||||
const size_t kPageSize = base::checked_cast<size_t>(getpagesize());
|
||||
ASSERT_TRUE(ScopedMmapResetMmap(&mapping, kPageSize));
|
||||
EXPECT_TRUE(mapping.is_valid());
|
||||
EXPECT_NE(MAP_FAILED, mapping.addr());
|
||||
EXPECT_EQ(kPageSize, mapping.len());
|
||||
|
||||
cookie.SetUp(mapping.addr_as<uint64_t*>());
|
||||
}
|
||||
|
||||
EXPECT_DEATH(cookie.Check(), "");
|
||||
}
|
||||
|
||||
TEST(ScopedMmapDeathTest, Reset) {
|
||||
ScopedMmap mapping;
|
||||
|
||||
const size_t kPageSize = base::checked_cast<size_t>(getpagesize());
|
||||
ASSERT_TRUE(ScopedMmapResetMmap(&mapping, kPageSize));
|
||||
EXPECT_TRUE(mapping.is_valid());
|
||||
EXPECT_NE(MAP_FAILED, mapping.addr());
|
||||
EXPECT_EQ(kPageSize, mapping.len());
|
||||
|
||||
TestCookie cookie;
|
||||
cookie.SetUp(mapping.addr_as<uint64_t*>());
|
||||
|
||||
mapping.Reset();
|
||||
|
||||
EXPECT_DEATH(cookie.Check(), "");
|
||||
}
|
||||
|
||||
TEST(ScopedMmapDeathTest, ResetMmap) {
|
||||
ScopedMmap mapping;
|
||||
|
||||
// Calling ScopedMmap::ResetMmap() frees the existing mapping before
|
||||
// establishing the new one, so the new one may wind up at the same address as
|
||||
// the old. In fact, this is likely. Create a two-page mapping and replace it
|
||||
// with a single-page mapping, so that the test can assure that the second
|
||||
// page isn’t mapped after establishing the second mapping.
|
||||
const size_t kPageSize = base::checked_cast<size_t>(getpagesize());
|
||||
ASSERT_TRUE(ScopedMmapResetMmap(&mapping, 2 * kPageSize));
|
||||
EXPECT_TRUE(mapping.is_valid());
|
||||
EXPECT_NE(MAP_FAILED, mapping.addr());
|
||||
EXPECT_EQ(2 * kPageSize, mapping.len());
|
||||
|
||||
TestCookie cookie;
|
||||
cookie.SetUp(
|
||||
reinterpret_cast<uint64_t*>(mapping.addr_as<char*>() + kPageSize));
|
||||
|
||||
ASSERT_TRUE(ScopedMmapResetMmap(&mapping, kPageSize));
|
||||
EXPECT_TRUE(mapping.is_valid());
|
||||
EXPECT_NE(MAP_FAILED, mapping.addr());
|
||||
EXPECT_EQ(kPageSize, mapping.len());
|
||||
|
||||
EXPECT_DEATH(cookie.Check(), "");
|
||||
}
|
||||
|
||||
TEST(ScopedMmapDeathTest, Mprotect) {
|
||||
ScopedMmap mapping;
|
||||
|
||||
const size_t kPageSize = base::checked_cast<size_t>(getpagesize());
|
||||
ASSERT_TRUE(ScopedMmapResetMmap(&mapping, kPageSize));
|
||||
EXPECT_TRUE(mapping.is_valid());
|
||||
EXPECT_NE(MAP_FAILED, mapping.addr());
|
||||
EXPECT_EQ(kPageSize, mapping.len());
|
||||
|
||||
char* addr = mapping.addr_as<char*>();
|
||||
*addr = 0;
|
||||
|
||||
ASSERT_TRUE(mapping.Mprotect(PROT_READ));
|
||||
|
||||
EXPECT_DEATH(*addr = 0, "");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace test
|
||||
} // namespace crashpad
|
@ -17,7 +17,6 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
@ -33,6 +32,7 @@
|
||||
#include "test/errors.h"
|
||||
#include "test/multiprocess.h"
|
||||
#include "test/scoped_temp_dir.h"
|
||||
#include "util/posix/scoped_mmap.h"
|
||||
|
||||
namespace crashpad {
|
||||
namespace test {
|
||||
@ -84,7 +84,7 @@ void CauseSignal(int sig) {
|
||||
}
|
||||
|
||||
case SIGBUS: {
|
||||
char* mapped;
|
||||
ScopedMmap mapped_file;
|
||||
{
|
||||
base::ScopedFD fd;
|
||||
{
|
||||
@ -100,21 +100,17 @@ void CauseSignal(int sig) {
|
||||
_exit(kUnexpectedExitStatus);
|
||||
}
|
||||
|
||||
mapped = reinterpret_cast<char*>(mmap(nullptr,
|
||||
getpagesize(),
|
||||
PROT_READ | PROT_WRITE,
|
||||
MAP_PRIVATE,
|
||||
fd.get(),
|
||||
0));
|
||||
if (mapped == MAP_FAILED) {
|
||||
PLOG(ERROR) << "mmap";
|
||||
if (!mapped_file.ResetMmap(nullptr,
|
||||
getpagesize(),
|
||||
PROT_READ | PROT_WRITE,
|
||||
MAP_PRIVATE,
|
||||
fd.get(),
|
||||
0)) {
|
||||
_exit(kUnexpectedExitStatus);
|
||||
}
|
||||
}
|
||||
if (mapped == MAP_FAILED) {
|
||||
_exit(kUnexpectedExitStatus);
|
||||
}
|
||||
|
||||
*mapped = 0;
|
||||
*mapped_file.addr_as<char*>() = 0;
|
||||
|
||||
_exit(kUnexpectedExitStatus);
|
||||
break;
|
||||
|
@ -139,6 +139,8 @@
|
||||
'posix/process_info.h',
|
||||
'posix/process_info_linux.cc',
|
||||
'posix/process_info_mac.cc',
|
||||
'posix/scoped_mmap.cc',
|
||||
'posix/scoped_mmap.h',
|
||||
'posix/signals.cc',
|
||||
'posix/signals.h',
|
||||
'posix/symbolic_constants_posix.cc',
|
||||
|
@ -76,6 +76,7 @@
|
||||
'numeric/in_range_cast_test.cc',
|
||||
'numeric/int128_test.cc',
|
||||
'posix/process_info_test.cc',
|
||||
'posix/scoped_mmap_test.cc',
|
||||
'posix/signals_test.cc',
|
||||
'posix/symbolic_constants_posix_test.cc',
|
||||
'stdlib/aligned_allocator_test.cc',
|
||||
|
Loading…
x
Reference in New Issue
Block a user