6

I know there are a quite a few static analysis tools for C# or .Net around. See this question for a good list of available tools. I have used some of those in the past and they have a good way of detecting problems.

I am currently looking for a way to automatically enforce some locking rules we have in our teams. For example I would like to enforce the following rules:

"Every public method that uses member foo must acquire a lock on bar" Or "Every call to foobar event must be outside lock to bar"

Writing custom FxCop rules, if feasible, seems rather complex. Is there any simpler way of doing it?

Community
  • 1
  • 1
Vincent Hubert
  • 1,386
  • 9
  • 23

3 Answers3

1

Multithreading is hard. Using locks is not the only way to make operations thread-safe. A developer may use non-blocking synchronization with a loop and Interlocked.CompareExchange, or some other mechanism instead. A rule can not determine if something is thread-safe.

If the purpose of rules is to ensure high quality code, I think the best way to go about this is to create a thread-safe version of your class which is simple to consume. Put checks in place that the more-complex synchronization code is only modified under code review by developers that understand multithreading.

foson
  • 10,037
  • 2
  • 35
  • 53
  • "Put checks in place that the more-complex synchronization code is only modified under code review by developers that understand multithreading" : Actually, that is exactly what we are trying to automate, because we currently do not have the process/flow/manpower to validate each modification. We are not trying to enforce correctness, just making sure obvious errors do not slip through. – Vincent Hubert Feb 28 '12 at 15:19
1

With NDepend you could write a code rule over a LINQ query (CQLinq) that could look like:

warnif count > 0 from m in Methods where
 m.IsUsing ("YourNamespace.YourClass.foo") && ( 
   ! m.IsUsing ("YourNamespace.YourClass.bar") ||
   ! m.IsUsing ("System.Threading.Monitor.Enter(Object)".AllowNoMatch()) ||
   ! m.IsUsing ("System.Threading.Monitor.Exit(Object)".AllowNoMatch()) )
select new { m, m.NbLinesOfCode }

Basically it will matches methods that uses the field foo, without using the field bar, or without calling Monitor Enter or Exit. This is not exactly what you are asking for, since you want lock explicitely on bar, but this is simple and quite close.

Notes that you can also write...

m.AssignField("YourNamespace.YourClass.foo")

... to restrict a specific write/assign field usage on foo.

Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
0

One of possible solutions could be implementation of Code Contracts. You define rules, run them at compile time (so can be also integrated in your CI environment if any) and get results.

For en example of using CodeContracts like a tool for code static analys see :

Static Code Analysis and Code Contracts

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • 1
    How would you write a contract that prohibits access to a certain filed unless a lock is taken? – svick Feb 27 '12 at 17:04
  • @svick: locking doesn't necessary mean you use `lock` statement. You can use other type `sync` objects in order to be able to check if object is signaled/locked/... whatever. – Tigran Feb 27 '12 at 18:58
  • OK, but still, how do you write a code contract that checks every access to the field? – svick Feb 27 '12 at 19:03
  • @svick: you can, for example: 1. Define a property and not field 2. You can on compilation inject IL that fires an event on every `__get` field access and check your rule. – Tigran Feb 27 '12 at 19:20