-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.c
2910 lines (2524 loc) · 77.6 KB
/
driver.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
/*
* driver.c: The new mono JIT compiler.
*
* Author:
* Paolo Molaro (lupus@ximian.com)
* Dietmar Maurer (dietmar@ximian.com)
*
* (C) 2002-2003 Ximian, Inc.
* (C) 2003-2006 Novell, Inc.
*/
#include <config.h>
#include <signal.h>
#if HAVE_SCHED_SETAFFINITY
#include <sched.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <mono/metadata/assembly.h>
#include <mono/metadata/loader.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/class.h>
#include <mono/metadata/object.h>
#include <mono/metadata/exception.h>
#include <mono/metadata/opcodes.h>
#include <mono/metadata/mono-endian.h>
#include <mono/metadata/tokentype.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/threads.h>
#include <mono/metadata/marshal.h>
#include <mono/metadata/socket-io.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/io-layer/io-layer.h>
#include "mono/metadata/profiler.h"
#include <mono/metadata/profiler-private.h>
#include <mono/metadata/mono-config.h>
#include <mono/metadata/environment.h>
#include <mono/metadata/verify.h>
#include <mono/metadata/verify-internals.h>
#include <mono/metadata/mono-debug.h>
#include <mono/metadata/security-manager.h>
#include <mono/metadata/security-core-clr.h>
#include <mono/metadata/gc-internal.h>
#include <mono/metadata/coree.h>
#include <mono/metadata/attach.h>
#include "mono/utils/mono-counters.h"
#include <mono/utils/gc_wrapper.h>
#include <stdio.h>
#include <windows.h>
#include <Winbase.h>
#include <string.h>
#include <tchar.h>
#include <TlHelp32.h>
#include <WinDef.h>
//#include <psapi.h>
#include "mini.h"
#include "jit.h"
#include <string.h>
#include <ctype.h>
#include <locale.h>
#include "version.h"
#include "debugger-agent.h"
static FILE *mini_stats_fd = NULL;
DWORD WINAPI mono_test_logger (PVOID pParam);
DWORD WINAPI mono_test_slow_logger (PVOID pParam);
DWORD WINAPI mono_test_entry_checker(PVOID pParam);
void ProcessLogger( DWORD processID );
void WindowsLogger();
void HandlerLogger( DWORD processID );
void ModuleLogger( DWORD processID );
void ThreadLogger( DWORD dwOnwerID );
void HookFunction(const char *pDllName, char* funcName, LPDWORD newFunction, LPDWORD *oldFunction);
LPDWORD FoundIAT(const char *pDllName, char* funcName);
DWORD GetThreadStartAddress(HANDLE hThread);
void ChangeSizeOfImage();
void CheckOutputDebugString(LPCTSTR String);
void Int2DCheck();
static void mini_usage (void);
typedef HMODULE(WINAPI *poldLoadLib)(LPCTSTR dllName);
HMODULE WINAPI HOOKLoadLib(LPCTSTR dllName);
poldLoadLib pLoadLib = NULL;
LPVOID LLaddr = NULL;
LPVOID LLaddrA = NULL;
LPVOID LLaddrW = NULL;
LPVOID LLaddrEx=NULL;
LPVOID LLaddrExA=NULL;
LPVOID LLaddrExW=NULL;
LPVOID NTLLaddr = NULL;
#define STATUS_INFO_LENGTH_MISMATCH 0xc0000004
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
#define ThreadQuerySetWin32StartAddress 9
#define SystemHandleInformation 16
#define ObjectBasicInformation 0
#define ObjectNameInformation 1
#define ObjectTypeInformation 2
typedef NTSTATUS(NTAPI *_NtQuerySystemInformation)(
ULONG SystemInformationClass,
PVOID SystemInformation,
ULONG SystemInformationLength,
PULONG ReturnLength
);
typedef NTSTATUS(NTAPI *_NtDuplicateObject)(
HANDLE SourceProcessHandle,
HANDLE SourceHandle,
HANDLE TargetProcessHandle,
PHANDLE TargetHandle,
ACCESS_MASK DesiredAccess,
ULONG Attributes,
ULONG Options
);
typedef NTSTATUS(NTAPI *_NtQueryObject)(
HANDLE ObjectHandle,
ULONG ObjectInformationClass,
PVOID ObjectInformation,
ULONG ObjectInformationLength,
PULONG ReturnLength
);
typedef struct _SYSTEM_HANDLE
{
ULONG ProcessId;
BYTE ObjectTypeNumber;
BYTE Flags;
USHORT Handle;
PVOID Object;
ACCESS_MASK GrantedAccess;
} SYSTEM_HANDLE, *PSYSTEM_HANDLE;
typedef struct _SYSTEM_HANDLE_INFORMATION
{
ULONG HandleCount;
SYSTEM_HANDLE Handles[1];
} SYSTEM_HANDLE_INFORMATION, *PSYSTEM_HANDLE_INFORMATION;
typedef enum _POOL_TYPE
{
NonPagedPool,
PagedPool,
NonPagedPoolMustSucceed,
DontUseThisType,
NonPagedPoolCacheAligned,
PagedPoolCacheAligned,
NonPagedPoolCacheAlignedMustS
} POOL_TYPE, *PPOOL_TYPE;
typedef struct _OBJECT_TYPE_INFORMATION
{
UNICODE_STRING Name;
ULONG TotalNumberOfObjects;
ULONG TotalNumberOfHandles;
ULONG TotalPagedPoolUsage;
ULONG TotalNonPagedPoolUsage;
ULONG TotalNamePoolUsage;
ULONG TotalHandleTableUsage;
ULONG HighWaterNumberOfObjects;
ULONG HighWaterNumberOfHandles;
ULONG HighWaterPagedPoolUsage;
ULONG HighWaterNonPagedPoolUsage;
ULONG HighWaterNamePoolUsage;
ULONG HighWaterHandleTableUsage;
ULONG InvalidAttributes;
GENERIC_MAPPING GenericMapping;
ULONG ValidAccess;
BOOLEAN SecurityRequired;
BOOLEAN MaintainHandleCount;
USHORT MaintainTypeList;
POOL_TYPE PoolType;
ULONG PagedPoolUsage;
ULONG NonPagedPoolUsage;
} OBJECT_TYPE_INFORMATION, *POBJECT_TYPE_INFORMATION;
_NtQuerySystemInformation pNtQuerySystemInformation = NULL;
_NtDuplicateObject pNtDuplicateObject =NULL;
PSYSTEM_HANDLE_INFORMATION handleInfo;
ULONG handleInfoSize = 0x1000000;
//FILE *logfile;
#ifndef HAVE_GETPROCESSID
/* Run-time GetProcessId detection for Windows */
#ifdef PLATFORM_WIN32
#define HAVE_GETPROCESSID
typedef DWORD (WINAPI *GETPROCESSID_PROC) (HANDLE);
typedef DWORD (WINAPI *NTQUERYINFORMATIONPROCESS_PROC) (HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
typedef DWORD (WINAPI *RTLNTSTATUSTODOSERROR_PROC) (NTSTATUS);
static DWORD WINAPI GetProcessId_detect (HANDLE process);
static GETPROCESSID_PROC GetProcessId = &GetProcessId_detect;
static NTQUERYINFORMATIONPROCESS_PROC NtQueryInformationProcess_proc = NULL;
static RTLNTSTATUSTODOSERROR_PROC RtlNtStatusToDosError_proc = NULL;
static DWORD WINAPI GetProcessId_ntdll (HANDLE process)
{
PROCESS_BASIC_INFORMATION pi;
NTSTATUS status;
status = NtQueryInformationProcess_proc (process, ProcessBasicInformation, &pi, sizeof (pi), NULL);
if (NT_SUCCESS (status)) {
return pi.UniqueProcessId;
} else {
SetLastError (RtlNtStatusToDosError_proc (status));
return 0;
}
}
static DWORD WINAPI GetProcessId_stub (HANDLE process)
{
SetLastError (ERROR_CALL_NOT_IMPLEMENTED);
return 0;
}
static DWORD WINAPI GetProcessId_detect (HANDLE process)
{
HMODULE module_handle;
GETPROCESSID_PROC GetProcessId_kernel;
/* Windows XP SP1 and above have GetProcessId API */
module_handle = GetModuleHandle (L"kernel32.dll");
if (module_handle != NULL) {
GetProcessId_kernel = (GETPROCESSID_PROC) GetProcAddress (module_handle, "GetProcessId");
if (GetProcessId_kernel != NULL) {
GetProcessId = GetProcessId_kernel;
return GetProcessId (process);
}
}
/* Windows 2000 and above have deprecated NtQueryInformationProcess API */
module_handle = GetModuleHandle (L"ntdll.dll");
if (module_handle != NULL) {
NtQueryInformationProcess_proc = (NTQUERYINFORMATIONPROCESS_PROC) GetProcAddress (module_handle, "NtQueryInformationProcess");
if (NtQueryInformationProcess_proc != NULL) {
RtlNtStatusToDosError_proc = (RTLNTSTATUSTODOSERROR_PROC) GetProcAddress (module_handle, "RtlNtStatusToDosError");
if (RtlNtStatusToDosError_proc != NULL) {
GetProcessId = &GetProcessId_ntdll;
return GetProcessId (process);
}
}
}
/* Fall back to ERROR_CALL_NOT_IMPLEMENTED */
GetProcessId = &GetProcessId_stub;
return GetProcessId (process);
}
#endif /* PLATFORM_WIN32 */
#endif /* !HAVE_GETPROCESSID */
#ifdef PLATFORM_WIN32
/* Need this to determine whether to detach console */
#include <mono/metadata/cil-coff.h>
/* This turns off command line globbing under win32 */
int _CRT_glob = 0;
#endif
typedef void (*OptFunc) (const char *p);
#undef OPTFLAG
#ifdef HAVE_ARRAY_ELEM_INIT
#define MSGSTRFIELD(line) MSGSTRFIELD1(line)
#define MSGSTRFIELD1(line) str##line
static const struct msgstr_t {
#define OPTFLAG(id,shift,name,desc) char MSGSTRFIELD(__LINE__) [sizeof (name) + sizeof (desc)];
#include "optflags-def.h"
#undef OPTFLAG
} opstr = {
#define OPTFLAG(id,shift,name,desc) name "\0" desc,
#include "optflags-def.h"
#undef OPTFLAG
};
static const gint16 opt_names [] = {
#define OPTFLAG(id,shift,name,desc) [(shift)] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
#include "optflags-def.h"
#undef OPTFLAG
};
#define optflag_get_name(id) ((const char*)&opstr + opt_names [(id)])
#define optflag_get_desc(id) (optflag_get_name(id) + 1 + strlen (optflag_get_name(id)))
#else /* !HAVE_ARRAY_ELEM_INIT */
typedef struct {
const char* name;
const char* desc;
} OptName;
#define OPTFLAG(id,shift,name,desc) {name,desc},
static const OptName
opt_names [] = {
#include "optflags-def.h"
{NULL, NULL}
};
#define optflag_get_name(id) (opt_names [(id)].name)
#define optflag_get_desc(id) (opt_names [(id)].desc)
#endif
static const OptFunc
opt_funcs [sizeof (int) * 8] = {
NULL
};
#define DEFAULT_OPTIMIZATIONS ( \
MONO_OPT_PEEPHOLE | \
MONO_OPT_CFOLD | \
MONO_OPT_INLINE | \
MONO_OPT_CONSPROP | \
MONO_OPT_COPYPROP | \
MONO_OPT_TREEPROP | \
MONO_OPT_DEADCE | \
MONO_OPT_BRANCH | \
MONO_OPT_LINEARS | \
MONO_OPT_INTRINS | \
MONO_OPT_LOOP | \
MONO_OPT_EXCEPTION | \
MONO_OPT_CMOV | \
MONO_OPT_GSHARED | \
MONO_OPT_SIMD | \
MONO_OPT_AOT)
#define EXCLUDED_FROM_ALL (MONO_OPT_SHARED | MONO_OPT_PRECOMP)
static guint32
parse_optimizations (const char* p)
{
/* the default value */
guint32 opt = DEFAULT_OPTIMIZATIONS;
guint32 exclude = 0;
const char *n;
int i, invert, len;
/* call out to cpu detection code here that sets the defaults ... */
opt |= mono_arch_cpu_optimizazions (&exclude);
opt &= ~exclude;
if (!p)
return opt;
while (*p) {
if (*p == '-') {
p++;
invert = TRUE;
} else {
invert = FALSE;
}
for (i = 0; i < G_N_ELEMENTS (opt_names) && optflag_get_name (i); ++i) {
n = optflag_get_name (i);
len = strlen (n);
if (strncmp (p, n, len) == 0) {
if (invert)
opt &= ~ (1 << i);
else
opt |= 1 << i;
p += len;
if (*p == ',') {
p++;
break;
} else if (*p == '=') {
p++;
if (opt_funcs [i])
opt_funcs [i] (p);
while (*p && *p++ != ',');
break;
}
/* error out */
break;
}
}
if (i == G_N_ELEMENTS (opt_names) || !optflag_get_name (i)) {
if (strncmp (p, "all", 3) == 0) {
if (invert)
opt = 0;
else
opt = ~(EXCLUDED_FROM_ALL | exclude);
p += 3;
if (*p == ',')
p++;
} else {
fprintf (stderr, "Invalid optimization name `%s'\n", p);
exit (1);
}
}
}
return opt;
}
static gboolean
parse_debug_options (const char* p)
{
MonoDebugOptions *opt = mini_get_debug_options ();
do {
if (!*p) {
fprintf (stderr, "Syntax error; expected debug option name\n");
return FALSE;
}
if (!strncmp (p, "casts", 5)) {
opt->better_cast_details = TRUE;
p += 5;
} else if (!strncmp (p, "mdb-optimizations", 17)) {
opt->mdb_optimizations = TRUE;
p += 17;
} else if (!strncmp (p, "gdb", 3)) {
opt->gdb = TRUE;
p += 3;
} else {
fprintf (stderr, "Invalid debug option `%s', use --help-debug for details\n", p);
return FALSE;
}
if (*p == ',') {
p++;
if (!*p) {
fprintf (stderr, "Syntax error; expected debug option name\n");
return FALSE;
}
}
} while (*p);
return TRUE;
}
typedef struct {
const char name [6];
const char desc [18];
MonoGraphOptions value;
} GraphName;
static const GraphName
graph_names [] = {
{"cfg", "Control Flow", MONO_GRAPH_CFG},
{"dtree", "Dominator Tree", MONO_GRAPH_DTREE},
{"code", "CFG showing code", MONO_GRAPH_CFG_CODE},
{"ssa", "CFG after SSA", MONO_GRAPH_CFG_SSA},
{"optc", "CFG after IR opts", MONO_GRAPH_CFG_OPTCODE}
};
static MonoGraphOptions
mono_parse_graph_options (const char* p)
{
const char *n;
int i, len;
for (i = 0; i < G_N_ELEMENTS (graph_names); ++i) {
n = graph_names [i].name;
len = strlen (n);
if (strncmp (p, n, len) == 0)
return graph_names [i].value;
}
fprintf (stderr, "Invalid graph name provided: %s\n", p);
exit (1);
}
int
mono_parse_default_optimizations (const char* p)
{
guint32 opt;
opt = parse_optimizations (p);
return opt;
}
static char*
opt_descr (guint32 flags) {
GString *str = g_string_new ("");
int i, need_comma;
need_comma = 0;
for (i = 0; i < G_N_ELEMENTS (opt_names); ++i) {
if (flags & (1 << i)) {
if (need_comma)
g_string_append_c (str, ',');
g_string_append (str, optflag_get_name (i));
need_comma = 1;
}
}
return g_string_free (str, FALSE);
}
static const guint32
opt_sets [] = {
0,
MONO_OPT_PEEPHOLE,
MONO_OPT_BRANCH,
MONO_OPT_CFOLD,
MONO_OPT_FCMOV,
#ifdef MONO_ARCH_SIMD_INTRINSICS
MONO_OPT_SIMD,
MONO_OPT_SSE2,
MONO_OPT_SIMD | MONO_OPT_SSE2,
#endif
MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_INTRINS,
MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS,
MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP,
MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_CFOLD,
MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE,
MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS,
MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_SSA,
MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_EXCEPTION,
MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_EXCEPTION | MONO_OPT_CMOV,
MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_EXCEPTION | MONO_OPT_ABCREM,
MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_EXCEPTION | MONO_OPT_ABCREM | MONO_OPT_SSAPRE,
MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_ABCREM,
MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_TREEPROP,
MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_SSAPRE,
MONO_OPT_BRANCH | MONO_OPT_PEEPHOLE | MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP | MONO_OPT_DEADCE | MONO_OPT_LOOP | MONO_OPT_INLINE | MONO_OPT_INTRINS | MONO_OPT_ABCREM | MONO_OPT_SHARED,
DEFAULT_OPTIMIZATIONS,
};
typedef int (*TestMethod) (void);
#if 0
static void
domain_dump_native_code (MonoDomain *domain) {
// need to poke into the domain, move to metadata/domain.c
// need to empty jit_info_table and code_mp
}
#endif
static int
mini_regression (MonoImage *image, int verbose, int *total_run)
{
guint32 i, opt, opt_flags;
MonoMethod *method;
MonoCompile *cfg;
char *n;
int result, expected, failed, cfailed, run, code_size, total;
TestMethod func;
GTimer *timer = g_timer_new ();
MonoDomain *domain = mono_domain_get ();
guint32 exclude = 0;
mono_arch_cpu_optimizazions (&exclude);
if (mini_stats_fd) {
fprintf (mini_stats_fd, "$stattitle = \'Mono Benchmark Results (various optimizations)\';\n");
fprintf (mini_stats_fd, "$graph->set_legend(qw(");
for (opt = 0; opt < G_N_ELEMENTS (opt_sets); opt++) {
opt_flags = opt_sets [opt];
n = opt_descr (opt_flags);
if (!n [0])
n = (char *)"none";
if (opt)
fprintf (mini_stats_fd, " ");
fprintf (mini_stats_fd, "%s", n);
}
fprintf (mini_stats_fd, "));\n");
fprintf (mini_stats_fd, "@data = (\n");
fprintf (mini_stats_fd, "[");
}
/* load the metadata */
for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
if (!method)
continue;
mono_class_init (method->klass);
if (!strncmp (method->name, "test_", 5) && mini_stats_fd) {
fprintf (mini_stats_fd, "\"%s\",", method->name);
}
}
if (mini_stats_fd)
fprintf (mini_stats_fd, "],\n");
total = 0;
*total_run = 0;
for (opt = 0; opt < G_N_ELEMENTS (opt_sets); ++opt) {
double elapsed, comp_time, start_time;
opt_flags = opt_sets [opt] & ~exclude;
mono_set_defaults (verbose, opt_flags);
n = opt_descr (opt_flags);
g_print ("Test run: image=%s, opts=%s\n", mono_image_get_filename (image), n);
g_free (n);
cfailed = failed = run = code_size = 0;
comp_time = elapsed = 0.0;
/* fixme: ugly hack - delete all previously compiled methods */
g_hash_table_destroy (domain_jit_info (domain)->jit_trampoline_hash);
domain_jit_info (domain)->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
mono_internal_hash_table_destroy (&(domain->jit_code_hash));
mono_jit_code_hash_init (&(domain->jit_code_hash));
g_timer_start (timer);
if (mini_stats_fd)
fprintf (mini_stats_fd, "[");
for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
if (!method)
continue;
if (strncmp (method->name, "test_", 5) == 0) {
expected = atoi (method->name + 5);
run++;
start_time = g_timer_elapsed (timer, NULL);
comp_time -= start_time;
cfg = mini_method_compile (method, opt_flags, mono_get_root_domain (), TRUE, FALSE, 0);
comp_time += g_timer_elapsed (timer, NULL);
if (cfg->exception_type == MONO_EXCEPTION_NONE) {
if (verbose >= 2)
g_print ("Running '%s' ...\n", method->name);
#ifdef MONO_USE_AOT_COMPILER
if ((func = mono_aot_get_method (mono_get_root_domain (), method)))
;
else
#endif
func = (TestMethod)(gpointer)cfg->native_code;
func = (TestMethod)mono_create_ftnptr (mono_get_root_domain (), func);
result = func ();
if (result != expected) {
failed++;
g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
}
code_size += cfg->code_len;
mono_destroy_compile (cfg);
} else {
cfailed++;
if (verbose)
g_print ("Test '%s' failed compilation.\n", method->name);
}
if (mini_stats_fd)
fprintf (mini_stats_fd, "%f, ",
g_timer_elapsed (timer, NULL) - start_time);
}
}
if (mini_stats_fd)
fprintf (mini_stats_fd, "],\n");
g_timer_stop (timer);
elapsed = g_timer_elapsed (timer, NULL);
if (failed > 0 || cfailed > 0){
g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
} else {
g_print ("Results: total tests: %d, all pass \n", run);
}
g_print ("Elapsed time: %f secs (%f, %f), Code size: %d\n\n", elapsed,
elapsed - comp_time, comp_time, code_size);
total += failed + cfailed;
*total_run += run;
}
if (mini_stats_fd) {
fprintf (mini_stats_fd, ");\n");
fflush (mini_stats_fd);
}
g_timer_destroy (timer);
return total;
}
static int
mini_regression_list (int verbose, int count, char *images [])
{
int i, total, total_run, run;
MonoAssembly *ass;
total_run = total = 0;
for (i = 0; i < count; ++i) {
ass = mono_assembly_open (images [i], NULL);
if (!ass) {
g_warning ("failed to load assembly: %s", images [i]);
continue;
}
total += mini_regression (mono_assembly_get_image (ass), verbose, &run);
total_run += run;
}
if (total > 0){
g_print ("Overall results: tests: %d, failed: %d, opt combinations: %d (pass: %.2f%%)\n",
total_run, total, (int)G_N_ELEMENTS (opt_sets), 100.0*(total_run-total)/total_run);
} else {
g_print ("Overall results: tests: %d, 100%% pass, opt combinations: %d\n",
total_run, (int)G_N_ELEMENTS (opt_sets));
}
return total;
}
#ifdef MONO_JIT_INFO_TABLE_TEST
typedef struct _JitInfoData
{
guint start;
guint length;
MonoJitInfo *ji;
struct _JitInfoData *next;
} JitInfoData;
typedef struct
{
guint start;
guint length;
int num_datas;
JitInfoData *data;
} Region;
typedef struct
{
int num_datas;
int num_regions;
Region *regions;
int num_frees;
JitInfoData *frees;
} ThreadData;
static int num_threads;
static ThreadData *thread_datas;
static MonoDomain *test_domain;
static JitInfoData*
alloc_random_data (Region *region)
{
JitInfoData **data;
JitInfoData *prev;
guint prev_end;
guint next_start;
guint max_len;
JitInfoData *d;
int num_retries = 0;
int pos, i;
restart:
prev = NULL;
data = ®ion->data;
pos = random () % (region->num_datas + 1);
i = 0;
while (*data != NULL) {
if (i++ == pos)
break;
prev = *data;
data = &(*data)->next;
}
if (prev == NULL)
g_assert (*data == region->data);
else
g_assert (prev->next == *data);
if (prev == NULL)
prev_end = region->start;
else
prev_end = prev->start + prev->length;
if (*data == NULL)
next_start = region->start + region->length;
else
next_start = (*data)->start;
g_assert (prev_end <= next_start);
max_len = next_start - prev_end;
if (max_len < 128) {
if (++num_retries >= 10)
return NULL;
goto restart;
}
if (max_len > 1024)
max_len = 1024;
d = g_new0 (JitInfoData, 1);
d->start = prev_end + random () % (max_len / 2);
d->length = random () % MIN (max_len, next_start - d->start) + 1;
g_assert (d->start >= prev_end && d->start + d->length <= next_start);
d->ji = g_new0 (MonoJitInfo, 1);
d->ji->method = (MonoMethod*) 0xABadBabe;
d->ji->code_start = (gpointer)(gulong) d->start;
d->ji->code_size = d->length;
d->ji->cas_inited = 1; /* marks an allocated jit info */
d->next = *data;
*data = d;
++region->num_datas;
return d;
}
static JitInfoData**
choose_random_data (Region *region)
{
int n;
int i;
JitInfoData **d;
g_assert (region->num_datas > 0);
n = random () % region->num_datas;
for (d = ®ion->data, i = 0;
i < n;
d = &(*d)->next, ++i)
;
return d;
}
static Region*
choose_random_region (ThreadData *td)
{
return &td->regions [random () % td->num_regions];
}
static ThreadData*
choose_random_thread (void)
{
return &thread_datas [random () % num_threads];
}
static void
free_jit_info_data (ThreadData *td, JitInfoData *free)
{
free->next = td->frees;
td->frees = free;
if (++td->num_frees >= 1000) {
int i;
for (i = 0; i < 500; ++i)
free = free->next;
while (free->next != NULL) {
JitInfoData *next = free->next->next;
//g_free (free->next->ji);
g_free (free->next);
free->next = next;
--td->num_frees;
}
}
}
#define NUM_THREADS 8
#define REGIONS_PER_THREAD 10
#define REGION_SIZE 0x10000
#define MAX_ADDR (REGION_SIZE * REGIONS_PER_THREAD * NUM_THREADS)
#define MODE_ALLOC 1
#define MODE_FREE 2
static void
test_thread_func (ThreadData *td)
{
int mode = MODE_ALLOC;
int i = 0;
gulong lookup_successes = 0, lookup_failures = 0;
MonoDomain *domain = test_domain;
int thread_num = (int)(td - thread_datas);
gboolean modify_thread = thread_num < NUM_THREADS / 2; /* only half of the threads modify the table */
for (;;) {
int alloc;
int lookup = 1;
if (td->num_datas == 0) {
lookup = 0;
alloc = 1;
} else if (modify_thread && random () % 1000 < 5) {
lookup = 0;
if (mode == MODE_ALLOC)
alloc = (random () % 100) < 70;
else if (mode == MODE_FREE)
alloc = (random () % 100) < 30;
}
if (lookup) {
/* modify threads sometimes look up their own jit infos */
if (modify_thread && random () % 10 < 5) {
Region *region = choose_random_region (td);
if (region->num_datas > 0) {
JitInfoData **data = choose_random_data (region);
guint pos = (*data)->start + random () % (*data)->length;
MonoJitInfo *ji;
ji = mono_jit_info_table_find (domain, (char*)(gulong) pos);
g_assert (ji->cas_inited);
g_assert ((*data)->ji == ji);
}
} else {
int pos = random () % MAX_ADDR;
char *addr = (char*)(gulong) pos;
MonoJitInfo *ji;
ji = mono_jit_info_table_find (domain, addr);
/*
* FIXME: We are actually not allowed
* to do this. By the time we examine
* the ji another thread might already
* have removed it.
*/
if (ji != NULL) {
g_assert (addr >= (char*)ji->code_start && addr < (char*)ji->code_start + ji->code_size);
++lookup_successes;
} else
++lookup_failures;
}
} else if (alloc) {
JitInfoData *data = alloc_random_data (choose_random_region (td));
if (data != NULL) {
mono_jit_info_table_add (domain, data->ji);
++td->num_datas;
}
} else {
Region *region = choose_random_region (td);
if (region->num_datas > 0) {
JitInfoData **data = choose_random_data (region);
JitInfoData *free;
mono_jit_info_table_remove (domain, (*data)->ji);
//(*data)->ji->cas_inited = 0; /* marks a free jit info */
free = *data;
*data = (*data)->next;
free_jit_info_data (td, free);
--region->num_datas;
--td->num_datas;
}
}
if (++i % 100000 == 0) {
int j;
g_print ("num datas %d (%ld - %ld): %d", (int)(td - thread_datas),
lookup_successes, lookup_failures, td->num_datas);
for (j = 0; j < td->num_regions; ++j)
g_print (" %d", td->regions [j].num_datas);
g_print ("\n");
}
if (td->num_datas < 100)
mode = MODE_ALLOC;
else if (td->num_datas > 2000)
mode = MODE_FREE;
}
}
/*
static void
small_id_thread_func (gpointer arg)
{
MonoThread *thread = mono_thread_current ();
MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
g_print ("my small id is %d\n", (int)thread->small_id);
mono_hazard_pointer_clear (hp, 1);
sleep (3);
g_print ("done %d\n", (int)thread->small_id);
}
*/
static void
jit_info_table_test (MonoDomain *domain)
{
int i;
g_print ("testing jit_info_table\n");
num_threads = NUM_THREADS;
thread_datas = g_new0 (ThreadData, num_threads);
for (i = 0; i < num_threads; ++i) {
int j;
thread_datas [i].num_regions = REGIONS_PER_THREAD;
thread_datas [i].regions = g_new0 (Region, REGIONS_PER_THREAD);
for (j = 0; j < REGIONS_PER_THREAD; ++j) {
thread_datas [i].regions [j].start = (num_threads * j + i) * REGION_SIZE;