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
/* shield.c: SHIELD IMPLEMENTATION
 *
 * $Id: //info.ravenbrook.com/project/mps/version/1.116/code/shield.c#1 $
 * Copyright (c) 2001-2016 Ravenbrook Limited.  See end of file for license.
 *
 * See: idea.shield, design.mps.shield.
 *
 * IMPORTANT: HERE BE DRAGONS! This code is subtle and
 * critical. Ensure you have read and understood design.mps.shield
 * before you touch it.
 */

#include "mpm.h"

SRCID(shield, "$Id: //info.ravenbrook.com/project/mps/version/1.116/code/shield.c#1 $");


void ShieldInit(Shield shield)
{
  shield->inside = FALSE;
  shield->suspended = FALSE;
  shield->queuePending = FALSE;
  shield->queue = NULL;
  shield->length = 0;
  shield->next = 0;
  shield->limit = 0;
  shield->depth = 0;
  shield->unsynced = 0;
  shield->holds = 0;
  shield->sig = ShieldSig;
}


void ShieldDestroyQueue(Shield shield, Arena arena)
{
  AVER(shield->limit == 0); /* queue must be empty */
  
  if (shield->length != 0) {
    AVER(shield->queue != NULL);
    ControlFree(arena, shield->queue,
                shield->length * sizeof shield->queue[0]);
    shield->queue = NULL;
    shield->length = 0;
  }
}


void ShieldFinish(Shield shield)
{
  /* The queue should already have been destroyed by
     GlobalsPrepareToDestroy calling ShieldDestroyQueue. */
  AVER(shield->length == 0);
  AVER(shield->limit == 0);
  AVER(shield->queue == NULL);

  AVER(shield->depth == 0);
  AVER(shield->unsynced == 0);
  AVER(shield->holds == 0);
  shield->sig = SigInvalid;
}


static Bool SegIsSynced(Seg seg);

Bool ShieldCheck(Shield shield)
{
  CHECKS(Shield, shield);
  /* Can't check Boolean bitfields <design/type/#bool.bitfield.check> */
  CHECKL(shield->queue == NULL || shield->length > 0);
  CHECKL(shield->limit <= shield->length);
  CHECKL(shield->next <= shield->limit);

  /* The mutator is not suspended while outside the shield
     (design.mps.shield.inv.outside.running). */
  CHECKL(shield->inside || !shield->suspended);

  /* If any segment is not synced, the mutator is suspended
     (design.mps.shield.inv.unsynced.suspended). */
  CHECKL(shield->unsynced == 0 || shield->suspended);

  /* The total depth is zero while outside the shield
     (design.mps.shield.inv.outside.depth). */
  CHECKL(shield->inside || shield->depth == 0);

  /* There are no unsynced segments when we're outside the shield. */
  CHECKL(shield->inside || shield->unsynced == 0);

  /* Every unsynced segment should be on the queue, because we have to
     remember to sync it before we return to the mutator. */
  CHECKL(shield->limit + shield->queuePending >= shield->unsynced);

  /* The mutator is suspeneded if there are any holds. */
  CHECKL(shield->holds == 0 || shield->suspended);

  /* This is too expensive to check all the time since we have an
     expanding shield queue that often has 16K elements instead of
     16. */
#if defined(AVER_AND_CHECK_ALL)
  {
    Count unsynced = 0;
    Index i;
    for (i = 0; i < shield->limit; ++i) {
      Seg seg = shield->queue[i];
      CHECKD(Seg, seg);
      if (!SegIsSynced(seg))
        ++unsynced;
    }
    CHECKL(unsynced + shield->queuePending == shield->unsynced);
  }
#endif

  return TRUE;
}


