Skip to content

Commit

Permalink
Bug 1536697 - Fix error handling in base::SharedMemory::Map. r=froydnj
Browse files Browse the repository at this point in the history
If mmap failed, we'd leave the memory_ member variable set to MAP_FAILED,
but everything else in this file checks for nullptr (and only nullptr) to
test if the pointer is valid.

Also, this removes the debug assertion that the mmap succeeded, to allow
writing unit tests where we expect it to fail (e.g., for insufficient
permissions).

Depends on D26747

Differential Revision: https://phabricator.services.mozilla.com/D26748

--HG--
extra : moz-landing-system : lando
  • Loading branch information
jld committed Jun 20, 2019
1 parent 142fe6a commit 1388ead
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions ipc/chromium/src/base/shared_memory_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -290,29 +290,28 @@ bool SharedMemory::Freeze() {

bool SharedMemory::Map(size_t bytes, void* fixed_address) {
if (mapped_file_ == -1) return false;
DCHECK(!memory_);

// Don't use MAP_FIXED when a fixed_address was specified, since that can
// replace pages that are alread mapped at that address.
memory_ =
void* mem =
mmap(fixed_address, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE),
MAP_SHARED, mapped_file_, 0);

bool mmap_succeeded = memory_ != MAP_FAILED;

DCHECK(mmap_succeeded) << "Call to mmap failed, errno=" << errno;

if (mmap_succeeded) {
if (fixed_address && memory_ != fixed_address) {
bool munmap_succeeded = munmap(memory_, bytes) == 0;
DCHECK(munmap_succeeded) << "Call to munmap failed, errno=" << errno;
memory_ = NULL;
return false;
}
if (mem == MAP_FAILED) {
CHROMIUM_LOG(WARNING) << "Call to mmap failed: " << strerror(errno);
return false;
}

mapped_size_ = bytes;
if (fixed_address && mem != fixed_address) {
bool munmap_succeeded = munmap(mem, bytes) == 0;
DCHECK(munmap_succeeded) << "Call to munmap failed, errno=" << errno;
return false;
}

return mmap_succeeded;
memory_ = mem;
mapped_size_ = bytes;
return true;
}

bool SharedMemory::Unmap() {
Expand Down

0 comments on commit 1388ead

Please sign in to comment.