-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
capn.go
521 lines (478 loc) · 12.5 KB
/
capn.go
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
package capnp
import (
"encoding/binary"
"errors"
"github.com/glycerine/rbtree"
)
// A SegmentID is a numeric identifier for a Segment.
type SegmentID uint32
// A Segment is an allocation arena for Cap'n Proto objects.
// It is part of a Message, which can contain other segments that
// reference each other.
type Segment struct {
msg *Message
id SegmentID
data []byte
}
// Message returns the message that contains s.
func (s *Segment) Message() *Message {
return s.msg
}
// ID returns the segment's ID.
func (s *Segment) ID() SegmentID {
return s.id
}
// Data returns the raw byte slice for the segment.
func (s *Segment) Data() []byte {
return s.data
}
func (s *Segment) inBounds(addr Address) bool {
return addr < Address(len(s.data))
}
func (s *Segment) regionInBounds(base Address, sz Size) bool {
end, ok := base.addSize(sz)
if !ok {
return false
}
return end <= Address(len(s.data))
}
// slice returns the segment of data from base to base+sz.
func (s *Segment) slice(base Address, sz Size) []byte {
// Bounds check should have happened before calling slice.
return s.data[base : base+Address(sz)]
}
func (s *Segment) readUint8(addr Address) uint8 {
return s.slice(addr, 1)[0]
}
func (s *Segment) readUint16(addr Address) uint16 {
return binary.LittleEndian.Uint16(s.slice(addr, 2))
}
func (s *Segment) readUint32(addr Address) uint32 {
return binary.LittleEndian.Uint32(s.slice(addr, 4))
}
func (s *Segment) readUint64(addr Address) uint64 {
return binary.LittleEndian.Uint64(s.slice(addr, 8))
}
func (s *Segment) readRawPointer(addr Address) rawPointer {
return rawPointer(s.readUint64(addr))
}
func (s *Segment) writeUint8(addr Address, val uint8) {
s.slice(addr, 1)[0] = val
}
func (s *Segment) writeUint16(addr Address, val uint16) {
binary.LittleEndian.PutUint16(s.slice(addr, 2), val)
}
func (s *Segment) writeUint32(addr Address, val uint32) {
binary.LittleEndian.PutUint32(s.slice(addr, 4), val)
}
func (s *Segment) writeUint64(addr Address, val uint64) {
binary.LittleEndian.PutUint64(s.slice(addr, 8), val)
}
func (s *Segment) writeRawPointer(addr Address, val rawPointer) {
s.writeUint64(addr, uint64(val))
}
// root returns a 1-element pointer list that references the first word
// in the segment. This only makes sense to call on the first segment
// in a message.
func (s *Segment) root() PointerList {
sz := ObjectSize{PointerCount: 1}
if !s.regionInBounds(0, sz.totalSize()) {
return PointerList{}
}
return PointerList{List{
seg: s,
length: 1,
size: sz,
depthLimit: s.msg.depthLimit(),
}}
}
func (s *Segment) lookupSegment(id SegmentID) (*Segment, error) {
if s.id == id {
return s, nil
}
return s.msg.Segment(id)
}
func (s *Segment) readPtr(off Address, depthLimit uint) (ptr Ptr, err error) {
val := s.readRawPointer(off)
s, off, val, err = s.resolveFarPointer(off, val)
if err != nil {
return Ptr{}, err
}
if val == 0 {
return Ptr{}, nil
}
if depthLimit == 0 {
return Ptr{}, errDepthLimit
}
// Be wary of overflow. Offset is 30 bits signed. List size is 29 bits
// unsigned. For both of these we need to check in terms of words if
// using 32 bit maths as bits or bytes will overflow.
switch val.pointerType() {
case structPointer:
sp, err := s.readStructPtr(off, val)
if err != nil {
return Ptr{}, err
}
if !s.msg.ReadLimiter().canRead(sp.readSize()) {
return Ptr{}, errReadLimit
}
sp.depthLimit = depthLimit - 1
return sp.ToPtr(), nil
case listPointer:
lp, err := s.readListPtr(off, val)
if err != nil {
return Ptr{}, err
}
if !s.msg.ReadLimiter().canRead(lp.readSize()) {
return Ptr{}, errReadLimit
}
lp.depthLimit = depthLimit - 1
return lp.ToPtr(), nil
case otherPointer:
if val.otherPointerType() != 0 {
return Ptr{}, errOtherPointer
}
return Interface{
seg: s,
cap: val.capabilityIndex(),
}.ToPtr(), nil
default:
// Only other types are far pointers.
return Ptr{}, errBadLandingPad
}
}
func (s *Segment) readStructPtr(off Address, val rawPointer) (Struct, error) {
addr, ok := val.offset().resolve(off)
if !ok {
return Struct{}, errPointerAddress
}
sz := val.structSize()
if !s.regionInBounds(addr, sz.totalSize()) {
return Struct{}, errPointerAddress
}
return Struct{
seg: s,
off: addr,
size: sz,
}, nil
}
func (s *Segment) readListPtr(off Address, val rawPointer) (List, error) {
addr, ok := val.offset().resolve(off)
if !ok {
return List{}, errPointerAddress
}
lsize, ok := val.totalListSize()
if !ok {
return List{}, errOverflow
}
if !s.regionInBounds(addr, lsize) {
return List{}, errPointerAddress
}
lt := val.listType()
if lt == compositeList {
hdr := s.readRawPointer(addr)
var ok bool
addr, ok = addr.addSize(wordSize)
if !ok {
return List{}, errOverflow
}
if hdr.pointerType() != structPointer {
return List{}, errBadTag
}
sz := hdr.structSize()
n := int32(hdr.offset())
// TODO(light): check that this has the same end address
if tsize, ok := sz.totalSize().times(n); !ok {
return List{}, errOverflow
} else if !s.regionInBounds(addr, tsize) {
return List{}, errPointerAddress
}
return List{
seg: s,
size: sz,
off: addr,
length: n,
flags: isCompositeList,
}, nil
}
if lt == bit1List {
return List{
seg: s,
off: addr,
length: val.numListElements(),
flags: isBitList,
}, nil
}
return List{
seg: s,
size: val.elementSize(),
off: addr,
length: val.numListElements(),
}, nil
}
func (s *Segment) resolveFarPointer(off Address, val rawPointer) (*Segment, Address, rawPointer, error) {
switch val.pointerType() {
case doubleFarPointer:
// A double far pointer points to a double pointer, where the
// first points to the actual data, and the second is the tag
// that would normally be placed right before the data (offset
// == 0).
faroff, segid := val.farAddress(), val.farSegment()
s, err := s.lookupSegment(segid)
if err != nil {
return nil, 0, 0, err
}
if !s.regionInBounds(faroff, wordSize*2) {
return nil, 0, 0, errPointerAddress
}
far := s.readRawPointer(faroff)
tagStart, ok := faroff.addSize(wordSize)
if !ok {
return nil, 0, 0, errOverflow
}
tag := s.readRawPointer(tagStart)
if far.pointerType() != farPointer || tag.offset() != 0 {
return nil, 0, 0, errPointerAddress
}
segid = far.farSegment()
if s, err = s.lookupSegment(segid); err != nil {
return nil, 0, 0, errBadLandingPad
}
return s, 0, landingPadNearPointer(far, tag), nil
case farPointer:
faroff, segid := val.farAddress(), val.farSegment()
s, err := s.lookupSegment(segid)
if err != nil {
return nil, 0, 0, err
}
if !s.regionInBounds(faroff, wordSize) {
return nil, 0, 0, errPointerAddress
}
val = s.readRawPointer(faroff)
return s, faroff, val, nil
default:
return s, off, val, nil
}
}
type offset struct {
id SegmentID
boff, bend int64 // in bits
newval Ptr
}
func makeOffsetKey(p Ptr) offset {
// Since this is used for copying, the address boundaries should already be clamped.
switch p.flags.ptrType() {
case structPtrType:
s := p.Struct()
return offset{
id: s.seg.id,
boff: int64(s.off) * 8,
bend: (int64(s.off) + int64(s.size.totalSize())) * 8,
}
case listPtrType:
l := p.List()
key := offset{
id: l.seg.id,
boff: int64(l.off) * 8,
}
if l.flags&isBitList != 0 {
key.bend = int64(l.off)*8 + int64(l.length)
} else {
key.bend = (int64(l.off) + int64(l.size.totalSize())*int64(l.length)) * 8
}
if l.flags&isCompositeList != 0 {
// Composite lists' offsets are after the tag word.
key.boff -= int64(wordSize) * 8
}
return key
default:
panic("unreachable")
}
}
func compare(a, b rbtree.Item) int {
ao := a.(offset)
bo := b.(offset)
switch {
case ao.id != bo.id:
return int(ao.id) - int(bo.id)
case ao.boff > bo.boff:
return 1
case ao.boff < bo.boff:
return -1
default:
return 0
}
}
func needsCopy(dest *Segment, src Ptr) bool {
if src.seg.msg != dest.msg {
return true
}
s := src.Struct()
if s.seg == nil {
return false
}
// Structs can only be referenced if they're not list members.
return s.flags&isListMember != 0
}
func (s *Segment) writePtr(cc copyContext, off Address, src Ptr) error {
// handle nulls
if !src.IsValid() {
s.writeRawPointer(off, 0)
return nil
}
srcSeg := src.Segment()
if i := src.Interface(); i.Segment() != nil {
if s.msg != srcSeg.msg {
c := s.msg.AddCap(i.Client())
i = NewInterface(s, c)
}
s.writeRawPointer(off, i.value(off))
return nil
}
if s != srcSeg {
// Different segments
if needsCopy(s, src) {
return copyPointer(cc, s, off, src)
}
if !hasCapacity(srcSeg.data, wordSize) {
// Double far pointer needed.
const landingSize = wordSize * 2
t, dstAddr, err := alloc(s, landingSize)
if err != nil {
return err
}
srcAddr := src.address()
t.writeRawPointer(dstAddr, rawFarPointer(srcSeg.id, srcAddr))
// alloc guarantees that two words are available.
t.writeRawPointer(dstAddr+Address(wordSize), src.value(srcAddr-Address(wordSize)))
s.writeRawPointer(off, rawDoubleFarPointer(t.id, dstAddr))
return nil
}
// Have room in the target for a tag
_, srcAddr, _ := alloc(srcSeg, wordSize)
srcSeg.writeRawPointer(srcAddr, src.value(srcAddr))
s.writeRawPointer(off, rawFarPointer(srcSeg.id, srcAddr))
return nil
}
s.writeRawPointer(off, src.value(off))
return nil
}
func copyPointer(cc copyContext, dstSeg *Segment, dstAddr Address, src Ptr) error {
if cc.depth >= 32 {
return errCopyDepth
}
cc = cc.init()
// First, see if the ptr has already been copied.
key := makeOffsetKey(src)
iter := cc.copies.FindLE(key)
if key.bend > key.boff {
if !iter.NegativeLimit() {
other := iter.Item().(offset)
if key.id == other.id {
if key.boff == other.boff && key.bend == other.bend {
return dstSeg.writePtr(cc.incDepth(), dstAddr, other.newval)
} else if other.bend >= key.bend {
return errOverlap
}
}
}
iter = iter.Next()
if !iter.Limit() {
other := iter.Item().(offset)
if key.id == other.id && other.boff < key.bend {
return errOverlap
}
}
}
// No copy nor overlap found, so we need to clone the target
newSeg, newAddr, err := alloc(dstSeg, Size((key.bend-key.boff)/8))
if err != nil {
return err
}
switch src.flags.ptrType() {
case structPtrType:
s := src.Struct()
dst := Struct{
seg: newSeg,
off: newAddr,
size: s.size,
depthLimit: maxDepth,
// clear flags
}
key.newval = dst.ToPtr()
cc.copies.Insert(key)
if err := copyStruct(cc, dst, s); err != nil {
return err
}
case listPtrType:
l := src.List()
dst := List{
seg: newSeg,
off: newAddr,
length: l.length,
size: l.size,
flags: l.flags,
depthLimit: maxDepth,
}
if dst.flags&isCompositeList != 0 {
// Copy tag word
newSeg.writeRawPointer(newAddr, l.seg.readRawPointer(l.off-Address(wordSize)))
var ok bool
dst.off, ok = dst.off.addSize(wordSize)
if !ok {
return errOverflow
}
}
key.newval = dst.ToPtr()
cc.copies.Insert(key)
// TODO(light): fast path for copying text/data
if dst.flags&isBitList != 0 {
copy(newSeg.data[newAddr:], l.seg.data[l.off:l.length+7/8])
} else {
for i := 0; i < l.Len(); i++ {
err := copyStruct(cc, dst.Struct(i), l.Struct(i))
if err != nil {
return err
}
}
}
default:
panic("unreachable")
}
return dstSeg.writePtr(cc.incDepth(), dstAddr, key.newval)
}
type copyContext struct {
copies *rbtree.Tree
depth int
}
func (cc copyContext) init() copyContext {
if cc.copies == nil {
return copyContext{
copies: rbtree.NewTree(compare),
}
}
return cc
}
func (cc copyContext) incDepth() copyContext {
return copyContext{
copies: cc.copies,
depth: cc.depth + 1,
}
}
var (
errPointerAddress = errors.New("capnp: invalid pointer address")
errBadLandingPad = errors.New("capnp: invalid far pointer landing pad")
errBadTag = errors.New("capnp: invalid tag word")
errOtherPointer = errors.New("capnp: unknown pointer type")
errObjectSize = errors.New("capnp: invalid object size")
errElementSize = errors.New("capnp: mismatched list element size")
errReadLimit = errors.New("capnp: read traversal limit reached")
errDepthLimit = errors.New("capnp: depth limit reached")
)
var (
errOverflow = errors.New("capnp: address or size overflow")
errOutOfBounds = errors.New("capnp: address out of bounds")
errCopyDepth = errors.New("capnp: copy depth too large")
errOverlap = errors.New("capnp: overlapping data on copy")
errListSize = errors.New("capnp: invalid list size")
)