add mmap_file for WIN32 and httpserver example improvements#3241
Conversation
also hardened against directory traversal
`lib/std/io/file_mmap.c3`: prevent view size from exceeding file mapping bounds.
| Win32_HANDLE h_file = (Win32_HANDLE)win32::_get_osfhandle(fd); | ||
| if (h_file == (void*)uptr::max) return io::FILE_NOT_VALID~; | ||
|
|
||
| bool is_write = access == WRITE || access == READWRITE; |
There was a problem hiding this comment.
EXECWRITE? Why not use to_win32 and tweak it?
|
|
||
| Win32_DWORD offset_high = (Win32_DWORD)(offset >> 32); | ||
| Win32_DWORD offset_low = (Win32_DWORD)(offset & 0xFFFFFFFF); | ||
| void* ptr = win32::mapViewOfFile(h_map, map_access, offset_high, offset_low, (Win32_SIZE_T)size); |
There was a problem hiding this comment.
What about the size = 0 case?
There was a problem hiding this comment.
https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-mapviewoffile
[in] dwNumberOfBytesToMap
The number of bytes of a file mapping to map to the view. All bytes must be within the maximum size specified by CreateFileMapping. If this parameter is 0 (zero), the mapping extends from the specified offset to the end of the file mapping.
For zero it says is valid, and maps from the offset to the end. I could change the contract comment to reflect this?
@param size : "Number of bytes to map, will be rounded up to page size"
There was a problem hiding this comment.
for our api consistency we could, on size == 0 do aligned_alloc_size which does one win32::allocation_granularity, I don't know what's the correct path here.
There was a problem hiding this comment.
my understanding is that POSIX mmap requires the length argument be strictly greater than zero, we are rounding up now to 1 pagesize when mmap_file
maybe in posix we should also if (size == 0) return INVALID_ARGS~;
|
👀 something quite useful for me |
|
Thank you! |
Can't add many features for this example of course