2 * Simple test of using an alternate buffer for the code.
14 # define MAP_ANON MAP_ANONYMOUS
15 # ifndef MAP_ANONYMOUS
16 # define MAP_ANONYMOUS 0
25 main(int argc, char *argv[])
33 void (*function)(void);
34 int mmap_prot, mmap_flags, result;
37 mmap_fd = open("/dev/zero", O_RDWR);
40 mmap_prot = PROT_READ | PROT_WRITE;
41 #if !(__OpenBSD__ || __APPLE__)
42 mmap_prot |= PROT_EXEC;
45 mmap_prot = PROT_MPROTECT(mmap_prot);
48 mmap_flags = MAP_PRIVATE;
50 mmap_flags |= MAP_ANON;
51 ptr = mmap(NULL, 1024 * 1024, mmap_prot, mmap_flags, mmap_fd, 0);
52 assert(ptr != MAP_FAILED);
58 _jit = jit_new_state();
62 jit_pushargi((jit_word_t)"%s\n");
64 jit_pushargi((jit_word_t)"ok");
67 /* call to jit_realize() is only required when using an alternate
68 * code buffer. Note that not using mmap'ed memory may not work
69 * on several ports and/or operating system versions */
73 if (jit_get_code(&length) != NULL)
79 /* check that a too small buffer fails */
81 function = jit_emit();
86 result = mprotect(ptr, 1024 * 1024, PROT_READ | PROT_WRITE);
89 /* and calling again with enough space works */
90 jit_set_code(ptr, 1024 * 1024);
91 function = jit_emit();
96 #if __NetBSD__ || __OpenBSD__ || __APPLE__
97 result = mprotect(ptr, 1024 * 1024, PROT_READ | PROT_EXEC);
104 munmap(ptr, 1024 * 1024);