2

I've been given an assignment so simulate a user-level thread library using signals, setjmp and longjmp c functions. Basically, the program include memory allocation for each 'thread', and using long jump and signals to simulate non-direct code flow and preemption.

When I run the valgrind tool, I get messages in the following form: """

Invalid write of size 8
==19100== at 0x560020F: __sigsetjmp (setjmp.S:36)
==19100== by 0x403EC3: switchThreads(bool, bool, bool) (uthreads.cpp:673)
==19100== by 0x403DE6: signalHandler(int) (uthreads.cpp:652)
==19100== by 0x56004EF: ??? (in /lib/x86_64-linux-gnu/libc-2.13.so)
==19100== by 0x404D93: t1() (tal3.cpp:23)
==19100== Address 0x5959c90 is 48 bytes inside a block of size 4,312 alloc'd

==19100== at 0x4C2851B: operator new(unsigned long, std::nothrow_t const&) (vg_replace_malloc.c:316)

==19100== by 0x402E67: uthread_spawn(void (*)()) (uthreads.cpp:358)
==19100== by 0x404DD6: main (a3.cpp:41)

"""

I've read some forums and documentation of this tool, and from my understanding, it seems the long jumps and 'setjmp' (as long as signal handling jumps) are not supported in valgrind, thus cause what seems to be as memory problems - but I couldn't come up with a definite answer.

Would appreciate your help. Thanks.

alk
  • 69,737
  • 10
  • 105
  • 255
Alonbs
  • 259
  • 1
  • 11
  • What happens here: `t1() (tal3.cpp:23)`? Sources would help. Is this behaviour, and also this valgrind log reproduceable? – alk Apr 12 '13 at 14:58

1 Answers1

5

it seems the long jumps and 'setjmp' (as long as signal handling jumps) are not supported in valgrind,

More precisely: "custom" stack switching via {sig,}longjmp is not supported by Valgrind directly.

You can put special Valgrind annotations to inform Valgrind about the stack switching you are performing, but this will likely not be an easy task. See this document, and VALGRIND_STACK_REGISTER, VALGRIND_STACK_DEREGISTER and VALGRIND_STACK_CHANGE client requests in particular.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362