Discussion:
[PATCH] xfsprogs: use abort() not ASSERT(0) for impossible switch case
Eric Sandeen
2014-08-21 17:32:02 UTC
Permalink
The original reason for the expletive below has been lost
in the mists of time, but at any rate, ASSERT() goes away in
libxfs, and this leads static analysis checkers to believe that
XFS_BTNUM_MAX is possible, and that we might overflow an array
later when using it as an index.

We can shut this up and mark it as truly impossible with abort().

Signed-off-by: Eric Sandeen <***@redhat.com>
---

diff --git a/include/xfs_btree.h b/include/xfs_btree.h
index 2590d40..f4a1f61 100644
--- a/include/xfs_btree.h
+++ b/include/xfs_btree.h
@@ -69,7 +69,7 @@ do { \
case XFS_BTNUM_BMAP: __XFS_BTREE_STATS_INC(bmbt, stat); break; \
case XFS_BTNUM_INO: __XFS_BTREE_STATS_INC(ibt, stat); break; \
case XFS_BTNUM_FINO: __XFS_BTREE_STATS_INC(fibt, stat); break; \
- case XFS_BTNUM_MAX: ASSERT(0); /* fucking gcc */ ; break; \
+ case XFS_BTNUM_MAX: abort(); /* fucking gcc */ ; break; \
} \
} while (0)

@@ -83,7 +83,7 @@ do { \
case XFS_BTNUM_BMAP: __XFS_BTREE_STATS_ADD(bmbt, stat, val); break; \
case XFS_BTNUM_INO: __XFS_BTREE_STATS_ADD(ibt, stat, val); break; \
case XFS_BTNUM_FINO: __XFS_BTREE_STATS_ADD(fibt, stat, val); break; \
- case XFS_BTNUM_MAX: ASSERT(0); /* fucking gcc */ ; break; \
+ case XFS_BTNUM_MAX: abort(); /* fucking gcc */ ; break; \
} \
} while (0)
Christoph Hellwig
2014-08-21 17:44:14 UTC
Permalink
Post by Eric Sandeen
The original reason for the expletive below has been lost
in the mists of time, but at any rate, ASSERT() goes away in
libxfs, and this leads static analysis checkers to believe that
XFS_BTNUM_MAX is possible, and that we might overflow an array
later when using it as an index.
We can shut this up and mark it as truly impossible with abort().
This won't work in kernel space, and we'd like to keep this file in sync.
Eric Sandeen
2014-08-21 17:45:33 UTC
Permalink
Post by Christoph Hellwig
Post by Eric Sandeen
The original reason for the expletive below has been lost
in the mists of time, but at any rate, ASSERT() goes away in
libxfs, and this leads static analysis checkers to believe that
XFS_BTNUM_MAX is possible, and that we might overflow an array
later when using it as an index.
We can shut this up and mark it as truly impossible with abort().
This won't work in kernel space, and we'd like to keep this file in sync.
Ah, right, sorry - spaced out that it was shared.

I'll add ASSERT_ALWAYS() to userspace then, perhaps.

Thanks,
-Eric
Eric Sandeen
2014-08-21 18:27:02 UTC
Permalink
The original reason for the expletive below has been lost
in the mists of time, but at any rate, ASSERT() goes away in
libxfs, and this leads static analysis checkers to believe that
XFS_BTNUM_MAX is possible, and that we might overflow an array
later when using it as an index.

We can shut this up and mark it as truly impossible by adding
a userspace definition for ASSERT_ALWAYS and using it.

Signed-off-by: Eric Sandeen <***@redhat.com>
---

V2: do it in a kernel-compatible way.

diff --git a/include/xfs_btree.h b/include/xfs_btree.h
index 2590d40..f24f787 100644
--- a/include/xfs_btree.h
+++ b/include/xfs_btree.h
@@ -69,7 +69,7 @@ do { \
case XFS_BTNUM_BMAP: __XFS_BTREE_STATS_INC(bmbt, stat); break; \
case XFS_BTNUM_INO: __XFS_BTREE_STATS_INC(ibt, stat); break; \
case XFS_BTNUM_FINO: __XFS_BTREE_STATS_INC(fibt, stat); break; \
- case XFS_BTNUM_MAX: ASSERT(0); /* fucking gcc */ ; break; \
+ case XFS_BTNUM_MAX: ASSERT_ALWAYS(0); /* fucking gcc */ ; break; \
} \
} while (0)

@@ -83,7 +83,7 @@ do { \
case XFS_BTNUM_BMAP: __XFS_BTREE_STATS_ADD(bmbt, stat, val); break; \
case XFS_BTNUM_INO: __XFS_BTREE_STATS_ADD(ibt, stat, val); break; \
case XFS_BTNUM_FINO: __XFS_BTREE_STATS_ADD(fibt, stat, val); break; \
- case XFS_BTNUM_MAX: ASSERT(0); /* fucking gcc */ ; break; \
+ case XFS_BTNUM_MAX: ASSERT_ALWAYS(0); /* fucking gcc */ ; break; \
} \
} while (0)

diff --git a/libxfs/xfs.h b/libxfs/xfs.h
index 30a316d..81d7cd9 100644
--- a/libxfs/xfs.h
+++ b/libxfs/xfs.h
@@ -48,6 +48,10 @@
#undef ASSERT
#define ASSERT(ex) assert(ex)

+#undef ASSERT_ALWAYS
+#define ASSERT_ALWAYS(ex) \
+ (unlikely(ex) ? (void)0 : abort())
+
typedef __uint32_t uint_t;
typedef __uint32_t inst_t; /* an instruction */
Dave Chinner
2014-08-21 22:55:21 UTC
Permalink
Post by Eric Sandeen
The original reason for the expletive below has been lost
in the mists of time, but at any rate, ASSERT() goes away in
libxfs, and this leads static analysis checkers to believe that
XFS_BTNUM_MAX is possible, and that we might overflow an array
later when using it as an index.
We can shut this up and mark it as truly impossible by adding
a userspace definition for ASSERT_ALWAYS and using it.
---
V2: do it in a kernel-compatible way.
diff --git a/include/xfs_btree.h b/include/xfs_btree.h
index 2590d40..f24f787 100644
--- a/include/xfs_btree.h
+++ b/include/xfs_btree.h
@@ -69,7 +69,7 @@ do { \
case XFS_BTNUM_BMAP: __XFS_BTREE_STATS_INC(bmbt, stat); break; \
case XFS_BTNUM_INO: __XFS_BTREE_STATS_INC(ibt, stat); break; \
case XFS_BTNUM_FINO: __XFS_BTREE_STATS_INC(fibt, stat); break; \
- case XFS_BTNUM_MAX: ASSERT(0); /* fucking gcc */ ; break; \
+ case XFS_BTNUM_MAX: ASSERT_ALWAYS(0); /* fucking gcc */ ; break; \
} \
} while (0)
@@ -83,7 +83,7 @@ do { \
case XFS_BTNUM_BMAP: __XFS_BTREE_STATS_ADD(bmbt, stat, val); break; \
case XFS_BTNUM_INO: __XFS_BTREE_STATS_ADD(ibt, stat, val); break; \
case XFS_BTNUM_FINO: __XFS_BTREE_STATS_ADD(fibt, stat, val); break; \
- case XFS_BTNUM_MAX: ASSERT(0); /* fucking gcc */ ; break; \
+ case XFS_BTNUM_MAX: ASSERT_ALWAYS(0); /* fucking gcc */ ; break; \
} \
} while (0)
Which we don't want in kernel space, because we want that case to be
optimised out by the compiler for production kernels....

What we should really do is properly abstract the btree stats
structure and put a pointer into the cursor so that the switch
statement can go away....

Cheers,

Dave.
--
Dave Chinner
***@fromorbit.com
Dave Chinner
2014-08-21 22:50:48 UTC
Permalink
Post by Eric Sandeen
The original reason for the expletive below has been lost
in the mists of time
Oh, no it hasn't.

That's a switch statement using enums for the cases and so if you
don't define every enum value in the switch statement gcc throws
warnings. IOWs, the switch statement has to either define them all or
contain a "default" case, either of which *does not need to exist* because
other code guarantees that the value of cur->bc_btnum is within
the valid range.

So, we have to put an invalid value into the switch statement to
make gcc shut the fuck up, and the ASSERT(0) is there to indicate
that "this should never, ever happen".
Post by Eric Sandeen
libxfs, and this leads static analysis checkers to believe that
XFS_BTNUM_MAX is possible, and that we might overflow an array
later when using it as an index.
We can shut this up and mark it as truly impossible with abort().
Random differences between kernel and user code to keep static
analysis checkers happy is not a good road to follow, because it
will just cause patch failures and people wondering "why is this
randomly different to the kernel code?". So, no, I don't really like
this approach.

Cheers,

Dave.
--
Dave Chinner
***@fromorbit.com
Loading...