Browse Source

Coccinelle: add a script that checks for mutex that are locked and unlocked in a function, but not unlocked when treating error cases.

Cyril Roelandt 13 years ago
parent
commit
ab1f11fd5e

+ 73 - 0
tools/dev/experimental/not_unlocked_mutex.cocci

@@ -0,0 +1,73 @@
+/*
+ * StarPU --- Runtime system for heterogeneous multicore architectures.
+ *
+ * Copyright (C) 2012 inria
+ *
+ * StarPU is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at
+ * your option) any later version.
+ *
+ * StarPU is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * See the GNU Lesser General Public License in COPYING.LGPL for more details.
+ */
+virtual context
+virtual org 
+virtual patch
+virtual report
+
+@initialize:python depends on report || org@
+msg="The mutex \"%s\" is not unlocked when leaving \"%s\""
+
+@r@
+expression E;
+identifier func;
+position p;
+@@
+func(...)
+{
+<...
+_STARPU_PTHREAD_MUTEX_LOCK(E);
+<... when != _STARPU_PTHREAD_MUTEX_UNLOCK(E)
+ if (...)
+{
+... when != _STARPU_PTHREAD_MUTEX_UNLOCK(E)
+return@p ...;
+}
+...>
+_STARPU_PTHREAD_MUTEX_UNLOCK(E);
+...>
+}
+
+@depends on r && context@
+position r.p;
+@@
+* return@p ...;
+
+@script:python depends on r && org@
+ps << r.p;
+f << r.func;
+E << r.E;
+@@
+for p in ps:
+	coccilib.org.print_todo(p, msg % (E, f))
+
+
+@depends on r && patch@
+expression r.E;
+position r.p;
+@@
++ _STARPU_PTHREAD_MUTEX_UNLOCK(E);
+return@p ...;
+
+@script:python depends on r && report@
+ps << r.p;
+f << r.func;
+E << r.E;
+@@
+for p in ps:
+	coccilib.org.print_todo(p, msg % (E, f))
+

+ 53 - 0
tools/dev/experimental/not_unlocked_mutex_test.c

@@ -0,0 +1,53 @@
+/*
+ * StarPU --- Runtime system for heterogeneous multicore architectures.
+ *
+ * Copyright (C) 2012 inria
+ *
+ * StarPU is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at
+ * your option) any later version.
+ *
+ * StarPU is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * See the GNU Lesser General Public License in COPYING.LGPL for more details.
+ */
+static pthread_mutex_t m;
+
+int 
+bad_0(void)
+{
+        blah();
+        _STARPU_PTHREAD_MUTEX_LOCK(&m);
+
+        if (err)
+                return 1;
+
+        if (err2)
+                return 2;
+
+        if (err3)
+        {   
+                lol();
+                return 3;
+        }   
+
+        _STARPU_PTHREAD_MUTEX_UNLOCK(&m);
+}
+
+int
+good_0(void)
+{
+        _STARPU_PTHREAD_MUTEX_lock(&m);
+
+        if (brol)
+        {   
+                _STARPU_PTHREAD_MUTEX_unlock(&m);
+                return;
+        }   
+
+        _STARPU_PTHREAD_MUTEX_unlock(&m);
+}
+