#include "base/files/memory_mapped_file.h"
#include <fcntl.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "base/threading/scoped_blocking_call.h"
#include "build/build_config.h"
namespace base {
MemoryMappedFile::MemoryMappedFile() = default;
bool MemoryMappedFile::MapFileRegionToMemory(
const MemoryMappedFile::Region& region,
Access access) {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
off_t map_start = 0;
size_t map_size = 0u;
int32_t data_offset = 0;
size_t byte_size = 0u;
if (region == MemoryMappedFile::Region::kWholeFile) {
int64_t file_len = file_.GetLength();
if (file_len < 0) {
DPLOG(ERROR) << "fstat " << file_.GetPlatformFile();
return false;
}
if (!IsValueInRangeForNumericType<size_t>(file_len)) {
return false;
}
map_size = base::checked_cast<size_t>(file_len);
byte_size = map_size;
} else {
int64_t aligned_start = 0;
size_t aligned_size = 0u;
CalculateVMAlignedBoundaries(region.offset, region.size, &aligned_start,
&aligned_size, &data_offset);
if (aligned_start < 0 ||
!IsValueInRangeForNumericType<off_t>(aligned_start)) {
DLOG(ERROR) << "Region bounds are not valid for mmap";
return false;
}
map_start = base::checked_cast<off_t>(aligned_start);
map_size = aligned_size;
byte_size = region.size;
}
if (map_size == 0u) {
return false;
}
int prot = 0;
int flags = MAP_SHARED;
switch (access) {
case READ_ONLY:
prot |= PROT_READ;
break;
case READ_WRITE:
prot |= PROT_READ | PROT_WRITE;
break;
case READ_WRITE_COPY:
prot |= PROT_READ | PROT_WRITE;
flags = MAP_PRIVATE;
break;
case READ_WRITE_EXTEND:
prot |= PROT_READ | PROT_WRITE;
if (!AllocateFileRegion(&file_, region.offset, region.size)) {
return false;
}
break;
}
auto* ptr = static_cast<uint8_t*>(
mmap(nullptr, map_size, prot, flags, file_.GetPlatformFile(), map_start));
if (ptr == MAP_FAILED) {
DPLOG(ERROR) << "mmap " << file_.GetPlatformFile();
return false;
}
bytes_ = UNSAFE_BUFFERS(base::span(ptr + data_offset, byte_size));
return true;
}
void MemoryMappedFile::CloseHandles() {
ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
if (!bytes_.empty()) {
munmap(bytes_.data(), bytes_.size());
}
file_.Close();
bytes_ = base::span<uint8_t>();
}
}