SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old January 20th, 2003, 12:25 AM
bsuribabu bsuribabu is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Hyderabad
Posts: 17 bsuribabu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to bsuribabu
Static Variables

Hi all,

where static variables will be stored , in data segment or stack segment or in Heap?


Suri

Reply With Quote
  #2  
Old January 20th, 2003, 11:37 AM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
i guess it depends on your OS/compiler combo...
but never in the stack segment!

since you talk about segments, it is 16bit code, no?

for 32bit, afaik all data goes on the heap and you could say "data segment == codesegment == heap".

[disclaimer]
i am not a compiler pro, so this might be completely wrong...
[/disclaimer]

anyway, why do you care?
__________________
--
Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more.

Reply With Quote
  #3  
Old January 20th, 2003, 01:59 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,430 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 21 h 41 m 55 sec
Reputation Power: 784
Static variables go to your data segment, along with your global variables. The stack segment is for local non-static variables and arguments passed to functions. The heap is used for carving out dynamically allocated variables (for example, when you use malloc(), calloc(), strdup() etc.)

As MHirsch pointed out, on a 32 bit compiler, segments don't really exist because of the flat memory model. However, something similar does exists, they are called sections. Most object files have at least 3 sections within them (i.e.)
data: for initialized global variables
bss: for uninitialized global variables
text: for the actual code itself.
With a 16 bit compiler, each section may occupy its own segment in memory, depending on the compiler settings. BTW the above section info holds true for both gcc and turbo-c. You can see what it puts in each section by generating a map file when you compile a program. In gcc, the option goes something like -Wl,-Map,foo.map and in turbo-c, it is an option in the menu somewhere.

The linker takes all the obj files and merges the sections individually (i.e. all text sections together, all data sections together etc.) to produce the final executable.

As an aside, in OpenBSD, they are making an effort in the upcoming versions to mark the data, heap and stack areas of a program in memory as non-executable. This will reduce attacks caused by buffer overflows. Some CPUs (such as Sparc and Alpha) support this natively, but Intels don't and so this has to be handled by the OS.

Last edited by Scorpions4ever : January 20th, 2003 at 02:12 PM.

Reply With Quote
  #4  
Old January 20th, 2003, 10:42 PM
bsuribabu bsuribabu is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Hyderabad
Posts: 17 bsuribabu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to bsuribabu
Hi Scorpion4server,

Thanks for great explanation . Thanks for ur support .

as per u r reply :

In OpenBSD, they are making an effort in the upcoming versions to mark the data, heap and stack areas of a program in memory as non-executable. This will reduce attacks caused by buffer overflows.

Actually i haven't undestood it properly . Can u clarify me in detail .



Suri

Reply With Quote
  #5  
Old January 21st, 2003, 01:12 AM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
i wonīt explain the details here, but:

if you donīt do correct input buffer length checking, one could send a string of 1000 chars into a variable of size 100. once pushed on the stack, it will overwrite the back-jump address of the calling function. if you do this "correctly", it will execute the program code that you put into the string.... HACKED! This is called a "buffer overflow"

to prevent this, some OSs donīt allow code on the stack to be executed. (for linux there is a patch and a kernel module that can do the same. but i read somewhere that there is programs that rely on code on the stack being executable - bad programming...)

Reply With Quote
  #6  
Old January 21st, 2003, 07:09 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,430 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 21 h 41 m 55 sec
Reputation Power: 784
Yep, M. Hirsch described the situation pretty accurately there. For a more technical article, check out http://www.insecure.org/stf/smashstack.txt

Reply With Quote
  #7  
Old January 21st, 2003, 01:40 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
i didnīt feel like posting that kind of info here. but now that i think about it...
I bet there is few script kiddies here and more admins/programmers that will take advantage of this kind of information.

Iīll start a post in the lounge to see what people think about it...

Reply With Quote
  #8  
Old January 23rd, 2003, 10:11 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
> if you donīt do correct input buffer length checking, one could send a string of 1000 chars into a variable of size 100

i thought that's exactly what causes a segmentation fault error? why doesn't that cause a a segmentation fault?

Reply With Quote
  #9  
Old January 24th, 2003, 12:45 AM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
segfaults occur if you have an invalid address, but you supply the right "code" and the address is valid since code on the stack or in the data "segment" is allowed. Thatīs the trick. Of course, after the program executed your code, it will probably hang, quit or segfault. But then itīs too late

Last edited by M.Hirsch : January 24th, 2003 at 12:47 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Static Variables


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway