V8 crashing with ‘Uncaught RangeError: Maximum call stack size exceeded’

While working with V8 in another process, it kept crashing at initialization with the message ‘Uncaught RangeError: Maximum call stack size exceeded’. I wasn’t doing anything big, just initialization.
After about 3 hour of debugging, I found out what was wrong. Normally, V8 will assume that you have no more than 512KB of stack space. To prevent stack overflows, it will take a stack address, substract 512kb from it, and remember that address. If it ever passes that address, it’ll throw a RangeError.

The problem lies in the fact that the program I was working with had a stack somewhere around 0x60000 or 384kb. V8 then substracts 512 from that, but instead of getting a nice stack-boundary, it ends up with an incredibly big number due to integer underflow. The next time it checks the stack, it compares the stack address (still somewhere around 0x60000) with it’s calculated stack limited (which, due to the underflow, is about 0xFFFE0000), and assumes it has a stack overflow.

To fix this, I had to manually set the stack limit. Basically, the following code takes a random stack address, divides it by 2, and uses that as the lower-limit to the stack:

	v8::ResourceConstraints rc;
	rc.set_stack_limit((uint32_t *)(((uint32_t)&rc)/2));
	v8::SetResourceConstraints(&rc);

One thought on “V8 crashing with ‘Uncaught RangeError: Maximum call stack size exceeded’”

Leave a Reply

Your email address will not be published. Required fields are marked *