Skip to content

Commit

Permalink
Handle zero-length mmaps correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
saagarjha committed Jun 16, 2020
1 parent 95dc3f5 commit 0754471
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 4 deletions.
3 changes: 2 additions & 1 deletion emu/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ void mem_next_page(struct mem *mem, page_t *page);
#define PAGE(addr) ((addr) >> PAGE_BITS)
#define PGOFFSET(addr) ((addr) & (PAGE_SIZE - 1))
typedef dword_t pages_t;
#define PAGE_ROUND_UP(bytes) (((bytes - 1) / PAGE_SIZE) + 1)
// bytes MUST be unsigned if you would like this to overflow to zero
#define PAGE_ROUND_UP(bytes) (PAGE((bytes) + PAGE_SIZE - 1))

#define BYTES_ROUND_DOWN(bytes) (PAGE(bytes) << PAGE_BITS)
#define BYTES_ROUND_UP(bytes) (PAGE_ROUND_UP(bytes) << PAGE_BITS)
Expand Down
3 changes: 0 additions & 3 deletions fs/real.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,6 @@ int realfs_poll(struct fd *fd) {
}

int realfs_mmap(struct fd *fd, struct mem *mem, page_t start, pages_t pages, off_t offset, int prot, int flags) {
if (pages == 0)
return 0;

int mmap_flags = 0;
if (flags & MMAP_PRIVATE) mmap_flags |= MAP_PRIVATE;
if (flags & MMAP_SHARED) mmap_flags |= MAP_SHARED;
Expand Down
1 change: 1 addition & 0 deletions kernel/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void mm_release(struct mm *mm) {
static addr_t do_mmap(addr_t addr, dword_t len, dword_t prot, dword_t flags, fd_t fd_no, dword_t offset) {
int err;
pages_t pages = PAGE_ROUND_UP(len);
if (!pages) return _EINVAL;
page_t page;
if (addr != 0) {
if (PGOFFSET(addr) != 0)
Expand Down

0 comments on commit 0754471

Please sign in to comment.