valgrind error and ucontext. Why “Use of uninitialised value of size 8”?

后端 未结 2 1166
说谎
说谎 2021-02-15 14:16

I have been trying to understand why valgrind complains about \"Use of uninitialised value of size 8\" for this small test program that uses ucontexts. It is basically a program

2条回答
  •  走了就别回头了
    2021-02-15 15:09

    You must notify valgrind about the stack's change. Read an example here https://github.com/lu-zero/valgrind/blob/master/memcheck/tests/linux/stack_changes.c

    This is the correct code:

    #include 
    #include 
    #include 
    #include 
    #include 
    
    #define STACK_SIZE   (8*1024)
    
    int n_ucs = 1;
    int max_switchs = 10;
    int n_switchs = 0;
    int tid = 0;
    
    ucontext_t *ucs;
    static ucontext_t engine_uc;
    
     void func(int arg)
    {
        while (n_switchs < max_switchs) {
            int c_tid = tid;
            int n_tid = (tid + 1) % n_ucs;
            n_switchs++;
            tid = n_tid;
            swapcontext(&ucs[c_tid], &ucs[n_tid]);
    
        }
    }
    
    int main(int argc, char **argv)
    {
        if (argc > 1)
            n_ucs = atoi(argv[1]);
        if (argc > 2)
            max_switchs = atoi(argv[2]);
    
        ucs = malloc(sizeof(ucontext_t) * n_ucs);
    
        //store the VALGRIND_STACK_REGISTER return values
        int* valgrind_ret = malloc(n_ucs*sizeof(int));
    
        int i;
        for (i = 0; i < n_ucs; i++) {
            /* Create initial ucontext_t, including stack */
            getcontext(&ucs[i]);
    
            //pass stack to valgrind
            void* mystack = malloc(STACK_SIZE);
            VALGRIND_STACK_REGISTER(mystack, mystack + STACK_SIZE);
    
            ucs[i].uc_stack.ss_sp = mystack;
            ucs[i].uc_stack.ss_size = STACK_SIZE;
            ucs[i].uc_stack.ss_flags = 0;
            ucs[i].uc_link = &engine_uc;
            makecontext(&ucs[i], (void (*)())func, 1, i);
        }
    
        /* jump to the first uc */
        swapcontext(&engine_uc, &ucs[tid]);
    
        /* destroy stacks */
        for (i = 0; i < n_ucs; i++) {
            //valgrind stack deregister 
            VALGRIND_STACK_DEREGISTER(valgrind_ret[i]);
    
            free(ucs[i].uc_stack.ss_sp);
        }
        free(ucs);
        return 0;
    }
    

提交回复
热议问题