-
Notifications
You must be signed in to change notification settings - Fork 51
/
memprof.c
1864 lines (1505 loc) · 46.2 KB
/
memprof.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
/*
+----------------------------------------------------------------------+
| Memprof |
+----------------------------------------------------------------------+
| Copyright (c) 2012-2013 Arnaud Le Blanc |
+----------------------------------------------------------------------+
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the conditions mentioned |
| in the accompanying LICENSE file are met. |
+----------------------------------------------------------------------+
| Author: Arnaud Le Blanc <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "SAPI.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_memprof.h"
#include "zend_extensions.h"
#include "zend_exceptions.h"
#include <stdint.h>
#include <sys/queue.h>
#include "util.h"
#include <Judy.h>
#if MEMPROF_DEBUG
# undef NDEBUG
#endif
#include <assert.h>
#if PHP_VERSION_ID < 80000
#include "memprof_legacy_arginfo.h"
#else
#include "memprof_arginfo.h"
#endif
#define MEMPROF_ENV_PROFILE "MEMPROF_PROFILE"
#define MEMPROF_FLAG_NATIVE "native"
#define MEMPROF_FLAG_DUMP_ON_LIMIT "dump_on_limit"
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#ifdef ZTS
/* TODO: support ZTS builds (without malloc hooks) */
# error "ZTS build not supported (yet)"
#endif
#if MEMPROF_CONFIGURE_VERSION != 3
# error Please rebuild configure (run phpize and reconfigure)
#endif
#if HAVE_MALLOC_HOOKS
# include <malloc.h>
# if MEMPROF_DEBUG
# define MALLOC_HOOK_CHECK_NOT_OWN() \
if (__malloc_hook == malloc_hook) { \
fprintf(stderr, "__malloc_hook == malloc_hook (set at %s:%d)", malloc_hook_file, malloc_hook_line); \
abort(); \
} \
# define MALLOC_HOOK_CHECK_OWN() \
if (__malloc_hook != malloc_hook) { \
fprintf(stderr, "__malloc_hook != malloc_hook"); \
abort(); \
} \
# define MALLOC_HOOK_SET_FILE_LINE() do { \
malloc_hook_file = __FILE__; \
malloc_hook_line = __LINE__; \
} while (0)
# else /* MEMPROF_DEBUG */
# define MALLOC_HOOK_CHECK_NOT_OWN()
# define MALLOC_HOOK_CHECK_OWN()
# define MALLOC_HOOK_SET_FILE_LINE()
# endif /* MEMPROF_DEBUG */
# define WITHOUT_MALLOC_HOOKS \
do { \
int ___malloc_hook_restored = 0; \
if (__malloc_hook == malloc_hook) { \
MALLOC_HOOK_RESTORE_OLD(); \
___malloc_hook_restored = 1; \
} \
do \
# define END_WITHOUT_MALLOC_HOOKS \
while (0); \
if (___malloc_hook_restored) { \
MALLOC_HOOK_SAVE_OLD(); \
MALLOC_HOOK_SET_OWN(); \
} \
} while (0)
# define MALLOC_HOOK_RESTORE_OLD() \
/* Restore all old hooks */ \
MALLOC_HOOK_CHECK_OWN(); \
__malloc_hook = old_malloc_hook; \
__free_hook = old_free_hook; \
__realloc_hook = old_realloc_hook; \
__memalign_hook = old_memalign_hook; \
# define MALLOC_HOOK_SAVE_OLD() \
/* Save underlying hooks */ \
MALLOC_HOOK_CHECK_NOT_OWN(); \
old_malloc_hook = __malloc_hook; \
old_free_hook = __free_hook; \
old_realloc_hook = __realloc_hook; \
old_memalign_hook = __memalign_hook; \
# define MALLOC_HOOK_SET_OWN() \
/* Restore our own hooks */ \
__malloc_hook = malloc_hook; \
__free_hook = free_hook; \
__realloc_hook = realloc_hook; \
__memalign_hook = memalign_hook; \
MALLOC_HOOK_SET_FILE_LINE();
#else /* HAVE_MALLOC_HOOKS */
# define MALLOC_HOOK_CHECK_NOT_OWN()
# define MALLOC_HOOK_IS_SET() (__malloc_hook == malloc_hook)
# define MALLOC_HOOK_RESTORE_OLD()
# define MALLOC_HOOK_SAVE_OLD()
# define MALLOC_HOOK_SET_OWN()
# define WITHOUT_MALLOC_HOOKS \
do { \
do
# define END_WITHOUT_MALLOC_HOOKS \
while (0); \
} while (0);
#endif /* HAVE_MALLOC_HOOKS */
#define MEMORY_LIMIT_ERROR_PREFIX "Allowed memory size of"
typedef LIST_HEAD(_alloc_list_head, _alloc) alloc_list_head;
/* a call frame */
typedef struct _frame {
char * fn_name;
size_t fn_name_len;
char * file_name;
size_t file_name_len;
struct _frame * prev;
size_t calls;
HashTable next_cache;
alloc_list_head allocs;
} frame;
/* an allocated block's infos */
typedef struct _alloc {
#if MEMPROF_DEBUG
size_t canary_a;
#endif
LIST_ENTRY(_alloc) list;
size_t size;
#if MEMPROF_DEBUG
size_t canary_b;
#endif
} alloc;
typedef union _alloc_bucket_item {
alloc alloc;
union _alloc_bucket_item * next_free;
} alloc_bucket_item;
typedef struct _alloc_buckets {
size_t growsize;
size_t nbuckets;
alloc_bucket_item * next_free;
alloc_bucket_item ** buckets;
} alloc_buckets;
static zend_bool dump_callgrind(php_stream * stream);
static zend_bool dump_pprof(php_stream * stream);
static ZEND_DECLARE_MODULE_GLOBALS(memprof)
#if HAVE_MALLOC_HOOKS
static void * malloc_hook(size_t size, const void *caller);
static void * realloc_hook(void *ptr, size_t size, const void *caller);
static void free_hook(void *ptr, const void *caller);
static void * memalign_hook(size_t alignment, size_t size, const void *caller);
#if MEMPROF_DEBUG
static int malloc_hook_line = 0;
static const char * malloc_hook_file = NULL;
#endif
static void * (*old_malloc_hook) (size_t size, const void *caller) = NULL;
static void * (*old_realloc_hook) (void *ptr, size_t size, const void *caller) = NULL;
static void (*old_free_hook) (void *ptr, const void *caller) = NULL;
static void * (*old_memalign_hook) (size_t alignment, size_t size, const void *caller) = NULL;
#endif /* HAVE_MALLOC_HOOKS */
static void (*old_zend_execute)(zend_execute_data *execute_data);
static void (*old_zend_execute_internal)(zend_execute_data *execute_data_ptr, zval *return_value);
#define zend_execute_fn zend_execute_ex
#if PHP_VERSION_ID < 70200 /* PHP 7.1 */
# define MEMPROF_ZEND_ERROR_CB_ARGS int type, const char *error_filename, const uint error_lineno, const char *format, va_list args
# define MEMPROF_ZEND_ERROR_CB_ARGS_PASSTHRU type, error_filename, error_lineno, format, args
#elif PHP_VERSION_ID < 80000 /* PHP 7.2 - 7.4 */
# define MEMPROF_ZEND_ERROR_CB_ARGS int type, const char *error_filename, const uint32_t error_lineno, const char *format, va_list args
# define MEMPROF_ZEND_ERROR_CB_ARGS_PASSTHRU type, error_filename, error_lineno, format, args
#elif PHP_VERSION_ID < 80100 /* PHP 8.0 */
# define MEMPROF_ZEND_ERROR_CB_ARGS int type, const char *error_filename, const uint32_t error_lineno, zend_string *message
# define MEMPROF_ZEND_ERROR_CB_ARGS_PASSTHRU type, error_filename, error_lineno, message
#else /* PHP 8.1 */
# define MEMPROF_ZEND_ERROR_CB_ARGS int type, zend_string *error_filename, const uint32_t error_lineno, zend_string *message
# define MEMPROF_ZEND_ERROR_CB_ARGS_PASSTHRU type, error_filename, error_lineno, message
#endif
static void (*old_zend_error_cb)(MEMPROF_ZEND_ERROR_CB_ARGS);
static void (*rinit_zend_error_cb)(MEMPROF_ZEND_ERROR_CB_ARGS);
static zend_bool zend_error_cb_overridden;
static void memprof_zend_error_cb(MEMPROF_ZEND_ERROR_CB_ARGS);
static PHP_INI_MH((*origOnChangeMemoryLimit)) = NULL;
static int memprof_dumped = 0;
static int track_mallocs = 0;
static frame root_frame;
static frame * current_frame;
static alloc_list_head * current_alloc_list;
static alloc_buckets current_alloc_buckets;
static Pvoid_t allocs_set = (Pvoid_t) NULL;
static const size_t zend_mm_heap_size = 4096;
static zend_mm_heap * zheap = NULL;
static zend_mm_heap * orig_zheap = NULL;
#define ALLOC_INIT(alloc, size) alloc_init(alloc, size)
#define ALLOC_LIST_INSERT_HEAD(head, elem) alloc_list_insert_head(head, elem)
#define ALLOC_LIST_REMOVE(elem) alloc_list_remove(elem)
ZEND_NORETURN static void out_of_memory(void) {
fprintf(stderr, "memprof: System out of memory, try lowering memory_limit\n");
exit(1);
}
static inline void * malloc_check(size_t size) {
void * ptr = malloc(size);
if (UNEXPECTED(ptr == NULL)) {
out_of_memory();
}
return ptr;
}
static inline void * realloc_check(void * ptr, size_t size) {
void * newptr = realloc(ptr, size);
if (UNEXPECTED(newptr == NULL)) {
out_of_memory();
}
return newptr;
}
ZEND_NORETURN static void int_overflow(void) {
fprintf(stderr, "memprof: Integer overflow in memory allocation, try lowering memory_limit\n");
exit(1);
}
static inline size_t safe_size(size_t nmemb, size_t size, size_t offset) {
size_t r = nmemb * size;
if (UNEXPECTED(nmemb != 0 && r / nmemb != size)) {
int_overflow();
}
if (UNEXPECTED(SIZE_MAX - r < offset)) {
int_overflow();
}
return r + offset;
}
static inline void alloc_init(alloc * alloc, size_t size) {
alloc->size = size;
alloc->list.le_next = NULL;
alloc->list.le_prev = NULL;
#if MEMPROF_DEBUG
alloc->canary_a = alloc->canary_b = size ^ 0x5a5a5a5a;
#endif
}
static void alloc_list_insert_head(alloc_list_head * head, alloc * elem) {
LIST_INSERT_HEAD(head, elem, list);
}
static void list_remove(alloc * elm) {
LIST_REMOVE(elm, list);
}
static void alloc_list_remove(alloc * elem) {
if (elem->list.le_prev || elem->list.le_next) {
list_remove(elem);
elem->list.le_next = NULL;
elem->list.le_prev = NULL;
}
}
#if MEMPROF_DEBUG
static void alloc_check_single(alloc * alloc, const char * function, int line) {
if (alloc->canary_a != (alloc->size ^ 0x5a5a5a5a) || alloc->canary_a != alloc->canary_b) {
fprintf(stderr, "canary mismatch for %p at %s:%d\n", alloc, function, line);
abort();
}
}
static void alloc_check(alloc * alloc, const char * function, int line) {
/* fprintf(stderr, "checking %p at %s:%d\n", alloc, function, line); */
alloc_check_single(alloc, function, line);
/*
for (alloc = current_alloc_list->lh_first; alloc; alloc = alloc->list.le_next) {
alloc_check_single(alloc, function, line);
}
*/
}
# define ALLOC_CHECK(alloc) alloc_check(alloc, __FUNCTION__, __LINE__);
#else /* MEMPROF_DEBUG */
# define ALLOC_CHECK(alloc)
#endif /* MEMPROF_DEBUG */
static void alloc_buckets_destroy(alloc_buckets * buckets)
{
size_t i;
for (i = 0; i < buckets->nbuckets; ++i) {
free(buckets->buckets[i]);
}
#if MEMPROF_DEBUG
memset(buckets->buckets, 0x5a, buckets->nbuckets * sizeof(buckets->buckets[0]));
#endif
free(buckets->buckets);
#if MEMPROF_DEBUG
memset(buckets, 0x5a, sizeof(*buckets));
#endif
}
static void alloc_buckets_grow(alloc_buckets * buckets)
{
size_t i;
alloc_bucket_item * bucket;
buckets->nbuckets++;
buckets->buckets = realloc_check(buckets->buckets, safe_size(buckets->nbuckets, sizeof(*buckets->buckets), 0));
buckets->growsize = safe_size(2, buckets->growsize, 0);
bucket = malloc_check(safe_size(buckets->growsize, sizeof(*bucket), 0));
buckets->buckets[buckets->nbuckets-1] = bucket;
for (i = 1; i < buckets->growsize; ++i) {
bucket[i-1].next_free = &bucket[i];
}
bucket[buckets->growsize-1].next_free = buckets->next_free;
buckets->next_free = &bucket[0];
}
static void alloc_buckets_init(alloc_buckets * buckets)
{
buckets->growsize = 128;
buckets->nbuckets = 0;
buckets->buckets = NULL;
buckets->next_free = NULL;
alloc_buckets_grow(buckets);
}
static alloc * alloc_buckets_alloc(alloc_buckets * buckets, size_t size)
{
alloc_bucket_item * item = buckets->next_free;
if (item == NULL) {
alloc_buckets_grow(buckets);
item = buckets->next_free;
}
buckets->next_free = item->next_free;
ALLOC_INIT(&item->alloc, size);
return &item->alloc;
}
static void alloc_buckets_free(alloc_buckets * buckets, alloc * a)
{
alloc_bucket_item * item;
item = (alloc_bucket_item*) a;
item->next_free = buckets->next_free;
buckets->next_free = item;
}
static void destroy_frame(frame * f)
{
alloc * a;
#if MEMPROF_DEBUG
memset(f->file_name, 0x5a, f->file_name_len);
memset(f->fn_name, 0x5a, f->fn_name_len);
#endif
free(f->file_name);
free(f->fn_name);
while (f->allocs.lh_first) {
a = f->allocs.lh_first;
ALLOC_CHECK(a);
ALLOC_LIST_REMOVE(a);
}
zend_hash_destroy(&f->next_cache);
#if MEMPROF_DEBUG
memset(f, 0x5a, sizeof(*f));
#endif
}
/* HashTable destructor */
static void frame_dtor(zval * pDest)
{
frame * f = Z_PTR_P(pDest);
destroy_frame(f);
free(f);
}
static void init_frame(frame * f, frame * prev, char *file_name, size_t file_name_len, char * fn_name, size_t fn_name_len)
{
zend_hash_init(&f->next_cache, 0, NULL, frame_dtor, 0);
f->file_name = malloc_check(safe_size(1, file_name_len, 1));
memcpy(f->file_name, file_name, file_name_len+1);
f->file_name_len = file_name_len;
f->fn_name = malloc_check(safe_size(1, fn_name_len, 1));
memcpy(f->fn_name, fn_name, fn_name_len+1);
f->fn_name_len = fn_name_len;
f->calls = 0;
f->prev = prev;
LIST_INIT(&f->allocs);
}
static frame * new_frame(frame * prev, char *file_name, size_t file_name_len, char * fn_name, size_t fn_name_len)
{
frame * f = malloc_check(sizeof(*f));
init_frame(f, prev, file_name, file_name_len, fn_name, fn_name_len);
return f;
}
static frame * get_or_create_frame(zend_execute_data * current_execute_data, frame * prev)
{
frame * f;
char file_name[256];
char fn_name[256];
size_t file_name_len;
size_t fn_name_len;
file_name_len = get_file_name(current_execute_data, file_name, sizeof(file_name));
fn_name_len = get_function_name(current_execute_data, fn_name, sizeof(fn_name));
if (prev == NULL) {
return new_frame(prev, file_name, file_name_len, fn_name, fn_name_len);
}
f = zend_hash_str_find_ptr(&prev->next_cache, fn_name, fn_name_len);
if (f == NULL) {
f = new_frame(prev, file_name, file_name_len, fn_name, fn_name_len);
zend_hash_str_add_ptr(&prev->next_cache, fn_name, fn_name_len, f);
}
return f;
}
static size_t frame_alloc_size(const frame * f)
{
size_t size = 0;
alloc * alloc;
LIST_FOREACH(alloc, &f->allocs, list) {
size += alloc->size;
}
return size;
}
static int frame_stack_depth(const frame * f)
{
const frame * prev;
int depth = 0;
for (prev = f; prev != &root_frame; prev = prev->prev) {
depth ++;
}
return depth;
}
static void mark_own_alloc(Pvoid_t * set, void * ptr, alloc * a)
{
Word_t * p;
JLI(p, *set, (Word_t)ptr);
*p = (Word_t) a;
}
static void unmark_own_alloc(Pvoid_t * set, void * ptr)
{
int ret;
MALLOC_HOOK_CHECK_NOT_OWN();
JLD(ret, *set, (Word_t)ptr);
}
alloc * is_own_alloc(Pvoid_t * set, void * ptr)
{
Word_t * p;
MALLOC_HOOK_CHECK_NOT_OWN();
JLG(p, *set, (Word_t)ptr);
if (p != NULL) {
return (alloc*) *p;
} else {
return 0;
}
}
#if HAVE_MALLOC_HOOKS
static void * malloc_hook(size_t size, const void *caller)
{
void *result;
WITHOUT_MALLOC_HOOKS {
result = malloc_check(size);
if (result != NULL) {
alloc * a = alloc_buckets_alloc(¤t_alloc_buckets, size);
if (track_mallocs) {
ALLOC_LIST_INSERT_HEAD(current_alloc_list, a);
}
mark_own_alloc(&allocs_set, result, a);
assert(is_own_alloc(&allocs_set, result));
}
} END_WITHOUT_MALLOC_HOOKS;
return result;
}
static void * realloc_hook(void *ptr, size_t size, const void *caller)
{
void *result;
alloc *a;
WITHOUT_MALLOC_HOOKS {
if (ptr != NULL && !(a = is_own_alloc(&allocs_set, ptr))) {
result = realloc(ptr, size);
} else {
/* ptr may be freed by realloc, so we must remove it from list now */
if (ptr != NULL) {
ALLOC_CHECK(a);
ALLOC_LIST_REMOVE(a);
unmark_own_alloc(&allocs_set, ptr);
alloc_buckets_free(¤t_alloc_buckets, a);
}
result = realloc(ptr, size);
if (result != NULL) {
/* succeeded; add result */
a = alloc_buckets_alloc(¤t_alloc_buckets, size);
if (track_mallocs) {
ALLOC_LIST_INSERT_HEAD(current_alloc_list, a);
}
mark_own_alloc(&allocs_set, result, a);
} else if (ptr != NULL) {
/* failed, re-add ptr, since it hasn't been freed */
a = alloc_buckets_alloc(¤t_alloc_buckets, size);
if (track_mallocs) {
ALLOC_LIST_INSERT_HEAD(current_alloc_list, a);
}
mark_own_alloc(&allocs_set, ptr, a);
}
}
} END_WITHOUT_MALLOC_HOOKS;
return result;
}
static void free_hook(void *ptr, const void *caller)
{
WITHOUT_MALLOC_HOOKS {
if (ptr != NULL) {
alloc * a;
if ((a = is_own_alloc(&allocs_set, ptr))) {
ALLOC_CHECK(a);
ALLOC_LIST_REMOVE(a);
free(ptr);
unmark_own_alloc(&allocs_set, ptr);
alloc_buckets_free(¤t_alloc_buckets, a);
} else {
free(ptr);
}
}
} END_WITHOUT_MALLOC_HOOKS;
}
static void * memalign_hook(size_t alignment, size_t size, const void *caller)
{
void * result;
WITHOUT_MALLOC_HOOKS {
result = memalign(alignment, size);
if (result != NULL) {
alloc *a = alloc_buckets_alloc(¤t_alloc_buckets, size);
if (track_mallocs) {
ALLOC_LIST_INSERT_HEAD(current_alloc_list, a);
}
mark_own_alloc(&allocs_set, result, a);
}
} END_WITHOUT_MALLOC_HOOKS;
return result;
}
#endif /* HAVE_MALLOC_HOOKS */
#define WITHOUT_MALLOC_TRACKING do { \
int ___old_track_mallocs = track_mallocs; \
track_mallocs = 0; \
do
#define END_WITHOUT_MALLOC_TRACKING \
while (0); \
track_mallocs = ___old_track_mallocs; \
} while (0)
static void * zend_malloc_handler(size_t size)
{
void *result;
assert(MEMPROF_G(profile_flags).enabled);
WITHOUT_MALLOC_HOOKS {
result = zend_mm_alloc(orig_zheap, size);
if (result != NULL) {
alloc * a = alloc_buckets_alloc(¤t_alloc_buckets, size);
if (track_mallocs) {
ALLOC_LIST_INSERT_HEAD(current_alloc_list, a);
}
mark_own_alloc(&allocs_set, result, a);
assert(is_own_alloc(&allocs_set, result));
}
} END_WITHOUT_MALLOC_HOOKS;
return result;
}
static void zend_free_handler(void * ptr)
{
assert(MEMPROF_G(profile_flags).enabled);
WITHOUT_MALLOC_HOOKS {
if (ptr != NULL) {
alloc * a;
if ((a = is_own_alloc(&allocs_set, ptr))) {
ALLOC_CHECK(a);
ALLOC_LIST_REMOVE(a);
zend_mm_free(orig_zheap, ptr);
unmark_own_alloc(&allocs_set, ptr);
alloc_buckets_free(¤t_alloc_buckets, a);
} else {
zend_mm_free(orig_zheap, ptr);
}
}
} END_WITHOUT_MALLOC_HOOKS;
}
static void * zend_realloc_handler(void * ptr, size_t size)
{
void *result;
alloc *a;
assert(MEMPROF_G(profile_flags).enabled);
WITHOUT_MALLOC_HOOKS {
if (ptr != NULL && !(a = is_own_alloc(&allocs_set, ptr))) {
result = zend_mm_realloc(orig_zheap, ptr, size);
} else {
/* ptr may be freed by realloc, so we must remove it from list now */
if (ptr != NULL) {
ALLOC_CHECK(a);
ALLOC_LIST_REMOVE(a);
unmark_own_alloc(&allocs_set, ptr);
alloc_buckets_free(¤t_alloc_buckets, a);
}
result = zend_mm_realloc(orig_zheap, ptr, size);
if (result != NULL) {
/* succeeded; add result */
a = alloc_buckets_alloc(¤t_alloc_buckets, size);
if (track_mallocs) {
ALLOC_LIST_INSERT_HEAD(current_alloc_list, a);
}
mark_own_alloc(&allocs_set, result, a);
} else if (ptr != NULL) {
/* failed, re-add ptr, since it hasn't been freed */
a = alloc_buckets_alloc(¤t_alloc_buckets, size);
if (track_mallocs) {
ALLOC_LIST_INSERT_HEAD(current_alloc_list, a);
}
mark_own_alloc(&allocs_set, ptr, a);
}
}
} END_WITHOUT_MALLOC_HOOKS;
return result;
}
// Some extensions override zend_error_cb and don't call the previous
// zend_error_cb, so memprof needs to be the last to override it
static void memprof_late_override_error_cb(void) {
old_zend_error_cb = zend_error_cb;
zend_error_cb = memprof_zend_error_cb;
zend_error_cb_overridden = 1;
}
static void memprof_zend_execute(zend_execute_data *execute_data)
{
if (UNEXPECTED(!zend_error_cb_overridden)) {
memprof_late_override_error_cb();
}
WITHOUT_MALLOC_TRACKING {
current_frame = get_or_create_frame(execute_data, current_frame);
current_frame->calls++;
current_alloc_list = ¤t_frame->allocs;
} END_WITHOUT_MALLOC_TRACKING;
old_zend_execute(execute_data);
if (MEMPROF_G(profile_flags).enabled) {
current_frame = current_frame->prev;
current_alloc_list = ¤t_frame->allocs;
}
}
static void memprof_zend_execute_internal(zend_execute_data *execute_data_ptr, zval *return_value)
{
int ignore = 0;
if (UNEXPECTED(!zend_error_cb_overridden)) {
memprof_late_override_error_cb();
}
if (&execute_data_ptr->func->internal_function == &zend_pass_function) {
ignore = 1;
} else if (execute_data_ptr->func->common.function_name) {
zend_string * name = execute_data_ptr->func->common.function_name;
if (ZSTR_LEN(name) == sizeof("call_user_func")-1
&& 0 == memcmp(name, "call_user_func", sizeof("call_user_func")))
{
ignore = 1;
} else if (ZSTR_LEN(name) == sizeof("call_user_func_array")-1
&& 0 == memcmp(name, "call_user_func_array", sizeof("call_user_func_array")))
{
ignore = 1;
}
}
WITHOUT_MALLOC_TRACKING {
if (!ignore) {
current_frame = get_or_create_frame(execute_data_ptr, current_frame);
current_frame->calls++;
current_alloc_list = ¤t_frame->allocs;
}
} END_WITHOUT_MALLOC_TRACKING;
if (!old_zend_execute_internal) {
execute_internal(execute_data_ptr, return_value);
} else {
old_zend_execute_internal(execute_data_ptr, return_value);
}
if (!ignore && MEMPROF_G(profile_flags).enabled) {
current_frame = current_frame->prev;
current_alloc_list = ¤t_frame->allocs;
}
}
static zend_bool should_autodump(int error_type, const char *message) {
if (EXPECTED(error_type != E_ERROR)) {
return 0;
}
if (EXPECTED(!MEMPROF_G(profile_flags).dump_on_limit)) {
return 0;
}
if (EXPECTED(strncmp(MEMORY_LIMIT_ERROR_PREFIX, message, strlen(MEMORY_LIMIT_ERROR_PREFIX)) != 0)) {
return 0;
}
return 1;
}
static char * generate_filename(const char * format) {
char * filename;
struct timeval tv;
uint64_t ts;
const char * output_dir = MEMPROF_G(output_dir);
char slash[] = "\0";
gettimeofday(&tv, NULL);
ts = ((uint64_t) tv.tv_sec) * 0x100000 + (((uint64_t) tv.tv_usec) % 0x100000);
if (!IS_SLASH(output_dir[strlen(output_dir)-1])) {
slash[0] = DEFAULT_SLASH;
}
spprintf(&filename, 0, "%s%smemprof.%s.%" PRIu64, output_dir, slash, format, ts);
return filename;
}
static void memprof_zend_error_cb_dump(MEMPROF_ZEND_ERROR_CB_ARGS)
{
char * filename = NULL;
php_stream * stream;
zend_bool error = 0;
#if PHP_VERSION_ID < 80000
const char * message_chr = format;
#else
const char * message_chr = ZSTR_VAL(message);
#endif
zend_string * new_message = NULL;
zend_mm_set_heap(orig_zheap);
zend_set_memory_limit((size_t)Z_L(-1) >> (size_t)Z_L(1));
zend_mm_set_heap(zheap);
WITHOUT_MALLOC_TRACKING {
if (MEMPROF_G(output_format) == FORMAT_CALLGRIND) {
filename = generate_filename(MEMPROF_OUTPUT_FORMAT_CALLGRIND);
stream = php_stream_open_wrapper_ex(filename, "w", 0, NULL, NULL);
if (stream != NULL) {
error = !dump_callgrind(stream);
php_stream_free(stream, PHP_STREAM_FREE_CLOSE);
} else {
error = 1;
}
} else if (MEMPROF_G(output_format) == FORMAT_PPROF) {
filename = generate_filename(MEMPROF_OUTPUT_FORMAT_PPROF);
stream = php_stream_open_wrapper_ex(filename, "w", 0, NULL, NULL);
if (stream != NULL) {
error = !dump_pprof(stream);
php_stream_free(stream, PHP_STREAM_FREE_CLOSE);
} else {
error = 1;
}
}
if (filename != NULL) {
if (error == 0) {
new_message = strpprintf(0, "%s (memprof dumped to %s)", message_chr, filename);
} else {
new_message = strpprintf(0, "%s (memprof failed dumping to %s, please check file permissions or disk capacity)", message_chr, filename);
}
efree(filename);
}
if (new_message != NULL) {
#if PHP_VERSION_ID < 80000
format = ZSTR_VAL(new_message);
#else
message = new_message;
#endif
}
} END_WITHOUT_MALLOC_TRACKING;
zend_mm_set_heap(orig_zheap);
zend_set_memory_limit(PG(memory_limit));
zend_mm_set_heap(zheap);
old_zend_error_cb(MEMPROF_ZEND_ERROR_CB_ARGS_PASSTHRU);
WITHOUT_MALLOC_TRACKING {
if (new_message != NULL) {
zend_string_free(new_message);
}
} END_WITHOUT_MALLOC_TRACKING;
}
static void memprof_zend_error_cb(MEMPROF_ZEND_ERROR_CB_ARGS)
{
#if PHP_VERSION_ID < 80000
const char * message_chr = format;
#else
const char * message_chr = ZSTR_VAL(message);
#endif
if (EXPECTED(!MEMPROF_G(profile_flags).enabled)) {
old_zend_error_cb(MEMPROF_ZEND_ERROR_CB_ARGS_PASSTHRU);
return;
}
if (EXPECTED(!should_autodump(type, message_chr))) {
old_zend_error_cb(MEMPROF_ZEND_ERROR_CB_ARGS_PASSTHRU);
return;
}
return memprof_zend_error_cb_dump(MEMPROF_ZEND_ERROR_CB_ARGS_PASSTHRU);
}
static PHP_INI_MH(OnChangeMemoryLimit)
{
int ret;
if (!origOnChangeMemoryLimit) {
return FAILURE;
}
ret = origOnChangeMemoryLimit(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
if (ret != SUCCESS) {
return ret;
}
if (MEMPROF_G(profile_flags).enabled && orig_zheap) {
zend_mm_set_heap(orig_zheap);
zend_set_memory_limit(PG(memory_limit));
zend_mm_set_heap(zheap);
}
return SUCCESS;
}
static void memprof_enable(memprof_profile_flags * pf)
{
assert(pf->enabled);
alloc_buckets_init(¤t_alloc_buckets);
init_frame(&root_frame, &root_frame, "", 0, "root", sizeof("root")-1);
root_frame.calls = 1;
current_frame = &root_frame;
current_alloc_list = &root_frame.allocs;
if (pf->native) {
MALLOC_HOOK_SAVE_OLD();
MALLOC_HOOK_SET_OWN();
}
memprof_dumped = 0;
if (is_zend_mm()) {
/* There is no way to completely free a zend_mm_heap with custom
* handlers, so we have to allocate it ourselves. We don't know the
* actual size of a _zend_mm_heap struct, but this should be enough. */
zheap = malloc_check(zend_mm_heap_size);
memset(zheap, 0, zend_mm_heap_size);
zend_mm_set_custom_handlers(zheap, zend_malloc_handler, zend_free_handler, zend_realloc_handler);
orig_zheap = zend_mm_set_heap(zheap);
} else {
zheap = NULL;
orig_zheap = NULL;
}
old_zend_execute = zend_execute_fn;
old_zend_execute_internal = zend_execute_internal;
zend_execute_fn = memprof_zend_execute;
zend_execute_internal = memprof_zend_execute_internal;
track_mallocs = 1;
}