GDT Expand-down and up

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
matias_beretta
Member
Member
Posts: 101
Joined: Mon Feb 26, 2007 3:39 pm

GDT Expand-down and up

Post by matias_beretta »

GDT: What's the difference between expand-down and expand-up??
Matías Beretta
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

One expands downwards in memory and the other one expands upwards :twisted: - serously...

[Edit]More helpful answer: Think of a system stack as expand-down - when you add to the stack, ESP decreases. A heap, on the other hand, is more often expand-up - lower addresses will be assigned first.[/Edit]
User avatar
matias_beretta
Member
Member
Posts: 101
Joined: Mon Feb 26, 2007 3:39 pm

Reply

Post by matias_beretta »

thanks, but which should i use in a data segment?
Matías Beretta
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Are you planning to expand it?
User avatar
matias_beretta
Member
Member
Posts: 101
Joined: Mon Feb 26, 2007 3:39 pm

Reply

Post by matias_beretta »

'Expand' = Add more descriptors?
Matías Beretta
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

The answer is "expand-up".
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Post by Pype.Clicker »

the difference is in what part of the segment is valid.
expand-up segments allow offsets from 0 to limit while expand-down segments allow from limit to 0xffffffff.

Even if you don't plan to expand your segments, it still makes sense to use expand-down segments for your stacks, as it gives you a stronger way to detect stack overflow (e.g. through the Stack Fault exception).

OS that relies on page faults and try to "guess" whether the program is just needing more stack or doing an invalid access may sometimes get fooled by allocation of a large array on the stack, e.g.

Code: Select all

void f() {
    char huge[1024*1024];
    huge[1024*1024-1]='d';
}
may work on some linux distros and (surprisingly?) crash on others.

Note, though, that using expand-down stack segment behaves weirdly in the usual C memory model (assuming DS.base == SS.base), requiring setup tricks and usually meaning that you traded stack-overflow protection against stack underflow protection...
Post Reply