Res ShieldDescribe(Shield shield, mps_lib_FILE *stream, Count depth)
{
  Res res;
  
  res = WriteF(stream, depth,
               "Shield $P {\n",    (WriteFP)shield,
               "  ", shield->inside ? "inside" : "outside", " shield\n",
               "  suspended $S\n", WriteFYesNo(shield->suspended),
               "  depth     $U\n", (WriteFU)shield->depth,
               "  next      $U\n", (WriteFU)shield->next,
               "  length    $U\n", (WriteFU)shield->length,
               "  unsynced  $U\n", (WriteFU)shield->unsynced,
               "  holds     $U\n", (WriteFU)shield->holds,
               "} Shield $P\n",    (WriteFP)shield,
               NULL);
  if (res != ResOK)
    return res;

  return ResOK;
}


/* SHIELD_AVER -- transgressive argument checking
 *
 * .trans.check: A number of shield functions cannot do normal
 * argument checking with AVERT because (for example) SegCheck checks
 * the shield invariants, and it is these functions that are enforcing
 * them.  Instead, we AVER(TESTT(Seg, seg)) to check the type
 * signature but not the contents.
 */

#define SHIELD_AVERT(type, exp) AVER(TESTT(type, exp))
#define SHIELD_AVERT_CRITICAL(type, exp) AVER_CRITICAL(TESTT(type, exp))


/* SegIsSynced -- is a segment synced?
 *
 * See design.mps.shield.def.synced.
 */

static Bool SegIsSynced(Seg seg)
{
  SHIELD_AVERT_CRITICAL(Seg, seg);
  return SegSM(seg) == SegPM(seg);
}


/* shieldSetSM -- set shield mode, maintaining sync count */

static void shieldSetSM(Shield shield, Seg seg, AccessSet mode)
{
  if (SegSM(seg) != mode) {
    if (SegIsSynced(seg)) {
      SegSetSM(seg, mode);
      ++shield->unsynced;
    } else {
      SegSetSM(seg, mode);
      if (SegIsSynced(seg)) {
        AVER(shield->unsynced > 0);
        --shield->unsynced;
      }
    }
  }
}


/* shieldSetPM -- set protection mode, maintaining sync count */

static void shieldSetPM(Shield shield, Seg seg, AccessSet mode)
{
  if (SegPM(seg) != mode) {
    if (SegIsSynced(seg)) {
      SegSetPM(seg, mode);
      ++shield->unsynced;
    } else {
      SegSetPM(seg, mode);
      if (SegIsSynced(seg)) {
        AVER(shield->unsynced > 0);
        --shield->unsynced;
      }
    }
  }
}  


/* SegIsExposed -- is a segment exposed?
 *
 * See design.mps.shield.def.exposed.
 */

static Bool SegIsExposed(Seg seg)
{
  SHIELD_AVERT_CRITICAL(Seg, seg);
  return seg->depth > 0;
}


/* shieldSync -- synchronize a segment's protection
 *
 * See design.mps.shield.inv.prot.shield.
 */

static void shieldSync(Shield shield, Seg seg)
{
  SHIELD_AVERT_CRITICAL(Seg, seg);

  if (!SegIsSynced(seg)) {
    shieldSetPM(shield, seg, SegSM(seg));
    ProtSet(SegBase(seg), SegLimit(seg), SegPM(seg));
  }
}


/* shieldSUspend -- suspend the mutator
 *
 * Called from inside impl.c.shield when any segment is not synced, in
 * order to provide exclusive access to the segment by the MPS.  See
 * .inv.unsynced.suspended.
 */

static void shieldSuspend(Arena arena)
{
  Shield shield;
  
  AVERT(Arena, arena);
  shield = ArenaShield(arena);
  AVER(shield->inside);

  if (!shield->suspended) {
    ThreadRingSuspend(ArenaThreadRing(arena), ArenaDeadRing(arena));
    shield->suspended = TRUE;
  }
}


/* ShieldHold -- suspend mutator access to the unprotectable
 *
 * From outside impl.c.shield, this is used when we really need to
 * lock everything against the mutator -- for example, during flip
 * when we must scan all thread registers at once.
 */

void (ShieldHold)(Arena arena)
{
  AVERT(Arena, arena);
  shieldSuspend(arena);
  ++ArenaShield(arena)->holds;
}


