forked from tcltk-depot/tbcload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmpRead.c
More file actions
2956 lines (2613 loc) · 84.8 KB
/
Copy pathcmpRead.c
File metadata and controls
2956 lines (2613 loc) · 84.8 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
/*
* cmpRead.c --
*
* This file contains the code used by the compiled file script loader to
* load a compiled script. The script loader is registered in the Init
* procedure of the "Loader" package.
*
* Copyright (c) 1998-2000 Ajuba Solutions
* Copyright (c) 2000, 2017 ActiveState Software Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: cmpRead.c,v 1.11 2002/11/28 16:53:10 andreas_kupries Exp $
*/
#include "cmpInt.h"
#include "proTbcLoad.h"
/*
* This structure contains signature information from a compiled file.
*/
typedef struct ImageSignature
{
Tcl_Size formatNumber; /* version number of the .tbc file format */
Tcl_Size cmpMajorVersion; /* major version of the compiler package that generated the compiled image */
Tcl_Size cmpMinorVersion; /* minor version of the compiler package that generated the compiled image */
Tcl_Size tclMajorVersion; /* major version of the interpreter that generated the compiled image */
Tcl_Size tclMinorVersion; /* minor version of the interpreter that generated the compiled image */
} ImageSignature;
/*
* The extraction environment contains the state of an extraction for a compiled
* ByteCode structure.
*/
typedef struct ExtractionEnv
{
char* imageBase; /* base of the compiled image bytes */
char* imageEnd; /* address immediately following the last byte in the compiled image */
char* curImagePtr; /* pointer to the part of the compiled image we are parsing */
ByteCode* codePtr; /* the ByteCode structure we are populating */
Tcl_Size codeSize; /* the size of the Bytecode structure */
LocMapSizes locMap; /* this structure holds the sizes of the location map arrays as extracted from the image header */
ImageSignature sig; /* Image signature from file */
} ExtractionEnv;
/*
* This struct holds the decoding context for a run of ExtractByteSequence
*/
typedef struct A85DecodeContext
{
Tcl_Size bytesToDecode; /* number of bytes left to decode */
unsigned char* curPtr; /* pointer to the next available location in the decode buffer */
int curChar; /* index of the next base-85 digit to be stored in the decode buffer */
int decodeBuf[5]; /* buffer to hold the 5-tuple we are currently collecting. */
} A85DecodeContext;
/*
* The current format version number. This will be set in
* TbcloadInit to the correct value for the current Tcl interpreter.
*/
static Tcl_Size formatVersion = 3;
/*
* The current Tcl version that we are loaded in.
*/
static Tcl_Size tclMajorVersion = 0, tclMinorVersion = 0;
/*
* No source available message. The source field in ByteCode structures that
* are extracted from compiled images is initialized with this string, and
* will be used to generate call stacks and error info.
* Also the string representation of procedure body objects is initialized
* with this value.
* Must be kept to under 128 bytes in length (because of the way the source
* code map is automatically generated in the ByteCode structs).
* There are two parts to the "source" for a compiled script:
* - a comment fo humans to look at (for example in an errorInfo trace)
* - a bit of code that throws an error; this is used to trigger an error
* in some situations; for example, if the following construct is used
* proc A {args} { set x 5 ; return "hello world $x" }
* proc B {args} [info body A]
* a call to B will trigger the error.
*/
static char noSourceCode[] = "# Compiled -- no source code available\nerror \"called a copy of a compiled script\"";
static Tcl_Size noSourceCodeSize = sizeof(noSourceCode);
/*
* Marker string appended by Tbcload_EvalObjCmd to the errorInfo, for use by
* the catch code to strip out error info that we don't want.
*/
#if USE_CATCH_WRAPPER
static char errorInfoMarker[] = CMP_ERRORINFO_MARKER;
#endif
/*
* Map between ExceptionRangeType enums and type codes.
* This map must be kept consistent with the equivalent one in cmpWrite.c.
*/
static ExcRangeMap excRangeMap[] = {
{LOOP_EXCEPTION_RANGE, CMP_LOOP_EXCEPTION_RANGE}, {CATCH_EXCEPTION_RANGE, CMP_CATCH_EXCEPTION_RANGE}, {0, '\0'}};
/*
* The list of VAR_ flag values to check when emitting. The order is
* is important an must be kept consistent with the equivalent list in
* cmpWrite.c
*/
static int varFlagsList[] = {0, 0, 0, 0, 0, 0, 0, 0, VAR_ARGUMENT, VAR_TEMPORARY, 0};
static int varFlagsListSize = sizeof(varFlagsList) / sizeof(varFlagsList[0]);
/*
* We use a modified encoding scheme which avoids the Tcl special characters
* $, {, }, [, ], and \.
* Because of this, we need to use a table instead of generating the character
* codes arithmetically.
* The decode map maps character values in the range 0 - 127 to the
* corresponding base-85 digit's value; characters that are not base-85
* digits have a map value of -1 if they are considered whitespace, and -2 if
* they are illegal characters in the sequence (they should not appear).
* The decoder skips whitespace while decoding.
*/
#define times85(x) ((((((x << 2) + x) << 2) + x) << 2) + x)
#define A85_WHITESPACE -1
#define A85_ILLEGAL_CHAR -2
#define A85_Z -3
static int decodeMap[] = {
A85_ILLEGAL_CHAR, /* ^@ */
A85_ILLEGAL_CHAR, /* ^A */
A85_ILLEGAL_CHAR, /* ^B */
A85_ILLEGAL_CHAR, /* ^C */
A85_ILLEGAL_CHAR, /* ^D */
A85_ILLEGAL_CHAR, /* ^E */
A85_ILLEGAL_CHAR, /* ^F */
A85_ILLEGAL_CHAR, /* ^G */
A85_ILLEGAL_CHAR, /* ^H */
A85_WHITESPACE, /* \t */
A85_WHITESPACE, /* \n */
A85_ILLEGAL_CHAR, /* ^K */
A85_ILLEGAL_CHAR, /* ^L */
A85_ILLEGAL_CHAR, /* ^M */
A85_ILLEGAL_CHAR, /* ^N */
A85_ILLEGAL_CHAR, /* ^O */
A85_ILLEGAL_CHAR, /* ^P */
A85_ILLEGAL_CHAR, /* ^Q */
A85_ILLEGAL_CHAR, /* ^R */
A85_ILLEGAL_CHAR, /* ^S */
A85_ILLEGAL_CHAR, /* ^T */
A85_ILLEGAL_CHAR, /* ^U */
A85_ILLEGAL_CHAR, /* ^V */
A85_ILLEGAL_CHAR, /* ^W */
A85_ILLEGAL_CHAR, /* ^X */
A85_ILLEGAL_CHAR, /* ^Y */
A85_ILLEGAL_CHAR, /* ^Z */
A85_ILLEGAL_CHAR, /* ^[ */
A85_ILLEGAL_CHAR, /* ^\ */
A85_ILLEGAL_CHAR, /* ^] */
A85_ILLEGAL_CHAR, /* ^^ */
A85_ILLEGAL_CHAR, /* ^_ */
A85_WHITESPACE, /* */
0, /* ! */
A85_ILLEGAL_CHAR, /* " (for hilit: ") */
2, /* # */
A85_ILLEGAL_CHAR, /* $ */
4, /* % */
5, /* & */
6, /* ' */
7, /* ( */
8, /* ) */
9, /* * */
10, /* + */
11, /* , */
12, /* - */
13, /* . */
14, /* / */
15, /* 0 */
16, /* 1 */
17, /* 2 */
18, /* 3 */
19, /* 4 */
20, /* 5 */
21, /* 6 */
22, /* 7 */
23, /* 8 */
24, /* 9 */
25, /* : */
26, /* ; */
27, /* < */
28, /* = */
29, /* > */
30, /* ? */
31, /* @ */
32, /* A */
33, /* B */
34, /* C */
35, /* D */
36, /* E */
37, /* F */
38, /* G */
39, /* H */
40, /* I */
41, /* J */
42, /* K */
43, /* L */
44, /* M */
45, /* N */
46, /* O */
47, /* P */
48, /* Q */
49, /* R */
50, /* S */
51, /* T */
52, /* U */
53, /* V */
54, /* W */
55, /* X */
56, /* Y */
57, /* Z */
A85_ILLEGAL_CHAR, /* [ */
A85_ILLEGAL_CHAR, /* \ */
A85_ILLEGAL_CHAR, /* ] */
61, /* ^ */
62, /* _ */
63, /* ` */
64, /* a */
65, /* b */
66, /* c */
67, /* d */
68, /* e */
69, /* f */
70, /* g */
71, /* h */
72, /* i */
73, /* j */
74, /* k */
75, /* l */
76, /* m */
77, /* n */
78, /* o */
79, /* p */
80, /* q */
81, /* r */
82, /* s */
83, /* t */
84, /* u */
1, /* v (replaces ") " */
3, /* w (replaces $) */
58, /* x (replaces [) */
59, /* y (replaces \) */
A85_Z, /* z */
A85_ILLEGAL_CHAR, /* { */
60, /* | (replaces ]) */
A85_ILLEGAL_CHAR, /* } */
A85_ILLEGAL_CHAR, /* ~ */
};
/*
* These Tcl_ObjType pointers are initialized the first time that the package
* is loaded; we do it this way because the actual object types are not
* exported by the TCL DLL, and therefore if we use the address of the
* standard types we get an undefined symbol at link time.
*/
static const Tcl_ObjType* cmpTclProProcBodyType = 0;
static const Tcl_ObjType* cmpByteCodeType = 0;
static const Tcl_ObjType* cmpDoubleType = 0;
static const Tcl_ObjType* cmpIntType = 0;
/*
* Same thing for AuxDataTypes.
*/
static const AuxDataType* cmpJumptableInfoType = 0;
static const AuxDataType* cmpDictUpdateInfoType = 0;
static const AuxDataType* cmpNewForeachInfoType = 0;
static int didLoadTypes = 0;
/*
* error message to generate when we run into the end of the buffer and we
* are expecting more stuff
*/
static char prematureEnd[] = "bytecode terminated prematurely";
/*
* Compatibility layer declarations and static storage follow.
*/
static int compatibilityLayerInit = 0;
/*
* This procedure is used internally to look up symbols in the current
* executable, to initialize the compatibility layer properly
*/
extern void* TbcloadGetSymbolAddress(const char* symbolName);
/*
* The factory for procbody objects.
*/
typedef Tcl_Obj*(ProcBodyFactory)(Proc* procPtr);
static ProcBodyFactory* procBodyFactory = 0;
/*
* The cleanup proc for procbody objects.
*/
typedef void(ProcBodyCleanup)(Proc* procPtr);
static ProcBodyCleanup* procBodyCleanup = 0;
/*
*
* The real tbcload::bcproc command implementation, Tcl_ProcObjCmd
*
*/
static Tcl_ObjCmdProc* bcprocCmdProc = NULL;
/*
* Prototypes for procedures defined later in this file:
*/
static int A85DecodeByte(Tcl_Interp* interp, int code, A85DecodeContext* ctxPtr);
static void A85InitDecodeContext(Tcl_Size numBytes, unsigned char* decodeBuf, A85DecodeContext* ctxPtr);
static int AllocAndExtractByteSequence(
Tcl_Interp* interp, ExtractionEnv* envPtr, int addNull, unsigned char** seqPtrPtr, Tcl_Size* seqSizePtr);
static int AllocAndExtractString(Tcl_Interp* interp, ExtractionEnv* envPtr, int addNull, char** strPtrPtr, Tcl_Size* strSizePtr);
static void AppendErrorLocation(Tcl_Interp* interp, ExtractionEnv* envPtr);
static int CheckSignature(Tcl_Interp* interp, ImageSignature* signaturePtr);
static void CleanupByteCode(ExtractionEnv* envPtr);
static void CleanupExtractEnv(ExtractionEnv* envPtr);
static Tcl_Obj* CreateSimpleObject(ExtractionEnv* envPtr);
static int ExcRangeFromName(Tcl_Size name, ExceptionRangeType* typePtr);
static int ExtractAuxDataArray(
Tcl_Interp* interp, Tcl_Size numAuxDataItems, ExtractionEnv* envPtr, AuxData* auxDataArray, Tcl_Size auxDataArraySize);
static int ExtractByteCode(Tcl_Interp* interp, ExtractionEnv* envPtr);
static int
ExtractByteSequence(Tcl_Interp* interp, Tcl_Size length, ExtractionEnv* envPtr, unsigned char* seqPtr, Tcl_Size seqSize);
static Tcl_Obj* ExtractCompiledFile(Tcl_Interp* interp, char* codePtr, Tcl_Size codeLength);
static CompiledLocal* ExtractCompiledLocal(Tcl_Interp* interp, ExtractionEnv* envPtr);
static int ExtractDictUpdateInfo(Tcl_Interp* interp, ExtractionEnv* envPtr, AuxData* auxDataPtr);
static int ExtractExcRangeArray(
Tcl_Interp* interp, Tcl_Size numExceptRanges, ExtractionEnv* envPtr, ExceptionRange* excArray, Tcl_Size excArraySize);
static int ExtractTclSize(Tcl_Interp* interp, ExtractionEnv* envPtr, Tcl_Size* valuePtr);
static int ExtractJumptableInfo(Tcl_Interp* interp, ExtractionEnv* envPtr, AuxData* auxDataPtr);
static int ExtractNewForeachInfo(Tcl_Interp* interp, ExtractionEnv* envPtr, AuxData* auxDataPtr);
static int
ExtractObjArray(Tcl_Interp* interp, Tcl_Size numLitObjects, ExtractionEnv* envPtr, Tcl_Obj** objArray, Tcl_Size objArraySize);
static Tcl_Obj* ExtractObject(Tcl_Interp* interp, ExtractionEnv* envPtr);
static Tcl_Obj* ExtractProcBody(Tcl_Interp* interp, ByteCode* codePtr, ExtractionEnv* envPtr);
static char* ExtractSignature(Tcl_Interp* interp, char* codePtr, char* codeEnd, ImageSignature* signaturePtr);
static int ExtractString(Tcl_Interp* interp, Tcl_Size length, ExtractionEnv* envPtr, char* strPtr, Tcl_Size strSize);
static char* FindEnd(char* first, char* last);
static int InitByteCode(Tcl_Interp* interp, ExtractionEnv* envPtr);
static int InitCompatibilityLayer(Tcl_Interp* interp);
static void InitExtractEnv(char* codeBase, char* codeEnd, ExtractionEnv* envPtr);
static void InitTypes();
/*
*----------------------------------------------------------------------
*
* Tbcload_EvalObjCmd --
*
* Eval the Tcl_Obj given as its first argument. If this Tcl_Obj contains
* the string representation of a compiled script, it builds a ByteCode
* from it, and executes it.
*
* Results:
* Returns a standard TCL result code.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int Tbcload_EvalObjCmd(void* dummy, Tcl_Interp* interp, int objc, Tcl_Obj* const objv[])
{
ImageSignature sig;
Tcl_Obj* cmdObjPtr;
char* scriptPtr;
Tcl_Size scriptLength;
int result;
if (objc < 2)
{
Tcl_WrongNumArgs(interp, 1, objv, "bytestream");
return TCL_ERROR;
}
/*
* Check the signature. If OK, then extract the whole file.
* If the script is empty, return TCL_OK here.
*/
scriptPtr = Tcl_GetStringFromObj(objv[1], &scriptLength);
if ((scriptLength < 1) || (scriptPtr == NULL))
{
return TCL_OK;
}
if ((ExtractSignature(interp, (char*)scriptPtr, (char*)(scriptPtr + scriptLength), &sig) == NULL) ||
(CheckSignature(interp, &sig) != TCL_OK))
{
return TCL_ERROR;
}
cmdObjPtr = ExtractCompiledFile(interp, scriptPtr, scriptLength);
if (cmdObjPtr == NULL)
{
return TCL_ERROR;
}
result = Tcl_EvalObjEx(interp, cmdObjPtr, 0);
Tcl_DecrRefCount(cmdObjPtr);
#if USE_CATCH_WRAPPER
if (result == TCL_ERROR)
{
Tcl_AppendObjToErrorInfo(interp, Tcl_NewStringObj("\n", -1));
Tcl_AppendObjToErrorInfo(interp, Tcl_NewStringObj(errorInfoMarker, -1));
}
#endif
return result;
}
/*
*----------------------------------------------------------------------
*
* Tbcload_ProcObjCmd --
*
* Wrapper procedure, dispatches the call to the appropriate implementation.
*
* Results:
* A standard Tcl object result value.
*
* Side effects:
* A new procedure gets created.
*
*----------------------------------------------------------------------
*/
int Tbcload_ProcObjCmd(void* dummy, Tcl_Interp* interp, int objc, Tcl_Obj* const objv[])
{
return (*bcprocCmdProc)(dummy, interp, objc, objv);
}
/*
*----------------------------------------------------------------------
*
* ExtractByteCode --
*
* Extracts the contents of a ByteCode structure from a compiled image.
*
* Results:
* Returns TCL_OK on success, TCL_ERROR on failure.
*
* Side effects:
* Populates the ByteCode with the value extracted from the file.
* Updates the contents of the ExtractionEnv structure: stores the new
* address of the next available byte.
* May also modify the interpreter's result in case of failure.
*
*----------------------------------------------------------------------
*/
static int ExtractByteCode(Tcl_Interp* interp, ExtractionEnv* envPtr)
{
ByteCode* codePtr;
LocMapSizes* locMapPtr = &envPtr->locMap;
if (InitByteCode(interp, envPtr) != TCL_OK)
{
goto error;
}
codePtr = envPtr->codePtr;
if ((ExtractByteSequence(interp, codePtr->numCodeBytes, envPtr, codePtr->codeStart, codePtr->numCodeBytes) != TCL_OK) ||
(ExtractByteSequence(interp, locMapPtr->codeDeltaSize, envPtr, codePtr->codeDeltaStart, locMapPtr->codeDeltaSize) !=
TCL_OK) ||
(ExtractByteSequence(interp, locMapPtr->codeLengthSize, envPtr, codePtr->codeLengthStart, locMapPtr->codeLengthSize) !=
TCL_OK))
{
goto error;
}
if ((locMapPtr->srcDeltaSize >= 0) &&
(ExtractByteSequence(interp, locMapPtr->srcDeltaSize, envPtr, codePtr->srcDeltaStart, locMapPtr->srcDeltaSize) != TCL_OK))
{
goto error;
}
if ((locMapPtr->srcLengthSize >= 0) &&
(ExtractByteSequence(interp, locMapPtr->srcLengthSize, envPtr, codePtr->srcLengthStart, locMapPtr->srcLengthSize) !=
TCL_OK))
{
goto error;
}
if ((ExtractObjArray(interp, codePtr->numLitObjects, envPtr, codePtr->objArrayPtr, codePtr->numLitObjects) != TCL_OK) ||
(ExtractExcRangeArray(interp, codePtr->numExceptRanges, envPtr, codePtr->exceptArrayPtr, codePtr->numExceptRanges) !=
TCL_OK) ||
(ExtractAuxDataArray(interp, codePtr->numAuxDataItems, envPtr, codePtr->auxDataArrayPtr, codePtr->numAuxDataItems) !=
TCL_OK))
{
goto error;
}
/*
* If the source map arrays were not included in the .tbc file (which is
* typically the case), generate them here; each command will start at 0
* and have length noSourceCodeSize
*/
if (locMapPtr->srcDeltaSize < 0)
{
memset(codePtr->srcDeltaStart, 0, (size_t)codePtr->numCommands);
}
if (locMapPtr->srcLengthSize < 0)
{
memset(codePtr->srcLengthStart, (char)noSourceCodeSize, (size_t)codePtr->numCommands);
}
return TCL_OK;
error:
CleanupByteCode(envPtr);
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* InitByteCode --
*
* Allocates and initializes a ByteCode structure using the values stored
* in a compiled image.
*
* Results:
* Returns TCL_OK on success, TCL_ERROR on failure.
*
* Side effects:
* Populates parts of the ByteCode with the value extracted from the file;
* also sets it up so that CleanupByteCode can clean it up even if the
* structure was only partially built.
* Updates the contents of the ExtractionEnv structure: stores the new
* address of the next available byte and the pointer to the newly allocated
* ByteCode struct.
* May also modify the interpreter's result in case of failure.
*
*----------------------------------------------------------------------
*/
static int InitByteCode(Tcl_Interp* interp, ExtractionEnv* envPtr)
{
Interp* iPtr = (Interp*)interp;
ByteCode* byteCodePtr;
unsigned char* p;
Tcl_Size size;
Tcl_Size numCommands, numSrcBytes, numCodeBytes, numLitObjects, numExceptRanges;
Tcl_Size numAuxDataItems, numCmdLocBytes, maxExceptDepth, maxStackDepth;
Tcl_Size objArrayBytes, exceptArrayBytes, auxDataArrayBytes;
LocMapSizes* locMapPtr = &envPtr->locMap;
Namespace* namespacePtr;
CleanupByteCode(envPtr);
/*
* Determine the size of the ByteCode struct, malloc it, then initialize
* the components that were not saved with the file image because it
* doesn't make sense to do so: pointer to the interpreter, epoch, etc...
* To determine the size of the ByteCode struct, we read in the .tbc
* control line, which has enough enformation to calculate the required
* size.
*/
if ((ExtractTclSize(interp, envPtr, &numCommands) != TCL_OK) || (ExtractTclSize(interp, envPtr, &numSrcBytes) != TCL_OK) ||
(ExtractTclSize(interp, envPtr, &numCodeBytes) != TCL_OK) || (ExtractTclSize(interp, envPtr, &numLitObjects) != TCL_OK) ||
(ExtractTclSize(interp, envPtr, &numExceptRanges) != TCL_OK) ||
(ExtractTclSize(interp, envPtr, &numAuxDataItems) != TCL_OK) ||
(ExtractTclSize(interp, envPtr, &numCmdLocBytes) != TCL_OK) ||
(ExtractTclSize(interp, envPtr, &maxExceptDepth) != TCL_OK) ||
(ExtractTclSize(interp, envPtr, &maxStackDepth) != TCL_OK) ||
(ExtractTclSize(interp, envPtr, &(locMapPtr->codeDeltaSize)) != TCL_OK) ||
(ExtractTclSize(interp, envPtr, &(locMapPtr->codeLengthSize)) != TCL_OK) ||
(ExtractTclSize(interp, envPtr, &(locMapPtr->srcDeltaSize)) != TCL_OK) ||
(ExtractTclSize(interp, envPtr, &(locMapPtr->srcLengthSize)) != TCL_OK))
{
return TCL_ERROR;
}
objArrayBytes = (numLitObjects * sizeof(Tcl_Obj*));
exceptArrayBytes = (numExceptRanges * sizeof(ExceptionRange));
auxDataArrayBytes = (numAuxDataItems * sizeof(AuxData));
/*
* generate the numCmdLocBytes from the values of the location and
* source arrays, which could be different here from the original,
* because the .tbc file was written without sources
*/
numCmdLocBytes = locMapPtr->codeDeltaSize + locMapPtr->codeLengthSize;
if (locMapPtr->srcDeltaSize < 0)
{
/*
* The source arrays have as many entries as the number of commands,
* because both start (0 for all) and delta (noSourceCodeSize for all)
* values fit in one byte
*/
numCmdLocBytes += numCommands;
}
else
{
numCmdLocBytes += locMapPtr->srcDeltaSize;
}
if (locMapPtr->srcLengthSize < 0)
{
numCmdLocBytes += numCommands;
}
else
{
numCmdLocBytes += locMapPtr->srcLengthSize;
}
size = sizeof(ByteCode);
size += TCL_ALIGN(numCodeBytes); /* align object array */
size += TCL_ALIGN(objArrayBytes); /* align exception range array */
size += TCL_ALIGN(exceptArrayBytes); /* align AuxData array */
size += auxDataArrayBytes;
size += numCmdLocBytes;
/*
* initialize the reference count on the ByteCode to 1 because we have a
* reference to it in the extraction environment struct.
*/
if (iPtr->varFramePtr != NULL)
{
namespacePtr = iPtr->varFramePtr->nsPtr;
}
else
{
namespacePtr = iPtr->globalNsPtr;
}
p = (unsigned char*)Tcl_Alloc((size_t)size);
byteCodePtr = (ByteCode*)p;
memset(byteCodePtr, 0, (size_t)size);
byteCodePtr->interpHandle = TclHandlePreserve(iPtr->handle);
byteCodePtr->compileEpoch = iPtr->compileEpoch;
byteCodePtr->nsPtr = namespacePtr;
byteCodePtr->nsEpoch = namespacePtr->resolverEpoch;
byteCodePtr->refCount = 1;
byteCodePtr->flags = TCL_BYTECODE_PRECOMPILED;
byteCodePtr->procPtr = NULL;
envPtr->codeSize = size;
envPtr->codePtr = byteCodePtr;
byteCodePtr->structureSize = 0;
byteCodePtr->numCommands = numCommands;
byteCodePtr->numSrcBytes = numSrcBytes;
byteCodePtr->numCodeBytes = numCodeBytes;
byteCodePtr->numLitObjects = numLitObjects;
byteCodePtr->numExceptRanges = numExceptRanges;
byteCodePtr->numAuxDataItems = numAuxDataItems;
byteCodePtr->numCmdLocBytes = numCmdLocBytes;
byteCodePtr->maxExceptDepth = maxExceptDepth;
byteCodePtr->maxStackDepth = maxStackDepth;
byteCodePtr->source = noSourceCode;
byteCodePtr->numSrcBytes = noSourceCodeSize;
/*
* The assignements to p must be kept consistent with the ones in
* TclInitByteCodeObj, so that the arrays are aligned as expected.
*/
p += sizeof(ByteCode);
byteCodePtr->codeStart = p;
memset(p, 0, (size_t)numCodeBytes);
p += TCL_ALIGN(numCodeBytes);
if (numLitObjects > 0)
{
byteCodePtr->objArrayPtr = (Tcl_Obj**)p;
memset(p, 0, (size_t)objArrayBytes);
}
else
{
byteCodePtr->objArrayPtr = (Tcl_Obj**)0;
}
p += TCL_ALIGN(objArrayBytes);
if (numExceptRanges > 0)
{
byteCodePtr->exceptArrayPtr = (ExceptionRange*)p;
memset(p, 0, (size_t)exceptArrayBytes);
}
else
{
byteCodePtr->exceptArrayPtr = (ExceptionRange*)0;
}
p += TCL_ALIGN(exceptArrayBytes);
if (numAuxDataItems > 0)
{
byteCodePtr->auxDataArrayPtr = (AuxData*)p;
memset(p, 0, (size_t)auxDataArrayBytes);
}
else
{
byteCodePtr->auxDataArrayPtr = (AuxData*)0;
}
p += auxDataArrayBytes;
byteCodePtr->codeDeltaStart = p;
p += locMapPtr->codeDeltaSize;
byteCodePtr->codeLengthStart = p;
p += locMapPtr->codeLengthSize;
byteCodePtr->srcDeltaStart = p;
if (locMapPtr->srcDeltaSize < 0)
{
p += byteCodePtr->numCommands;
}
else
{
p += locMapPtr->srcDeltaSize;
}
byteCodePtr->srcLengthStart = p;
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* CleanupByteCode --
*
* Cleans up and frees the ByteCode structure in the ExtractionEnv.
* Generally this will only be called in the case when an error
* occurs extracting the ByteCode, while the ByteCodes will generally
* get cleaned up by tclCompile.c:TclCleanupByteCode, which has a
* special case for precompiled flagged ByteCode structures.
*
* Results:
* None.
*
* Side effects:
* Frees a number of arrays and TCL objects that may have been allocated
* while the ByteCode was assembled from the compiled image..
*
*----------------------------------------------------------------------
*/
static void CleanupByteCode(ExtractionEnv* envPtr)
{
ByteCode* byteCodePtr = envPtr->codePtr;
if (byteCodePtr)
{
byteCodePtr->refCount--;
if (byteCodePtr->refCount < 1)
{
if (byteCodePtr->numLitObjects > 0)
{
Tcl_Obj** objArrayPtr = byteCodePtr->objArrayPtr;
Tcl_Obj* objPtr;
Tcl_Size i;
for (i = 0; i < byteCodePtr->numLitObjects; i++)
{
objPtr = *objArrayPtr;
objArrayPtr += 1;
if (objPtr)
{
Tcl_DecrRefCount(objPtr);
}
}
}
Tcl_Free((char*)byteCodePtr);
}
envPtr->codePtr = NULL;
}
}
/*
*----------------------------------------------------------------------
*
* ExtractTclSize --
*
* Extracts a Tcl_Size value from a compiled image. Skips whitespace, then gets
* the value from the next word. If successful, places the value in the location
* pointed to by valuePtr.
*
* Results:
* Returns TCL_OK on success, TCL_ERROR on failure.
*
* Side effects:
* Loads the Tcl_Size at valuePtr with the value extracted from the file.
* Updates the contents of the ExtractionEnv structure: stores the new
* address of the next available byte.
* May also modify the interpreter's result in case of failure.
*
*----------------------------------------------------------------------
*/
static int ExtractTclSize(Tcl_Interp* interp, ExtractionEnv* envPtr, Tcl_Size* valuePtr)
{
const char* codePtr = envPtr->curImagePtr;
const char* codeEnd = envPtr->imageEnd;
/* Skip leading whitespace */
while (codePtr < codeEnd && isspace(UCHAR(*codePtr)))
codePtr++;
if (codePtr >= codeEnd)
{
Tcl_SetObjResult(interp, Tcl_NewStringObj(prematureEnd, -1));
return TCL_ERROR;
}
/* Find token end without mutating the buffer */
const char* endPtr = FindEnd((char*)codePtr, (char*)codeEnd);
/* Slice token into a Tcl_Obj and parse as Tcl_Size */
Tcl_Obj* tok = Tcl_NewStringObj(codePtr, (Tcl_Size)(endPtr - codePtr));
if (Tcl_GetSizeIntFromObj(interp, tok, valuePtr) != TCL_OK)
{
AppendErrorLocation(interp, envPtr);
return TCL_ERROR;
}
/* Advance scanner */
envPtr->curImagePtr = (char*)endPtr;
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* ExtractByteSequence --
*
* Extracts a byte sequence from the compiled image, place it into the array
* at seqPtr. The length argument can be specified as -1, in which case the
* procedure extracts the length from the byte sequence header, or as a
* number that specifies the expected length of the sequence. In the latter
* case, the length obtained from the byte sequence header is compared to
* the value passed in, and an error is returned if the two don't match.
* The seqSize argument specifies the size of the seqPtr array.
* The byte sequence is assumed to have been encoded with a modified
* ASCII85 filter, as described in cmpWrite.c.
*
* Results:
* Returns TCL_OK on success, TCL_ERROR on failure.
*
* Side effects:
* Loads the byte sequence into the array starting at seqPtr.
* Updates the contents of the ExtractionEnv structure: stores the new
* address of the next available byte.
* May also modify the interpreter's result in case of failure.
*
*----------------------------------------------------------------------
*/
static int
ExtractByteSequence(Tcl_Interp* interp, Tcl_Size length, ExtractionEnv* envPtr, unsigned char* seqPtr, Tcl_Size seqSize)
{
char* imagePtr;
char* imageEnd;
Tcl_Size hLen;
int code;
A85DecodeContext decodeCtx;
A85DecodeContext* ctxPtr = &decodeCtx;
/*
* read the length from the header; we need to do this even in cases where
* the length is passed in.
* Then do the length checks if necessary.
*/
if (ExtractTclSize(interp, envPtr, &hLen) != TCL_OK)
{
return TCL_ERROR;
}
if (length < 0)
{
length = hLen;
}
else if (length != hLen)
{
{
Tcl_Obj* res = Tcl_GetObjResult(interp);
Tcl_AppendToObj(res, "inconsistent byte sequence length", -1);
}
AppendErrorLocation(interp, envPtr);
return TCL_ERROR;
}
if (length > seqSize)
{
{
Tcl_Obj* res = Tcl_GetObjResult(interp);
Tcl_AppendToObj(res, "byte sequence too big for storage", -1);
}
AppendErrorLocation(interp, envPtr);
return TCL_ERROR;
}
imagePtr = envPtr->curImagePtr;
imageEnd = envPtr->imageEnd;
A85InitDecodeContext(length, seqPtr, ctxPtr);
while (decodeCtx.bytesToDecode > 0)
{
if (imagePtr == imageEnd)
{
envPtr->curImagePtr = imagePtr;
{
Tcl_Obj* res = Tcl_GetObjResult(interp);
Tcl_AppendToObj(res, prematureEnd, -1);
}
return TCL_ERROR;
}
code = decodeMap[(int)*imagePtr];
imagePtr += 1;
if (code == A85_ILLEGAL_CHAR)
{
envPtr->curImagePtr = imagePtr - 1;
{
Tcl_Obj* res = Tcl_GetObjResult(interp);
Tcl_AppendToObj(res, "malformed byte sequence", -1);
}
AppendErrorLocation(interp, envPtr);
return TCL_ERROR;
}
else if (code != A85_WHITESPACE)
{
if (A85DecodeByte(interp, code, ctxPtr) != TCL_OK)
{
envPtr->curImagePtr = imagePtr - 1;
AppendErrorLocation(interp, envPtr);
return TCL_ERROR;
}
}
}
envPtr->curImagePtr = imagePtr;
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* ExtractString --
*
* Extracts a string from the compiled image, place it into the array
* at strPtr. The length argument must be specified, because strings are
* written out without count fields.
*
* Results:
* Returns TCL_OK on success, TCL_ERROR on failure.
*
* Side effects:
* Loads the string into the array starting at strPtr.
* Updates the contents of the ExtractionEnv structure: stores the new
* address of the next available byte.
* May also modify the interpreter's result in case of failure.
*
*----------------------------------------------------------------------
*/
static int ExtractString(Tcl_Interp* interp, Tcl_Size length, ExtractionEnv* envPtr, char* strPtr, Tcl_Size strSize)
{
char* imagePtr;
imagePtr = envPtr->curImagePtr;
if ((imagePtr + strSize) > envPtr->imageEnd)
{
{
Tcl_Obj* res = Tcl_GetObjResult(interp);
Tcl_AppendToObj(res, prematureEnd, -1);
}
return TCL_ERROR;
}
memcpy(strPtr, imagePtr, (size_t)strSize);
envPtr->curImagePtr += strSize;
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* AllocAndExtractByteSequence --
*
* Get the size of the byte sequence from the compiled image, alloc a buffer
* large enough for it, then extract it. Places the allocated pointer in