1

I am trying to see if SAL can be used to detect concurrency bugs. I prepared a project according to Sample C++ project for code analysis and tried the sample code on Code analysis for C/C++ warnings.

However, I am not getting the warnings that should appear in some of the sample codes. Specifically, C26100, C26101、C26110、C26111、C26112、C26115、C2611 and C26140 get warnings, but C26105, C26116, C26130, C26135, C26160, C26165, C26166 and C26167 do not.

For C26105, we tried the following code on C26105

#include <sal.h>
#include <Windows.h>

_Create_lock_level_(MutexLockLevel);
_Create_lock_level_(TunnelLockLevel);
_Create_lock_level_(ChannelLockLevel);
_Lock_level_order_(MutexLockLevel, TunnelLockLevel);
_Lock_level_order_(TunnelLockLevel, ChannelLockLevel);
_Has_lock_level_(MutexLockLevel) HANDLE gMutex;

struct Tunnel
{
    _Has_lock_level_(TunnelLockLevel) CRITICAL_SECTION cs;
};

struct Channel
{
    _Has_lock_level_(ChannelLockLevel) CRITICAL_SECTION cs;
};

void OrderInversion(Channel* pChannel, Tunnel* pTunnel)
{
    EnterCriticalSection(&pChannel->cs);
        // I expect Warning C26105, but no warning.
    WaitForSingleObject(gMutex, INFINITE);
    EnterCriticalSection(&pTunnel->cs);
    LeaveCriticalSection(&pTunnel->cs);
    LeaveCriticalSection(&pChannel->cs);
}

Why is this? If you have any information, I would appreciate it if you could let me know.

My environment is as follows.

  • Windows 10 Pro 21H2 19044.1826
  • Visual Studio Community 2022 Version 17.2.6
minguu42
  • 11
  • 1

1 Answers1

0

1.Set the Code Analysis rules: Microsoft All rules in properties.

enter image description here

2.Enable Code Analysis on Build:Yes enter image description here

Error List: enter image description here

Minxin Yu - MSFT
  • 2,234
  • 1
  • 3
  • 14
  • Thank you for the information. However, I changed the Code Analysis rules to `Microsoft All rules` and still did not get the warning. – minguu42 Aug 09 '22 at 04:57
  • Hi,@minguu42 I updated answer. Enable Code Analysis on Build:Yes – Minxin Yu - MSFT Aug 09 '22 at 07:22
  • Thanks @Minxin Yu - MSFT for the additional information. I have tried that and no warning is given. [Issue is up in the developer community](https://developercommunity.visualstudio.com/t/SAL-annotations-does-not-give-C26100-or/10112284) and current status is Under Consideration. – minguu42 Aug 10 '22 at 01:48