test-mmap.c revision f8db050073c46a20de538fb22c33e22952ee7223
/*
* Small test program to verify simulated mmap behaviour.
*
* When running qemu-linux-user with the -p flag, you may need to tell
* this test program about the pagesize because getpagesize() will not reflect
* the -p choice. Simply pass one argument beeing the pagesize.
*
* Copyright (c) 2007 AXIS Communications AB
* Written by Edgar E. Iglesias.
*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
/*
* Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
* other than GPL or LGPL is available it will apply instead, Oracle elects to use only
* the General Public License version 2 (GPLv2) at this time for any software where
* a choice of GPL license versions is made available with the language indicating
* that GPLv2 or any later version may be used, or where a choice of which version
* of the GPL is applied is otherwise unspecified.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#define D(x)
#define fail_unless(x) \
do \
{ \
if (!(x)) { \
exit (EXIT_FAILURE); \
} \
} while (0);
unsigned char *dummybuf;
static unsigned int pagesize;
static unsigned int pagemask;
int test_fd;
void check_aligned_anonymous_unfixed_mmaps(void)
{
void *p1;
void *p2;
void *p3;
void *p4;
void *p5;
uintptr_t p;
int i;
for (i = 0; i < 0x1fff; i++)
{
/* Make sure we get pages aligned with the pagesize. The
target expects this. */
D(printf ("p=%x\n", p));
fail_unless ((p & pagemask) == 0);
fail_unless ((p & pagemask) == 0);
fail_unless ((p & pagemask) == 0);
fail_unless ((p & pagemask) == 0);
fail_unless ((p & pagemask) == 0);
/* Make sure we can read from the entire area. */
}
}
void check_large_anonymous_unfixed_mmap(void)
{
void *p1;
uintptr_t p;
len = 0x02000000;
/* Make sure we get pages aligned with the pagesize. The
target expects this. */
fail_unless ((p & pagemask) == 0);
/* Make sure we can read from the entire area. */
}
{
char *p1;
char *p2;
char *p3;
uintptr_t p;
int i;
for (i = 0; i < 0x2fff; i++)
{
int nlen;
fail_unless ((p & pagemask) == 0);
fail_unless ((p & pagemask) == 0);
/* Check if the mmaped areas collide. */
fail_unless (0);
/* Make sure we get pages aligned with the pagesize. The
target expects this. */
fail_unless ((p & pagemask) == 0);
}
}
void check_aligned_anonymous_fixed_mmaps(void)
{
char *addr;
void *p1;
uintptr_t p;
int i;
/* Find a suitable address to start with. */
-1, 0);
for (i = 0; i < 40; i++)
{
/* Create submaps within our unfixed map. */
-1, 0);
/* Make sure we get pages aligned with the pagesize.
The target expects this. */
fail_unless ((p & pagemask) == 0);
}
}
{
char *addr;
void *p1;
uintptr_t p;
int i;
/* Find a suitable address to start with. Right were the x86 hosts
stack is. */
addr = ((void *)0x80000000);
for (i = 0; i < 20; i++)
{
/* Create submaps within our unfixed map. */
-1, 0);
/* Make sure we get pages aligned with the pagesize.
The target expects this. */
fail_unless ((p & pagemask) == 0);
}
}
void check_file_unfixed_mmaps(void)
{
uintptr_t p;
int i;
for (i = 0; i < 0x10; i++)
{
test_fd, 0);
/* Make sure we get pages aligned with the pagesize. The
target expects this. */
fail_unless ((p & pagemask) == 0);
fail_unless ((p & pagemask) == 0);
fail_unless ((p & pagemask) == 0);
/* Verify that the file maps was made correctly. */
fail_unless (*p1 == 0);
}
}
void check_file_unfixed_eof_mmaps(void)
{
char *cp;
unsigned int *p1;
uintptr_t p;
int i;
for (i = 0; i < 0x10; i++)
{
/* Make sure we get pages aligned with the pagesize. The
target expects this. */
fail_unless ((p & pagemask) == 0);
/* Verify that the file maps was made correctly. */
/* Verify that the end of page is accessable and zeroed. */
}
}
void check_file_fixed_eof_mmaps(void)
{
char *addr;
char *cp;
unsigned int *p1;
uintptr_t p;
int i;
/* Find a suitable address to start with. */
-1, 0);
for (i = 0; i < 0x10; i++)
{
/* Create submaps within our unfixed map. */
/* Make sure we get pages aligned with the pagesize. The
target expects this. */
fail_unless ((p & pagemask) == 0);
/* Verify that the file maps was made correctly. */
/* Verify that the end of page is accessable and zeroed. */
}
}
void check_file_fixed_mmaps(void)
{
unsigned char *addr;
int i;
/* Find a suitable address to start with. */
-1, 0);
for (i = 0; i < 40; i++)
{
test_fd, 0);
/* Make sure we get pages aligned with the pagesize.
The target expects this. */
/* Verify that the file maps was made correctly. */
fail_unless (*p1 == 0);
}
}
{
char tempname[] = "/tmp/.cmmapXXXXXX";
unsigned int i;
/* Trust the first argument, otherwise probe the system for our
pagesize. */
if (argc > 1)
else
/* Assume pagesize is a power of two. */
/* Fill the file with int's counting from zero and up. */
for (i = 0; i < (pagesize * 4) / sizeof i; i++)
/* Append a few extra writes to make the file end at non
page boundary. */
/* Run the tests. */
/* Fails at the moment. */
/* check_aligned_anonymous_fixed_mmaps_collide_with_host(); */
return EXIT_SUCCESS;
}