Page 1 of 1

GDT Expand-down and up

Posted: Wed Dec 12, 2007 7:33 am
by matias_beretta
GDT: What's the difference between expand-down and expand-up??

Posted: Wed Dec 12, 2007 7:51 am
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]

Reply

Posted: Wed Dec 12, 2007 8:21 am
by matias_beretta
thanks, but which should i use in a data segment?

Posted: Wed Dec 12, 2007 9:01 am
by JamesM
Are you planning to expand it?

Reply

Posted: Wed Dec 12, 2007 9:04 am
by matias_beretta
'Expand' = Add more descriptors?

Posted: Wed Dec 12, 2007 9:34 am
by JamesM
The answer is "expand-up".

Posted: Wed Dec 12, 2007 10:42 am
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...