-
Notifications
You must be signed in to change notification settings - Fork 32
/
ffi.c
6713 lines (6106 loc) · 203 KB
/
ffi.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
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2018 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Dmitry Stogov <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "php_ffi.h"
#include "ext/standard/info.h"
#include "php_scandir.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
#include "zend_closures.h"
#include "main/SAPI.h"
#include <ffi.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
ZEND_DECLARE_MODULE_GLOBALS(ffi)
typedef enum _zend_ffi_tag_kind {
ZEND_FFI_TAG_ENUM,
ZEND_FFI_TAG_STRUCT,
ZEND_FFI_TAG_UNION
} zend_ffi_tag_kind;
static const char *zend_ffi_tag_kind_name[3] = {"enum", "struct", "union"};
typedef struct _zend_ffi_tag {
zend_ffi_tag_kind kind;
zend_ffi_type *type;
} zend_ffi_tag;
typedef enum _zend_ffi_type_kind {
ZEND_FFI_TYPE_VOID,
ZEND_FFI_TYPE_FLOAT,
ZEND_FFI_TYPE_DOUBLE,
#ifdef HAVE_LONG_DOUBLE
ZEND_FFI_TYPE_LONGDOUBLE,
#endif
ZEND_FFI_TYPE_UINT8,
ZEND_FFI_TYPE_SINT8,
ZEND_FFI_TYPE_UINT16,
ZEND_FFI_TYPE_SINT16,
ZEND_FFI_TYPE_UINT32,
ZEND_FFI_TYPE_SINT32,
ZEND_FFI_TYPE_UINT64,
ZEND_FFI_TYPE_SINT64,
ZEND_FFI_TYPE_ENUM,
ZEND_FFI_TYPE_BOOL,
ZEND_FFI_TYPE_CHAR,
ZEND_FFI_TYPE_POINTER,
ZEND_FFI_TYPE_FUNC,
ZEND_FFI_TYPE_ARRAY,
ZEND_FFI_TYPE_STRUCT,
} zend_ffi_type_kind;
typedef enum _zend_ffi_flags {
ZEND_FFI_FLAG_CONST = (1 << 0),
ZEND_FFI_FLAG_OWNED = (1 << 1),
ZEND_FFI_FLAG_PERSISTENT = (1 << 2),
} zend_ffi_flags;
struct _zend_ffi_type {
zend_ffi_type_kind kind;
size_t size;
uint32_t align;
uint32_t attr;
union {
struct {
zend_ffi_type_kind kind;
} enumeration;
struct {
zend_ffi_type *type;
zend_long length;
} array;
struct {
zend_ffi_type *type;
} pointer;
struct {
HashTable fields;
} record;
struct {
zend_ffi_type *ret_type;
HashTable *args;
ffi_abi abi;
} func;
};
};
typedef struct _zend_ffi_field {
size_t offset;
zend_bool is_const;
zend_bool is_nested; /* part of nested anonymous struct */
uint8_t first_bit;
uint8_t bits;
zend_ffi_type *type;
} zend_ffi_field;
typedef enum _zend_ffi_symbol_kind {
ZEND_FFI_SYM_TYPE,
ZEND_FFI_SYM_CONST,
ZEND_FFI_SYM_VAR,
ZEND_FFI_SYM_FUNC
} zend_ffi_symbol_kind;
typedef struct _zend_ffi_symbol {
zend_ffi_symbol_kind kind;
zend_bool is_const;
zend_ffi_type *type;
union {
void *addr;
int64_t value;
};
} zend_ffi_symbol;
typedef struct _zend_ffi_scope {
HashTable *symbols;
HashTable *tags;
} zend_ffi_scope;
typedef struct _zend_ffi {
zend_object std;
DL_HANDLE lib;
HashTable *symbols;
HashTable *tags;
zend_bool persistent;
} zend_ffi;
#define ZEND_FFI_TYPE_OWNED (1<<0)
#define ZEND_FFI_TYPE(t) \
((zend_ffi_type*)(((uintptr_t)(t)) & ~ZEND_FFI_TYPE_OWNED))
#define ZEND_FFI_TYPE_IS_OWNED(t) \
(((uintptr_t)(t)) & ZEND_FFI_TYPE_OWNED)
#define ZEND_FFI_TYPE_MAKE_OWNED(t) \
((zend_ffi_type*)(((uintptr_t)(t)) | ZEND_FFI_TYPE_OWNED))
typedef struct _zend_ffi_cdata {
zend_object std;
zend_ffi_type *type;
void *ptr;
void *ptr_holder;
zend_ffi_flags flags;
} zend_ffi_cdata;
typedef struct _zend_ffi_ctype {
zend_object std;
zend_ffi_type *type;
} zend_ffi_ctype;
static zend_class_entry *zend_ffi_exception_ce;
static zend_class_entry *zend_ffi_parser_exception_ce;
static zend_class_entry *zend_ffi_ce;
static zend_class_entry *zend_ffi_cdata_ce;
static zend_class_entry *zend_ffi_ctype_ce;
static zend_object_handlers zend_ffi_handlers;
static zend_object_handlers zend_ffi_cdata_handlers;
static zend_object_handlers zend_ffi_cdata_value_handlers;
static zend_object_handlers zend_ffi_cdata_free_handlers;
static zend_object_handlers zend_ffi_ctype_handlers;
static zend_internal_function zend_ffi_new_fn;
static zend_internal_function zend_ffi_cast_fn;
static zend_internal_function zend_ffi_type_fn;
/* forward declarations */
static void _zend_ffi_type_dtor(zend_ffi_type *type);
static void zend_ffi_finalize_type(zend_ffi_dcl *dcl);
static int zend_ffi_is_same_type(zend_ffi_type *type1, zend_ffi_type *type2);
static zend_ffi_type *zend_ffi_remember_type(zend_ffi_type *type);
static char *zend_ffi_parse_directives(const char *filename, char *code_pos, char **scope_name, char **lib, zend_bool preload);
static ZEND_FUNCTION(ffi_trampoline);
#if FFI_CLOSURES
static void *zend_ffi_create_callback(zend_ffi_type *type, zval *value);
#endif
static zend_always_inline void zend_ffi_type_dtor(zend_ffi_type *type) /* {{{ */
{
if (UNEXPECTED(ZEND_FFI_TYPE_IS_OWNED(type))) {
_zend_ffi_type_dtor(type);
return;
}
}
/* }}} */
static zend_always_inline void zend_ffi_object_init(zend_object *object, zend_class_entry *ce) /* {{{ */
{
GC_SET_REFCOUNT(object, 1);
GC_TYPE_INFO(object) = IS_OBJECT | ((GC_COLLECTABLE | IS_OBJ_DESTRUCTOR_CALLED) << GC_FLAGS_SHIFT);
object->ce = ce;
object->properties = NULL;
zend_objects_store_put(object);
}
/* }}} */
static zend_object *zend_ffi_cdata_new(zend_class_entry *class_type) /* {{{ */
{
zend_ffi_cdata *cdata;
cdata = emalloc(sizeof(zend_ffi_cdata));
zend_ffi_object_init(&cdata->std, class_type);
cdata->std.handlers = &zend_ffi_cdata_handlers;
cdata->type = NULL;
cdata->ptr = NULL;
cdata->flags = 0;
return &cdata->std;
}
/* }}} */
static int zend_ffi_is_compatible_type(zend_ffi_type *dst_type, zend_ffi_type *src_type) /* {{{ */
{
while (1) {
if (dst_type == src_type) {
return 1;
} else if (dst_type->kind == src_type->kind) {
if (dst_type->kind < ZEND_FFI_TYPE_POINTER) {
return 1;
} else if (dst_type->kind == ZEND_FFI_TYPE_POINTER) {
dst_type = ZEND_FFI_TYPE(dst_type->pointer.type);
src_type = ZEND_FFI_TYPE(src_type->pointer.type);
if (dst_type->kind == ZEND_FFI_TYPE_VOID ||
src_type->kind == ZEND_FFI_TYPE_VOID) {
return 1;
}
} else if (dst_type->kind == ZEND_FFI_TYPE_ARRAY &&
(dst_type->array.length == src_type->array.length ||
dst_type->array.length == 0)) {
dst_type = ZEND_FFI_TYPE(dst_type->array.type);
src_type = ZEND_FFI_TYPE(src_type->array.type);
} else {
break;
}
} else if (dst_type->kind == ZEND_FFI_TYPE_POINTER &&
src_type->kind == ZEND_FFI_TYPE_ARRAY) {
dst_type = ZEND_FFI_TYPE(dst_type->pointer.type);
src_type = ZEND_FFI_TYPE(src_type->array.type);
if (dst_type->kind == ZEND_FFI_TYPE_VOID) {
return 1;
}
} else {
break;
}
}
return 0;
}
/* }}} */
static ffi_type *zend_ffi_make_fake_struct_type(zend_ffi_type *type) /* {{{ */
{
ffi_type *t = emalloc(sizeof(ffi_type) + sizeof(ffi_type*) * (zend_hash_num_elements(&type->record.fields) + 1));
int i;
zend_ffi_field *field;
t->size = type->size;
t->alignment = type->align;
t->type = FFI_TYPE_STRUCT;
t->elements = (ffi_type**)(t + 1);
i = 0;
ZEND_HASH_FOREACH_PTR(&type->record.fields, field) {
switch (ZEND_FFI_TYPE(field->type)->kind) {
case ZEND_FFI_TYPE_FLOAT:
t->elements[i] = &ffi_type_float;
break;
case ZEND_FFI_TYPE_DOUBLE:
t->elements[i] = &ffi_type_double;
break;
#ifndef PHP_WIN32
case ZEND_FFI_TYPE_LONGDOUBLE:
t->elements[i] = &ffi_type_longdouble;
break;
#endif
case ZEND_FFI_TYPE_SINT8:
case ZEND_FFI_TYPE_UINT8:
case ZEND_FFI_TYPE_BOOL:
case ZEND_FFI_TYPE_CHAR:
t->elements[i] = &ffi_type_uint8;
break;
case ZEND_FFI_TYPE_SINT16:
case ZEND_FFI_TYPE_UINT16:
t->elements[i] = &ffi_type_uint16;
break;
case ZEND_FFI_TYPE_SINT32:
case ZEND_FFI_TYPE_UINT32:
t->elements[i] = &ffi_type_uint32;
break;
case ZEND_FFI_TYPE_SINT64:
case ZEND_FFI_TYPE_UINT64:
t->elements[i] = &ffi_type_uint64;
break;
case ZEND_FFI_TYPE_POINTER:
t->elements[i] = &ffi_type_pointer;
break;
default:
efree(t);
zend_throw_error(zend_ffi_exception_ce, "Passing incompatible struct/union");
return NULL;
}
i++;
} ZEND_HASH_FOREACH_END();
t->elements[i] = NULL;
return t;
}
/* }}} */
static ffi_type *zend_ffi_get_type(zend_ffi_type *type) /* {{{ */
{
zend_ffi_type_kind kind = type->kind;
again:
switch (kind) {
case ZEND_FFI_TYPE_FLOAT:
return &ffi_type_float;
case ZEND_FFI_TYPE_DOUBLE:
return &ffi_type_double;
#ifndef PHP_WIN32
case ZEND_FFI_TYPE_LONGDOUBLE:
return &ffi_type_longdouble;
#endif
case ZEND_FFI_TYPE_UINT8:
return &ffi_type_uint8;
case ZEND_FFI_TYPE_SINT8:
return &ffi_type_sint8;
case ZEND_FFI_TYPE_UINT16:
return &ffi_type_uint16;
case ZEND_FFI_TYPE_SINT16:
return &ffi_type_sint16;
case ZEND_FFI_TYPE_UINT32:
return &ffi_type_uint32;
case ZEND_FFI_TYPE_SINT32:
return &ffi_type_sint32;
case ZEND_FFI_TYPE_UINT64:
return &ffi_type_uint64;
case ZEND_FFI_TYPE_SINT64:
return &ffi_type_sint64;
case ZEND_FFI_TYPE_POINTER:
return &ffi_type_pointer;
case ZEND_FFI_TYPE_VOID:
return &ffi_type_void;
case ZEND_FFI_TYPE_BOOL:
return &ffi_type_uint8;
case ZEND_FFI_TYPE_CHAR:
return &ffi_type_sint8;
case ZEND_FFI_TYPE_ENUM:
kind = type->enumeration.kind;
goto again;
case ZEND_FFI_TYPE_STRUCT:
if (!(type->attr & ZEND_FFI_ATTR_UNION)) {
ffi_type *t = zend_ffi_make_fake_struct_type(type);
if (t) {
return t;
}
}
zend_throw_error(zend_ffi_exception_ce, "FFI return struct/union is not implemented");
break;
case ZEND_FFI_TYPE_ARRAY:
zend_throw_error(zend_ffi_exception_ce, "FFI return array is not implemented");
break;
default:
zend_throw_error(zend_ffi_exception_ce, "FFI internal error");
break;
}
return NULL;
}
/* }}} */
static zend_never_inline zend_ffi_cdata *zend_ffi_cdata_to_zval_slow(void *ptr, zend_ffi_type *type, zend_ffi_flags flags) /* {{{ */
{
zend_ffi_cdata *cdata = emalloc(sizeof(zend_ffi_cdata));
zend_ffi_object_init(&cdata->std, zend_ffi_cdata_ce);
cdata->std.handlers =
(type->kind < ZEND_FFI_TYPE_POINTER) ?
&zend_ffi_cdata_value_handlers :
&zend_ffi_cdata_handlers;
cdata->type = type;
cdata->flags = flags;
cdata->ptr = ptr;
return cdata;
}
/* }}} */
static zend_never_inline zend_ffi_cdata *zend_ffi_cdata_to_zval_slow_ptr(void *ptr, zend_ffi_type *type, zend_ffi_flags flags) /* {{{ */
{
zend_ffi_cdata *cdata = emalloc(sizeof(zend_ffi_cdata));
zend_ffi_object_init(&cdata->std, zend_ffi_cdata_ce);
cdata->std.handlers = &zend_ffi_cdata_handlers;
cdata->type = type;
cdata->flags = flags;
cdata->ptr = (void*)&cdata->ptr_holder;
*(void**)cdata->ptr = *(void**)ptr;
return cdata;
}
/* }}} */
static zend_never_inline zend_ffi_cdata *zend_ffi_cdata_to_zval_slow_ret(void *ptr, zend_ffi_type *type, zend_ffi_flags flags) /* {{{ */
{
zend_ffi_cdata *cdata = emalloc(sizeof(zend_ffi_cdata));
zend_ffi_object_init(&cdata->std, zend_ffi_cdata_ce);
cdata->std.handlers =
(type->kind < ZEND_FFI_TYPE_POINTER) ?
&zend_ffi_cdata_value_handlers :
&zend_ffi_cdata_handlers;
cdata->type = type;
cdata->flags = flags;
if (type->kind == ZEND_FFI_TYPE_POINTER) {
cdata->ptr = (void*)&cdata->ptr_holder;
*(void**)cdata->ptr = *(void**)ptr;
} else if (type->kind == ZEND_FFI_TYPE_STRUCT) {
cdata->ptr = emalloc(type->size);
cdata->flags |= ZEND_FFI_FLAG_OWNED;
memcpy(cdata->ptr, ptr, type->size);
} else {
cdata->ptr = ptr;
}
return cdata;
}
/* }}} */
static zend_always_inline void zend_ffi_cdata_to_zval(zend_ffi_cdata *cdata, void *ptr, zend_ffi_type *type, int read_type, zval *rv, zend_ffi_flags flags, zend_bool is_ret) /* {{{ */
{
if (read_type == BP_VAR_R) {
zend_ffi_type_kind kind = type->kind;
again:
switch (kind) {
case ZEND_FFI_TYPE_FLOAT:
ZVAL_DOUBLE(rv, *(float*)ptr);
return;
case ZEND_FFI_TYPE_DOUBLE:
ZVAL_DOUBLE(rv, *(double*)ptr);
return;
#ifdef HAVE_LONG_DOUBLE
case ZEND_FFI_TYPE_LONGDOUBLE:
ZVAL_DOUBLE(rv, *(long double*)ptr);
return;
#endif
case ZEND_FFI_TYPE_UINT8:
ZVAL_LONG(rv, *(uint8_t*)ptr);
return;
case ZEND_FFI_TYPE_SINT8:
ZVAL_LONG(rv, *(int8_t*)ptr);
return;
case ZEND_FFI_TYPE_UINT16:
ZVAL_LONG(rv, *(uint16_t*)ptr);
return;
case ZEND_FFI_TYPE_SINT16:
ZVAL_LONG(rv, *(int16_t*)ptr);
return;
case ZEND_FFI_TYPE_UINT32:
ZVAL_LONG(rv, *(uint32_t*)ptr);
return;
case ZEND_FFI_TYPE_SINT32:
ZVAL_LONG(rv, *(int32_t*)ptr);
return;
case ZEND_FFI_TYPE_UINT64:
ZVAL_LONG(rv, *(uint64_t*)ptr);
return;
case ZEND_FFI_TYPE_SINT64:
ZVAL_LONG(rv, *(int64_t*)ptr);
return;
case ZEND_FFI_TYPE_BOOL:
ZVAL_BOOL(rv, *(uint8_t*)ptr);
return;
case ZEND_FFI_TYPE_CHAR:
ZVAL_INTERNED_STR(rv, ZSTR_CHAR(*(unsigned char*)ptr));
return;
case ZEND_FFI_TYPE_ENUM:
kind = type->enumeration.kind;
goto again;
case ZEND_FFI_TYPE_POINTER:
if (*(void**)ptr == NULL) {
ZVAL_NULL(rv);
return;
} else if ((type->attr & ZEND_FFI_ATTR_CONST) && ZEND_FFI_TYPE(type->pointer.type)->kind == ZEND_FFI_TYPE_CHAR) {
ZVAL_STRING(rv, *(char**)ptr);
return;
}
if (!cdata) {
if (is_ret) {
cdata = zend_ffi_cdata_to_zval_slow_ret(ptr, type, flags);
} else {
cdata = zend_ffi_cdata_to_zval_slow_ptr(ptr, type, flags);
}
} else {
GC_ADDREF(&cdata->std);
}
ZVAL_OBJ(rv, &cdata->std);
return;
default:
break;
}
}
if (!cdata) {
if (is_ret) {
cdata = zend_ffi_cdata_to_zval_slow_ret(ptr, type, flags);
} else {
cdata = zend_ffi_cdata_to_zval_slow(ptr, type, flags);
}
} else {
GC_ADDREF(&cdata->std);
}
ZVAL_OBJ(rv, &cdata->std);
}
/* }}} */
static void zend_ffi_bit_field_to_zval(void *ptr, zend_ffi_field *field, zval *rv) /* {{{ */
{
uint64_t *p1 = (uint64_t *)((char*)ptr + (field->first_bit / 64) * 8);
uint64_t *p2 = (uint64_t *)((char*)ptr + ((field->first_bit + field->bits - 1) / 64) * 8);
uint64_t pos = field->first_bit % 64;
uint64_t shift = 64 - (field->bits % 64);
uint64_t val;
if (p1 == p2) {
if (field->bits == 64) {
val = *p1;
shift = 0;
} else {
val = *p1 << (shift - pos);
}
} else {
val = (*p1 >> pos) | (*p2 << (64 - pos));
}
if (ZEND_FFI_TYPE(field->type)->kind == ZEND_FFI_TYPE_CHAR
|| ZEND_FFI_TYPE(field->type)->kind == ZEND_FFI_TYPE_SINT8
|| ZEND_FFI_TYPE(field->type)->kind == ZEND_FFI_TYPE_SINT16
|| ZEND_FFI_TYPE(field->type)->kind == ZEND_FFI_TYPE_SINT32
|| ZEND_FFI_TYPE(field->type)->kind == ZEND_FFI_TYPE_SINT64) {
val = (int64_t)val >> shift;
} else {
val = val >> shift;
}
ZVAL_LONG(rv, val);
}
/* }}} */
static int zend_ffi_zval_to_bit_field(void *ptr, zend_ffi_field *field, zval *value) /* {{{ */
{
uint64_t *p1 = (uint64_t *)((char*)ptr + (field->first_bit / 64) * 8);
uint64_t *p2 = (uint64_t *)((char*)ptr + ((field->first_bit + field->bits - 1) / (8 * 8)) * 8);
uint64_t pos = field->first_bit % 64;
uint64_t mask;
uint64_t val = zval_get_long(value);
if (p1 == p2) {
if (field->bits == 64) {
*p1 = val;
} else {
mask = ((1ULL << field->bits) - 1ULL) << pos;
*p1 = (*p1 & ~mask) | (val << pos) & mask;
}
} else {
mask = ((1ULL << (64 - pos)) - 1ULL) << pos;
*p1 = (*p1 & ~mask) | (val << pos) & mask;
mask = (1ULL << pos) - 1ULL;
*p2 = (*p2 & ~mask) | (val >> (64 - pos)) & mask;
}
return SUCCESS;
}
/* }}} */
static zend_always_inline int zend_ffi_zval_to_cdata(void *ptr, zend_ffi_type *type, zval *value) /* {{{ */
{
zend_long lval;
double dval;
zend_string *tmp_str;
zend_string *str;
zend_ffi_type_kind kind = type->kind;
again:
switch (kind) {
case ZEND_FFI_TYPE_FLOAT:
dval = zval_get_double(value);
*(float*)ptr = dval;
break;
case ZEND_FFI_TYPE_DOUBLE:
dval = zval_get_double(value);
*(double*)ptr = dval;
break;
#ifdef HAVE_LONG_DOUBLE
case ZEND_FFI_TYPE_LONGDOUBLE:
dval = zval_get_double(value);
*(long double*)ptr = dval;
break;
#endif
case ZEND_FFI_TYPE_UINT8:
lval = zval_get_long(value);
*(uint8_t*)ptr = lval;
break;
case ZEND_FFI_TYPE_SINT8:
lval = zval_get_long(value);
*(int8_t*)ptr = lval;
break;
case ZEND_FFI_TYPE_UINT16:
lval = zval_get_long(value);
*(uint16_t*)ptr = lval;
break;
case ZEND_FFI_TYPE_SINT16:
lval = zval_get_long(value);
*(int16_t*)ptr = lval;
break;
case ZEND_FFI_TYPE_UINT32:
lval = zval_get_long(value);
*(uint32_t*)ptr = lval;
break;
case ZEND_FFI_TYPE_SINT32:
lval = zval_get_long(value);
*(int32_t*)ptr = lval;
break;
case ZEND_FFI_TYPE_UINT64:
lval = zval_get_long(value);
*(uint64_t*)ptr = lval;
break;
case ZEND_FFI_TYPE_SINT64:
lval = zval_get_long(value);
*(int64_t*)ptr = lval;
break;
case ZEND_FFI_TYPE_BOOL:
*(uint8_t*)ptr = zend_is_true(value);
break;
case ZEND_FFI_TYPE_CHAR:
str = zval_get_tmp_string(value, &tmp_str);
if (ZSTR_LEN(str) == 1) {
*(char*)ptr = ZSTR_VAL(str)[0];
} else {
zend_throw_error(zend_ffi_exception_ce, "Attempt to perform assign of incompatible C type");
return FAILURE;
}
zend_tmp_string_release(tmp_str);
break;
case ZEND_FFI_TYPE_ENUM:
kind = type->enumeration.kind;
goto again;
case ZEND_FFI_TYPE_POINTER:
if (Z_TYPE_P(value) == IS_NULL) {
*(void**)ptr = NULL;
break;
} else if (Z_TYPE_P(value) == IS_OBJECT && Z_OBJCE_P(value) == zend_ffi_cdata_ce) {
zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ_P(value);
if (zend_ffi_is_compatible_type(type, ZEND_FFI_TYPE(cdata->type))) {
if (ZEND_FFI_TYPE(cdata->type)->kind == ZEND_FFI_TYPE_POINTER) {
*(void**)ptr = *(void**)cdata->ptr;
} else {
if (cdata->flags & ZEND_FFI_FLAG_OWNED) {
zend_throw_error(zend_ffi_exception_ce, "Attempt to perform assign of owned C pointer");
return FAILURE;
}
*(void**)ptr = cdata->ptr;
}
return SUCCESS;
/* Allow transparent assignment of not-owned CData to compatible pointers */
} else if (ZEND_FFI_TYPE(cdata->type)->kind != ZEND_FFI_TYPE_POINTER
&& zend_ffi_is_compatible_type(ZEND_FFI_TYPE(type->pointer.type), ZEND_FFI_TYPE(cdata->type))) {
if (cdata->flags & ZEND_FFI_FLAG_OWNED) {
zend_throw_error(zend_ffi_exception_ce, "Attempt to perform assign pointer to owned C data");
return FAILURE;
}
*(void**)ptr = cdata->ptr;
return SUCCESS;
}
#if FFI_CLOSURES
} else if (ZEND_FFI_TYPE(type->pointer.type)->kind == ZEND_FFI_TYPE_FUNC) {
void *callback = zend_ffi_create_callback(ZEND_FFI_TYPE(type->pointer.type), value);
if (callback) {
*(void**)ptr = callback;
break;
} else {
return FAILURE;
}
#endif
}
zend_throw_error(zend_ffi_exception_ce, "Attempt to perform assign of incompatible C type");
return FAILURE;
case ZEND_FFI_TYPE_STRUCT:
case ZEND_FFI_TYPE_ARRAY:
default:
if (Z_TYPE_P(value) == IS_OBJECT && Z_OBJCE_P(value) == zend_ffi_cdata_ce) {
zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ_P(value);
if (zend_ffi_is_compatible_type(type, ZEND_FFI_TYPE(cdata->type)) &&
type->size == ZEND_FFI_TYPE(cdata->type)->size) {
memcpy(ptr, cdata->ptr, type->size);
return SUCCESS;
}
}
zend_throw_error(zend_ffi_exception_ce, "Attempt to perform assign of incompatible C type");
return FAILURE;
}
return SUCCESS;
}
/* }}} */
#if FFI_CLOSURES
typedef struct _zend_ffi_callback_data {
zend_fcall_info_cache fcc;
zend_ffi_type *type;
void *code;
void *callback;
ffi_cif cif;
uint32_t arg_count;
ffi_type *ret_type;
ffi_type *arg_types[0];
} zend_ffi_callback_data;
static void zend_ffi_callback_hash_dtor(zval *zv) /* {{{ */
{
zend_ffi_callback_data *callback_data = Z_PTR_P(zv);
ffi_closure_free(callback_data->callback);
if (callback_data->fcc.function_handler->common.fn_flags & ZEND_ACC_CLOSURE) {
OBJ_RELEASE(ZEND_CLOSURE_OBJECT(callback_data->fcc.function_handler));
}
efree(callback_data);
}
/* }}} */
static void zend_ffi_callback_trampoline(ffi_cif* cif, void* ret, void** args, void* data) /* {{{ */
{
zend_ffi_callback_data *callback_data = (zend_ffi_callback_data*)data;
zend_fcall_info fci;
zval retval;
ALLOCA_FLAG(use_heap)
fci.size = sizeof(zend_fcall_info);
ZVAL_UNDEF(&fci.function_name);
fci.retval = &retval;
fci.params = do_alloca(sizeof(zval) *callback_data->arg_count, use_heap);
fci.object = NULL;
fci.no_separation = 1;
fci.param_count = callback_data->arg_count;
if (callback_data->type->func.args) {
int n = 0;
zend_ffi_type *arg_type;
ZEND_HASH_FOREACH_PTR(callback_data->type->func.args, arg_type) {
arg_type = ZEND_FFI_TYPE(arg_type);
zend_ffi_cdata_to_zval(NULL, args[n], arg_type, BP_VAR_R, &fci.params[n], (zend_ffi_flags)(arg_type->attr & ZEND_FFI_ATTR_CONST), 0);
n++;
} ZEND_HASH_FOREACH_END();
}
ZVAL_UNDEF(&retval);
if (zend_call_function(&fci, &callback_data->fcc) != SUCCESS) {
zend_throw_error(zend_ffi_exception_ce, "Cannot call callback");
}
if (callback_data->arg_count) {
int n = 0;
for (n = 0; n < callback_data->arg_count; n++) {
zval_ptr_dtor(&fci.params[n]);
n++;
}
}
free_alloca(fci.params, use_heap);
zend_ffi_zval_to_cdata(ret, ZEND_FFI_TYPE(callback_data->type->func.ret_type), &retval);
}
/* }}} */
static void *zend_ffi_create_callback(zend_ffi_type *type, zval *value) /* {{{ */
{
zend_fcall_info_cache fcc;
char *error = NULL;
uint32_t arg_count;
void *code;
void *callback;
zend_ffi_callback_data *callback_data;
if (type->attr & ZEND_FFI_ATTR_VARIADIC) {
zend_throw_error(zend_ffi_exception_ce, "Variadic function closures are not supported");
return NULL;
}
if (!zend_is_callable_ex(value, NULL, 0, NULL, &fcc, &error)) {
zend_throw_error(zend_ffi_exception_ce, "Attempt to assing an invalid callback, %s", error);
return NULL;
}
arg_count = type->func.args ? zend_hash_num_elements(type->func.args) : 0;
if (arg_count < fcc.function_handler->common.required_num_args) {
zend_throw_error(zend_ffi_exception_ce, "Attempt to assing an invalid callback, insufficient number of arguments");
return NULL;
}
callback = ffi_closure_alloc(sizeof(ffi_closure), &code);
if (!callback) {
zend_throw_error(zend_ffi_exception_ce, "Cannot allocate callback");
return NULL;
}
callback_data = emalloc(sizeof(zend_ffi_callback_data) + sizeof(ffi_type*) * arg_count);
memcpy(&callback_data->fcc, &fcc, sizeof(zend_fcall_info_cache));
callback_data->type = type;
callback_data->callback = callback;
callback_data->code = code;
callback_data->arg_count = arg_count;
if (type->func.args) {
int n = 0;
zend_ffi_type *arg_type;
ZEND_HASH_FOREACH_PTR(type->func.args, arg_type) {
arg_type = ZEND_FFI_TYPE(arg_type);
callback_data->arg_types[n] = zend_ffi_get_type(arg_type);
if (!callback_data->arg_types[n]) {
efree(callback_data);
ffi_closure_free(callback);
return NULL;
}
n++;
} ZEND_HASH_FOREACH_END();
}
callback_data->ret_type = zend_ffi_get_type(type->func.ret_type);
if (!callback_data->ret_type) {
efree(callback_data);
ffi_closure_free(callback);
return NULL;
}
if (ffi_prep_cif(&callback_data->cif, type->func.abi, callback_data->arg_count, callback_data->ret_type, callback_data->arg_types) != FFI_OK) {
zend_throw_error(zend_ffi_exception_ce, "Cannot prepare callback CIF");
efree(callback_data);
ffi_closure_free(callback);
return NULL;
}
if (ffi_prep_closure_loc(callback, &callback_data->cif, zend_ffi_callback_trampoline, callback_data, code) != FFI_OK) {
zend_throw_error(zend_ffi_exception_ce, "Cannot prepare callback");
efree(callback_data);
ffi_closure_free(callback);
return NULL;
}
if (!FFI_G(callbacks)) {
FFI_G(callbacks) = emalloc(sizeof(HashTable));
zend_hash_init(FFI_G(callbacks), 0, NULL, zend_ffi_callback_hash_dtor, 0);
}
zend_hash_next_index_insert_ptr(FFI_G(callbacks), callback_data);
if (fcc.function_handler->common.fn_flags & ZEND_ACC_CLOSURE) {
GC_ADDREF(ZEND_CLOSURE_OBJECT(fcc.function_handler));
}
return code;
}
/* }}} */
#endif
static zval* zend_ffi_cdata_get(zval *object, zval *rv) /* {{{ */
{
zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ_P(object);
zend_ffi_type *type = ZEND_FFI_TYPE(cdata->type);
#if 0
if (UNEXPECTED(!cdata->ptr)) {
zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
return &EG(uninitialized_zval);
}
#endif
zend_ffi_cdata_to_zval(cdata, cdata->ptr, type, BP_VAR_R, rv, 0, 0);
return rv;
}
/* }}} */
static void zend_ffi_cdata_set(zval *object, zval *value) /* {{{ */
{
zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ_P(object);
zend_ffi_type *type = ZEND_FFI_TYPE(cdata->type);
#if 0
if (UNEXPECTED(!cdata->ptr)) {
zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
return;
}
#endif
zend_ffi_zval_to_cdata(cdata->ptr, type, value);
}
/* }}} */
static int zend_ffi_cdata_cast_object(zval *readobj, zval *writeobj, int type) /* {{{ */
{
return FAILURE;
}
/* }}} */
static zval *zend_ffi_cdata_read_field(zval *object, zval *member, int read_type, void **cache_slot, zval *rv) /* {{{ */
{
zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ_P(object);
zend_ffi_type *type = ZEND_FFI_TYPE(cdata->type);
void *ptr = cdata->ptr;
zend_ffi_field *field;
if (cache_slot && *cache_slot == type) {
field = *(cache_slot + 1);
} else {
zend_string *tmp_field_name;
zend_string *field_name = zval_get_tmp_string(member, &tmp_field_name);
if (UNEXPECTED(type->kind != ZEND_FFI_TYPE_STRUCT)) {
if (type->kind == ZEND_FFI_TYPE_POINTER) {
/* transparently dereference the pointer */
if (UNEXPECTED(!ptr)) {
zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
zend_tmp_string_release(tmp_field_name);
return &EG(uninitialized_zval);
}
ptr = (void*)(*(char**)ptr);
if (UNEXPECTED(!ptr)) {
zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
zend_tmp_string_release(tmp_field_name);
return &EG(uninitialized_zval);
}
type = ZEND_FFI_TYPE(type->pointer.type);
}
if (UNEXPECTED(type->kind != ZEND_FFI_TYPE_STRUCT)) {
zend_throw_error(zend_ffi_exception_ce, "Attempt to read field '%s' of non C struct/union", ZSTR_VAL(field_name));
zend_tmp_string_release(tmp_field_name);
return &EG(uninitialized_zval);
}
}
field = zend_hash_find_ptr(&type->record.fields, field_name);
if (UNEXPECTED(!field)) {
zend_throw_error(zend_ffi_exception_ce, "Attempt to read undefined field '%s' of C struct/union", ZSTR_VAL(field_name));
zend_tmp_string_release(tmp_field_name);
return &EG(uninitialized_zval);
}
zend_tmp_string_release(tmp_field_name);
if (cache_slot) {
*cache_slot = type;
*(cache_slot + 1) = field;
}
}
#if 0
if (UNEXPECTED(!ptr)) {
zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
return &EG(uninitialized_zval);
}
#endif
if (EXPECTED(!field->bits)) {
zend_ffi_type *field_type = field->type;
if (ZEND_FFI_TYPE_IS_OWNED(field_type)) {
field_type = ZEND_FFI_TYPE(field_type);
if (!(field_type->attr & ZEND_FFI_ATTR_STORED)
&& field_type->kind == ZEND_FFI_TYPE_POINTER) {
field->type = field_type = zend_ffi_remember_type(field_type);
}
}
ptr = (void*)(((char*)ptr) + field->offset);
zend_ffi_cdata_to_zval(NULL, ptr, field_type, read_type, rv, (cdata->flags & ZEND_FFI_FLAG_CONST) | (zend_ffi_flags)field->is_const, 0);
} else {
zend_ffi_bit_field_to_zval(ptr, field, rv);
}
return rv;
}
/* }}} */
static zval *zend_ffi_cdata_write_field(zval *object, zval *member, zval *value, void **cache_slot) /* {{{ */