Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
How to check if an adress is within the valid adress-space?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
sulu
Guru
Guru


Joined: 21 May 2002
Posts: 399
Location: Dornbirn/Austria

PostPosted: Fri Jun 13, 2003 10:55 am    Post subject: How to check if an adress is within the valid adress-space? Reply with quote

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
View user's profile Send private message
GuidoJ
Guru
Guru


Joined: 24 Dec 2002
Posts: 444
Location: Netherlands

PostPosted: Fri Jun 13, 2003 11:55 am    Post subject: Reply with quote

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
View user's profile Send private message
sulu
Guru
Guru


Joined: 21 May 2002
Posts: 399
Location: Dornbirn/Austria

PostPosted: Fri Jun 13, 2003 9:04 pm    Post subject: Reply with quote

Hi GuidoJ

Thanks for replying 8)

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
View user's profile Send private message
far
Guru
Guru


Joined: 10 Mar 2003
Posts: 394
Location: Stockholm, Sweden

PostPosted: Fri Jun 13, 2003 11:07 pm    Post subject: Reply with quote

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
View user's profile Send private message
sulu
Guru
Guru


Joined: 21 May 2002
Posts: 399
Location: Dornbirn/Austria

PostPosted: Mon Jun 16, 2003 5:17 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum