-
Notifications
You must be signed in to change notification settings - Fork 3
/
cleaner.c
1454 lines (1205 loc) · 53 KB
/
cleaner.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
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#define VERSION "1.1"
#define DEBUG 1
#define DEBUG_VERBOSE 0
#define MAX_TYPES 256
#define MAX_FUNCS 256 /* this includes imports! */
uint64_t leb(
uint8_t** buf,
uint8_t* bufend,
int is_signed)
{
uint64_t val = 0, shift = 0, i = 0;
while (*buf + i < bufend)
{
uint64_t b = (uint64_t)((*buf)[i]);
uint64_t last = val;
val += (b & 0x7FU) << shift;
if (val < last)
{
fprintf(stderr, "LEB128 overflow in input wasm at offset %ld.\n",
i);
exit(100);
}
++i;
if (b & 0x80U)
{
shift += 7;
continue;
}
*buf += i;
if (is_signed && shift < 64 && (b & 0x40U))
val |= (~0 << shift);
return val;
}
return 0;
}
void leb_out(
uint64_t i,
uint8_t** o)
{
do
{
uint8_t b = i & 0x7FU;
i >>= 7U;
if (i != 0)
b |= 0x80;
**o = b;
(*o)++;
} while (i != 0);
}
void leb_out_pad(
uint64_t i,
uint8_t** o,
int padto)
{
fprintf(stderr, "Leb_out_pad(i=%ld, pad=%d): [", i, padto);
padto--;
do
{
uint8_t b = i & 0x7FU;
i >>= 7U;
if (i != 0 || padto > 0)
b |= 0x80;
**o = b;
(*o)++;
fprintf(stderr, " 0x%02X", b);
padto--;
} while (i > 0 || padto >= 0);
fprintf(stderr, " ]\n");
}
int cleaner (
uint8_t* w, // web assembly input buffer
uint8_t* o, // web assembly output buffer
ssize_t* len) // length of input buffer when called, and len of output buffer when returned
{
// require at least `need` bytes
#define REQUIRE(need)\
{\
if (DEBUG && DEBUG_VERBOSE)\
fprintf(stderr, "Require %ld b\tfrom 0x%lX to 0x%lX\n",\
((uint64_t)(need)),\
((uint64_t)(w-wstart)),\
((uint64_t)(w+need-wstart)));\
if (wlen - (w - wstart) < need)\
return \
fprintf(stderr,\
"Truncated web assembly input. SrcLine: %d. Illegally short at position %ld [0x%lx].\n"\
"wlen: %ld w-wstart: %ld need:%ld\n"\
"%08lX : %02X %02X %02X %02X\n",\
__LINE__,\
((uint64_t)(w - wstart)),\
((uint64_t)(w - wstart)),\
((uint64_t)(wlen)),\
((uint64_t)(w-wstart)),\
((uint64_t)(need)),\
((uint64_t)((w - wstart) - 4)),\
*(w-4), *(w-3), *(w-2), *(w-1));\
}
// advance `adv` bytes
#define ADVANCE(adv)\
{\
if (DEBUG && DEBUG_VERBOSE)\
fprintf(stderr, "Advance %ld b\tfrom 0x%lX to 0x%lX\n",\
((uint64_t)(adv)),\
((uint64_t)(w-wstart)),\
((uint64_t)(w+adv-wstart)));\
w += adv;\
REQUIRE(0);\
}
#define LEB()\
(tmp2=w-wstart,tmp=leb(&w, wend, 0),\
(DEBUG && DEBUG_VERBOSE &&\
fprintf(stderr, "Leb read at 0x%lX: %ld\n", tmp2, tmp)),tmp)
#define SIGNED_LEB()\
(tmp2=w-wstart,tmp=leb(&w, wend, 1),\
(DEBUG && DEBUG_VERBOSE &&\
fprintf(stderr, "Signed Leb read at 0x%lX: %ld\n", tmp2, tmp)),tmp)
uint8_t* wstart = w; // remember start of buffer
ssize_t wlen = *len;
uint8_t* wend = w + wlen;
uint64_t tmp, tmp2;
// read magic number
REQUIRE(4);
//00 61 73 6D
if (w[0] != 0x00U || w[1] != 0x61U || w[2] != 0x73U || w[3] != 0x6DU)
return fprintf(stderr, "Magic number missing or invalid %02X=%d %02X=%d %02X=%d %02X=%d\n",
w[0], w[0] == 0x00U, w[1], w[1] == 0x61U, w[2], w[2] == 0x73U, w[3], w[3] == 0x6DU);
ADVANCE(4);
// read version
REQUIRE(4)
if (w[0] != 0x01U || w[1] || w[2] || w[3])
return fprintf(stderr, "Only version 1.00 of WASM standard is supported\n");
ADVANCE(4);
// first section loop
if (DEBUG)
fprintf(stderr, "First pass start\n");
int func_hook = -1;
int func_cbak = -1;
int mem_export = -1; // RH UPTO: find out what memory is exported and carry it over (do we need this??)
int out_import_count = -1; // the number of imports there will be in the output file
ssize_t out_import_size = 0; // the size ofthe import section in the output
ssize_t out_code_size = 0;
int func_count = -1;
int hook_cbak_type = -1;
int func_type[MAX_FUNCS]; // each function we discover import/func has its type id recorded here
struct {
uint8_t set;
uint8_t rc;
uint8_t r[30];
uint8_t pc;
uint8_t p[31];
} types[MAX_TYPES];
for (int x = 0; x < MAX_TYPES; ++x)
{
func_type[x] = -1;
types[x].set = 0;
}
int guard_func_idx = -1;
uint8_t* next_section_start = 0;
while (w < wend)
{
if (next_section_start && w != next_section_start)
{
return fprintf(stderr, "Internal sanity check failed. w = %ld, next_section_start = %ld\n",
w - wstart, next_section_start - wstart);
}
REQUIRE(1);
uint8_t section_type = w[0];
ADVANCE(1);
uint64_t section_len = LEB();
if (DEBUG)
fprintf(stderr, "Section type: %d, Section len: %ld, Section offset: 0x%lX\n",
section_type, section_len, w - wstart);
REQUIRE(section_len);
next_section_start = w + section_len;
switch (section_type)
{
case 0x01U: // types
{
int type_count = LEB();
for (int i = 0; i < type_count; ++i)
{
REQUIRE(1);
if (w[0] != 0x60U)
return fprintf(stderr, "Illegal func type didn't start with 0x60U at %lX\n",
(w - wstart));
ADVANCE(1);
int param_count = LEB();
types[i].pc = param_count;
int is_P32 = 0;
for (int j = 0; j < param_count; ++j)
{
int param_type = LEB();
types[i].p[j] = param_type;
if (param_type == 0x7FU && param_count == 1)
is_P32 = 1;
}
int result_count = LEB();
types[i].set = 1;
types[i].rc = result_count;
if (result_count == 1)
for (int j = 0; j < result_count; ++j)
{
int result_type = LEB();
types[i].r[j] = result_type;
if (result_type == 0x7EU && result_count == 1 && is_P32)
{
if (DEBUG)
fprintf(stderr, "Hook/Cbak type: %d\n", i);
if (hook_cbak_type != -1)
return fprintf(stderr, "int64_t func(int32_t) appears in type section twice!\n");
hook_cbak_type = i;
}
}
}
continue;
}
case 0x02U: // imports
{
// just get an import count
int count = LEB();
if (DEBUG)
fprintf(stderr, "Import count: %d\n", count);
int func_upto = 0;
for (int i = 0; i < count; ++i)
{
uint8_t* import_start = w;
// module name
int mod_length = LEB();
REQUIRE(mod_length);
if (mod_length != 3 || w[0] != 'e' || w[1] != 'n' || w[2] != 'v')
return fprintf(stderr, "Did not import only from module 'env'\n");
ADVANCE(mod_length);
// import name
int name_length = LEB();
REQUIRE(name_length);
if (name_length == 2 && w[0] == '_' && w[1] == 'g')
{
guard_func_idx = func_upto;
fprintf(stderr, "Guard function found at index: %d\n", guard_func_idx);
}
ADVANCE(name_length);
REQUIRE(1);
uint8_t import_type = w[0];
ADVANCE(1);
// only function imports
if (import_type != 0x00U)
{
if (guard_func_idx == i)
return fprintf(stderr, "Guard import _g was not imported as a function!\n");
if (import_type == 0x01U)
{
// table type
REQUIRE(1);
ADVANCE(1);
int dualLimit = (*w == 0x00U);
ADVANCE(1);
LEB();
if (dualLimit)
LEB();
}
else if (import_type == 0x02U)
{
// mem type
int dualLimit = (*w == 0x00U);
LEB();
if (dualLimit)
LEB();
}
else if (import_type == 0x03U)
{
REQUIRE(2);
ADVANCE(2);
}
}
else
{
uint64_t import_idx = LEB();
func_type[func_upto++] = import_idx;
out_import_size += (w - import_start);
if (DEBUG)
fprintf(stderr, "Import %d type %ld out_import_size = %ld\n",
func_upto, import_idx, out_import_size);
}
}
out_import_count = func_upto;
if (out_import_count > 127*127)
return fprintf(stderr, "Unsupported number of imports: %d\n", out_import_count);
out_import_size += (out_import_count <= 127 ? 1U : 2U);
continue;
}
case 0x03U: // funcs
{
func_count = LEB();
if (DEBUG)
fprintf(stderr, "Function count: %d\n", func_count);
for (int i = 0; i < func_count; ++i)
{
func_type[out_import_count + i] = LEB();
if (DEBUG)
fprintf(stderr, "Func %d is type %d\n",
out_import_count + i, func_type[out_import_count + i]);
}
continue;
}
case 0x07U: // exports
{
uint8_t* export_end = w + section_len;
uint64_t export_count = LEB();
for (uint64_t i = 0; i < export_count; ++i)
{
// we only care about two exports: hook and cbak
// since we have to parse name first we'll read it in passing
// and store info about it here
int status = 0; // 1 = hook() 2 = cbak(), 0 = irrelevant
// read export name
uint64_t export_name_len = LEB();
REQUIRE(export_name_len);
if (export_name_len == 4)
{
if (w[0] == 'h' && w[1] == 'o' && w[2] == 'o' && w[3] == 'k')
status = 1;
else
if (w[0] == 'c' && w[1] == 'b' && w[2] == 'a' && w[3] == 'k')
status = 2;
}
ADVANCE(export_name_len);
// export type
REQUIRE(1);
uint8_t export_type = w[0];
ADVANCE(1);
// export idx
uint64_t export_idx = LEB();
if (status == 1)
func_hook = export_idx;
else if (status == 2)
func_cbak = export_idx;
if (func_hook > -1 && func_cbak > -1)
break;
}
// hook() is required at minimum
if (func_hook < 0)
return fprintf(stderr, "Could not find hook() export in wasm input\n");
w = export_end;
continue;
}
case 0x0AU:
{
uint64_t code_count = LEB();
for (uint64_t i = 0; i < code_count; ++i)
{
uint8_t* code_start = w;
uint64_t code_size = LEB();
ADVANCE(code_size);
if (i == (func_hook - out_import_count) || i == (func_cbak - out_import_count))
out_code_size += (w - code_start);
}
continue;
}
default:
{
ADVANCE(section_len);
continue;
}
}
}
if (hook_cbak_type == -1)
return fprintf(stderr, "Hook/cbak has the wrong function signature. Must be int64_t (*) (uint32_t).\n");
fprintf(stderr, "hook idx: %d, cbak idx: %d\n", func_hook, func_cbak);
if (guard_func_idx == -1)
return fprintf(stderr, "Guard function _g was not imported / missing.\n");
// reset to top
w = wstart;
// pass two: write out
if (DEBUG)
fprintf(stderr, "Second pass start\n");
uint8_t* ostart = o;
// magic number and version: 8 bytes
for (int i = 0; i < 8; ++i)
*o++ = *w++;
int type_new[MAX_TYPES];
memset(type_new, 0, sizeof(type_new));
next_section_start = 0;
while (w < wend)
{
if (next_section_start && w != next_section_start)
{
return fprintf(stderr, "Internal sanity check failed. w = %ld, next_section_start = %ld\n",
w - wstart, next_section_start - wstart);
}
REQUIRE(1);
uint8_t section_type = w[0];
ADVANCE(1);
uint64_t section_len = LEB();
if (DEBUG)
fprintf(stderr, "Source section type: %d, Section len: %ld, Section offset: 0x%lX\n",
section_type, section_len, w - wstart);
REQUIRE(section_len);
next_section_start = w + section_len;
switch (section_type)
{
case 0x04U: // tables
case 0x08U: // start
case 0x09U: // elements
case 0x00U: // custom section
{
// these sections are dropped
ADVANCE(section_len);
continue;
}
case 0x05U: // memory
case 0x0BU: // data section
case 0x0CU: // data count section
{
// copied as is
*o++ = section_type;
leb_out(section_len, &o);
memcpy(o, w, section_len);
o += section_len;
ADVANCE(section_len);
continue;
}
case 0x01U: // type section
{
ADVANCE(section_len);
*o++ = 0x01U; // write section type
uint8_t used[MAX_TYPES];
memset(used, 0, MAX_TYPES);
// count types
int type_count = 0;
int imports_use_hook_cbak_type = 0;
int section_size = 0;
for (int i = 0; i < out_import_count; ++i)
{
if (!used[func_type[i]])
{
type_count++;
used[func_type[i]] = 1;
section_size += 3U + types[func_type[i]].rc + types[func_type[i]].pc;
if (func_type[i] == hook_cbak_type && !imports_use_hook_cbak_type)
{
imports_use_hook_cbak_type = 1;
hook_cbak_type = type_count-1;
if (DEBUG)
fprintf(stderr, "Imports DO use hook_cbak_type = %d\n", hook_cbak_type);
}
}
}
if (!imports_use_hook_cbak_type)
{
hook_cbak_type = type_count++;
section_size += 5U;
if (DEBUG)
fprintf(stderr, "Imports do not use hook_cbak_type = %d\n", hook_cbak_type);
}
if (type_count > 127*127)
return fprintf(stderr, "Too many types in wasm!\n");
// account for the type vector size bytes
section_size += (type_count > 127 ? 2U : 1U);
if (DEBUG)
fprintf(stderr, "Writing type section, proposed size: %d\n", section_size);
// write out section size
leb_out(section_size, &o);
uint8_t* out_start = o;
// write type vector len
leb_out(type_count, &o);
// write out types
memset(used, 0, MAX_TYPES);
int upto = 0;
for (int i = 0; i < out_import_count; ++i)
{
int t = func_type[i];
if (!types[t].set)
return fprintf(stderr, "Tried to write unset type %d from func %d\n", func_type[i], i);
if (used[t])
continue;
used[t] = 1;
*o++ = 0x60U; // functype lead in byte
// write parameter count
leb_out(types[t].pc, &o);
// write each parameter type
for (int j = 0; j < types[t].pc; ++j)
leb_out(types[t].p[j], &o);
leb_out(types[t].rc, &o);
for (int j = 0; j < types[t].rc; ++j)
leb_out(types[t].r[j], &o);
type_new[t] = upto++;
// done for this record
}
// write out cbak/hook type if needed
if (!imports_use_hook_cbak_type)
{
*o++ = 0x60U;
*o++ = 0x01U;
*o++ = 0x7FU;
*o++ = 0x01U;
*o++ = 0x7EU;
}
if (DEBUG)
fprintf(stderr, "Actually written type section size: %ld\n", o - out_start);
continue;
}
case 0x02U: // imports
{
*o++ = 0x02U;
if (DEBUG)
{
fprintf(stderr, "Writing import section, proposed size, count: %ld, %d\n",
out_import_size, out_import_count);
}
leb_out(out_import_size, &o);
uint8_t* import_start = o;
leb_out(out_import_count, &o);
int type_count = 0;
int count = LEB();
for (int i = 0; i < count; ++i)
{
// module name
int mod_length = LEB();
REQUIRE(mod_length);
uint8_t* mod = w;
ADVANCE(mod_length);
// import name
int name_length = LEB();
REQUIRE(name_length);
uint8_t* name = w;
ADVANCE(name_length);
// only function imports
if (*w != 0x00U)
{
int it = *w;
ADVANCE(1);
if (it == 0x01U)
{
// table type
REQUIRE(1);
ADVANCE(1);
int dualLimit = (*w == 0x00U);
ADVANCE(1);
LEB();
if (dualLimit)
LEB();
}
else if (it == 0x02U)
{
// mem type
int dualLimit = (*w == 0x00U);
LEB();
if (dualLimit)
LEB();
}
else if (it == 0x03U)
{
REQUIRE(2);
ADVANCE(2);
}
continue;
}
ADVANCE(1);
// write mod
leb_out(mod_length, &o);
memcpy(o, mod, mod_length);
o += mod_length;
// write name
leb_out(name_length, &o);
memcpy(o, name, name_length);
o += name_length;
// write import type (always 0)
*o++ = 0x00U;
if (DEBUG)
fprintf(stderr, "New import: %d old type: %d new type: %d\n", i, func_type[i], type_new[func_type[i]]);
// write new type idx
leb_out(type_new[func_type[i]], &o);
LEB(); // discard old type
// advance to next entry
}
if (DEBUG)
fprintf(stderr, "Actually written import size: %ld\n", o - import_start);
continue;
}
case 0x03U: // functions
{
*o++ = 0x03U;
ssize_t s = (func_cbak == -1 ? 0x01U : 0x02U);
if (hook_cbak_type > 127U*127U)
return fprintf(stderr, "Illegally large hook_cbak type index\n");
if (hook_cbak_type > 127U)
s <<= 1U; // double size if > 127
s++; // one byte for the vector size
if (DEBUG)
fprintf(stderr, "Writing function section, proposed size: %ld\n", s);
leb_out(s, &o); // sections size
uint8_t* function_start = o;
*o++ = (func_cbak == -1 ? 0x01U : 0x02U); // vector size
leb_out(hook_cbak_type, &o); // vector entries
if (func_cbak != -1)
{
leb_out(hook_cbak_type, &o);
if (DEBUG)
fprintf(stderr, "Writing cbak [idx=%d, type=%d]\n", func_cbak, hook_cbak_type);
}
ADVANCE(section_len);
if (DEBUG)
fprintf(stderr, "Actually written function size: %ld\n", o - function_start);
continue;
}
case 0x06U: // globals
{
// globals are copied byte for byte
*o++ = 0x06U;
leb_out(section_len, &o);
memcpy(o, w, section_len);
o += section_len;
ADVANCE(section_len);
continue;
}
case 0x07U: // exports
{
*o++ = 0x07U;
// size
// V M NNNN 0 1 [ M NNNN 0 2 ]
*o++ = (func_cbak == -1 ? 0x08U : 0x0FU);
// vec len
*o++ = (func_cbak == -1 ? 0x01U : 0x02U);
int cbak_first = (func_cbak < func_hook);
if (cbak_first && func_cbak != -1)
{
*o++ = 0x04U;
*o++ = 'c'; *o++ = 'b'; *o++ = 'a'; *o++ = 'k';
*o++ = 0x00U;
leb_out(out_import_count + 0, &o);
*o++ = 0x04U;
*o++ = 'h'; *o++ = 'o'; *o++ = 'o'; *o++ = 'k';
*o++ = 0x00U;
leb_out(out_import_count + 1, &o);
}
else
{
*o++ = 0x04U;
*o++ = 'h'; *o++ = 'o'; *o++ = 'o'; *o++ = 'k';
*o++ = 0x00U;
leb_out(out_import_count + 0, &o);
if (func_cbak != -1)
{
*o++ = 0x04U;
*o++ = 'c'; *o++ = 'b'; *o++ = 'a'; *o++ = 'k';
*o++ = 0x00U;
leb_out(out_import_count + 1, &o);
}
}
ADVANCE(section_len);
continue;
}
case 0x0AU: // code section (aka function body)
{
*o++ = 0x0AU;
if (DEBUG)
fprintf(stderr, "Output code size: %ld\n", out_code_size + 1);
// RH NOTE:
// In addition to moving a properly formed clean guard of the form { i32.const, i32.const, _g, drop }
// we can also reinterpret a badly formed guard like { i32.con, i32.store, ..., i32.con, _g, drop }.
// This becomes what's known as a guard rewrite. In this case additional instructions beyond the
// original size of the hook will be inserted at the start of the relevant loop. This counter tracks
// these, and the reserved space allows us to output the right LEB128 at the end.
int total_guard_rewrite_bytes = 0;
uint8_t* codesec_out_size_ptr = o;
// we need to correct this at the end
leb_out_pad(out_code_size + 1 /* allow for vec len */, &o, 3);
*o++ = (func_cbak == -1 ? 0x01U : 0x02U); // vec len
uint64_t count = LEB();
for (uint64_t i = 0; i < count; ++i)
{
uint8_t* code_start = w;
uint64_t code_size = LEB();
if (i == (func_hook - out_import_count) || i == (func_cbak - out_import_count))
{
//leb_out(code_size, &o);
int guard_rewrite_bytes = 0;
uint8_t* code_size_ptr = o;
// we need to correct this at the end
leb_out_pad(code_size, &o, 3);
int pad_len = 3 - (w-code_start);
if (pad_len < 0)
return fprintf(stderr,
"Codesec %ld was too large! Size must fit in 3 leb128 bytes!\n", i);
total_guard_rewrite_bytes += pad_len;
//memcpy(o, code_start, w-code_start);
// parse locals
uint8_t* locals_start = w;
uint64_t locals_count = LEB();
fprintf(stderr, "Locals count: %ld\n", locals_count);
for (int i = 0; i < locals_count; ++i)
{
LEB(); // inner len
REQUIRE(1); // local type
ADVANCE(1);
}
memcpy(o, locals_start, w-locals_start);
o += (w-locals_start);
uint8_t* expr_start = w;
uint64_t expr_size = code_size - (w-locals_start);
fprintf(stderr, "Expr start: %ld [0x%lx]\n", expr_size, expr_size);
// parse code
uint8_t* last_loop = 0; // where the start of the last loop instruction is in the input
uint8_t* last_loop_out = 0; // where the start of the last loop instruction is in the output
int i32_found = 0;
uint8_t* call_guard_found = 0;
uint8_t* last_i32 = 0;
uint64_t last_i32_actual = 0; // the actual leb value
uint8_t* second_last_i32 = 0;
uint64_t second_last_i32_actual = 0; // the actual leb value
int between_const_and_guard = 0;
#define RESET_GUARD_FINDER()\
{\
i32_found = 0;\
call_guard_found = 0;\
last_i32 = 0;\
last_i32_actual = 0;\
second_last_i32 = 0;\
second_last_i32_actual = 0;\
between_const_and_guard = 0;\
}
while (w - expr_start < expr_size)
{
uint8_t* instr_start = w;
REQUIRE(1);
uint8_t ins = *w;
ADVANCE(1);
if (ins == 0x02U || ins == 0x03U || ins == 0x04U) // block, loop, if
{
REQUIRE(1);
uint8_t block_type = *w;
if ((block_type >= 0x7CU && block_type <= 0x7FU) ||
block_type == 0x7BU || block_type == 0x70U ||
block_type == 0x7BU || block_type == 0x40U)
{
ADVANCE(1);
}
else
SIGNED_LEB();
memcpy(o, instr_start, w-instr_start);
o += (w - instr_start);
if (ins == 0x03U) // loop
{
last_loop = w;
last_loop_out = o;
}
RESET_GUARD_FINDER();
continue;
}
if (ins == 0x1AU) // drop
{
REQUIRE(1);
*o++ = ins;
if (i32_found >= 2 && call_guard_found && last_loop)
{
if (between_const_and_guard > 0)
{
if (second_last_i32_actual < last_i32_actual)
{
uint64_t swap = last_i32_actual;
last_i32_actual = second_last_i32_actual;
second_last_i32_actual = swap;
}
uint8_t guard_code[128];
uint8_t* g = guard_code;
*g++ = 0x41U;
leb_out(second_last_i32_actual, &g);
*g++ = 0x41U;
leb_out(last_i32_actual, &g);
*g++ = 0x10U;
leb_out(guard_func_idx, &g);
*g++ = 0x1AU;
ssize_t guard_len = g - guard_code;
ssize_t rest_len = w - last_loop;
char guard_print[128]; guard_print[0] = '\0';
snprintf(guard_print, 128, "_g(0x%08lx,%ld)", second_last_i32_actual,
last_i32_actual);
int guard_pad_len = 20 - strlen(guard_print);
if (guard_pad_len < 0) guard_pad_len = 0;
snprintf(guard_print, 128, "_g(0x%08lx,%.*s%ld)",
second_last_i32_actual,
guard_pad_len,
" ",
last_i32_actual);
fprintf(stderr, "Found dirty guard %s\tat: %ld [0x%lx] - %ld [0x%lx],\t"
"rewriting to %ld [0x%lx] - %ld [0x%lx]\n",
guard_print,
second_last_i32 - wstart,
second_last_i32 - wstart,
w - wstart,
w - wstart,
last_loop - wstart,
last_loop - wstart,
last_loop - wstart + guard_len,
last_loop - wstart + guard_len
);
// erase guard call with nops and an additional drop
// to preserve the stack at this location during runtime
int bytes_to_fill = w - call_guard_found - 2;
*call_guard_found = 0x1AU; // drop
while (bytes_to_fill-- > 0)
*(++call_guard_found) = 0x01U; // nop
// first move the instructions down
memcpy(last_loop_out + guard_len, last_loop, rest_len);
// then copy the guard into position
memcpy(last_loop_out, guard_code, guard_len);