[rust-dev] Integer overflow, round -2147483648

Patrick Walton pcwalton at mozilla.com
Sun Jun 22 16:12:01 PDT 2014


On 6/22/14 2:12 PM, Cameron Zwarich wrote:
> For some applications, Rust’s bounds checks and the inability of rustc
> to eliminate them in nontrivial cases will already be too much of a
> performance sacrifice. What do we say to those people? Is it just that
> memory safety is important because of its security implications, and
> other forms of program correctness are not?
>
> I am wary of circling around on this topic again, but I feel that the
> biggest mistake in this discussion js that checked overflow in a
> language requires a potential trap on every single integer operation.
> Languages like Ada (and Swift, to a lesser extent), allow for slightly
> imprecise exceptions in the case of integer overflow.

I believe that it is possible that the overhead of integer overflow will 
be negligible in the future, and that paper is exciting to me too! But I 
feel that:

1. Integer overflow is primarily a security concern when it compromises 
memory safety. Quoting OWASP [1], emphasis mine:

"An integer overflow condition exists when an integer, which has not 
been properly sanity checked, is used in the *determination of an offset 
or size for memory allocation, copying, concatenation, or similarly*."

Other features in Rust, such as bounds checks, unsigned indexing, and 
the borrow check, defend against the memory safety problems. So the 
benefits of defending against memory safety are likely to be quite 
different in Rust from in C. It's a question of costs versus benefits, 
as always.

2. Signed integer overflow is not undefined behavior in Rust as it is in 
C. This mitigates some of the more frightening risks associated with it.

3. The As-If-Infinitely-Ranged paper is research. Like all research, the 
risk of adopting integer overflow checks is somewhat high; it might 
still not work out to be acceptable in practice when we've exhausted all 
potential compiler optimizations that it allows. That risk has to be 
compared against the potential reward, which is likely to be lesser in 
Rust than in C because of the reasons outlined in (1) and (2).

4. We have a pretty tight shipping schedule at this point: 1.0, which is 
the backwards compatible release, is on track to be released this year. 
We are making excellent progress on backwards incompatible language 
changes (closed 10 out of 45 issues over the past 2 weeks!), but we must 
be consciously fighting scope creep in order to release a product.

5. It's not clear to me that integer overflow cannot be added backwards 
compatibly: we can lexically scope checked arithmetic in much the same 
way C# lexically scopes unchecked arithmetic. It's not in line with 
Rust's philosophy of safe-by-default, but saying that we might introduce 
a safer opt-in version of Rust in the future strikes me as a fairly 
pragmatic compromise, and one that is not without successful precedent: 
for example, C, has, for all intents and purposes, successfully shed the 
baggage of its "variables without an annotated type default to int" 
design mistake via a bog-standard compiler warning.

For these reasons, I'm pretty comfortable with not making any changes 
for 1.0, and deferring this issue to later.

Patrick

[1]: https://www.owasp.org/index.php/Integer_overflow



More information about the Rust-dev mailing list