What is the purpose of these padding instructions in a spin-lock in a critical section on Windows?

137 Views Asked by At

I've been reverse engineering the EnterCriticalSection function on Windows 10 and found this interesting spin-loop:

enter image description here

It goes:

lbl_loop:

mov     ecx, [rsp+60h]
mov     ecx, [rsp+60h]
mov     ecx, [rsp+60h]
pause
mov     ecx, [rsp+60h]
inc     ecx
mov     [rsp+60h], ecx

cmp     ecx, eax
jb      lbl_loop

So my question is - what is the purpose of reading 4 times from [rsp+60h] and then writing back into it from a loop?

Why couldn't they just do:

lbl_loop:

pause
inc     ecx

cmp     ecx, eax
jb      lbl_loop

mov     [rsp+60h], ecx

PS. Note this is a production build of Windows 10. And the rest of the EnterCriticalSection function is optimized. So this is not a debugging build.

0

There are 0 best solutions below