[Lustre-discuss] Finding bugs in Lustre with Coccinelle

Isaac Huang isaac_huang at xyratex.com
Sat Jan 7 23:58:20 PST 2012


Today I decided to try Coccinelle on latest Lustre code found on
master at git://git.whamcloud.com/fs/lustre-release.git.

I came up with a simple Coccinelle script that tries to detect the
case where a new object is allocated and dereferenced without checking
it against NULL.

Eight such bugs were unconvered:
$ spatch -sp_file /tmp/LIBCFS_ALLOC.cocci -dir lnet 2>/dev/null > /tmp/lustre.diff
$ spatch -sp_file /tmp/OBD_ALLOC.cocci -dir lustre 2>/dev/null >> /tmp/lustre.diff
$ diffstat /tmp/lustre.diff
 b/llite/dir.c          |    2 ++
 b/mdc/lproc_mdc.c      |    1 +
 b/mgc/mgc_request.c    |    1 +
 b/obdclass/obd_mount.c |    2 ++
 b/selftest/conctl.c    |    1 +
 obdclass/obd_mount.c   |    1 +
 6 files changed, 8 insertions(+)

I've attached here the lustre.diff, which contained dummy fixes. I
hope they do get fixed. OBD_ALLOC.cocci is also attached.
LIBCFS_ALLOC.cocci is almost identical - I guess they could be
combined into one with some regex matching but I haven't figured out
how yet.

This is to demonstrate how useful such a tool can be. The Linux kernel
seems to have already integrated Coccinelle checks in the build
system. Lustre could adopt something similar, and Coccinelle could do
much more complex things than this.

- Isaac
-------------- next part --------------
// find calls to malloc
@call@
expression ptr;
expression E;
position p;
@@

OBD_ALLOC(ptr at p, E);

// find ok calls to malloc
@ok@
expression ptr;
expression E;
position call.p;
@@

OBD_ALLOC(ptr at p, E);
... when != ptr
(
 (ptr == NULL || ...)
|
 (ptr != NULL || ...)
)

// fix bad calls to malloc
@depends on !ok@
expression ptr;
expression E;
position call.p;
@@

OBD_ALLOC(ptr at p, E);
+ LASSERT(ptr != NULL); /* or handle the NULL case? */
-------------- next part --------------
A non-text attachment was scrubbed...
Name: lustre.diff
Type: text/x-diff
Size: 3435 bytes
Desc: not available
URL: <http://lists.lustre.org/pipermail/lustre-discuss-lustre.org/attachments/20120108/c4fd14bd/attachment.diff>


More information about the lustre-discuss mailing list