Re: virtual memory range that can be used for wrong pointers

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


On Wed, Aug 01, 2018 at 03:28:51PM +0300, Yury Norov wrote:
> Hi Oleg,
> On Wed, Aug 01, 2018 at 12:58:01PM +0300, Oleg wrote:
> >   Hello.
> > 
> > I have a code where i need more than one "wrong" pointer value(this one is
> > NULL pointer). Something like a signal handler SIG_IGN that equal to 1.
> > 
> > So, my question is, what is the guarantied range of addresses (from 0 to X),
> > which can be safely used for this purpose on any hardware architecture?
> > 
> > Thanks.
> > 
> > -- 
> > Олег Неманов (Oleg Nemanov)
> > --
> Assuming your code is userspace code, right?


> There is no generic rule that NULL is invalid pointer. One can mmap()
> memory at NULL with MAP_FIXED and it will make NULL dereferencable.

You broke my world :-).
> Though, usually the whole page containing NULL is not mapped to
> userspace.

So, i can assume that address from 0 to 4k can't be returned from malloc
(or be in code in form of a function pointer or pointer to a global variable)
and i can use it to specify a value distinguished from any pointer?

> You can check valid mappings with 'cat /proc/<pid>/maps'.
> Dereferencing of any address out of that list will cause segfault.
> If you need the range of addresses that cause segfaults if
> dereferenced, the most portable way would be mapping sufficient
> amount of memory with PROT_NONE.

No-no. E.g. i have some function that set an action on some event:

int set_event_act(enum event e, void (*act)(void));

User of set_event_act() can supply a custom act() for event e OR
use one of predefined actions. E.g.:

#define ACT_NONE 0
#define ACT_ACT1 1
#define ACT_ACT2 2

and later in a code:

set_event_act(e, ACT_NONE);


set_event_act(e, ACT_ACT1);


set_event_act(e, ACT_ACT2);


set_event_act(e, custom);

If i understand you correctly, i can do this safely, can't i?

Олег Неманов (Oleg Nemanov)
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux