Go to file
riastradh 76d4b81251 Clarify compile-time and run-time arithmetic safety assertions.
This is an experiment with a handful of macros for writing the
checks, most of which are compile-time:

MUL_OK(t, a, b)         Does a*b avoid overflow in type t?
ADD_OK(t, a, b)         Does a + b avoid overflow in type t?
TOOMANY(t, x, b, m)     Are there more than m b-element blocks in x in type t?
                        (I.e., does ceiling(x/b) > m?)

Addenda that might make sense but are not needed here:

MUL(t, a, b, &p)        Set p = a*b and return 0, or return ERANGE if overflow.
ADD(t, a, b, &s)        Set s = a+b and return 0, or return ERANGE if overflow.

Example:

	uint32_t a = ..., b = ..., y = ..., z = ..., x, w;

        /* input validation */
        error = MUL(size_t, a, b, &x);
        if (error)
                fail;
        if (TOOMANY(uint32_t, x, BLKSIZ, MAX_NBLK))
                fail;
        y = HOWMANY(x, BLKSIZ);
        if (z > Z_MAX)
                fail;
        ...
        /* internal computation */
        __CTASSERT(MUL_OK(uint32_t, Z_MAX, MAX_NBLK));
        w = z*y;

Obvious shortcomings:

1. Nothing checks your ctassert matches your subsequent arithmetic.
   (Maybe we could have BOUNDED_MUL(t, x, xmax, y, ymax) with a
   ctassert inside.)

2. Nothing flows the bounds needed by the arithmetic you use back
   into candidate definitions of X_MAX/Y_MAX.

But at least the reviewer's job is only to make sure that (a) the
MUL_OK matches the *, and (b) the bounds in the assertion match the
bounds on the inputs -- in particular, the reviewer need not derive
the bounds from the context, only confirm they are supported by the
paths to it.

This is not meant to be a general-purpose proof assistant, or even a
special-purpose one like gfverif <http://gfverif.cryptojedi.org/>.
Rather, it is an experiment in adding a modicum of compile-time
verification with a simple C API change.

This also is not intended to serve as trapping arithmetic on
overflow.  The goal here is to enable writing the program with
explicit checks on input and compile-time annotations on computation
to gain confident that overflow won't happen in the computation.
2017-07-29 21:04:07 +00:00
bin PR bin/48498 PR bin/52426 2017-07-26 23:09:41 +00:00
common Fix ISO C compliance: strtol of "0xX" should give the largest valid 2017-07-06 21:08:44 +00:00
compat Don't mix abis in any CRUNCHEDPROG 2017-01-05 21:28:42 +00:00
crypto PR/51682: Antoine Beaupré: Simplify and comment previous patch. 2017-07-23 05:40:27 +00:00
dist/pf
distrib If i386/svr4_machdep.h is no longer to be installed in /usr/include 2017-07-29 19:39:58 +00:00
doc expat-2.2.2 security fix release out. 2017-07-28 22:53:32 +00:00
etc turn on "console" which can be either the serial console or the glass tty 2017-07-13 16:03:36 +00:00
external Apply upstream patch: 2017-07-28 22:24:27 +00:00
extsrc
games typo 2017-07-02 07:02:47 +00:00
include Implement __cxa_thread_atexit and __cxa_thread_atexit_impl. This 2017-07-11 15:21:31 +00:00
lib The sparc port needs s_nearbyint.c as well. 2017-07-27 10:03:10 +00:00
libexec Fix thinko in previous: even if the address is an unsigned value and we 2017-07-24 08:08:34 +00:00
regress Remove regress lseek tests, obsoleted by ATF 2017-05-14 04:26:40 +00:00
rescue Remove MKCRYPTO option. 2017-05-21 15:28:36 +00:00
sbin Fix Dt argument. 2017-07-18 19:50:54 +00:00
share Remove references to i386. 2017-07-29 12:15:12 +00:00
sys Remove TCP_COMPAT_42 from the config files. Pass 3. 2017-07-29 18:08:56 +00:00
tests PR bin/48498 PR bin/52426 2017-07-26 17:50:20 +00:00
tools --disable-nls does not work in gdb subdir, so add am_cv_func_iconv=no to 2017-07-01 12:23:09 +00:00
usr.bin Clarify compile-time and run-time arithmetic safety assertions. 2017-07-29 21:04:07 +00:00
usr.sbin sparc has db_machdep.c now. 2017-07-21 20:58:44 +00:00
BUILDING regen 2017-05-21 15:30:20 +00:00
Makefile don't descend into gnu 2017-04-11 14:04:15 +00:00
Makefile.inc
UPDATING New dhcpcd does not like update builds from previous versions. 2017-04-03 09:37:58 +00:00
build.sh - Allow all other targets (like kernel=) to obey mkrepro (-P) 2017-04-08 18:22:35 +00:00