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
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.