Skip to content

Commit

Permalink
libspl sys/sysmacros.h: Fix P2ROUNDUP_TYPED to not trigger integer ov…
Browse files Browse the repository at this point in the history
…erflow

The original P2ROUNDUP_TYPED macro contains a -x which triggers PaX's
integer overflow detection for unsigned integers. Replace the macro with
an equivalent version that does not trigger the overflow.

Axioms:
A. (-(x)) === (~((x) - 1)) === (~(x) + 1) under two's complement
B. ~(x & y) === ((~(x)) | (~(y))) under De Morgan's law
C. ~(~x) = x under the law of the excluded middle

Proof:
0. (-(-(x) & -(align))) original
1. (~(-(x) & -(align)) + 1) by A
2. (((~(-(x))) | (~(-(align)))) + 1) by B
3. (((~(~((x) - 1))) | (~(~((align) - 1)))) + 1) by A
4. (((((x) - 1)) | (((align) - 1))) + 1) by C

Signed-off-by: Jason Zaman <[email protected]>
Reviewed-by: Chris Dunlop <[email protected]>
Reviewed-by: Richard Yao <[email protected]>
  • Loading branch information
perfinion committed Oct 26, 2015
1 parent b23d543 commit 28a6a86
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions lib/libspl/include/sys/sysmacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@
*/
#define P2ALIGN(x, align) ((x) & -(align))
#define P2CROSS(x, y, align) (((x) ^ (y)) > (align) - 1)
#define P2ROUNDUP(x, align) (-(-(x) & -(align)))
#define P2ROUNDUP_TYPED(x, align, type) \
(-(-(type)(x) & -(type)(align)))
#define P2ROUNDUP(x, align) ((((x) - 1) | ((align) - 1)) + 1)
#define P2BOUNDARY(off, len, align) \
(((off) ^ ((off) + (len) - 1)) > (align) - 1)
#define P2PHASE(x, align) ((x) & ((align) - 1))
Expand Down Expand Up @@ -79,7 +77,7 @@
#define P2NPHASE_TYPED(x, align, type) \
(-(type)(x) & ((type)(align) - 1))
#define P2ROUNDUP_TYPED(x, align, type) \
(-(-(type)(x) & -(type)(align)))
((((type)(x) - 1) | ((type)(align) - 1)) + 1)
#define P2END_TYPED(x, align, type) \
(-(~(type)(x) & -(type)(align)))
#define P2PHASEUP_TYPED(x, align, phase, type) \
Expand Down

0 comments on commit 28a6a86

Please sign in to comment.