Skip to content

Commit a24b444

Browse files
committed
auto merge of #18728 : thestinger/rust/int, r=cmr
This fixes the gap in the language definition causing #18726 by defining a clear bound on the maximum size for libraries to enforce. Closes #18069
2 parents bfaa7bc + 210e059 commit a24b444

File tree

6 files changed

+59
-20
lines changed

6 files changed

+59
-20
lines changed

src/doc/reference.md

+8-11
Original file line numberDiff line numberDiff line change
@@ -3557,17 +3557,14 @@ The machine types are the following:
35573557

35583558
#### Machine-dependent integer types
35593559

3560-
The Rust type `uint` [^rustuint] is an
3561-
unsigned integer type with target-machine-dependent size. Its size, in
3562-
bits, is equal to the number of bits required to hold any memory address on
3563-
the target machine.
3564-
3565-
The Rust type `int` [^rustint] is a two's complement signed integer type with
3566-
target-machine-dependent size. Its size, in bits, is equal to the size of the
3567-
rust type `uint` on the same target machine.
3568-
3569-
[^rustuint]: A Rust `uint` is analogous to a C99 `uintptr_t`.
3570-
[^rustint]: A Rust `int` is analogous to a C99 `intptr_t`.
3560+
The `uint` type is an unsigned integer type with the same number of bits as the
3561+
platform's pointer type. It can represent every memory address in the process.
3562+
3563+
The `int` type is a signed integer type with the same number of bits as the
3564+
platform's pointer type. The theoretical upper bound on object and array size
3565+
is the maximum `int` value. This ensures that `int` can be used to calculate
3566+
differences between pointers into an object or array and can address every byte
3567+
within an object along with one byte past the end.
35713568

35723569
### Textual types
35733570

src/librustc_trans/trans/adt.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -475,17 +475,17 @@ fn ensure_struct_fits_in_address_space<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
475475
scapegoat: Ty<'tcx>) {
476476
let mut offset = 0;
477477
for &llty in fields.iter() {
478-
// Invariant: offset < ccx.max_obj_size() <= 1<<61
478+
// Invariant: offset < ccx.obj_size_bound() <= 1<<61
479479
if !packed {
480480
let type_align = machine::llalign_of_min(ccx, llty);
481481
offset = roundup(offset, type_align);
482482
}
483-
// type_align is a power-of-2, so still offset < ccx.max_obj_size()
484-
// llsize_of_alloc(ccx, llty) is also less than ccx.max_obj_size()
483+
// type_align is a power-of-2, so still offset < ccx.obj_size_bound()
484+
// llsize_of_alloc(ccx, llty) is also less than ccx.obj_size_bound()
485485
// so the sum is less than 1<<62 (and therefore can't overflow).
486486
offset += machine::llsize_of_alloc(ccx, llty);
487487

488-
if offset >= ccx.max_obj_size() {
488+
if offset >= ccx.obj_size_bound() {
489489
ccx.report_overbig_object(scapegoat);
490490
}
491491
}
@@ -504,11 +504,11 @@ fn ensure_enum_fits_in_address_space<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
504504
let discr_size = machine::llsize_of_alloc(ccx, ll_inttype(ccx, discr));
505505
let (field_size, field_align) = union_size_and_align(fields);
506506

507-
// field_align < 1<<32, discr_size <= 8, field_size < MAX_OBJ_SIZE <= 1<<61
507+
// field_align < 1<<32, discr_size <= 8, field_size < OBJ_SIZE_BOUND <= 1<<61
508508
// so the sum is less than 1<<62 (and can't overflow).
509509
let total_size = roundup(discr_size, field_align) + field_size;
510510

511-
if total_size >= ccx.max_obj_size() {
511+
if total_size >= ccx.obj_size_bound() {
512512
ccx.report_overbig_object(scapegoat);
513513
}
514514
}

src/librustc_trans/trans/context.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -705,8 +705,23 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
705705
&self.local.trait_cache
706706
}
707707

708-
pub fn max_obj_size(&self) -> u64 {
709-
1<<31 /* FIXME #18069: select based on architecture */
708+
/// Return exclusive upper bound on object size.
709+
///
710+
/// The theoretical maximum object size is defined as the maximum positive `int` value. This
711+
/// ensures that the `offset` semantics remain well-defined by allowing it to correctly index
712+
/// every address within an object along with one byte past the end, along with allowing `int`
713+
/// to store the difference between any two pointers into an object.
714+
///
715+
/// The upper bound on 64-bit currently needs to be lower because LLVM uses a 64-bit integer to
716+
/// represent object size in bits. It would need to be 1 << 61 to account for this, but is
717+
/// currently conservatively bounded to 1 << 47 as that is enough to cover the current usable
718+
/// address space on 64-bit ARMv8 and x86_64.
719+
pub fn obj_size_bound(&self) -> u64 {
720+
match self.sess().target.target.target_word_size[] {
721+
"32" => 1 << 31,
722+
"64" => 1 << 47,
723+
_ => unreachable!() // error handled by config::build_target_config
724+
}
710725
}
711726

712727
pub fn report_overbig_object(&self, obj: Ty<'tcx>) -> ! {

src/librustc_trans/trans/type_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn ensure_array_fits_in_address_space<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
3434
scapegoat: Ty<'tcx>) {
3535
let esz = machine::llsize_of_alloc(ccx, llet);
3636
match esz.checked_mul(size) {
37-
Some(n) if n < ccx.max_obj_size() => {}
37+
Some(n) if n < ccx.obj_size_bound() => {}
3838
_ => { ccx.report_overbig_object(scapegoat) }
3939
}
4040
}

src/test/compile-fail/huge-enum.rs

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
// FIXME: work properly with higher limits
1414

15+
#[cfg(target_word_size = "32")]
1516
fn main() {
1617
let big: Option<[u32, ..(1<<29)-1]> = None;
1718
}
19+
20+
#[cfg(target_word_size = "64")]
21+
fn main() {
22+
let big: Option<[u32, ..(1<<45)-1]> = None;
23+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::mem::size_of;
12+
13+
#[cfg(target_word_size = "32")]
14+
pub fn main() {
15+
assert_eq!(size_of::<[u8, ..(1 << 31) - 1]>(), (1 << 31) - 1);
16+
}
17+
18+
#[cfg(target_word_size = "64")]
19+
pub fn main() {
20+
assert_eq!(size_of::<[u8, ..(1 << 47) - 1]>(), (1 << 47) - 1);
21+
}

0 commit comments

Comments
 (0)