include cleanup
[trinity.git] / syscalls / mremap.c
blob1c3cdc58b85821451f08779e42987e76dee902a4
1 /*
2 * asmlinkage unsigned long sys_mremap(unsigned long addr,
3 * unsigned long old_len, unsigned long new_len,
4 * unsigned long flags, unsigned long new_addr)
5 */
7 #include <stdlib.h>
8 #include <sys/mman.h>
9 #include "arch.h"
10 #include "maps.h"
11 #include "random.h"
12 #include "sanitise.h"
13 #include "shm.h"
14 #include "utils.h"
16 static unsigned long rand_size(void)
18 const unsigned long sizes[] = { 1 * MB, 2 * MB, 4 * MB, 10 * MB, 1 * GB, 2 * GB };
20 return sizes[rand() % ARRAY_SIZE(sizes)];
23 static void sanitise_mremap(int childno)
25 struct map *map;
27 map = common_set_mmap_ptr_len(childno);
29 shm->syscall[childno].a3 = map->size; //TODO: Munge this.
31 if (shm->syscall[childno].a4 & MREMAP_FIXED) {
32 shm->syscall[childno].a5 = rand_size();
33 } else {
34 shm->syscall[childno].a5 = 0;
37 /* Sometimes dirty the mapping first. */
38 if (!(map->prot & PROT_WRITE))
39 return;
41 if (rand_bool())
42 dirty_mapping(map);
46 * If we successfully remapped a range, we need to update our record of it
47 * so we don't re-use the old address.
49 static void post_mremap(int childno)
51 struct map *map = (struct map *) shm->scratch[childno];
52 void *ptr = (void *) shm->syscall[childno].retval;
54 if (ptr != MAP_FAILED)
55 map->ptr = ptr;
57 shm->scratch[childno] = 0;
60 struct syscallentry syscall_mremap = {
61 .name = "mremap",
62 .num_args = 5,
63 .sanitise = sanitise_mremap,
64 .arg1name = "addr",
65 .arg1type = ARG_MMAP,
66 .arg2name = "old_len",
67 .arg3name = "new_len",
68 .arg4name = "flags",
69 .arg4type = ARG_LIST,
70 .arg4list = {
71 .num = 2,
72 .values = { MREMAP_MAYMOVE, MREMAP_FIXED },
74 .arg5name = "new_addr",
75 .arg5type = ARG_ADDRESS,
76 .group = GROUP_VM,
77 .post = post_mremap,