0

I'm running an exercise to simulate threads using contexts. Solved the problem "use of uninitialised value of size 8" but still get the error:

valgrind ./s --track-origins=yes
==7790== Memcheck, a memory error detector
==7790== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==7790== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==7790== Command: ./s --track-origins=yes
==7790== 
main: inicio
ping: inicio
ping0
pong: inicio
pong0
ping1
pong1
ping2
pong2
ping3
pong3
ping4
pong4
ping5
pong5
ping6
pong6
ping7
pong7
ping8
pong8
ping9
pong9
ping: fim
pong: fim
main: fim
==7790== Conditional jump or move depends on uninitialised value(s)
==7790==    at 0x483C9F5: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==7790==    by 0x10A0A4: SOLUTION::CPU::Context::~Context() (in /home/allan/Documents/Sistemas Operacionais/SO/1_Trabalho/s)
==7790==    by 0x10975A: SOLUTION::Main::run() (in /home/allan/Documents/Sistemas Operacionais/SO/1_Trabalho/s)
==7790==    by 0x1095AF: main (in /home/allan/Documents/Sistemas Operacionais/SO/1_Trabalho/s)
==7790== 
==7790== 
==7790== HEAP SUMMARY:
==7790==     in use at exit: 0 bytes in 0 blocks
==7790==   total heap usage: 7 allocs, 7 frees, 204,680 bytes allocated
==7790== 
==7790== All heap blocks were freed -- no leaks are possible
==7790== 
==7790== Use --track-origins=yes to see where uninitialised values come from
==7790== For lists of detected and suppressed errors, rerun with: -s
==7790== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

The main code is:

[...] //not important part, just some cout
        ContextMain = new CPU::Context();
        ping_name = "ping";
        pong_name = "pong";

        ping = new CPU::Context(func_ping, (char *) ping_name.data());
        pong = new CPU::Context(func_pong, (char *) pong_name.data());

        CPU::switch_context(ContextMain, ping) ;
        CPU::switch_context(ContextMain, pong) ;

        std::cout << "main: fim\n";

        delete ContextMain;
        delete ping;
        delete pong;

And the Constructor and Destructor of Context class

Context(void (* func)(Tn ...), Tn ... an) {

                void *stack;
                //Setting up _context
                getcontext(&_context);

                stack = malloc(STACK_SIZE);

                valcontext = VALGRIND_STACK_REGISTER(stack, ((uint8_t*)stack) + STACK_SIZE);

                _context.uc_link            = 0;
                //_context.uc_stack.ss_sp     = new char [STACK_SIZE]; //Can we use 'new' and 'delete'?
                _context.uc_stack.ss_sp     = stack; //Can we use 'new' and 'delete'?
                _stack = (char*) _context.uc_stack.ss_sp;
                _context.uc_stack.ss_size   = STACK_SIZE;
                _context.uc_stack.ss_flags  = 0;

                //Attaching func to _context
                makecontext(&(_context), (void (*) (void))func, sizeof...(an), an ...);
            }
CPU::Context::~Context()
{
    //delete _context.uc_stack.ss_sp;
    free(_context.uc_stack.ss_sp);
    //delete _stack; ??
    VALGRIND_STACK_DEREGISTER(valcontext);
}

As i am using an external library (ucontext.h) and cannot initialize the [...].uc_stack.ss_sp, what I could do to solve the error?

HydroBR
  • 1
  • 1
  • It's hard to tell what goes wrong, because we don't see all the code. What does `CPU::switch_context()` do for example, and what is the variant of the constructor of `Context` that doesn't take any arguments? – G. Sliepen Sep 17 '20 at 19:35
  • Maybe a [Rule of Three](https://stackoverflow.com/q/4172722/10077) problem? – Fred Larson Sep 17 '20 at 19:37
  • Just out of the blue, but does the external library modifies the `uc_stack.ss_sp` pointer? PS: You could use `new char [STACK_SIZE]` and `delete` it later. This is actually the intended way of doing it in C++. – WolverinDEV Sep 17 '20 at 21:58
  • 1
    Here goes the [library documentation](https://pubs.opengroup.org/onlinepubs/7908799/xsh/ucontext.h.html) @G.Sliepen – HydroBR Sep 17 '20 at 22:33
  • The default constructor is just ` Context() { _stack = 0; } ` – HydroBR Sep 17 '20 at 22:34
  • So maybe you should call `free(_stack)` instead of `free(_context.uc_stack.ss_sp)`? – G. Sliepen Sep 18 '20 at 07:29
  • What happens if you DEREGISTER before the free? – Paul Floyd Sep 23 '20 at 08:15

0 Answers0