V8中的堆栈溢出错误

在运行V8代码的时候或者Nodejs等依赖于V8的时候,可能遇到如下错误:

Maximum call stack size exceeded

这个错误是由于V8的堆栈溢出了,V8默认的堆栈大小是512k,如果超过了就会溢出。

在运行Nodejs的时候,我遇到了这个问题,从下面的文章找到了线索:

http://semoon1314.blog.163.com/blog/static/13665733520117315324304/


说明:

这个文章说需要修改V8的代码,重新编译,其实是不需要的,nodejs的node提供了一个选项--max-stack-size就是用于设置V8的堆栈大小的,在node.cc中可以找到相关代码的实现。


http://fw.hardijzer.nl/?p=97

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 0×60000 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 0×60000) 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);

articles:

http://www.cnblogs.com/sweetwxh/archive/2011/09/11/2173596.html
http://fw.hardijzer.nl/?p=97
http://semoon1314.blog.163.com/blog/static/13665733520117315324304/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值