Detecting the call stack depth
by steveukx
HTML
<h1>Call Stack Depth Detection</h1>
<p>Use a try/catch block to wrap the entire call-stack depth detection</p>
<input type="button" value="attack stack with try catch" onclick="CallStackStretcher()" />
<p>Use a try/catch block in each step of the call-stack depth detection process</p>
<input type="button" value="attack stack with many try catches" onclick="DefensiveCallStackStretcher()" />
<p>Do not use any try/catch blocks in the call-stack depth detection - allows the exception to be thrown but has a one second timeout to check how far the process got before it threw.</p>
<input type="button" value="attack stack without try catch" onclick="RawCallStackStretcher()" />
CSS
* { font: 10pt/1.5 verdana, helvetica, sans-serif; }
h1 { font-size: 14pt; margin: 0.5em 0; padding: 0; font-family: "Lucida Sans Unicode"; font-weight: normal; }
p { margin: 1.5em 0 0 0; }
input { background: transparent; text-decoration: underline; color: #00f; border: none; cursor: pointer; }
input:hover { color: navy; }
JavaScript
var CallStackStretcher = (function() {
var testCount = 0;
return function() {
var iterations = 0;
function foo() {
iterations++;
foo();
};
try {
foo();
}
catch (e) {
testCount += 1;
var notify = 'test ' + testCount + ' = ' + iterations;
alert(notify);
}
}
}());
var DefensiveCallStackStretcher = (function() {
var testCount = 0;
return function() {
var myTestCount = testCount++;
var iterations = 0;
function foo() {
try {
iterations++;
foo();
}
catch(e) {
testCount += 1;
var notify = 'test ' + testCount + ' = ' + iterations;
alert(notify);
}
};
foo();
}
}());
var RawCallStackStretcher = (function() {
return function() {
var iterations = 0;
function foo() {
iterations++;
foo();
};
window.setTimeout( function() {
var notify = 'iterations: ' + iterations;
alert(notify);
}, 1000);
foo();
}
}());