/* ShieldRelease -- declare mutator could be resumed
 *
 * In practice, we don't resume the mutator until ShieldLeave, but
 * this marks the earliest point at which we could resume.
 */

void (ShieldRelease)(Arena arena)
{
  Shield shield;
  
  AVERT(Arena, arena);
  shield = ArenaShield(arena);
  AVER(shield->inside);
  AVER(shield->suspended);

  AVER(shield->holds > 0);
  --shield->holds;

  /* It is only correct to actually resume the mutator here if
     shield->depth is 0, shield->unsycned is 0, and the queue is
     empty. */
  /* See design.mps.shield.improv.resume for a discussion of when it
     might be a good idea to resume the mutator early. */
}


/* shieldProtLower -- reduce protection on a segment
 *
 * This ensures actual prot mode does not include mode.
 */

static void shieldProtLower(Shield shield, Seg seg, AccessSet mode)
{
  /* <design/trace/#fix.noaver> */
  SHIELD_AVERT_CRITICAL(Seg, seg);
  AVERT_CRITICAL(AccessSet, mode);

  if (BS_INTER(SegPM(seg), mode) != AccessSetEMPTY) {
    shieldSetPM(shield, seg, BS_DIFF(SegPM(seg), mode));
    ProtSet(SegBase(seg), SegLimit(seg), SegPM(seg));
  }
}


/* shieldDequeue -- remove a segment from the shield queue */

static Seg shieldDequeue(Shield shield, Index i)
{
  Seg seg;
  AVER(i < shield->limit);
  seg = shield->queue[i];
  AVERT(Seg, seg);
  AVER(seg->queued);
  shield->queue[i] = NULL; /* to ensure it can't get re-used */
  seg->queued = FALSE;
  return seg;
}


/* shieldFlushEntry -- flush a single entry from the queue
 *
 * If the segment is exposed we can simply dequeue it, because later
 * there will be a call to ShieldCover that will put it back on the
 * queue.  If the segment is not exposed, we can sync its protection.
 * (And if it does not have the shield raised any more, that will do
 * nothing.)
 */

static void shieldFlushEntry(Shield shield, Index i)
{
  Seg seg = shieldDequeue(shield, i);

  if (!SegIsExposed(seg))
    shieldSync(shield, seg);
}


/* shieldQueueReset -- reset shield queue pointers */

static void shieldQueueReset(Shield shield)
{
  AVER(shield->depth == 0); /* overkill: implies no segs are queued */
  AVER(shield->unsynced == 0);
  shield->next = 0;
  shield->limit = 0;
}


/* shieldQueueEntryCompare -- comparison for queue sorting */

static Compare shieldAddrCompare(Addr left, Addr right)
{
  if (left < right)
    return CompareLESS;
  else if (left == right)
    return CompareEQUAL;
  else
    return CompareGREATER;
}

static Compare shieldQueueEntryCompare(void *left, void *right, void *closure)
{
  Seg segA = left, segB = right;

  /* These checks are not critical in a hot build, but slow down cool
     builds quite a bit, so just check the signatures. */
  AVER(TESTT(Seg, segA));
  AVER(TESTT(Seg, segB));
  UNUSED(closure);

  return shieldAddrCompare(SegBase(segA), SegBase(segB));
}


/* shieldFlushEntries -- flush queue coalescing protects
 *
 * Sort the shield queue into address order, then iterate over it
 * coalescing protection work, in order to reduce the number of system
 * calls to a minimum.  This is very important on OS X, where
 * protection calls are extremely inefficient, but has no net gain on
 * Windows.
 *
 * TODO: Could we keep extending the outstanding area over memory
 * that's *not* in the queue but has the same protection mode?  Might
 * require design.mps.shield.improve.noseg.
 */

static void shieldFlushEntries(Shield shield)
{
  Addr base = NULL, limit;
  AccessSet mode;
  Index i;

  if (shield->length == 0) {
    AVER(shield->queue == NULL);
    return;
  }

  QuickSort((void *)shield->queue, shield->limit,
            shieldQueueEntryCompare, UNUSED_POINTER,
            &shield->sortStruct);

  mode = AccessSetEMPTY;
  limit = NULL;
  for (i = 0; i < shield->limit; ++i) {
    Seg seg = shieldDequeue(shield, i);
    if (!SegIsSynced(seg)) {
      shieldSetPM(shield, seg, SegSM(seg));
      if (SegSM(seg) != mode || SegBase(seg) != limit) {
        if (base != NULL) {
          AVER(base < limit);
          ProtSet(base, limit, mode);
        }
        base = SegBase(seg);
        mode = SegSM(seg);
      }
      limit = SegLimit(seg);
    }
  }
  if (base != NULL) {
    AVER(base < limit);
    ProtSet(base, limit, mode);
  }

  shieldQueueReset(shield);
}


/* shieldQueue -- consider adding a segment to the queue
 *
 * If the segment is out of sync, either sync it, or ensure it is
 * queued and the mutator is suspended.
 */

static void shieldQueue(Arena arena, Seg seg)
{
  Shield shield;
  
  /* <design/trace/#fix.noaver> */
  AVERT_CRITICAL(Arena, arena);
  shield = ArenaShield(arena);
  SHIELD_AVERT_CRITICAL(Seg, seg);

  if (SegIsSynced(seg) || seg->queued)
    return;

  if (SegIsExposed(seg)) {
    /* This can occur if the mutator isn't suspended, we expose a
       segment, then raise the shield on it.  In this case, the
       mutator isn't allowed to see the segment, but we don't need to
       queue it until its covered. */
    shieldSuspend(arena);
    return;
  }

  /* Allocate or extend the shield queue if necessary. */
  if (shield->next >= shield->length) {
    void *p;
    Res res;
    Count length;

    AVER(shield->next == shield->length);

    if (shield->length == 0)
      length = ShieldQueueLENGTH;
    else
      length = shield->length * 2;
    
    res = ControlAlloc(&p, arena, length * sizeof shield->queue[0]);
    if (res != ResOK) {
      AVER(ResIsAllocFailure(res));
      /* Carry on with the existing queue. */
    } else {
      if (shield->length > 0) {
        Size oldSize = shield->length * sizeof shield->queue[0];
        AVER(shield->queue != NULL);
        mps_lib_memcpy(p, shield->queue, oldSize);
        ControlFree(arena, shield->queue, oldSize);
      }
      shield->queue = p;
      shield->length = length;
    }
  }

  /* Queue unavailable, so synchronize now.  Or if the mutator is not
     yet suspended and the code raises the shield on a covered
     segment, protect it now, because that's probably better than
     suspending the mutator. */
  if (shield->length == 0 || !shield->suspended) {
    shieldSync(shield, seg);
    return;
  }

  AVER_CRITICAL(shield->limit <= shield->length);
  AVER_CRITICAL(shield->next <= shield->limit);

  /* If we failed to extend the shield queue array, degrade to an LRU
     circular buffer. */
  if (shield->next >= shield->length)
    shield->next = 0;
  AVER_CRITICAL(shield->next < shield->length);

  AVER_CRITICAL(shield->length > 0);

  /* If the limit is less than the length, then the queue array has
     yet to be filled, and next is an uninitialized entry.
     Otherwise it's the tail end from last time around, and needs to
     be flushed. */
  if (shield->limit >= shield->length) {
    AVER_CRITICAL(shield->limit == shield->length);
    shieldFlushEntry(shield, shield->next);
  }

  shield->queue[shield->next] = seg;
  ++shield->next;
  seg->queued = TRUE;

  if (shield->next >= shield->limit)
    shield->limit = shield->next;
}


/* ShieldRaise -- declare segment should be protected from mutator
 *
 * Does not immediately protect the segment, unless the segment is
 * covered and the shield queue is unavailable.
 */

void (ShieldRaise)(Arena arena, Seg seg, AccessSet mode)
{
  Shield shield;

  SHIELD_AVERT(Arena, arena);
  SHIELD_AVERT(Seg, seg);
  AVERT(AccessSet, mode);
  shield = ArenaShield(arena);
  AVER(!shield->queuePending);
  shield->queuePending = TRUE;

  /* design.mps.shield.inv.prot.shield preserved */
  shieldSetSM(ArenaShield(arena), seg, BS_UNION(SegSM(seg), mode));
  
  /* Ensure design.mps.shield.inv.unsynced.suspended and
     design.mps.shield.inv.unsynced.depth */
  shieldQueue(arena, seg);
  shield->queuePending = FALSE;

  /* Check queue and segment consistency. */
  AVERT(Arena, arena);
  AVERT(Seg, seg);
}


/* ShieldLower -- declare segment may be accessed by mutator */

void (ShieldLower)(Arena arena, Seg seg, AccessSet mode)
{
  Shield shield;
  
  AVERT(Arena, arena);
  shield = ArenaShield(arena);
  SHIELD_AVERT(Seg, seg);
  AVERT(AccessSet, mode);

  /* SegIsSynced(seg) is not changed by the following preserving
     design.mps.shield.inv.unsynced.suspended and
     design.mps.shield.inv.prot.shield. */
  shieldSetSM(shield, seg, BS_DIFF(SegSM(seg), mode));
  /* TODO: Do we need to promptly call shieldProtLower here?  It
     loses the opportunity to coalesce the protection call. It would
     violate design.mps.shield.prop.inside.access. */
  /* shieldQueue(arena, seg); */
  shieldProtLower(shield, seg, mode);

  /* Check queue and segment consistency. */
  AVERT(Arena, arena);
  AVERT(Seg, seg);
}


/* ShieldEnter -- enter the shield, allowing exposes */

void (ShieldEnter)(Arena arena)
{
  Shield shield;
  
  AVERT(Arena, arena);
  shield = ArenaShield(arena);
  AVER(!shield->inside);
  AVER(shield->depth == 0);
  AVER(!shield->suspended);

  shieldQueueReset(shield);

  shield->inside = TRUE;
}


/* shieldDebugCheck -- expensive consistency check
 *
 * While developing the shield it is very easy to make a consistency
 * mistake that causes random corruption of the heap, usually because
 * all the attempts to avoid protection and suspension end up failing
 * to enforce design.mps.shield.prop.mutator.access.  In these cases,
 * try enabling SHIELD_DEBUG and extending this code as necessary.
 *
 * The basic idea is to iterate over *all* segments and check
 * consistency with the arena and shield queue.
 */

#if defined(SHIELD_DEBUG)
static void shieldDebugCheck(Arena arena)
{
  Shield shield;
  Seg seg;
  Count queued = 0;
  Count depth = 0;

  AVERT(Arena, arena);
  shield = ArenaShield(arena);
  AVER(shield->inside || shield->limit == 0);

  if (SegFirst(&seg, arena))
    do {
      depth += SegDepth(seg);
      if (shield->limit == 0) {
        AVER(!seg->queued);
        AVER(SegIsSynced(seg));
        /* You can directly set protections here to see if it makes a
           difference. */
        /* ProtSet(SegBase(seg), SegLimit(seg), SegPM(seg)); */
      } else {
        if (seg->queued)
          ++queued;
      }
    } while(SegNext(&seg, arena, seg));

  AVER(depth == shield->depth);
  AVER(queued == shield->limit);
}
#endif


/* ShieldFlush -- empty the shield queue
 *
 * .shield.flush: Flush empties the shield queue.  This needs to be
 * called before queued segments are destroyed, to remove them from
 * the queue.  We flush the whole queue because finding the entry is
 * O(n) and we're very likely reclaiming and destroying loads of
 * segments.  See also design.mps.shield.improv.resume.
 *
 * The memory for the segment may become spare, and not released back
 * to the operating system. Since we keep track of protection on
 * segments and not grains we have no way of keeping track of the
 * protection state of spare grains. We therefore flush the protection
 * to get it back into the default state (unprotected).  See also
 * design.mps.shield.improv.noseg.
 */

void (ShieldFlush)(Arena arena)
{
  Shield shield;
  
  AVERT(Arena, arena);
  shield = ArenaShield(arena);
#ifdef SHIELD_DEBUG
  shieldDebugCheck(arena);
#endif
  shieldFlushEntries(shield);
  AVER(shield->unsynced == 0); /* everything back in sync */
#ifdef SHIELD_DEBUG
  shieldDebugCheck(arena);
#endif
}


/* ShieldLeave -- leave the shield, protect segs from mutator */

void (ShieldLeave)(Arena arena)
{
  Shield shield;
  
  AVERT(Arena, arena);
  shield = ArenaShield(arena);
  AVER(shield->inside);
  AVER(shield->depth == 0); /* no pending covers */
  AVER(shield->holds == 0);

  ShieldFlush(arena);

  AVER(shield->unsynced == 0); /* everything back in sync */

  /* Ensuring the mutator is running at this point guarantees
     .inv.outside.running */
  if (shield->suspended) {
    ThreadRingResume(ArenaThreadRing(arena), ArenaDeadRing(arena));
    shield->suspended = FALSE;
  }

  shield->inside = FALSE;
}



/* ShieldExpose -- allow the MPS access to a segment while denying the mutator
 *
 * The first expose of a shielded segment suspends the mutator to
 * ensure the MPS has exclusive access.
 */

void (ShieldExpose)(Arena arena, Seg seg)
{
  Shield shield;
  AccessSet mode = AccessREAD | AccessWRITE;

  /* <design/trace/#fix.noaver> */
  AVERT_CRITICAL(Arena, arena);
  shield = ArenaShield(arena);
  AVER_CRITICAL(shield->inside);

  SegSetDepth(seg, SegDepth(seg) + 1);
  AVER_CRITICAL(SegDepth(seg) > 0); /* overflow */
  ++shield->depth;
  AVER_CRITICAL(shield->depth > 0); /* overflow */
  
  if (BS_INTER(SegPM(seg), mode) != AccessSetEMPTY)
    shieldSuspend(arena);

  /* Ensure design.mps.shield.inv.expose.prot. */
  /* TODO: Mass exposure -- see
     design.mps.shield.improv.mass-expose. */
  shieldProtLower(shield, seg, mode);
}


/* ShieldCover -- declare MPS no longer needs access to seg */

void (ShieldCover)(Arena arena, Seg seg)
{
  Shield shield;
  
  /* <design/trace/#fix.noaver> */
  AVERT_CRITICAL(Arena, arena);
  shield = ArenaShield(arena);
  AVERT_CRITICAL(Seg, seg);
  AVER_CRITICAL(SegPM(seg) == AccessSetEMPTY);
 
  AVER_CRITICAL(SegDepth(seg) > 0);
  SegSetDepth(seg, SegDepth(seg) - 1);
  AVER_CRITICAL(shield->depth > 0);
  --shield->depth;

  /* Ensure design.mps.shield.inv.unsynced.depth. */
  shieldQueue(arena, seg);
}


/* C. COPYRIGHT AND LICENSE
 *
 * Copyright (C) 2001-2016 Ravenbrook Limited <http://www.ravenbrook.com/>.
 * All rights reserved.  This is an open source license.  Contact
 * Ravenbrook for commercial licensing options.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. Redistributions in any form must be accompanied by information on how
 * to obtain complete source code for this software and any accompanying
 * software that uses this software.  The source code must either be
 * included in the distribution or be available for no more than the cost
 * of distribution plus a nominal fee, and must be freely redistributable
 * under reasonable conditions.  For an executable file, complete source
 * code means the source code for all modules it contains. It does not
 * include source code for modules or files that typically accompany the
 * major components of the operating system on which the executable file
 * runs.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */