Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 28 additions & 45 deletions mmap/unix/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,23 @@
#if APR_HAS_MMAP || defined(BEOS)

#ifndef BEOS
struct mm_layout {
apr_mmap_t mm;
apr_off_t poffset;
};

static APR_INLINE
apr_mmap_t *alloc_with_poffset(apr_pool_t *p)
/* Returns the offset of ADDR from the start of the page containing
* it. The start of a mapping is always page-aligned, so for the
* ->mm pointer of an apr_mmap_t this is the offset of the data from
* the start of the underlying mapping; no state beyond apr_mmap_t
* (the size of which cannot change) is needed to track it. */
static apr_size_t page_offset(apr_uintptr_t addr)
{
struct mm_layout *layout = apr_pcalloc(p, sizeof(*layout));
return &layout->mm;
}
#if defined(_SC_PAGESIZE)
long psize = sysconf(_SC_PAGESIZE);

static APR_INLINE
void set_poffset(apr_mmap_t *mm, apr_off_t poffset)
{
struct mm_layout *layout = (struct mm_layout *)mm;
layout->poffset = poffset;
}
/* the page size should be a power of two */
assert(psize > 0 && (psize & (psize - 1)) == 0);

static APR_INLINE
apr_off_t get_poffset(apr_mmap_t *mm)
{
struct mm_layout *layout = (struct mm_layout *)mm;
return layout->poffset;
return (apr_size_t)(addr & (apr_uintptr_t)(psize - 1));
#else
return 0;
#endif
}
#endif

Expand All @@ -94,7 +87,11 @@ static apr_status_t mmap_cleanup(void *themmap)
#ifdef BEOS
rv = delete_area(mm->area);
#else
rv = munmap((char *)mm->mm - get_poffset(mm), mm->size + get_poffset(mm));
{
apr_size_t poffset = page_offset((apr_uintptr_t)mm->mm);

rv = munmap((char *)mm->mm - poffset, mm->size + poffset);
}
#endif
mm->mm = (void *)-1;

Expand All @@ -114,8 +111,7 @@ APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new,
area_id aid = -1;
uint32 pages = 0;
#else
static long psize;
apr_off_t poffset = 0;
apr_size_t poffset;
apr_int32_t native_flags = 0;
#endif

Expand All @@ -134,9 +130,6 @@ APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new,
return APR_EBADF;

#ifdef BEOS

*new = (apr_mmap_t *)apr_pcalloc(cont, sizeof(apr_mmap_t));

/* XXX: mmap shouldn't really change the seek offset */
apr_file_seek(file, APR_SET, &offset);

Expand All @@ -156,11 +149,7 @@ APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new,

if (aid >= B_NO_ERROR)
read(file->filedes, mm, size);

(*new)->area = aid;

#else
*new = alloc_with_poffset(cont);

if (flag & APR_MMAP_WRITE) {
native_flags |= PROT_WRITE;
Expand All @@ -169,15 +158,8 @@ APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new,
native_flags |= PROT_READ;
}

#if defined(_SC_PAGESIZE)
if (psize == 0) {
psize = sysconf(_SC_PAGESIZE);
/* the page size should be a power of two */
assert(psize > 0 && (psize & (psize - 1)) == 0);
}
poffset = offset & (apr_off_t)(psize - 1);
set_poffset(*new, poffset);
#endif
/* Map from the start of the page containing the offset. */
poffset = page_offset((apr_uintptr_t)offset);

mm = mmap(NULL, size + poffset,
native_flags, MAP_SHARED,
Expand All @@ -192,6 +174,12 @@ APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new,
mm = (char *)mm + poffset;
#endif

/* Allocate only on success, so that a caller which repeatedly
* fails does not consume memory from the pool. */
(*new) = (apr_mmap_t *)apr_pcalloc(cont, sizeof(apr_mmap_t));
#ifdef BEOS
(*new)->area = aid;
#endif
(*new)->mm = mm;
(*new)->size = size;
(*new)->cntxt = cont;
Expand All @@ -207,12 +195,7 @@ APR_DECLARE(apr_status_t) apr_mmap_dup(apr_mmap_t **new_mmap,
apr_mmap_t *old_mmap,
apr_pool_t *p)
{
#ifdef BEOS
*new_mmap = (apr_mmap_t *)apr_pmemdup(p, old_mmap, sizeof(apr_mmap_t));
#else
*new_mmap = (apr_mmap_t *)apr_pmemdup(p, old_mmap,
sizeof(struct mm_layout));
#endif
(*new_mmap)->cntxt = p;

APR_RING_INSERT_AFTER(old_mmap, *new_mmap, link);
Expand Down
43 changes: 28 additions & 15 deletions mmap/win32/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new, apr_file_t *file,
DWORD mvaccess = 0;
DWORD offlo;
DWORD offhi;
/** The physical start, size and offset */
apr_off_t pstart;
apr_size_t psize;
apr_off_t poffset;
HANDLE mhandle = NULL;
void *mv;

if (size == 0)
return APR_EINVAL;
Expand All @@ -99,10 +105,9 @@ APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new, apr_file_t *file,
memblock = si.dwAllocationGranularity;
}

*new = apr_pcalloc(cont, sizeof(apr_mmap_t));
(*new)->pstart = (offset / memblock) * memblock;
(*new)->poffset = offset - (*new)->pstart;
(*new)->psize = (apr_size_t)((*new)->poffset) + size;
pstart = (offset / memblock) * memblock;
poffset = offset - pstart;
psize = (apr_size_t)poffset + size;
/* The size of the CreateFileMapping object is the current size
* of the size of the mmap object (e.g. file size), not the size
* of the mapped region!
Expand All @@ -111,35 +116,43 @@ APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new, apr_file_t *file,
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
(*new)->mhandle = CreateFileMappingW(file->filehand, NULL, fmaccess,
0, 0, NULL);
mhandle = CreateFileMappingW(file->filehand, NULL, fmaccess,
0, 0, NULL);
}
#endif
#if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
{
(*new)->mhandle = CreateFileMappingA(file->filehand, NULL, fmaccess,
0, 0, NULL);
mhandle = CreateFileMappingA(file->filehand, NULL, fmaccess,
0, 0, NULL);
}
#endif
if (!(*new)->mhandle || (*new)->mhandle == INVALID_HANDLE_VALUE)
if (!mhandle || mhandle == INVALID_HANDLE_VALUE)
{
*new = NULL;
return apr_get_os_error();
}

offlo = (DWORD)(*new)->pstart;
offhi = (DWORD)((*new)->pstart >> 32);
(*new)->mv = MapViewOfFile((*new)->mhandle, mvaccess, offhi,
offlo, (*new)->psize);
if (!(*new)->mv)
offlo = (DWORD)pstart;
offhi = (DWORD)(pstart >> 32);
mv = MapViewOfFile(mhandle, mvaccess, offhi, offlo, psize);
if (!mv)
{
apr_status_t rv = apr_get_os_error();
CloseHandle((*new)->mhandle);
CloseHandle(mhandle);
*new = NULL;
return rv;
}

/* Allocate only on success, so that a caller which repeatedly
* fails does not consume memory from the pool. */
*new = apr_pcalloc(cont, sizeof(apr_mmap_t));
(*new)->pstart = pstart;
(*new)->poffset = poffset;
(*new)->psize = psize;
(*new)->mhandle = mhandle;
(*new)->mv = mv;

(*new)->mm = (char*)((*new)->mv) + (*new)->poffset;
(*new)->size = size;
(*new)->cntxt = cont;
Expand Down
79 changes: 79 additions & 0 deletions test/testmmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,39 @@ static void test_mmap_contents(abts_case *tc, void *data)

}

