[Vm-dev] Cog on Mac. Problems with mprotect()?

Igor Stasenko siguctua at gmail.com
Mon Dec 6 09:48:55 UTC 2010


On 6 December 2010 00:59, John M McIntosh
<johnmci at smalltalkconsulting.com> wrote:
>
> mprotect  likely returns Invalid Argument because the range is wrong.
>
> I traded some notes with Eliot on this in August, you'll need to chase him to see what we decided to change in order to avoid the problem.
> Yes this is the for mprotect(x,y,PROT_READ | PROT_WRITE): Invalid argument  I guess...
>

In NativeBoost i overriding the #initStackPagesAndInterpret,
to replace call:
	self sqMakeMemoryNotExecutableFrom: objectMemory startOfMemory
asUnsignedInteger
		To: objectMemory memoryLimit asUnsignedInteger.

with:
	self sqMakeMemoryExecutableFrom: objectMemory startOfMemory asUnsignedInteger
		To: objectMemory memoryLimit asUnsignedInteger.

so, really. This should not be a problem with my little patch, because
i using same arguments as in original version.


mmap and mprotect explicitly says that start address should be always
a multiple of page size (which is usually 4k on x86, and there are
usually some special functions to get the page size somewhere).


> "This is the mprotect for the squeak heap
> start 525336576 end 1043333096 firstPage 525336576 roundUpLength 517996544

What is strange, that docs says nothing about the length parameter
constraints. Should it also be a page size aligned or not?

because:

start: 525336576 \\ 4096 0
end: 1043333096 \\ 4096  4072

that could be the cause of problem, if length not rounded to page size.


> that works with my change to subtract out the cogCodeSize
>
>        sqMakeMemoryNotExecutableFromTo(((usqInt)heapBase), ((usqInt)GIV(memoryLimit))-GIV(cogCodeSize));
> But the
>        sqMakeMemoryNotExecutableFromTo(((usqInt)theStackMemory), (((usqInt)theStackMemory)) + stackPagesBytes);
>
> fails
>

Since the sqMakeMemory[Not]ExecutableFromTo() in both variants using:

mprotect(startAddr, endAddr - startAddr + 1, ...

so i think that
     sqMakeMemoryNotExecutableFromTo(((usqInt)theStackMemory),
(((usqInt)theStackMemory)) + stackPagesBytes);

should be:

     sqMakeMemoryNotExecutableFromTo(((usqInt)theStackMemory),
(((usqInt)theStackMemory)) + stackPagesBytes - 1);

and then we should ensure that stackPagesBytes is page-size aligned.
Otherwise, if even that won't work, i can't imagine what else we can do :)


Btw, Eliot why you made these functions
(sqMakeMemory[Not]Executable...) accept an address range instead of
address + length ?

mmap(), VirtualAlloc(),  mprotect() and VirtualProtect all using addr
+ size arguments, not the address range.


-- 
Best regards,
Igor Stasenko AKA sig.


More information about the Vm-dev mailing list