/
nativecall_libffi.c
787 lines (735 loc) · 36.1 KB
/
nativecall_libffi.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
#include "moar.h"
#include <platform/threads.h>
//~ ffi_type * MVM_nativecall_get_ffi_type(MVMThreadContext *tc, MVMuint64 type_id, void **values, MVMuint64 offset) {
ffi_type * MVM_nativecall_get_ffi_type(MVMThreadContext *tc, MVMuint64 type_id) {
if ((type_id & MVM_NATIVECALL_ARG_RW_MASK) == MVM_NATIVECALL_ARG_RW)
return &ffi_type_pointer;
switch (type_id & MVM_NATIVECALL_ARG_TYPE_MASK) {
case MVM_NATIVECALL_ARG_CHAR:
return &ffi_type_schar;
case MVM_NATIVECALL_ARG_SHORT:
return &ffi_type_sshort;
case MVM_NATIVECALL_ARG_INT:
return &ffi_type_sint;
case MVM_NATIVECALL_ARG_LONG:
return &ffi_type_slong;
case MVM_NATIVECALL_ARG_LONGLONG:
return &ffi_type_sint64; /* XXX ffi_type_slonglong not defined */
case MVM_NATIVECALL_ARG_FLOAT:
return &ffi_type_float;
case MVM_NATIVECALL_ARG_DOUBLE:
return &ffi_type_double;
case MVM_NATIVECALL_ARG_ASCIISTR:
case MVM_NATIVECALL_ARG_UTF8STR:
case MVM_NATIVECALL_ARG_UTF16STR:
case MVM_NATIVECALL_ARG_CPPSTRUCT:
case MVM_NATIVECALL_ARG_CSTRUCT:
case MVM_NATIVECALL_ARG_CPOINTER:
case MVM_NATIVECALL_ARG_CARRAY:
case MVM_NATIVECALL_ARG_CUNION:
case MVM_NATIVECALL_ARG_VMARRAY:
case MVM_NATIVECALL_ARG_CALLBACK:
return &ffi_type_pointer;
case MVM_NATIVECALL_ARG_UCHAR:
return &ffi_type_uchar;
case MVM_NATIVECALL_ARG_USHORT:
return &ffi_type_ushort;
case MVM_NATIVECALL_ARG_UINT:
return &ffi_type_uint;
case MVM_NATIVECALL_ARG_ULONG:
return &ffi_type_ulong;
case MVM_NATIVECALL_ARG_ULONGLONG:
return &ffi_type_uint64; /* XXX ffi_type_ulonglong not defined */
default:
return &ffi_type_void;
}
}
/* Maps a calling convention name to an ID. */
ffi_abi MVM_nativecall_get_calling_convention(MVMThreadContext *tc, MVMString *name) {
ffi_abi result = FFI_DEFAULT_ABI;
//~ if (name && MVM_string_graphs(tc, name) > 0) {
//~ char *cname = MVM_string_utf8_encode_C_string(tc, name);
//~ if (strcmp(cname, "cdecl") == 0)
//~ result = DC_CALL_C_X86_CDECL;
//~ else if (strcmp(cname, "stdcall") == 0)
//~ result = DC_CALL_C_X86_WIN32_STD;
//~ else if (strcmp(cname, "stdcall") == 0)
//~ result = DC_CALL_C_X64_WIN64;
//~ else {
//~ char *waste[] = { cname, NULL };
//~ MVM_exception_throw_adhoc_free(tc, waste,
//~ "Unknown calling convention '%s' used for native call", cname);
//~ }
//~ MVM_free(cname);
//~ }
return result;
}
/* Sets up a callback, caching the information to avoid duplicate work. */
//~ static char callback_handler(DCCallback *cb, DCArgs *args, DCValue *result, MVMNativeCallback *data);
static void callback_handler(ffi_cif *cif, void *cb_result, void **cb_args, void *data);
static void * unmarshal_callback(MVMThreadContext *tc, MVMObject *callback, MVMObject *sig_info) {
MVMNativeCallbackCacheHead *callback_data_head = NULL;
MVMNativeCallback **callback_data_handle;
MVMString *cuid;
if (!IS_CONCRETE(callback))
return NULL;
/* Try to locate existing cached callback info. */
callback = MVM_frame_find_invokee(tc, callback, NULL);
cuid = ((MVMCode *)callback)->body.sf->body.cuuid;
MVM_HASH_GET(tc, tc->native_callback_cache, cuid, callback_data_head);
if (!callback_data_head) {
callback_data_head = MVM_malloc(sizeof(MVMNativeCallbackCacheHead));
callback_data_head->head = NULL;
MVM_HASH_BIND(tc, tc->native_callback_cache, cuid, callback_data_head);
}
callback_data_handle = &(callback_data_head->head);
while (*callback_data_handle) {
if ((*callback_data_handle)->target == callback) /* found it, break */
break;
callback_data_handle = &((*callback_data_handle)->next);
}
if (!*callback_data_handle) {
/* First, build the MVMNativeCallback */
MVMCallsite *cs;
MVMObject *typehash;
MVMint64 num_info, i;
MVMNativeCallback *callback_data;
/* cb is a piece of executable memory we obtain from libffi. */
void *cb;
ffi_cif *cif;
ffi_closure *closure;
ffi_status status;
num_info = MVM_repr_elems(tc, sig_info);
/* We'll also build up a MoarVM callsite as we go. */
cs = MVM_calloc(1, sizeof(MVMCallsite));
cs->flag_count = num_info - 1;
cs->arg_flags = MVM_malloc(cs->flag_count * sizeof(MVMCallsiteEntry));
cs->arg_count = num_info - 1;
cs->num_pos = num_info - 1;
cs->has_flattening = 0;
cs->is_interned = 0;
cs->with_invocant = NULL;
callback_data = MVM_malloc(sizeof(MVMNativeCallback));
callback_data->num_types = num_info;
callback_data->typeinfos = MVM_malloc(num_info * sizeof(MVMint16));
callback_data->types = MVM_malloc(num_info * sizeof(MVMObject *));
callback_data->next = NULL;
cif = (ffi_cif *)MVM_malloc(sizeof(ffi_cif));
callback_data->convention = FFI_DEFAULT_ABI;
callback_data->ffi_arg_types = MVM_malloc(sizeof(ffi_type *) * (cs->arg_count ? cs->arg_count : 1));
/* Collect information about the return type. */
typehash = MVM_repr_at_pos_o(tc, sig_info, 0);
callback_data->types[0] = MVM_repr_at_key_o(tc, typehash, tc->instance->str_consts.typeobj);
callback_data->typeinfos[0] = MVM_nativecall_get_arg_type(tc, typehash, 1);
callback_data->ffi_ret_type = MVM_nativecall_get_ffi_type(tc, callback_data->typeinfos[0]);
for (i = 1; i < num_info; i++) {
typehash = MVM_repr_at_pos_o(tc, sig_info, i);
callback_data->types[i] = MVM_repr_at_key_o(tc, typehash, tc->instance->str_consts.typeobj);
callback_data->typeinfos[i] = MVM_nativecall_get_arg_type(tc, typehash, 0) & ~MVM_NATIVECALL_ARG_FREE_STR;
callback_data->ffi_arg_types[i - 1] = MVM_nativecall_get_ffi_type(tc, callback_data->typeinfos[i]);
switch (callback_data->typeinfos[i] & MVM_NATIVECALL_ARG_TYPE_MASK) {
case MVM_NATIVECALL_ARG_CHAR:
case MVM_NATIVECALL_ARG_SHORT:
case MVM_NATIVECALL_ARG_INT:
case MVM_NATIVECALL_ARG_LONG:
case MVM_NATIVECALL_ARG_LONGLONG:
cs->arg_flags[i - 1] = MVM_CALLSITE_ARG_INT;
break;
case MVM_NATIVECALL_ARG_UCHAR:
case MVM_NATIVECALL_ARG_USHORT:
case MVM_NATIVECALL_ARG_UINT:
case MVM_NATIVECALL_ARG_ULONG:
case MVM_NATIVECALL_ARG_ULONGLONG:
/* TODO: should probably be UINT, when we can support that. */
cs->arg_flags[i - 1] = MVM_CALLSITE_ARG_INT;
break;
case MVM_NATIVECALL_ARG_FLOAT:
case MVM_NATIVECALL_ARG_DOUBLE:
cs->arg_flags[i - 1] = MVM_CALLSITE_ARG_NUM;
break;
default:
cs->arg_flags[i - 1] = MVM_CALLSITE_ARG_OBJ;
break;
}
}
MVM_callsite_try_intern(tc, &cs);
callback_data->instance = tc->instance;
callback_data->cs = cs;
callback_data->target = callback;
status = ffi_prep_cif(cif, callback_data->convention, (unsigned int)cs->arg_count,
callback_data->ffi_ret_type, callback_data->ffi_arg_types);
closure = ffi_closure_alloc(sizeof(ffi_closure), &cb);
if (!closure)
MVM_panic(1, "Unable to allocate memory for callback closure");
ffi_prep_closure_loc(closure, cif, callback_handler, callback_data, cb);
callback_data->cb = cb;
/* Now insert the MVMCallback into the linked list. */
*callback_data_handle = callback_data;
}
return (*callback_data_handle)->cb;
}
/* Called to handle a callback. */
typedef struct {
MVMObject *invokee;
MVMRegister *args;
MVMCallsite *cs;
} CallbackInvokeData;
static void callback_invoke(MVMThreadContext *tc, void *data) {
/* Invoke the coderef, to set up the nested interpreter. */
CallbackInvokeData *cid = (CallbackInvokeData *)data;
STABLE(cid->invokee)->invoke(tc, cid->invokee, cid->cs, cid->args);
/* Ensure we exit interp after callback. */
tc->thread_entry_frame = tc->cur_frame;
}
static void callback_handler(ffi_cif *cif, void *cb_result, void **cb_args, void *cb_data) {
CallbackInvokeData cid;
MVMint32 num_roots, i;
MVMRegister res;
MVMRegister *args;
MVMNativeCallback *data = (MVMNativeCallback *)cb_data;
void **values = MVM_malloc(sizeof(void *) * (data->cs->arg_count ? data->cs->arg_count : 1));
unsigned int interval_id;
/* Locate the MoarVM thread this callback is being run on. */
MVMThreadContext *tc = MVM_nativecall_find_thread_context(data->instance);
/* Unblock GC if needed, so this thread can do work. */
MVMint32 was_blocked = MVM_gc_is_thread_blocked(tc);
if (was_blocked)
MVM_gc_mark_thread_unblocked(tc);
interval_id = MVM_telemetry_interval_start(tc, "nativecall callback handler");
/* Build a callsite and arguments buffer. */
args = MVM_malloc(data->num_types * sizeof(MVMRegister));
num_roots = 0;
for (i = 1; i < data->num_types; i++) {
MVMObject *type = data->types[i];
MVMint16 typeinfo = data->typeinfos[i];
switch (typeinfo & MVM_NATIVECALL_ARG_TYPE_MASK) {
case MVM_NATIVECALL_ARG_CHAR:
args[i - 1].i64 = *(signed char *)cb_args[i - 1];
break;
case MVM_NATIVECALL_ARG_SHORT:
args[i - 1].i64 = *(signed short *)cb_args[i - 1];
break;
case MVM_NATIVECALL_ARG_INT:
args[i - 1].i64 = *(signed int *)cb_args[i - 1];
break;
case MVM_NATIVECALL_ARG_LONG:
args[i - 1].i64 = *(signed long *)cb_args[i - 1];
break;
case MVM_NATIVECALL_ARG_LONGLONG:
args[i - 1].i64 = *(signed long long *)cb_args[i - 1];
break;
case MVM_NATIVECALL_ARG_FLOAT:
args[i - 1].n64 = *(float *)cb_args[i - 1];
break;
case MVM_NATIVECALL_ARG_DOUBLE:
args[i - 1].n64 = *(double *)cb_args[i - 1];
break;
case MVM_NATIVECALL_ARG_ASCIISTR:
case MVM_NATIVECALL_ARG_UTF8STR:
case MVM_NATIVECALL_ARG_UTF16STR:
args[i - 1].o = MVM_nativecall_make_str(tc, type, typeinfo, *(char **)cb_args[i - 1]);
MVM_gc_root_temp_push(tc, (MVMCollectable **)&(args[i - 1].o));
num_roots++;
break;
case MVM_NATIVECALL_ARG_CSTRUCT:
args[i - 1].o = MVM_nativecall_make_cstruct(tc, type, *(void **)cb_args[i - 1]);
MVM_gc_root_temp_push(tc, (MVMCollectable **)&(args[i - 1].o));
num_roots++;
break;
case MVM_NATIVECALL_ARG_CPPSTRUCT:
args[i - 1].o = MVM_nativecall_make_cppstruct(tc, type, *(void **)cb_args[i - 1]);
MVM_gc_root_temp_push(tc, (MVMCollectable **)&(args[i - 1].o));
num_roots++;
break;
case MVM_NATIVECALL_ARG_CPOINTER:
args[i - 1].o = MVM_nativecall_make_cpointer(tc, type, *(void **)cb_args[i - 1]);
MVM_gc_root_temp_push(tc, (MVMCollectable **)&(args[i - 1].o));
num_roots++;
break;
case MVM_NATIVECALL_ARG_CARRAY:
args[i - 1].o = MVM_nativecall_make_carray(tc, type, *(void **)cb_args[i - 1]);
MVM_gc_root_temp_push(tc, (MVMCollectable **)&(args[i - 1].o));
num_roots++;
break;
case MVM_NATIVECALL_ARG_CUNION:
args[i - 1].o = MVM_nativecall_make_cunion(tc, type, *(void **)cb_args[i - 1]);
MVM_gc_root_temp_push(tc, (MVMCollectable **)&(args[i - 1].o));
num_roots++;
break;
case MVM_NATIVECALL_ARG_CALLBACK:
/* TODO: A callback -return- value means that we have a C method
* that needs to be wrapped similarly to a is native(...) Perl 6
* sub. */
/* XXX do something with the function pointer: *(void **)cb_args[i - 1] */
args[i - 1].o = type;
MVM_gc_root_temp_push(tc, (MVMCollectable **)&(args[i - 1].o));
num_roots++;
case MVM_NATIVECALL_ARG_UCHAR:
args[i - 1].i64 = *(unsigned char *)cb_args[i - 1];
break;
case MVM_NATIVECALL_ARG_USHORT:
args[i - 1].i64 = *(unsigned short *)cb_args[i - 1];
break;
case MVM_NATIVECALL_ARG_UINT:
args[i - 1].i64 = *(unsigned int *)cb_args[i - 1];
break;
case MVM_NATIVECALL_ARG_ULONG:
args[i - 1].i64 = *(unsigned long *)cb_args[i - 1];
break;
case MVM_NATIVECALL_ARG_ULONGLONG:
args[i - 1].i64 = *(unsigned long long *)cb_args[i - 1];
break;
default:
MVM_telemetry_interval_stop(tc, interval_id, "nativecall callback handler failed");
MVM_exception_throw_adhoc(tc,
"Internal error: unhandled libffi callback argument type");
}
}
/* Call into a nested interpreter (since we already are in one). Need to
* save a bunch of state around each side of this. */
cid.invokee = data->target;
cid.args = args;
cid.cs = data->cs;
{
MVMuint8 **backup_interp_cur_op = tc->interp_cur_op;
MVMuint8 **backup_interp_bytecode_start = tc->interp_bytecode_start;
MVMRegister **backup_interp_reg_base = tc->interp_reg_base;
MVMCompUnit **backup_interp_cu = tc->interp_cu;
MVMFrame *backup_cur_frame = MVM_frame_force_to_heap(tc, tc->cur_frame);
MVMFrame *backup_thread_entry_frame = tc->thread_entry_frame;
void **backup_jit_return_address = tc->jit_return_address;
tc->jit_return_address = NULL;
MVMROOT2(tc, backup_cur_frame, backup_thread_entry_frame, {
MVMuint32 backup_mark = MVM_gc_root_temp_mark(tc);
jmp_buf backup_interp_jump;
memcpy(backup_interp_jump, tc->interp_jump, sizeof(jmp_buf));
tc->cur_frame->return_value = &res;
tc->cur_frame->return_type = MVM_RETURN_OBJ;
MVM_interp_run(tc, callback_invoke, &cid);
tc->interp_cur_op = backup_interp_cur_op;
tc->interp_bytecode_start = backup_interp_bytecode_start;
tc->interp_reg_base = backup_interp_reg_base;
tc->interp_cu = backup_interp_cu;
tc->cur_frame = backup_cur_frame;
tc->current_frame_nr = backup_cur_frame->sequence_nr;
tc->thread_entry_frame = backup_thread_entry_frame;
tc->jit_return_address = backup_jit_return_address;
memcpy(tc->interp_jump, backup_interp_jump, sizeof(jmp_buf));
MVM_gc_root_temp_mark_reset(tc, backup_mark);
});
}
/* Handle return value. */
if (res.o) {
MVMContainerSpec const *contspec = STABLE(res.o)->container_spec;
if (contspec && contspec->fetch_never_invokes)
contspec->fetch(tc, res.o, &res);
}
switch (data->typeinfos[0] & MVM_NATIVECALL_ARG_TYPE_MASK) {
case MVM_NATIVECALL_ARG_VOID:
break;
case MVM_NATIVECALL_ARG_CHAR:
*(ffi_sarg *)cb_result = MVM_nativecall_unmarshal_char(tc, res.o);
break;
case MVM_NATIVECALL_ARG_SHORT:
*(ffi_sarg *)cb_result = MVM_nativecall_unmarshal_short(tc, res.o);
break;
case MVM_NATIVECALL_ARG_INT:
*(ffi_sarg *)cb_result = MVM_nativecall_unmarshal_int(tc, res.o);
break;
case MVM_NATIVECALL_ARG_LONG:
*(ffi_sarg *)cb_result = MVM_nativecall_unmarshal_long(tc, res.o);
break;
case MVM_NATIVECALL_ARG_LONGLONG:
*(signed long long *)cb_result = MVM_nativecall_unmarshal_longlong(tc, res.o);
break;
case MVM_NATIVECALL_ARG_FLOAT:
*(float *)cb_result = MVM_nativecall_unmarshal_float(tc, res.o);
break;
case MVM_NATIVECALL_ARG_DOUBLE:
*(double *)cb_result = MVM_nativecall_unmarshal_double(tc, res.o);
break;
case MVM_NATIVECALL_ARG_ASCIISTR:
case MVM_NATIVECALL_ARG_UTF8STR:
case MVM_NATIVECALL_ARG_UTF16STR:
*(void **)cb_result = MVM_nativecall_unmarshal_string(tc, res.o, data->typeinfos[0], NULL, MVM_NATIVECALL_UNMARSHAL_KIND_RETURN);
break;
case MVM_NATIVECALL_ARG_CSTRUCT:
*(void **)cb_result = MVM_nativecall_unmarshal_cstruct(tc, res.o, MVM_NATIVECALL_UNMARSHAL_KIND_RETURN);
break;
case MVM_NATIVECALL_ARG_CPPSTRUCT:
*(void **)cb_result = MVM_nativecall_unmarshal_cppstruct(tc, res.o, MVM_NATIVECALL_UNMARSHAL_KIND_RETURN);
break;
case MVM_NATIVECALL_ARG_CPOINTER:
*(void **)cb_result = MVM_nativecall_unmarshal_cpointer(tc, res.o, MVM_NATIVECALL_UNMARSHAL_KIND_RETURN);
break;
case MVM_NATIVECALL_ARG_CARRAY:
*(void **)cb_result = MVM_nativecall_unmarshal_carray(tc, res.o, MVM_NATIVECALL_UNMARSHAL_KIND_RETURN);
break;
case MVM_NATIVECALL_ARG_CUNION:
*(void **)cb_result = MVM_nativecall_unmarshal_cunion(tc, res.o, MVM_NATIVECALL_UNMARSHAL_KIND_RETURN);
break;
case MVM_NATIVECALL_ARG_VMARRAY:
*(void **)cb_result = MVM_nativecall_unmarshal_vmarray(tc, res.o, MVM_NATIVECALL_UNMARSHAL_KIND_RETURN);
break;
case MVM_NATIVECALL_ARG_CALLBACK:
*(void **)cb_result = unmarshal_callback(tc, res.o, data->types[0]);
break;
case MVM_NATIVECALL_ARG_UCHAR:
*(ffi_arg *)cb_result = MVM_nativecall_unmarshal_uchar(tc, res.o);
break;
case MVM_NATIVECALL_ARG_USHORT:
*(ffi_arg *)cb_result = MVM_nativecall_unmarshal_ushort(tc, res.o);
break;
case MVM_NATIVECALL_ARG_UINT:
*(ffi_arg *)cb_result = MVM_nativecall_unmarshal_uint(tc, res.o);
break;
case MVM_NATIVECALL_ARG_ULONG:
*(ffi_arg *)cb_result = MVM_nativecall_unmarshal_ulong(tc, res.o);
break;
case MVM_NATIVECALL_ARG_ULONGLONG:
*(unsigned long long *)cb_result = MVM_nativecall_unmarshal_ulonglong(tc, res.o);
break;
default:
MVM_telemetry_interval_stop(tc, interval_id, "nativecall callback handler failed");
MVM_exception_throw_adhoc(tc,
"Internal error: unhandled libffi callback return type");
}
/* Clean up. */
MVM_gc_root_temp_pop_n(tc, num_roots);
MVM_free(args);
/* Re-block GC if needed, so other threads will be able to collect. */
if (was_blocked)
MVM_gc_mark_thread_blocked(tc);
MVM_telemetry_interval_stop(tc, interval_id, "nativecall callback handler");
}
#define handle_arg(what, cont_X, dc_type, reg_slot, unmarshal_fun) do { \
MVMRegister r; \
if ((arg_types[i] & MVM_NATIVECALL_ARG_RW_MASK) == MVM_NATIVECALL_ARG_RW) { \
if (MVM_6model_container_is ## cont_X(tc, value)) { \
MVM_6model_container_de ## cont_X(tc, value, &r); \
values[i] = MVM_malloc(sizeof(void *)); \
*(void **)values[i] = MVM_malloc(sizeof(dc_type)); \
*(dc_type *)*(void **)values[i] = (dc_type)r. reg_slot ; \
} \
else \
MVM_exception_throw_adhoc(tc, \
"Native call expected argument %d to reference a native %s, but got %s", \
i, what, REPR(value)->name); \
} \
else { \
values[i] = MVM_malloc(sizeof(dc_type)); \
if (value && IS_CONCRETE(value) && STABLE(value)->container_spec) { \
STABLE(value)->container_spec->fetch(tc, value, &r); \
*(dc_type *)values[i] = unmarshal_fun(tc, r.o); \
} \
else { \
*(dc_type *)values[i] = unmarshal_fun(tc, value); \
} \
} \
} while (0)
#define handle_ret(tc, c_type, ffi_type, make_fun) do { \
if (sizeof(c_type) < sizeof(ffi_type)) { \
ffi_type ret; \
ffi_call(&cif, entry_point, &ret, values); \
MVM_gc_mark_thread_unblocked(tc); \
result = make_fun(tc, res_type, (c_type)ret); \
} \
else { \
c_type ret; \
ffi_call(&cif, entry_point, &ret, values); \
MVM_gc_mark_thread_unblocked(tc); \
result = make_fun(tc, res_type, ret); \
} \
} while (0)
MVMObject * MVM_nativecall_invoke(MVMThreadContext *tc, MVMObject *res_type,
MVMObject *site, MVMObject *args) {
MVMObject *result = NULL;
char **free_strs = NULL;
MVMint16 num_strs = 0;
MVMint16 i;
/* Get native call body, so we can locate the call info. Read out all we
* shall need, since later we may allocate a result and and move it. */
MVMNativeCallBody *body = MVM_nativecall_get_nc_body(tc, site);
MVMint16 num_args = body->num_args;
MVMint16 *arg_types = body->arg_types;
MVMint16 ret_type = body->ret_type;
void *entry_point = body->entry_point;
void **values = MVM_malloc(sizeof(void *) * (num_args ? num_args : 1));
unsigned int interval_id;
ffi_cif cif;
ffi_status status = ffi_prep_cif(&cif, body->convention, (unsigned int)num_args, body->ffi_ret_type, body->ffi_arg_types);
interval_id = MVM_telemetry_interval_start(tc, "nativecall invoke");
MVM_telemetry_interval_annotate((uintptr_t)entry_point, interval_id, "nc entrypoint");
/* Process arguments. */
for (i = 0; i < num_args; i++) {
MVMObject *value = MVM_repr_at_pos_o(tc, args, i);
switch (arg_types[i] & MVM_NATIVECALL_ARG_TYPE_MASK) {
case MVM_NATIVECALL_ARG_CHAR:
handle_arg("integer", cont_i, signed char, i64, MVM_nativecall_unmarshal_char);
break;
case MVM_NATIVECALL_ARG_SHORT:
handle_arg("integer", cont_i, signed short, i64, MVM_nativecall_unmarshal_short);
break;
case MVM_NATIVECALL_ARG_INT:
handle_arg("integer", cont_i, signed int, i64, MVM_nativecall_unmarshal_int);
break;
case MVM_NATIVECALL_ARG_LONG:
handle_arg("integer", cont_i, signed long, i64, MVM_nativecall_unmarshal_long);
break;
case MVM_NATIVECALL_ARG_LONGLONG:
handle_arg("integer", cont_i, signed long long, i64, MVM_nativecall_unmarshal_longlong);
break;
case MVM_NATIVECALL_ARG_FLOAT:
handle_arg("number", cont_n, float, n64, MVM_nativecall_unmarshal_float);
break;
case MVM_NATIVECALL_ARG_DOUBLE:
handle_arg("number", cont_n, double, n64, MVM_nativecall_unmarshal_double);
break;
case MVM_NATIVECALL_ARG_ASCIISTR:
case MVM_NATIVECALL_ARG_UTF8STR:
case MVM_NATIVECALL_ARG_UTF16STR: {
MVMint16 free = 0;
char *str = MVM_nativecall_unmarshal_string(tc, value, arg_types[i], &free, i);
if (free) {
if (!free_strs)
free_strs = (char**)MVM_malloc(num_args * sizeof(char *));
free_strs[num_strs] = str;
num_strs++;
}
values[i] = MVM_malloc(sizeof(void *));
*(void **)values[i] = str;
break;
}
case MVM_NATIVECALL_ARG_CSTRUCT:
values[i] = MVM_malloc(sizeof(void *));
*(void **)values[i] = MVM_nativecall_unmarshal_cstruct(tc, value, i);
break;
case MVM_NATIVECALL_ARG_CPPSTRUCT: {
/* We need to allocate the struct (THIS) for C++ constructor before passing it along. */
if (i == 0 && !IS_CONCRETE(value)) {
MVMCPPStructREPRData *repr_data = (MVMCPPStructREPRData *)STABLE(res_type)->REPR_data;
/* Allocate a full byte aligned area where the C++ structure fits into. */
void *ptr = MVM_malloc(repr_data->struct_size > 0 ? repr_data->struct_size : 1);
result = MVM_nativecall_make_cppstruct(tc, res_type, ptr);
values[i] = MVM_malloc(sizeof(void *));
*(void **)values[i] = ptr;
}
else {
values[i] = MVM_malloc(sizeof(void *));
*(void **)values[i] = MVM_nativecall_unmarshal_cppstruct(tc, value, i);
}
break;
}
case MVM_NATIVECALL_ARG_CPOINTER:
if ((arg_types[i] & MVM_NATIVECALL_ARG_RW_MASK) == MVM_NATIVECALL_ARG_RW) {
values[i] = MVM_malloc(sizeof(void *));
*(void **)values[i] = MVM_malloc(sizeof(void *));
*(void **)*(void **)values[i] = (void *)MVM_nativecall_unmarshal_cpointer(tc, value, i);
}
else {
values[i] = MVM_malloc(sizeof(void *));
*(void **)values[i] = MVM_nativecall_unmarshal_cpointer(tc, value, i);
}
break;
case MVM_NATIVECALL_ARG_CARRAY:
values[i] = MVM_malloc(sizeof(void *));
*(void **)values[i] = MVM_nativecall_unmarshal_carray(tc, value, i);
break;
case MVM_NATIVECALL_ARG_CUNION:
values[i] = MVM_malloc(sizeof(void *));
*(void **)values[i] = MVM_nativecall_unmarshal_cunion(tc, value, i);
break;
case MVM_NATIVECALL_ARG_VMARRAY:
values[i] = MVM_malloc(sizeof(void *));
*(void **)values[i] = MVM_nativecall_unmarshal_vmarray(tc, value, i);
break;
case MVM_NATIVECALL_ARG_CALLBACK:
values[i] = MVM_malloc(sizeof(void *));
*(void **)values[i] = unmarshal_callback(tc, value, body->arg_info[i]);
break;
case MVM_NATIVECALL_ARG_UCHAR:
handle_arg("integer", cont_i, unsigned char, i64, MVM_nativecall_unmarshal_uchar);
break;
case MVM_NATIVECALL_ARG_USHORT:
handle_arg("integer", cont_i, unsigned short, i64, MVM_nativecall_unmarshal_ushort);
break;
case MVM_NATIVECALL_ARG_UINT:
handle_arg("integer", cont_i, unsigned int, i64, MVM_nativecall_unmarshal_uint);
break;
case MVM_NATIVECALL_ARG_ULONG:
handle_arg("integer", cont_i, unsigned long, i64, MVM_nativecall_unmarshal_ulong);
break;
case MVM_NATIVECALL_ARG_ULONGLONG:
handle_arg("integer", cont_i, unsigned long long, i64, MVM_nativecall_unmarshal_ulonglong);
break;
default:
MVM_telemetry_interval_stop(tc, interval_id, "nativecall invoke failed");
MVM_exception_throw_adhoc(tc, "Internal error: unhandled libffi argument type");
}
}
MVMROOT2(tc, args, res_type, {
MVM_gc_mark_thread_blocked(tc);
if (result) {
/* We are calling a C++ constructor so we hand back the invocant (THIS) we recorded earlier. */
void *ret; // We are not going to use it, but we need to pass it to libffi.
ffi_call(&cif, entry_point, &ret, values);
MVM_gc_mark_thread_unblocked(tc);
}
else {
/* Process return values. */
switch (ret_type & MVM_NATIVECALL_ARG_TYPE_MASK) {
case MVM_NATIVECALL_ARG_VOID: {
void *ret;
ffi_call(&cif, entry_point, &ret, values);
MVM_gc_mark_thread_unblocked(tc);
result = res_type;
break;
}
case MVM_NATIVECALL_ARG_CHAR:
handle_ret(tc, signed char, ffi_sarg, MVM_nativecall_make_int);
break;
case MVM_NATIVECALL_ARG_SHORT:
handle_ret(tc, signed short, ffi_sarg, MVM_nativecall_make_int);
break;
case MVM_NATIVECALL_ARG_INT:
handle_ret(tc, signed int, ffi_sarg, MVM_nativecall_make_int);
break;
case MVM_NATIVECALL_ARG_LONG:
handle_ret(tc, signed long, ffi_sarg, MVM_nativecall_make_int);
break;
case MVM_NATIVECALL_ARG_LONGLONG:
handle_ret(tc, signed long long, ffi_sarg, MVM_nativecall_make_int);
break;
case MVM_NATIVECALL_ARG_FLOAT: {
float ret;
ffi_call(&cif, entry_point, &ret, values);
MVM_gc_mark_thread_unblocked(tc);
result = MVM_nativecall_make_num(tc, res_type, ret);
break;
}
case MVM_NATIVECALL_ARG_DOUBLE: {
double ret;
ffi_call(&cif, entry_point, &ret, values);
MVM_gc_mark_thread_unblocked(tc);
result = MVM_nativecall_make_num(tc, res_type, ret);
break;
}
case MVM_NATIVECALL_ARG_ASCIISTR:
case MVM_NATIVECALL_ARG_UTF8STR:
case MVM_NATIVECALL_ARG_UTF16STR: {
char *ret;
ffi_call(&cif, entry_point, &ret, values);
MVM_gc_mark_thread_unblocked(tc);
result = MVM_nativecall_make_str(tc, res_type, body->ret_type, ret);
break;
}
case MVM_NATIVECALL_ARG_CSTRUCT:
handle_ret(tc, void *, ffi_arg, MVM_nativecall_make_cstruct);
break;
case MVM_NATIVECALL_ARG_CPPSTRUCT:
handle_ret(tc, void *, ffi_arg, MVM_nativecall_make_cppstruct);
break;
case MVM_NATIVECALL_ARG_CPOINTER:
handle_ret(tc, void *, ffi_arg, MVM_nativecall_make_cpointer);
break;
case MVM_NATIVECALL_ARG_CARRAY:
handle_ret(tc, void *, ffi_arg, MVM_nativecall_make_carray);
break;
case MVM_NATIVECALL_ARG_CUNION:
handle_ret(tc, void *, ffi_arg, MVM_nativecall_make_cunion);
break;
case MVM_NATIVECALL_ARG_CALLBACK: {
/* TODO: A callback -return- value means that we have a C method
* that needs to be wrapped similarly to a is native(...) Perl 6
* sub. */
void *ret;
ffi_call(&cif, entry_point, &ret, values);
MVM_gc_mark_thread_unblocked(tc);
/* XXX do something with the function pointer: ret */
result = res_type;
break;
}
case MVM_NATIVECALL_ARG_UCHAR:
handle_ret(tc, unsigned char, ffi_arg, MVM_nativecall_make_int);
break;
case MVM_NATIVECALL_ARG_USHORT:
handle_ret(tc, unsigned short, ffi_arg, MVM_nativecall_make_int);
break;
case MVM_NATIVECALL_ARG_UINT:
handle_ret(tc, unsigned int, ffi_arg, MVM_nativecall_make_int);
break;
case MVM_NATIVECALL_ARG_ULONG:
handle_ret(tc, unsigned long, ffi_arg, MVM_nativecall_make_int);
break;
case MVM_NATIVECALL_ARG_ULONGLONG:
handle_ret(tc, unsigned long long, ffi_arg, MVM_nativecall_make_int);
break;
default:
MVM_telemetry_interval_stop(tc, interval_id, "nativecall invoke failed");
MVM_exception_throw_adhoc(tc, "Internal error: unhandled libffi return type");
}
}
});
for (i = 0; i < num_args; i++) {
MVMObject *value = MVM_repr_at_pos_o(tc, args, i);
if ((arg_types[i] & MVM_NATIVECALL_ARG_RW_MASK) == MVM_NATIVECALL_ARG_RW) {
switch (arg_types[i] & MVM_NATIVECALL_ARG_TYPE_MASK) {
case MVM_NATIVECALL_ARG_CHAR:
MVM_6model_container_assign_i(tc, value, (MVMint64)*(signed char *)*(void **)values[i]);
break;
case MVM_NATIVECALL_ARG_SHORT:
MVM_6model_container_assign_i(tc, value, (MVMint64)*(signed short *)*(void **)values[i]);
break;
case MVM_NATIVECALL_ARG_INT:
MVM_6model_container_assign_i(tc, value, (MVMint64)*(signed int *)*(void **)values[i]);
break;
case MVM_NATIVECALL_ARG_LONG:
MVM_6model_container_assign_i(tc, value, (MVMint64)*(signed long *)*(void **)values[i]);
break;
case MVM_NATIVECALL_ARG_LONGLONG:
MVM_6model_container_assign_i(tc, value, (MVMint64)*(signed long long *)*(void **)values[i]);
break;
case MVM_NATIVECALL_ARG_FLOAT:
MVM_6model_container_assign_n(tc, value, (MVMnum64)*(float *)*(void **)values[i]);
break;
case MVM_NATIVECALL_ARG_DOUBLE:
MVM_6model_container_assign_n(tc, value, (MVMnum64)*(double *)*(void **)values[i]);
break;
case MVM_NATIVECALL_ARG_UCHAR:
MVM_6model_container_assign_i(tc, value, (MVMint64)*(unsigned char *)*(void **)values[i]);
break;
case MVM_NATIVECALL_ARG_USHORT:
MVM_6model_container_assign_i(tc, value, (MVMint64)*(unsigned short *)*(void **)values[i]);
break;
case MVM_NATIVECALL_ARG_UINT:
MVM_6model_container_assign_i(tc, value, (MVMint64)*(unsigned int *)*(void **)values[i]);
break;
case MVM_NATIVECALL_ARG_ULONG:
MVM_6model_container_assign_i(tc, value, (MVMint64)*(unsigned long *)*(void **)values[i]);
break;
case MVM_NATIVECALL_ARG_ULONGLONG:
MVM_6model_container_assign_i(tc, value, (MVMint64)*(unsigned long long *)*(void **)values[i]);
break;
case MVM_NATIVECALL_ARG_CPOINTER:
REPR(value)->box_funcs.set_int(tc, STABLE(value), value, OBJECT_BODY(value),
(MVMint64)(uintptr_t)*(void **)*(void **)values[i]);
break;
default:
MVM_telemetry_interval_stop(tc, interval_id, "nativecall invoke failed");
MVM_exception_throw_adhoc(tc, "Internal error: unhandled libffi argument type");
}
}
/* Perform CArray/CStruct write barriers. */
MVM_nativecall_refresh(tc, value);
}
/* Free any memory that we need to. */
if (free_strs) {
for (i = 0; i < num_strs; i++)
MVM_free(free_strs[i]);
MVM_free(free_strs);
}
if (values)
MVM_free(values);
MVM_telemetry_interval_stop(tc, interval_id, "nativecall invoke");
return result;
}