| 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 |
#include "ffi.h" |
| 10 |
|
| 11 |
#include <unistd.h> |
| 12 |
|
| 13 |
static cfunction compile(Dst_DECL, lua_State* L, cfunction func, int ref); |
| 14 |
|
| 15 |
static void* reserve_code(struct jit* jit, lua_State* L, size_t sz); |
| 16 |
static void commit_code(struct jit* jit, void* p, size_t sz); |
| 17 |
|
| 18 |
static void debug_int(int f) |
| 19 |
{ |
| 20 |
printf("Here from debugger int = [%d]\n", f); |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
static void debug_double(double f) |
| 25 |
{ |
| 26 |
printf("Here from debugger float = [%lf]\n", f); |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
static void debug_float(float f) |
| 31 |
{ |
| 32 |
printf("Here from debugger float = [%f]\n", f); |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
static void debug_statement() |
| 37 |
{ |
| 38 |
printf("Here from debugger\n"); |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
static void debug_statement_with_arg(void * p) |
| 43 |
{ |
| 44 |
printf("Here from debugger inp = [%p]\n", p); |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
static void push_int(lua_State* L, int val) |
| 49 |
{ lua_pushinteger(L, val); } |
| 50 |
|
| 51 |
static void push_uint(lua_State* L, unsigned int val) |
| 52 |
{ lua_pushinteger(L, val); } |
| 53 |
|
| 54 |
static void push_float(lua_State* L, float val) |
| 55 |
{ lua_pushnumber(L, val); } |
| 56 |
|
| 57 |
#ifndef _WIN32 |
| 58 |
static int GetLastError(void) |
| 59 |
{ return errno; } |
| 60 |
static void SetLastError(int err) |
| 61 |
{ errno = err; } |
| 62 |
#endif |
| 63 |
|
| 64 |
#ifdef NDEBUG |
| 65 |
#define shred(a,b,c) |
| 66 |
#else |
| 67 |
#define shred(p,s,e) memset((uint8_t*)(p)+(s),0xCC,(e)-(s)) |
| 68 |
#endif |
| 69 |
|
| 70 |
|
| 71 |
#ifdef _WIN64 |
| 72 |
#include "dynasm/dasm_x86.h" |
| 73 |
#include "call_x64win.h" |
| 74 |
#elif defined __amd64__ |
| 75 |
#include "dynasm/dasm_x86.h" |
| 76 |
#include "call_x64.h" |
| 77 |
#elif defined __arm__ || defined __arm || defined __ARM__ || defined __ARM || defined ARM || defined _ARM_ || defined ARMV4I || defined _M_ARM |
| 78 |
#include "dynasm/dasm_arm.h" |
| 79 |
#include "call_arm.h" |
| 80 |
#elif defined __aarch64__ |
| 81 |
#include "dynasm/dasm_arm64.h" |
| 82 |
#include "call_arm64.h" |
| 83 |
#else |
| 84 |
#include "dynasm/dasm_x86.h" |
| 85 |
#include "call_x86.h" |
| 86 |
#endif |
| 87 |
|
| 88 |
#ifndef _MSC_VER |
| 89 |
struct __attribute__((__packed__)) jit_head { |
| 90 |
size_t size; |
| 91 |
int ref; |
| 92 |
uint8_t padding[4]; |
| 93 |
uint8_t jump[JUMP_SIZE]; |
| 94 |
}; |
| 95 |
#else |
| 96 |
#pragma pack(push,1) |
| 97 |
struct jit_head { |
| 98 |
size_t size; |
| 99 |
int ref; |
| 100 |
uint8_t padding[4]; |
| 101 |
uint8_t jump[JUMP_SIZE]; |
| 102 |
}; |
| 103 |
#pragma pack(pop) |
| 104 |
#endif |
| 105 |
#define LINKTABLE_MAX_SIZE (sizeof(extnames) / sizeof(extnames[0]) * (JUMP_SIZE)) |
| 106 |
#define LINKTABLE_MAX_NUM (sizeof(extnames) / sizeof(extnames[0])) |
| 107 |
|
| 108 |
static void dump_externs(struct jit* jit) |
| 109 |
{ |
| 110 |
|
| 111 |
#ifdef ARCH_ARM64 |
| 112 |
struct page* page = jit->pages[jit->pagenum-1]; |
| 113 |
unsigned int * ip = (unsigned int*)(page+1); |
| 114 |
printf("Offset = [%ld]\n", page->off); |
| 115 |
printf("size = [%ld]\n", page->off); |
| 116 |
for (int i = 0, func_ind = 0; (i < LINKTABLE_MAX_NUM-1) ; i++, func_ind++) { |
| 117 |
printf("holder = [%p] func address = [%p]\n", ((char*)ip), *(cfunction*)((char*)ip)); |
| 118 |
ip += 2; |
| 119 |
printf("[%p]:ext(%d) = [%0X]\n", (ip), func_ind, *(ip)); |
| 120 |
printf("[%p]:ext(%d) = [%0X]\n", (ip+1), func_ind, *(ip+1)); |
| 121 |
printf("[%p]:ext(%d) = [%0X]\n", (ip+2), func_ind, *(ip+2)); |
| 122 |
ip+=4; |
| 123 |
} |
| 124 |
#endif |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
static void dump_code(struct jit* jit, char * ccode) |
| 129 |
{ |
| 130 |
#ifdef ARCH_ARM64 |
| 131 |
struct page* page = jit->pages[jit->pagenum-1]; |
| 132 |
unsigned char * code = (unsigned char*)(page+1); |
| 133 |
printf("JUMP_SIZE = [%zu] LINKTABLE_ELEMENT_SIZE=[%zu], LINKTABLE_MAX_NUM=[%ld]\n", |
| 134 |
(size_t)JUMP_SIZE, LINKTABLE_MAX_SIZE/LINKTABLE_MAX_NUM, LINKTABLE_MAX_NUM); |
| 135 |
/* The array extnames has (const char *)0 as the last element, which will not |
| 136 |
* have an entry in code. Thus we have to traverse through LINKTABLE_MAX_NUM -1 |
| 137 |
* elements or LINKTABLE_MAX_SIZE - JUMP_SIZE bytes to skip the link table */ |
| 138 |
code += LINKTABLE_MAX_SIZE-JUMP_SIZE; |
| 139 |
struct jit_head *jh1 = (struct jit_head *)code; /* This is the jit_head from compile_globals */ |
| 140 |
code = (unsigned char*)((struct jit_head *)code+1); /* This is the jit_head from compile_globals */ |
| 141 |
/* Incidentally the generated code was less than the available size on first page, and thus |
| 142 |
* we did not have to adjust for 2nd page onwards */ |
| 143 |
struct jit_head *jh2 = (struct jit_head *)code; |
| 144 |
code = (unsigned char*)((struct jit_head *)code+1); |
| 145 |
printf("jh_size = %zu jh->size = %zu\n", sizeof(struct jit_head), jh2->size); |
| 146 |
printf("jh->ref = %d\n", jh2->ref); |
| 147 |
printf("jh->func = %p content = %p\n", (void*)jh2->jump, *(cfunction*)((void*)jh2->jump)); |
| 148 |
printf("jmpadr = [%p]\n", (void*)(*(uint64_t *)((char*)(jh2->jump)+0))); |
| 149 |
printf("ins[1] = [%X]\n", *(unsigned int *)((char*)(jh2->jump)+8)); |
| 150 |
printf("ins[2] = [%X]\n", *(unsigned int *)((char*)(jh2->jump)+12)); |
| 151 |
printf("ins[3] = [%X]\n", *(unsigned int *)((char*)(jh2->jump)+16)); |
| 152 |
//printf("computed code = [%p]\n", code); CODE STARTS HERE IN THE PAGE |
| 153 |
unsigned int * ip = (unsigned int*)ccode; |
| 154 |
for (int i = 0; (char*)&ip[i] < ((char*)page + page->off); i++) { |
| 155 |
printf("[%p]:ins(%02d) = [%0X]\n", &(ip[i]), i, ip[i]); |
| 156 |
} |
| 157 |
#endif |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
static cfunction compile(struct jit* jit, lua_State* L, cfunction func, int ref) |
| 162 |
{ |
| 163 |
struct jit_head* code; |
| 164 |
size_t codesz; |
| 165 |
int err; |
| 166 |
|
| 167 |
if ((err = dasm_checkstep(jit, -1)) !=0 ) { |
| 168 |
char buf[32]; |
| 169 |
sprintf(buf, "%x", err); |
| 170 |
printf("%s:%d\n", __FILE__, __LINE__); |
| 171 |
luaL_error(L, "dasm_link error %s", buf); |
| 172 |
} |
| 173 |
if ((err = dasm_link(jit, &codesz)) != 0) { |
| 174 |
char buf[32]; |
| 175 |
sprintf(buf, "%x", err); |
| 176 |
printf("%s:%d\n", __FILE__, __LINE__); |
| 177 |
luaL_error(L, "dasm_link error %s", buf); |
| 178 |
} |
| 179 |
|
| 180 |
codesz += sizeof(struct jit_head); |
| 181 |
code = (struct jit_head*) reserve_code(jit, L, codesz); |
| 182 |
//printf("%s:%d reserved code = [%p] jump=[%p] size=[%zu]\n", __FILE__, __LINE__, code, code->jump, codesz); |
| 183 |
code->ref = ref; |
| 184 |
code->size = codesz; |
| 185 |
compile_extern_jump(jit, L, func, code->jump); |
| 186 |
|
| 187 |
if ((err = dasm_encode(jit, code+1)) != 0) { |
| 188 |
char buf[32]; |
| 189 |
sprintf(buf, "%x", err); |
| 190 |
commit_code(jit, code, 0); |
| 191 |
luaL_error(L, "dasm_encode error %s", buf); |
| 192 |
} |
| 193 |
|
| 194 |
commit_code(jit, code, codesz); |
| 195 |
//sys_icache_invalidate(code, codesz); |
| 196 |
//dump_externs(jit); |
| 197 |
//dump_code(jit, (char*)(code+1)); |
| 198 |
return (cfunction) (code+1); |
| 199 |
} |
| 200 |
|
| 201 |
typedef uint8_t jump_t[JUMP_SIZE]; |
| 202 |
|
| 203 |
int get_extern(struct jit* jit, uint8_t* addr, int idx, int type) |
| 204 |
{ |
| 205 |
struct page* page = jit->pages[jit->pagenum-1]; |
| 206 |
jump_t* jumps = (jump_t*) (page+1); |
| 207 |
struct jit_head* h = (struct jit_head*) ((uint8_t*) page + page->off); |
| 208 |
uint8_t* jmp; |
| 209 |
ptrdiff_t off; |
| 210 |
|
| 211 |
if (idx == jit->function_extern) { |
| 212 |
jmp = h->jump; |
| 213 |
} else { |
| 214 |
jmp = jumps[idx]; |
| 215 |
} |
| 216 |
|
| 217 |
if (type) { |
| 218 |
/* compensate for room taken up for the offset so that we can work rip |
| 219 |
* relative */ |
| 220 |
addr += BRANCH_OFF; |
| 221 |
|
| 222 |
/* see if we can fit the offset in the branch displacement, if not use the |
| 223 |
* jump instruction placed via compile_extern_jump */ |
| 224 |
off = *(uint8_t**) jmp - addr; |
| 225 |
|
| 226 |
if (MIN_BRANCH <= off && off <= MAX_BRANCH) { |
| 227 |
//printf("%s:%d offset = [%x]\n", __FILE__, __LINE__, (int32_t) off); |
| 228 |
return (int32_t) off; |
| 229 |
} else { |
| 230 |
//printf("%s:%d offset = [%x]\n", __FILE__, __LINE__, (int32_t)(jmp + sizeof(uint8_t*) - addr)); |
| 231 |
return (int32_t)(jmp + sizeof(uint8_t*) - addr); |
| 232 |
} |
| 233 |
} |
| 234 |
else { |
| 235 |
printf("%s:%d THIS CONDITION IS NOT PROVIDED FOR\n", __FILE__, __LINE__); |
| 236 |
return (uint32_t) (uintptr_t)jmp; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
static void* reserve_code(struct jit* jit, lua_State* L, size_t sz) |
| 241 |
{ |
| 242 |
struct page* page; |
| 243 |
size_t off = (jit->pagenum > 0) ? jit->pages[jit->pagenum-1]->off : 0; |
| 244 |
size_t size = (jit->pagenum > 0) ? jit->pages[jit->pagenum-1]->size : 0; |
| 245 |
|
| 246 |
if (off + sz >= size) { |
| 247 |
int i; |
| 248 |
uint8_t* pdata; |
| 249 |
cfunction func; |
| 250 |
|
| 251 |
/* need to create a new page */ |
| 252 |
jit->pages = (struct page**) realloc(jit->pages, (++jit->pagenum) * sizeof(jit->pages[0])); |
| 253 |
|
| 254 |
size = ALIGN_UP(sz + LINKTABLE_MAX_SIZE + sizeof(struct page), jit->align_page_size); |
| 255 |
|
| 256 |
page = (struct page*) AllocPage(size); |
| 257 |
jit->pages[jit->pagenum-1] = page; |
| 258 |
pdata = (uint8_t*) page; |
| 259 |
page->size = size; |
| 260 |
page->off = sizeof(struct page); |
| 261 |
|
| 262 |
lua_newtable(L); |
| 263 |
|
| 264 |
#define ADDFUNC(DLL, NAME) \ |
| 265 |
lua_pushliteral(L, #NAME); \ |
| 266 |
func = DLL ? (cfunction) GetProcAddressA(DLL, #NAME) : NULL; \ |
| 267 |
func = func ? func : (cfunction) &NAME; \ |
| 268 |
lua_pushcfunction(L, (lua_CFunction) func); \ |
| 269 |
lua_rawset(L, -3) |
| 270 |
|
| 271 |
ADDFUNC(NULL, check_double); |
| 272 |
ADDFUNC(NULL, check_float); |
| 273 |
ADDFUNC(NULL, check_uint64); |
| 274 |
ADDFUNC(NULL, check_int64); |
| 275 |
//ADDFUNC(NULL, check_int16); |
| 276 |
//ADDFUNC(NULL, check_uint16); |
| 277 |
ADDFUNC(NULL, check_int32); |
| 278 |
ADDFUNC(NULL, check_uint32); |
| 279 |
//ADDFUNC(NULL, check_int8); |
| 280 |
//ADDFUNC(NULL, check_uint8); |
| 281 |
ADDFUNC(NULL, check_uintptr); |
| 282 |
ADDFUNC(NULL, check_enum); |
| 283 |
ADDFUNC(NULL, check_typed_pointer); |
| 284 |
ADDFUNC(NULL, check_typed_cfunction); |
| 285 |
ADDFUNC(NULL, check_complex_double); |
| 286 |
ADDFUNC(NULL, check_complex_float); |
| 287 |
ADDFUNC(NULL, unpack_varargs_stack); |
| 288 |
ADDFUNC(NULL, unpack_varargs_stack_skip); |
| 289 |
ADDFUNC(NULL, unpack_varargs_reg); |
| 290 |
ADDFUNC(NULL, unpack_varargs_float); |
| 291 |
ADDFUNC(NULL, unpack_varargs_int); |
| 292 |
ADDFUNC(NULL, push_cdata); |
| 293 |
ADDFUNC(NULL, push_int); |
| 294 |
ADDFUNC(NULL, push_uint); |
| 295 |
ADDFUNC(NULL, lua_pushinteger); |
| 296 |
ADDFUNC(NULL, push_float); |
| 297 |
ADDFUNC(NULL, debug_int); |
| 298 |
ADDFUNC(NULL, debug_float); |
| 299 |
ADDFUNC(NULL, debug_double); |
| 300 |
ADDFUNC(NULL, debug_statement); |
| 301 |
ADDFUNC(NULL, debug_statement_with_arg); |
| 302 |
ADDFUNC(jit->kernel32_dll, SetLastError); |
| 303 |
ADDFUNC(jit->kernel32_dll, GetLastError); |
| 304 |
ADDFUNC(jit->lua_dll, luaL_error); |
| 305 |
ADDFUNC(jit->lua_dll, lua_pushnumber); |
| 306 |
ADDFUNC(jit->lua_dll, lua_pushboolean); |
| 307 |
ADDFUNC(jit->lua_dll, lua_gettop); |
| 308 |
ADDFUNC(jit->lua_dll, lua_rawgeti); |
| 309 |
ADDFUNC(jit->lua_dll, lua_pushnil); |
| 310 |
ADDFUNC(jit->lua_dll, lua_callk); |
| 311 |
ADDFUNC(jit->lua_dll, lua_settop); |
| 312 |
ADDFUNC(jit->lua_dll, lua_remove); |
| 313 |
#undef ADDFUNC |
| 314 |
|
| 315 |
for (i = 0; extnames[i] != NULL; i++) { |
| 316 |
|
| 317 |
if (strcmp(extnames[i], "FUNCTION") == 0) { |
| 318 |
shred(pdata + page->off, 0, JUMP_SIZE); |
| 319 |
jit->function_extern = i; |
| 320 |
|
| 321 |
} else { |
| 322 |
lua_getfield(L, -1, extnames[i]); |
| 323 |
func = (cfunction) lua_tocfunction(L, -1); |
| 324 |
|
| 325 |
if (func == NULL) { |
| 326 |
luaL_error(L, "internal error: missing link for %s", extnames[i]); |
| 327 |
} |
| 328 |
|
| 329 |
compile_extern_jump(jit, L, func, pdata + page->off); |
| 330 |
lua_pop(L, 1); |
| 331 |
} |
| 332 |
|
| 333 |
page->off += JUMP_SIZE; |
| 334 |
} |
| 335 |
|
| 336 |
page->freed = page->off; |
| 337 |
lua_pop(L, 1); |
| 338 |
|
| 339 |
} else { |
| 340 |
//printf("%s:%d\n", __FILE__, __LINE__); |
| 341 |
page = jit->pages[jit->pagenum-1]; |
| 342 |
EnableWrite(page, page->size); |
| 343 |
} |
| 344 |
|
| 345 |
//printf("%s:%d page = [%p] page+off = [%p] pagenum=[%zu]\n", __FILE__, __LINE__, page, (char*)(((char*)page)+page->off+sizeof(struct jit_head)), jit->pagenum); |
| 346 |
|
| 347 |
return (uint8_t*) page + page->off; |
| 348 |
} |
| 349 |
|
| 350 |
#ifdef OS_OSX //{ |
| 351 |
#include <libkern/OSCacheControl.h> |
| 352 |
#endif //} |
| 353 |
#ifdef OS_LINUX //{ |
| 354 |
#ifdef __clang__ //{ |
| 355 |
#ifdef __cplusplus //{ |
| 356 |
extern "C" void __clear_cache(void *, void*); |
| 357 |
#else //}{ |
| 358 |
extern void __clear_cache(void *, void*); |
| 359 |
#endif //} |
| 360 |
#else //}{ |
| 361 |
///#include <sys/cachectl.h> |
| 362 |
extern void __builtin___clear_cache(void *begin, void *end); |
| 363 |
#endif //} |
| 364 |
#endif //} |
| 365 |
|
| 366 |
static void commit_code(struct jit* jit, void* code, size_t sz) |
| 367 |
{ |
| 368 |
struct page* page = jit->pages[jit->pagenum-1]; |
| 369 |
page->off += sz; |
| 370 |
//mprotect(page, page->size, PROT_NONE); |
| 371 |
EnableExecute(page, page->size); |
| 372 |
#ifdef OS_OSX //{ |
| 373 |
sys_icache_invalidate(page, page->size); |
| 374 |
#else //}{ |
| 375 |
#ifdef OS_LINUX //{ |
| 376 |
#ifdef __clang__ //{ |
| 377 |
{ |
| 378 |
const char *start = (const char *)page; |
| 379 |
const char *end = (const char *)page + page->size; |
| 380 |
__clear_cache((void*)(start), (void*)(end)); |
| 381 |
} |
| 382 |
#else //}{ |
| 383 |
{ |
| 384 |
const char *start = (const char *)page; |
| 385 |
const char *end = (const char *)page + page->size; |
| 386 |
__builtin___clear_cache((void*)(start), (void*)(end)); |
| 387 |
} |
| 388 |
#endif //} |
| 389 |
#endif //} |
| 390 |
#endif //} |
| 391 |
|
| 392 |
{ |
| 393 |
#if 0 |
| 394 |
FILE* out = fopen("out.bin", "wb"); |
| 395 |
fwrite(page, page->off, 1, out); |
| 396 |
fclose(out); |
| 397 |
#endif |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
/* push_func_ref pushes a copy of the upval table embedded in the compiled |
| 402 |
* function func. |
| 403 |
*/ |
| 404 |
void push_func_ref(lua_State* L, cfunction func) |
| 405 |
{ |
| 406 |
struct jit_head* h = ((struct jit_head*) func) - 1; |
| 407 |
lua_rawgeti(L, LUA_REGISTRYINDEX, h->ref); |
| 408 |
} |
| 409 |
|
| 410 |
void free_code(struct jit* jit, lua_State* L, cfunction func) |
| 411 |
{ |
| 412 |
size_t i; |
| 413 |
struct jit_head* h = ((struct jit_head*) func) - 1; |
| 414 |
for (i = 0; i < jit->pagenum; i++) { |
| 415 |
struct page* p = jit->pages[i]; |
| 416 |
|
| 417 |
if ((uint8_t*) h < (uint8_t*) p || (uint8_t*) p + p->size <= (uint8_t*) h) { |
| 418 |
continue; |
| 419 |
} |
| 420 |
|
| 421 |
luaL_unref(L, LUA_REGISTRYINDEX, h->ref); |
| 422 |
|
| 423 |
EnableWrite(p, p->size); |
| 424 |
p->freed += h->size; |
| 425 |
|
| 426 |
shred(h, 0, h->size); |
| 427 |
|
| 428 |
if (p->freed < p->off) { |
| 429 |
EnableExecute(p, p->size); |
| 430 |
return; |
| 431 |
} |
| 432 |
|
| 433 |
FreePage(p, p->size); |
| 434 |
memmove(&jit->pages[i], &jit->pages[i+1], (jit->pagenum - (i+1)) * sizeof(jit->pages[0])); |
| 435 |
jit->pagenum--; |
| 436 |
return; |
| 437 |
} |
| 438 |
|
| 439 |
assert(!"couldn't find func in the jit pages"); |
| 440 |
} |
| 441 |
|
| 442 |
|