lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wednesday 14, Tony Finch wrote:
> On Tue, 13 Apr 2010, Robert G. Jakabosky wrote:
> > It isn't pure C code, since it has to used assembly to switch C-stacks,
> > but it might work for you.
>
> http://coco.luajit.org/portability.html
>
> You can manipulate the stack pointer in C without mucking around inside
> the guts of a jmp_buf using alloca() or C99 variable size arrays. There
> are some toy example coroutine implementations using this idea at
> http://dotat.at/cgi/git?p=picoro.git;a=heads

That method provides no protection against stack overflows in the coroutines, 
since it is just reserving space on the main C-stack for all the coroutine 
stacks.  All those stacks are back-to-back, overflowing one stack will 
corrupt another stack.  Also you are limited to about 510 (tested on Linux 
2.6.28) coroutines with 16K stacks before you run out of space on the main 
C-stack and crash.

Using the jmp_buf hacks or assembly code, allows the program to allocate the 
stacks using malloc() or mmap().  With mmap() it is possible to add red-zones 
before & after the stacks to keep stack overflows from silently corrupting 
memory.  Better to crash on a memory access error, then to allow memory 
corruption.

-- 
Robert G. Jakabosky