]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/interpret/allocation.rs
Rollup merge of #61499 - varkor:issue-53457, r=oli-obk
[rust.git] / src / librustc / mir / interpret / allocation.rs
1 //! The virtual memory representation of the MIR interpreter.
2
3 use super::{
4     Pointer, EvalResult, AllocId, ScalarMaybeUndef, write_target_uint, read_target_uint, Scalar,
5 };
6
7 use crate::ty::layout::{Size, Align};
8 use syntax::ast::Mutability;
9 use std::{iter, fmt::{self, Display}};
10 use crate::mir;
11 use std::ops::{Deref, DerefMut};
12 use rustc_data_structures::sorted_map::SortedMap;
13 use rustc_macros::HashStable;
14 use rustc_target::abi::HasDataLayout;
15 use std::borrow::Cow;
16
17 /// Used by `check_bounds` to indicate whether the pointer needs to be just inbounds
18 /// or also inbounds of a *live* allocation.
19 #[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
20 pub enum InboundsCheck {
21     Live,
22     MaybeDead,
23 }
24
25 /// Used by `check_in_alloc` to indicate context of check
26 #[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
27 pub enum CheckInAllocMsg {
28     MemoryAccessTest,
29     NullPointerTest,
30     PointerArithmeticTest,
31     InboundsTest,
32 }
33
34 impl Display for CheckInAllocMsg {
35     /// When this is printed as an error the context looks like this
36     /// "{test name} failed: pointer must be in-bounds at offset..."
37     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38         write!(f, "{}", match *self {
39             CheckInAllocMsg::MemoryAccessTest => "Memory access",
40             CheckInAllocMsg::NullPointerTest => "Null pointer test",
41             CheckInAllocMsg::PointerArithmeticTest => "Pointer arithmetic",
42             CheckInAllocMsg::InboundsTest => "Inbounds test",
43         })
44     }
45 }
46
47 #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
48 pub struct Allocation<Tag=(),Extra=()> {
49     /// The actual bytes of the allocation.
50     /// Note that the bytes of a pointer represent the offset of the pointer
51     pub bytes: Vec<u8>,
52     /// Maps from byte addresses to extra data for each pointer.
53     /// Only the first byte of a pointer is inserted into the map; i.e.,
54     /// every entry in this map applies to `pointer_size` consecutive bytes starting
55     /// at the given offset.
56     pub relocations: Relocations<Tag>,
57     /// Denotes undefined memory. Reading from undefined memory is forbidden in miri
58     pub undef_mask: UndefMask,
59     /// The alignment of the allocation to detect unaligned reads.
60     pub align: Align,
61     /// Whether the allocation is mutable.
62     /// Also used by codegen to determine if a static should be put into mutable memory,
63     /// which happens for `static mut` and `static` with interior mutability.
64     pub mutability: Mutability,
65     /// Extra state for the machine.
66     pub extra: Extra,
67 }
68
69
70 pub trait AllocationExtra<Tag>: ::std::fmt::Debug + Clone {
71     // There is no constructor in here because the constructor's type depends
72     // on `MemoryKind`, and making things sufficiently generic leads to painful
73     // inference failure.
74
75     /// Hook for performing extra checks on a memory read access.
76     ///
77     /// Takes read-only access to the allocation so we can keep all the memory read
78     /// operations take `&self`. Use a `RefCell` in `AllocExtra` if you
79     /// need to mutate.
80     #[inline(always)]
81     fn memory_read(
82         _alloc: &Allocation<Tag, Self>,
83         _ptr: Pointer<Tag>,
84         _size: Size,
85     ) -> EvalResult<'tcx> {
86         Ok(())
87     }
88
89     /// Hook for performing extra checks on a memory write access.
90     #[inline(always)]
91     fn memory_written(
92         _alloc: &mut Allocation<Tag, Self>,
93         _ptr: Pointer<Tag>,
94         _size: Size,
95     ) -> EvalResult<'tcx> {
96         Ok(())
97     }
98
99     /// Hook for performing extra checks on a memory deallocation.
100     /// `size` will be the size of the allocation.
101     #[inline(always)]
102     fn memory_deallocated(
103         _alloc: &mut Allocation<Tag, Self>,
104         _ptr: Pointer<Tag>,
105         _size: Size,
106     ) -> EvalResult<'tcx> {
107         Ok(())
108     }
109 }
110
111 // For Tag=() and no extra state, we have is a trivial implementation.
112 impl AllocationExtra<()> for () { }
113
114 // The constructors are all without extra; the extra gets added by a machine hook later.
115 impl<Tag> Allocation<Tag> {
116     /// Creates a read-only allocation initialized by the given bytes
117     pub fn from_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>, align: Align) -> Self {
118         let bytes = slice.into().into_owned();
119         let undef_mask = UndefMask::new(Size::from_bytes(bytes.len() as u64), true);
120         Self {
121             bytes,
122             relocations: Relocations::new(),
123             undef_mask,
124             align,
125             mutability: Mutability::Immutable,
126             extra: (),
127         }
128     }
129
130     pub fn from_byte_aligned_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>) -> Self {
131         Allocation::from_bytes(slice, Align::from_bytes(1).unwrap())
132     }
133
134     pub fn undef(size: Size, align: Align) -> Self {
135         assert_eq!(size.bytes() as usize as u64, size.bytes());
136         Allocation {
137             bytes: vec![0; size.bytes() as usize],
138             relocations: Relocations::new(),
139             undef_mask: UndefMask::new(size, false),
140             align,
141             mutability: Mutability::Mutable,
142             extra: (),
143         }
144     }
145 }
146
147 impl<'tcx> ::serialize::UseSpecializedDecodable for &'tcx Allocation {}
148
149 /// Alignment and bounds checks
150 impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
151     /// Checks if the pointer is "in-bounds". Notice that a pointer pointing at the end
152     /// of an allocation (i.e., at the first *inaccessible* location) *is* considered
153     /// in-bounds!  This follows C's/LLVM's rules.
154     /// If you want to check bounds before doing a memory access, better use `check_bounds`.
155     fn check_bounds_ptr(
156         &self,
157         ptr: Pointer<Tag>,
158         msg: CheckInAllocMsg,
159     ) -> EvalResult<'tcx> {
160         let allocation_size = self.bytes.len() as u64;
161         ptr.check_in_alloc(Size::from_bytes(allocation_size), msg)
162     }
163
164     /// Checks if the memory range beginning at `ptr` and of size `Size` is "in-bounds".
165     #[inline(always)]
166     pub fn check_bounds(
167         &self,
168         cx: &impl HasDataLayout,
169         ptr: Pointer<Tag>,
170         size: Size,
171         msg: CheckInAllocMsg,
172     ) -> EvalResult<'tcx> {
173         // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow)
174         self.check_bounds_ptr(ptr.offset(size, cx)?, msg)
175     }
176 }
177
178 /// Byte accessors
179 impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
180     /// The last argument controls whether we error out when there are undefined
181     /// or pointer bytes. You should never call this, call `get_bytes` or
182     /// `get_bytes_with_undef_and_ptr` instead,
183     ///
184     /// This function also guarantees that the resulting pointer will remain stable
185     /// even when new allocations are pushed to the `HashMap`. `copy_repeatedly` relies
186     /// on that.
187     fn get_bytes_internal(
188         &self,
189         cx: &impl HasDataLayout,
190         ptr: Pointer<Tag>,
191         size: Size,
192         check_defined_and_ptr: bool,
193         msg: CheckInAllocMsg,
194     ) -> EvalResult<'tcx, &[u8]>
195     {
196         self.check_bounds(cx, ptr, size, msg)?;
197
198         if check_defined_and_ptr {
199             self.check_defined(ptr, size)?;
200             self.check_relocations(cx, ptr, size)?;
201         } else {
202             // We still don't want relocations on the *edges*
203             self.check_relocation_edges(cx, ptr, size)?;
204         }
205
206         AllocationExtra::memory_read(self, ptr, size)?;
207
208         assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
209         assert_eq!(size.bytes() as usize as u64, size.bytes());
210         let offset = ptr.offset.bytes() as usize;
211         Ok(&self.bytes[offset..offset + size.bytes() as usize])
212     }
213
214     #[inline]
215     pub fn get_bytes(
216         &self,
217         cx: &impl HasDataLayout,
218         ptr: Pointer<Tag>,
219         size: Size,
220     ) -> EvalResult<'tcx, &[u8]>
221     {
222         self.get_bytes_internal(cx, ptr, size, true, CheckInAllocMsg::MemoryAccessTest)
223     }
224
225     /// It is the caller's responsibility to handle undefined and pointer bytes.
226     /// However, this still checks that there are no relocations on the *edges*.
227     #[inline]
228     pub fn get_bytes_with_undef_and_ptr(
229         &self,
230         cx: &impl HasDataLayout,
231         ptr: Pointer<Tag>,
232         size: Size,
233     ) -> EvalResult<'tcx, &[u8]>
234     {
235         self.get_bytes_internal(cx, ptr, size, false, CheckInAllocMsg::MemoryAccessTest)
236     }
237
238     /// Just calling this already marks everything as defined and removes relocations,
239     /// so be sure to actually put data there!
240     pub fn get_bytes_mut(
241         &mut self,
242         cx: &impl HasDataLayout,
243         ptr: Pointer<Tag>,
244         size: Size,
245     ) -> EvalResult<'tcx, &mut [u8]>
246     {
247         assert_ne!(size.bytes(), 0, "0-sized accesses should never even get a `Pointer`");
248         self.check_bounds(cx, ptr, size, CheckInAllocMsg::MemoryAccessTest)?;
249
250         self.mark_definedness(ptr, size, true)?;
251         self.clear_relocations(cx, ptr, size)?;
252
253         AllocationExtra::memory_written(self, ptr, size)?;
254
255         assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
256         assert_eq!(size.bytes() as usize as u64, size.bytes());
257         let offset = ptr.offset.bytes() as usize;
258         Ok(&mut self.bytes[offset..offset + size.bytes() as usize])
259     }
260 }
261
262 /// Reading and writing
263 impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
264     /// Reads bytes until a `0` is encountered. Will error if the end of the allocation is reached
265     /// before a `0` is found.
266     pub fn read_c_str(
267         &self,
268         cx: &impl HasDataLayout,
269         ptr: Pointer<Tag>,
270     ) -> EvalResult<'tcx, &[u8]>
271     {
272         assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
273         let offset = ptr.offset.bytes() as usize;
274         match self.bytes[offset..].iter().position(|&c| c == 0) {
275             Some(size) => {
276                 let size_with_null = Size::from_bytes((size + 1) as u64);
277                 // Go through `get_bytes` for checks and AllocationExtra hooks.
278                 // We read the null, so we include it in the request, but we want it removed
279                 // from the result!
280                 Ok(&self.get_bytes(cx, ptr, size_with_null)?[..size])
281             }
282             None => err!(UnterminatedCString(ptr.erase_tag())),
283         }
284     }
285
286     /// Validates that `ptr.offset` and `ptr.offset + size` do not point to the middle of a
287     /// relocation. If `allow_ptr_and_undef` is `false`, also enforces that the memory in the
288     /// given range contains neither relocations nor undef bytes.
289     pub fn check_bytes(
290         &self,
291         cx: &impl HasDataLayout,
292         ptr: Pointer<Tag>,
293         size: Size,
294         allow_ptr_and_undef: bool,
295     ) -> EvalResult<'tcx>
296     {
297         // Check bounds and relocations on the edges
298         self.get_bytes_with_undef_and_ptr(cx, ptr, size)?;
299         // Check undef and ptr
300         if !allow_ptr_and_undef {
301             self.check_defined(ptr, size)?;
302             self.check_relocations(cx, ptr, size)?;
303         }
304         Ok(())
305     }
306
307     /// Writes `src` to the memory starting at `ptr.offset`.
308     ///
309     /// Will do bounds checks on the allocation.
310     pub fn write_bytes(
311         &mut self,
312         cx: &impl HasDataLayout,
313         ptr: Pointer<Tag>,
314         src: &[u8],
315     ) -> EvalResult<'tcx>
316     {
317         let bytes = self.get_bytes_mut(cx, ptr, Size::from_bytes(src.len() as u64))?;
318         bytes.clone_from_slice(src);
319         Ok(())
320     }
321
322     /// Sets `count` bytes starting at `ptr.offset` with `val`. Basically `memset`.
323     pub fn write_repeat(
324         &mut self,
325         cx: &impl HasDataLayout,
326         ptr: Pointer<Tag>,
327         val: u8,
328         count: Size
329     ) -> EvalResult<'tcx>
330     {
331         let bytes = self.get_bytes_mut(cx, ptr, count)?;
332         for b in bytes {
333             *b = val;
334         }
335         Ok(())
336     }
337
338     /// Read a *non-ZST* scalar
339     ///
340     /// zsts can't be read out of two reasons:
341     /// * byteorder cannot work with zero element buffers
342     /// * in oder to obtain a `Pointer` we need to check for ZSTness anyway due to integer pointers
343     ///   being valid for ZSTs
344     ///
345     /// Note: This function does not do *any* alignment checks, you need to do these before calling
346     pub fn read_scalar(
347         &self,
348         cx: &impl HasDataLayout,
349         ptr: Pointer<Tag>,
350         size: Size
351     ) -> EvalResult<'tcx, ScalarMaybeUndef<Tag>>
352     {
353         // get_bytes_unchecked tests relocation edges
354         let bytes = self.get_bytes_with_undef_and_ptr(cx, ptr, size)?;
355         // Undef check happens *after* we established that the alignment is correct.
356         // We must not return Ok() for unaligned pointers!
357         if self.check_defined(ptr, size).is_err() {
358             // this inflates undefined bytes to the entire scalar, even if only a few
359             // bytes are undefined
360             return Ok(ScalarMaybeUndef::Undef);
361         }
362         // Now we do the actual reading
363         let bits = read_target_uint(cx.data_layout().endian, bytes).unwrap();
364         // See if we got a pointer
365         if size != cx.data_layout().pointer_size {
366             // *Now* better make sure that the inside also is free of relocations.
367             self.check_relocations(cx, ptr, size)?;
368         } else {
369             match self.relocations.get(&ptr.offset) {
370                 Some(&(tag, alloc_id)) => {
371                     let ptr = Pointer::new_with_tag(alloc_id, Size::from_bytes(bits as u64), tag);
372                     return Ok(ScalarMaybeUndef::Scalar(ptr.into()))
373                 }
374                 None => {},
375             }
376         }
377         // We don't. Just return the bits.
378         Ok(ScalarMaybeUndef::Scalar(Scalar::from_uint(bits, size)))
379     }
380
381     /// Note: This function does not do *any* alignment checks, you need to do these before calling
382     pub fn read_ptr_sized(
383         &self,
384         cx: &impl HasDataLayout,
385         ptr: Pointer<Tag>,
386     ) -> EvalResult<'tcx, ScalarMaybeUndef<Tag>>
387     {
388         self.read_scalar(cx, ptr, cx.data_layout().pointer_size)
389     }
390
391     /// Write a *non-ZST* scalar
392     ///
393     /// zsts can't be read out of two reasons:
394     /// * byteorder cannot work with zero element buffers
395     /// * in oder to obtain a `Pointer` we need to check for ZSTness anyway due to integer pointers
396     ///   being valid for ZSTs
397     ///
398     /// Note: This function does not do *any* alignment checks, you need to do these before calling
399     pub fn write_scalar(
400         &mut self,
401         cx: &impl HasDataLayout,
402         ptr: Pointer<Tag>,
403         val: ScalarMaybeUndef<Tag>,
404         type_size: Size,
405     ) -> EvalResult<'tcx>
406     {
407         let val = match val {
408             ScalarMaybeUndef::Scalar(scalar) => scalar,
409             ScalarMaybeUndef::Undef => return self.mark_definedness(ptr, type_size, false),
410         };
411
412         let bytes = match val.to_bits_or_ptr(type_size, cx) {
413             Err(val) => val.offset.bytes() as u128,
414             Ok(data) => data,
415         };
416
417         let endian = cx.data_layout().endian;
418         let dst = self.get_bytes_mut(cx, ptr, type_size)?;
419         write_target_uint(endian, dst, bytes).unwrap();
420
421         // See if we have to also write a relocation
422         match val {
423             Scalar::Ptr(val) => {
424                 self.relocations.insert(
425                     ptr.offset,
426                     (val.tag, val.alloc_id),
427                 );
428             }
429             _ => {}
430         }
431
432         Ok(())
433     }
434
435     /// Note: This function does not do *any* alignment checks, you need to do these before calling
436     pub fn write_ptr_sized(
437         &mut self,
438         cx: &impl HasDataLayout,
439         ptr: Pointer<Tag>,
440         val: ScalarMaybeUndef<Tag>
441     ) -> EvalResult<'tcx>
442     {
443         let ptr_size = cx.data_layout().pointer_size;
444         self.write_scalar(cx, ptr.into(), val, ptr_size)
445     }
446 }
447
448 /// Relocations
449 impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
450     /// Returns all relocations overlapping with the given ptr-offset pair.
451     pub fn relocations(
452         &self,
453         cx: &impl HasDataLayout,
454         ptr: Pointer<Tag>,
455         size: Size,
456     ) -> &[(Size, (Tag, AllocId))] {
457         // We have to go back `pointer_size - 1` bytes, as that one would still overlap with
458         // the beginning of this range.
459         let start = ptr.offset.bytes().saturating_sub(cx.data_layout().pointer_size.bytes() - 1);
460         let end = ptr.offset + size; // this does overflow checking
461         self.relocations.range(Size::from_bytes(start)..end)
462     }
463
464     /// Checks that there are no relocations overlapping with the given range.
465     #[inline(always)]
466     fn check_relocations(
467         &self,
468         cx: &impl HasDataLayout,
469         ptr: Pointer<Tag>,
470         size: Size,
471     ) -> EvalResult<'tcx> {
472         if self.relocations(cx, ptr, size).is_empty() {
473             Ok(())
474         } else {
475             err!(ReadPointerAsBytes)
476         }
477     }
478
479     /// Removes all relocations inside the given range.
480     /// If there are relocations overlapping with the edges, they
481     /// are removed as well *and* the bytes they cover are marked as
482     /// uninitialized. This is a somewhat odd "spooky action at a distance",
483     /// but it allows strictly more code to run than if we would just error
484     /// immediately in that case.
485     fn clear_relocations(
486         &mut self,
487         cx: &impl HasDataLayout,
488         ptr: Pointer<Tag>,
489         size: Size,
490     ) -> EvalResult<'tcx> {
491         // Find the start and end of the given range and its outermost relocations.
492         let (first, last) = {
493             // Find all relocations overlapping the given range.
494             let relocations = self.relocations(cx, ptr, size);
495             if relocations.is_empty() {
496                 return Ok(());
497             }
498
499             (relocations.first().unwrap().0,
500              relocations.last().unwrap().0 + cx.data_layout().pointer_size)
501         };
502         let start = ptr.offset;
503         let end = start + size;
504
505         // Mark parts of the outermost relocations as undefined if they partially fall outside the
506         // given range.
507         if first < start {
508             self.undef_mask.set_range(first, start, false);
509         }
510         if last > end {
511             self.undef_mask.set_range(end, last, false);
512         }
513
514         // Forget all the relocations.
515         self.relocations.remove_range(first..last);
516
517         Ok(())
518     }
519
520     /// Error if there are relocations overlapping with the edges of the
521     /// given memory range.
522     #[inline]
523     fn check_relocation_edges(
524         &self,
525         cx: &impl HasDataLayout,
526         ptr: Pointer<Tag>,
527         size: Size,
528     ) -> EvalResult<'tcx> {
529         self.check_relocations(cx, ptr, Size::ZERO)?;
530         self.check_relocations(cx, ptr.offset(size, cx)?, Size::ZERO)?;
531         Ok(())
532     }
533 }
534
535
536 /// Undefined bytes
537 impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
538     /// Checks that a range of bytes is defined. If not, returns the `ReadUndefBytes`
539     /// error which will report the first byte which is undefined.
540     #[inline]
541     fn check_defined(&self, ptr: Pointer<Tag>, size: Size) -> EvalResult<'tcx> {
542         self.undef_mask.is_range_defined(
543             ptr.offset,
544             ptr.offset + size,
545         ).or_else(|idx| err!(ReadUndefBytes(idx)))
546     }
547
548     pub fn mark_definedness(
549         &mut self,
550         ptr: Pointer<Tag>,
551         size: Size,
552         new_state: bool,
553     ) -> EvalResult<'tcx> {
554         if size.bytes() == 0 {
555             return Ok(());
556         }
557         self.undef_mask.set_range(
558             ptr.offset,
559             ptr.offset + size,
560             new_state,
561         );
562         Ok(())
563     }
564 }
565
566 /// Relocations
567 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, RustcEncodable, RustcDecodable)]
568 pub struct Relocations<Tag=(), Id=AllocId>(SortedMap<Size, (Tag, Id)>);
569
570 impl<Tag, Id> Relocations<Tag, Id> {
571     pub fn new() -> Self {
572         Relocations(SortedMap::new())
573     }
574
575     // The caller must guarantee that the given relocations are already sorted
576     // by address and contain no duplicates.
577     pub fn from_presorted(r: Vec<(Size, (Tag, Id))>) -> Self {
578         Relocations(SortedMap::from_presorted_elements(r))
579     }
580 }
581
582 impl<Tag> Deref for Relocations<Tag> {
583     type Target = SortedMap<Size, (Tag, AllocId)>;
584
585     fn deref(&self) -> &Self::Target {
586         &self.0
587     }
588 }
589
590 impl<Tag> DerefMut for Relocations<Tag> {
591     fn deref_mut(&mut self) -> &mut Self::Target {
592         &mut self.0
593     }
594 }
595
596 ////////////////////////////////////////////////////////////////////////////////
597 // Undefined byte tracking
598 ////////////////////////////////////////////////////////////////////////////////
599
600 type Block = u64;
601
602 /// A bitmask where each bit refers to the byte with the same index. If the bit is `true`, the byte
603 /// is defined. If it is `false` the byte is undefined.
604 #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
605 pub struct UndefMask {
606     blocks: Vec<Block>,
607     len: Size,
608 }
609
610 impl_stable_hash_for!(struct mir::interpret::UndefMask{blocks, len});
611
612 impl UndefMask {
613     pub const BLOCK_SIZE: u64 = 64;
614
615     pub fn new(size: Size, state: bool) -> Self {
616         let mut m = UndefMask {
617             blocks: vec![],
618             len: Size::ZERO,
619         };
620         m.grow(size, state);
621         m
622     }
623
624     /// Checks whether the range `start..end` (end-exclusive) is entirely defined.
625     ///
626     /// Returns `Ok(())` if it's defined. Otherwise returns the index of the byte
627     /// at which the first undefined access begins.
628     #[inline]
629     pub fn is_range_defined(&self, start: Size, end: Size) -> Result<(), Size> {
630         if end > self.len {
631             return Err(self.len);
632         }
633
634         // FIXME(oli-obk): optimize this for allocations larger than a block.
635         let idx = (start.bytes()..end.bytes())
636             .map(|i| Size::from_bytes(i))
637             .find(|&i| !self.get(i));
638
639         match idx {
640             Some(idx) => Err(idx),
641             None => Ok(())
642         }
643     }
644
645     pub fn set_range(&mut self, start: Size, end: Size, new_state: bool) {
646         let len = self.len;
647         if end > len {
648             self.grow(end - len, new_state);
649         }
650         self.set_range_inbounds(start, end, new_state);
651     }
652
653     pub fn set_range_inbounds(&mut self, start: Size, end: Size, new_state: bool) {
654         let (blocka, bita) = bit_index(start);
655         let (blockb, bitb) = bit_index(end);
656         if blocka == blockb {
657             // first set all bits but the first `bita`
658             // then unset the last `64 - bitb` bits
659             let range = if bitb == 0 {
660                 u64::max_value() << bita
661             } else {
662                 (u64::max_value() << bita) & (u64::max_value() >> (64 - bitb))
663             };
664             if new_state {
665                 self.blocks[blocka] |= range;
666             } else {
667                 self.blocks[blocka] &= !range;
668             }
669             return;
670         }
671         // across block boundaries
672         if new_state {
673             // set bita..64 to 1
674             self.blocks[blocka] |= u64::max_value() << bita;
675             // set 0..bitb to 1
676             if bitb != 0 {
677                 self.blocks[blockb] |= u64::max_value() >> (64 - bitb);
678             }
679             // fill in all the other blocks (much faster than one bit at a time)
680             for block in (blocka + 1) .. blockb {
681                 self.blocks[block] = u64::max_value();
682             }
683         } else {
684             // set bita..64 to 0
685             self.blocks[blocka] &= !(u64::max_value() << bita);
686             // set 0..bitb to 0
687             if bitb != 0 {
688                 self.blocks[blockb] &= !(u64::max_value() >> (64 - bitb));
689             }
690             // fill in all the other blocks (much faster than one bit at a time)
691             for block in (blocka + 1) .. blockb {
692                 self.blocks[block] = 0;
693             }
694         }
695     }
696
697     #[inline]
698     pub fn get(&self, i: Size) -> bool {
699         let (block, bit) = bit_index(i);
700         (self.blocks[block] & (1 << bit)) != 0
701     }
702
703     #[inline]
704     pub fn set(&mut self, i: Size, new_state: bool) {
705         let (block, bit) = bit_index(i);
706         self.set_bit(block, bit, new_state);
707     }
708
709     #[inline]
710     fn set_bit(&mut self, block: usize, bit: usize, new_state: bool) {
711         if new_state {
712             self.blocks[block] |= 1 << bit;
713         } else {
714             self.blocks[block] &= !(1 << bit);
715         }
716     }
717
718     pub fn grow(&mut self, amount: Size, new_state: bool) {
719         if amount.bytes() == 0 {
720             return;
721         }
722         let unused_trailing_bits = self.blocks.len() as u64 * Self::BLOCK_SIZE - self.len.bytes();
723         if amount.bytes() > unused_trailing_bits {
724             let additional_blocks = amount.bytes() / Self::BLOCK_SIZE + 1;
725             assert_eq!(additional_blocks as usize as u64, additional_blocks);
726             self.blocks.extend(
727                 // FIXME(oli-obk): optimize this by repeating `new_state as Block`
728                 iter::repeat(0).take(additional_blocks as usize),
729             );
730         }
731         let start = self.len;
732         self.len += amount;
733         self.set_range_inbounds(start, start + amount, new_state);
734     }
735 }
736
737 #[inline]
738 fn bit_index(bits: Size) -> (usize, usize) {
739     let bits = bits.bytes();
740     let a = bits / UndefMask::BLOCK_SIZE;
741     let b = bits % UndefMask::BLOCK_SIZE;
742     assert_eq!(a as usize as u64, a);
743     assert_eq!(b as usize as u64, b);
744     (a as usize, b as usize)
745 }