• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
DracoLacertae

stack overflows: why not turn the stack upside down?

10 posts in this topic

We all know about stack overflows.  You overwrite your buffer on the stack, and you are now trashing another buffer, or even worse, the return address.  Then when 'ret' (I'm using x86 lingo here), runs, the cpu jumps to a bad place.  This is because on most platforms the stack grows downward.  When you exceed your stack frame's bouundary, you start tracking the previous stack frame.

 

This downward growth is mostly hisorical.  On older computers, before virtual memory, your stack started at the top of RAM, and grew downwards.  The heap started somewhere else and grew up.  You ran out of memory when the two pointer met.  With virtual memory, you can put the stack 'anywhere' and even grow it on a page fault in some cases.  And I think it's time to turn the stack around:

 

 

Suppose f() calls g() and that calls h():

classic stack:  'R' is where the return address goes:
low addr  [empty-----------------R][--f--]   high addr
low addr  [empty------------[--g-R][--f--]   high addr
low addr  [empty-----[--h-R][--g-R][--f--]   high addr

How H overflows:

low addr  [empty-----[--h--*R*****][--f--]   high addr


we crash, or worse, run an explot from h inserting an return address to who-knows-where


upside-down stack:
low addr  [--f--][-empty------------------]   high addr
low addr  [--f--][R-g--][empty-----------]   high addr
low addr  [--f--][R-g--][R-h--][empty----]   high addr

Now H overflows:
low addr  [--f--][R-g--][R-h***********--]

We've overflowed into empty space.  Return address and previous stack frames are safe

This wouldn't be too hard to do.  Most stack access during a function is done pointer arithmetic:

mov eax, [esp-4]

mov eax, [esp+4]

 

I know there are some hardware platforms that already have stacks that grow upwards.  Redefining the ABI for x86 would break using standard libraries, but for inside your own application, this might enhance security.  I suppose its even possible to use an 'upwards' stack for your application, and then when you call a 3rd party library, switch the stack pointer to a seperate area where you have a standard downwards stack defined.   I imagine this would involve a lot of hacking around in gcc or llvm to make it work.  In an open OS like linux, maybe you could recompile the whole system to use upwards stacks. 

 

Just a thought.  Downvote if it stinks! 

 

 

 

 

 

 

 

1

Share this post


Link to post
Share on other sites

The heap grows the other way though... stack at one end, heap at other, of addressable memory. When they meet in the middle it's Bummed In The Gob Time (R)

0

Share this post


Link to post
Share on other sites

Redefining the ABI for x86.


The ABI is partly defined by the hardware itself. call, push, pop, etc. explicitly modify ESP to go in a certain direction. Not only would a compiler have to use different opcodes, they'd very likely be less efficient.
1

Share this post


Link to post
Share on other sites
(Singing) "Whichever way the stack grows doesn't really matter to me!"


The heap grows the other way though... stack at one end, heap at other, of addressable memory. When they meet in the middle it's Bummed In The Gob Time (R)


This doesn't apply to many modern systems. Consider that each thread in a multithreaded system needs its own stack.
1

Share this post


Link to post
Share on other sites
There's also the whole split stack thing that some runtimes can do (if the stack bounds are exceeded, new pages are allocated somewhere else and life carries on). This is mostly done for microthreads/coroutines that only need a single page of stack (usually) due to the performance overhead incurred by all the stuff necessary to actually use split stacks.
0

Share this post


Link to post
Share on other sites

 

(I'd even go so far as to say putting buffers on the stack at all is something to avoid.)

I like this.

 

I also like the idea of seperate data and program control stacks.  I've worked with FORTH, and there's two parallel stacks.  That wouldn't be hard to do with x86 either.  Call and ret can use the hardware stack, and it's downward growth wouldn't be a concern.  Then argument passing and temporary data would be on a completely different stack, one that has nothing to do the 'esp' stack pointer.  Actually, the data stack could just be managed using the 'ebp' pointer; you don't really need a base pointer anyway.  You sort of do to make debugging easier, but you can get by without it.

 

 

The ABI is partly defined by the hardware itself. call, push, pop, etc. explicitly modify ESP to go in a certain direction. Not only would a compiler have to use different opcodes, they'd very likely be less efficient.

 

On x86 I don't think it would be too bad, at least for push/pop.   This page puts them at about 1% of each other.  http://qinsb.blogspot.com/2011/03/x86-register-save-performance.html

Doing your own call and return might be a little bit of a hit, but I hope most of the program's time is spent doing something other than function call overhead.

 

But, I've found something that makes the whole thing moot.  Having an upwards growing stack is not the panacea of stack buffer overruns.  from

 

the tl;dr is that you can allocate a buffer too small on the stack, and then call a function that's supposed to write into that buffer.  As it writes into the previous frame's buffer, it will blast past the buffer and trash its own, including the return address.

0

Share this post


Link to post
Share on other sites

 

(I'd even go so far as to say putting buffers on the stack at all is something to avoid.)

I also like the idea of seperate data and program control stacks.  I've worked with FORTH, and there's two parallel stacks.  That wouldn't be hard to do with x86 either.  Call and ret can use the hardware stack, and it's downward growth wouldn't be a concern.  Then argument passing and temporary data would be on a completely different stack, one that has nothing to do the 'esp' stack pointer.  Actually, the data stack could just be managed using the 'ebp' pointer; you don't really need a base pointer anyway.  You sort of do to make debugging easier, but you can get by without it.

Having your own stack, separate from "the stack" is quite common in the game engines that I've used. It's easier to catch bugs, because you can verify stack contents yourself (e.g. memsetting and memcmping regions with marker patterns at key moments), and if there are bugs you don't have the traditional problem of "I'm trying to decypher this crash dump, but I can't get a call-stack because it's corrupted". It's also ridiculously fast to allocate from (when you strip all your debugging code from the shipping build) and fast to deallocate (opting in to stack-unwinding when required).

I wrote a bit about my implementation here: http://eighthengine.com/2013/08/30/a-memory-management-adventure-2/

Edited by Hodgman
2

Share this post


Link to post
Share on other sites

Stack overflows are actually underflows, since the stack pointer goes down, not up. It was specifically designed that way because it's easier to let a program crash when it reaches the underflow, than to add a check for an upper-limit of the stack.

If you were to use an increasing stack pointer, you'd still need a way to detect overflows, otherwise you're just inviting hackers to use the stack for buffer-overflow type attacks.

So the "downward growth" that causes underflows is not just a historical side-effect - it is a safety feature.

 

Also, I think older programs (DOS 16 bit programs) used segments for memory access, which were the same thing as virtual memory - they certainly did not access memory using physical addresses like "the top of RAM". The stack and data segments could be placed anywhere in memory below 640K (and later 1MB with virtual x86, but never higher than that). The heap was only later introduced, and the heap memory addresses were served by a memory-manager, which usually allocated it's memory above the 1MB mark (and below,in the "upper-memory blocks"), where the stack could not reach, so the stack and heap would never intersect. The stack was limited to the size of a segment - 64k. The heap was limited to the size of extended memory + upper-memory. So this is not true: "You ran out of memory when the two pointer met". Until virtual memory came along, there was never a moment when the stack and heap shared the same address space.

Edited by tonemgub
0

Share this post


Link to post
Share on other sites

What is the actual problem you are trying to solve here? Running out of stack space seems the only one to me that would not be masking a program error of some kind. What makes you think that the default stack size on whatever hardware you are using is a problem?

0

Share this post


Link to post
Share on other sites

(Singing) "Whichever way the stack grows doesn't really matter to me!"

 

The heap grows the other way though... stack at one end, heap at other, of addressable memory. When they meet in the middle it's Bummed In The Gob Time (R)


This doesn't apply to many modern systems. Consider that each thread in a multithreaded system needs its own stack.

 

 

Good point! Drunk again ;)

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0