tags:

views:

341

answers:

6

What is "stack thrash"? Or "a stack thrash"? (Since I don't know the definition I'm not sure if it is a countable or uncountable term.)

+1  A: 

I've never encountered the term, and the leading Google and Wikipedia hits don't describe it.

However, it seems to be analogous to disk thrash which is heavy inefficient use of a disk. That's not directly applicable to a stack—which doesn't correspond to anything mechanical. Maybe it is related to page faulting which would result in a mechanically inefficient relationship. Or stack overflow corrected by some expensive executive/kernel remedy.

Some context for the term would help.

wallyk
+1 i am generous ..:P
Neeraj
+2  A: 

I've seen this term used in the context of Forth, where the lack of stack frame access sometimes requires excessive use of stack manipulations ("thrashing the stack") to get to certain words to the top of the stack.

Also, This glossary defines it as "Frequent stack expansion (overflow) and contraction (underflow)". Clearly a definition in need of further explanation. Perhaps someone more familiar with the Cray X1 can explain.

ergosys
+1  A: 

I heard the term "stack thrashing" in the context of stack guards, but I think it's more often called "stack smashing". Stack smashing means any kind of exploiting stack vulnerabilities (like buffer overflows). That's why the GCC stack protection is called SSP (Stack-Smashing Protector).

AndiDog
"thrash" <> "smash"
talkaboutquality
A: 

I heard once "trashing the stack" (without the first h) in the sense of "corrupting the stack", e.g. when a function gets/puts more bytes from/on the stack than it should, messing up the stack pointer and making it impossible for the other function to work correctly afterwards.

It may happen when you call an external function with the wrong calling convention (e.g. when a function in a dll is stdcall but you declare it as cdecl), or when you call a variadic function like printf or scanf passing a wrong format string, so it pops more/less bytes than it could.

Matteo Italia
"thrash" <> "trash"
talkaboutquality
I know, but since I didn't ever heard that I thought that maybe he may have misheard it.
Matteo Italia
+1  A: 

I am sure that the term is entirely colloquial and has not precise meaning. Do you have a specific citation or link to where you have seen it used?

Without any precise technical definition, I suggest that it means whatever the user of the term thinks it means. It could I suppose be applied to any kind of abuse or inefficient use of a stack, but since all that typically happens in a stack manipulation is that the stack pointer gets moved it would be hard to define what would constiute 'trashing' that was not simply normal and desirable behaviour.

In a multi-threaded application where each thread has its own stack, context switching would imply rapid switching of stacks also, but that event is entirely and less ambiguously described by the term "task-thrashing".

Clifford
+3  A: 

Stack thrashing is like heap thrashing, but on the stack.

There, now that's explained.

Oh, you want more detail huh ?

If you emulate a stack based processor on a processor that isn't you're thrashing the stack.

If your C code malloc's and free's every other line of code, you're thrashing the heap.

The point of stack thrashing as a problem is that if you profiled your code, the CPU spending pretty much all it's time popping and pushing.

For heap thrashing that's malloc() & free() being your #1 & #2 most used functions.

Now some CPU's ( rockwell make some) actually are optimized to run a stack based language in hardware.

  • Internal ram that caches the top N kilobtyes of stack inside the CPU
  • Few registers
  • All instructions stack relative

Oddly enough, the Java Virtual Machine is a stack based model.

Running a really dumb FORTH implementation on x86 hardware will thrash the stack. The sort of thing you might write after reading the Forth spec, so you emit x86 machine code for forth instructions and DONT optimize it. Forth guys, I apologise, I know your implmentations are a lot better.

Postscript is stack based too, which makes early postscript printers exciting: they had limited ram and slow CPU's: and ran a stack-thrashing language. I'm sure a lot of effort went into things like the original Apple Laserwriter to make it run better. It had a Motorola 68000 CPU running at (10ish) megahertz and 1Mb of ram IIRC.

Again, stack thrashers.

Did that help ?

Tim Williscroft
If I find the original reference again, I'll give this definition a try and see if it fits. But it certainly makes sense, accepts that I wrote "thrash" and addresses the question and key word as-is, and is consistent with the English definition of the word. Thank you very much, Tim.
talkaboutquality