lightning
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Lightning] mprotect() can't be used on any memory address


From: Alban Bedel
Subject: [Lightning] mprotect() can't be used on any memory address
Date: Sat, 8 Mar 2008 20:05:53 +0100

Hi,

since a long time I wanted to raise this subject here. The linux man
page for mprotect is really bad. It miss some crucial information and
the provided example is just plain broken. If one look at the POSIX
docs there is this note:

The behavior of this function is unspecified if the mapping was not
established by a call to mmap()

This mean that mprotect() can only be used on pointers returned by
mmap() and nothing else. I made a bit of research on the subject and
this was confirmed by several post from kernel developers on the lkml.

In practice using any address work most of the time. However it rely on
some undefined behaviour and will change the access right to a whole
bunch of unrelated things. And some OS might very well forbid
WRITE|EXEC.

I understand that this is currently out of the scope of lightning
itself. However to portably get some memory that can be turned
executable you must use map(). If the OS support it you probably want
to use an anonymous map instead of mapping /dev/zero. And you need the
page size. There are defines, but man getpagesize tell us it exist bcs
using defines is not reliable on all architectures. However it is
deprected, one should use sysconf (_SC_PAGE_SIZE) instead (or
_SC_PAGESIZE on some systems).

All in all, there is many ways to get that wrong with all the layers of
old APIs and bad documentation. So I think it wouldn't be a bad idea if
ligthning provided some clean and portable implementation.

It could be either with a new call to get such memory, or by making
jit_flush_code() alloc a buffer and copy/patch the code there.

        Albeu





reply via email to

[Prev in Thread] Current Thread [Next in Thread]