forked from ptitSeb/box64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebuild_wrappers.py
executable file
·1324 lines (1169 loc) · 48.6 KB
/
rebuild_wrappers.py
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
#!/usr/bin/env python3
import os
import sys
try:
assert(sys.version_info.major == 3)
if sys.version_info.minor >= 9:
# Python 3.9+
from typing import Generic, NewType, Optional, TypeVar, Union, final
from collections.abc import Iterable
Dict = dict
List = list
Tuple = tuple
elif sys.version_info.minor >= 8:
# Python [3.8, 3.9)
from typing import Dict, List, Tuple, Generic, Iterable, NewType, Optional, TypeVar, Union, final
elif (sys.version_info.minor >= 5) and (sys.version_info.micro >= 2):
# Python [3.5.2, 3.8)
from typing import Dict, List, Tuple, Generic, Iterable, NewType, Optional, TypeVar, Union
final = lambda fun: fun # type: ignore
elif sys.version_info.minor >= 5:
# Python [3.5, 3.5.2)
from typing import Dict, List, Tuple, Generic, Iterable, Optional, TypeVar, Union
class GTDummy:
def __getitem__(self, _): return self
final = lambda fun: fun # type: ignore
def NewType(_, b): return b # type: ignore
else:
# Python < 3.5
#print("Your Python version does not have the typing module, fallback to empty 'types'")
# Dummies
class GTDummy:
def __getitem__(self, _):
return self
Dict = GTDummy() # type: ignore
List = GTDummy() # type: ignore
Generic = GTDummy() # type: ignore
Iterable = GTDummy() # type: ignore
Optional = GTDummy() # type: ignore
def NewType(_, b): return b # type: ignore
Tuple = GTDummy() # type: ignore
def TypeVar(T): return object # type: ignore
Union = GTDummy() # type: ignore
except ImportError:
print("It seems your Python version is quite broken...")
assert(False)
"""
Generates all files in src/wrapped/generated
===
TL;DR: Automagically creates type definitions (/.F.+/ functions/typedefs...).
All '//%' in the headers are used by the script.
Reads each lines of each "_private.h" headers.
For each of them:
- If if starts with a #ifdef, #else, #ifndef, #endif, it memorizes which definition is required
- If it starts with a "GO", it will do multiple things:
- It memorizes the type used by the function (second macro argument)
- It memorizes the type it is mapped to, if needed (eg, iFEvp is mapped to iFEp: the first "real" argument is dropped)
- It checks if the type given (both original and mapped to) are valid
- If the signature contains a 'E' but it is not a "GOM" command, it will throw an error
- If the line starts with a '//%S', it will memorize a structure declaration.
The structure of it is: "//%S <letter> <structure name> <signature equivalent>"
NOTE: Those structure letters are "fake types" that are accepted in the macros.
After sorting the data, it generates:
wrapper.c
---------
(Private) type definitions (/.F.+_t/)
Function definitions (/.F.+/ functions, that actually execute the function given as argument)
isSimpleWrapper definition
wrapper.h
---------
Generic "wrapper_t" type definition
Function declarations (/.F.+/ functions)
*types.h
--------
Local types definition, for the original signatures
The SUPER() macro definition, used to generate and initialize the `*_my_t` library structure
(TODO: also automate this declaration/definition? It would require more metadata,
and may break sometime in the future due to the system changing...)
*defs.h
-------
Local `#define`s, for signature mapping
*undefs.h
---------
Local `#undefine`s, for signature mapping
Example:
========
In wrappedtest_private.h:
----------------------
//%S X TestLibStructure ppu
GO(superfunction, pFX)
GOM(superFunction2, pFpX)
Generated files:
wrapper.c: [snippet]
----------
typedef void *(*pFppu_t)(void*, void*, uint32_t);
typedef void *(*pFpppu_t)(void*, void*, void*, uint32_t);
void pFppu(x64emu_t *emu, uintptr_t fcn) { pFppu_t *fn = (pFppu_t)fn; R_RAX=...; }
void pFpppu(x64emu_t *emu, uintptr_t fcn) { pFpppu_t *fn = (pFpppu_t)fn; R_RAX=...; }
int isSimpleWrapper(wrapper_t fun) {
if (fcn == pFppu) return 1;
if (fcn == pFpppu) return 1;
return 0;
}
wrapper.h: [snippet]
----------
void pFppu(x64emu_t *emu, uintptr_t fcn);
void pFpppu(x64emu_t *emu, uintptr_t fcn);
int isSimpleWrapper(wrapper_t fun);
wrappedtesttypes.h:
-------------------
typedef void *(*pFpX_t)(void*, TestLibStructure);
#define SUPER() \\
GO(superFunction2, pFpX)
wrappedtestdefs.h:
------------------
#define pFX pFppu
#define pFpX pFpppu
wrappedtestundefs.h:
--------------------
#undef pFX
#undef pFpX
"""
# TODO: Add /.F.*A/ automatic generation (and suppression)
# Free letters: B FG J QR T XYZab e gh jk mno qrst xyz
class FunctionType(str):
values: List[str] = ['E', 'v', 'c', 'w', 'i', 'I', 'C', 'W', 'u', 'U', 'f', 'd', 'D', 'K', 'l', 'L', 'p', 'V', 'O', 'S', 'N', 'M', 'H', 'P', 'A']
@staticmethod
def validate(s: str, post: str) -> bool:
if len(s) < 3:
raise NotImplementedError("Type {0} too short{1}".format(s, post))
chk_type = s[0] + s[2:]
if "E" in s:
if ("E" in s[:2]) or ("E" in s[3:]):
raise NotImplementedError(
"emu64_t* not as the first parameter{0}".format(post))
if len(s) < 4:
raise NotImplementedError("Type {0} too short{1}".format(s, post))
# TODO: change *FEv into true functions (right now they are redirected to *FE)
#chk_type = s[0] + s[3:]
if s[1] not in ["F"]:
raise NotImplementedError("Bad middle letter {0}{1}".format(s[1], post))
return all(c in FunctionType.values for c in chk_type) and (('v' not in chk_type[1:]) or (len(chk_type) == 2))
def splitchar(self) -> List[int]:
"""
splitchar -- Sorting key function for function signatures
The longest strings are at the end, and for identical length, the string
are sorted using a pseudo-lexicographic sort, where characters have a value
of `values.index`.
"""
try:
ret = [len(self), FunctionType.values.index(self[0])]
for c in self[2:]:
ret.append(FunctionType.values.index(c))
return ret
except ValueError as e:
raise ValueError("Value is " + self) from e
def __getitem__(self, i: Union[int, slice]) -> 'FunctionType':
return FunctionType(super().__getitem__(i))
assert(all(c not in FunctionType.values[:i] for i, c in enumerate(FunctionType.values)))
RedirectType = NewType('RedirectType', FunctionType)
DefineType = NewType('DefineType', str)
StructType = NewType('StructType', str)
T = TypeVar('T')
U = TypeVar('U')
# TODO: simplify construction of this (add an 'insert' method?...)
class CustOrderedDict(Generic[T, U], Iterable[T]):
def __init__(self, dict: Dict[T, U], keys: List[T]):
self.__indict__ = dict
self.__inkeys__ = keys
def __iter__(self):
return iter(self.__inkeys__)
def __getitem__(self, k: T) -> U:
return self.__indict__[k]
Filename = str
ClausesStr = str
@final
class Define:
name: DefineType
inverted_: bool
defines: List[DefineType] = []
def __init__(self, name: str, inverted_: bool):
# All values of "name" are included in defines (throw otherwise)
if DefineType(name) not in self.defines:
raise KeyError(name)
self.name = DefineType(name)
self.inverted_ = inverted_
def copy(self) -> "Define":
return Define(self.name, self.inverted_)
def value(self) -> int:
return self.defines.index(self.name)*2 + (1 if self.inverted_ else 0)
def invert(self) -> None:
"""
invert -- Transform a `defined()` into a `!defined()` and vice-versa, in place.
"""
self.inverted_ = not self.inverted_
def inverted(self) -> "Define":
"""
inverted -- Transform a `defined()` into a `!defined()` and vice-versa, out-of-place.
"""
return Define(self.name, not self.inverted_)
def __str__(self) -> str:
if self.inverted_:
return "!defined(" + self.name + ")"
else:
return "defined(" + self.name + ")"
@final
class Clause:
defines: List[Define]
def __init__(self, defines: Union[List[Define], str] = []):
if isinstance(defines, str):
if defines == "":
self.defines = []
else:
self.defines = list(
map(
lambda x:
Define(x[9:-1] if x[0] == '!' else x[8:-1], x[0] == '!')
, defines.split(" && ")
)
)
else:
self.defines = [d.copy() for d in defines]
def copy(self) -> "Clause":
return Clause(self.defines)
def append(self, define: Define) -> None:
self.defines.append(define)
def invert_last(self) -> None:
self.defines[-1].invert()
def pop_last(self) -> None:
if len(self.defines) > 0: self.defines.pop()
def __str__(self) -> str:
return " && ".join(map(str, self.defines))
@final
class Clauses:
"""
Represent a list of clauses, aka a list of or-ed together and-ed "defined"
conditions
"""
definess: List[Clause]
def __init__(self, definess: Union[List[Clause], str] = []):
if isinstance(definess, str):
if definess == "()":
self.definess = []
elif ") || (" in definess:
self.definess = list(map(Clause, definess[1:-1].split(") || (")))
else:
self.definess = [Clause(definess)]
else:
self.definess = definess[:]
def copy(self) -> "Clauses":
return Clauses(self.definess[:])
def add(self, defines: Clause) -> None:
self.definess.append(defines)
def splitdef(self) -> List[int]:
"""
splitdef -- Sorting key function for #ifdefs
All #if defined(...) are sorted first by the number of clauses, then by the
number of '&&' in each clause and then by the "key" of the tested names
(left to right, inverted placed after non-inverted).
"""
ret = [len(self.definess)]
for cunj in self.definess:
ret.append(len(cunj.defines))
for cunj in self.definess:
for d in cunj.defines:
ret.append(d.value())
return ret
def __str__(self) -> ClausesStr:
if len(self.definess) == 1:
return str(self.definess[0])
else:
return "(" + ") || (".join(map(str, self.definess)) + ")"
JumbledGlobals = Dict[ClausesStr, List[FunctionType]]
JumbledRedirects = Dict[ClausesStr, Dict[RedirectType, FunctionType]]
JumbledTypedefs = Dict[RedirectType, List[str]]
JumbledStructures = Dict[str, Tuple[StructType, str]]
JumbledStructUses = Dict[RedirectType, FunctionType]
JumbledFilesSpecific = Dict[Filename, Tuple[JumbledTypedefs, JumbledStructures, JumbledStructUses]]
SortedGlobals = CustOrderedDict[ClausesStr, List[FunctionType]]
SortedRedirects = CustOrderedDict[ClausesStr, List[Tuple[RedirectType, FunctionType]]]
SortedTypedefs = CustOrderedDict[RedirectType, List[str]]
SortedStructUses = CustOrderedDict[RedirectType, FunctionType]
SortedFilesSpecific = Dict[Filename, Tuple[SortedTypedefs, SortedStructUses]]
def readFiles(files: Iterable[Filename]) -> Tuple[JumbledGlobals, JumbledRedirects, JumbledFilesSpecific]:
"""
readFiles
This function is the one that parses the files.
It returns the jumbled (gbl, redirects, {file: (typedefs, mystructs)}) tuple.
"""
# Initialize variables: gbl for all values, redirects for redirections
gbl : JumbledGlobals = {}
redirects: JumbledRedirects = {}
filespec : JumbledFilesSpecific = {}
functions: Dict[str, Filename] = {}
halt_required = False # Is there a GO(*, .FE*) or similar in-batch error(s)?
# First read the files inside the headers
for filepath in files:
filename: Filename = filepath.split("/")[-1]
dependants: Clause = Clause()
# typedefs is a list of all "*FE*" types for the current file
# mystructs is a map of all char -> (structure C name, replacement) for structures
typedefs : JumbledTypedefs = {}
mystructs : JumbledStructures = {}
mystructuses: JumbledStructUses = {}
filespec[filename[:-10]] = (typedefs, mystructs, mystructuses)
def add_symbol_name(funname: Union[str, None], funsname: Dict[ClausesStr, List[str]] = {"": []}):
# Optional arguments are evaluated only once!
nonlocal halt_required
if funname is None:
for k in funsname:
if (k != "") and (len(funsname[k]) != 0):
# Note: if this condition ever raises, check the wrapper pointed by it.
# If you find no problem, comment the error below, add a "pass" line (so python is happy)
# and open a ticket so I can fix this.
raise NotImplementedError("Some functions are only implemented under one condition (probably) ({0}:{1})"
.format(k, filename) + " [extra note in the script]")
for f in funsname[k]:
if f in ['_fini', '_init', '__bss_start', '__data_start', '_edata', '_end']:
continue # Always allow those symbols [TODO: check if OK]
if f in functions:
# Check for resemblances between functions[f] and filename
if filename.startswith(functions[f][:-12]) or functions[f].startswith(filename[:-12]):
# Probably OK
continue
# Manual compatible libs detection
match = lambda l, r: (filename[7:-10], functions[f][7:-10]) in [(l, r), (r, l)]
if match("sdl1image", "sdl2image") \
or match("sdl1mixer", "sdl2mixer") \
or match("sdl1net", "sdl2net") \
or match("sdl1ttf", "sdl2ttf") \
or match("libGL", "libEGL") \
or match("libc", "tcmallocminimal"):
continue
# Note: this test is very (too) simple. If it ever raises, comment
# `halt_required = True` and open an issue.
print("The function or data {0} is declared in multiple files ({1}/{2})"
.format(f, functions[f], filename) + " [extra note in the script]")
halt_required = True
functions[f] = filename
else:
if funname == "":
raise NotImplementedError("This function name (\"\") is suspicious... ({0})".format(filename))
l = len(dependants.defines)
already_pst = funname in funsname[""]
if l > 1:
return
elif l == 1:
funsname.setdefault(str(dependants), [])
already_pst = already_pst or (funname in funsname[str(dependants)])
if already_pst:
print("Function or data {0} is duplicated! ({1})".format(funname, filename))
halt_required = True
return
if l == 1:
s = str(dependants.defines[0].inverted())
if (s in funsname) and (funname in funsname[s]):
funsname[s].remove(funname)
funsname[""].append(funname)
else:
funsname[str(dependants)].append(funname)
else:
funsname[""].append(funname)
with open(filepath, 'r') as file:
line: str # Because VSCode really struggles with files
for line in file:
ln = line.strip()
# If the line is a `#' line (#ifdef LD80BITS/#ifndef LD80BITS/header)
if ln.startswith("#"):
preproc_cmd = ln[1:].strip()
try:
if preproc_cmd.startswith("if defined(GO)"):
continue #if defined(GO) && defined(GOM)...
elif preproc_cmd.startswith("if !(defined(GO)"):
continue #if !(defined(GO) && defined(GOM)...)
elif preproc_cmd.startswith("error"):
continue #error meh!
elif preproc_cmd.startswith("endif"):
dependants.pop_last()
elif preproc_cmd.startswith("ifdef"):
dependants.append(Define(preproc_cmd[5:].strip(), False))
elif preproc_cmd.startswith("ifndef"):
dependants.append(Define(preproc_cmd[6:].strip(), True))
elif preproc_cmd.startswith("else"):
dependants.invert_last()
else:
raise NotImplementedError("Unknown preprocessor directive: {0} ({1}:{2})".format(
preproc_cmd.split(" ")[0], filename, line[:-1]
))
except KeyError as k:
raise NotImplementedError("Unknown key: {0} ({1}:{2})".format(
k.args[0], filename, line[:-1]
)) from k
# If the line is a `GO...' line (GO/GOM/GO2/...)...
elif ln.startswith("GO"):
# ... then look at the second parameter of the line
try:
gotype = ln.split("(")[0].strip()
funname = ln.split(",")[0].split("(")[1].strip()
ln = ln.split(",")[1].split(")")[0].strip()
add_symbol_name(funname)
except IndexError:
raise NotImplementedError("Invalid GO command: {0}:{1}".format(
filename, line[:-1]
))
hasFlatStructure = False
origLine = ln
if not FunctionType.validate(ln, " ({0}:{1})".format(filename, line[:-1])):
if (ln[0] in FunctionType.values) \
and ('v' not in ln[2:]) \
and all((c in FunctionType.values) or (c in mystructs) for c in ln[2:]):
hasFlatStructure = True
for sn in mystructs:
ln = ln.replace(sn, mystructs[sn][1])
mystructuses[RedirectType(FunctionType(origLine))] = FunctionType(ln)
else:
# This needs more work
old = RedirectType(FunctionType(ln))
acceptables = ['0', '1'] + FunctionType.values
if any(c not in acceptables for c in ln[2:]):
raise NotImplementedError("{0} ({1}:{2})".format(ln[2:], filename, line[:-1]))
# Ok, this is acceptable: there is 0, 1 and/or void
ln = ln[:2] + (ln[2:]
.replace("v", "") # void -> nothing
.replace("0", "i") # 0 -> integer
.replace("1", "i")) # 1 -> integer
assert(len(ln) >= 3)
redirects.setdefault(str(dependants), {})
redirects[str(dependants)][old] = FunctionType(ln)
origLine = ln
# Simply append the function type if it's not yet existing
gbl.setdefault(str(dependants), [])
if ln not in gbl[str(dependants)]:
gbl[str(dependants)].append(FunctionType(ln))
if origLine[2] == "E":
if (gotype != "GOM") and (gotype != "GOWM"):
if (gotype != "GO2") or not (line.split(',')[2].split(')')[0].strip().startswith('my_')):
print("\033[91mThis is probably not what you meant!\033[m ({0}:{1})".format(filename, line[:-1]))
halt_required = True
if len(origLine) > 3:
funtype = RedirectType(FunctionType(origLine[:2] + origLine[3:]))
else:
funtype = RedirectType(FunctionType(origLine[:2] + "v"))
# filename isn't stored with the '_private.h' part
typedefs.setdefault(funtype, [])
typedefs[funtype].append(funname)
elif (gotype == "GOM") or (gotype == "GOWM"):
# OK on box64 for a GOM to not have emu...
funtype = RedirectType(FunctionType(origLine))
typedefs.setdefault(funtype, [])
typedefs[funtype].append(funname)
# print("\033[94mAre you sure of this?\033[m ({0}:{1})".format(filename, line[:-1]))
# halt_required = True
elif hasFlatStructure:
# Still put the type in typedefs, but don't add the function name
typedefs.setdefault(RedirectType(FunctionType(origLine)), [])
# If the line is a structure metadata information...
elif ln.startswith("//%S"):
metadata = [e for e in ln.split() if e]
if len(metadata) != 4:
raise NotImplementedError("Invalid structure metadata supply (too many fields) ({0}:{1})".format(filename, line[:-1]))
if metadata[0] != "//%S":
raise NotImplementedError("Invalid structure metadata supply (invalid signature) ({0}:{1})".format(filename, line[:-1]))
if len(metadata[1]) != 1:
# If you REALLY need it, consider opening a ticket
# Before you do, consider that everything that is a valid in a C token is valid here too
raise NotImplementedError("Structure names cannot be of length greater than 1 ({0}:{1})".format(filename, line[:-1]))
if metadata[3] == "":
# If you need this, please open an issue (this is never actually called, empty strings are removed)
raise NotImplementedError("Invalid structure metadata supply (empty replacement) ({0}:{1})".format(filename, line[:-1]))
if any(c not in FunctionType.values for c in metadata[3]):
# Note that replacement cannot be another structure type
raise NotImplementedError("Invalid structure metadata supply (invalid replacement) ({0}:{1})".format(filename, line[:-1]))
if metadata[1] in mystructs:
raise NotImplementedError("Invalid structure nickname {0} (duplicate) ({1}/{2})".format(metadata[1], filename, line[:-1]))
if (metadata[1] in FunctionType.values) or (metadata[1] in ['0', '1']):
raise NotImplementedError("Invalid structure nickname {0} (reserved) ({1}/{2})".format(metadata[1], filename, line[:-1]))
# OK, add into the database
mystructs[metadata[1]] = (StructType(metadata[2]), metadata[3])
# If the line contains any symbol name...
elif ("GO" in ln) or ("DATA" in ln):
# Probably "//GO(..., " or "DATA(...," at least
try:
funname = ln.split('(')[1].split(',')[0].strip()
add_symbol_name(funname)
except IndexError:
# Oops, it wasn't...
pass
add_symbol_name(None)
if halt_required:
raise ValueError("Fix all previous errors before proceeding")
if ("" not in gbl) or ("" not in redirects):
print("\033[1;31mThere is suspiciously not many types...\033[m")
print("Check the CMakeLists.txt file. If you are SURE there is nothing wrong"
" (as a random example, `set()` resets the variable...), then comment out the following exit.")
print("(Also, the program WILL crash later if you proceed.)")
sys.exit(2) # Check what you did, not proceeding
return gbl, redirects, filespec
def sortArrays(gbl_tmp : JumbledGlobals, red_tmp : JumbledRedirects, filespec: JumbledFilesSpecific) \
-> Tuple[SortedGlobals, SortedRedirects, SortedFilesSpecific]:
# Now, take all function types, and make a new table gbl_vals
# This table contains all #if conditions for when a function type needs to
# be generated. There is also a filter to avoid duplicate/opposite clauses.
gbl_vals: Dict[FunctionType, Clauses] = {}
for k1 in gbl_tmp:
ks = Clause(k1)
for v in gbl_tmp[k1]:
if k1 == "":
# Unconditionally define v
gbl_vals[v] = Clauses()
elif v in gbl_vals:
if gbl_vals[v].definess == []:
# v already unconditionally defined
continue
for other_key in gbl_vals[v].definess:
for other_key_val in other_key.defines:
if other_key_val not in ks.defines:
# Not a duplicate or more specific case
# (could be a less specific one though)
break
else:
break
else:
gbl_vals[v].add(ks)
else:
gbl_vals[v] = Clauses([Clause(k1)])
for v in gbl_vals:
strdefines = list(map(str, gbl_vals[v].definess))
for k2 in gbl_vals[v].definess:
for i in range(len(k2.defines)):
if " && ".join(map(str, k2.defines[:i] + [k2.defines[i].inverted()] + k2.defines[i+1:])) in strdefines:
# Opposite clauses detected
gbl_vals[v] = Clauses()
break
else:
continue
break
# Now create a new gbl and gbl_idxs
# gbl will contain the final version of gbl (without duplicates, based on
# gbl_vals)
# gbl_idxs will contain all #if clauses
gbl: Dict[ClausesStr, List[FunctionType]] = {}
gbl_idxs: List[ClausesStr] = []
for k1 in gbl_vals:
clauses = gbl_vals[k1]
key = str(clauses)
gbl.setdefault(key, [])
gbl[key].append(k1)
if key not in gbl_idxs:
gbl_idxs.append(key)
# Sort the #if clauses as defined in `splitdef`
gbl_idxs.sort(key=lambda c: Clauses(c).splitdef())
# This map will contain all additional function types that are "redirected"
# to an already defined type (with some remapping).
redirects_vals: Dict[Tuple[RedirectType, FunctionType], Clauses] = {}
for k1 in red_tmp:
ks = Clause(k1)
for v in red_tmp[k1]:
if k1 == "":
# Unconditionally define v
redirects_vals[(v, red_tmp[k1][v])] = Clauses()
elif (v, red_tmp[k1][v]) in redirects_vals:
if redirects_vals[(v, red_tmp[k1][v])].definess == []:
# v already unconditionally defined
continue
for other_key in redirects_vals[(v, red_tmp[k1][v])].definess:
for other_key_val in other_key.defines:
if other_key_val not in ks.defines:
# Not a duplicate or more specific case
# (could be a less specific one though)
break
else:
break
else:
redirects_vals[(v, red_tmp[k1][v])].add(ks)
else:
redirects_vals[(v, red_tmp[k1][v])] = Clauses([Clause(k1)])
# Also do the same trick as before (it also helps keep the order
# in the file deterministic)
redirects: Dict[ClausesStr, List[Tuple[RedirectType, FunctionType]]] = {}
redirects_idxs: List[ClausesStr] = []
for k1, v in redirects_vals:
clauses = redirects_vals[(k1, v)]
key = str(clauses)
redirects.setdefault(key, [])
redirects[key].append((k1, v))
if key not in redirects_idxs:
redirects_idxs.append(key)
redirects_idxs.sort(key=lambda c: Clauses(c).splitdef())
# Sort the function types as defined in `splitchar`
for k3 in gbl:
gbl[k3].sort(key=FunctionType.splitchar)
FunctionType.values = FunctionType.values + ['0', '1']
for k3 in redirects:
redirects[k3].sort(key=lambda v: v[0].splitchar() + v[1].splitchar())
FunctionType.values = FunctionType.values[:-2]
sortedfilespec: SortedFilesSpecific = {}
for fn in filespec:
# Maybe do better?
mystructs_vals: List[str] = sorted(filespec[fn][1].keys())
if mystructs_vals != []:
FunctionType.values = FunctionType.values + list(mystructs_vals)
mytypedefs_vals: List[RedirectType] = sorted(filespec[fn][0].keys(), key=FunctionType.splitchar)
sortedfilespec[fn] = (
CustOrderedDict(dict((v, sorted(filespec[fn][0][v])) for v in mytypedefs_vals), mytypedefs_vals),
CustOrderedDict(filespec[fn][2], sorted(filespec[fn][2], key=FunctionType.splitchar))
)
if mystructs_vals != []:
FunctionType.values = FunctionType.values[:-len(mystructs_vals)]
return CustOrderedDict(gbl, gbl_idxs), CustOrderedDict(redirects, redirects_idxs), sortedfilespec
def checkRun(root: str, jumbled: JumbledFilesSpecific, \
gbls: SortedGlobals, redirects: SortedRedirects, filesspec: SortedFilesSpecific) -> Optional[str]:
# Check if there was any new functions compared to last run
functions_list: str = ""
for k in gbls:
for v in gbls[k]:
functions_list = functions_list + "#" + k + " " + v + "\n"
for k in redirects:
for vr, vf in redirects[k]:
functions_list = functions_list + "#" + k + " " + vr + " -> " + vf + "\n"
for filename in sorted(filesspec.keys()):
functions_list = functions_list + filename + ":\n"
for st in sorted(jumbled[filename][1].keys()):
functions_list = functions_list + \
"% " + st + " " + jumbled[filename][1][st][0] + " " + jumbled[filename][1][st][1] + "\n"
for vr in filesspec[filename][0]:
functions_list = functions_list + "- " + vr + ":\n"
for fn in filesspec[filename][0][vr]:
functions_list = functions_list + " - " + fn + "\n"
for defined in filesspec[filename][1]:
functions_list = functions_list + "% " + defined + "\n"
# functions_list is a unique string, compare it with the last run
try:
last_run = ""
with open(os.path.join(root, "src", "wrapped", "generated", "functions_list.txt"), 'r') as file:
last_run = file.read()
if last_run == functions_list:
# Mark as OK for CMake
with open(os.path.join(root, "src", "wrapped", "generated", "functions_list.txt"), 'w') as file:
file.write(functions_list)
return None
except IOError:
# The file does not exist yet, first run
pass
return functions_list
def main(root: str, files: Iterable[Filename], ver: str):
"""
main -- The main function
root: the root path (where the CMakeLists.txt is located)
files: a list of files to parse (wrapped*.h)
ver: version number
"""
# gbl_tmp:
# "defined() && ..." -> [vFv, ...]
# red_tmp:
# "defined() && ..." -> [vFEv -> vFv, ...]
# fsp_tmp:
# "filename" -> (
# [vFEv -> fopen, ...],
# [G -> ("SDL_J...", UU), ...],
# [vFGppp -> vFUUppp, ...]
# )
gbl_tmp: JumbledGlobals
red_tmp: JumbledRedirects
fsp_tmp: JumbledFilesSpecific
gbl_tmp, red_tmp, fsp_tmp = readFiles(files)
# gbls: sorted gbl_tmp
# redirects: sorted red_tmp
# filesspec:
# "filename" -> (
# sorted [vFEv -> fopen, ...],
# sorted [vFGppp -> vFUUppp, ...]
# )
gbls : SortedGlobals
redirects: SortedRedirects
filesspec: SortedFilesSpecific
gbls, redirects, filesspec = sortArrays(gbl_tmp, red_tmp, fsp_tmp)
functions_list = checkRun(root, fsp_tmp, gbls, redirects, filesspec)
if functions_list is None:
print("Detected same build as last run, skipping")
return 0
# Detect simple wrappings
simple_wraps: Dict[ClausesStr, List[Tuple[FunctionType, int]]] = {}
# H could be allowed maybe?
allowed_simply: str = "v"
allowed_regs : str = "cCwWiuIUlLp"
allowed_fpr : str = "fd"
# Sanity checks
forbidden_simple: str = "EDKVOSNMHPA"
assert(len(allowed_simply) + len(allowed_regs) + len(allowed_fpr) + len(forbidden_simple) == len(FunctionType.values))
assert(all(c not in allowed_regs for c in allowed_simply))
assert(all(c not in allowed_simply + allowed_regs for c in allowed_fpr))
assert(all(c not in allowed_simply + allowed_regs + allowed_fpr for c in forbidden_simple))
assert(all(c in allowed_simply + allowed_regs + allowed_fpr + forbidden_simple for c in FunctionType.values))
def check_simple(v: FunctionType):
regs_count: int = 0
fpr_count : int = 0
if v[0] in forbidden_simple:
return None
for c in v[2:]:
if c in allowed_regs:
regs_count = regs_count + 1
elif c in allowed_fpr:
fpr_count = fpr_count + 1
elif c in allowed_simply:
continue
else:
return None
# No character in forbidden_simply
if (regs_count <= 6) and (fpr_count <= 8):
# All checks passed!
ret_val = 1 + fpr_count
if v[0] in allowed_fpr:
ret_val = -ret_val
return ret_val
else:
# Too many arguments...
return None
# Only search in real wrappers (mapped ones are nearly always not simple)
for k in gbls:
tmp = [ (v, ret_val) for v, ret_val in map(lambda v: (v, check_simple(v)), gbls[k]) if ret_val is not None ]
if tmp:
simple_wraps[k] = tmp
simple_idxs = sorted(simple_wraps.keys(), key=lambda x: Clauses(x).splitdef())
# Now the files rebuilding part
# File headers and guards
files_header = {
"wrapper.c": """
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "wrapper.h"
#include "emu/x64emu_private.h"
#include "emu/x87emu_private.h"
#include "regs.h"
#include "x64emu.h"
extern void* my__IO_2_1_stdin_ ;
extern void* my__IO_2_1_stdout_;
extern void* my__IO_2_1_stderr_;
static void* io_convert(void* v)
{lbr}
if(!v)
return v;
if(v == my__IO_2_1_stdin_)
return stdin;
if(v == my__IO_2_1_stdout_)
return stdout;
if(v == my__IO_2_1_stderr_)
return stderr;
return v;
{rbr}
#define ST0val ST0.d
int of_convert(int);
""",
"wrapper.h": """
#ifndef __WRAPPER_H_
#define __WRAPPER_H_
#include <stdint.h>
#include <string.h>
typedef struct x64emu_s x64emu_t;
// the generic wrapper pointer functions
typedef void (*wrapper_t)(x64emu_t* emu, uintptr_t fnc);
// list of defined wrapper
// E = current x86emu struct
// v = void
// C = unsigned byte c = char
// W = unsigned short w = short
// u = uint32, i = int32
// U = uint64, I = int64
// L = unsigned long, l = signed long (long is an int with the size of a pointer)
// H = Huge 128bits value/struct
// p = pointer, P = void* on the stack
// f = float, d = double, D = long double, K = fake long double
// V = vaargs
// O = libc O_ flags bitfield
// o = stdout
// S = _IO_2_1_stdXXX_ pointer (or FILE*)
// N = ... automatically sending 1 arg
// M = ... automatically sending 2 args
// A = va_list
// 0 = constant 0, 1 = constant 1
""",
"fntypes.h": """
#ifndef __{filename}TYPES_H_
#define __{filename}TYPES_H_
#ifndef LIBNAME
#error You should only #include this file inside a wrapped*.c file
#endif
#ifndef ADDED_FUNCTIONS
#define ADDED_FUNCTIONS()
#endif
""",
"fndefs.h": """
#ifndef __{filename}DEFS_H_
#define __{filename}DEFS_H_
""",
"fnundefs.h": """
#ifndef __{filename}UNDEFS_H_
#define __{filename}UNDEFS_H_
"""
}
files_guard = {
"wrapper.c": """
""",
"wrapper.h": """
int isSimpleWrapper(wrapper_t fun);
#endif // __WRAPPER_H_
""",
"fntypes.h": """
#endif // __{filename}TYPES_H_
""",
"fndefs.h": """
#endif // __{filename}DEFS_H_
""",
"fnundefs.h": """
#endif // __{filename}UNDEFS_H_
"""
}
banner = "/********************************************************" + ('*'*len(ver)) + "***\n" \
" * File automatically generated by rebuild_wrappers.py (v" + ver + ") *\n" \
" ********************************************************" + ('*'*len(ver)) + "***/\n"
trim = lambda string: '\n'.join(line[2:] for line in string.splitlines())[1:]
# Yes, the for loops are inversed. This is because both dicts should have the same keys.
for fhdr in files_guard:
files_header[fhdr] = banner + trim(files_header[fhdr])
for fhdr in files_header:
files_guard[fhdr] = trim(files_guard[fhdr])
# Rewrite the wrapper.c file:
# i and u should only be 32 bits
# E v c w i I C W u U f d D K l L p V O S N M H P A
td_types = ["x64emu_t*", "void", "int8_t", "int16_t", "int64_t", "int64_t", "uint8_t", "uint16_t", "uint64_t", "uint64_t", "float", "double", "long double", "double", "intptr_t", "uintptr_t", "void*", "void*", "int32_t", "void*", "...", "...", "unsigned __int128", "void*", "void*"]
if len(FunctionType.values) != len(td_types):
raise NotImplementedError("len(values) = {lenval} != len(td_types) = {lentypes}".format(lenval=len(FunctionType.values), lentypes=len(td_types)))
def generate_typedefs(arr: Iterable[FunctionType], file) -> None:
for v in arr:
file.write("typedef " + td_types[FunctionType.values.index(v[0])] + " (*" + v + "_t)"
+ "(" + ', '.join(td_types[FunctionType.values.index(t)] for t in v[2:]) + ");\n")
with open(os.path.join(root, "src", "wrapped", "generated", "wrapper.c"), 'w') as file:
file.write(files_header["wrapper.c"].format(lbr="{", rbr="}", version=ver))
# First part: typedefs
for k in gbls:
if k != str(Clauses()):
file.write("\n#if " + k + "\n")
generate_typedefs(gbls[k], file)
if k != str(Clauses()):
file.write("#endif\n")
file.write("\n")
# Next part: function definitions
# Helper variables
# Return type template
vals = [
"\n#error Invalid return type: emulator\n", # E
"fn({0});", # v
"R_RAX=fn({0});", # c
"R_RAX=fn({0});", # w
"R_RAX=(int64_t)fn({0});", # i should be int32_t
"R_RAX=(int64_t)fn({0});", # I
"R_RAX=(unsigned char)fn({0});", # C
"R_RAX=(unsigned short)fn({0});", # W
"R_RAX=(uint64_t)fn({0});", # u should be uint32_t
"R_RAX=fn({0});", # U
"emu->xmm[0].f[0]=fn({0});", # f
"emu->xmm[0].d[0]=fn({0});", # d
"long double ld=fn({0}); fpu_do_push(emu); ST0val = ld;", # D
"double db=fn({0}); fpu_do_push(emu); ST0val = db;", # K
"R_RAX=(intptr_t)fn({0});", # l