From 89bbe3011562da5865bb2d0d34cbd27c525cddb2 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Mon, 16 Dec 2019 23:19:16 +1100 Subject: [PATCH] Update bounds-checking note for overflow-checking --- doc/glossary.md | 6 ++++-- doc/note/bounds-checking.md | 41 ++++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/doc/glossary.md b/doc/glossary.md index 8ae5bb6e..316a71eb 100644 --- a/doc/glossary.md +++ b/doc/glossary.md @@ -11,8 +11,10 @@ where it left off, in the middle of the function body. See the A type that depends on another value. For example, a variable `n`'s type might be "the length of `s`", for some other [slice](/doc/note/slices-arrays-and-tables.md)-typed variable `s`. Dependent -types are *a* way to implement [bounds checking](/doc/note/bounds-checking.md), -but they're not the only way. Wuffs does not use them. +types are *a* way to implement compile-time [bounds +checking](/doc/note/bounds-checking.md), but they're not the only way, and +there's more to programming languages than their type systems. Wuffs does not +use dependent types. #### Effect diff --git a/doc/note/bounds-checking.md b/doc/note/bounds-checking.md index 2ef75072..f4f94136 100644 --- a/doc/note/bounds-checking.md +++ b/doc/note/bounds-checking.md @@ -7,26 +7,39 @@ example, the expression `x[..]` is always valid because the implicit `i` and length. Similarly, an expression like `x[i]` is invalid unless there is a compile-time -proof that `i` is in bounds: that `((0 <= i) and (i < x.length()))`. Proofs can +proof that `i` is in-bounds: that `((0 <= i) and (i < x.length()))`. Proofs can involve natural bounds (e.g. if `i` has type `base.u8` then `(0 <= i)` is trivially true since a `base.u8` is unsigned), [refinements](/doc/glossary.md#refinement-type) (e.g. if `i`has type -`base.u32[..= 100]` then 100 is an upper bound for `i`) and [interval -arithmetic](/doc/note/interval-arithmetic.md) (e.g. for an expression like -`x[m + n]`, the upper bound for `m + n` is the sum of the upper bounds of `m` -and `n`). +`base.u32[..= 100]` then 100 is an inclusive upper bound for `i`), +[facts](/doc/note/facts.md) (e.g. an explicit prior check `if i < x.length()`) +and [interval arithmetic](/doc/note/interval-arithmetic.md) (e.g. for an +expression like `x[m + n]`, the upper bound for `m + n` is the sum of the upper +bounds of `m` and `n`). For example, if `a` is an `array[1024] base.u8`, and `expr` is some expression -of type `base.u32`, then `a[expr & 1023]` is valid, because the bitwise-and +of type `base.u32`, then `a[expr & 1023]` is valid (in-bounds). The bitwise-and means that the overall index expression is in the range `[0 ..= 1023]`, regardless of `expr`'s range. -Proofs can also involve [facts](/doc/note/facts.md), dynamic constraints that -are not confined to the type system. For example, when the if-true branch of an -`if i < j { etc }` statement is taken, that `(i < j)` fact could combine with -another `(j <= x.length())` fact to deduce that `(i < x.length())` when inside -that if-true branch (but not necessarily outside of that if statement). -Wuffs does not have dependent types. Dependent types are one way to enforce -bounds checking, but they're not the only way, and there's more to programming -languages than their type systems. +## Overflow Checking + +Arithmetic overflow checking uses the same mechanisms as bounds checking. For +example, if `m` and `n` both have type `base.u8` (or a refinement thereof) then +the expression `m + n` also has type `base.u8`. Proving that `m + n` evaluates +to something inside the `base.u8` range, `[0 ..= 255]`, is equivalent to +proving that the expression `h[m + n]` is valid (in-bounds) for a hypothetical +`h` array of length `256`. + + +## `nullptr` Checking + +Guarding against `nullptr` dereferences is another case of bounds checking. +Like other programming languages, `nptr T` and `ptr T` are types: nullable and +non-null pointers to some type `T`. In Wuffs, the latter is effectively a +refinement. Assuming that `nullptr` is equivalent to `0`, `ptr T` is +conceptually just `(nptr T)[1 ..= UINTPTR_MAX]`, although that's not actually +valid syntax. Similarly, if `expr` is a pointer-typed expression then checking +that `expr` is not `nullptr` is equivalent to checking that `expr` is in the +range `[1 ..= UINTPTR_MAX]`.