C 卡在do{}while(false)循环中

C 卡在do{}while(false)循环中,c,assembly,macros,operating-system,C,Assembly,Macros,Operating System,首先,这与任务有关 这是一门操作系统课程,我们应该使用光纤,这样我们的系统在进行长时间计算时能够响应。为此,我们提供了保存和恢复堆栈等功能。我们的想法是,不让For循环运行100次,而是运行一次,保存堆栈并返回,执行一些其他操作,然后恢复函数的堆栈 问题在于提供的用于保存和恢复堆栈的宏陷入了无限循环。令人惊讶的是,它是以do{}while(false)循环的形式编写的,所以这种情况甚至不应该发生 是什么导致了这一切?下面是宏: #define stack_saverestore(from_sta

首先,这与任务有关

这是一门操作系统课程,我们应该使用光纤,这样我们的系统在进行长时间计算时能够响应。为此,我们提供了保存和恢复堆栈等功能。我们的想法是,不让For循环运行100次,而是运行一次,保存堆栈并返回,执行一些其他操作,然后恢复函数的堆栈

问题在于提供的用于保存和恢复堆栈的宏陷入了无限循环。令人惊讶的是,它是以do{}while(false)循环的形式编写的,所以这种情况甚至不应该发生

是什么导致了这一切?下面是宏:

#define stack_saverestore(from_stack,to_stack) do {                  \
 asm volatile(                                                       \
   "  pushl %%eax      \n\t"                                         \
   "  pushl %%ecx      \n\t"                                         \
   "  pushl %%ebp      \n\t"                                         \
   "  pushl $1f        \n\t"                                         \
   "                   \n\t"                                         \
   "  movl  %%esp,(%0) \n\t"                                         \
   "  movl  (%1),%%esp \n\t"                                         \
   "                   \n\t"                                         \
   "  ret              \n\t"                                         \
   "1:                 \n\t"                                         \
   "  popl %%ebp       \n\t"                                         \
   "  popl %%ecx       \n\t"                                         \
   "  popl %%eax       \n\t"                                         \
  :                                                                  \
  :"a" (&from_stack), "c"  (&to_stack)                               \
  :_ALL_REGISTERS, "memory"                                          \
 );                                                                  \
} while(false)
我很确定问题不在宏本身,因为我在另一个地方使用它,没有任何问题。我调试它时遇到问题,因为宏主要是汇编代码

编辑:stack_saverestore背后的逻辑

//
// Switch stacks.
//
// Algo: 
//   1. Save _c's context to stack, 
//   2. push ip of _c's restore handler
//   3. switch stacks
//   4. execute ip of _n's restore handler to restore _n's context from stack.
//
//
// stack layout: 
//  teip[-1:-32]: continuation to restore, 
//  Stack layout expected by teip:
//     ebp[ -33: -64], 
//     ebx[ -65: -96], 
//     eax[ -97:-128], 
//     Stack layout expected by eip+4:
//        Preserved.
用法详细信息s:宏用于实现非常基本的外壳的光纤。我正在做的事情并不重要,但基本上我在添加一个大数的除数。我知道这不是最佳方式,但这不是问题所在

void fiberFactor(addr_t* pmain_stack, addr_t* pf_stack,
        shellstate_t& shellstate) {

    addr_t & main_stack = main_stack; 
    addr_t & f_stack = f_stack;
    bool& done = shellstate.fiber_done;

    int n = shellstate.factorArg;
    int i = 1;
    int sum = 0;


    for (i = 1; i < n; i++) {
        for (int j = 0; j < 1000; j++) {
            if (n % i == 0) {
                sum = sum + i;
            }
            i++;
        }
        i--;
        shellstate.fiber_done = false;
        hoh_debug("about to switch stacks, i "<<i<<sum);
        stack_saverestore(f_stack, main_stack); //Never returns from here.
    }
//The hope is that with each iteration of the outer for loop, 
//we do some computation, and then yield execution. 
//Eventually, the computation is finished, and we set the flags here,   
//and switch out for the last time.
    for (;;) {
        shellstate.fiber_done = true;
        shellstate.fiber_do = false;
        shellstate.factorVal = sum;
        stack_saverestore(f_stack, main_stack);
    }

}


//This function is called by the shell as part of the main loop. 
//If we have to do something, as indicated by the booleans, do it.
void shell_step_fiber(shellstate_t& shellstate, addr_t& main_stack,
        addr_t& f_stack, addr_t f_array, uint32_t f_arraysize) {

    if (shellstate.resetFiber) {
        shellstate.resetFiber = false;
        stack_init3(f_stack, f_array, f_arraysize, &fiberFactor, &main_stack, &f_stack, &shellstate); //Reset the stacks.
    }

    if (shellstate.fiber_do){
        stack_saverestore(main_stack, f_stack); //Switch to fiberFactor. This works without a hitch.
    }
}
void fiberFactor(addr\u t*pmain\u堆栈、addr\u t*pf\u堆栈、,
shellstate_t&shellstate){
addr_t&main_stack=主_stack;
addr_t&f_stack=f_stack;
bool&done=shellstate.fiber\u done;
int n=shellstate.factorArg;
int i=1;
整数和=0;
对于(i=1;ihoh_debug(“即将切换堆栈,我”有几件事突然向我袭来

首先,你的堆栈需要空间来存储它们的内容。如果我读得正确,你的堆栈基本上是地址,可能相隔4个字节——你试图在堆栈上存储16个字节。最终的结果是,当你写入一个堆栈时,你破坏了另一个堆栈

要纠正这一点,我建议您自己创建一个堆栈结构,它显式地为您想要保存/恢复的所有内容留出空间

typedef struct {
    uint32_t  eip;
    uint32_t  ebp;
    uint32_t  ecx;
    uint32_t  eax;
} my_stacktype_t; 
当x86上的堆栈向下扩展时,这些项的顺序与您推送的相反

下一件让我吃惊的事情是,您只保存了必要寄存器的子集。也许您的循环主体只使用了这些寄存器,但如果它发生变化,您将需要存储更多/不同的寄存器。我建议保存/恢复所有通用寄存器:eax、ebx、ecx、edx、esi、edi、ebp、esp和eip(希望我没有忘记一个——我在这里是凭记忆来的)

我必须绝对确定您的用例场景,但在堆栈上存储堆栈至少是一种代码味道。根据我的经验,堆栈通常存储为全局变量或从堆中动态分配


希望这有帮助。

结果表明错误不在循环中,而是在我的代码中-尤其是在fiberFactor中的这一行:

addr_t & main_stack = main_stack; 
这没有意义,因为传递给函数的参数是pmain_stack。更改为

addr_t & main_stack = *pmain_stack; 
f_stack也一样,解决了这个问题


归功于@DavidWolfherd发现了它。虽然为什么这不会导致错误,而是导致堆栈存储还原代码中的无限循环,但我没有任何线索。

如何定义
false
呢?我的眼睛是不是有欺骗性,或者这是隐藏在asm中的一个硬
ret
。@qrdl我认为这只是一个普通的旧布尔值。哇,是的,没错。别担心Don’别问我为什么-这段代码是给我们的,让我们按原样使用。不过我会编辑以包含对它的描述。@如果我们从宏正确的假设开始(我想我看到了它在做什么)的话,谁会看到上面的评论,那么问题可能是您调用错误,或者存储在堆栈中的信息正在损坏。您能告诉我们您是如何调用它的吗?我确实设法解决了这个问题,它与堆栈保存代码无关。此外,我知道这不是处理此问题的最佳方法,但我们不允许更改更改给定的代码,只是为了使用它。