Debian Codegraph @

src/wasm/ffi.c

raw
1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 2018-2023 Hood Chatham, Brion Vibber, Kleis Auke Wolthuizen, and others.
3
4 wasm32/emscripten Foreign Function Interface
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 ``Software''), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
25 ----------------------------------------------------------------------- */
26
27 #include <ffi.h>
28 #include <ffi_common.h>
29
30 #include <stdlib.h>
31 #include <stdint.h>
32
33 #include <emscripten/emscripten.h>
34
35 #ifdef DEBUG_F
36 #define LOG_DEBUG(args...) \
37 console.warn(`====LIBFFI(line __LINE__)`, args)
38 #else
39 #define LOG_DEBUG(args...) 0
40 #endif
41
42 #define EM_JS_MACROS(ret, name, args, body...) EM_JS(ret, name, args, body)
43
44 EM_JS_DEPS(libffi, "$getWasmTableEntry,$setWasmTableEntry,$getEmptyTableSlot,$convertJsFunctionToWasm");
45
46 #define DEREF_U8(addr, offset) HEAPU8[addr + offset]
47 #define DEREF_S8(addr, offset) HEAP8[addr + offset]
48 #define DEREF_U16(addr, offset) HEAPU16[(addr >> 1) + offset]
49 #define DEREF_S16(addr, offset) HEAP16[(addr >> 1) + offset]
50 #define DEREF_U32(addr, offset) HEAPU32[(addr >> 2) + offset]
51 #define DEREF_S32(addr, offset) HEAP32[(addr >> 2) + offset]
52
53 #define DEREF_F32(addr, offset) HEAPF32[(addr >> 2) + offset]
54 #define DEREF_F64(addr, offset) HEAPF64[(addr >> 3) + offset]
55 #define DEREF_U64(addr, offset) HEAPU64[(addr >> 3) + offset]
56
57 #define CHECK_FIELD_OFFSET(struct, field, offset) \
58 _Static_assert( \
59 offsetof(struct, field) == offset, \
60 "Memory layout of '" #struct "' has changed: '" #field "' is in an unexpected location");
61
62 #if __SIZEOF_POINTER__ == 4
63
64 #define FFI_EMSCRIPTEN_ABI FFI_WASM32_EMSCRIPTEN
65 #define PTR_SIG 'i'
66
67 #define DEC_PTR(p) p
68 #define ENC_PTR(p) p
69
70 #define DEREF_PTR(addr, offset) DEREF_U32(addr, offset)
71 #define DEREF_PTR_NUMBER(addr, offset) DEREF_PTR(addr, offset)
72
73 CHECK_FIELD_OFFSET(ffi_cif, abi, 4*0);
74 CHECK_FIELD_OFFSET(ffi_cif, nargs, 4*1);
75 CHECK_FIELD_OFFSET(ffi_cif, arg_types, 4*2);
76 CHECK_FIELD_OFFSET(ffi_cif, rtype, 4*3);
77 CHECK_FIELD_OFFSET(ffi_cif, flags, 4*5);
78 CHECK_FIELD_OFFSET(ffi_cif, nfixedargs, 4*6);
79
80 #define CIF__ABI(addr) DEREF_U32(addr, 0)
81 #define CIF__NARGS(addr) DEREF_U32(addr, 1)
82 #define CIF__ARGTYPES(addr) DEREF_U32(addr, 2)
83 #define CIF__RTYPE(addr) DEREF_U32(addr, 3)
84 #define CIF__FLAGS(addr) DEREF_U32(addr, 5)
85 #define CIF__NFIXEDARGS(addr) DEREF_U32(addr, 6)
86
87 CHECK_FIELD_OFFSET(ffi_type, size, 0);
88 CHECK_FIELD_OFFSET(ffi_type, alignment, 4);
89 CHECK_FIELD_OFFSET(ffi_type, type, 6);
90 CHECK_FIELD_OFFSET(ffi_type, elements, 8);
91
92 #define FFI_TYPE__SIZE(addr) DEREF_U32(addr, 0)
93 #define FFI_TYPE__ALIGN(addr) DEREF_U16(addr + 4, 0)
94 #define FFI_TYPE__TYPEID(addr) DEREF_U16(addr + 6, 0)
95 #define FFI_TYPE__ELEMENTS(addr) DEREF_U32(addr + 8, 0)
96
97 #elif __SIZEOF_POINTER__ == 8
98
99 #define FFI_EMSCRIPTEN_ABI FFI_WASM64_EMSCRIPTEN
100 #define PTR_SIG 'j'
101
102 // DEC_PTR casts a pointer value (comming from Wasm) represented as BigInt (i64) to Number (i53).
103 // This should be used for a pointer that is expected to be within the i53 range. If the pointer
104 // value is outside the Number's range, the value will become NaN.
105 #define DEC_PTR(p) bigintToI53Checked(p)
106 // ENC_PTR casts a pointer value represented as Number to BigInt (i64)
107 #define ENC_PTR(p) BigInt(p)
108
109 #define DEREF_PTR(addr, offset) DEREF_U64(addr, offset)
110 #define DEREF_PTR_NUMBER(addr, offset) DEC_PTR(DEREF_PTR(addr, offset))
111
112 CHECK_FIELD_OFFSET(ffi_cif, abi, 0);
113 CHECK_FIELD_OFFSET(ffi_cif, nargs, 4);
114 CHECK_FIELD_OFFSET(ffi_cif, arg_types, 8);
115 CHECK_FIELD_OFFSET(ffi_cif, rtype, 16);
116 CHECK_FIELD_OFFSET(ffi_cif, flags, 28);
117 CHECK_FIELD_OFFSET(ffi_cif, nfixedargs, 32);
118
119 #define CIF__ABI(addr) DEREF_U32(addr, 0)
120 #define CIF__NARGS(addr) DEREF_U32(addr + 4, 0)
121 #define CIF__ARGTYPES(addr) DEREF_U64(addr + 8, 0)
122 #define CIF__RTYPE(addr) DEREF_U64(addr + 16, 0)
123 #define CIF__FLAGS(addr) DEREF_U32(addr + 28, 0)
124 #define CIF__NFIXEDARGS(addr) DEREF_U32(addr + 32, 0)
125
126 CHECK_FIELD_OFFSET(ffi_type, size, 0);
127 CHECK_FIELD_OFFSET(ffi_type, alignment, 8);
128 CHECK_FIELD_OFFSET(ffi_type, type, 10);
129 CHECK_FIELD_OFFSET(ffi_type, elements, 16);
130
131 #define FFI_TYPE__SIZE(addr) DEREF_U64(addr, 0)
132 #define FFI_TYPE__ALIGN(addr) DEREF_U16(addr + 8, 0)
133 #define FFI_TYPE__TYPEID(addr) DEREF_U16(addr + 10, 0)
134 #define FFI_TYPE__ELEMENTS(addr) DEREF_U64(addr + 16, 0)
135
136 #else
137 #error "Unknown pointer size"
138 #endif
139
140 #define ALIGN_ADDRESS(addr, align) (addr &= (~((align) - 1)))
141 #define STACK_ALLOC(stack, size, align) ((stack -= (size)), ALIGN_ADDRESS(stack, align))
142
143 // Most wasm runtimes support at most 1000 Js trampoline args.
144 #define MAX_ARGS 1000
145
146 #include <stddef.h>
147
148 #define VARARGS_FLAG 1
149
150 #define FFI_OK_MACRO 0
151 _Static_assert(FFI_OK_MACRO == FFI_OK, "FFI_OK must be 0");
152
153 #define FFI_BAD_TYPEDEF_MACRO 1
154 _Static_assert(FFI_BAD_TYPEDEF_MACRO == FFI_BAD_TYPEDEF, "FFI_BAD_TYPEDEF must be 1");
155
156 ffi_status FFI_HIDDEN
157 ffi_prep_cif_machdep(ffi_cif *cif)
158 {
159 if (cif->abi != FFI_EMSCRIPTEN_ABI)
160 return FFI_BAD_ABI;
161 // This is called after ffi_prep_cif_machdep_var so we need to avoid
162 // overwriting cif->nfixedargs.
163 if (!(cif->flags & VARARGS_FLAG))
164 cif->nfixedargs = cif->nargs;
165 if (cif->nargs > MAX_ARGS)
166 return FFI_BAD_TYPEDEF;
167 if (cif->rtype->type == FFI_TYPE_COMPLEX)
168 return FFI_BAD_TYPEDEF;
169 // If they put the COMPLEX type into a struct we won't notice, but whatever.
170 for (int i = 0; i < cif->nargs; i++)
171 if (cif->arg_types[i]->type == FFI_TYPE_COMPLEX)
172 return FFI_BAD_TYPEDEF;
173 return FFI_OK;
174 }
175
176 ffi_status FFI_HIDDEN
177 ffi_prep_cif_machdep_var(ffi_cif *cif, unsigned nfixedargs, unsigned ntotalargs)
178 {
179 cif->flags |= VARARGS_FLAG;
180 cif->nfixedargs = nfixedargs;
181 // The varargs takes up one extra argument
182 if (cif->nfixedargs + 1 > MAX_ARGS)
183 return FFI_BAD_TYPEDEF;
184 return FFI_OK;
185 }
186
187 /**
188 * A Javascript helper function. This takes an argument typ which is a wasm
189 * pointer to an ffi_type object. It returns a pair a type and a type id.
190 *
191 * - If it is not a struct, return its type and its typeid field.
192 * - If it is a struct of size >= 2, return the type and its typeid (which
193 * will be FFI_TYPE_STRUCT)
194 * - If it is a struct of size 0, return FFI_TYPE_VOID (????? this is broken)
195 * - If it is a struct of size 1, replace it with the single field and apply
196 * the same logic again to that.
197 *
198 * By always unboxing structs up front, we can avoid messy casework later.
199 */
200 EM_JS_MACROS(
201 void,
202 unbox_small_structs, (ffi_type type_ptr), {
203 type_ptr = DEC_PTR(type_ptr);
204 var type_id = FFI_TYPE__TYPEID(type_ptr);
205 while (type_id === FFI_TYPE_STRUCT) {
206 // Don't unbox single element structs if they are bigger than 16 bytes. This
207 // is a work around for the fact that Python will give incorrect values for
208 // the size of the field in these cases: it says that the struct has pointer
209 // size and alignment and are of type pointer, even though it is more
210 // accurately a struct and has a larger size. Keeping it as a struct here
211 // will let us get the ABI right (which is in fact that the true argument is
212 // a pointer to the stack... so maybe Python issn't so wrong??)
213 //
214 // See the Python comment here:
215 // https://github.com/python/cpython/blob/a16a9f978f42b8a09297c1efbf33877f6388c403/Modules/_ctypes/stgdict.c#L718-L779
216 if (DEC_PTR(FFI_TYPE__SIZE(type_ptr)) > 16) {
217 break;
218 }
219 var elements = DEC_PTR(FFI_TYPE__ELEMENTS(type_ptr));
220 var first_element = DEREF_PTR_NUMBER(elements, 0);
221 if (first_element === 0) {
222 type_id = FFI_TYPE_VOID;
223 break;
224 } else if (DEREF_PTR_NUMBER(elements, 1) === 0) {
225 type_ptr = first_element;
226 type_id = FFI_TYPE__TYPEID(first_element);
227 } else {
228 break;
229 }
230 }
231 return [type_ptr, type_id];
232 })
233
234 EM_JS_MACROS(
235 void,
236 ffi_call_js, (ffi_cif *cif, ffi_fp fn, void *rvalue, void **avalue),
237 {
238 cif = DEC_PTR(cif);
239 fn = DEC_PTR(fn);
240 rvalue = DEC_PTR(rvalue);
241 avalue = DEC_PTR(avalue);
242 var abi = CIF__ABI(cif);
243 var nargs = CIF__NARGS(cif);
244 var nfixedargs = CIF__NFIXEDARGS(cif);
245 var arg_types_ptr = DEC_PTR(CIF__ARGTYPES(cif));
246 var flags = CIF__FLAGS(cif);
247 var rtype_unboxed = unbox_small_structs(CIF__RTYPE(cif));
248 var rtype_ptr = rtype_unboxed[0];
249 var rtype_id = rtype_unboxed[1];
250 var orig_stack_ptr = stackSave();
251 var cur_stack_ptr = orig_stack_ptr;
252
253 var args = [];
254 // Does our onwards call return by argument or normally? We return by argument
255 // no matter what.
256 var ret_by_arg = false;
257
258 if (rtype_id === FFI_TYPE_COMPLEX) {
259 throw new Error('complex ret marshalling nyi');
260 }
261 if (rtype_id < 0 || rtype_id > FFI_TYPE_LAST) {
262 throw new Error('Unexpected rtype ' + rtype_id);
263 }
264 // If the return type is a struct with multiple entries or a long double, the
265 // function takes an extra first argument which is a pointer to return value.
266 // Conveniently, we've already received a pointer to return value, so we can
267 // just use this. We also mark a flag that we don't need to convert the return
268 // value of the dynamic call back to C.
269 if (rtype_id === FFI_TYPE_LONGDOUBLE || rtype_id === FFI_TYPE_STRUCT) {
270 args.push(ENC_PTR(rvalue));
271 ret_by_arg = true;
272 }
273
274 // Accumulate a Javascript list of arguments for the Javascript wrapper for
275 // the wasm function. The Javascript wrapper does a type conversion from
276 // Javascript to C automatically, here we manually do the inverse conversion
277 // from C to Javascript.
278 for (var i = 0; i < nfixedargs; i++) {
279 var arg_ptr = DEREF_PTR_NUMBER(avalue, i);
280 var arg_unboxed = unbox_small_structs(DEREF_PTR(arg_types_ptr, i));
281 var arg_type_ptr = arg_unboxed[0];
282 var arg_type_id = arg_unboxed[1];
283
284 // It's okay here to always use unsigned integers as long as the size is 32
285 // or 64 bits. Smaller sizes get extended to 32 bits differently according
286 // to whether they are signed or unsigned.
287 switch (arg_type_id) {
288 case FFI_TYPE_INT:
289 case FFI_TYPE_SINT32:
290 case FFI_TYPE_UINT32:
291 args.push(DEREF_U32(arg_ptr, 0));
292 break;
293 case FFI_TYPE_FLOAT:
294 args.push(DEREF_F32(arg_ptr, 0));
295 break;
296 case FFI_TYPE_DOUBLE:
297 args.push(DEREF_F64(arg_ptr, 0));
298 break;
299 case FFI_TYPE_UINT8:
300 args.push(DEREF_U8(arg_ptr, 0));
301 break;
302 case FFI_TYPE_SINT8:
303 args.push(DEREF_S8(arg_ptr, 0));
304 break;
305 case FFI_TYPE_UINT16:
306 args.push(DEREF_U16(arg_ptr, 0));
307 break;
308 case FFI_TYPE_SINT16:
309 args.push(DEREF_S16(arg_ptr, 0));
310 break;
311 case FFI_TYPE_UINT64:
312 case FFI_TYPE_SINT64:
313 args.push(DEREF_U64(arg_ptr, 0));
314 break;
315 case FFI_TYPE_LONGDOUBLE:
316 // long double is passed as a pair of BigInts.
317 args.push(DEREF_U64(arg_ptr, 0));
318 args.push(DEREF_U64(arg_ptr, 1));
319 break;
320 case FFI_TYPE_STRUCT:
321 // Nontrivial structs are passed by pointer.
322 // Have to copy the struct onto the stack though because C ABI says it's
323 // call by value.
324 var size = DEC_PTR(FFI_TYPE__SIZE(arg_type_ptr));
325 var align = FFI_TYPE__ALIGN(arg_type_ptr);
326 STACK_ALLOC(cur_stack_ptr, size, align);
327 HEAP8.subarray(cur_stack_ptr, cur_stack_ptr+size).set(HEAP8.subarray(arg_ptr, arg_ptr + size));
328 args.push(ENC_PTR(cur_stack_ptr));
329 break;
330 case FFI_TYPE_POINTER:
331 args.push(DEREF_PTR(arg_ptr, 0));
332 break;
333 case FFI_TYPE_COMPLEX:
334 throw new Error('complex marshalling nyi');
335 default:
336 throw new Error('Unexpected type ' + arg_type_id);
337 }
338 }
339
340 // Wasm functions can't directly manipulate the callstack, so varargs
341 // arguments have to go on a separate stack. A varags function takes one extra
342 // argument which is a pointer to where on the separate stack the args are
343 // located. Because stacks are allocated backwards, we have to loop over the
344 // varargs backwards.
345 //
346 // We don't have any way of knowing how many args were actually passed, so we
347 // just always copy extra nonsense past the end. The ownwards call will know
348 // not to look at it.
349 if (flags & VARARGS_FLAG) {
350 var struct_arg_info = [];
351 for (var i = nargs - 1; i >= nfixedargs; i--) {
352 var arg_ptr = DEREF_PTR_NUMBER(avalue, i);
353 var arg_unboxed = unbox_small_structs(DEREF_PTR(arg_types_ptr, i));
354 var arg_type_ptr = arg_unboxed[0];
355 var arg_type_id = arg_unboxed[1];
356 switch (arg_type_id) {
357 case FFI_TYPE_UINT8:
358 case FFI_TYPE_SINT8:
359 STACK_ALLOC(cur_stack_ptr, 1, 1);
360 DEREF_U8(cur_stack_ptr, 0) = DEREF_U8(arg_ptr, 0);
361 break;
362 case FFI_TYPE_UINT16:
363 case FFI_TYPE_SINT16:
364 STACK_ALLOC(cur_stack_ptr, 2, 2);
365 DEREF_U16(cur_stack_ptr, 0) = DEREF_U16(arg_ptr, 0);
366 break;
367 case FFI_TYPE_INT:
368 case FFI_TYPE_UINT32:
369 case FFI_TYPE_SINT32:
370 case FFI_TYPE_FLOAT:
371 STACK_ALLOC(cur_stack_ptr, 4, 4);
372 DEREF_U32(cur_stack_ptr, 0) = DEREF_U32(arg_ptr, 0);
373 break;
374 case FFI_TYPE_DOUBLE:
375 case FFI_TYPE_UINT64:
376 case FFI_TYPE_SINT64:
377 STACK_ALLOC(cur_stack_ptr, 8, 8);
378 DEREF_U32(cur_stack_ptr, 0) = DEREF_U32(arg_ptr, 0);
379 DEREF_U32(cur_stack_ptr, 1) = DEREF_U32(arg_ptr, 1);
380 break;
381 case FFI_TYPE_LONGDOUBLE:
382 STACK_ALLOC(cur_stack_ptr, 16, 8);
383 DEREF_U32(cur_stack_ptr, 0) = DEREF_U32(arg_ptr, 0);
384 DEREF_U32(cur_stack_ptr, 1) = DEREF_U32(arg_ptr, 1);
385 DEREF_U32(cur_stack_ptr, 2) = DEREF_U32(arg_ptr, 2);
386 DEREF_U32(cur_stack_ptr, 3) = DEREF_U32(arg_ptr, 3);
387 break;
388 case FFI_TYPE_STRUCT:
389 // Again, struct must be passed by pointer.
390 // But ABI is by value, so have to copy struct onto stack.
391 // Currently arguments are going onto stack so we can't put it there now. Come back for this.
392 STACK_ALLOC(cur_stack_ptr, __SIZEOF_POINTER__, __SIZEOF_POINTER__);
393 struct_arg_info.push([cur_stack_ptr, arg_ptr, DEC_PTR(FFI_TYPE__SIZE(arg_type_ptr)), FFI_TYPE__ALIGN(arg_type_ptr)]);
394 break;
395 case FFI_TYPE_POINTER:
396 STACK_ALLOC(cur_stack_ptr, __SIZEOF_POINTER__, __SIZEOF_POINTER__);
397 DEREF_PTR(cur_stack_ptr, 0) = DEREF_PTR(arg_ptr, 0);
398 break;
399 case FFI_TYPE_COMPLEX:
400 throw new Error('complex arg marshalling nyi');
401 default:
402 throw new Error('Unexpected argtype ' + arg_type_id);
403 }
404 }
405 // extra normal argument which is the pointer to the varargs.
406 args.push(ENC_PTR(cur_stack_ptr));
407 // Now allocate variable struct args on stack too.
408 for (var i = 0; i < struct_arg_info.length; i++) {
409 var struct_info = struct_arg_info[i];
410 var arg_target = struct_info[0];
411 var arg_ptr = struct_info[1];
412 var size = struct_info[2];
413 var align = struct_info[3];
414 STACK_ALLOC(cur_stack_ptr, size, align);
415 HEAP8.subarray(cur_stack_ptr, cur_stack_ptr+size).set(HEAP8.subarray(arg_ptr, arg_ptr + size));
416 DEREF_PTR(arg_target, 0) = ENC_PTR(cur_stack_ptr);
417 }
418 }
419 stackRestore(cur_stack_ptr);
420 stackAlloc(0); // stackAlloc enforces alignment invariants on the stack pointer
421 LOG_DEBUG("CALL_FUNC_PTR", "fn:", fn, "args:", args);
422 var result = getWasmTableEntry(fn).apply(null, args);
423 // Put the stack pointer back (we moved it if there were any struct args or we
424 // made a varargs call)
425 stackRestore(orig_stack_ptr);
426
427 // We need to return by argument. If return value was a nontrivial struct or
428 // long double, the onwards call already put the return value in rvalue
429 if (ret_by_arg) {
430 return;
431 }
432
433 // Otherwise the result was automatically converted from C into Javascript and
434 // we need to manually convert it back to C.
435 switch (rtype_id) {
436 case FFI_TYPE_VOID:
437 break;
438 case FFI_TYPE_INT:
439 case FFI_TYPE_UINT32:
440 case FFI_TYPE_SINT32:
441 DEREF_U32(rvalue, 0) = result;
442 break;
443 case FFI_TYPE_FLOAT:
444 DEREF_F32(rvalue, 0) = result;
445 break;
446 case FFI_TYPE_DOUBLE:
447 DEREF_F64(rvalue, 0) = result;
448 break;
449 case FFI_TYPE_UINT8:
450 case FFI_TYPE_SINT8:
451 DEREF_U8(rvalue, 0) = result;
452 break;
453 case FFI_TYPE_UINT16:
454 case FFI_TYPE_SINT16:
455 DEREF_U16(rvalue, 0) = result;
456 break;
457 case FFI_TYPE_UINT64:
458 case FFI_TYPE_SINT64:
459 DEREF_U64(rvalue, 0) = result;
460 break;
461 case FFI_TYPE_POINTER:
462 DEREF_PTR(rvalue, 0) = result;
463 break;
464 case FFI_TYPE_COMPLEX:
465 throw new Error('complex ret marshalling nyi');
466 default:
467 throw new Error('Unexpected rtype ' + rtype_id);
468 }
469 });
470
471 void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue) {
472 ffi_call_js(cif, fn, rvalue, avalue);
473 }
474
475 #if __SIZEOF_POINTER__ == 4
476
477 CHECK_FIELD_OFFSET(ffi_closure, ftramp, 4*0);
478 CHECK_FIELD_OFFSET(ffi_closure, cif, 4*1);
479 CHECK_FIELD_OFFSET(ffi_closure, fun, 4*2);
480 CHECK_FIELD_OFFSET(ffi_closure, user_data, 4*3);
481
482 #define CLOSURE__wrapper(addr) DEREF_U32(addr, 0)
483 #define CLOSURE__cif(addr) DEREF_U32(addr, 1)
484 #define CLOSURE__fun(addr) DEREF_U32(addr, 2)
485 #define CLOSURE__user_data(addr) DEREF_U32(addr, 3)
486
487 #elif __SIZEOF_POINTER__ == 8
488
489 CHECK_FIELD_OFFSET(ffi_closure, ftramp, 0);
490 CHECK_FIELD_OFFSET(ffi_closure, cif, 8);
491 CHECK_FIELD_OFFSET(ffi_closure, fun, 16);
492 CHECK_FIELD_OFFSET(ffi_closure, user_data, 24);
493
494 #define CLOSURE__wrapper(addr) DEREF_U64(addr, 0)
495 #define CLOSURE__cif(addr) DEREF_U64(addr, 1)
496 #define CLOSURE__fun(addr) DEREF_U64(addr, 2)
497 #define CLOSURE__user_data(addr) DEREF_U64(addr, 3)
498
499 #else
500 #error "Unknown pointer size"
501 #endif
502
503 EM_JS_MACROS(void *, ffi_closure_alloc_js, (size_t size, void **code), {
504 size = DEC_PTR(size);
505 code = DEC_PTR(code);
506 var closure = _malloc(size);
507 var index = getEmptyTableSlot();
508 DEREF_PTR(code, 0) = ENC_PTR(index);
509 CLOSURE__wrapper(closure) = ENC_PTR(index);
510 return ENC_PTR(closure);
511 })
512
513 void * __attribute__ ((visibility ("default")))
514 ffi_closure_alloc(size_t size, void **code) {
515 return ffi_closure_alloc_js(size, code);
516 }
517
518 EM_JS_MACROS(void, ffi_closure_free_js, (void *closure), {
519 closure = DEC_PTR(closure);
520 var index = DEC_PTR(CLOSURE__wrapper(closure));
521 freeTableIndexes.push(index);
522 _free(closure);
523 })
524
525 void __attribute__ ((visibility ("default")))
526 ffi_closure_free(void *closure) {
527 return ffi_closure_free_js(closure);
528 }
529
530 EM_JS_MACROS(
531 ffi_status,
532 ffi_prep_closure_loc_js,
533 (ffi_closure *closure, ffi_cif *cif, void *fun, void *user_data, void *codeloc),
534 {
535 closure = DEC_PTR(closure);
536 cif = DEC_PTR(cif);
537 fun = DEC_PTR(fun);
538 user_data = DEC_PTR(user_data);
539 codeloc = DEC_PTR(codeloc);
540 var abi = CIF__ABI(cif);
541 var nargs = CIF__NARGS(cif);
542 var nfixedargs = CIF__NFIXEDARGS(cif);
543 var arg_types_ptr = DEC_PTR(CIF__ARGTYPES(cif));
544 var rtype_unboxed = unbox_small_structs(CIF__RTYPE(cif));
545 var rtype_ptr = rtype_unboxed[0];
546 var rtype_id = rtype_unboxed[1];
547
548 // First construct the signature of the javascript trampoline we are going to create.
549 // Important: this is the signature for calling us, the onward call always has sig viiii.
550 var sig;
551 var ret_by_arg = false;
552 switch (rtype_id) {
553 case FFI_TYPE_VOID:
554 sig = 'v';
555 break;
556 case FFI_TYPE_STRUCT:
557 case FFI_TYPE_LONGDOUBLE:
558 // Return via a first pointer argument.
559 sig = 'v' + PTR_SIG;
560 ret_by_arg = true;
561 break;
562 case FFI_TYPE_INT:
563 case FFI_TYPE_UINT8:
564 case FFI_TYPE_SINT8:
565 case FFI_TYPE_UINT16:
566 case FFI_TYPE_SINT16:
567 case FFI_TYPE_UINT32:
568 case FFI_TYPE_SINT32:
569 sig = 'i';
570 break;
571 case FFI_TYPE_FLOAT:
572 sig = 'f';
573 break;
574 case FFI_TYPE_DOUBLE:
575 sig = 'd';
576 break;
577 case FFI_TYPE_UINT64:
578 case FFI_TYPE_SINT64:
579 sig = 'j';
580 break;
581 case FFI_TYPE_POINTER:
582 sig = PTR_SIG;
583 break;
584 case FFI_TYPE_COMPLEX:
585 throw new Error('complex ret marshalling nyi');
586 default:
587 throw new Error('Unexpected rtype ' + rtype_id);
588 }
589 var unboxed_arg_type_id_list = [];
590 var unboxed_arg_type_info_list = [];
591 for (var i = 0; i < nargs; i++) {
592 var arg_unboxed = unbox_small_structs(DEREF_PTR(arg_types_ptr, i));
593 var arg_type_ptr = arg_unboxed[0];
594 var arg_type_id = arg_unboxed[1];
595 unboxed_arg_type_id_list.push(arg_type_id);
596 unboxed_arg_type_info_list.push([DEC_PTR(FFI_TYPE__SIZE(arg_type_ptr)), FFI_TYPE__ALIGN(arg_type_ptr)]);
597 }
598 for (var i = 0; i < nfixedargs; i++) {
599 switch (unboxed_arg_type_id_list[i]) {
600 case FFI_TYPE_INT:
601 case FFI_TYPE_UINT8:
602 case FFI_TYPE_SINT8:
603 case FFI_TYPE_UINT16:
604 case FFI_TYPE_SINT16:
605 case FFI_TYPE_UINT32:
606 case FFI_TYPE_SINT32:
607 sig += 'i';
608 break;
609 case FFI_TYPE_FLOAT:
610 sig += 'f';
611 break;
612 case FFI_TYPE_DOUBLE:
613 sig += 'd';
614 break;
615 case FFI_TYPE_LONGDOUBLE:
616 sig += 'jj';
617 break;
618 case FFI_TYPE_UINT64:
619 case FFI_TYPE_SINT64:
620 sig += 'j';
621 break;
622 case FFI_TYPE_STRUCT:
623 case FFI_TYPE_POINTER:
624 sig += PTR_SIG;
625 break;
626 case FFI_TYPE_COMPLEX:
627 throw new Error('complex marshalling nyi');
628 default:
629 throw new Error('Unexpected argtype ' + arg_type_id);
630 }
631 }
632 if (nfixedargs < nargs) {
633 // extra pointer to varargs stack
634 sig += PTR_SIG;
635 }
636 LOG_DEBUG("CREATE_CLOSURE", "sig:", sig);
637 function trampoline() {
638 var args = Array.prototype.slice.call(arguments);
639 var size = 0;
640 var orig_stack_ptr = stackSave();
641 var cur_ptr = orig_stack_ptr;
642 var ret_ptr;
643 var jsarg_idx = 0;
644 // Should we return by argument or not? The onwards call returns by argument
645 // no matter what. (Warning: ret_by_arg means the opposite in ffi_call)
646 if (ret_by_arg) {
647 ret_ptr = args[jsarg_idx++];
648 } else {
649 // We might return 4 bytes or 8 bytes, allocate 8 just in case.
650 STACK_ALLOC(cur_ptr, 8, 8);
651 ret_ptr = cur_ptr;
652 }
653 cur_ptr -= __SIZEOF_POINTER__ * nargs;
654 var args_ptr = cur_ptr;
655 var carg_idx = 0;
656 // Here we either have the actual argument, or a pair of BigInts for long
657 // double, or a pointer to struct. We have to store into args_ptr[i] a
658 // pointer to the ith argument. If the argument is a struct, just store the
659 // pointer. Otherwise allocate stack space and copy the js argument onto the
660 // stack.
661 for (; carg_idx < nfixedargs; carg_idx++) {
662 // jsarg_idx might start out as 0 or 1 depending on ret_by_arg
663 // it advances an extra time for long double
664 var cur_arg = args[jsarg_idx++];
665 var arg_type_info = unboxed_arg_type_info_list[carg_idx];
666 var arg_size = arg_type_info[0];
667 var arg_align = arg_type_info[1];
668 var arg_type_id = unboxed_arg_type_id_list[carg_idx];
669 switch (arg_type_id) {
670 case FFI_TYPE_UINT8:
671 case FFI_TYPE_SINT8:
672 // Bad things happen if we don't align to 4 here
673 STACK_ALLOC(cur_ptr, 1, 4);
674 DEREF_PTR(args_ptr, carg_idx) = ENC_PTR(cur_ptr);
675 DEREF_U8(cur_ptr, 0) = cur_arg;
676 break;
677 case FFI_TYPE_UINT16:
678 case FFI_TYPE_SINT16:
679 // Bad things happen if we don't align to 4 here
680 STACK_ALLOC(cur_ptr, 2, 4);
681 DEREF_PTR(args_ptr, carg_idx) = ENC_PTR(cur_ptr);
682 DEREF_U16(cur_ptr, 0) = cur_arg;
683 break;
684 case FFI_TYPE_INT:
685 case FFI_TYPE_UINT32:
686 case FFI_TYPE_SINT32:
687 STACK_ALLOC(cur_ptr, 4, 4);
688 DEREF_PTR(args_ptr, carg_idx) = ENC_PTR(cur_ptr);
689 DEREF_U32(cur_ptr, 0) = cur_arg;
690 break;
691 case FFI_TYPE_STRUCT:
692 // cur_arg is already a pointer to struct
693 // copy it onto stack to pass by value
694 STACK_ALLOC(cur_ptr, arg_size, arg_align);
695 HEAP8.subarray(cur_ptr, cur_ptr + arg_size).set(HEAP8.subarray(DEC_PTR(cur_arg), DEC_PTR(cur_arg) + arg_size));
696 DEREF_PTR(args_ptr, carg_idx) = ENC_PTR(cur_ptr);
697 break;
698 case FFI_TYPE_FLOAT:
699 STACK_ALLOC(cur_ptr, 4, 4);
700 DEREF_PTR(args_ptr, carg_idx) = ENC_PTR(cur_ptr);
701 DEREF_F32(cur_ptr, 0) = cur_arg;
702 break;
703 case FFI_TYPE_DOUBLE:
704 STACK_ALLOC(cur_ptr, 8, 8);
705 DEREF_PTR(args_ptr, carg_idx) = ENC_PTR(cur_ptr);
706 DEREF_F64(cur_ptr, 0) = cur_arg;
707 break;
708 case FFI_TYPE_UINT64:
709 case FFI_TYPE_SINT64:
710 STACK_ALLOC(cur_ptr, 8, 8);
711 DEREF_PTR(args_ptr, carg_idx) = ENC_PTR(cur_ptr);
712 DEREF_U64(cur_ptr, 0) = cur_arg;
713 break;
714 case FFI_TYPE_LONGDOUBLE:
715 STACK_ALLOC(cur_ptr, 16, 8);
716 DEREF_PTR(args_ptr, carg_idx) = ENC_PTR(cur_ptr);
717 DEREF_U64(cur_ptr, 0) = cur_arg;
718 cur_arg = args[jsarg_idx++];
719 DEREF_U64(cur_ptr, 1) = cur_arg;
720 break;
721 case FFI_TYPE_POINTER:
722 STACK_ALLOC(cur_ptr, __SIZEOF_POINTER__, __SIZEOF_POINTER__);
723 DEREF_PTR(args_ptr, carg_idx) = ENC_PTR(cur_ptr);
724 DEREF_PTR(cur_ptr, 0) = cur_arg;
725 break;
726 }
727 }
728 // If its a varargs call, last js argument is a pointer to the varargs.
729 var varargs = DEC_PTR(args[args.length - 1]);
730 // We have no way of knowing how many varargs were actually provided, this
731 // fills the rest of the stack space allocated with nonsense. The onward
732 // call will know to ignore the nonsense.
733
734 // We either have a pointer to the argument if the argument is not a struct
735 // or a pointer to pointer to struct. We need to store a pointer to the
736 // argument into args_ptr[i]
737 for (; carg_idx < nargs; carg_idx++) {
738 var arg_type_id = unboxed_arg_type_id_list[carg_idx];
739 var arg_type_info = unboxed_arg_type_info_list[carg_idx];
740 var arg_size = arg_type_info[0];
741 var arg_align = arg_type_info[1];
742 if (arg_type_id === FFI_TYPE_STRUCT) {
743 // In this case varargs is a pointer to pointer to struct so we need to
744 // deref once
745 var struct_ptr = DEREF_PTR_NUMBER(varargs, 0);
746 STACK_ALLOC(cur_ptr, arg_size, arg_align);
747 HEAP8.subarray(cur_ptr, cur_ptr + arg_size).set(HEAP8.subarray(struct_ptr, struct_ptr + arg_size));
748 DEREF_PTR(args_ptr, carg_idx) = ENC_PTR(cur_ptr);
749 } else {
750 DEREF_PTR(args_ptr, carg_idx) = ENC_PTR(varargs);
751 }
752 varargs += __SIZEOF_POINTER__;
753 }
754 stackRestore(cur_ptr);
755 stackAlloc(0); // stackAlloc enforces alignment invariants on the stack pointer
756 LOG_DEBUG("CALL_CLOSURE", "closure:", closure, "fptr", CLOSURE__fun(closure), "cif", CLOSURE__cif(closure));
757 getWasmTableEntry(CLOSURE__fun(closure))(
758 CLOSURE__cif(closure), ENC_PTR(ret_ptr), ENC_PTR(args_ptr),
759 CLOSURE__user_data(closure)
760 );
761 stackRestore(orig_stack_ptr);
762
763 // If we aren't supposed to return by argument, figure out what to return.
764 if (!ret_by_arg) {
765 switch (sig[0]) {
766 case 'i':
767 return DEREF_U32(ret_ptr, 0);
768 case 'j':
769 return DEREF_U64(ret_ptr, 0);
770 case 'd':
771 return DEREF_F64(ret_ptr, 0);
772 case 'f':
773 return DEREF_F32(ret_ptr, 0);
774 }
775 }
776 }
777 try {
778 var wasm_trampoline = convertJsFunctionToWasm(trampoline, sig);
779 } catch(e) {
780 return FFI_BAD_TYPEDEF_MACRO;
781 }
782 setWasmTableEntry(codeloc, wasm_trampoline);
783 CLOSURE__cif(closure) = ENC_PTR(cif);
784 CLOSURE__fun(closure) = ENC_PTR(fun);
785 CLOSURE__user_data(closure) = ENC_PTR(user_data);
786 return FFI_OK_MACRO;
787 })
788
789 // EM_JS does not correctly handle function pointer arguments, so we need a
790 // helper
791 ffi_status ffi_prep_closure_loc(ffi_closure *closure, ffi_cif *cif,
792 void (*fun)(ffi_cif *, void *, void **, void *),
793 void *user_data, void *codeloc) {
794 if (cif->abi != FFI_EMSCRIPTEN_ABI)
795 return FFI_BAD_ABI;
796 return ffi_prep_closure_loc_js(closure, cif, (void *)fun, user_data,
797 codeloc);
798 }