Skip to content

Commit

Permalink
Auto merge of #58475 - alex:usize-overflow-panic, r=<try>
Browse files Browse the repository at this point in the history
[WIP] Make usize overflow always have debug-assertions semantics

This is a prototype for rust-lang/rfcs#2635 to enable us to collect some measurements.
  • Loading branch information
bors committed Feb 23, 2019
2 parents 02a4e27 + 864dd3c commit abc6e24
Show file tree
Hide file tree
Showing 19 changed files with 56 additions and 104 deletions.
15 changes: 7 additions & 8 deletions src/librustc/ty/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,21 @@ impl AbsoluteBytePos {

impl<'sess> OnDiskCache<'sess> {
/// Creates a new OnDiskCache instance from the serialized data in `data`.
pub fn new(sess: &'sess Session, data: Vec<u8>, start_pos: usize) -> OnDiskCache<'sess> {
pub fn new(sess: &'sess Session, data: Vec<u8>) -> OnDiskCache<'sess> {
debug_assert!(sess.opts.incremental.is_some());

// Wrapping in a scope so we can borrow `data`
let footer: Footer = {
let mut decoder = opaque::Decoder::new(&data[..], start_pos);

// Decode the *position* of the footer which can be found in the
// last 8 bytes of the file.
decoder.set_position(data.len() - IntEncodedWithFixedSize::ENCODED_SIZE);
let mut decoder = opaque::Decoder::new(
&data, data.len() - IntEncodedWithFixedSize::ENCODED_SIZE);
let query_result_index_pos = IntEncodedWithFixedSize::decode(&mut decoder)
.expect("Error while trying to decode query result index position.")
.0 as usize;

// Decoder the file footer which contains all the lookup tables, etc.
decoder.set_position(query_result_index_pos);
decoder = opaque::Decoder::new(&data, query_result_index_pos);
decode_tagged(&mut decoder, TAG_FILE_FOOTER)
.expect("Error while trying to decode query result index position.")
};
Expand Down Expand Up @@ -540,7 +539,7 @@ impl<'a, 'tcx: 'a, 'x> ty_codec::TyDecoder<'a, 'tcx> for CacheDecoder<'a, 'tcx,

#[inline]
fn peek_byte(&self) -> u8 {
self.opaque.data[self.opaque.position()]
self.opaque.data()[0]
}

fn cached_ty_for_shorthand<F>(&mut self,
Expand Down Expand Up @@ -569,9 +568,9 @@ impl<'a, 'tcx: 'a, 'x> ty_codec::TyDecoder<'a, 'tcx> for CacheDecoder<'a, 'tcx,
fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
where F: FnOnce(&mut Self) -> R
{
debug_assert!(pos < self.opaque.data.len());
debug_assert!(pos < self.opaque.original_data.len());

let new_opaque = opaque::Decoder::new(self.opaque.data, pos);
let new_opaque = opaque::Decoder::new(&self.opaque.original_data, pos);
let old_opaque = mem::replace(&mut self.opaque, new_opaque);
let r = f(self);
self.opaque = old_opaque;
Expand Down
14 changes: 11 additions & 3 deletions src/librustc_codegen_llvm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ use crate::type_of::PointeeInfo;
use rustc_codegen_ssa::traits::*;
use libc::c_uint;

use syntax::ast;

use rustc_data_structures::base_n;
use rustc_data_structures::small_c_str::SmallCStr;
use rustc::mir::mono::Stats;
use rustc::session::config::{self, DebugInfo};
use rustc::session::Session;
use rustc::ty::layout::{LayoutError, LayoutOf, Size, TyLayout, VariantIdx};
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::{self, Ty, TyCtxt, TyKind};
use rustc::util::nodemap::FxHashMap;
use rustc_target::spec::{HasTargetSpec, Target};
use rustc_codegen_ssa::callee::resolve_and_get_fn;
Expand Down Expand Up @@ -409,8 +411,14 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
&self.tcx.sess
}

fn check_overflow(&self) -> bool {
self.check_overflow
fn check_overflow(&self, ty: Option<Ty<'tcx>>) -> bool {
let type_specific_overflow = match ty {
Some(ty) => {
ty.sty == TyKind::Uint(ast::UintTy::Usize)
},
None => false
};
self.check_overflow || type_specific_overflow
}

fn stats(&self) -> &RefCell<Stats> {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
// NOTE: Unlike binops, negation doesn't have its own
// checked operation, just a comparison with the minimum
// value, so we have to check for the assert message.
if !bx.check_overflow() {
if !bx.check_overflow(None) {
if let mir::interpret::EvalErrorKind::OverflowNeg = *msg {
const_cond = Some(expected);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
// with #[rustc_inherit_overflow_checks] and inlined from
// another crate (mostly core::num generic/#[inline] fns),
// while the current crate doesn't use overflow checks.
if !bx.cx().check_overflow() {
if !bx.cx().check_overflow(Some(input_ty)) {
let val = self.codegen_scalar_binop(bx, op, lhs, rhs, input_ty);
return OperandValue::Pair(val, bx.cx().const_bool(false));
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/traits/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub trait MiscMethods<'tcx>: BackendTypes {
fn vtables(
&self,
) -> &RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), Self::Value>>;
fn check_overflow(&self) -> bool;
fn check_overflow(&self, ty: Option<Ty<'tcx>>) -> bool;
fn instances(&self) -> &RefCell<FxHashMap<Instance<'tcx>, Self::Value>>;
fn get_fn(&self, instance: Instance<'tcx>) -> Self::Value;
fn get_param(&self, llfn: Self::Value, index: c_uint) -> Self::Value;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_incremental/persist/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub fn load_query_result_cache<'sess>(sess: &'sess Session) -> OnDiskCache<'sess
}

match load_data(sess.opts.debugging_opts.incremental_info, &query_cache_path(sess)) {
LoadResult::Ok{ data: (bytes, start_pos) } => OnDiskCache::new(sess, bytes, start_pos),
LoadResult::Ok{ data: (bytes, _) } => OnDiskCache::new(sess, bytes),
_ => OnDiskCache::new_empty(sess.source_map())
}
}
4 changes: 2 additions & 2 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl<'a, 'tcx: 'a> TyDecoder<'a, 'tcx> for DecodeContext<'a, 'tcx> {

#[inline]
fn peek_byte(&self) -> u8 {
self.opaque.data[self.opaque.position()]
self.opaque.data()[0]
}

#[inline]
Expand Down Expand Up @@ -212,7 +212,7 @@ impl<'a, 'tcx: 'a> TyDecoder<'a, 'tcx> for DecodeContext<'a, 'tcx> {
fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
where F: FnOnce(&mut Self) -> R
{
let new_opaque = opaque::Decoder::new(self.opaque.data, pos);
let new_opaque = opaque::Decoder::new(self.opaque.original_data, pos);
let old_opaque = mem::replace(&mut self.opaque, new_opaque);
let old_state = mem::replace(&mut self.lazy_state, LazyState::NoNode);
let r = f(self);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/build/expr/as_rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
ExprKind::Unary { op, arg } => {
let arg = unpack!(block = this.as_operand(block, scope, arg));
// Check for -MIN on signed integers
if this.hir.check_overflow() && op == UnOp::Neg && expr.ty.is_signed() {
if this.hir.check_overflow(expr.ty) && op == UnOp::Neg && expr.ty.is_signed() {
let bool_ty = this.hir.bool_ty();

let minval = this.minval_literal(expr_span, expr.ty);
Expand Down Expand Up @@ -410,7 +410,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
) -> BlockAnd<Rvalue<'tcx>> {
let source_info = self.source_info(span);
let bool_ty = self.hir.bool_ty();
if self.hir.check_overflow() && op.is_checkable() && ty.is_integral() {
if self.hir.check_overflow(ty) && op.is_checkable() && ty.is_integral() {
let result_tup = self.hir.tcx().intern_tup(&[ty, bool_ty]);
let result_value = self.temp(result_tup, span);

Expand Down
6 changes: 3 additions & 3 deletions src/librustc_mir/hair/cx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc::hir::Node;
use rustc::middle::region;
use rustc::infer::InferCtxt;
use rustc::ty::subst::Subst;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::{self, Ty, TyCtxt, TyKind};
use rustc::ty::subst::{Kind, Substs};
use rustc::ty::layout::VariantIdx;
use syntax::ast;
Expand Down Expand Up @@ -218,8 +218,8 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
self.tables
}

pub fn check_overflow(&self) -> bool {
self.check_overflow
pub fn check_overflow(&self, ty: Ty<'tcx>) -> bool {
self.check_overflow || ty.sty == TyKind::Uint(ast::UintTy::Usize)
}

pub fn type_is_copy_modulo_regions(&self, ty: Ty<'tcx>, span: Span) -> bool {
Expand Down
8 changes: 4 additions & 4 deletions src/libserialize/leb128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ pub fn write_signed_leb128(out: &mut Vec<u8>, value: i128) {
}

#[inline]
pub fn read_signed_leb128(data: &[u8], start_position: usize) -> (i128, usize) {
pub fn read_signed_leb128(data: &[u8]) -> (i128, usize) {
let mut result = 0;
let mut shift = 0;
let mut position = start_position;
let mut position = 0;
let mut byte;

loop {
Expand All @@ -136,7 +136,7 @@ pub fn read_signed_leb128(data: &[u8], start_position: usize) -> (i128, usize) {
result |= -(1 << shift);
}

(result, position - start_position)
(result, position)
}

macro_rules! impl_test_unsigned_leb128 {
Expand Down Expand Up @@ -176,7 +176,7 @@ fn test_signed_leb128() {
}
let mut pos = 0;
for &x in &values {
let (value, bytes_read) = read_signed_leb128(&mut stream, pos);
let (value, bytes_read) = read_signed_leb128(&mut stream[pos..]);
pos += bytes_read;
assert_eq!(x, value);
}
Expand Down
1 change: 1 addition & 0 deletions src/libserialize/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Core encoding and decoding interfaces.
#![feature(specialization)]
#![feature(never_type)]
#![feature(nll)]
#![feature(ptr_wrapping_offset_from)]
#![cfg_attr(test, feature(test))]

pub use self::serialize::{Decoder, Encoder, Decodable, Encodable};
Expand Down
48 changes: 22 additions & 26 deletions src/libserialize/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,59 +157,55 @@ impl Encoder {
// -----------------------------------------------------------------------------

pub struct Decoder<'a> {
pub data: &'a [u8],
position: usize,
pub original_data: &'a [u8],
data: &'a [u8],
}

impl<'a> Decoder<'a> {
#[inline]
pub fn new(data: &'a [u8], position: usize) -> Decoder<'a> {
pub fn new(data: &'a [u8], pos: usize) -> Decoder<'a> {
Decoder {
data,
position,
original_data: data,
data: &data[pos..],
}
}

#[inline]
pub fn position(&self) -> usize {
self.position
pub fn data(&self) -> &[u8] {
self.data
}

#[inline]
pub fn set_position(&mut self, pos: usize) {
self.position = pos
pub fn position(&self) -> usize {
self.data.as_ptr().wrapping_offset_from(self.original_data.as_ptr()) as usize
}

#[inline]
pub fn advance(&mut self, bytes: usize) {
self.position += bytes;
self.data = &self.data[bytes..];
}

#[inline]
pub fn read_raw_bytes(&mut self, s: &mut [u8]) -> Result<(), String> {
let start = self.position;
let end = start + s.len();

s.copy_from_slice(&self.data[start..end]);

self.position = end;
s.copy_from_slice(&self.data[..s.len()]);
self.advance(s.len());

Ok(())
}
}

macro_rules! read_uleb128 {
($dec:expr, $t:ty, $fun:ident) => ({
let (value, bytes_read) = leb128::$fun(&$dec.data[$dec.position ..]);
$dec.position += bytes_read;
let (value, bytes_read) = leb128::$fun(&$dec.data);
$dec.advance(bytes_read);
Ok(value)
})
}

macro_rules! read_sleb128 {
($dec:expr, $t:ty) => ({
let (value, bytes_read) = read_signed_leb128($dec.data, $dec.position);
$dec.position += bytes_read;
let (value, bytes_read) = read_signed_leb128($dec.data);
$dec.advance(bytes_read);
Ok(value as $t)
})
}
Expand Down Expand Up @@ -245,8 +241,8 @@ impl<'a> serialize::Decoder for Decoder<'a> {

#[inline]
fn read_u8(&mut self) -> Result<u8, Self::Error> {
let value = self.data[self.position];
self.position += 1;
let value = self.data[0];
self.advance(1);
Ok(value)
}

Expand Down Expand Up @@ -277,8 +273,8 @@ impl<'a> serialize::Decoder for Decoder<'a> {

#[inline]
fn read_i8(&mut self) -> Result<i8, Self::Error> {
let as_u8 = self.data[self.position];
self.position += 1;
let as_u8 = self.data[0];
self.advance(1);
unsafe { Ok(::std::mem::transmute(as_u8)) }
}

Expand Down Expand Up @@ -314,8 +310,8 @@ impl<'a> serialize::Decoder for Decoder<'a> {
#[inline]
fn read_str(&mut self) -> Result<Cow<'_, str>, Self::Error> {
let len = self.read_usize()?;
let s = ::std::str::from_utf8(&self.data[self.position..self.position + len]).unwrap();
self.position += len;
let s = ::std::str::from_utf8(&self.data[..len]).unwrap();
self.advance(len);
Ok(Cow::Borrowed(s))
}

Expand Down
17 changes: 0 additions & 17 deletions src/test/run-make-fulldeps/issue-14500/Makefile

This file was deleted.

1 change: 0 additions & 1 deletion src/test/run-make-fulldeps/issue-14500/bar.rs

This file was deleted.

7 changes: 0 additions & 7 deletions src/test/run-make-fulldeps/issue-14500/foo.c

This file was deleted.

5 changes: 0 additions & 5 deletions src/test/run-make-fulldeps/issue-14500/foo.rs

This file was deleted.

11 changes: 0 additions & 11 deletions src/test/run-make-fulldeps/lto-smoke-c/Makefile

This file was deleted.

7 changes: 0 additions & 7 deletions src/test/run-make-fulldeps/lto-smoke-c/bar.c

This file was deleted.

4 changes: 0 additions & 4 deletions src/test/run-make-fulldeps/lto-smoke-c/foo.rs

This file was deleted.

0 comments on commit abc6e24

Please sign in to comment.