static void test_mmap_dup(abts_case *tc, void *data)
{
apr_off_t *offset = data;
apr_pool_t *subp;
apr_mmap_t *dup;
apr_status_t rv;

if (!themmap) {
ABTS_SKIP(tc, data, "MMap not open, skipping mmap dup.");
return;
}

apr_pool_create(&subp, ptest);
rv = apr_mmap_dup(&dup, themmap, subp);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);

/* Deleting the original leaves the region mapped via the dup. */
rv = apr_mmap_delete(themmap);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
themmap = NULL;
ABTS_STR_NEQUAL(tc, dup->mm, thisfdata, thisfsize);

/* Deleting the last reference must unmap the region. */
rv = apr_mmap_delete(dup);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
apr_pool_destroy(subp);

/* Map the file again for the remaining tests. */
rv = apr_mmap_create(&themmap, thefile, *offset, thisfsize,
APR_MMAP_READ, ptest);
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
}

static void test_mmap_delete(abts_case *tc, void *data)
{
apr_status_t rv;
Expand Down Expand Up @@ -259,6 +292,50 @@ static void test_mmap_offset(abts_case *tc, void *data)

}

/* A failed apr_mmap_create() must not consume memory from the pool;
* callers such as the FILE bucket may retry many times within the
* lifetime of one pool. */
static void test_mmap_create_fail(abts_case *tc, void *data)
{
static const char fname[] = "data/mmap_wronly.txt";
apr_pool_t *subp;
apr_file_t *f;
apr_mmap_t *mm = (void *)tc; /* any non-NULL value */
apr_status_t rv;
apr_size_t before;

apr_pool_create(&subp, ptest);

/* A file which is open only for writing cannot be mapped for
* reading. */
rv = apr_file_open(&f, fname,
APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_TRUNCATE,
APR_FPROT_OS_DEFAULT, subp);
APR_ASSERT_SUCCESS(tc, "open file for writing", rv);
if (rv != APR_SUCCESS) {
return;
}
APR_ASSERT_SUCCESS(tc, "write to file", apr_file_puts("hello, world", f));

/* apr_pool_num_bytes() always returns zero unless built with
* pool debugging, so this is only checked in such builds. */
before = apr_pool_num_bytes(subp, 0);

rv = apr_mmap_create(&mm, f, 0, 5, APR_MMAP_READ, subp);
ABTS_TRUE(tc, rv != APR_SUCCESS);
ABTS_PTR_EQUAL(tc, NULL, mm);

ABTS_ASSERT(tc, "failed apr_mmap_create() allocated from the pool",
apr_pool_num_bytes(subp, 0) == before);

if (mm) {
apr_mmap_delete(mm);
}
apr_file_close(f);
apr_file_remove(fname, subp);
apr_pool_destroy(subp);
}

#endif

abts_suite *testmmap(abts_suite *suite)
Expand All @@ -276,10 +353,12 @@ abts_suite *testmmap(abts_suite *suite)
abts_run_test(suite, test_mmap_create, &test_set[i].offset);
abts_run_test(suite, test_mmap_contents, &test_set[i].offset);
abts_run_test(suite, test_mmap_offset, &test_set[i].offset);
abts_run_test(suite, test_mmap_dup, &test_set[i].offset);
abts_run_test(suite, test_mmap_delete, NULL);
abts_run_test(suite, test_file_close, NULL);
apr_pool_clear(ptest);
}
abts_run_test(suite, test_mmap_create_fail, NULL);
apr_pool_destroy(ptest);
#else
abts_run_test(suite, not_implemented, NULL);
Expand Down
Loading