-
-
Notifications
You must be signed in to change notification settings - Fork 250
/
guminterceptor.c
2108 lines (1700 loc) · 56.2 KB
/
guminterceptor.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
/*
* Copyright (C) 2008-2024 Ole André Vadla Ravnås <[email protected]>
* Copyright (C) 2008 Christian Berentsen <[email protected]>
* Copyright (C) 2024 Francesco Tamagni <[email protected]>
* Copyright (C) 2024 Yannis Juglaret <[email protected]>
*
* Licence: wxWindows Library Licence, Version 3.1
*/
#include "guminterceptor.h"
#include "gumcodesegment.h"
#include "guminterceptor-priv.h"
#include "gumlibc.h"
#include "gummemory.h"
#include "gumprocess-priv.h"
#include "gumtls.h"
#include <string.h>
#ifdef HAVE_DARWIN
# include <mach/mach.h>
#endif
#ifdef HAVE_MIPS
# define GUM_INTERCEPTOR_CODE_SLICE_SIZE 1024
#else
# define GUM_INTERCEPTOR_CODE_SLICE_SIZE 256
#endif
#define GUM_INTERCEPTOR_LOCK(o) g_rec_mutex_lock (&(o)->mutex)
#define GUM_INTERCEPTOR_UNLOCK(o) g_rec_mutex_unlock (&(o)->mutex)
typedef struct _GumInterceptorTransaction GumInterceptorTransaction;
typedef guint GumInstrumentationError;
typedef struct _GumDestroyTask GumDestroyTask;
typedef struct _GumUpdateTask GumUpdateTask;
typedef struct _GumSuspendOperation GumSuspendOperation;
typedef struct _ListenerEntry ListenerEntry;
typedef struct _InterceptorThreadContext InterceptorThreadContext;
typedef struct _GumInvocationStackEntry GumInvocationStackEntry;
typedef struct _ListenerDataSlot ListenerDataSlot;
typedef struct _ListenerInvocationState ListenerInvocationState;
typedef void (* GumUpdateTaskFunc) (GumInterceptor * self,
GumFunctionContext * ctx, gpointer prologue);
struct _GumInterceptorTransaction
{
gboolean is_dirty;
gint level;
GQueue * pending_destroy_tasks;
GHashTable * pending_update_tasks;
GumInterceptor * interceptor;
};
struct _GumInterceptor
{
#ifndef GUM_DIET
GObject parent;
#else
GumObject parent;
#endif
GRecMutex mutex;
GHashTable * function_by_address;
GumInterceptorBackend * backend;
GumCodeAllocator allocator;
volatile guint selected_thread_id;
GumInterceptorTransaction current_transaction;
};
enum _GumInstrumentationError
{
GUM_INSTRUMENTATION_ERROR_NONE,
GUM_INSTRUMENTATION_ERROR_WRONG_SIGNATURE,
GUM_INSTRUMENTATION_ERROR_POLICY_VIOLATION,
GUM_INSTRUMENTATION_ERROR_WRONG_TYPE,
};
struct _GumDestroyTask
{
GumFunctionContext * ctx;
GDestroyNotify notify;
gpointer data;
};
struct _GumUpdateTask
{
GumFunctionContext * ctx;
GumUpdateTaskFunc func;
};
struct _GumSuspendOperation
{
GumThreadId current_thread_id;
GQueue suspended_threads;
};
struct _ListenerEntry
{
#ifndef GUM_DIET
GumInvocationListenerInterface * listener_interface;
GumInvocationListener * listener_instance;
#else
union
{
GumInvocationListener * listener_interface;
GumInvocationListener * listener_instance;
};
#endif
gpointer function_data;
};
struct _InterceptorThreadContext
{
GumInvocationBackend listener_backend;
GumInvocationBackend replacement_backend;
gint ignore_level;
GumInvocationStack * stack;
GArray * listener_data_slots;
};
struct _GumInvocationStackEntry
{
GumFunctionContext * function_ctx;
gpointer caller_ret_addr;
GumInvocationContext invocation_context;
GumCpuContext cpu_context;
guint8 listener_invocation_data[GUM_MAX_LISTENERS_PER_FUNCTION]
[GUM_MAX_LISTENER_DATA];
gboolean calling_replacement;
gint original_system_error;
};
struct _ListenerDataSlot
{
GumInvocationListener * owner;
guint8 data[GUM_MAX_LISTENER_DATA];
};
struct _ListenerInvocationState
{
GumPointCut point_cut;
ListenerEntry * entry;
InterceptorThreadContext * interceptor_ctx;
guint8 * invocation_data;
};
#ifndef GUM_DIET
static void gum_interceptor_dispose (GObject * object);
static void gum_interceptor_finalize (GObject * object);
static void the_interceptor_weak_notify (gpointer data,
GObject * where_the_object_was);
#endif
static GumReplaceReturn gum_interceptor_replace_with_type (
GumInterceptor * self, GumInterceptorType type, gpointer function_address,
gpointer replacement_function, gpointer replacement_data,
gpointer * original_function);
static GumFunctionContext * gum_interceptor_instrument (GumInterceptor * self,
GumInterceptorType type, gpointer function_address,
GumInstrumentationError * error);
static void gum_interceptor_activate (GumInterceptor * self,
GumFunctionContext * ctx, gpointer prologue);
static void gum_interceptor_deactivate (GumInterceptor * self,
GumFunctionContext * ctx, gpointer prologue);
static void gum_interceptor_transaction_init (
GumInterceptorTransaction * transaction, GumInterceptor * interceptor);
static void gum_interceptor_transaction_destroy (
GumInterceptorTransaction * transaction);
static void gum_interceptor_transaction_begin (
GumInterceptorTransaction * self);
static void gum_interceptor_transaction_end (GumInterceptorTransaction * self);
static gboolean gum_maybe_suspend_thread (const GumThreadDetails * details,
gpointer user_data);
static void gum_interceptor_transaction_schedule_destroy (
GumInterceptorTransaction * self, GumFunctionContext * ctx,
GDestroyNotify notify, gpointer data);
static void gum_interceptor_transaction_schedule_update (
GumInterceptorTransaction * self, GumFunctionContext * ctx,
GumUpdateTaskFunc func);
static GumFunctionContext * gum_function_context_new (
GumInterceptor * interceptor, gpointer function_address,
GumInterceptorType type);
static void gum_function_context_finalize (GumFunctionContext * function_ctx);
static void gum_function_context_destroy (GumFunctionContext * function_ctx);
static void gum_function_context_perform_destroy (
GumFunctionContext * function_ctx);
static gboolean gum_function_context_is_empty (
GumFunctionContext * function_ctx);
static void gum_function_context_add_listener (
GumFunctionContext * function_ctx, GumInvocationListener * listener,
gpointer function_data);
static void gum_function_context_remove_listener (
GumFunctionContext * function_ctx, GumInvocationListener * listener);
static void listener_entry_free (ListenerEntry * entry);
static gboolean gum_function_context_has_listener (
GumFunctionContext * function_ctx, GumInvocationListener * listener);
static ListenerEntry ** gum_function_context_find_listener (
GumFunctionContext * function_ctx, GumInvocationListener * listener);
static ListenerEntry ** gum_function_context_find_taken_listener_slot (
GumFunctionContext * function_ctx);
static void gum_function_context_fixup_cpu_context (
GumFunctionContext * function_ctx, GumCpuContext * cpu_context);
static InterceptorThreadContext * get_interceptor_thread_context (void);
static void release_interceptor_thread_context (
InterceptorThreadContext * context);
static InterceptorThreadContext * interceptor_thread_context_new (void);
static void interceptor_thread_context_destroy (
InterceptorThreadContext * context);
static gpointer interceptor_thread_context_get_listener_data (
InterceptorThreadContext * self, GumInvocationListener * listener,
gsize required_size);
static void interceptor_thread_context_forget_listener_data (
InterceptorThreadContext * self, GumInvocationListener * listener);
static GumInvocationStackEntry * gum_invocation_stack_push (
GumInvocationStack * stack, GumFunctionContext * function_ctx,
gpointer caller_ret_addr);
static gpointer gum_invocation_stack_pop (GumInvocationStack * stack);
static GumInvocationStackEntry * gum_invocation_stack_peek_top (
GumInvocationStack * stack);
static gpointer gum_interceptor_resolve (GumInterceptor * self,
gpointer address);
static gboolean gum_interceptor_has (GumInterceptor * self,
gpointer function_address);
static gpointer gum_page_address_from_pointer (gpointer ptr);
static gint gum_page_address_compare (gconstpointer a, gconstpointer b);
#ifndef GUM_DIET
G_DEFINE_TYPE (GumInterceptor, gum_interceptor, G_TYPE_OBJECT)
#endif
static GMutex _gum_interceptor_lock;
static GumInterceptor * _the_interceptor = NULL;
static GumSpinlock gum_interceptor_thread_context_lock = GUM_SPINLOCK_INIT;
static GHashTable * gum_interceptor_thread_contexts;
static GPrivate gum_interceptor_context_private =
G_PRIVATE_INIT ((GDestroyNotify) release_interceptor_thread_context);
static GumTlsKey gum_interceptor_guard_key;
static GumInvocationStack _gum_interceptor_empty_stack = { NULL, 0 };
#ifndef GUM_DIET
static void
gum_interceptor_class_init (GumInterceptorClass * klass)
{
GObjectClass * object_class = G_OBJECT_CLASS (klass);
object_class->dispose = gum_interceptor_dispose;
object_class->finalize = gum_interceptor_finalize;
}
#endif
void
_gum_interceptor_init (void)
{
gum_interceptor_thread_contexts = g_hash_table_new_full (NULL, NULL,
(GDestroyNotify) interceptor_thread_context_destroy, NULL);
gum_interceptor_guard_key = gum_tls_key_new ();
}
void
_gum_interceptor_deinit (void)
{
gum_tls_key_free (gum_interceptor_guard_key);
g_hash_table_unref (gum_interceptor_thread_contexts);
gum_interceptor_thread_contexts = NULL;
}
static void
gum_interceptor_init (GumInterceptor * self)
{
g_rec_mutex_init (&self->mutex);
self->function_by_address = g_hash_table_new_full (NULL, NULL, NULL,
(GDestroyNotify) gum_function_context_destroy);
gum_code_allocator_init (&self->allocator, GUM_INTERCEPTOR_CODE_SLICE_SIZE);
gum_interceptor_transaction_init (&self->current_transaction, self);
}
static void
gum_interceptor_do_dispose (GumInterceptor * self)
{
GUM_INTERCEPTOR_LOCK (self);
gum_interceptor_transaction_begin (&self->current_transaction);
self->current_transaction.is_dirty = TRUE;
g_hash_table_remove_all (self->function_by_address);
gum_interceptor_transaction_end (&self->current_transaction);
GUM_INTERCEPTOR_UNLOCK (self);
}
static void
gum_interceptor_do_finalize (GumInterceptor * self)
{
gum_interceptor_transaction_destroy (&self->current_transaction);
if (self->backend != NULL)
_gum_interceptor_backend_destroy (self->backend);
g_rec_mutex_clear (&self->mutex);
g_hash_table_unref (self->function_by_address);
gum_code_allocator_free (&self->allocator);
}
#ifndef GUM_DIET
static void
gum_interceptor_dispose (GObject * object)
{
gum_interceptor_do_dispose (GUM_INTERCEPTOR (object));
G_OBJECT_CLASS (gum_interceptor_parent_class)->dispose (object);
}
static void
gum_interceptor_finalize (GObject * object)
{
gum_interceptor_do_finalize (GUM_INTERCEPTOR (object));
G_OBJECT_CLASS (gum_interceptor_parent_class)->finalize (object);
}
#else
static void
gum_interceptor_finalize (GumObject * object)
{
GumInterceptor * self = GUM_INTERCEPTOR (object);
g_mutex_lock (&_gum_interceptor_lock);
if (_the_interceptor == self)
_the_interceptor = NULL;
g_mutex_unlock (&_gum_interceptor_lock);
gum_interceptor_do_dispose (self);
gum_interceptor_do_finalize (self);
}
#endif
GumInterceptor *
gum_interceptor_obtain (void)
{
GumInterceptor * interceptor;
g_mutex_lock (&_gum_interceptor_lock);
#ifndef GUM_DIET
if (_the_interceptor != NULL)
{
interceptor = GUM_INTERCEPTOR (g_object_ref (_the_interceptor));
}
else
{
_the_interceptor = g_object_new (GUM_TYPE_INTERCEPTOR, NULL);
g_object_weak_ref (G_OBJECT (_the_interceptor),
the_interceptor_weak_notify, NULL);
interceptor = _the_interceptor;
}
#else
if (_the_interceptor != NULL)
{
interceptor = gum_object_ref (_the_interceptor);
}
else
{
_the_interceptor = g_new0 (GumInterceptor, 1);
_the_interceptor->parent.ref_count = 1;
_the_interceptor->parent.finalize = gum_interceptor_finalize;
gum_interceptor_init (_the_interceptor);
interceptor = _the_interceptor;
}
#endif
g_mutex_unlock (&_gum_interceptor_lock);
return interceptor;
}
#ifndef GUM_DIET
static void
the_interceptor_weak_notify (gpointer data,
GObject * where_the_object_was)
{
g_mutex_lock (&_gum_interceptor_lock);
g_assert (_the_interceptor == (GumInterceptor *) where_the_object_was);
_the_interceptor = NULL;
g_mutex_unlock (&_gum_interceptor_lock);
}
#endif
GumAttachReturn
gum_interceptor_attach (GumInterceptor * self,
gpointer function_address,
GumInvocationListener * listener,
gpointer listener_function_data)
{
GumAttachReturn result = GUM_ATTACH_OK;
GumFunctionContext * function_ctx;
GumInstrumentationError error;
gum_interceptor_ignore_current_thread (self);
GUM_INTERCEPTOR_LOCK (self);
gum_interceptor_transaction_begin (&self->current_transaction);
self->current_transaction.is_dirty = TRUE;
function_address = gum_interceptor_resolve (self, function_address);
function_ctx = gum_interceptor_instrument (self, GUM_INTERCEPTOR_TYPE_DEFAULT,
function_address, &error);
if (function_ctx == NULL)
goto instrumentation_error;
if (gum_function_context_has_listener (function_ctx, listener))
goto already_attached;
gum_function_context_add_listener (function_ctx, listener,
listener_function_data);
goto beach;
instrumentation_error:
{
switch (error)
{
case GUM_INSTRUMENTATION_ERROR_WRONG_SIGNATURE:
result = GUM_ATTACH_WRONG_SIGNATURE;
break;
case GUM_INSTRUMENTATION_ERROR_POLICY_VIOLATION:
result = GUM_ATTACH_POLICY_VIOLATION;
break;
case GUM_INSTRUMENTATION_ERROR_WRONG_TYPE:
result = GUM_ATTACH_WRONG_TYPE;
break;
default:
g_assert_not_reached ();
}
goto beach;
}
already_attached:
{
result = GUM_ATTACH_ALREADY_ATTACHED;
goto beach;
}
beach:
{
gum_interceptor_transaction_end (&self->current_transaction);
GUM_INTERCEPTOR_UNLOCK (self);
gum_interceptor_unignore_current_thread (self);
return result;
}
}
void
gum_interceptor_detach (GumInterceptor * self,
GumInvocationListener * listener)
{
GHashTableIter iter;
gpointer key, value;
gum_interceptor_ignore_current_thread (self);
GUM_INTERCEPTOR_LOCK (self);
gum_interceptor_transaction_begin (&self->current_transaction);
self->current_transaction.is_dirty = TRUE;
g_hash_table_iter_init (&iter, self->function_by_address);
while (g_hash_table_iter_next (&iter, NULL, &value))
{
GumFunctionContext * function_ctx = value;
if (gum_function_context_has_listener (function_ctx, listener))
{
gum_function_context_remove_listener (function_ctx, listener);
gum_interceptor_transaction_schedule_destroy (&self->current_transaction,
function_ctx,
#ifndef GUM_DIET
g_object_unref, g_object_ref (listener)
#else
gum_object_unref, gum_object_ref (listener)
#endif
);
if (gum_function_context_is_empty (function_ctx))
{
g_hash_table_iter_remove (&iter);
}
}
}
gum_spinlock_acquire (&gum_interceptor_thread_context_lock);
g_hash_table_iter_init (&iter, gum_interceptor_thread_contexts);
while (g_hash_table_iter_next (&iter, &key, NULL))
{
InterceptorThreadContext * thread_ctx = key;
interceptor_thread_context_forget_listener_data (thread_ctx, listener);
}
gum_spinlock_release (&gum_interceptor_thread_context_lock);
gum_interceptor_transaction_end (&self->current_transaction);
GUM_INTERCEPTOR_UNLOCK (self);
gum_interceptor_unignore_current_thread (self);
}
GumReplaceReturn
gum_interceptor_replace (GumInterceptor * self,
gpointer function_address,
gpointer replacement_function,
gpointer replacement_data,
gpointer * original_function)
{
return gum_interceptor_replace_with_type (self, GUM_INTERCEPTOR_TYPE_DEFAULT,
function_address, replacement_function, replacement_data,
original_function);
}
GumReplaceReturn
gum_interceptor_replace_fast (GumInterceptor * self,
gpointer function_address,
gpointer replacement_function,
gpointer * original_function)
{
return gum_interceptor_replace_with_type (self, GUM_INTERCEPTOR_TYPE_FAST,
function_address, replacement_function, NULL,
original_function);
}
static GumReplaceReturn
gum_interceptor_replace_with_type (GumInterceptor * self,
GumInterceptorType type,
gpointer function_address,
gpointer replacement_function,
gpointer replacement_data,
gpointer * original_function)
{
GumReplaceReturn result = GUM_REPLACE_OK;
GumFunctionContext * function_ctx;
GumInstrumentationError error;
GUM_INTERCEPTOR_LOCK (self);
gum_interceptor_transaction_begin (&self->current_transaction);
self->current_transaction.is_dirty = TRUE;
function_address = gum_interceptor_resolve (self, function_address);
function_ctx =
gum_interceptor_instrument (self, type, function_address, &error);
if (function_ctx == NULL)
goto instrumentation_error;
if (function_ctx->replacement_function != NULL)
goto already_replaced;
function_ctx->replacement_data = replacement_data;
function_ctx->replacement_function = replacement_function;
if (original_function != NULL)
*original_function = function_ctx->on_invoke_trampoline;
goto beach;
instrumentation_error:
{
switch (error)
{
case GUM_INSTRUMENTATION_ERROR_WRONG_SIGNATURE:
result = GUM_REPLACE_WRONG_SIGNATURE;
break;
case GUM_INSTRUMENTATION_ERROR_POLICY_VIOLATION:
result = GUM_REPLACE_POLICY_VIOLATION;
break;
case GUM_INSTRUMENTATION_ERROR_WRONG_TYPE:
result = GUM_REPLACE_WRONG_TYPE;
break;
default:
g_assert_not_reached ();
}
goto beach;
}
already_replaced:
{
result = GUM_REPLACE_ALREADY_REPLACED;
goto beach;
}
beach:
{
gum_interceptor_transaction_end (&self->current_transaction);
GUM_INTERCEPTOR_UNLOCK (self);
return result;
}
}
void
gum_interceptor_revert (GumInterceptor * self,
gpointer function_address)
{
GumFunctionContext * function_ctx;
GUM_INTERCEPTOR_LOCK (self);
gum_interceptor_transaction_begin (&self->current_transaction);
self->current_transaction.is_dirty = TRUE;
function_address = gum_interceptor_resolve (self, function_address);
function_ctx = (GumFunctionContext *) g_hash_table_lookup (
self->function_by_address, function_address);
if (function_ctx == NULL)
goto beach;
function_ctx->replacement_function = NULL;
function_ctx->replacement_data = NULL;
if (gum_function_context_is_empty (function_ctx))
{
g_hash_table_remove (self->function_by_address, function_address);
}
beach:
gum_interceptor_transaction_end (&self->current_transaction);
GUM_INTERCEPTOR_UNLOCK (self);
}
void
gum_interceptor_begin_transaction (GumInterceptor * self)
{
GUM_INTERCEPTOR_LOCK (self);
gum_interceptor_transaction_begin (&self->current_transaction);
GUM_INTERCEPTOR_UNLOCK (self);
}
void
gum_interceptor_end_transaction (GumInterceptor * self)
{
GUM_INTERCEPTOR_LOCK (self);
gum_interceptor_transaction_end (&self->current_transaction);
GUM_INTERCEPTOR_UNLOCK (self);
}
gboolean
gum_interceptor_flush (GumInterceptor * self)
{
gboolean flushed = FALSE;
GUM_INTERCEPTOR_LOCK (self);
if (self->current_transaction.level == 0)
{
gum_interceptor_transaction_begin (&self->current_transaction);
gum_interceptor_transaction_end (&self->current_transaction);
flushed =
g_queue_is_empty (self->current_transaction.pending_destroy_tasks);
}
GUM_INTERCEPTOR_UNLOCK (self);
return flushed;
}
GumInvocationContext *
gum_interceptor_get_current_invocation (void)
{
InterceptorThreadContext * interceptor_ctx;
GumInvocationStackEntry * entry;
interceptor_ctx = get_interceptor_thread_context ();
entry = gum_invocation_stack_peek_top (interceptor_ctx->stack);
if (entry == NULL)
return NULL;
return &entry->invocation_context;
}
GumInvocationContext *
gum_interceptor_get_live_replacement_invocation (gpointer replacement_function)
{
InterceptorThreadContext * interceptor_ctx;
GumInvocationStackEntry * entry;
interceptor_ctx = get_interceptor_thread_context ();
entry = gum_invocation_stack_peek_top (interceptor_ctx->stack);
if (entry == NULL)
return NULL;
if (!entry->calling_replacement)
return NULL;
if (replacement_function != entry->function_ctx->replacement_function)
return NULL;
return &entry->invocation_context;
}
GumInvocationStack *
gum_interceptor_get_current_stack (void)
{
InterceptorThreadContext * context;
context = g_private_get (&gum_interceptor_context_private);
if (context == NULL)
return &_gum_interceptor_empty_stack;
return context->stack;
}
void
gum_interceptor_ignore_current_thread (GumInterceptor * self)
{
InterceptorThreadContext * interceptor_ctx;
interceptor_ctx = get_interceptor_thread_context ();
interceptor_ctx->ignore_level++;
}
void
gum_interceptor_unignore_current_thread (GumInterceptor * self)
{
InterceptorThreadContext * interceptor_ctx;
interceptor_ctx = get_interceptor_thread_context ();
interceptor_ctx->ignore_level--;
}
gboolean
gum_interceptor_maybe_unignore_current_thread (GumInterceptor * self)
{
InterceptorThreadContext * interceptor_ctx;
interceptor_ctx = get_interceptor_thread_context ();
if (interceptor_ctx->ignore_level <= 0)
return FALSE;
interceptor_ctx->ignore_level--;
return TRUE;
}
void
gum_interceptor_ignore_other_threads (GumInterceptor * self)
{
self->selected_thread_id = gum_process_get_current_thread_id ();
}
void
gum_interceptor_unignore_other_threads (GumInterceptor * self)
{
g_assert (self->selected_thread_id == gum_process_get_current_thread_id ());
self->selected_thread_id = 0;
}
gpointer
gum_invocation_stack_translate (GumInvocationStack * self,
gpointer return_address)
{
guint i;
for (i = 0; i != self->len; i++)
{
GumInvocationStackEntry * entry;
entry = &g_array_index (self, GumInvocationStackEntry, i);
if (entry->function_ctx->on_leave_trampoline == return_address)
return entry->caller_ret_addr;
}
return return_address;
}
void
gum_interceptor_save (GumInvocationState * state)
{
*state = gum_interceptor_get_current_stack ()->len;
}
void
gum_interceptor_restore (GumInvocationState * state)
{
GumInvocationStack * stack;
guint old_depth, new_depth, i;
stack = gum_interceptor_get_current_stack ();
old_depth = *state;
new_depth = stack->len;
if (new_depth == old_depth)
return;
for (i = old_depth; i != new_depth; i++)
{
GumInvocationStackEntry * entry;
entry = &g_array_index (stack, GumInvocationStackEntry, i);
g_atomic_int_dec_and_test (&entry->function_ctx->trampoline_usage_counter);
}
g_array_set_size (stack, old_depth);
}
void
gum_interceptor_with_lock_held (GumInterceptor * self,
GumInterceptorLockedFunc func,
gpointer user_data)
{
GUM_INTERCEPTOR_LOCK (self);
func (user_data);
GUM_INTERCEPTOR_UNLOCK (self);
}
gboolean
gum_interceptor_is_locked (GumInterceptor * self)
{
if (!g_rec_mutex_trylock (&self->mutex))
return TRUE;
GUM_INTERCEPTOR_UNLOCK (self);
return FALSE;
}
gpointer
_gum_interceptor_peek_top_caller_return_address (void)
{
GumInvocationStack * stack;
GumInvocationStackEntry * entry;
stack = gum_interceptor_get_current_stack ();
if (stack->len == 0)
return NULL;
entry = &g_array_index (stack, GumInvocationStackEntry, stack->len - 1);
return entry->caller_ret_addr;
}
gpointer
_gum_interceptor_translate_top_return_address (gpointer return_address)
{
GumInvocationStack * stack;
GumInvocationStackEntry * entry;
stack = gum_interceptor_get_current_stack ();
if (stack->len == 0)
goto fallback;
entry = &g_array_index (stack, GumInvocationStackEntry, stack->len - 1);
if (entry->function_ctx->on_leave_trampoline != return_address)
goto fallback;
return entry->caller_ret_addr;
fallback:
return return_address;
}
static GumFunctionContext *
gum_interceptor_instrument (GumInterceptor * self,
GumInterceptorType type,
gpointer function_address,
GumInstrumentationError * error)
{
GumFunctionContext * ctx;
*error = GUM_INSTRUMENTATION_ERROR_NONE;
ctx = (GumFunctionContext *) g_hash_table_lookup (self->function_by_address,
function_address);
if (ctx != NULL)
{
if (ctx->type != type)
{
*error = GUM_INSTRUMENTATION_ERROR_WRONG_TYPE;
return NULL;
}
return ctx;
}
if (self->backend == NULL)
{
self->backend =
_gum_interceptor_backend_create (&self->mutex, &self->allocator);
}
ctx = gum_function_context_new (self, function_address, type);
if (gum_process_get_code_signing_policy () == GUM_CODE_SIGNING_REQUIRED)
{
if (!_gum_interceptor_backend_claim_grafted_trampoline (self->backend, ctx))
goto policy_violation;
}
else
{
if (!_gum_interceptor_backend_create_trampoline (self->backend, ctx))
goto wrong_signature;
}
g_hash_table_insert (self->function_by_address, function_address, ctx);
gum_interceptor_transaction_schedule_update (&self->current_transaction, ctx,
gum_interceptor_activate);
return ctx;
policy_violation:
{
*error = GUM_INSTRUMENTATION_ERROR_POLICY_VIOLATION;
goto propagate_error;
}
wrong_signature:
{
*error = GUM_INSTRUMENTATION_ERROR_WRONG_SIGNATURE;
goto propagate_error;
}
propagate_error:
{
gum_function_context_finalize (ctx);
return NULL;
}
}
static void
gum_interceptor_activate (GumInterceptor * self,
GumFunctionContext * ctx,
gpointer prologue)
{
if (ctx->destroyed)
return;
g_assert (!ctx->activated);
ctx->activated = TRUE;
_gum_interceptor_backend_activate_trampoline (self->backend, ctx,
prologue);
}
static void
gum_interceptor_deactivate (GumInterceptor * self,
GumFunctionContext * ctx,
gpointer prologue)
{
GumInterceptorBackend * backend = self->backend;
g_assert (ctx->activated);
ctx->activated = FALSE;
_gum_interceptor_backend_deactivate_trampoline (backend, ctx, prologue);
}
static void
gum_interceptor_transaction_init (GumInterceptorTransaction * transaction,
GumInterceptor * interceptor)
{
transaction->is_dirty = FALSE;
transaction->level = 0;
transaction->pending_destroy_tasks = g_queue_new ();
transaction->pending_update_tasks = g_hash_table_new_full (
NULL, NULL, NULL, (GDestroyNotify) g_array_unref);
transaction->interceptor = interceptor;
}
static void
gum_interceptor_transaction_destroy (GumInterceptorTransaction * transaction)
{
GumDestroyTask * task;
g_hash_table_unref (transaction->pending_update_tasks);