how to check memory access

Hi,

I have a none-null pointer to memory.

Q: how can I check access to memory region ?

Thanks a lot,
Vasilii

vasa <vv40in@mail.ru> wrote:

I have a none-null pointer to memory.
Q: how can I check access to memory region ?

I’m not sure what you mean by “check access”. Do you want to
verify that the pointer is valid? Or just pointing to the “right thing”?

-Adam

I have a none-null pointer to memory.
Q: how can I check access to memory region ?

I’m not sure what you mean by “check access”. Do you want to
verify that the pointer is valid? Or just pointing to the “right thing”?

May be both things…

Vasili

I think you can trap the SIGSEGV signal and then just try to dereference the
pointer.

“none” <none@none.com> wrote in message news:9ocr7v$mfn$1@nntp.qnx.com

“vasa” <> vv40in@mail.ru> > wrote in message news:9ob9s7$p8l$> 1@inn.qnx.com> …
I have a none-null pointer to memory.
Q: how can I check access to memory region ?

I’m not sure what you mean by “check access”. Do you want to
verify that the pointer is valid? Or just pointing to the “right
thing”?

May be both things…

Well you can assert or check that a pointer is not NULL before following
it.
You could check ranges that it must fall into, ie. if it’s a pointer
inside
an array, it’s address should not be less then the 0th element and more
then
the Nth element.

Check out the function assert() to get you started, but in all honesty,
you
can’t prove that a pointer will point to valid data. The whole idea of a
pointer is that it can point to anything (I assume we’re talking about C).

-Adam

I believe on x86 there are verr/verw instructions.
check inline.h

“vasa” <vv40in@mail.ru> wrote in message news:9o8b53$1ep$1@inn.qnx.com

Hi,

I have a none-null pointer to memory.

Q: how can I check access to memory region ?

Thanks a lot,
Vasilii

“Bill Caroselli (Q-TPS)” <qtps@earthlink.net> wrote in message
news:9ocv9q$r5i$4@inn.qnx.com

I think you can trap the SIGSEGV signal and then just try to dereference
the
pointer.

I’ve tryed that on QNX4 and the problem is that if the signal hander doesn’t
do a jump or an exit, you will return to the point of interrupt and it will
get
rexecuted, resulting in endless SIGSEGV.

“none” <> none@none.com> > wrote in message news:9ocr7v$mfn$> 1@nntp.qnx.com> …
“vasa” <> vv40in@mail.ru> > wrote in message
news:9ob9s7$p8l$> 1@inn.qnx.com> …
I have a none-null pointer to memory.
Q: how can I check access to memory region ?

I’m not sure what you mean by “check access”. Do you want to
verify that the pointer is valid? Or just pointing to the “right
thing”?

May be both things…

Well you can assert or check that a pointer is not NULL before following
it.
You could check ranges that it must fall into, ie. if it’s a pointer
inside
an array, it’s address should not be less then the 0th element and more
then
the Nth element.

Check out the function assert() to get you started, but in all honesty,
you
can’t prove that a pointer will point to valid data. The whole idea of
a
pointer is that it can point to anything (I assume we’re talking about
C).

-Adam
\

me <a@b.c> wrote:

I believe on x86 there are verr/verw instructions.
check inline.h

I believe they only check to see if your segment is correct,
and since NTO is flat…

You could try sigsetjmp/siglongjmp

“vasa” <> vv40in@mail.ru> > wrote in message news:9o8b53$1ep$> 1@inn.qnx.com> …
Hi,

I have a none-null pointer to memory.

Q: how can I check access to memory region ?

Thanks a lot,
Vasilii
\


cburgess@qnx.com

Good point.

“Mario Charest” <mcharest@clipzinformatic.com> wrote in message
news:9od6o0$2fd$1@inn.qnx.com

“Bill Caroselli (Q-TPS)” <> qtps@earthlink.net> > wrote in message
news:9ocv9q$r5i$> 4@inn.qnx.com> …
I think you can trap the SIGSEGV signal and then just try to dereference
the
pointer.


I’ve tryed that on QNX4 and the problem is that if the signal hander
doesn’t
do a jump or an exit, you will return to the point of interrupt and it
will
get
rexecuted, resulting in endless SIGSEGV.

You could try sigsetjmp/siglongjmp

Could I use that in particular thread ?
what is behavior sigsetjmp/siglongjmp in multithreaded environment ?.

Vasilii

vasa <vv40in@mail.ru> wrote:

You could try sigsetjmp/siglongjmp

Could I use that in particular thread ?
what is behavior sigsetjmp/siglongjmp in multithreaded environment ?.

As long as both setjmp and longjmp are done in the same thread, it should
be alright.


cburgess@qnx.com

