forked from urbit/vere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.c
2275 lines (1940 loc) · 46.9 KB
/
manage.c
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
/// @file
#include "pkg/noun/manage.h"
#include "pkg/noun/v2/manage.h"
#include "pkg/noun/v3/manage.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "allocate.h"
#include "events.h"
#include "hashtable.h"
#include "imprison.h"
#include "jets.h"
#include "jets/k.h"
#include "log.h"
#include "nock.h"
#include "openssl/crypto.h"
#include "options.h"
#include "platform/rsignal.h"
#include "retrieve.h"
#include "trace.h"
#include "urcrypt.h"
#include "vortex.h"
#include "xtract.h"
// XX stack-overflow recovery should be gated by -a
//
#undef NO_OVERFLOW
/* (u3_noun)setjmp(u3R->esc.buf): setjmp within road.
*/
#if 0
c3_o
u3m_trap(void);
#else
# define u3m_trap() (u3_noun)(_setjmp(u3R->esc.buf))
#endif
/* u3m_signal(): treat a nock-level exception as a signal interrupt.
*/
void
u3m_signal(u3_noun sig_l);
/* u3m_dump(): dump the current road to stderr.
*/
void
u3m_dump(void);
/* u3m_fall(): return to parent road.
*/
void
u3m_fall(void);
/* u3m_leap(): in u3R, create a new road within the existing one.
*/
void
u3m_leap(c3_w pad_w);
/* u3m_golf(): record cap length for u3m_flog().
*/
c3_w
u3m_golf(void);
/* u3m_flog(): pop the cap.
**
** A common sequence for inner allocation is:
**
** c3_w gof_w = u3m_golf();
** u3m_leap();
** // allocate some inner stuff...
** u3m_fall();
** // inner stuff is still valid, but on cap
** u3m_flog(gof_w);
**
** u3m_flog(0) simply clears the cap.
*/
void
u3m_flog(c3_w gof_w);
/* u3m_soft_top(): top-level safety wrapper.
*/
u3_noun
u3m_soft_top(c3_w mil_w, // timer ms
c3_w pad_w, // base memory pad
u3_funk fun_f,
u3_noun arg);
// u3m_signal uses restricted functionality signals for compatibility reasons:
// some platforms may not provide true POSIX asynchronous signals and their
// compat layer will then implement this restricted functionality subset.
// u3m_signal never needs to interrupt I/O operations, its signal handlers
// do not manipulate signals, do not modify shared state, and always either
// return or longjmp.
//
static rsignal_jmpbuf u3_Signal;
#include "sigsegv.h"
#ifndef SIGSTKSZ
# define SIGSTKSZ 16384
#endif
#ifndef NO_OVERFLOW
static uint8_t Sigstk[SIGSTKSZ];
#endif
#if 0
/* _cm_punt(): crudely print trace.
*/
static void
_cm_punt(u3_noun tax)
{
u3_noun xat;
for ( xat = tax; xat; xat = u3t(xat) ) {
u3m_p("&", u3h(xat));
}
}
#endif
/* _cm_emergency(): write emergency text to stderr, never failing.
*/
static void
_cm_emergency(c3_c* cap_c, c3_l sig_l)
{
write(2, "\r\n", 2);
write(2, cap_c, strlen(cap_c));
if ( sig_l ) {
write(2, ": ", 2);
write(2, &sig_l, 4);
}
write(2, "\r\n", 2);
}
static void _cm_overflow(void *arg1, void *arg2, void *arg3)
{
(void)(arg1);
(void)(arg2);
(void)(arg3);
u3m_signal(c3__over);
}
/* _cm_signal_handle(): handle a signal in general.
*/
static void
_cm_signal_handle(c3_l sig_l)
{
if ( c3__over == sig_l ) {
#ifndef NO_OVERFLOW
sigsegv_leave_handler(_cm_overflow, NULL, NULL, NULL);
#endif
}
else {
u3m_signal(sig_l);
}
}
#ifndef NO_OVERFLOW
static void
_cm_signal_handle_over(int emergency, stackoverflow_context_t scp)
{
_cm_signal_handle(c3__over);
}
#endif
static void
_cm_signal_handle_term(int x)
{
// Ignore if we are using base memory from work memory, very rare.
//
if ( (0 != u3H->rod_u.kid_p) && (&(u3H->rod_u) == u3R) ) {
_cm_emergency("ignored", c3__term);
}
else {
_cm_signal_handle(c3__term);
}
}
static void
_cm_signal_handle_intr(int x)
{
// Interrupt: stop work. Ignore if not working, or (rarely) using base.
//
if ( &(u3H->rod_u) == u3R ) {
_cm_emergency("ignored", c3__intr);
}
else {
_cm_signal_handle(c3__intr);
}
}
static void
_cm_signal_handle_alrm(int x)
{
_cm_signal_handle(c3__alrm);
}
/* _cm_signal_reset(): reset top road after signal longjmp.
*/
static void
_cm_signal_reset(void)
{
u3R = &u3H->rod_u;
u3R->cap_p = u3R->mat_p;
u3R->ear_p = 0;
u3R->kid_p = 0;
}
#if 0
/* _cm_stack_recover(): recover stack trace, with lacunae.
*/
static u3_noun
_cm_stack_recover(u3a_road* rod_u)
{
c3_w len_w;
len_w = 0;
{
u3_noun tax = rod_u->bug.tax;
while ( tax ) {
len_w++;
tax = u3t(tax);
}
if ( len_w < 4096 ) {
return u3a_take(rod_u->bug.tax);
}
else {
u3_noun beg, fin;
c3_w i_w;
tax = rod_u->bug.tax;
beg = u3_nul;
for ( i_w = 0; i_w < 2048; i_w++ ) {
beg = u3nc(u3a_take(u3h(tax)), beg);
tax = u3t(tax);
}
beg = u3kb_flop(beg);
for ( i_w = 0; i_w < (len_w - 4096); i_w++ ) {
tax = u3t(tax);
}
fin = u3nc(u3nc(c3__lose, c3__over), u3a_take(tax));
return u3kb_weld(beg, fin);
}
}
}
#endif
/* _cm_stack_unwind(): unwind to the top level, preserving all frames.
*/
static u3_noun
_cm_stack_unwind(void)
{
u3_noun tax;
while ( u3R != &(u3H->rod_u) ) {
u3_noun yat = u3m_love(u3R->bug.tax);
u3R->bug.tax = u3kb_weld(yat, u3R->bug.tax);
}
tax = u3R->bug.tax;
u3R->bug.tax = 0;
return tax;
}
/* _cm_signal_recover(): recover from a deep signal, after longjmp. Free arg.
*/
static u3_noun
_cm_signal_recover(c3_l sig_l, u3_noun arg)
{
u3_noun tax;
// Unlikely to be set, but it can be made to happen.
//
tax = u3H->rod_u.bug.tax;
u3H->rod_u.bug.tax = 0;
if ( &(u3H->rod_u) == u3R ) {
// A top-level crash - rather odd. We should GC.
//
_cm_emergency("recover: top", sig_l);
u3C.wag_w |= u3o_check_corrupt;
// Reset the top road - the problem could be a fat cap.
//
_cm_signal_reset();
if ( (c3__meme == sig_l) && (u3a_open(u3R) <= 256) ) {
// Out of memory at the top level. Error becomes c3__full,
// and we release the emergency buffer. To continue work,
// we need to readjust the image, eg, migrate to 64 bit.
//
u3z(u3R->bug.mer);
u3R->bug.mer = 0;
sig_l = c3__full;
}
return u3nt(3, sig_l, tax);
}
else {
u3_noun pro;
// A signal was generated while we were within Nock.
//
_cm_emergency("recover: dig", sig_l);
#if 0
// Descend to the innermost trace, collecting stack.
//
{
u3a_road* rod_u;
u3R = &(u3H->rod_u);
rod_u = u3R;
while ( rod_u->kid_p ) {
#if 0
u3l_log("collecting %d frames",
u3kb_lent((u3to(u3_road, rod_u->kid_p)->bug.tax));
#endif
tax = u3kb_weld(_cm_stack_recover(u3to(u3_road, rod_u->kid_p)), tax);
rod_u = u3to(u3_road, rod_u->kid_p);
}
}
#else
tax = _cm_stack_unwind();
#endif
pro = u3nt(3, sig_l, tax);
_cm_signal_reset();
u3z(arg);
return pro;
}
}
/* _cm_signal_deep(): start deep processing; set timer for [mil_w] or 0.
*/
static void
_cm_signal_deep(c3_w mil_w)
{
// disable outer system signal handling
//
if ( 0 != u3C.sign_hold_f ) {
u3C.sign_hold_f();
}
#ifndef NO_OVERFLOW
stackoverflow_install_handler(_cm_signal_handle_over, Sigstk, SIGSTKSZ);
#endif
rsignal_install_handler(SIGINT, _cm_signal_handle_intr);
rsignal_install_handler(SIGTERM, _cm_signal_handle_term);
// Provide a little emergency memory, for use in case things
// go utterly haywire.
//
if ( 0 == u3H->rod_u.bug.mer ) {
u3H->rod_u.bug.mer = u3i_string(
"emergency buffer with sufficient space to cons the trace and bail"
);
}
if ( mil_w ) {
struct itimerval itm_u;
timerclear(&itm_u.it_interval);
itm_u.it_value.tv_sec = (mil_w / 1000);
itm_u.it_value.tv_usec = 1000 * (mil_w % 1000);
if ( rsignal_setitimer(ITIMER_VIRTUAL, &itm_u, 0) ) {
u3l_log("loom: set timer failed %s", strerror(errno));
}
else {
rsignal_install_handler(SIGVTALRM, _cm_signal_handle_alrm);
}
}
u3t_boot();
}
/* _cm_signal_done():
*/
static void
_cm_signal_done()
{
rsignal_deinstall_handler(SIGINT);
rsignal_deinstall_handler(SIGTERM);
rsignal_deinstall_handler(SIGVTALRM);
#ifndef NO_OVERFLOW
stackoverflow_deinstall_handler();
#endif
{
struct itimerval itm_u;
timerclear(&itm_u.it_interval);
timerclear(&itm_u.it_value);
if ( rsignal_setitimer(ITIMER_VIRTUAL, &itm_u, 0) ) {
u3l_log("loom: clear timer failed %s", strerror(errno));
}
}
// restore outer system signal handling
//
if ( 0 != u3C.sign_move_f ) {
u3C.sign_move_f();
}
u3t_boff();
}
/* u3m_signal(): treat a nock-level exception as a signal interrupt.
*/
void
u3m_signal(u3_noun sig_l)
{
rsignal_longjmp(u3_Signal, sig_l);
}
/* u3m_file(): load file, as atom, or bail.
*/
u3_noun
u3m_file(c3_c* pas_c)
{
struct stat buf_b;
c3_i fid_i = c3_open(pas_c, O_RDONLY, 0644);
c3_w fln_w, red_w;
c3_y* pad_y;
if ( (fid_i < 0) || (fstat(fid_i, &buf_b) < 0) ) {
u3l_log("%s: %s", pas_c, strerror(errno));
return u3m_bail(c3__fail);
}
fln_w = buf_b.st_size;
pad_y = c3_malloc(buf_b.st_size);
red_w = read(fid_i, pad_y, fln_w);
close(fid_i);
if ( fln_w != red_w ) {
c3_free(pad_y);
return u3m_bail(c3__fail);
}
else {
u3_noun pad = u3i_bytes(fln_w, (c3_y *)pad_y);
c3_free(pad_y);
return pad;
}
}
/* u3m_mark(): mark all nouns in the road.
*/
c3_w
u3m_mark(FILE* fil_u)
{
c3_w tot_w = 0;
tot_w += u3v_mark(fil_u);
tot_w += u3j_mark(fil_u);
tot_w += u3n_mark(fil_u);
tot_w += u3a_mark_road(fil_u);
return tot_w;
}
/* _pave_parts(): build internal tables.
*/
static void
_pave_parts(void)
{
u3R->cax.har_p = u3h_new_cache(u3C.hap_w); // transient
u3R->cax.per_p = u3h_new_cache(u3C.per_w); // persistent
u3R->jed.war_p = u3h_new();
u3R->jed.cod_p = u3h_new();
u3R->jed.han_p = u3h_new();
u3R->jed.bas_p = u3h_new();
u3R->byc.har_p = u3h_new();
}
/* _pave_road(): writes road boundaries to loom mem (stored at mat_w)
*/
static u3_road*
_pave_road(c3_w* rut_w, c3_w* mat_w, c3_w* cap_w, c3_w siz_w)
{
c3_dessert(((uintptr_t)rut_w & u3a_balign-1) == 0);
u3_road* rod_u = (void*) mat_w;
// enable in case of corruption
//
// memset(mem_w, 0, 4 * len_w);
memset(rod_u, 0, sizeof(c3_w) * siz_w);
// the top and bottom of the heap are initially the same
//
rod_u->rut_p = u3of(c3_w, rut_w);
rod_u->hat_p = u3of(c3_w, rut_w);
rod_u->mat_p = u3of(c3_w, mat_w); // stack bottom
rod_u->cap_p = u3of(c3_w, cap_w); // stack top
_rod_vaal(rod_u);
return rod_u;
}
/* _pave_north(): calculate boundaries and initialize north road.
mem_w - the "beginning" of your loom (its lowest address). Corresponds to rut
in a north road.
siz_w - the size in bytes of your road record (or home record in the case of
paving home).
len_w - size of your loom in words
*/
static u3_road*
_pave_north(c3_w* mem_w, c3_w siz_w, c3_w len_w, c3_o kid_o)
{
// in a north road, the heap is low and the stack is high
//
// the heap starts at the base memory pointer [mem_w];
// the stack starts at the end of the memory segment,
// minus space for the road structure [siz_w]
//
// 00~~~|R|---|H|######|C|+++|M|~~~FF
// ^--u3R which _pave_road returns (u3H for home road)
//
c3_w* mat_w = c3_align(mem_w + len_w - siz_w, u3a_balign, C3_ALGLO);
c3_w* rut_w = c3_align(mem_w, u3a_balign, C3_ALGHI);
c3_w* cap_w = mat_w;
if ( c3y == kid_o ) {
u3e_ward(u3of(c3_w, rut_w) - 1, u3of(c3_w, cap_w));
}
return _pave_road(rut_w, mat_w, cap_w, siz_w);
}
/* _pave_south(): calculate boundaries and initialize south road.
mem_w - the "beginning" of your loom (its lowest address). Corresponds to mat
in a south road.
siz_w - the size in bytes of your road record (or home record in the case of
paving home).
len_w - size of your loom in words
*/
static u3_road*
_pave_south(c3_w* mem_w, c3_w siz_w, c3_w len_w)
{
// in a south road, the heap is high and the stack is low
//
// the heap starts at the end of the memory segment;
// the stack starts at the base memory pointer [mem_w],
// and ends after the space for the road structure [siz_w]
//
// 00~~~|M|+++|C|######|H|---|R|~~~FFF
// ^---u3R which _pave_road returns
//
c3_w* mat_w = c3_align(mem_w, u3a_balign, C3_ALGHI);
c3_w* rut_w = c3_align(mem_w + len_w, u3a_balign, C3_ALGLO);
c3_w* cap_w = mat_w + siz_w;
u3e_ward(u3of(c3_w, cap_w) - 1, u3of(c3_w, rut_w));
return _pave_road(rut_w, mat_w, cap_w, siz_w);
}
/* _pave_home(): initialize pristine home road.
*/
static void
_pave_home(void)
{
c3_w* mem_w = u3_Loom + u3a_walign;
c3_w siz_w = c3_wiseof(u3v_home);
c3_w len_w = u3C.wor_i - u3a_walign;
u3H = (void *)_pave_north(mem_w, siz_w, len_w, c3n);
u3H->ver_w = U3V_VERLAT;
u3R = &u3H->rod_u;
_pave_parts();
}
STATIC_ASSERT( ((c3_wiseof(u3v_home) * 4) == sizeof(u3v_home)),
"home road alignment" );
/* _find_home(): in restored image, point to home road.
*/
static void
_find_home(void)
{
c3_w ver_w = *(u3_Loom + u3C.wor_i - 1);
c3_o mig_o = c3y; // did we migrate?
switch ( ver_w ) {
case U3V_VER1: u3m_v2_migrate();
case U3V_VER2: u3m_v3_migrate();
case U3V_VER3: {
mig_o = c3n;
break;
}
default: {
fprintf(stderr, "loom: checkpoint version mismatch: "
"have %u, need %u\r\n",
ver_w, U3V_VERLAT);
abort();
}
}
// NB: the home road is always north
//
c3_w* mem_w = u3_Loom + u3a_walign;
c3_w siz_w = c3_wiseof(u3v_home);
c3_w len_w = u3C.wor_i - u3a_walign;
c3_w* mat_w = c3_align(mem_w + len_w - siz_w, u3a_balign, C3_ALGLO);
u3H = (void *)mat_w;
u3R = &u3H->rod_u;
// this looks risky, but there are no legitimate scenarios
// where it's wrong
//
u3R->cap_p = u3R->mat_p = u3a_outa(u3H);
// check for obvious corruption
//
if ( c3n == mig_o ) {
c3_w nor_w, sou_w;
u3_post low_p, hig_p;
u3m_water(&low_p, &hig_p);
nor_w = (low_p + ((1 << u3a_page) - 1)) >> u3a_page;
sou_w = u3P.pag_w - (hig_p >> u3a_page);
if ( (nor_w > u3P.nor_u.pgs_w) || (sou_w != u3P.sou_u.pgs_w) ) {
fprintf(stderr, "loom: corrupt size north (%u, %u) south (%u, %u)\r\n",
nor_w, u3P.nor_u.pgs_w, sou_w, u3P.sou_u.pgs_w);
u3_assert(!"loom: corrupt size");
}
// the north segment is in-order on disk; it being oversized
// doesn't necessarily indicate corruption.
//
if ( nor_w < u3P.nor_u.pgs_w ) {
fprintf(stderr, "loom: strange size north (%u, %u)\r\n",
nor_w, u3P.nor_u.pgs_w);
}
// XX move me
//
u3a_ream();
}
/* As a further guard against any sneaky loom corruption */
u3a_loom_sane();
_rod_vaal(u3R);
}
/* u3m_pave(): instantiate or activate image.
*/
void
u3m_pave(c3_o nuu_o)
{
if ( c3y == nuu_o ) {
_pave_home();
}
else {
_find_home();
}
}
#if 0
/* u3m_clear(): clear all allocated data in road.
*/
void
u3m_clear(void)
{
u3h_free(u3R->cax.har_p);
u3j_free();
u3n_free();
}
void
u3m_dump(void)
{
c3_w hat_w;
c3_w fre_w = 0;
c3_w i_w;
hat_w = _(u3a_is_north(u3R)) ? u3R->hat_w - u3R->rut_w
: u3R->rut_w - u3R->hat_w;
for ( i_w = 0; i_w < u3_cc_fbox_no; i_w++ ) {
u3a_fbox* fre_u = u3R->all.fre_u[i_w];
while ( fre_u ) {
fre_w += fre_u->box_u.siz_w;
fre_u = fre_u->nex_u;
}
}
u3l_log("dump: hat_w %x, fre_w %x, allocated %x",
hat_w, fre_w, (hat_w - fre_w));
if ( 0 != (hat_w - fre_w) ) {
c3_w* box_w = _(u3a_is_north(u3R)) ? u3R->rut_w : u3R->hat_w;
c3_w mem_w = 0;
while ( box_w < (_(u3a_is_north(u3R)) ? u3R->hat_w : u3R->rut_w) ) {
u3a_box* box_u = (void *)box_w;
if ( 0 != box_u->use_w ) {
#ifdef U3_MEMORY_DEBUG
// u3l_log("live %d words, code %x", box_u->siz_w, box_u->cod_w);
#endif
mem_w += box_u->siz_w;
}
box_w += box_u->siz_w;
}
u3l_log("second count: %x", mem_w);
}
}
#endif
/* u3m_bail(): bail out. Does not return.
**
** Bail motes:
**
** %evil :: erroneous cryptography
** %exit :: semantic failure
** %oops :: assertion failure
** %intr :: interrupt
** %fail :: computability failure
** %over :: stack overflow (a kind of %fail)
** %meme :: out of memory
**
** These are equivalents of the full exception noun, the error ball:
**
** $% [%0 success]
** [%1 paths]
** [%2 trace]
** [%3 code trace]
** ==
**
** XX several of these abort() calls should be gated by -a
*/
c3_i
u3m_bail(u3_noun how)
{
// printf some metadata
//
switch ( how ) {
case c3__evil:
case c3__exit: break;
default: {
if ( _(u3ud(how)) ) {
c3_c str_c[5];
str_c[0] = ((how >> 0) & 0xff);
str_c[1] = ((how >> 8) & 0xff);
str_c[2] = ((how >> 16) & 0xff);
str_c[3] = ((how >> 24) & 0xff);
str_c[4] = 0;
fprintf(stderr, "\r\nbail: %s\r\n", str_c);
}
else if ( 1 != u3h(how) ) {
u3_assert(_(u3ud(u3h(how))));
fprintf(stderr, "\r\nbail: %d\r\n", u3h(how));
}
}
}
if ( &(u3H->rod_u) == u3R ) {
// XX set exit code
//
fprintf(stderr, "home: bailing out\r\n");
abort();
}
// intercept fatal errors
//
switch ( how ) {
case c3__foul:
case c3__oops: {
// XX set exit code
//
fprintf(stderr, "bailing out\r\n");
abort();
}
}
if ( &(u3H->rod_u) == u3R ) {
// For top-level errors, which shouldn't happen often, we have no
// choice but to use the signal process; and we require the flat
// form of how.
//
// XX JB: these seem unrecoverable, at least wrt memory management,
// so they've been disabled above for now
//
u3_assert(_(u3a_is_cat(how)));
u3m_signal(how);
}
// release the emergency buffer, ensuring space for cells
//
u3z(u3R->bug.mer);
u3R->bug.mer = 0;
/* Reconstruct a correct error ball.
*/
if ( _(u3ud(how)) ) {
switch ( how ) {
case c3__exit: {
how = u3nc(2, u3R->bug.tax);
} break;
default: {
how = u3nt(3, how, u3R->bug.tax);
} break;
}
}
/* Longjmp, with an underscore.
*/
_longjmp(u3R->esc.buf, how);
}
int c3_cooked() { return u3m_bail(c3__oops); }
/* u3m_error(): bail out with %exit, ct_pushing error.
*/
c3_i
u3m_error(c3_c* str_c)
{
u3t_mean(u3i_string(str_c));
return u3m_bail(c3__exit);
}
/* u3m_leap(): in u3R, create a new road within the existing one.
*/
void
u3m_leap(c3_w pad_w)
{
c3_w len_w; /* the length of the new road (avail - (pad + wiseof(u3a_road))) */
u3_road* rod_u;
_rod_vaal(u3R);
/* Measure the pad - we'll need it.
*/
{
#if 0
if ( pad_w < u3R->all.fre_w ) {
pad_w = 0;
}
else {
pad_w -= u3R->all.fre_w;
}
#endif
if ( (pad_w + c3_wiseof(u3a_road)) >= u3a_open(u3R) ) {
/* not enough storage to leap */
u3m_bail(c3__meme);
}
pad_w += c3_wiseof(u3a_road);
len_w = u3a_open(u3R) - pad_w;
c3_align(len_w, u3a_walign, C3_ALGHI);
}
/* Allocate a region on the cap.
*/
{
u3p(c3_w) bot_p; /* S: bot_p = new mat. N: bot_p = new rut */
if ( c3y == u3a_is_north(u3R) ) {
bot_p = u3R->hat_p + pad_w;
rod_u = _pave_south(u3a_into(bot_p), c3_wiseof(u3a_road), len_w);
#if 0
fprintf(stderr, "NPAR.hat_p: 0x%x %p, SKID.hat_p: 0x%x %p\r\n",
u3R->hat_p, u3a_into(u3R->hat_p),
rod_u->hat_p, u3a_into(rod_u->hat_p));
#endif
}
else {
bot_p = u3R->cap_p;
rod_u = _pave_north(u3a_into(bot_p), c3_wiseof(u3a_road), len_w, c3y);
#if 0
fprintf(stderr, "SPAR.hat_p: 0x%x %p, NKID.hat_p: 0x%x %p\r\n",
u3R->hat_p, u3a_into(u3R->hat_p),
rod_u->hat_p, u3a_into(rod_u->hat_p));
#endif
}
}
/* Attach the new road to its parents.
*/
{
u3_assert(0 == u3R->kid_p);
rod_u->par_p = u3of(u3_road, u3R);
u3R->kid_p = u3of(u3_road, rod_u);
}
/* Set up the new road.
*/
{
u3R = rod_u;
_pave_parts();
}
#ifdef U3_MEMORY_DEBUG
rod_u->all.fre_w = 0;
#endif
_rod_vaal(u3R);
}
void
_print_diff(c3_c* cap_c, c3_w a, c3_w b)
{
c3_w diff = a<b ? b-a : a-b;
u3a_print_memory(stderr, cap_c, diff);
}
/* u3m_fall(): in u3R, return an inner road to its parent.
*/
void
u3m_fall()
{
u3_assert(0 != u3R->par_p);
#if 0
/* If you're printing a lot of these you need to change
* u3a_print_memory from fprintf to u3l_log
*/
fprintf(stderr, "fall: from %s %p, to %s %p (cap 0x%x, was 0x%x)\r\n",
_(u3a_is_north(u3R)) ? "north" : "south",
u3R,
_(u3a_is_north(u3to(u3_road, u3R->par_p))) ? "north" : "south",
u3to(u3_road, u3R->par_p),
u3R->hat_p,
u3R->rut_p);
_print_diff("unused free", u3R->hat_p, u3R->cap_p);
_print_diff("freeing", u3R->rut_p, u3R->hat_p);
_print_diff("stack", u3R->cap_p, u3R->mat_p);
static c3_w wat_w = 500000000;
if (u3to(u3_road, u3R->par_p) == &u3H->rod_u) {
wat_w = 500000000;
}
else {
wat_w = c3_min(wat_w,
u3R->hat_p < u3R->cap_p ?
u3R->cap_p - u3R->hat_p :
u3R->hat_p - u3R->cap_p);
}
u3a_print_memory(stderr, "low water mark", wat_w);
#endif
u3to(u3_road, u3R->par_p)->pro.nox_d += u3R->pro.nox_d;
u3to(u3_road, u3R->par_p)->pro.cel_d += u3R->pro.cel_d;
/* The new cap is the old hat - it's as simple as that.
*/
u3to(u3_road, u3R->par_p)->cap_p = u3R->hat_p;
/* And, we're back home.
*/
u3R = u3to(u3_road, u3R->par_p);
u3R->kid_p = 0;
}
/* u3m_hate(): new, integrated leap mechanism (enter).
*/
void
u3m_hate(c3_w pad_w)
{
u3_assert(0 == u3R->ear_p);
u3R->ear_p = u3R->cap_p;
u3m_leap(pad_w);
u3R->bug.mer = u3i_string(
"emergency buffer with sufficient space to cons the trace and bail"
);
}
/* u3m_love(): return product from leap.
*/
u3_noun
u3m_love(u3_noun pro)
{
// save cache pointers from current road