Hello all,

Ive got an intermittent problem and think Ive finally narrowed it down. In my library almost every function saves every register that it uses that is not also used to pass parameters, then restores them before returning. I did this to make it easier when calling a function so that the caller could expect as little state change as possible. Sometimes though, one of the volatile registers (which was saved at the beggining of the function and restored) will return changed. This problem usually happens rarely (usually taking on the order of at least a few million calls to various functions), it happens in random places after calls to apparantly random functions and I am wondering, if the system were to switch contexts say right at the point where it is returning from a function call, would it change the volatile registers?

An example:

Code:
	Function1 PROC 

		mov		r11, rcx
		call	Function2

							;<-----is it possible for r11 to change in this call even though function 2 restores r11 before returning?

		add		r11, r12

		mov		rax, r11

		RET

	Function1 endp

	Function2 PROC 

		push	r11
		mov		r11, rdx
		mov		rax, rcx
		mul		r11
		pop		r11
		RET

	Function2 endp
just some bs code to illustrate an example (I could give a concrete example, but that would be a lot more code that I thought would confuse the concept). Anyways, in the example above, r11 is used in both functions, function 2 saves r11 before using it and restores it before returning, now 99.99999999% of the time everything runs fine, but every once in a great while r11 will be changed when function1 uses it after returning from the call to function2 it will be changed.

Ive been able to trap this problem by checking r11, which in most of my functions holds a pointer to the first array passed in to a function, immediately after returning from a function call with something like this:

Code:
	Function1 PROC 

		local  _tr11:qword

		mov		r11, rcx
		mov		_tr11, r11
		call	Function2

							;<-----is it possible for r11 to change in this call even though function 2 restores r11 before returning?

		cmp		r11, _tr11
		je		ItsGood
		mov		rax, rax

ItsGood:

		add		r11, r12

		mov		rax, r11

		RET

	Function1 endp

	Function2 PROC 

		push	r11
		mov		r11, rdx
		mov		rax, rcx
		mul		r11
		pop		r11
		RET

	Function2 endp
Ill put a breakpoint on the mov rax, rax instruction so that it runs fine unless it returns from the function call changed. I cant figure out HOW its getting changed though, so I was wondering, since the system considers r11 volatile across function calls, would it save r11 if a context switch happens after completing a call to a function but before dropping back in to the calling function?

Thanks in advance.