Nicolas Cannasse schrieb:
> Adrian Veith a écrit :
>> Hello Nicolas,
>>
>> I found the error. Its in neko_vm_alloc:
>>
>>   vm->c_stack_max = (void*)(((int_val)&vm) - (stack_size - 0x10000));
>>
>> when allocating a vm and the stack is lower than stack_size, c_stack_max
>> has an integer overflow. the next call to val_callEx will fail, even if
>> there is enough stack. On Windows stack_size is assumed to be at last
>> 1MB (which is quite a lot if you have a lot of threads). I suggest, that
>> stack_size is down sized if there is an overflow in c_stack_size and
>> that c_stack_size is min 0x10000
>>
>> now I help myself with a higher default stack for my application.
>
> The idea is to make sure that we have at least 64KB stack free, for
> the callstack between startup and first VM call and for the stack
> needed to throw the exception in case an overflow occurs. That is
> indeed quite a lot but hasn't caused any issue so far. We could indeed
> reduce it.
>
> What is the allocated stack_size for your threads ?
>

The problem is not the 64KB, its the 1MB, which is the default
stack_size for Windows. If the actual stack at creation time of the vm
is lower than (1MB -64kb) than result for c_stack_max is negative.

ESP: 0x50000 at time of call of neko_vm_alloc - enough stacksize

than c_stack_vm gets: 0x50000 - (0x100000 - 0x10000) = 0xFFAF000


on call of val_callEx you check:

    if( (unsigned)((int_val)&vm) < (unsigned)(int_val)vm->c_stack_max )
        val_throw(alloc_string("C Stack Overflow"));

ESP: 0x50000 - enough stack size

but 0x50000 < 0xFFAF000 -> Exception "C Stack Overflow"

to avoid this error I have now a minimum stack size of 2MB for my
application.

i would change code in neko_vm_alloc to something like this:

vm->c_stack_max = (void*)(((int_val)&vm) - (stack_size - 0x10000));
if (int(vm->c_stack_max) < 0) vm->c_stack_max = 0x10000;


Best,

Adrian.



--
Neko : One VM to run them all
(http://nekovm.org)

Reply via email to