“vasa” <vv40in@mail.ru> wrote in message news:9ob9s7$p8l$1@inn.qnx.com

I have a none-null pointer to memory.
Q: how can I check access to memory region ?

I’m not sure what you mean by “check access”. Do you want to
verify that the pointer is valid? Or just pointing to the “right
thing”?

May be both things…

Well you can assert or check that a pointer is not NULL before following it.
You could check ranges that it must fall into, ie. if it’s a pointer inside
an array, it’s address should not be less then the 0th element and more then
the Nth element.

Check out the function assert() to get you started, but in all honesty, you
can’t prove that a pointer will point to valid data. The whole idea of a
pointer is that it can point to anything (I assume we’re talking about C).

-Adam

I cannot use sigjump.
It is in pthread_cleanup_push block


Best regards,
Vasilii

Colin Burgess <cburgess@qnx.com> ÓÏÏÂÝÉÌ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ:9odm93$8vn$1@nntp.qnx.com

vasa <> vv40in@mail.ru> > wrote:
You could try sigsetjmp/siglongjmp

Could I use that in particular thread ?
what is behavior sigsetjmp/siglongjmp in multithreaded environment ?.

As long as both setjmp and longjmp are done in the same thread, it should
be alright.


cburgess@qnx.com

Wouldn’t the following work?

/* tests whether argv[1] is a good pointer */
#include <stdio.h>
#include <setjmp.h>
#include <signal.h>
#include <pthread.h>

pthread_key_t buffer_key;

static void handle_sigsegv(int signo)
{
jmp_buf *env;

printf("%d: whoops!\n", pthread_self());
env = (jmp_buf *)pthread_getspecific( buffer_key );
siglongjmp( *env, 0 );
}

int memory_ok( void *address )
{
unsigned a, notok;
void (*saved_sigsegv)(int);
jmp_buf *env;
int oldstate;

pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, &oldstate );

env = (jmp_buf *)pthread_getspecific( buffer_key );
if ( sigsetjmp( *env, 1 ) == 1 ) {
signal(SIGSEGV, saved_sigsegv);
pthread_setcancelstate( oldstate, NULL );
return 0;
}

saved_sigsegv = signal(SIGSEGV, handle_sigsegv);
a = *((unsigned *)address);
signal(SIGSEGV, saved_sigsegv);
pthread_setcancelstate( oldstate, NULL );

return 1;
}

void cleanup_jmp_env( void *value )
{
free( value );
pthread_setspecific( buffer_key, NULL );
}

void *test_thread(void *value)
{
jmp_buf *env;

env = (jmp_buf *)malloc( sizeof *env );
pthread_setspecific( buffer_key, env );

if ( memory_ok( value ) ) {
printf("%d: all is ok!\n", pthread_self());
}
else {
printf("%d: all is not ok!\n", pthread_self());
}
return NULL;
}

int main(int argc, char **argv)
{
pthread_t tids[2];

pthread_key_create( &buffer_key, cleanup_jmp_env );

pthread_create( &tids[0], NULL, test_thread, argv[1] );
pthread_create( &tids[1], NULL, test_thread, argv[1] );

pthread_join( tids[0], NULL );
pthread_join( tids[1], NULL );
return 0;
}

vasa <vv40in@mail.ru> wrote:

I cannot use sigjump.
It is in pthread_cleanup_push block


Best regards,
Vasilii


cburgess@qnx.com

Yes, it works OK

But I thought it could solutions without context switching for exception.
I.e. somebody should to know all of process accessible memory map and I could to ask he (?)

Vasilii

vasa <vv40in@mail.ru> wrote:

Yes, it works OK

But I thought it could solutions without context switching for exception.
I.e. somebody should to know all of process accessible memory map and I could to ask he (?)

You can query the process’ memory map via the procfs interface.

Take a look at the source code to pidin and look for DCMD_PROC_PAGEDATA in pidin_proc.c

pidin’s source code is on our cvs server (cvs.qnx.com nto/util/pidin)


cburgess@qnx.com

Sort of defeats the purpose of having an operating system, doesn’t it ?
That is, if you are going to do your own memory protection, why buy an
O/S that does it for you ?

Exceptions are the mechanism by which your process is informed of a
memory violation by the operating system, if you don’t like this, you
are left with few choices of operating systems since this is the
predominant signalling method. Off the top of my head, I can’t see how
any operating system using common MMU’s could avoid a process → kernel
context switch on a violation.

-----Original Message-----
From: vasa [mailto:vv40in@mail.ru]
Posted At: Tuesday, September 25, 2001 2:19 PM
Posted To: os
Conversation: how to check memory access
Subject: Re: how to check memory access


Yes, it works OK

But I thought it could solutions without context switching for
exception.
I.e. somebody should to know all of process accessible memory map and I
could to ask he (?)

Vasilii