executing code in mmapped memory

From: Anton Ertl (anton_at_mips.complang.tuwien.ac.at)
Date: 08/23/03


Date: Sat, 23 Aug 2003 13:11:59 GMT

I am porting something like a JIT compiler to HP/UX (the test machine
I use is an 9000/879/K260 under HP/UX B.11.00 ).

I allocate the memory for the code I generate at run-time with

mmap(next_address, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0)

(in the specific execution I looked at, this returned a region at
0x6f60d000). Then I generate the code there. As soon as the program
tries to execute the first instruction in this region, it produces a
SIGSEGV (found by single-stepping with gdb). The same instruction
executed from code in the binary works.

I have three theories of what's going causing the segmentation
violation:

- the mmap does not produce a PROT_EXEC region even though I ask for
it.

- the HP-PA segmentation somehow gets in the way.

- the cache flushing fails somehow. I use the following code, with a
linewidth of 32:

void cacheflush(void * address, size_t size, size_t linewidth)
{
  void *p=(void *)((size_t)address & (-linewidth));

  for(; p<address+size;)
    asm volatile("fdc (%0)\n\t"
                 "sync\n\t"
                 "fic,m %1(%0)\n\t"
                 "sync" : "+r"(p) : "r"(linewidth) : "memory" );
}

I am actually somewhat surprised by the SIGSEGV because earlier
versions that also use a bit of run-time code generation work.
However, checking the old code again, I see that it does not use mmap
but malloc, and gets a block starting at 0x12120.

Do you have any idea why I am getting these segmentation violations?

- anton

-- 
M. Anton Ertl                    Some things have to be seen to be believed
anton@mips.complang.tuwien.ac.at Most things have to be believed to be seen
http://www.complang.tuwien.ac.at/anton/home.html


Relevant Pages

  • Re: Structure of large link libraries in f95
    ... that all of the program has to be brought into memory so that execution ... If you relink a program under HP/UX and write over the executable while it is running, the running program will crash. ... With "copy on write", a modified version is written to the paging file, and that copy is used for later references. ...
    (comp.lang.fortran)
  • Re: Parallel quicksort Question
    ... exceeds the million elements programs it does not finish the execution ... and it answers with "segmentation fault". ...
    (comp.parallel.mpi)