slab_release_fragment.py
verification/tools/slab_release_fragment.py
1"""Pinned exact small-slot release body; no enclosing allocator theorem."""2import hashlib345FRAGMENT_SHA256 = '5d028e9870ecf6f3417ff39ca3c70c14560ec5b7b7f7bfed45bcde8a3e5d7b6c'6LIST_HELPERS_SHA256 = '29fbd071a1d82b6bfe3d24828039f44a16857dc6855850db7144722b0b72b345'7PREFIX_SHA256 = 'cf33a5081acd48d783186cb646a6ca8271673520d354eccb9d0feead7b385ecf'8TAIL_SHA256 = 'd7e9921ed041245433c20a7a232714b7ee22e4524a4122f67a354e390d05d9bc'9FINAL_TAIL_SHA256 = 'f44ea275a1bdd460f04bd6e30695d4798bc129a568538e560f303fd3f90e8fc7'101112def between(source, start, end):13 if source.count(start) != 1 or source.count(end) != 1:14 raise RuntimeError('Slab release fragment anchors changed or are ambiguous')15 begin = source.index(start)16 finish = source.index(end, begin)17 return source[begin:finish]181920def stage(source, *, receiver='AllocatorHeap'):21 if receiver not in ('AllocatorHeap', 'BoundaryHeap'):22 raise RuntimeError('Unreviewed slab release receiver context')23 release = between(source, b' pub unsafe fn release(',24 b' /// Reallocate without changing the old allocation on failure.')25 fragment = between(release,26 b' unsafe {\n let was_full = (*slab).live as usize == slots(class);',27 b' } else {\n let (count, _) = run_geometry(layout)')28 if not fragment.endswith(b' }\n'):29 raise RuntimeError('Slab release fragment no longer ends at the small-path boundary')30 digest = hashlib.sha256(fragment).hexdigest()31 if digest != FRAGMENT_SHA256:32 raise RuntimeError('Slab release fragment changed without source review')33 wrapper = (b'\n#[cfg(kani)] impl<const B: usize, const P: usize> ' + receiver.encode() + b'<B, P> {\n'34 b' unsafe fn proof_small_release(&mut self, source: &mut impl PageSource, '35 b'pointer: *mut u8, slab: *mut Slab, class: usize) {\n'36 + fragment + b' }\n}\n')37 if wrapper.count(fragment) != 1:38 raise RuntimeError('Slab release wrapper changed the selected source bytes')39 return wrapper, {'sha256': digest, 'bytes': len(fragment),40 'receiver': receiver,41 'scope': 'exact small-release unsafe body; caller provides slab/class/pointer'}424344def boundary_helpers(source):45 helpers = between(source, b' unsafe fn link_slab(', b' unsafe fn acquire_span(')46 if hashlib.sha256(helpers).hexdigest() != LIST_HELPERS_SHA256:47 raise RuntimeError('Slab list helpers changed without source review')48 return (b'impl<const B: usize, const P: usize> BoundaryHeap<B, P> {\n'49 + helpers + b'}\n')505152def nonfinal(source):53 # Pin the complete body/guard as well as both selected contiguous blocks.54 wrapper, receipt = stage(source)55 prefix = between(wrapper,56 b' let was_full = (*slab).live as usize == slots(class);',57 b' if (*slab).live == 0 {')58 tail = between(wrapper, b' let index = (pointer.addr()',59 b' }\n }\n')60 if not prefix.endswith(b' (*slab).live -= 1;\n') or not tail.endswith(b' }\n'):61 raise RuntimeError('Nonfinal slab release path boundaries changed')62 if hashlib.sha256(prefix).hexdigest() != PREFIX_SHA256 or hashlib.sha256(tail).hexdigest() != TAIL_SHA256:63 raise RuntimeError('Nonfinal slab release path changed without source review')64 path = (b'\n#[cfg(kani)] impl<const B: usize, const P: usize> AllocatorHeap<B, P> {\n'65 b' unsafe fn proof_release_prefix(&mut self, slab: *mut Slab, class: usize) -> bool {\n'66 b' unsafe {\n' + prefix + b' was_full\n }\n }\n'67 b' unsafe fn proof_release_nonfinal_tail(&mut self, pointer: *mut u8, slab: *mut Slab, '68 b'class: usize, was_full: bool) {\n unsafe {\n' + tail + b' }\n }\n}\n')69 if path.count(prefix) != 1 or path.count(tail) != 1:70 raise RuntimeError('Nonfinal slab path wrapper changed selected source bytes')71 return path, {**receipt, 'prefix_sha256': PREFIX_SHA256, 'tail_sha256': TAIL_SHA256,72 'scope': 'actual nonfinal path: decrement prefix then guarded else tail; caller proves live>0 between blocks'}737475def final_boundary(source):76 wrapper, receipt = stage(source, receiver='BoundaryHeap')77 prefix = between(wrapper,78 b' let was_full = (*slab).live as usize == slots(class);',79 b' if (*slab).live == 0 {')80 tail = between(wrapper, b' if !was_full {', b' } else {')81 if hashlib.sha256(prefix).hexdigest() != PREFIX_SHA256 or hashlib.sha256(tail).hexdigest() != FINAL_TAIL_SHA256:82 raise RuntimeError('Final slab release path changed without source review')83 if not tail.endswith(b' self.release_span(source, slab.cast(), span_order(class));\n'):84 raise RuntimeError('Final slab dispatch boundary changed')85 path = (b'\n#[cfg(kani)] impl<const B: usize, const P: usize> BoundaryHeap<B, P> {\n'86 b' unsafe fn proof_release_prefix(&mut self, slab: *mut Slab, class: usize) -> bool {\n'87 b' unsafe {\n' + prefix + b' was_full\n }\n }\n'88 b' unsafe fn proof_release_final_tail(&mut self, source: &mut impl PageSource, '89 b'slab: *mut Slab, class: usize, was_full: bool) {\n unsafe {\n'90 + tail + b' }\n }\n}\n')91 if path.count(prefix) != 1 or path.count(tail) != 1:92 raise RuntimeError('Final slab path wrapper changed selected source bytes')93 return path, {**receipt, 'prefix_sha256': PREFIX_SHA256, 'tail_sha256': FINAL_TAIL_SHA256,94 'scope': 'conditional final dispatch-only path; caller proves live==0 between prefix and then tail'}