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