Skip to content

Commit 1d661c4

Browse files
committed
more hacky mmap edits
1 parent ae61575 commit 1d661c4

File tree

3 files changed

+48
-39
lines changed

3 files changed

+48
-39
lines changed

extension/data_loader/mman_windows.cpp

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#include <errno.h>
2323
#include <io.h>
24+
#include <limits>
25+
#include <cstdint>
2426
#include <windows.h>
2527

2628
#ifndef STATUS_SECTION_TOO_BIG
@@ -129,49 +131,42 @@ static DWORD __map_mmap_prot_file(const int prot) {
129131

130132
} // namespace
131133

132-
void* mmap(void* addr, size_t len, int prot, int flags, int fildes, off_t off) {
134+
void* mmap(
135+
void* addr,
136+
size_t len,
137+
int prot,
138+
int flags,
139+
int fildes,
140+
std::uint64_t off) {
133141
HANDLE fm, h;
134-
135142
void* map = MAP_FAILED;
136143

137-
#ifdef _MSC_VER
138-
#pragma warning(push)
139-
#pragma warning(disable : 4293)
140-
#endif
141-
142-
const DWORD dwFileOffsetLow = (sizeof(off_t) <= sizeof(DWORD))
143-
? (DWORD)off
144-
: (DWORD)(off & 0xFFFFFFFFL);
145-
const DWORD dwFileOffsetHigh = (sizeof(off_t) <= sizeof(DWORD))
146-
? (DWORD)0
147-
: (DWORD)((off >> 32) & 0xFFFFFFFFL);
148-
const DWORD protect = __map_mmap_prot_page(prot);
149-
const DWORD desiredAccess = __map_mmap_prot_file(prot);
150-
151-
const off_t maxSize = off + (off_t)len;
152-
153-
const DWORD dwMaxSizeLow = (sizeof(off_t) <= sizeof(DWORD))
154-
? (DWORD)maxSize
155-
: (DWORD)(maxSize & 0xFFFFFFFFL);
156-
const DWORD dwMaxSizeHigh = (sizeof(off_t) <= sizeof(DWORD))
157-
? (DWORD)0
158-
: (DWORD)((maxSize >> 32) & 0xFFFFFFFFL);
159-
160-
#ifdef _MSC_VER
161-
#pragma warning(pop)
162-
#endif
163-
164144
errno = 0;
165145

166146
if (len == 0
167147
/* Unsupported flag combinations */
168148
|| (flags & MAP_FIXED) != 0
169-
/* Usupported protection combinations */
149+
/* Unsupported protection combinations */
170150
|| prot == PROT_EXEC) {
171151
errno = EINVAL;
172152
return MAP_FAILED;
173153
}
174154

155+
if (off > std::numeric_limits<std::uint64_t>::max() - len) {
156+
errno = EINVAL;
157+
return MAP_FAILED;
158+
}
159+
160+
const std::uint64_t maxSize = off + static_cast<std::uint64_t>(len);
161+
162+
const DWORD dwFileOffsetLow = static_cast<DWORD>(off & 0xFFFFFFFFULL);
163+
const DWORD dwFileOffsetHigh = static_cast<DWORD>((off >> 32) & 0xFFFFFFFFULL);
164+
const DWORD protect = __map_mmap_prot_page(prot);
165+
const DWORD desiredAccess = __map_mmap_prot_file(prot);
166+
167+
const DWORD dwMaxSizeLow = static_cast<DWORD>(maxSize & 0xFFFFFFFFULL);
168+
const DWORD dwMaxSizeHigh = static_cast<DWORD>((maxSize >> 32) & 0xFFFFFFFFULL);
169+
175170
h = ((flags & MAP_ANONYMOUS) == 0) ? (HANDLE)_get_osfhandle(fildes)
176171
: INVALID_HANDLE_VALUE;
177172

extension/data_loader/mman_windows.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#endif
3232

3333
#include <sys/types.h>
34+
#include <cstdint>
3435

3536
#ifdef __cplusplus
3637
extern "C" {
@@ -56,7 +57,13 @@ extern "C" {
5657
#define MS_SYNC 2
5758
#define MS_INVALIDATE 4
5859

59-
void* mmap(void* addr, size_t len, int prot, int flags, int fildes, off_t off);
60+
void* mmap(
61+
void* addr,
62+
size_t len,
63+
int prot,
64+
int flags,
65+
int fildes,
66+
std::uint64_t off);
6067
int munmap(void* addr, size_t len);
6168
int mprotect(void* addr, size_t len, int prot);
6269
int msync(void* addr, size_t len, int flags);

extension/data_loader/mmap_data_loader.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <cerrno>
1212
#include <cstring>
1313
#include <limits>
14+
#include <cstdint>
1415

1516
#include <fcntl.h>
1617
#include <sys/stat.h>
@@ -180,12 +181,6 @@ Error MmapDataLoader::validate_input(size_t offset, size_t size) const {
180181
offset,
181182
size,
182183
file_size_);
183-
ET_CHECK_OR_RETURN_ERROR(
184-
// Recommended by a lint warning.
185-
offset <= std::numeric_limits<off_t>::max(),
186-
InvalidArgument,
187-
"Offset %zu too large for off_t",
188-
offset);
189184
return Error::Ok;
190185
}
191186

@@ -220,13 +215,19 @@ Result<FreeableBuffer> MmapDataLoader::load(
220215

221216
// Map the pages read-only. Use shared mappings so that other processes
222217
// can also map the same pages and share the same memory.
218+
#if defined(_WIN32)
219+
const std::uint64_t map_offset = static_cast<std::uint64_t>(range.start);
220+
#else
221+
const off_t map_offset = static_cast<off_t>(range.start);
222+
#endif
223+
223224
void* pages = ::mmap(
224225
nullptr,
225226
map_size,
226227
PROT_READ,
227228
MAP_SHARED,
228229
fd_,
229-
static_cast<off_t>(range.start));
230+
map_offset);
230231
ET_CHECK_OR_RETURN_ERROR(
231232
pages != MAP_FAILED,
232233
AccessFailed,
@@ -328,13 +329,19 @@ Error MmapDataLoader::load_into(
328329
// Map the pages read-only. MAP_PRIVATE vs. MAP_SHARED doesn't matter since
329330
// the data is read-only, but use PRIVATE just to further avoid accidentally
330331
// modifying the file.
332+
#if defined(_WIN32)
333+
const std::uint64_t map_offset = static_cast<std::uint64_t>(range.start);
334+
#else
335+
const off_t map_offset = static_cast<off_t>(range.start);
336+
#endif
337+
331338
void* pages = ::mmap(
332339
nullptr,
333340
map_size,
334341
PROT_READ,
335342
MAP_PRIVATE,
336343
fd_,
337-
static_cast<off_t>(range.start));
344+
map_offset);
338345
ET_CHECK_OR_RETURN_ERROR(
339346
pages != MAP_FAILED,
340347
AccessFailed,

0 commit comments

Comments
 (0)