Debian Codegraph @

texk/web2c/luatexdir/luaffi/call.c.orig

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