lang: Limit arrays and slices to 2^31 - 1 elements
b21693da7a71e833f6ebea0e3d61563322a514e63fe191fbe6ba88ca75325c6f
Lengths were limited to 2^32 - 1 elements. A sign-extended u32 index can then exceed 2^31 and still be below a length, so RV64 selection zero-extended every computed slice index with two instructions before the bounds check. Arrays and slices now hold at most 2^31 - 1 elements, `types::MAX_LENGTH`. The resolver rejects longer array types and repeated array literals. Slice growth (`append`, `reserve`, `resize`, `extend`) and `@sliceOf` trap beyond the limit, before they allocate or build the slice, and page slices clamp their length to it. Slices of elements of two or more bytes were already shorter, because their size in bytes must fit in u32. An array or slice index whose register is sign-extended is now used as it is: an index of 2^31 or more fails the unsigned check, so the check also proves that the index equals its zero extension. A longer array or slice, which only hostile RIL can build, therefore traps instead of reaching memory past its end. The prog.* fixtures run 22.0% fewer instructions (geometric mean) than before the logical condition change, against 18.9% without the limit. The in-kernel compiler of the runtime test now finishes its first load before the 14M-tick window that should interrupt it. That window is now 8M ticks, inside the measured range of 5M to 11M where the load is interrupted after reserving its output. Assisted-by: Claude:opus-5.5
1 parent
6293a9a9
lib/std/arch/rv64/isel.rad
+17 -26
| 143 | 143 | constant UNVISITED: Extension = Extension { sign: 0, zero: 0 }; |
|
| 144 | 144 | /// Extension of a register that holds zero or one. |
|
| 145 | 145 | constant BOOLEAN: Extension = Extension { sign: 8, zero: 8 }; |
|
| 146 | 146 | /// Extension of the result of an RV64 word instruction. |
|
| 147 | 147 | constant WORD: Extension = Extension { sign: 32, zero: 64 }; |
|
| 148 | - | /// Largest array length for which a passed bounds check proves that a |
|
| 149 | - | /// sign-extended word index equals its zero extension. A larger index has its |
|
| 150 | - | /// high bits set, so it fails the unsigned check. |
|
| 151 | - | constant MAX_SIGNED_INDEX_COUNT: u32 = 0x80000000; |
|
| 152 | 148 | ||
| 153 | 149 | /// Extension from the sign and zero widths. A register that is zero above a width |
|
| 154 | 150 | /// is also the sign extension of the next wider width. |
|
| 155 | 151 | fn extension(sign: u32, zero: u32) -> Extension { |
|
| 156 | 152 | let wider = zero * 2; |
| 380 | 376 | } |
|
| 381 | 377 | case il::Instr::Field { src, .. } => { |
|
| 382 | 378 | mark(o, src); |
|
| 383 | 379 | return false; |
|
| 384 | 380 | } |
|
| 385 | - | case il::Instr::Elem { base, index, count, .. } => { |
|
| 381 | + | case il::Instr::Elem { base, .. } => { |
|
| 386 | 382 | mark(o, base); |
|
| 387 | - | if count > MAX_SIGNED_INDEX_COUNT { |
|
| 388 | - | observe(o, index); |
|
| 389 | - | } |
|
| 390 | 383 | return false; |
|
| 391 | 384 | } |
|
| 392 | - | case il::Instr::SliceElem { slice, index, .. } => { |
|
| 385 | + | case il::Instr::SliceElem { slice, .. } => { |
|
| 393 | 386 | mark(o, slice); |
|
| 394 | - | observe(o, index); |
|
| 395 | 387 | return false; |
|
| 396 | 388 | } |
|
| 397 | 389 | case il::Instr::Call { func: callee, args, .. } => { |
|
| 398 | 390 | observe(o, callee); |
|
| 399 | 391 | for arg in il::argVals(func, args) { |
| 435 | 427 | } |
|
| 436 | 428 | return true; |
|
| 437 | 429 | } |
|
| 438 | 430 | ||
| 439 | 431 | /// Find the registers whose high bits an instruction can observe: a 64-bit |
|
| 440 | - | /// operation, a switch, memory, a call, or a return. Slice indices and bounds are |
|
| 432 | + | /// operation, a switch, memory, a call, or a return. Slice bounds are |
|
| 441 | 433 | /// zero-extended, so they observe the high bits too: a zero-extended word is used |
|
| 442 | 434 | /// as it is. Copies, block arguments and operations that keep high bits pass the |
|
| 443 | - | /// observation on to their operands. Comparisons, array indices, narrow stores and |
|
| 435 | + | /// observation on to their operands. Comparisons, indices, narrow stores and |
|
| 444 | 436 | /// extensions extend or ignore the high bits. Instructions not listed here observe |
|
| 445 | 437 | /// all their operands. |
|
| 446 | 438 | /// |
|
| 447 | 439 | /// A first pass visits every instruction, and later passes revisit those that pass |
|
| 448 | 440 | /// observations on, until none is added. Uses mostly follow definitions, so the |
| 801 | 793 | } |
|
| 802 | 794 | try emit::emit(e, encode::slli(rd, rs, SHIFT_W32)); |
|
| 803 | 795 | try emit::emit(e, encode::srli(rd, rd, SHIFT_W32)); |
|
| 804 | 796 | } |
|
| 805 | 797 | ||
| 806 | - | /// Resolve the word `index` of a bounds check into a register. A register that holds |
|
| 807 | - | /// its zero extension, or for `signed` its sign extension, is used as it is. Other |
|
| 808 | - | /// indices are zero-extended into [`super::SCRATCH1`]. |
|
| 809 | - | fn resolveIndex(s: &mut Selector, e: &mut emit::Emitter, index: il::Val, signed: bool) -> gen::Reg |
|
| 798 | + | /// Resolve the word `index` of a bounds check into a register. Arrays and slices are |
|
| 799 | + | /// shorter than 2^31 elements, so a register that holds the sign or zero extension of |
|
| 800 | + | /// the index is used as it is. Other indices are zero-extended into [`super::SCRATCH1`]. |
|
| 801 | + | fn resolveIndex(s: &mut Selector, e: &mut emit::Emitter, index: il::Val) -> gen::Reg |
|
| 810 | 802 | throws (super::Error) |
|
| 811 | 803 | { |
|
| 812 | - | if isExtended(s, index, il::Type::W32, false) |
|
| 813 | - | or (signed and isExtended(s, index, il::Type::W32, true)) |
|
| 814 | - | { |
|
| 804 | + | if isExtended(s, index, il::Type::W32, false) or isExtended(s, index, il::Type::W32, true) { |
|
| 815 | 805 | return try resolveVal(s, e, super::SCRATCH1, index); |
|
| 816 | 806 | } |
|
| 817 | 807 | try loadU32(s, e, super::SCRATCH1, super::SCRATCH1, index); |
|
| 818 | 808 | return super::SCRATCH1; |
|
| 819 | 809 | } |
| 1288 | 1278 | try emitAddOffset(e, rd, rbase, (k * size as u64) as i64); |
|
| 1289 | 1279 | return; |
|
| 1290 | 1280 | } |
|
| 1291 | 1281 | // A sign-extended index above the largest one fails the check, so the |
|
| 1292 | 1282 | // check proves that it equals its zero extension. |
|
| 1293 | - | let ri = try resolveIndex(s, e, index, count <= MAX_SIGNED_INDEX_COUNT); |
|
| 1283 | + | let ri = try resolveIndex(s, e, index); |
|
| 1294 | 1284 | try emit::loadImm(e, super::ADDR_SCRATCH, count as i64); |
|
| 1295 | 1285 | try emitTrapUnlessLtu(e, ri, super::ADDR_SCRATCH); |
|
| 1296 | 1286 | if size == 1 { |
|
| 1297 | 1287 | try emit::emit(e, encode::add(rd, rbase, ri)); |
|
| 1298 | 1288 | } else { |
| 1302 | 1292 | } |
|
| 1303 | 1293 | case il::Instr::SliceElem { dst, slice, index, layout } => { |
|
| 1304 | 1294 | let size = layouts[layout].size; |
|
| 1305 | 1295 | let rd = getDstReg(s, dst, super::SCRATCH1); |
|
| 1306 | 1296 | let rslice = try getSrcReg(s, e, slice, super::SCRATCH2); |
|
| 1307 | - | // A slice can be longer than 2^31 elements, so only a zero-extended |
|
| 1308 | - | // index is used as it is. |
|
| 1309 | - | let ri = try resolveIndex(s, e, index, false); |
|
| 1297 | + | // Slices are shorter than 2^31 elements, so a sign-extended index that |
|
| 1298 | + | // passes the check equals its zero extension. A longer slice, which |
|
| 1299 | + | // only hostile RIL can build, traps for every index of 2^31 or more. |
|
| 1300 | + | let ri = try resolveIndex(s, e, index); |
|
| 1310 | 1301 | try emit::emitLoad(e, super::ADDR_SCRATCH, rslice, SLICE_LEN_OFFSET, il::Type::W32); |
|
| 1311 | 1302 | try emitTrapUnlessLtu(e, ri, super::ADDR_SCRATCH); |
|
| 1312 | 1303 | let mut offset = ri; |
|
| 1313 | 1304 | if size <> 1 { |
|
| 1314 | 1305 | try emitScale(e, super::SCRATCH1, ri, size, super::SCRATCH3); |
| 2424 | 2415 | } |
|
| 2425 | 2416 | } else { |
|
| 2426 | 2417 | try emit::loadImm(e, super::ADDR_SCRATCH, elementSize as i64); |
|
| 2427 | 2418 | try emit::emit(e, encode::divu(super::A1, super::A1, super::ADDR_SCRATCH)); |
|
| 2428 | 2419 | } |
|
| 2429 | - | // Clamp the length to 32 bits. |
|
| 2430 | - | try emit::emit(e, encode::srli(super::ADDR_SCRATCH, super::A1, 32)); |
|
| 2420 | + | // Clamp the length to `types::MAX_LENGTH`, which is 31 bits. |
|
| 2421 | + | try emit::emit(e, encode::srli(super::ADDR_SCRATCH, super::A1, 31)); |
|
| 2431 | 2422 | try emit::emit(e, encode::beq(super::ADDR_SCRATCH, super::ZERO, super::INSTR_SIZE * 3)); |
|
| 2432 | 2423 | try emit::emit(e, encode::addi(super::A1, super::ZERO, -1)); |
|
| 2433 | - | try emit::emit(e, encode::srli(super::A1, super::A1, 32)); |
|
| 2424 | + | try emit::emit(e, encode::srli(super::A1, super::A1, 33)); |
|
| 2434 | 2425 | try emit::emit(e, encode::sd(super::A0, super::SCRATCH3, SLICE_PTR_OFFSET)); |
|
| 2435 | 2426 | try emit::emit(e, encode::sw(super::A1, super::SCRATCH3, SLICE_LEN_OFFSET)); |
|
| 2436 | 2427 | try emit::emit(e, encode::sw(super::A1, super::SCRATCH3, SLICE_CAP_OFFSET)); |
|
| 2437 | 2428 | try emit::emit(e, encode::mv(super::SCRATCH1, super::ZERO)); |
|
| 2438 | 2429 | let skip = (e.code.len - branch) as i32 * super::INSTR_SIZE; |
lib/std/arch/rv64/tests.rad
+25 -3
| 707 | 707 | op: il::BinOp::Add, typ: il::Type::W64, dst: il::Reg { n: 5 }, a: reg(1), b: reg(3) |
|
| 708 | 708 | }; |
|
| 709 | 709 | try testing::expect( |
|
| 710 | 710 | wordExtensions(try generateBody(&table[..], nil, &[sum, loaded, mixed, wide])) == 1 |
|
| 711 | 711 | ); |
|
| 712 | - | // A computed index of a slice is zero-extended, so that slices longer than 2^31 |
|
| 713 | - | // elements keep every index. |
|
| 712 | + | // Slices are shorter than 2^31 elements, so a computed index is used as it is. |
|
| 714 | 713 | let computed = il::Instr::SliceElem { |
|
| 715 | 714 | dst: il::Reg { n: 4 }, slice: il::Reg { n: 1 }, index: reg(2), layout: 0 |
|
| 716 | 715 | }; |
|
| 717 | - | try testing::expect(wordExtensions(try generateBody(&table[..], nil, &[sum, computed])) == 1); |
|
| 716 | + | try testing::expect(wordExtensions(try generateBody(&table[..], nil, &[sum, computed])) == 0); |
|
| 717 | + | // An index of unknown extension is still zero-extended. |
|
| 718 | + | let unknown = il::Instr::SliceElem { |
|
| 719 | + | dst: il::Reg { n: 4 }, slice: il::Reg { n: 1 }, index: reg(0), layout: 0 |
|
| 720 | + | }; |
|
| 721 | + | try testing::expect(wordExtensions(try generateBody(&table[..], nil, &[unknown])) == 1); |
|
| 718 | 722 | } |
|
| 719 | 723 | ||
| 720 | 724 | /// A loaded word whose high bits nothing observes is sign-extended like word |
|
| 721 | 725 | /// arithmetic, so comparing them needs no extension. A loaded word that a 64-bit |
|
| 722 | 726 | /// operation reads stays zero-extended. |
| 740 | 744 | }; |
|
| 741 | 745 | let widened = try generateBody(&table[..], nil, &[sum, loaded, mixed, wide]); |
|
| 742 | 746 | try testing::expect(loads(widened, 6) == 1); |
|
| 743 | 747 | } |
|
| 744 | 748 | ||
| 749 | + | /// A page slice holds at most `MAX_LENGTH` elements: a longer page is clamped, |
|
| 750 | + | /// like every slice length, to 2^31 - 1. |
|
| 751 | + | @test unsafe fn testPageSliceLengthLimit() throws (testing::TestError) { |
|
| 752 | + | let table = [il::Layout { size: 1, alignment: 1, slots: &[] }]; |
|
| 753 | + | let page = il::Instr::PageSlice { |
|
| 754 | + | err: il::Reg { n: 2 }, slot: il::Reg { n: 0 }, handle: reg(1), write: false, layout: 0 |
|
| 755 | + | }; |
|
| 756 | + | let code = try generateBody(&table[..], nil, &[page]); |
|
| 757 | + | let mut clamped = false; |
|
| 758 | + | for word in code { |
|
| 759 | + | try testing::expect(word <> encode::srli(rv64::A1, rv64::A1, 32)); |
|
| 760 | + | if word == encode::srli(rv64::A1, rv64::A1, 33) { |
|
| 761 | + | set clamped = true; |
|
| 762 | + | } |
|
| 763 | + | } |
|
| 764 | + | try testing::expect(clamped); |
|
| 765 | + | } |
|
| 766 | + | ||
| 745 | 767 | /// Number of loads with `funct3` in `code`: 2 for `lw` and 6 for `lwu`. |
|
| 746 | 768 | fn loads(code: *[u32], funct3: u32) -> u32 { |
|
| 747 | 769 | let mut count: u32 = 0; |
|
| 748 | 770 | for word in code { |
|
| 749 | 771 | if (word & 0x7f) == 0x03 and ((word >> 12) & 7) == funct3 { |
lib/std/lang/lower.rad
+62 -18
| 8860 | 8860 | set capVal = try lowerExpr(self, args[2]); |
|
| 8861 | 8861 | } |
|
| 8862 | 8862 | if not isLtEq(lenVal, capVal) { |
|
| 8863 | 8863 | try emitTrapIfLt(self, il::Type::W32, capVal, lenVal); |
|
| 8864 | 8864 | } |
|
| 8865 | + | // The length does not exceed the capacity, so a capacity within the limit |
|
| 8866 | + | // keeps the length within it. |
|
| 8867 | + | let maxVal = il::Val::Imm(types::MAX_LENGTH as i64); |
|
| 8868 | + | if not isLtEq(capVal, maxVal) { |
|
| 8869 | + | try emitTrapIfLt(self, il::Type::W32, maxVal, capVal); |
|
| 8870 | + | } |
|
| 8865 | 8871 | return try buildRawSliceValue(self, item, mutable, ptrVal, lenVal, capVal); |
|
| 8866 | 8872 | } |
|
| 8867 | 8873 | ||
| 8868 | 8874 | /// Lower a `try` expression. |
|
| 8869 | 8875 | fn lowerTry(self: &mut Lowerer, t: ast::Try, tryExprTy: resolver::Type) -> il::Val |
| 9246 | 9252 | self, dst, src, il::Val::Imm(0), wordEnd, COPY_WORD_MAX, label |
|
| 9247 | 9253 | ); |
|
| 9248 | 9254 | try emitCopyLoop(self, dst, src, il::Val::Reg(tailStart), bytes, tailWord, label); |
|
| 9249 | 9255 | } |
|
| 9250 | 9256 | ||
| 9251 | - | /// Largest slice length whose elements fit in u32 bytes. |
|
| 9257 | + | /// Largest length of a slice of elements of `stride` bytes: its size in bytes must |
|
| 9258 | + | /// fit in u32, and its length must not exceed [`types::MAX_LENGTH`]. Zero-sized |
|
| 9259 | + | /// elements are limited only by the element count. |
|
| 9252 | 9260 | fn maxSliceLength(stride: u32) -> u32 { |
|
| 9253 | 9261 | if stride == 0 { |
|
| 9254 | - | return parser::U32_MAX; |
|
| 9262 | + | return types::MAX_LENGTH; |
|
| 9255 | 9263 | } |
|
| 9256 | - | return parser::U32_MAX / stride; |
|
| 9264 | + | let limit = parser::U32_MAX / stride; |
|
| 9265 | + | return limit if limit < types::MAX_LENGTH else types::MAX_LENGTH; |
|
| 9257 | 9266 | } |
|
| 9258 | 9267 | ||
| 9259 | 9268 | /// Lower `slice.append(val, allocator)` and return its result aggregate. |
|
| 9260 | 9269 | /// |
|
| 9261 | 9270 | /// Emits inline grow-if-needed logic: |
| 9263 | 9272 | /// load len, cap from slice header |
|
| 9264 | 9273 | /// if len < cap: jmp @store |
|
| 9265 | 9274 | /// else: jmp @grow |
|
| 9266 | 9275 | /// |
|
| 9267 | 9276 | /// @grow: |
|
| 9268 | - | /// limit = U32_MAX / stride |
|
| 9269 | - | /// trap if cap > (limit - 1) / 2 |
|
| 9270 | - | /// newCap = cap * 2 + 1 |
|
| 9277 | + | /// limit = min(U32_MAX / stride, MAX_LENGTH) |
|
| 9278 | + | /// trap if cap >= limit |
|
| 9279 | + | /// newCap = cap * 2 + 1 if it fits, otherwise cap + 1 |
|
| 9271 | 9280 | /// bytes = newCap * stride |
|
| 9272 | 9281 | /// call allocator.func(result, allocator.data, bytes, alignment) |
|
| 9273 | 9282 | /// if allocation failed: return error result |
|
| 9274 | 9283 | /// copy old data to new pointer |
|
| 9275 | 9284 | /// update slice ptr and cap |
| 9327 | 9336 | try emitBrCmp(self, il::CmpOp::Ult, il::Type::W32, lenVal, capVal, storeBlock, growBlock); |
|
| 9328 | 9337 | try switchToAndSeal(self, growBlock); |
|
| 9329 | 9338 | ||
| 9330 | 9339 | // -- @grow block ---------------------------------------------------------- |
|
| 9331 | 9340 | ||
| 9332 | - | // Each allocation size and slice length must fit in u32. |
|
| 9341 | + | // The next element must fit within the slice length limit. |
|
| 9333 | 9342 | let limit = maxSliceLength(stride); |
|
| 9334 | - | try emitTrapUnlessCmp(self, il::CmpOp::Ult, il::Type::W32, capVal, il::Val::Imm( |
|
| 9335 | - | ((limit - 1) / 2 + 1) as i64 |
|
| 9336 | - | )); |
|
| 9343 | + | try emitTrapUnlessCmp(self, il::CmpOp::Ult, il::Type::W32, capVal, il::Val::Imm(limit as i64)); |
|
| 9344 | + | let newCapReg = nextReg(self); |
|
| 9345 | + | let allocBlock = try createBlockWithParam(self, "append.alloc", il::Param { |
|
| 9346 | + | value: newCapReg, type: il::ValType::Data(il::Type::W32) |
|
| 9347 | + | }); |
|
| 9348 | + | let doubleBlock = try createBlock(self, "append.double"); |
|
| 9349 | + | let exactBlock = try createBlock(self, "append.exact"); |
|
| 9350 | + | try emitBrCmp( |
|
| 9351 | + | self, |
|
| 9352 | + | il::CmpOp::Ult, |
|
| 9353 | + | il::Type::W32, |
|
| 9354 | + | capVal, |
|
| 9355 | + | il::Val::Imm(((limit - 1) / 2 + 1) as i64), |
|
| 9356 | + | doubleBlock, |
|
| 9357 | + | exactBlock |
|
| 9358 | + | ); |
|
| 9337 | 9359 | ||
| 9338 | - | // Grow capacity to `cap * 2 + 1`. |
|
| 9360 | + | // Double capacity when it fits, and otherwise allocate one more element. |
|
| 9361 | + | try switchToAndSeal(self, doubleBlock); |
|
| 9339 | 9362 | let doubledVal = try emitTypedBinOp(self, il::BinOp::Shl, il::Type::W32, capVal, il::Val::Imm( |
|
| 9340 | 9363 | 1 |
|
| 9341 | 9364 | )); |
|
| 9342 | - | let newCapVal = try emitTypedBinOp(self, il::BinOp::Or, il::Type::W32, doubledVal, il::Val::Imm( |
|
| 9365 | + | let twiceVal = try emitTypedBinOp(self, il::BinOp::Or, il::Type::W32, doubledVal, il::Val::Imm( |
|
| 9343 | 9366 | 1 |
|
| 9344 | 9367 | )); |
|
| 9368 | + | try emitJmpWithArg(self, allocBlock, twiceVal); |
|
| 9369 | + | ||
| 9370 | + | try switchToAndSeal(self, exactBlock); |
|
| 9371 | + | let exactVal = try emitTypedBinOp(self, il::BinOp::Add, il::Type::W32, capVal, il::Val::Imm(1)); |
|
| 9372 | + | try emitJmpWithArg(self, allocBlock, exactVal); |
|
| 9373 | + | ||
| 9374 | + | try switchToAndSeal(self, allocBlock); |
|
| 9375 | + | let newCapVal = il::Val::Reg(newCapReg); |
|
| 9345 | 9376 | ||
| 9346 | - | // Allocate `newCap * stride` bytes, then copy the old elements and publish |
|
| 9347 | - | // the new pointer and capacity. |
|
| 9377 | + | // Zero-sized elements need capacity but no storage. |
|
| 9348 | 9378 | if stride == 0 { |
|
| 9349 | 9379 | try emitStoreW32At(self, newCapVal, sliceReg, SLICE_CAP_OFFSET); |
|
| 9350 | 9380 | } else { |
|
| 9381 | + | // Allocate `newCap * stride` bytes, then copy the old elements and publish |
|
| 9382 | + | // the new pointer and capacity. |
|
| 9351 | 9383 | let byteSize = try emitTypedBinOp( |
|
| 9352 | - | self, il::BinOp::Mul, il::Type::W32, newCapVal, il::Val::Imm(stride as i64) |
|
| 9384 | + | self, |
|
| 9385 | + | il::BinOp::Mul, |
|
| 9386 | + | il::Type::W32, |
|
| 9387 | + | newCapVal, |
|
| 9388 | + | il::Val::Imm( |
|
| 9389 | + | stride as i64 |
|
| 9390 | + | ) |
|
| 9353 | 9391 | ); |
|
| 9354 | 9392 | try emitSliceAlloc(self, allocResult, allocReg, byteSize, alignment); |
|
| 9355 | 9393 | try emitAllocForward(self, allocResult, result, info, doneBlock, "append.copy"); |
|
| 9356 | 9394 | try emitSliceGrowCopy(self, allocResult, sliceReg, lenVal, elemType, newCapVal, "append"); |
|
| 9357 | 9395 | } |
| 9449 | 9487 | let newLenVal = try emitTypedBinOp(self, il::BinOp::Add, il::Type::W32, lenVal, countVal); |
|
| 9450 | 9488 | if stride == 0 { |
|
| 9451 | 9489 | try emitStoreW32At(self, newLenVal, sliceReg, SLICE_CAP_OFFSET); |
|
| 9452 | 9490 | } else { |
|
| 9453 | 9491 | let byteSize = try emitTypedBinOp( |
|
| 9454 | - | self, il::BinOp::Mul, il::Type::W32, newLenVal, il::Val::Imm(stride as i64) |
|
| 9492 | + | self, |
|
| 9493 | + | il::BinOp::Mul, |
|
| 9494 | + | il::Type::W32, |
|
| 9495 | + | newLenVal, |
|
| 9496 | + | il::Val::Imm( |
|
| 9497 | + | stride as i64 |
|
| 9498 | + | ) |
|
| 9455 | 9499 | ); |
|
| 9456 | 9500 | try emitSliceAlloc(self, result, allocReg, byteSize, alignment); |
|
| 9457 | 9501 | ||
| 9458 | 9502 | try emitAllocBranch(self, result, doneBlock, "reserve.copy"); |
|
| 9459 | 9503 | try emitSliceGrowCopy(self, result, sliceReg, lenVal, elemType, newLenVal, "reserve"); |
| 9600 | 9644 | /// A smaller `len` only sets the length. Growth within capacity fills the new |
|
| 9601 | 9645 | /// slots. Growth past capacity traps unless `len` fits in u32 bytes, allocates |
|
| 9602 | 9646 | /// exactly `len` elements, copies the old elements, and publishes the new |
|
| 9603 | 9647 | /// pointer and capacity. The fill runs after that, and the length changes last, |
|
| 9604 | 9648 | /// so a failed allocation leaves the slice unchanged. |
|
| 9605 | - | /// Zero-sized elements need no allocation. |
|
| 9649 | + | /// Zero-sized elements need no allocation or fill. |
|
| 9606 | 9650 | /// |
|
| 9607 | 9651 | /// load len, cap from slice header |
|
| 9608 | 9652 | /// if newLen < len: jmp @shrink |
|
| 9609 | 9653 | /// if cap < newLen: jmp @grow (else @fill) |
|
| 9610 | 9654 | /// |
| 9725 | 9769 | /// names the receiver still copies its own elements. A new length that fits in |
|
| 9726 | 9770 | /// the capacity needs no other check. Growth traps unless `len + src.len` fits in |
|
| 9727 | 9771 | /// u32 bytes, and allocates `max(cap * 2 + 1, newLen)` elements when the doubled |
|
| 9728 | 9772 | /// capacity fits in u32 bytes, and `newLen` elements otherwise. The slice header |
|
| 9729 | 9773 | /// changes only after allocation succeeds. |
|
| 9730 | - | /// Zero-sized elements need no allocation. |
|
| 9774 | + | /// Zero-sized elements need no allocation or copy. |
|
| 9731 | 9775 | /// |
|
| 9732 | 9776 | /// load len, cap, src.len, src.ptr |
|
| 9733 | 9777 | /// newLen = len + src.len (64-bit, cannot wrap) |
|
| 9734 | 9778 | /// if cap < newLen: jmp @grow (else @place) |
|
| 9735 | 9779 | /// |
lib/std/lang/resolver.rad
+14 -2
| 544 | 544 | SymbolOverflow, |
|
| 545 | 545 | /// Expression has the wrong type. |
|
| 546 | 546 | TypeMismatch(TypeMismatch), |
|
| 547 | 547 | /// Numeric literal does not fit within the required range. |
|
| 548 | 548 | NumericLiteralOverflow, |
|
| 549 | + | /// Array length exceeds [`types::MAX_LENGTH`]. |
|
| 550 | + | ArrayTooLong, |
|
| 549 | 551 | /// Record literal omitted a required field. |
|
| 550 | 552 | RecordFieldMissing(*[u8]), |
|
| 551 | 553 | /// Record representation is private to another module. |
|
| 552 | 554 | OpaqueRecordAccess, |
|
| 553 | 555 | /// Record literal referenced a field that does not exist. |
| 3950 | 3952 | setNodeType(self, node, Type::U32); |
|
| 3951 | 3953 | ||
| 3952 | 3954 | return int.magnitude as u32; |
|
| 3953 | 3955 | } |
|
| 3954 | 3956 | ||
| 3957 | + | /// Check the length of an array type or repeated array literal: a compile-time |
|
| 3958 | + | /// constant of at most [`types::MAX_LENGTH`] elements. |
|
| 3959 | + | fn checkArrayLength(self: &mut Resolver, node: *ast::Node) -> u32 throws (ResolveError) { |
|
| 3960 | + | let length = try checkSizeInt(self, node); |
|
| 3961 | + | if length > types::MAX_LENGTH { |
|
| 3962 | + | throw emitError(self, node, ErrorKind::ArrayTooLong); |
|
| 3963 | + | } |
|
| 3964 | + | return length; |
|
| 3965 | + | } |
|
| 3966 | + | ||
| 3955 | 3967 | /// Check that constructor arguments match record fields. |
|
| 3956 | 3968 | /// |
|
| 3957 | 3969 | /// Verifies argument count matches field count, and that each argument is |
|
| 3958 | 3970 | /// assignable to its corresponding field type. |
|
| 3959 | 3971 | fn checkRecordConstructorArgs( |
| 7393 | 7405 | setNodeCoercion(self, lit.item, coercion); |
|
| 7394 | 7406 | set valueTy = elemTy; |
|
| 7395 | 7407 | } |
|
| 7396 | 7408 | } |
|
| 7397 | 7409 | } |
|
| 7398 | - | let count = try checkSizeInt(self, lit.count); |
|
| 7410 | + | let count = try checkArrayLength(self, lit.count); |
|
| 7399 | 7411 | let arrayTy = Type::Array(ArrayType { |
|
| 7400 | 7412 | item: allocType(self, valueTy), |
|
| 7401 | 7413 | length: count, |
|
| 7402 | 7414 | }); |
|
| 7403 | 7415 | return setNodeType(self, node, arrayTy); |
| 8901 | 8913 | } |
|
| 8902 | 8914 | } |
|
| 8903 | 8915 | } |
|
| 8904 | 8916 | case ast::TypeSig::Array { itemType, length } => { |
|
| 8905 | 8917 | let item = try infer(self, itemType); |
|
| 8906 | - | let length = try checkSizeInt(self, length); |
|
| 8918 | + | let length = try checkArrayLength(self, length); |
|
| 8907 | 8919 | ||
| 8908 | 8920 | return Type::Array(ArrayType { item: allocType(self, item), length }); |
|
| 8909 | 8921 | } |
|
| 8910 | 8922 | case ast::TypeSig::Slice { class, itemType, mutable } => { |
|
| 8911 | 8923 | let item = try infer(self, itemType); |
lib/std/lang/resolver/printer.rad
+3 -0
| 431 | 431 | io::print("too many symbols in one scope"); |
|
| 432 | 432 | } |
|
| 433 | 433 | case super::ErrorKind::NumericLiteralOverflow => { |
|
| 434 | 434 | io::print("numeric literal overflow"); |
|
| 435 | 435 | } |
|
| 436 | + | case super::ErrorKind::ArrayTooLong => { |
|
| 437 | + | io::print("array length must be below 2^31"); |
|
| 438 | + | } |
|
| 436 | 439 | case super::ErrorKind::RecordFieldStyleMismatch => { |
|
| 437 | 440 | io::print("brace syntax not allowed for tuple-style records"); |
|
| 438 | 441 | } |
|
| 439 | 442 | case super::ErrorKind::ExpectedIdentifier => { |
|
| 440 | 443 | io::print("expected identifier"); |
lib/std/lang/resolver/tests/structure.rad
+21 -0
| 1188 | 1188 | let case resolver::SymbolData::Constant { type: resolver::Type::Array(arrType), .. } = |
|
| 1189 | 1189 | a.symbols[*sym].data else throw testing::TestError::Failed; |
|
| 1190 | 1190 | try testing::expect(arrType.length == 3); |
|
| 1191 | 1191 | } |
|
| 1192 | 1192 | ||
| 1193 | + | /// An array type can have a length of up to 2^31 - 1 elements. |
|
| 1194 | + | @test unsafe fn testArrayLengthAtLimit() throws (testing::TestError) { |
|
| 1195 | + | let mut a = testResolver(); |
|
| 1196 | + | let result = try resolveProgramStr(&mut a, "fn f(p: *[u8; 2147483647]) {}"); |
|
| 1197 | + | try expectNoErrors(&result); |
|
| 1198 | + | } |
|
| 1199 | + | ||
| 1200 | + | /// An array type of 2^31 elements is rejected. |
|
| 1201 | + | @test unsafe fn testArrayLengthAboveLimit() throws (testing::TestError) { |
|
| 1202 | + | let mut a = testResolver(); |
|
| 1203 | + | let result = try resolveProgramStr(&mut a, "fn f(p: *[u8; 2147483648]) {}"); |
|
| 1204 | + | try expectErrorKind(&result, resolver::ErrorKind::ArrayTooLong); |
|
| 1205 | + | } |
|
| 1206 | + | ||
| 1207 | + | /// A repeated array literal of 2^31 elements is rejected. |
|
| 1208 | + | @test unsafe fn testArrayRepeatAboveLimit() throws (testing::TestError) { |
|
| 1209 | + | let mut a = testResolver(); |
|
| 1210 | + | let result = try resolveProgramStr(&mut a, "fn f() { let a = [0; 2147483648]; }"); |
|
| 1211 | + | try expectErrorKind(&result, resolver::ErrorKind::ArrayTooLong); |
|
| 1212 | + | } |
|
| 1213 | + | ||
| 1193 | 1214 | /// Test that a record field can use a constant as its array length. |
|
| 1194 | 1215 | @test unsafe fn testRecordFieldWithConstArrayLength() throws (testing::TestError) { |
|
| 1195 | 1216 | let mut a = testResolver(); |
|
| 1196 | 1217 | let program = "constant SIZE: u32 = 4; record Buffer { data: [i32; SIZE], }"; |
|
| 1197 | 1218 | let result = try resolveProgramStr(&mut a, program); |
lib/std/lang/types.rad
+6 -0
| 1 | 1 | //! Shared Radiance language types. |
|
| 2 | 2 | ||
| 3 | + | /// Largest number of elements of an array or slice. Lengths are below 2^31, so |
|
| 4 | + | /// an index that passes a bounds check is the same value whether its 32 bits are |
|
| 5 | + | /// sign-extended or zero-extended. Array types beyond it are rejected, and code |
|
| 6 | + | /// that would create a longer slice traps. |
|
| 7 | + | export constant MAX_LENGTH: u32 = 0x7fffffff; |
|
| 8 | + | ||
| 3 | 9 | /// Ownership and safety class for pointer-like types. |
|
| 4 | 10 | export union PointerClass: Copy { |
|
| 5 | 11 | /// Owned pointer, eg. `*T`. |
|
| 6 | 12 | Owned, |
|
| 7 | 13 | /// Reference, borrowed pointer, eg. `&T`. |
test/runtime/kernel/dispatchcheck.rad
+5 -2
| 19 | 19 | use kernel::lifecycle; |
|
| 20 | 20 | use kernel::remote; |
|
| 21 | 21 | ||
| 22 | 22 | /// Delay before the first user window, in timer ticks. |
|
| 23 | 23 | constant START_DELAY: u64 = 1000000; |
|
| 24 | - | /// End of the first interrupted load, relative to the first window start. |
|
| 25 | - | constant FIRST_WINDOW_END: u64 = 14000000; |
|
| 24 | + | /// End of the first interrupted load, relative to the first window start. It |
|
| 25 | + | /// must fall after the loader reserves its output and before its compilation |
|
| 26 | + | /// ends. Both depend on the speed of the generated compiler: the load reserves |
|
| 27 | + | /// its output after about 4 million ticks and ends after about 12 million. |
|
| 28 | + | constant FIRST_WINDOW_END: u64 = 8000000; |
|
| 26 | 29 | /// End of the controller's cancellation window. |
|
| 27 | 30 | constant CANCEL_WINDOW_END: u64 = 50000000; |
|
| 28 | 31 | /// End of the retry loader's first window. |
|
| 29 | 32 | constant RETRY_WINDOW_END: u64 = 60000000; |
|
| 30 | 33 | /// End of the witness window. |
test/tests/builtin.sliceof.limit.rad
added
+16 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! `@sliceOf` traps for a length of 2^31 elements or more. |
|
| 3 | + | ||
| 4 | + | /// Return its argument, so that the length is not a constant. |
|
| 5 | + | fn id(n: u32) -> u32 { |
|
| 6 | + | return n; |
|
| 7 | + | } |
|
| 8 | + | ||
| 9 | + | @default unsafe fn main() -> i32 { |
|
| 10 | + | let mut arr: [u8; 4] = [1, 2, 3, 4]; |
|
| 11 | + | let ptr: *unsafe u8 = &arr[0]; |
|
| 12 | + | let small: *unsafe [u8] = @sliceOf(ptr, id(4), id(0x7fffffff)); |
|
| 13 | + | assert small.len == 4; |
|
| 14 | + | let large: *unsafe [u8] = @sliceOf(ptr, id(0x80000000)); |
|
| 15 | + | return large.len as i32; |
|
| 16 | + | } |
test/tests/slice.append.limit.rad
added
+38 -0
| 1 | + | //! returns: 0 |
|
| 2 | + | //! A full slice below the length limit reaches the allocator. |
|
| 3 | + | ||
| 4 | + | /// Allocation failure. |
|
| 5 | + | union Error: Copy { |
|
| 6 | + | /// No storage is available. |
|
| 7 | + | Full, |
|
| 8 | + | } |
|
| 9 | + | ||
| 10 | + | /// Slice allocation interface. |
|
| 11 | + | trait Allocator { |
|
| 12 | + | /// Allocate storage. |
|
| 13 | + | fn (&mut Allocator) allocate(size: u32, alignment: u32) -> *mut opaque throws (Error); |
|
| 14 | + | } |
|
| 15 | + | ||
| 16 | + | /// Allocator that records the requested size. |
|
| 17 | + | record Reject: Copy { |
|
| 18 | + | /// Most recently requested size. |
|
| 19 | + | seen: u32, |
|
| 20 | + | } |
|
| 21 | + | ||
| 22 | + | instance Allocator for Reject { |
|
| 23 | + | /// Record and reject the request. |
|
| 24 | + | fn (r: &mut Reject) allocate(size: u32, alignment: u32) -> *mut opaque throws (Error) { |
|
| 25 | + | set r.seen = size; |
|
| 26 | + | throw Error::Full; |
|
| 27 | + | } |
|
| 28 | + | } |
|
| 29 | + | ||
| 30 | + | @default unsafe fn main() -> i32 { |
|
| 31 | + | static data: [u8; 1] = [0]; |
|
| 32 | + | let mut reject = Reject { seen: 0 }; |
|
| 33 | + | let mut bytes: *mut [u8] = @sliceOf(&mut data[0], 0x40000000, 0x40000000); |
|
| 34 | + | try bytes.append(1, &mut reject) catch {}; |
|
| 35 | + | assert reject.seen == 0x40000001; |
|
| 36 | + | assert bytes.len == 0x40000000; |
|
| 37 | + | return 0; |
|
| 38 | + | } |
test/tests/slice.append.limit.trap.rad
added
+13 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! A full slice at the length limit traps before allocation. |
|
| 4 | + | ||
| 5 | + | use std::lang::alloc; |
|
| 6 | + | ||
| 7 | + | @default unsafe fn main(buf: *mut [u8; 64]) -> i32 { |
|
| 8 | + | static data: [u8; 1] = [0]; |
|
| 9 | + | let mut arena = alloc::new(&mut buf[..]); |
|
| 10 | + | let mut bytes: *mut [u8] = @sliceOf(&mut data[0], 0x7fffffff, 0x7fffffff); |
|
| 11 | + | try! bytes.append(1, &mut arena); |
|
| 12 | + | return 0; |
|
| 13 | + | } |
test/tests/slice.append.ril
+67 -60
| 16 | 16 | reserve %6 $L4; |
|
| 17 | 17 | load w32 %7 %4 8; |
|
| 18 | 18 | load w32 %8 %4 12; |
|
| 19 | 19 | br.ult w32 %7 %8 @append.store1 @append.grow3; |
|
| 20 | 20 | @append.store1 |
|
| 21 | - | load w64 %26 %4 0; |
|
| 22 | - | mul w64 %27 %7 4; |
|
| 23 | - | add w64 %28 %26 %27; |
|
| 24 | - | store w32 %2 %28 0; |
|
| 25 | - | add w32 %29 %7 1; |
|
| 26 | - | store w32 %29 %4 8; |
|
| 21 | + | load w64 %28 %4 0; |
|
| 22 | + | mul w64 %29 %7 4; |
|
| 23 | + | add w64 %30 %28 %29; |
|
| 24 | + | store w32 %2 %30 0; |
|
| 25 | + | add w32 %31 %7 1; |
|
| 26 | + | store w32 %31 %4 8; |
|
| 27 | 27 | store w64 0 %5 0; |
|
| 28 | - | field %30 %5 8; |
|
| 29 | - | blit %30 %4 $L1; |
|
| 28 | + | field %32 %5 8; |
|
| 29 | + | blit %32 %4 $L1; |
|
| 30 | 30 | jmp @append.done2; |
|
| 31 | 31 | @append.done2 |
|
| 32 | - | load w64 %31 %5 0; |
|
| 33 | - | reserve %32 $L1; |
|
| 34 | - | br.ne w64 %31 0 @err12 @ok11; |
|
| 32 | + | load w64 %33 %5 0; |
|
| 33 | + | reserve %34 $L1; |
|
| 34 | + | br.ne w64 %33 0 @err15 @ok14; |
|
| 35 | 35 | @append.grow3 |
|
| 36 | - | br.ult w32 %8 536870912 @guard#pass4 @guard#trap5; |
|
| 36 | + | br.ult w32 %8 1073741823 @guard#pass4 @guard#trap5; |
|
| 37 | 37 | @guard#pass4 |
|
| 38 | - | shl w32 %9 %8 1; |
|
| 39 | - | or w32 %10 %9 1; |
|
| 40 | - | mul w32 %11 %10 4; |
|
| 41 | - | load w64 %13 %3 0; |
|
| 42 | - | load w64 %14 %3 8; |
|
| 43 | - | load w64 %12 %14 0; |
|
| 44 | - | call void %12(%6, %13, %11, 4); |
|
| 45 | - | load w64 %15 %6 0; |
|
| 46 | - | br.ne w64 %15 0 @append.copy6 @append.copy7; |
|
| 38 | + | br.ult w32 %8 536870912 @append.double7 @append.exact8; |
|
| 47 | 39 | @guard#trap5 |
|
| 48 | 40 | ebreak; |
|
| 49 | 41 | unreachable; |
|
| 50 | - | @append.copy6 |
|
| 51 | - | store w64 %15 %5 0; |
|
| 52 | - | field %16 %6 8; |
|
| 53 | - | field %17 %5 24; |
|
| 54 | - | blit %17 %16 $L3; |
|
| 42 | + | @append.alloc6(w32 %9) |
|
| 43 | + | mul w32 %13 %9 4; |
|
| 44 | + | load w64 %15 %3 0; |
|
| 45 | + | load w64 %16 %3 8; |
|
| 46 | + | load w64 %14 %16 0; |
|
| 47 | + | call void %14(%6, %15, %13, 4); |
|
| 48 | + | load w64 %17 %6 0; |
|
| 49 | + | br.ne w64 %17 0 @append.copy9 @append.copy10; |
|
| 50 | + | @append.double7 |
|
| 51 | + | shl w32 %10 %8 1; |
|
| 52 | + | or w32 %11 %10 1; |
|
| 53 | + | jmp @append.alloc6(%11); |
|
| 54 | + | @append.exact8 |
|
| 55 | + | add w32 %12 %8 1; |
|
| 56 | + | jmp @append.alloc6(%12); |
|
| 57 | + | @append.copy9 |
|
| 58 | + | store w64 %17 %5 0; |
|
| 59 | + | field %18 %6 8; |
|
| 60 | + | field %19 %5 24; |
|
| 61 | + | blit %19 %18 $L3; |
|
| 55 | 62 | jmp @append.done2; |
|
| 56 | - | @append.copy7 |
|
| 57 | - | load w64 %18 %6 8; |
|
| 58 | - | load w64 %19 %4 0; |
|
| 59 | - | mul w64 %20 %7 4; |
|
| 60 | - | jmp @append8(0); |
|
| 61 | - | @append8(w64 %21) |
|
| 62 | - | br.ult w64 %21 %20 @append9 @append10; |
|
| 63 | - | @append9 |
|
| 64 | - | add w64 %22 %19 %21; |
|
| 65 | - | load w32 %23 %22 0; |
|
| 66 | - | add w64 %24 %18 %21; |
|
| 67 | - | store w32 %23 %24 0; |
|
| 68 | - | add w64 %25 %21 4; |
|
| 69 | - | jmp @append8(%25); |
|
| 70 | - | @append10 |
|
| 71 | - | store w64 %18 %4 0; |
|
| 72 | - | store w32 %10 %4 12; |
|
| 63 | + | @append.copy10 |
|
| 64 | + | load w64 %20 %6 8; |
|
| 65 | + | load w64 %21 %4 0; |
|
| 66 | + | mul w64 %22 %7 4; |
|
| 67 | + | jmp @append11(0); |
|
| 68 | + | @append11(w64 %23) |
|
| 69 | + | br.ult w64 %23 %22 @append12 @append13; |
|
| 70 | + | @append12 |
|
| 71 | + | add w64 %24 %21 %23; |
|
| 72 | + | load w32 %25 %24 0; |
|
| 73 | + | add w64 %26 %20 %23; |
|
| 74 | + | store w32 %25 %26 0; |
|
| 75 | + | add w64 %27 %23 4; |
|
| 76 | + | jmp @append11(%27); |
|
| 77 | + | @append13 |
|
| 78 | + | store w64 %20 %4 0; |
|
| 79 | + | store w32 %9 %4 12; |
|
| 73 | 80 | jmp @append.store1; |
|
| 74 | - | @ok11 |
|
| 75 | - | field %33 %5 8; |
|
| 76 | - | blit %32 %33 $L1; |
|
| 77 | - | jmp @merge13; |
|
| 78 | - | @err12 |
|
| 79 | - | reserve %34 $L2; |
|
| 80 | - | store w64 %31 %34 0; |
|
| 81 | - | field %35 %5 24; |
|
| 82 | - | field %36 %34 24; |
|
| 83 | - | blit %36 %35 $L3; |
|
| 84 | - | blit %0 %34 $L2; |
|
| 81 | + | @ok14 |
|
| 82 | + | field %35 %5 8; |
|
| 83 | + | blit %34 %35 $L1; |
|
| 84 | + | jmp @merge16; |
|
| 85 | + | @err15 |
|
| 86 | + | reserve %36 $L2; |
|
| 87 | + | store w64 %33 %36 0; |
|
| 88 | + | field %37 %5 24; |
|
| 89 | + | field %38 %36 24; |
|
| 90 | + | blit %38 %37 $L3; |
|
| 91 | + | blit %0 %36 $L2; |
|
| 85 | 92 | ret; |
|
| 86 | - | @merge13 |
|
| 87 | - | reserve %37 $L2; |
|
| 88 | - | store w64 0 %37 0; |
|
| 89 | - | field %38 %37 8; |
|
| 90 | - | blit %38 %32 $L1; |
|
| 91 | - | blit %0 %37 $L2; |
|
| 93 | + | @merge16 |
|
| 94 | + | reserve %39 $L2; |
|
| 95 | + | store w64 0 %39 0; |
|
| 96 | + | field %40 %39 8; |
|
| 97 | + | blit %40 %34 $L1; |
|
| 98 | + | blit %0 %39 $L2; |
|
| 92 | 99 | ret; |
|
| 93 | 100 | } |
test/tests/slice.append.trait.ril
+67 -60
| 16 | 16 | reserve %6 $L4; |
|
| 17 | 17 | load w32 %7 %4 8; |
|
| 18 | 18 | load w32 %8 %4 12; |
|
| 19 | 19 | br.ult w32 %7 %8 @append.store1 @append.grow3; |
|
| 20 | 20 | @append.store1 |
|
| 21 | - | load w64 %26 %4 0; |
|
| 22 | - | mul w64 %27 %7 4; |
|
| 23 | - | add w64 %28 %26 %27; |
|
| 24 | - | store w32 %2 %28 0; |
|
| 25 | - | add w32 %29 %7 1; |
|
| 26 | - | store w32 %29 %4 8; |
|
| 21 | + | load w64 %28 %4 0; |
|
| 22 | + | mul w64 %29 %7 4; |
|
| 23 | + | add w64 %30 %28 %29; |
|
| 24 | + | store w32 %2 %30 0; |
|
| 25 | + | add w32 %31 %7 1; |
|
| 26 | + | store w32 %31 %4 8; |
|
| 27 | 27 | store w64 0 %5 0; |
|
| 28 | - | field %30 %5 8; |
|
| 29 | - | blit %30 %4 $L1; |
|
| 28 | + | field %32 %5 8; |
|
| 29 | + | blit %32 %4 $L1; |
|
| 30 | 30 | jmp @append.done2; |
|
| 31 | 31 | @append.done2 |
|
| 32 | - | load w64 %31 %5 0; |
|
| 33 | - | reserve %32 $L1; |
|
| 34 | - | br.ne w64 %31 0 @err12 @ok11; |
|
| 32 | + | load w64 %33 %5 0; |
|
| 33 | + | reserve %34 $L1; |
|
| 34 | + | br.ne w64 %33 0 @err15 @ok14; |
|
| 35 | 35 | @append.grow3 |
|
| 36 | - | br.ult w32 %8 536870912 @guard#pass4 @guard#trap5; |
|
| 36 | + | br.ult w32 %8 1073741823 @guard#pass4 @guard#trap5; |
|
| 37 | 37 | @guard#pass4 |
|
| 38 | - | shl w32 %9 %8 1; |
|
| 39 | - | or w32 %10 %9 1; |
|
| 40 | - | mul w32 %11 %10 4; |
|
| 41 | - | load w64 %13 %3 0; |
|
| 42 | - | load w64 %14 %3 8; |
|
| 43 | - | load w64 %12 %14 0; |
|
| 44 | - | call void %12(%6, %13, %11, 4); |
|
| 45 | - | load w64 %15 %6 0; |
|
| 46 | - | br.ne w64 %15 0 @append.copy6 @append.copy7; |
|
| 38 | + | br.ult w32 %8 536870912 @append.double7 @append.exact8; |
|
| 47 | 39 | @guard#trap5 |
|
| 48 | 40 | ebreak; |
|
| 49 | 41 | unreachable; |
|
| 50 | - | @append.copy6 |
|
| 51 | - | store w64 %15 %5 0; |
|
| 52 | - | field %16 %6 8; |
|
| 53 | - | field %17 %5 24; |
|
| 54 | - | blit %17 %16 $L3; |
|
| 42 | + | @append.alloc6(w32 %9) |
|
| 43 | + | mul w32 %13 %9 4; |
|
| 44 | + | load w64 %15 %3 0; |
|
| 45 | + | load w64 %16 %3 8; |
|
| 46 | + | load w64 %14 %16 0; |
|
| 47 | + | call void %14(%6, %15, %13, 4); |
|
| 48 | + | load w64 %17 %6 0; |
|
| 49 | + | br.ne w64 %17 0 @append.copy9 @append.copy10; |
|
| 50 | + | @append.double7 |
|
| 51 | + | shl w32 %10 %8 1; |
|
| 52 | + | or w32 %11 %10 1; |
|
| 53 | + | jmp @append.alloc6(%11); |
|
| 54 | + | @append.exact8 |
|
| 55 | + | add w32 %12 %8 1; |
|
| 56 | + | jmp @append.alloc6(%12); |
|
| 57 | + | @append.copy9 |
|
| 58 | + | store w64 %17 %5 0; |
|
| 59 | + | field %18 %6 8; |
|
| 60 | + | field %19 %5 24; |
|
| 61 | + | blit %19 %18 $L3; |
|
| 55 | 62 | jmp @append.done2; |
|
| 56 | - | @append.copy7 |
|
| 57 | - | load w64 %18 %6 8; |
|
| 58 | - | load w64 %19 %4 0; |
|
| 59 | - | mul w64 %20 %7 4; |
|
| 60 | - | jmp @append8(0); |
|
| 61 | - | @append8(w64 %21) |
|
| 62 | - | br.ult w64 %21 %20 @append9 @append10; |
|
| 63 | - | @append9 |
|
| 64 | - | add w64 %22 %19 %21; |
|
| 65 | - | load w32 %23 %22 0; |
|
| 66 | - | add w64 %24 %18 %21; |
|
| 67 | - | store w32 %23 %24 0; |
|
| 68 | - | add w64 %25 %21 4; |
|
| 69 | - | jmp @append8(%25); |
|
| 70 | - | @append10 |
|
| 71 | - | store w64 %18 %4 0; |
|
| 72 | - | store w32 %10 %4 12; |
|
| 63 | + | @append.copy10 |
|
| 64 | + | load w64 %20 %6 8; |
|
| 65 | + | load w64 %21 %4 0; |
|
| 66 | + | mul w64 %22 %7 4; |
|
| 67 | + | jmp @append11(0); |
|
| 68 | + | @append11(w64 %23) |
|
| 69 | + | br.ult w64 %23 %22 @append12 @append13; |
|
| 70 | + | @append12 |
|
| 71 | + | add w64 %24 %21 %23; |
|
| 72 | + | load w32 %25 %24 0; |
|
| 73 | + | add w64 %26 %20 %23; |
|
| 74 | + | store w32 %25 %26 0; |
|
| 75 | + | add w64 %27 %23 4; |
|
| 76 | + | jmp @append11(%27); |
|
| 77 | + | @append13 |
|
| 78 | + | store w64 %20 %4 0; |
|
| 79 | + | store w32 %9 %4 12; |
|
| 73 | 80 | jmp @append.store1; |
|
| 74 | - | @ok11 |
|
| 75 | - | field %33 %5 8; |
|
| 76 | - | blit %32 %33 $L1; |
|
| 77 | - | jmp @merge13; |
|
| 78 | - | @err12 |
|
| 79 | - | reserve %34 $L2; |
|
| 80 | - | store w64 %31 %34 0; |
|
| 81 | - | field %35 %5 24; |
|
| 82 | - | field %36 %34 24; |
|
| 83 | - | blit %36 %35 $L3; |
|
| 84 | - | blit %0 %34 $L2; |
|
| 81 | + | @ok14 |
|
| 82 | + | field %35 %5 8; |
|
| 83 | + | blit %34 %35 $L1; |
|
| 84 | + | jmp @merge16; |
|
| 85 | + | @err15 |
|
| 86 | + | reserve %36 $L2; |
|
| 87 | + | store w64 %33 %36 0; |
|
| 88 | + | field %37 %5 24; |
|
| 89 | + | field %38 %36 24; |
|
| 90 | + | blit %38 %37 $L3; |
|
| 91 | + | blit %0 %36 $L2; |
|
| 85 | 92 | ret; |
|
| 86 | - | @merge13 |
|
| 87 | - | reserve %37 $L2; |
|
| 88 | - | store w64 0 %37 0; |
|
| 89 | - | field %38 %37 8; |
|
| 90 | - | blit %38 %32 $L1; |
|
| 91 | - | blit %0 %37 $L2; |
|
| 93 | + | @merge16 |
|
| 94 | + | reserve %39 $L2; |
|
| 95 | + | store w64 0 %39 0; |
|
| 96 | + | field %40 %39 8; |
|
| 97 | + | blit %40 %34 $L1; |
|
| 98 | + | blit %0 %39 $L2; |
|
| 92 | 99 | ret; |
|
| 93 | 100 | } |
test/tests/slice.basic.ril
+10 -0
| 30 | 30 | ret; |
|
| 31 | 31 | } |
|
| 32 | 32 | ||
| 33 | 33 | fn void $sliceOf(ptr ref mut $L0 %0, ptr own const $L1 %1, w32 %2) { |
|
| 34 | 34 | @entry0 |
|
| 35 | + | br.ult w32 2147483647 %2 @guard#trap1 @guard#pass2; |
|
| 36 | + | @guard#trap1 |
|
| 37 | + | ebreak; |
|
| 38 | + | unreachable; |
|
| 39 | + | @guard#pass2 |
|
| 35 | 40 | reserve %3 $L2; |
|
| 36 | 41 | store w64 %1 %3 0; |
|
| 37 | 42 | store w32 %2 %3 8; |
|
| 38 | 43 | store w32 %2 %3 12; |
|
| 39 | 44 | blit %0 %3 $L0; |
| 45 | 50 | br.ult w32 %3 %2 @guard#trap1 @guard#pass2; |
|
| 46 | 51 | @guard#trap1 |
|
| 47 | 52 | ebreak; |
|
| 48 | 53 | unreachable; |
|
| 49 | 54 | @guard#pass2 |
|
| 55 | + | br.ult w32 2147483647 %3 @guard#trap3 @guard#pass4; |
|
| 56 | + | @guard#trap3 |
|
| 57 | + | ebreak; |
|
| 58 | + | unreachable; |
|
| 59 | + | @guard#pass4 |
|
| 50 | 60 | reserve %4 $L2; |
|
| 51 | 61 | store w64 %1 %4 0; |
|
| 52 | 62 | store w32 %2 %4 8; |
|
| 53 | 63 | store w32 %3 %4 12; |
|
| 54 | 64 | blit %0 %4 $L0; |
test/tests/slice.extend.bytes.ril
+2 -2
| 24 | 24 | load w32 %7 %1 8; |
|
| 25 | 25 | load w64 %8 %1 0; |
|
| 26 | 26 | add w64 %9 %5 %7; |
|
| 27 | 27 | br.ult w64 %6 %9 @extend.grow1 @extend.place2; |
|
| 28 | 28 | @extend.grow1 |
|
| 29 | - | br.ult w64 %9 4294967296 @guard#pass4 @guard#trap5; |
|
| 29 | + | br.ult w64 %9 2147483648 @guard#pass4 @guard#trap5; |
|
| 30 | 30 | @extend.place2 |
|
| 31 | 31 | load w64 %26 %3 0; |
|
| 32 | 32 | add w64 %27 %26 %5; |
|
| 33 | 33 | or w64 %28 %27 %8; |
|
| 34 | 34 | and w64 %29 %28 7; |
| 38 | 38 | jmp @extend14(0); |
|
| 39 | 39 | @extend.done3 |
|
| 40 | 40 | load w64 %43 %4 0; |
|
| 41 | 41 | br.ne w64 %43 0 @err21 @ok20; |
|
| 42 | 42 | @guard#pass4 |
|
| 43 | - | br.ult w32 %6 2147483648 @extend.double7 @extend.exact8; |
|
| 43 | + | br.ult w32 %6 1073741824 @extend.double7 @extend.exact8; |
|
| 44 | 44 | @guard#trap5 |
|
| 45 | 45 | ebreak; |
|
| 46 | 46 | unreachable; |
|
| 47 | 47 | @extend.alloc6(w32 %10) |
|
| 48 | 48 | mul w32 %13 %10 1; |
test/tests/slice.reserve.limit.rad
added
+21 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! Growing a slice to 2^31 elements or more traps before it allocates. |
|
| 4 | + | //! A length at the limit reaches the allocator, which fails normally. |
|
| 5 | + | ||
| 6 | + | use std::lang::alloc; |
|
| 7 | + | ||
| 8 | + | @default fn main(buf: *mut [u8; 256]) -> i32 { |
|
| 9 | + | let mut arena = alloc::new(&mut buf[..]); |
|
| 10 | + | let mut bytes: *mut [u8] = &mut []; |
|
| 11 | + | try bytes.reserve(0x7fffffff, &mut arena) catch { |
|
| 12 | + | // Too large for the arena: the expected failure. |
|
| 13 | + | }; |
|
| 14 | + | assert bytes.len == 0; |
|
| 15 | + | try bytes.resize(0x7fffffff, 0, &mut arena) catch {}; |
|
| 16 | + | assert bytes.len == 0; |
|
| 17 | + | try bytes.reserve(0x80000000, &mut arena) catch { |
|
| 18 | + | return 1; |
|
| 19 | + | }; |
|
| 20 | + | return 2; |
|
| 21 | + | } |
test/tests/slice.zero-sized.limit.rad
added
+13 -0
| 1 | + | //! returns: 133 |
|
| 2 | + | //! uses-std |
|
| 3 | + | //! Zero-sized slices still obey the element count limit. |
|
| 4 | + | ||
| 5 | + | use std::lang::alloc; |
|
| 6 | + | ||
| 7 | + | @default fn main(buf: *mut [u8; 64]) -> i32 { |
|
| 8 | + | let mut allocator = alloc::new(&mut buf[..]); |
|
| 9 | + | let mut items: *mut [[u8; 0]] = &mut []; |
|
| 10 | + | let value: [u8; 0] = []; |
|
| 11 | + | try! items.resize(0x80000000, value, &mut allocator); |
|
| 12 | + | return 0; |
|
| 13 | + | } |
test/verify/expected
+4 -0
| 73 | 73 | return.never.catch Bounds |
|
| 74 | 74 | slice.alloc.loop Type |
|
| 75 | 75 | slice.alloc.trait Type |
|
| 76 | 76 | slice.append.errors Type |
|
| 77 | 77 | slice.append.failure Type |
|
| 78 | + | slice.append.limit Type |
|
| 79 | + | slice.append.limit.trap Symbol |
|
| 78 | 80 | slice.append.trait Type |
|
| 79 | 81 | slice.append Type |
|
| 80 | 82 | slice.append.values Type |
|
| 81 | 83 | slice.assign Type |
|
| 82 | 84 | slice.basic Slot |
| 94 | 96 | slice.header.unsafe Slot |
|
| 95 | 97 | slice.of Type |
|
| 96 | 98 | slice.ptr.unsafe Slot |
|
| 97 | 99 | slice.range.capacity Type |
|
| 98 | 100 | slice.range.dynamic Type |
|
| 101 | + | slice.reserve.limit Symbol |
|
| 99 | 102 | slice.reserve.result Type |
|
| 100 | 103 | slice.reserve Type |
|
| 101 | 104 | slice.resize.extend Type |
|
| 102 | 105 | slice.resize.fill.snapshot Type |
|
| 103 | 106 | slice.sizes Type |
|
| 104 | 107 | slice.subslice Type |
|
| 105 | 108 | slice.take.sizes Type |
|
| 106 | 109 | slice.take Type |
|
| 107 | 110 | slice.zero-sized Slot |
|
| 111 | + | slice.zero-sized.limit Symbol |
|
| 108 | 112 | static.empty.slice.optional Data |
|
| 109 | 113 | static.slice.empty Data |
|
| 110 | 114 | static.slice.empty.record.table Data |
|
| 111 | 115 | static.slice.empty.table Data |
|
| 112 | 116 | static.slice.offset Data |