-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathlayout.rs
More file actions
3529 lines (3086 loc) · 138 KB
/
layout.rs
File metadata and controls
3529 lines (3086 loc) · 138 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2013-2015 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::mem;
use core::ptr;
use core::cell::Cell;
use crate::data;
use crate::text;
use crate::private::capability::{ClientHook};
use crate::private::arena::{BuilderArena, ReaderArena, NullArena, SegmentId};
use crate::private::mask::Mask;
use crate::private::primitive::{Primitive, WireValue};
use crate::private::units::*;
use crate::private::zero;
use crate::{MessageSize, Result};
pub use self::ElementSize::{Void, Bit, Byte, TwoBytes, FourBytes, EightBytes, Pointer, InlineComposite};
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ElementSize {
Void = 0,
Bit = 1,
Byte = 2,
TwoBytes = 3,
FourBytes = 4,
EightBytes = 5,
Pointer = 6,
InlineComposite = 7
}
impl ElementSize {
fn from(val: u8) -> ElementSize {
match val {
0 => ElementSize::Void,
1 => ElementSize::Bit,
2 => ElementSize::Byte,
3 => ElementSize::TwoBytes,
4 => ElementSize::FourBytes,
5 => ElementSize::EightBytes,
6 => ElementSize::Pointer,
7 => ElementSize::InlineComposite,
_ => panic!("illegal element size: {}", val),
}
}
}
pub fn data_bits_per_element(size: ElementSize) -> BitCount32 {
match size {
Void => 0,
Bit => 1,
Byte => 8,
TwoBytes => 16,
FourBytes => 32,
EightBytes => 64,
Pointer => 0,
InlineComposite => 0
}
}
pub fn pointers_per_element(size: ElementSize) -> WirePointerCount32 {
match size {
Pointer => 1,
_ => 0
}
}
#[derive(Clone, Copy, Debug)]
pub struct StructSize {
pub data: WordCount16,
pub pointers: WirePointerCount16,
}
impl StructSize {
pub fn total(&self) -> WordCount32 {
self.data as WordCount32
+ self.pointers as WordCount32
* WORDS_PER_POINTER as WordCount32
}
}
#[repr(u8)]
#[derive(Clone, Copy, PartialEq)]
pub enum WirePointerKind {
Struct = 0,
List = 1,
Far = 2,
Other = 3
}
pub enum PointerType {
Null,
Struct,
List,
Capability,
}
impl WirePointerKind {
fn from(val: u8) -> WirePointerKind {
match val {
0 => WirePointerKind::Struct,
1 => WirePointerKind::List,
2 => WirePointerKind::Far,
3 => WirePointerKind::Other,
_ => panic!("illegal element size: {}", val),
}
}
}
#[repr(C)]
pub struct WirePointer {
offset_and_kind: WireValue<u32>,
upper32bits: WireValue<u32>,
}
#[test]
#[cfg(feature = "unaligned")]
fn wire_pointer_align() {
// We cast *u8 to *WirePointer, so we need to make sure its alignment allows that.
assert_eq!(core::mem::align_of::<WirePointer>(), 1);
}
impl WirePointer {
#[inline]
pub fn kind(&self) -> WirePointerKind {
WirePointerKind::from(self.offset_and_kind.get() as u8 & 3)
}
#[inline]
pub fn is_positional(&self) -> bool {
(self.offset_and_kind.get() & 2) == 0 // match Struct and List but not Far and Other.
}
#[inline]
pub fn is_capability(&self) -> bool {
self.offset_and_kind.get() == WirePointerKind::Other as u32
}
#[inline]
pub fn target(&self) -> *const u8 {
let this_addr: *const u8 = self as *const _ as *const _;
unsafe { this_addr.offset(8 * (1 + ((self.offset_and_kind.get() as i32) >> 2)) as isize) }
}
#[inline]
pub fn target_from_segment(&self, arena: &dyn ReaderArena, segment_id: u32) -> Result<*const u8> {
let this_addr: *const u8 = self as *const _ as *const _;
let offset = 1 + ((self.offset_and_kind.get() as i32) >> 2);
arena.check_offset(segment_id, this_addr, offset)
}
#[inline]
pub fn mut_target(&mut self) -> *mut u8 {
let this_addr: *mut u8 = self as *mut _ as *mut _;
this_addr.wrapping_offset(BYTES_PER_WORD as isize * (1 + ((self.offset_and_kind.get() as i32) >> 2)) as isize)
}
#[inline]
pub fn set_kind_and_target(&mut self, kind: WirePointerKind, target: *mut u8) {
let this_addr: isize = self as *const _ as isize;
let target_addr: isize = target as *const _ as isize;
self.offset_and_kind.set(
((((target_addr - this_addr) / BYTES_PER_WORD as isize) as i32 - 1) << 2) as u32
| (kind as u32))
}
#[inline]
pub fn set_kind_with_zero_offset(&mut self, kind: WirePointerKind) {
self.offset_and_kind.set(kind as u32)
}
#[inline]
pub fn set_kind_and_target_for_empty_struct(&mut self) {
//# This pointer points at an empty struct. Assuming the
//# WirePointer itself is in-bounds, we can set the target to
//# point either at the WirePointer itself or immediately after
//# it. The latter would cause the WirePointer to be "null"
//# (since for an empty struct the upper 32 bits are going to
//# be zero). So we set an offset of -1, as if the struct were
//# allocated immediately before this pointer, to distinguish
//# it from null.
self.offset_and_kind.set(0xfffffffc);
}
#[inline]
pub fn inline_composite_list_element_count(&self) -> ElementCount32 {
self.offset_and_kind.get() >> 2
}
#[inline]
pub fn set_kind_and_inline_composite_list_element_count(&mut self,
kind: WirePointerKind,
element_count: ElementCount32) {
self.offset_and_kind.set(( element_count << 2) | (kind as u32))
}
#[inline]
pub fn far_position_in_segment(&self) -> WordCount32 {
self.offset_and_kind.get() >> 3
}
#[inline]
pub fn is_double_far(&self) -> bool {
((self.offset_and_kind.get() >> 2) & 1) != 0
}
#[inline]
pub fn set_far(&mut self, is_double_far: bool, pos: WordCount32) {
self.offset_and_kind
.set(( pos << 3) | ((is_double_far as u32) << 2) | WirePointerKind::Far as u32);
}
#[inline]
pub fn set_cap(&mut self, index: u32) {
self.offset_and_kind.set(WirePointerKind::Other as u32);
self.upper32bits.set(index);
}
#[inline]
pub fn struct_data_size(&self) -> WordCount16 {
(self.upper32bits.get() & 0xffffffff) as WordCount16
}
#[inline]
pub fn struct_ptr_count(&self) -> WordCount16 {
(self.upper32bits.get() >> 16) as WordCount16
}
#[inline]
pub fn struct_word_size(&self) -> WordCount32 {
self.struct_data_size() as WordCount32 +
self.struct_ptr_count() as WordCount32 * WORDS_PER_POINTER as u32
}
#[inline]
pub fn set_struct_size(&mut self, size: StructSize) {
self.upper32bits.set(size.data as u32 | ((size.pointers as u32) << 16))
}
#[inline]
pub fn set_struct_size_from_pieces(&mut self, ds: WordCount16, rc: WirePointerCount16) {
self.set_struct_size(StructSize { data: ds, pointers: rc })
}
#[inline]
pub fn list_element_size(&self) -> ElementSize {
ElementSize::from(self.upper32bits.get() as u8 & 7)
}
#[inline]
pub fn list_element_count(&self) -> ElementCount32 {
self.upper32bits.get() >> 3
}
#[inline]
pub fn list_inline_composite_word_count(&self) -> WordCount32 {
self.list_element_count()
}
#[inline]
pub fn set_list_size_and_count(&mut self, es: ElementSize, ec: ElementCount32) {
assert!(ec < (1 << 29), "Lists are limited to 2**29 elements");
self.upper32bits.set((ec << 3 ) | (es as u32));
}
#[inline]
pub fn set_list_inline_composite(&mut self, wc: WordCount32) {
assert!(wc < (1 << 29), "Inline composite lists are limited to 2**29 words");
self.upper32bits.set((wc << 3) | (InlineComposite as u32));
}
#[inline]
pub fn far_segment_id(&self) -> SegmentId {
self.upper32bits.get() as SegmentId
}
#[inline]
pub fn set_far_segment_id(&mut self, si: SegmentId) {
self.upper32bits.set(si)
}
#[inline]
pub fn cap_index(&self) -> u32 {
self.upper32bits.get()
}
#[inline]
pub fn set_cap_index(&mut self, index: u32) {
self.upper32bits.set(index)
}
#[inline]
pub fn is_null(&self) -> bool {
self.offset_and_kind.get() == 0 && self.upper32bits.get() == 0
}
}
mod wire_helpers {
use alloc::boxed::Box;
use alloc::string::ToString;
use core::{ptr, slice};
use crate::private::capability::ClientHook;
use crate::private::arena::*;
use crate::private::layout::{
CapTableBuilder, CapTableReader, ElementSize, ListBuilder, ListReader,
StructBuilder, StructReader, StructSize, WirePointer, WirePointerKind};
use crate::private::layout::{data_bits_per_element, pointers_per_element};
use crate::private::layout::ElementSize::*;
use crate::private::units::*;
use crate::data;
use crate::text;
use crate::{Error, MessageSize, Result};
pub struct SegmentAnd<T> {
#[allow(dead_code)]
segment_id: u32,
pub value: T,
}
#[inline]
pub fn round_bytes_up_to_words(bytes: ByteCount32) -> WordCount32 {
//# This code assumes 64-bit words.
(bytes + 7) / BYTES_PER_WORD as u32
}
//# The maximum object size is 4GB - 1 byte. If measured in bits,
//# this would overflow a 32-bit counter, so we need to accept
//# BitCount64. However, 32 bits is enough for the returned
//# ByteCounts and WordCounts.
#[inline]
pub fn round_bits_up_to_words(bits: BitCount64) -> WordCount32 {
//# This code assumes 64-bit words.
((bits + 63) / (BITS_PER_WORD as u64)) as WordCount32
}
#[allow(dead_code)]
#[inline]
pub fn round_bits_up_to_bytes(bits: BitCount64) -> ByteCount32 {
((bits + 7) / (BITS_PER_BYTE as u64)) as ByteCount32
}
#[inline]
pub fn bounds_check(arena: &dyn ReaderArena,
segment_id: u32,
start: *const u8, size_in_words: usize,
_kind: WirePointerKind) -> Result<()> {
arena.contains_interval(segment_id, start, size_in_words)
}
#[inline]
pub fn amplified_read(arena: &dyn ReaderArena,
virtual_amount: u64) -> Result<()> {
arena.amplified_read(virtual_amount)
}
#[inline]
pub unsafe fn allocate(
arena: &dyn BuilderArena,
reff: *mut WirePointer,
segment_id: u32,
amount: WordCount32, kind: WirePointerKind) -> (*mut u8, *mut WirePointer, u32)
{
let is_null = (*reff).is_null();
if !is_null {
zero_object(arena, segment_id, reff)
}
if amount == 0 && kind == WirePointerKind::Struct {
(*reff).set_kind_and_target_for_empty_struct();
return (reff as *mut _, reff, segment_id);
}
match arena.allocate(segment_id, amount) {
None => {
//# Need to allocate in a different segment. We'll need to
//# allocate an extra pointer worth of space to act as
//# the landing pad for a far pointer.
let amount_plus_ref = amount + POINTER_SIZE_IN_WORDS as u32;
let (segment_id, word_idx) = arena.allocate_anywhere(amount_plus_ref);
let (seg_start, _seg_len) = arena.get_segment_mut(segment_id);
let ptr = seg_start.offset(word_idx as isize * BYTES_PER_WORD as isize);
//# Set up the original pointer to be a far pointer to
//# the new segment.
(*reff).set_far(false, word_idx);
(*reff).set_far_segment_id(segment_id);
//# Initialize the landing pad to indicate that the
//# data immediately follows the pad.
let reff = ptr as *mut WirePointer;
let ptr1 = ptr.offset(BYTES_PER_WORD as isize);
(*reff).set_kind_and_target(kind, ptr1);
(ptr1, reff, segment_id)
}
Some(idx) => {
let (seg_start, _seg_len) = arena.get_segment_mut(segment_id);
let ptr = (seg_start).offset(idx as isize * BYTES_PER_WORD as isize);
(*reff).set_kind_and_target(kind, ptr);
(ptr, reff, segment_id)
}
}
}
#[inline]
pub unsafe fn follow_builder_fars(
arena: &dyn BuilderArena,
reff: *mut WirePointer,
ref_target: *mut u8,
segment_id: u32) -> Result<(*mut u8, *mut WirePointer, u32)>
{
// If `ref` is a far pointer, follow it. On return, `ref` will have been updated to point at
// a WirePointer that contains the type information about the target object, and a pointer
// to the object contents is returned. The caller must NOT use `ref->target()` as this may
// or may not actually return a valid pointer. `segment` is also updated to point at the
// segment which actually contains the object.
//
// If `ref` is not a far pointer, this simply returns `ref_target`. Usually, `ref_target`
// should be the same as `ref->target()`, but may not be in cases where `ref` is only a tag.
if (*reff).kind() == WirePointerKind::Far {
let segment_id = (*reff).far_segment_id();
let (seg_start, _seg_len) = arena.get_segment_mut(segment_id);
let pad: *mut WirePointer =
(seg_start as *mut WirePointer).offset((*reff).far_position_in_segment() as isize);
if !(*reff).is_double_far() {
Ok(((*pad).mut_target(), pad, segment_id))
} else {
//# Landing pad is another far pointer. It is followed by a
//# tag describing the pointed-to object.
let reff = pad.offset(1);
let segment_id = (*pad).far_segment_id();
let (segment_start, _segment_len) = arena.get_segment_mut(segment_id);
let ptr = segment_start.offset((*pad).far_position_in_segment() as isize * BYTES_PER_WORD as isize);
Ok((ptr, reff, segment_id))
}
} else {
Ok((ref_target as *mut u8, reff, segment_id))
}
}
/// Follows a WirePointer to get a triple containing:
/// - the pointed-to object
/// - the resolved WirePointer, whose kind is something other than WirePointerKind::Far
/// - the segment on which the pointed-to object lives
#[inline]
pub unsafe fn follow_fars(
arena: &dyn ReaderArena,
reff: *const WirePointer,
segment_id: u32)
-> Result<(*const u8, *const WirePointer, u32)>
{
if (*reff).kind() == WirePointerKind::Far {
let far_segment_id = (*reff).far_segment_id();
let (seg_start, _seg_len) = arena.get_segment(far_segment_id)?;
let ptr = seg_start.offset((*reff).far_position_in_segment() as isize * BYTES_PER_WORD as isize);
let pad_words: usize = if (*reff).is_double_far() { 2 } else { 1 };
bounds_check(arena, far_segment_id, ptr, pad_words, WirePointerKind::Far)?;
let pad: *const WirePointer = ptr as *const _;
if !(*reff).is_double_far() {
Ok(((*pad).target_from_segment(arena, far_segment_id)?, pad, far_segment_id))
} else {
// Landing pad is another far pointer. It is followed by a tag describing the
// pointed-to object.
let tag = pad.offset(1);
let double_far_segment_id = (*pad).far_segment_id();
let (segment_start, _segment_len) = arena.get_segment(double_far_segment_id)?;
let ptr = segment_start.offset((*pad).far_position_in_segment() as isize * BYTES_PER_WORD as isize);
Ok((ptr, tag, double_far_segment_id))
}
} else {
Ok(((*reff).target_from_segment(arena, segment_id)?, reff, segment_id))
}
}
pub unsafe fn zero_object(
arena: &dyn BuilderArena,
segment_id: u32,
reff: *mut WirePointer)
{
//# Zero out the pointed-to object. Use when the pointer is
//# about to be overwritten making the target object no longer
//# reachable.
match (*reff).kind() {
WirePointerKind::Struct | WirePointerKind::List | WirePointerKind::Other => {
zero_object_helper(arena, segment_id, reff, (*reff).mut_target())
}
WirePointerKind::Far => {
let segment_id = (*reff).far_segment_id();
let (seg_start, _seg_len) = arena.get_segment_mut(segment_id);
let pad: *mut WirePointer =
(seg_start as *mut WirePointer).offset((*reff).far_position_in_segment() as isize);
if (*reff).is_double_far() {
let segment_id = (*pad).far_segment_id();
let (seg_start, _seg_len) = arena.get_segment_mut(segment_id);
let ptr = seg_start.offset((*pad).far_position_in_segment() as isize * BYTES_PER_WORD as isize);
zero_object_helper(arena,
segment_id,
pad.offset(1),
ptr);
ptr::write_bytes(pad, 0u8, 2);
} else {
zero_object(arena, segment_id, pad);
ptr::write_bytes(pad, 0u8, 1);
}
}
}
}
pub unsafe fn zero_object_helper(
arena: &dyn BuilderArena,
segment_id: u32,
tag: *mut WirePointer,
ptr: *mut u8)
{
match (*tag).kind() {
WirePointerKind::Other => { panic!("Don't know how to handle OTHER") }
WirePointerKind::Struct => {
let pointer_section: *mut WirePointer =
ptr.offset((*tag).struct_data_size() as isize * BYTES_PER_WORD as isize) as *mut _;
let count = (*tag).struct_ptr_count() as isize;
for i in 0..count {
zero_object(arena, segment_id, pointer_section.offset(i));
}
ptr::write_bytes(ptr, 0u8, (*tag).struct_word_size() as usize * BYTES_PER_WORD);
}
WirePointerKind::List => {
match (*tag).list_element_size() {
Void => { }
Bit | Byte | TwoBytes | FourBytes | EightBytes => {
ptr::write_bytes(
ptr, 0u8,
BYTES_PER_WORD as usize *
round_bits_up_to_words((
(*tag).list_element_count() *
data_bits_per_element(
(*tag).list_element_size())) as u64) as usize)
}
Pointer => {
let count = (*tag).list_element_count() as usize;
for i in 0..count as isize {
zero_object(arena, segment_id, ptr.offset(i * BYTES_PER_WORD as isize) as *mut _);
}
ptr::write_bytes(ptr, 0u8, count * BYTES_PER_WORD);
}
InlineComposite => {
let element_tag: *mut WirePointer = ptr as *mut _;
assert!((*element_tag).kind() == WirePointerKind::Struct,
"Don't know how to handle non-STRUCT inline composite");
let data_size = (*element_tag).struct_data_size();
let pointer_count = (*element_tag).struct_ptr_count();
let mut pos = ptr.offset(BYTES_PER_WORD as isize);
let count = (*element_tag).inline_composite_list_element_count();
if pointer_count > 0 {
for _ in 0..count {
pos = pos.offset(data_size as isize * BYTES_PER_WORD as isize);
for _ in 0..pointer_count {
zero_object(arena, segment_id, pos as *mut WirePointer);
pos = pos.offset(BYTES_PER_WORD as isize);
}
}
}
ptr::write_bytes(ptr, 0u8,
BYTES_PER_WORD as usize *
((*element_tag).struct_word_size() * count + 1) as usize);
}
}
}
WirePointerKind::Far => { panic!("Unexpected FAR pointer") }
}
}
#[inline]
pub unsafe fn zero_pointer_and_fars(
arena: &dyn BuilderArena,
_segment_id: u32,
reff: *mut WirePointer) -> Result<()>
{
// Zero out the pointer itself and, if it is a far pointer, zero the landing pad as well,
// but do not zero the object body. Used when upgrading.
if (*reff).kind() == WirePointerKind::Far {
let far_segment_id = (*reff).far_segment_id();
let (seg_start, _seg_len) = arena.get_segment_mut(far_segment_id);
let pad = seg_start.offset((*reff).far_position_in_segment() as isize * BYTES_PER_WORD as isize);
let num_elements = if (*reff).is_double_far() { 2 } else { 1 };
ptr::write_bytes(pad, 0, num_elements * BYTES_PER_WORD);
}
ptr::write_bytes(reff, 0, 1);
Ok(())
}
pub unsafe fn total_size(
arena: &dyn ReaderArena,
segment_id: u32,
reff: *const WirePointer,
mut nesting_limit: i32) -> Result<MessageSize>
{
let mut result = MessageSize { word_count: 0, cap_count: 0};
if (*reff).is_null() { return Ok(result) };
if nesting_limit <= 0 {
return Err(Error::failed("Message is too deeply nested.".to_string()));
}
nesting_limit -= 1;
let (ptr, reff, segment_id) = follow_fars(arena, reff, segment_id)?;
match (*reff).kind() {
WirePointerKind::Struct => {
bounds_check(arena, segment_id,
ptr, (*reff).struct_word_size() as usize,
WirePointerKind::Struct)?;
result.word_count += (*reff).struct_word_size() as u64;
let pointer_section: *const WirePointer =
ptr.offset((*reff).struct_data_size() as isize * BYTES_PER_WORD as isize) as *const _;
let count: isize = (*reff).struct_ptr_count() as isize;
for i in 0..count {
result.plus_eq(total_size(arena, segment_id, pointer_section.offset(i), nesting_limit)?);
}
}
WirePointerKind::List => {
match (*reff).list_element_size() {
Void => {}
Bit | Byte | TwoBytes | FourBytes | EightBytes => {
let total_words = round_bits_up_to_words(
(*reff).list_element_count() as u64 *
data_bits_per_element((*reff).list_element_size()) as u64);
bounds_check(
arena, segment_id, ptr, total_words as usize, WirePointerKind::List)?;
result.word_count += total_words as u64;
}
Pointer => {
let count = (*reff).list_element_count();
bounds_check(
arena, segment_id, ptr, count as usize * WORDS_PER_POINTER,
WirePointerKind::List)?;
result.word_count += count as u64 * WORDS_PER_POINTER as u64;
for i in 0..count as isize {
result.plus_eq(
total_size(arena, segment_id,
(ptr as *const WirePointer).offset(i),
nesting_limit)?);
}
}
InlineComposite => {
let word_count = (*reff).list_inline_composite_word_count();
bounds_check(arena, segment_id, ptr,
word_count as usize + POINTER_SIZE_IN_WORDS,
WirePointerKind::List)?;
let element_tag: *const WirePointer = ptr as *const _;
let count = (*element_tag).inline_composite_list_element_count();
if (*element_tag).kind() != WirePointerKind::Struct {
return Err(Error::failed(
"Don't know how to handle non-STRUCT inline composite.".to_string()));
}
let actual_size = (*element_tag).struct_word_size() as u64 * count as u64;
if actual_size > word_count as u64 {
return Err(Error::failed(
"InlineComposite list's elements overrun its word count.".to_string()));
}
// Count the actual size rather than the claimed word count because
// that's what we end up with if we make a copy.
result.word_count += actual_size as u64 + POINTER_SIZE_IN_WORDS as u64;
let data_size = (*element_tag).struct_data_size();
let pointer_count = (*element_tag).struct_ptr_count();
if pointer_count > 0 {
let mut pos = ptr.offset(BYTES_PER_WORD as isize);
for _ in 0..count {
pos = pos.offset(data_size as isize * BYTES_PER_WORD as isize);
for _ in 0..pointer_count {
result.plus_eq(
total_size(arena, segment_id,
pos as *const WirePointer, nesting_limit)?);
pos = pos.offset(BYTES_PER_WORD as isize);
}
}
}
}
}
}
WirePointerKind::Far => {
return Err(Error::failed("Malformed double-far pointer.".to_string()));
}
WirePointerKind::Other => {
if (*reff).is_capability() {
result.cap_count += 1;
} else {
return Err(Error::failed("Unknown pointer type.".to_string()));
}
}
}
Ok(result)
}
// Helper for copy_message().
unsafe fn copy_struct(
arena: &dyn BuilderArena,
segment_id: u32,
cap_table: CapTableBuilder,
dst: *mut u8,
src: *const u8,
data_size: isize,
pointer_count: isize) {
ptr::copy_nonoverlapping(src, dst, data_size as usize * BYTES_PER_WORD);
let src_refs: *const WirePointer = (src as *const WirePointer).offset(data_size);
let dst_refs: *mut WirePointer = (dst as *mut WirePointer).offset(data_size);
for ii in 0..pointer_count {
copy_message(arena, segment_id, cap_table, dst_refs.offset(ii), src_refs.offset(ii));
}
}
// Copies from a trusted message.
// Returns (new_dst_ptr, new_dst, new_segment_id).
pub unsafe fn copy_message(
arena: &dyn BuilderArena,
segment_id: u32,
cap_table: CapTableBuilder,
dst: *mut WirePointer,
src: *const WirePointer) -> (*mut u8, *mut WirePointer, u32)
{
match (*src).kind() {
WirePointerKind::Struct => {
if (*src).is_null() {
ptr::write_bytes(dst, 0, 1);
(ptr::null_mut(), dst, segment_id)
} else {
let src_ptr = (*src).target();
let (dst_ptr, dst, segment_id) = allocate(
arena, dst, segment_id, (*src).struct_word_size(), WirePointerKind::Struct);
copy_struct(arena,
segment_id,
cap_table,
dst_ptr,
src_ptr,
(*src).struct_data_size() as isize,
(*src).struct_ptr_count() as isize);
(*dst).set_struct_size_from_pieces((*src).struct_data_size(), (*src).struct_ptr_count());
(dst_ptr, dst, segment_id)
}
}
WirePointerKind::List => {
match (*src).list_element_size() {
ElementSize::Void |
ElementSize::Bit |
ElementSize::Byte |
ElementSize::TwoBytes |
ElementSize::FourBytes |
ElementSize::EightBytes => {
let word_count = round_bits_up_to_words(
(*src).list_element_count() as u64 *
data_bits_per_element((*src).list_element_size()) as u64);
let src_ptr = (*src).target();
let (dst_ptr, dst, segment_id) = allocate(
arena, dst, segment_id, word_count, WirePointerKind::List);
ptr::copy_nonoverlapping(src_ptr, dst_ptr, word_count as usize * BYTES_PER_WORD);
(*dst).set_list_size_and_count((*src).list_element_size(),
(*src).list_element_count());
(dst_ptr, dst, segment_id)
}
ElementSize::Pointer => {
let src_refs: *const WirePointer = (*src).target() as _;
let (dst_refs, dst, segment_id) = allocate(
arena, dst, segment_id,
(*src).list_element_count(),
WirePointerKind::List);
for ii in 0 .. ((*src).list_element_count() as isize) {
copy_message(arena,
segment_id,
cap_table,
dst_refs.offset(ii * BYTES_PER_WORD as isize) as *mut WirePointer,
src_refs.offset(ii));
}
(*dst).set_list_size_and_count(ElementSize::Pointer, (*src).list_element_count());
(dst_refs, dst, segment_id)
}
ElementSize::InlineComposite => {
let src_ptr = (*src).target();
let (dst_ptr, dst, segment_id) = allocate(
arena,
dst,
segment_id,
(*src).list_inline_composite_word_count() + 1,
WirePointerKind::List);
(*dst).set_list_inline_composite((*src).list_inline_composite_word_count());
let src_tag: *const WirePointer = src_ptr as _;
ptr::copy_nonoverlapping(src_tag, dst_ptr as *mut WirePointer, 1);
let mut src_element = src_ptr.offset(BYTES_PER_WORD as isize);
let mut dst_element = dst_ptr.offset(BYTES_PER_WORD as isize);
if (*src_tag).kind() != WirePointerKind::Struct {
panic!("unsupported INLINE_COMPOSITE list");
}
for _ in 0 .. (*src_tag).inline_composite_list_element_count() {
copy_struct(arena,
segment_id,
cap_table,
dst_element,
src_element,
(*src_tag).struct_data_size() as isize,
(*src_tag).struct_ptr_count() as isize);
src_element = src_element.offset(BYTES_PER_WORD as isize * (*src_tag).struct_word_size() as isize);
dst_element = dst_element.offset(BYTES_PER_WORD as isize * (*src_tag).struct_word_size() as isize);
}
(dst_ptr, dst, segment_id)
}
}
}
WirePointerKind::Other => {
panic!("Unchecked message contained an OTHER pointer.")
}
WirePointerKind::Far => {
panic!("Unchecked message contained a far pointer.")
}
}
}
pub unsafe fn transfer_pointer(
arena: &dyn BuilderArena,
dst_segment_id: u32, dst: *mut WirePointer,
src_segment_id: u32, src: *mut WirePointer)
{
//# Make *dst point to the same object as *src. Both must
//# reside in the same message, but can be in different
//# segments. Not always-inline because this is rarely used.
//
//# Caller MUST zero out the source pointer after calling this,
//# to make sure no later code mistakenly thinks the source
//# location still owns the object. transferPointer() doesn't
//# do this zeroing itself because many callers transfer
//# several pointers in a loop then zero out the whole section.
assert!((*dst).is_null());
// We expect the caller to ensure the target is already null so won't leak.
if (*src).is_null() {
ptr::write_bytes(dst, 0, 1);
} else if (*src).is_positional() {
transfer_pointer_split(arena, dst_segment_id, dst, src_segment_id, src, (*src).mut_target());
} else {
ptr::copy_nonoverlapping(src, dst, 1);
}
}
pub unsafe fn transfer_pointer_split(
arena: &dyn BuilderArena,
dst_segment_id: u32, dst: *mut WirePointer,
src_segment_id: u32, src_tag: *mut WirePointer,
src_ptr: *mut u8)
{
// Like the other transfer_pointer, but splits src into a tag and a
// target. Particularly useful for OrphanBuilder.
if dst_segment_id == src_segment_id {
// Same segment, so create a direct pointer.
if (*src_tag).kind() == WirePointerKind::Struct && (*src_tag).struct_word_size() == 0 {
(*dst).set_kind_and_target_for_empty_struct();
} else {
(*dst).set_kind_and_target((*src_tag).kind(), src_ptr);
}
// We can just copy the upper 32 bits. (Use memcpy() to comply with aliasing rules.)
ptr::copy_nonoverlapping(&(*src_tag).upper32bits, &mut (*dst).upper32bits, 1);
} else {
// Need to create a far pointer. Try to allocate it in the same segment as the source,
// so that it doesn't need to be a double-far.
match arena.allocate(src_segment_id, 1) {
None => {
//# Darn, need a double-far.
let (far_segment_id, word_idx) = arena.allocate_anywhere(2);
let (seg_start, _seg_len) = arena.get_segment_mut(far_segment_id);
let landing_pad: *mut WirePointer = (seg_start as *mut WirePointer).offset(word_idx as isize);
let (src_seg_start, _seg_len) = arena.get_segment_mut(src_segment_id);
(*landing_pad).set_far(
false,
((src_ptr as usize - src_seg_start as usize) / BYTES_PER_WORD) as u32);
(*landing_pad).set_far_segment_id(src_segment_id);
let landing_pad1 = landing_pad.offset(1);
(*landing_pad1).set_kind_with_zero_offset((*src_tag).kind());
ptr::copy_nonoverlapping(
&(*src_tag).upper32bits,
&mut (*landing_pad1).upper32bits,
1);
(*dst).set_far(true, word_idx);
(*dst).set_far_segment_id(far_segment_id);
}
Some(landing_pad_word) => {
//# Simple landing pad is just a pointer.
let (seg_start, seg_len) = arena.get_segment_mut(src_segment_id);
assert!(landing_pad_word < seg_len);
let landing_pad: *mut WirePointer = (seg_start as *mut WirePointer).offset(landing_pad_word as isize);
(*landing_pad).set_kind_and_target((*src_tag).kind(), src_ptr);
ptr::copy_nonoverlapping(&(*src_tag).upper32bits,
&mut (*landing_pad).upper32bits, 1);
(*dst).set_far(false, landing_pad_word);
(*dst).set_far_segment_id(src_segment_id);
}
}
}
}
#[inline]
pub unsafe fn init_struct_pointer<'a>(
arena: &'a dyn BuilderArena,
reff: *mut WirePointer,
segment_id: u32,
cap_table: CapTableBuilder,
size: StructSize) -> StructBuilder<'a>
{
let (ptr, reff, segment_id) = allocate(
arena,
reff,
segment_id,
size.total(),
WirePointerKind::Struct);
(*reff).set_struct_size(size);
StructBuilder {
arena: arena,
segment_id: segment_id,
cap_table: cap_table,
data: ptr as *mut _,
pointers: ptr.offset((size.data as usize) as isize * BYTES_PER_WORD as isize) as *mut _,
data_size: size.data as WordCount32 * (BITS_PER_WORD as BitCount32),
pointer_count: size.pointers,
}
}
#[inline]
pub unsafe fn get_writable_struct_pointer<'a>(
arena: &'a dyn BuilderArena,
mut reff: *mut WirePointer,
mut segment_id: u32,
cap_table: CapTableBuilder,