| 1 |
/* vim: ts=4 sw=4 sts=4 et tw=78 |
| 2 |
* Portions copyright (c) 2015-present, Facebook, Inc. All rights reserved. |
| 3 |
* Portions copyright (c) 2011 James R. McKaskill. |
| 4 |
* |
| 5 |
* This source code is licensed under the BSD-style license found in the |
| 6 |
* LICENSE file in the root directory of this source tree. An additional grant |
| 7 |
* of patent rights can be found in the PATENTS file in the same directory. |
| 8 |
*/ |
| 9 |
|
| 10 |
#pragma once |
| 11 |
|
| 12 |
#ifdef _MSC_VER |
| 13 |
#define _CRT_SECURE_NO_WARNINGS |
| 14 |
#endif |
| 15 |
|
| 16 |
#include <stdint.h> |
| 17 |
#include <stdbool.h> |
| 18 |
#include <stdlib.h> |
| 19 |
#include <string.h> |
| 20 |
#include <assert.h> |
| 21 |
|
| 22 |
/* |
| 23 |
#include <execinfo.h> |
| 24 |
#define STACK_TRACE() {\ |
| 25 |
void* callstack[128]; \ |
| 26 |
int i, frames = backtrace(callstack, 128); \ |
| 27 |
backtrace_symbols_fd(callstack, frames, STDERR_FILENO); \ |
| 28 |
} |
| 29 |
*/ |
| 30 |
|
| 31 |
#ifdef __cplusplus |
| 32 |
extern "C" { |
| 33 |
# include <lua.h> |
| 34 |
# include <lauxlib.h> |
| 35 |
# include <lualib.h> |
| 36 |
} |
| 37 |
# define EXTERN_C extern "C" |
| 38 |
#else |
| 39 |
# include <lua.h> |
| 40 |
# include <lauxlib.h> |
| 41 |
# include <lualib.h> |
| 42 |
# define EXTERN_C extern |
| 43 |
#endif |
| 44 |
|
| 45 |
#ifdef _WIN32 |
| 46 |
#include <windows.h> |
| 47 |
#else |
| 48 |
#include <errno.h> |
| 49 |
#include <unistd.h> |
| 50 |
#include <dlfcn.h> |
| 51 |
#include <sys/mman.h> |
| 52 |
#endif |
| 53 |
|
| 54 |
#include <complex.h> |
| 55 |
#define HAVE_COMPLEX |
| 56 |
#define HAVE_LONG_DOUBLE |
| 57 |
|
| 58 |
#ifndef NDEBUG |
| 59 |
#define DASM_CHECKS |
| 60 |
#endif |
| 61 |
|
| 62 |
struct jit; |
| 63 |
#define Dst_DECL struct jit* Dst |
| 64 |
#define Dst_REF (Dst->ctx) |
| 65 |
#define DASM_EXTERN(a,b,c,d) get_extern(a,b,c,d) |
| 66 |
|
| 67 |
#include "dynasm/dasm_proto.h" |
| 68 |
|
| 69 |
#if defined LUA_FFI_BUILD_AS_DLL |
| 70 |
# define EXPORT __declspec(dllexport) |
| 71 |
#elif defined __GNUC__ |
| 72 |
# define EXPORT __attribute__((visibility("default"))) |
| 73 |
#else |
| 74 |
# define EXPORT |
| 75 |
#endif |
| 76 |
|
| 77 |
EXTERN_C EXPORT int luaopen_ffi(lua_State* L); |
| 78 |
|
| 79 |
static int lua_absindex2(lua_State* L, int idx) { |
| 80 |
return (LUA_REGISTRYINDEX <= idx && idx < 0) |
| 81 |
? lua_gettop(L) + idx + 1 |
| 82 |
: idx; |
| 83 |
} |
| 84 |
/* use our own version of lua_absindex such that lua_absindex(L, 0) == 0 */ |
| 85 |
#define lua_absindex(L, idx) lua_absindex2(L, idx) |
| 86 |
|
| 87 |
#if LUA_VERSION_NUM == 501 |
| 88 |
static void lua_callk(lua_State *L, int nargs, int nresults, int ctx, lua_CFunction k) |
| 89 |
{ |
| 90 |
lua_call(L, nargs, nresults); |
| 91 |
} |
| 92 |
/* |
| 93 |
** set functions from list 'l' into table at top - 'nup'; each |
| 94 |
** function gets the 'nup' elements at the top as upvalues. |
| 95 |
** Returns with only the table at the stack. |
| 96 |
*/ |
| 97 |
static void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { |
| 98 |
luaL_checkstack(L, nup, "too many upvalues"); |
| 99 |
for (; l && l->name; l++) { /* fill the table with given functions */ |
| 100 |
int i; |
| 101 |
for (i = 0; i < nup; i++) /* copy upvalues to the top */ |
| 102 |
lua_pushvalue(L, -nup); |
| 103 |
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ |
| 104 |
lua_setfield(L, -(nup + 2), l->name); |
| 105 |
} |
| 106 |
lua_pop(L, nup); /* remove upvalues */ |
| 107 |
} |
| 108 |
#define lua_setuservalue lua_setfenv |
| 109 |
#define lua_getuservalue lua_getfenv |
| 110 |
#define lua_rawlen lua_objlen |
| 111 |
static char* luaL_prepbuffsize(luaL_Buffer* B, size_t sz) { |
| 112 |
if (sz > LUAL_BUFFERSIZE) { |
| 113 |
luaL_error(B->L, "string too long"); |
| 114 |
} |
| 115 |
return luaL_prepbuffer(B); |
| 116 |
} |
| 117 |
#elif LUA_VERSION_NUM >= 503 |
| 118 |
static void (lua_remove)(lua_State *L, int idx) { |
| 119 |
lua_remove(L, idx); |
| 120 |
} |
| 121 |
#endif |
| 122 |
|
| 123 |
/* architectures */ |
| 124 |
#if defined _WIN32 && defined UNDER_CE |
| 125 |
# define OS_CE |
| 126 |
#elif defined _WIN32 |
| 127 |
# define OS_WIN |
| 128 |
#elif defined __APPLE__ && defined __MACH__ |
| 129 |
# define OS_OSX |
| 130 |
#elif defined __linux__ |
| 131 |
# define OS_LINUX |
| 132 |
#elif defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__ |
| 133 |
# define OS_BSD |
| 134 |
#elif defined unix || defined __unix__ || defined __unix || defined _POSIX_VERSION || defined _XOPEN_VERSION |
| 135 |
# define OS_POSIX |
| 136 |
#endif |
| 137 |
|
| 138 |
/* architecture */ |
| 139 |
#if defined __i386__ || defined _M_IX86 |
| 140 |
# define ARCH_X86 |
| 141 |
#elif defined __amd64__ || defined _M_X64 |
| 142 |
# define ARCH_X64 |
| 143 |
#elif defined __arm__ || defined __ARM__ || defined ARM || defined __ARM || defined __arm |
| 144 |
# define ARCH_ARM |
| 145 |
#elif defined __aarch64__ |
| 146 |
# define ARCH_ARM64 |
| 147 |
#elif defined __powerpc64__ |
| 148 |
# define ARCH_PPC64 |
| 149 |
#else |
| 150 |
# error |
| 151 |
#endif |
| 152 |
|
| 153 |
|
| 154 |
#ifdef _WIN32 |
| 155 |
|
| 156 |
# ifdef UNDER_CE |
| 157 |
static void* DoLoadLibraryA(const char* name) { |
| 158 |
wchar_t buf[MAX_PATH]; |
| 159 |
int sz = MultiByteToWideChar(CP_UTF8, 0, name, -1, buf, 512); |
| 160 |
if (sz > 0) { |
| 161 |
buf[sz] = 0; |
| 162 |
return LoadLibraryW(buf); |
| 163 |
} else { |
| 164 |
return NULL; |
| 165 |
} |
| 166 |
} |
| 167 |
# define LoadLibraryA DoLoadLibraryA |
| 168 |
# else |
| 169 |
# define GetProcAddressA GetProcAddress |
| 170 |
# endif |
| 171 |
|
| 172 |
# define LIB_FORMAT_1 "%s.dll" |
| 173 |
# define AllocPage(size) VirtualAlloc(NULL, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE) |
| 174 |
# define FreePage(data, size) VirtualFree(data, 0, MEM_RELEASE) |
| 175 |
# define EnableExecute(data, size) do {DWORD old; VirtualProtect(data, size, PAGE_EXECUTE, &old); FlushInstructionCache(GetCurrentProcess(), data, size);} while (0) |
| 176 |
# define EnableWrite(data, size) do {DWORD old; VirtualProtect(data, size, PAGE_READWRITE, &old);} while (0) |
| 177 |
|
| 178 |
#else |
| 179 |
#ifdef OS_OSX |
| 180 |
# define LIB_FORMAT_1 "%s.dylib" |
| 181 |
# define LIB_FORMAT_2 "lib%s.dylib" |
| 182 |
#else |
| 183 |
# define LIB_FORMAT_1 "%s.so" |
| 184 |
# define LIB_FORMAT_2 "lib%s.so" |
| 185 |
#endif |
| 186 |
# define LoadLibraryA(name) dlopen(name, RTLD_LAZY | RTLD_GLOBAL) |
| 187 |
# define GetProcAddressA(lib, name) dlsym(lib, name) |
| 188 |
# define AllocPage(size) mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0) |
| 189 |
# define FreePage(data, size) munmap(data, size) |
| 190 |
# define EnableExecute(data, size) mprotect(data, size, PROT_READ|PROT_EXEC) |
| 191 |
# define EnableWrite(data, size) mprotect(data, size, PROT_READ|PROT_WRITE) |
| 192 |
#endif |
| 193 |
|
| 194 |
//#if defined ARCH_X86 || defined ARCH_X64 |
| 195 |
#if defined ARCH_X86 || defined ARCH_X64 || defined ARCH_ARM64 |
| 196 |
#define ALLOW_MISALIGNED_ACCESS |
| 197 |
#endif |
| 198 |
|
| 199 |
struct token; |
| 200 |
|
| 201 |
struct parser { |
| 202 |
int line; |
| 203 |
const char* next; |
| 204 |
const char* prev; |
| 205 |
unsigned align_mask; |
| 206 |
}; |
| 207 |
|
| 208 |
struct page { |
| 209 |
size_t size; |
| 210 |
size_t off; |
| 211 |
size_t freed; |
| 212 |
}; |
| 213 |
|
| 214 |
struct jit { |
| 215 |
lua_State* L; |
| 216 |
int32_t last_errno; |
| 217 |
dasm_State* ctx; |
| 218 |
size_t pagenum; |
| 219 |
struct page** pages; |
| 220 |
size_t align_page_size; |
| 221 |
void** globals; |
| 222 |
int function_extern; |
| 223 |
void* lua_dll; |
| 224 |
void* kernel32_dll; |
| 225 |
}; |
| 226 |
|
| 227 |
#define ALIGN_DOWN(PTR, MASK) \ |
| 228 |
(((uintptr_t) (PTR)) & (~ ((uintptr_t) (MASK)) )) |
| 229 |
#define ALIGN_UP(PTR, MASK) \ |
| 230 |
(( ((uintptr_t) (PTR)) + ((uintptr_t) (MASK)) ) & (~ ((uintptr_t) (MASK)) )) |
| 231 |
|
| 232 |
/* struct cdata/struct ctype */ |
| 233 |
|
| 234 |
#define PTR_ALIGN_MASK (sizeof(void*) - 1) |
| 235 |
#define FUNCTION_ALIGN_MASK (sizeof(void (*)()) - 1) |
| 236 |
#define DEFAULT_ALIGN_MASK 7 |
| 237 |
|
| 238 |
#ifdef OS_OSX |
| 239 |
/* TODO: figure out why the alignof trick doesn't work on OS X */ |
| 240 |
#define ALIGNED_DEFAULT 7 |
| 241 |
#elif defined __GNUC__ |
| 242 |
#define ALIGNED_DEFAULT (__alignof__(void* __attribute__((aligned))) - 1) |
| 243 |
#else |
| 244 |
#define ALIGNED_DEFAULT PTR_ALIGN_MASK |
| 245 |
#endif |
| 246 |
|
| 247 |
extern int jit_key; |
| 248 |
extern int ctype_mt_key; |
| 249 |
extern int cdata_mt_key; |
| 250 |
extern int cmodule_mt_key; |
| 251 |
extern int callback_mt_key; |
| 252 |
extern int constants_key; |
| 253 |
extern int types_key; |
| 254 |
extern int gc_key; |
| 255 |
extern int callbacks_key; |
| 256 |
extern int functions_key; |
| 257 |
extern int abi_key; |
| 258 |
extern int next_unnamed_key; |
| 259 |
extern int niluv_key; |
| 260 |
extern int asmname_key; |
| 261 |
|
| 262 |
int equals_upval(lua_State* L, int idx, int* key); |
| 263 |
void push_upval(lua_State* L, int* key); |
| 264 |
void set_upval(lua_State* L, int* key); |
| 265 |
struct jit* get_jit(lua_State* L); |
| 266 |
|
| 267 |
/* both ctype and cdata are stored as userdatas |
| 268 |
* |
| 269 |
* usr value is a table shared between the related subtypes which has: |
| 270 |
* name -> member ctype (for structs and unions) |
| 271 |
* +ves -> member ctype - in memory order (for structs) |
| 272 |
* +ves -> argument ctype (for function prototypes) |
| 273 |
* 0 -> return ctype (for function prototypes) |
| 274 |
* light userdata -> misc |
| 275 |
*/ |
| 276 |
|
| 277 |
enum { |
| 278 |
C_CALL, |
| 279 |
STD_CALL, |
| 280 |
FAST_CALL, |
| 281 |
}; |
| 282 |
|
| 283 |
enum { |
| 284 |
INVALID_TYPE, |
| 285 |
VOID_TYPE, |
| 286 |
FLOAT_TYPE, |
| 287 |
DOUBLE_TYPE, |
| 288 |
LONG_DOUBLE_TYPE, |
| 289 |
COMPLEX_FLOAT_TYPE, |
| 290 |
COMPLEX_DOUBLE_TYPE, |
| 291 |
COMPLEX_LONG_DOUBLE_TYPE, |
| 292 |
BOOL_TYPE, |
| 293 |
INT8_TYPE, |
| 294 |
INT16_TYPE, |
| 295 |
INT32_TYPE, |
| 296 |
INT64_TYPE, |
| 297 |
INTPTR_TYPE, |
| 298 |
ENUM_TYPE, |
| 299 |
UNION_TYPE, |
| 300 |
STRUCT_TYPE, |
| 301 |
FUNCTION_TYPE, |
| 302 |
FUNCTION_PTR_TYPE, |
| 303 |
}; |
| 304 |
|
| 305 |
#define IS_CHAR_UNSIGNED (((char) -1) > 0) |
| 306 |
#define IS_COMPLEX(type) ((type) == COMPLEX_FLOAT_TYPE || (type) == COMPLEX_DOUBLE_TYPE) |
| 307 |
|
| 308 |
#define POINTER_BITS 2 |
| 309 |
#define POINTER_MAX ((1 << POINTER_BITS) - 1) |
| 310 |
|
| 311 |
#define ALIGNOF(S) ((int) ((char*) &S.v - (char*) &S - 1)) |
| 312 |
|
| 313 |
/* Note: if adding a new member that is associated with a struct/union |
| 314 |
* definition then it needs to be copied over in ctype.c:set_defined for when |
| 315 |
* we create types based off of the declaration alone. |
| 316 |
* |
| 317 |
* Since this is used as a header for every ctype and cdata, and we create a |
| 318 |
* ton of them on the stack, we try and minimise its size. |
| 319 |
*/ |
| 320 |
struct ctype { |
| 321 |
size_t base_size; /* size of the base type in bytes */ |
| 322 |
|
| 323 |
union { |
| 324 |
/* valid if is_bitfield */ |
| 325 |
struct { |
| 326 |
/* size of bitfield in bits */ |
| 327 |
unsigned bit_size : 7; |
| 328 |
/* offset within the current byte between 0-63 */ |
| 329 |
unsigned bit_offset : 6; |
| 330 |
}; |
| 331 |
/* Valid if is_array */ |
| 332 |
size_t array_size; |
| 333 |
/* Valid for is_variable_struct or is_variable_array. If |
| 334 |
* variable_size_known (only used for is_variable_struct) then this is |
| 335 |
* the total increment otherwise this is the per element increment. |
| 336 |
*/ |
| 337 |
size_t variable_increment; |
| 338 |
}; |
| 339 |
size_t offset; |
| 340 |
unsigned align_mask : 4; /* as (align bytes - 1) eg 7 gives 8 byte alignment */ |
| 341 |
unsigned pointers : POINTER_BITS; /* number of dereferences to get to the base type including +1 for arrays */ |
| 342 |
unsigned const_mask : POINTER_MAX + 1; /* const pointer mask, LSB is current pointer, +1 for the whether the base type is const */ |
| 343 |
unsigned type : 5; /* value given by type enum above */ |
| 344 |
unsigned is_reference : 1; |
| 345 |
unsigned is_array : 1; |
| 346 |
unsigned is_defined : 1; |
| 347 |
unsigned is_null : 1; |
| 348 |
unsigned has_member_name : 1; |
| 349 |
unsigned calling_convention : 2; |
| 350 |
unsigned has_var_arg : 1; |
| 351 |
unsigned is_variable_array : 1; /* set for variable array types where we don't know the variable size yet */ |
| 352 |
unsigned is_variable_struct : 1; |
| 353 |
unsigned variable_size_known : 1; /* used for variable structs after we know the variable size */ |
| 354 |
unsigned is_bitfield : 1; |
| 355 |
unsigned has_bitfield : 1; |
| 356 |
unsigned is_jitted : 1; |
| 357 |
unsigned is_packed : 1; |
| 358 |
unsigned is_unsigned : 1; |
| 359 |
}; |
| 360 |
|
| 361 |
#ifdef _MSC_VER |
| 362 |
__declspec(align(16)) |
| 363 |
#endif |
| 364 |
struct cdata { |
| 365 |
const struct ctype type |
| 366 |
#ifdef __GNUC__ |
| 367 |
__attribute__ ((aligned(16))) |
| 368 |
#endif |
| 369 |
; |
| 370 |
}; |
| 371 |
|
| 372 |
typedef void (*cfunction)(void); |
| 373 |
|
| 374 |
#ifdef HAVE_COMPLEX |
| 375 |
typedef double complex complex_double; |
| 376 |
typedef float complex complex_float; |
| 377 |
static complex_double mk_complex_double(double real, double imag) { |
| 378 |
return real + imag * 1i; |
| 379 |
} |
| 380 |
static complex_double mk_complex_float(double real, double imag) { |
| 381 |
return real + imag * 1i; |
| 382 |
} |
| 383 |
#else |
| 384 |
typedef struct { |
| 385 |
double real, imag; |
| 386 |
} complex_double; |
| 387 |
|
| 388 |
typedef struct { |
| 389 |
float real, imag; |
| 390 |
} complex_float; |
| 391 |
|
| 392 |
static complex_double mk_complex_double(double real, double imag) { |
| 393 |
complex_double ret = { real, imag }; |
| 394 |
return ret; |
| 395 |
} |
| 396 |
static complex_float mk_complex_float(double real, double imag) { |
| 397 |
complex_float ret = { real, imag }; |
| 398 |
return ret; |
| 399 |
} |
| 400 |
static double creal(complex_double c) { |
| 401 |
return c.real; |
| 402 |
} |
| 403 |
static float crealf(complex_float c) { |
| 404 |
return c.real; |
| 405 |
} |
| 406 |
|
| 407 |
static double cimag(complex_double c) { |
| 408 |
return c.imag; |
| 409 |
} |
| 410 |
static float cimagf(complex_float c) { |
| 411 |
return c.imag; |
| 412 |
} |
| 413 |
#endif |
| 414 |
|
| 415 |
#define CALLBACK_FUNC_USR_IDX 1 |
| 416 |
|
| 417 |
void set_defined(lua_State* L, int ct_usr, struct ctype* ct); |
| 418 |
struct ctype* push_ctype(lua_State* L, int ct_usr, const struct ctype* ct); |
| 419 |
void* push_cdata(lua_State* L, int ct_usr, const struct ctype* ct); /* called from asm */ |
| 420 |
void push_callback(lua_State* L, cfunction luafunc, cfunction cfunc); |
| 421 |
void check_ctype(lua_State* L, int idx, struct ctype* ct); |
| 422 |
void* to_cdata(lua_State* L, int idx, struct ctype* ct); |
| 423 |
void* check_cdata(lua_State* L, int idx, struct ctype* ct); |
| 424 |
size_t ctype_size(lua_State* L, const struct ctype* ct); |
| 425 |
|
| 426 |
int parse_type(lua_State* L, struct parser* P, struct ctype* type); |
| 427 |
void parse_argument(lua_State* L, struct parser* P, int ct_usr, struct ctype* type, struct token* name, struct parser* asmname); |
| 428 |
void push_type_name(lua_State* L, int usr, const struct ctype* ct); |
| 429 |
|
| 430 |
int push_user_mt(lua_State* L, int ct_usr, const struct ctype* ct); |
| 431 |
|
| 432 |
int ffi_cdef(lua_State* L); |
| 433 |
|
| 434 |
void push_func_ref(lua_State* L, cfunction func); |
| 435 |
void free_code(struct jit* jit, lua_State* L, cfunction func); |
| 436 |
int x86_return_size(lua_State* L, int usr, const struct ctype* ct); |
| 437 |
void compile_function(lua_State* L, cfunction f, int ct_usr, const struct ctype* ct); |
| 438 |
cfunction compile_callback(lua_State* L, int fidx, int ct_usr, const struct ctype* ct); |
| 439 |
void compile_globals(struct jit* jit, lua_State* L); |
| 440 |
int get_extern(struct jit* jit, uint8_t* addr, int idx, int type); |
| 441 |
|
| 442 |
/* WARNING: assembly needs to be updated for prototype changes of these functions */ |
| 443 |
int check_bool(lua_State* L, int idx); |
| 444 |
double check_double(lua_State* L, int idx); |
| 445 |
double check_complex_imag(lua_State* L, int idx); |
| 446 |
float check_float(lua_State* L, int idx); |
| 447 |
uint64_t check_uint64(lua_State* L, int idx); |
| 448 |
int64_t check_int64(lua_State* L, int idx); |
| 449 |
int32_t check_int32(lua_State* L, int idx); |
| 450 |
uint32_t check_uint32(lua_State* L, int idx); |
| 451 |
int16_t check_int16(lua_State* L, int idx); |
| 452 |
uint16_t check_uint16(lua_State* L, int idx); |
| 453 |
int8_t check_int8(lua_State* L, int idx); |
| 454 |
uint8_t check_uint8(lua_State* L, int idx); |
| 455 |
uintptr_t check_uintptr(lua_State* L, int idx); |
| 456 |
int32_t check_enum(lua_State* L, int idx, int to_usr, const struct ctype* tt); |
| 457 |
/* these two will always push a value so that we can create structs/functions on the fly */ |
| 458 |
void* check_typed_pointer(lua_State* L, int idx, int to_usr, const struct ctype* tt); |
| 459 |
cfunction check_typed_cfunction(lua_State* L, int idx, int to_usr, const struct ctype* tt); |
| 460 |
complex_double check_complex_double(lua_State* L, int idx); |
| 461 |
complex_float check_complex_float(lua_State* L, int idx); |
| 462 |
|
| 463 |
void unpack_varargs_stack(lua_State* L, int first, int last, char* to); |
| 464 |
void unpack_varargs_reg(lua_State* L, int first, int last, char* to); |
| 465 |
|
| 466 |
void unpack_varargs_stack_skip(lua_State* L, int first, int last, int ints_to_skip, int floats_to_skip, char* to); |
| 467 |
void unpack_varargs_float(lua_State* L, int first, int last, int max, char* to); |
| 468 |
void unpack_varargs_int(lua_State* L, int first, int last, int max, char* to); |
| 469 |
|
| 470 |
|
| 471 |
|