View previous topic :: View next topic |
Author |
Message |
sulu Guru


Joined: 21 May 2002 Posts: 399 Location: Dornbirn/Austria
|
Posted: Fri Jun 13, 2003 10:55 am Post subject: How to check if an adress is within the valid adress-space? |
|
|
Hi C-Geeks
The question:
Is there an efficient way to check whether the adress stored in a pointer is within the adress-space of the process without seg-faulting if the adress is invalid?
Little Background:
This is to be used in a buffer-check-routine. First it has to be checked if the pointer to the buffer is a valid adress.
Only if this has been proven one may access the buffer and do some further checks without risking a program crash due to seg-faulting. |
|
Back to top |
|
 |
GuidoJ Guru


Joined: 24 Dec 2002 Posts: 444 Location: Netherlands
|
Posted: Fri Jun 13, 2003 11:55 am Post subject: |
|
|
This is very OS specific. Frankly I have no idea how this is handled on Linux.
In general, assuming that each process has its own address space (on an ia32 architecture this would mean that each process has its own page directory), then the only "illegal" addresses" are those that are reserved by the OS for mapping shared objects. Of course this does is no protection against segmentation faults, so this is probably not what you want to know.
I think your question is how to find out where the start and end of the heap of a process are. Still, this is OS dependend, or more specificly where the heap pages are mapped into the address space of the process. |
|
Back to top |
|
 |
sulu Guru


Joined: 21 May 2002 Posts: 399 Location: Dornbirn/Austria
|
Posted: Fri Jun 13, 2003 9:04 pm Post subject: |
|
|
Hi GuidoJ
Thanks for replying
Maybe i have to rephrase my question.
How to check that a pointer does not point to a valid object or data but without a dreferenciation which would lead to a segfault in case of an uninitialized pointer?
Maybe this could be achieved with a signal handler.
But i dont want to write a signal handler because i tried this once and didnt succeed.
Quote: |
I think your question is how to find out where the start and end of the heap of a process are. |
I know that dynamicaly allocated objects reside on the heap. Knowing the start and end of th application heap would be great but there remains the stack.
Quote: |
Still, this is OS dependend, or more specificly where the heap pages are mapped into the address space of the process.
|
This is true but i think of some glibc-functions or something similar which should decouple the problem from os-specific behavior.
Regards
Sulu |
|
Back to top |
|
 |
far Guru


Joined: 10 Mar 2003 Posts: 394 Location: Stockholm, Sweden
|
Posted: Fri Jun 13, 2003 11:07 pm Post subject: |
|
|
sulu wrote: | Maybe this could be achieved with a signal handler. |
Just catching SIGSEGV is not enough since you can't continue after that.
I tried to do it by cloning the process and catching it in the cloned process, but I couldn't make clone(2) work. _________________ The Porthole Portage Frontend |
|
Back to top |
|
 |
sulu Guru


Joined: 21 May 2002 Posts: 399 Location: Dornbirn/Austria
|
Posted: Mon Jun 16, 2003 5:17 am Post subject: |
|
|
Hi far.
Quote: |
Just catching SIGSEGV is not enough since you can't continue after that.
|
You can if you do your own signal handling. I caught the SIGSEGV when an uninitialized pointer occured successfully but i wanted to switch back to the default handling after the initialization stage of the program and that was where i failed.
I think a way could be to exploit the functions dealing with meomory-mapping. A dereferenciation of a pointer
results in a page lookup wich naturally fails in case of an uninitialized pointer because there is no valid page to be found in that case.
So if there is a distinction between "page not found" and "invalid page" this could be a way to do this check.
But i dont know the name of those mm_map function and whether they are available for using in a standard-C-program.
Regards
Sulu
[/quote] |
|
Back to top |
|
 |
|