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

后端 未结 2 1163
说谎
说谎 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:05

    I still don't exactly understand why valgrind is showing these uninitialized errors exactly but i'll give it my best shot to explain what I understood till now;

    On running and analyzing the program via valgrind and based on information from man pages of swapcontext(3) and getcontext(3), I think it is failing to detect the some context swaps (failing to see stack pointer change for swapcontext from tid 0 to tid 1 and the swapcontext from tid 1 back to tid 0)

    Read below as: who's stack[number of call]:function call

    So, I think function call trace is something like this:

    main:swapcontext(main, tid 0) ->

    main[tid 0's 1st func call]:func() ->

    tid 0:swapcontext(tid 0, tid 1) -> {Stack => tiod 0}

    tid 1:func() ->

    swapcontext(tid 1, tid 0) -> {Stack => tiod 1}

    tid 0[2nd call]: func() ->

    return immediately since n_switchs = 2 ->

    pop tid 0[2nd call]: func() stack frame from tid 1's stack -> {1st Uninitialized access according to valgrind}

    tid 0[2nd call]: func() finishes -> checks uc_link; finds engine_uc (main context) set there ->

    From here on things get unclear for me but following seems to be the likely trace:

    resets sigprocmask -> {2nd Uninitialized access} setcontext()s back to main context -> {3rd Uinitialized access ?} {Stack => main}

    On return, stack frame for [tid 0's 1st call] popped from main's stack->

    main [tid 0's 1st call]:func() finishes as well because of n_switchs = 2 -> check uc_link; finds engine_uc again -> resets sigprocmask -> {not uninitialized access ?}

    On return, stack frame for main:swapcontext() is popped from main's stack ->

    setcontext()s back to main context -> {4th Uninitialized access ?} {Stack => main}

    we come back to main(), free stuff and exit

    Some References:

    https://www.gnu.org/software/libc/manual/html_node/System-V-contexts.html http://www.cs.uwm.edu/classes/cs315/Bacon/Lecture/HTML/ch10s07.html

    Note: I know this is not a complete answer but I didn't want to post such a long explanation in comments section; hence posted here.

    0 讨论(0)
  • 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 <stdio.h>
    #include <stdlib.h>
    #include <sys/time.h>
    #include <ucontext.h>
    #include <valgrind/valgrind.h>
    
    #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;
    }
    
    0 讨论(0)
提交回复
热议问题