Bug with InterlockedExchange() or possibly compiler?

Doron, folks,

Ooops, I’m so sorry about the confusion. Until this moment I didn’t
understand that the DDK came with its own compiler. I’ve always had the
Visual tools loaded on systems where I’ve done driver work and I assumed
that that compiler was used. Silly me!

So, I misstated. The InterlockedXxxx() problem of testing the result
value against zero occurs on my system with the Windows DDK 3790.1830.
Our specific example was built with a Windows Server 2003 Checked x86
Build Environment command window. cl reports the following version:

C:\WINDDK\3790~1.183>cl.exe
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.4035 for
80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

I should note that the Studio .NET 2003 compiler on my box actually
appears to be older, if I’m understanding these revs correctly (vc7’s
cl.exe shows 13.10.3077). But let me clarify, that’s *not* the compiler
I am using. I’m using the DDK compiler (13.10.4035).

I didn’t try any of this with earlier DDKs. If you guys think narrowing
the problem down that way is useful, let me know what you think I should
test. As based on what Don W. was saying earlier, I suspect this
problem could be present back a ways…

Oh, one other thing (good news), I just compiled this for x64, and the
problem doesn’t seem to exist in that environment! The compiler *does*
emit a cmp instruction before the jne. Note, the DDK compiler rev for
x64 is 14.00.40310.41. So, perhaps the problem was fixed in major rev
14 of cl?? Anyway, here is the x64 code, for reference, see how offset
1103 differs from the x86 version:

; 3324 :
; 3325 : ULONG prevState;
; 3326 : prevState = InterlockedOr(&pCmdBlock->State, 0x4);

010bb 48 8b 84 24 78
01 00 00 mov rax, QWORD PTR pCmdBlock$[rsp]
010c3 48 83 c0 4c add rax, 76 ;
0000004cH
010c7 48 89 84 24 40
01 00 00 mov QWORD PTR tv849[rsp], rax
010cf 48 8b 84 24 40
01 00 00 mov rax, QWORD PTR tv849[rsp]
010d7 0f 0d 08 prefetchw BYTE PTR [rax]
010da 48 8b 8c 24 40
01 00 00 mov rcx, QWORD PTR tv849[rsp]
010e2 8b 01 mov eax, DWORD PTR [rcx]
$xxxxx@CompleteSc@2:
010e4 8b d0 mov edx, eax
010e6 83 ca 04 or edx, 4
010e9 48 8b 8c 24 40
01 00 00 mov rcx, QWORD PTR tv849[rsp]
010f1 48 8b 8c 24 40
01 00 00 mov rcx, QWORD PTR tv849[rsp]
010f9 f0 0f b1 11 lock cmpxchg DWORD PTR [rcx], edx
010fd 75 e5 jne SHORT $xxxxx@CompleteSc@2
010ff 89 44 24 58 mov DWORD PTR prevState$29717[rsp], eax

; 3327 : if (prevState == 0)

01103 83 7c 24 58 00 cmp DWORD PTR prevState$29717[rsp], 0
01108 75 0c jne SHORT $xxxxx@CompleteSc@2

; 3328 : {
; 3329 : KdPrint((“HUH!\n”));

0110a 48 8d 0d 00 00
00 00 lea rcx, OFFSET
FLAT:??xxxxx@xxxxx@EAGOAJOD@?4textHUH?$CB?6?$AA@
01111 e8 00 00 00 00 call DbgPrint
$xxxxx@CompleteSc@2:

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Doron Holan
Sent: Friday, May 19, 2006 12:54 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] Bug with InterlockedExchange() or possibly
compiler?

Any reason why you aren’t using the DDK’s cl.exe?

– I can spell, I just can’t type.