【发布时间】:2017-11-12 11:27:13
【问题描述】:
我目前正在编写自己的线程库。而且我无法调试未处理的异常“堆栈 cookie 检测代码检测到基于堆栈的缓冲区溢出”。下面是导致未处理异常的代码
void Scheduler()
{
void *curr_esp;
TCB* next_thread = ready_queue->data;
popFront(&ready_queue);
__asm
{
pushad
mov curr_esp, esp
}
curr_thread->esp = curr_esp;
if (curr_thread->status == RUNNING)
{
curr_thread->status = READY;
Enqueue(curr_thread, &ready_queue);
}
curr_thread = next_thread;
if (curr_thread->status == READY)
{
curr_thread->status = RUNNING;
curr_esp = next_thread->esp;
__asm
{
mov esp, curr_esp
popad
}
}
else if (curr_thread->status == NEW)
{
curr_thread->status = RUNNING;
curr_thread->params = (curr_thread->fn)(curr_thread->params);
__asm
{
mov esp,curr_esp
}
if (curr_thread->status == RUNNING)
{
thread_exit(curr_thread->params);
}
}
}
这是执行应该运行 threadlib 和 thd_yield 的主要自旋功能,基本上只是调用我的调度程序
void *spin1(void *a)
{
int i;
for(i=0;i< 20; i++)
{
printf("SPIN1\n");
if((i+1)%4==0)
thd_yield();
}
return NULL;
}
void* spin2(void *a)
{
int i;
for(i=0;i< 20; i++)
{
printf("SPIN2\n");
if((i+1)%4==0)
thd_yield();
}
return NULL;
}
int main()
{
thread_id_ id;
thd_init();
id = new_thd(spin2, NULL);
spin1(NULL);
}
输出应该是5组4个“spin1”s和“spin2”s交替。
旋转1 自旋1 自旋1 自旋1 自旋2 自旋2 自旋2 自旋2 自旋1 ..
该代码对于前两组“spin1”和 1 工作得非常好,但在第二组“spin2”上给了我一个未处理的异常。我检查了正在存储和检索的堆栈指针,它们被正确存储和检索,链表存储和内存分配。更糟糕的是,我没有显示哪条线导致了错误。
注意:我不允许使用任何线程系统调用,它必须在 C 中。
如果有帮助,这是我的 TCB 结构
typedef struct _TCB_
{
/* Unique ID*/
thread_id_ id;
/* Thread status*/
enum ThreadState status;
/* ID of next thread*/
thread_id_ wait_id;
void *esp;
void *(*fn)(void*);
void *params;
void *stack;
}TCB;
如果我的源文件能帮助你解决这个问题,我很乐意分享。
【问题讨论】:
-
这篇文章或许能给你一些线索kallanreed.wordpress.com/2015/02/14/…
-
似乎这个问题的答案可能取决于您使用的编译器的版本以及您使用的编译选项。
标签: c multithreading assembly variable-assignment