Debian Codegraph @

src/native.c

raw
1 /*
2 * native.c - dynamic native code generation
3 *
4 * Copyright (c) 2021-2024 Shiro Kawai <[email protected]>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the authors nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /* EXPERIMENTAL */
35
36 /* CAUTION
37 * Be careful not to create a code path to call these APIs directly
38 * from Scheme. It would open up for casual Scheme code to execute
39 * arbitrary machine code, bypassing our runtime check completely.
40 *
41 * Instead, the Scheme API for these functionalities should be placed in
42 * gauche.bootstrap module. The module is only alive during initialization
43 * process, so the built-in Scheme procedures can call them, but they'll
44 * be unaccessible from the user code. See libnative.scm.
45 */
46
47 #define LIBGAUCHE_BODY
48 #include "gauche.h"
49 #include "gauche/priv/configP.h"
50 #include "gauche/priv/vmP.h"
51 #include "gauche/priv/mmapP.h"
52 #include "gauche/priv/nativeP.h"
53
54 #if defined(HAVE_SYS_MMAN_H)
55 #include <sys/mman.h>
56 #endif
57
58 static long sys_getpagesize()
59 {
60 #if defined(GAUCHE_WINDOWS)
61 SYSTEM_INFO sysinfo;
62 GetSystemInfo(&sysinfo);
63 return (long)sysinfo.dwPageSize;
64 #else /* !GAUCHE_WINDOWS */
65 return sysconf(_SC_PAGESIZE);
66 #endif /* !GAUCHE_WINDOWS */
67 }
68
69 /*======================================================================
70 * FFI support
71 */
72
73 /*
74 * For the time being, we use a fixed area (mmapped executable page)
75 * as the scratch code pad, and simply manage it like a stack. That is,
76 * the region below free is 'used'. It's enough for simple FFI, since
77 * the generated code won't live after the dynamic extent of FFI call.
78 */
79 struct ScmCodeCacheRec {
80 ScmMemoryRegion *wpad; /* writable page */
81 ScmMemoryRegion *xpad; /* executable page */
82 void *free;
83 };
84
85 #define CODE_PAD_SIZE 4096
86
87 static ScmObj sym_o;
88 static ScmObj sym_p;
89 static ScmObj sym_i;
90 static ScmObj sym_b;
91 static ScmObj sym_i16;
92 static ScmObj sym_i32;
93 static ScmObj sym_i64;
94 static ScmObj sym_d;
95 static ScmObj sym_f;
96 static ScmObj sym_s;
97 static ScmObj sym_v;
98
99 static void init_code_cache(ScmVM *vm) {
100 if (vm->codeCache != NULL) return;
101
102 ScmCodeCache *cc = SCM_NEW(ScmCodeCache);
103 Scm_SysMmapWX(CODE_PAD_SIZE, &cc->wpad, &cc->xpad);
104 cc->free = cc->wpad->ptr;
105 vm->codeCache = cc;
106 }
107
108 static inline void *allocate_code_cache(ScmVM *vm, size_t size)
109 {
110 ScmCodeCache *cc = vm->codeCache;
111 if (cc->free + size > cc->wpad->ptr + cc->wpad->size) {
112 Scm_Error("VM code cache overflow");
113 }
114 void *region = cc->free;
115 cc->free += size;
116 return region;
117 }
118
119 static inline void free_code_cache(ScmVM *vm, void *ptr)
120 {
121 ScmCodeCache *cc = vm->codeCache;
122 SCM_ASSERT(ptr >= cc->wpad->ptr && ptr < cc->wpad->ptr + cc->wpad->size);
123 cc->free = ptr;
124 }
125
126 /* Returns address in xpad that corresponds to the wpad_ptr. */
127 static inline void *get_entry_address(ScmCodeCache *cc, void *wpad_ptr)
128 {
129 return cc->xpad->ptr + (wpad_ptr - cc->wpad->ptr);
130 }
131
132 /*
133 * some utility to 'patch' the code array
134 */
135
136 typedef union {
137 intptr_t n;
138 uint8_t bn[SIZEOF_INTPTR_T];
139
140 int16_t i16;
141 uint8_t bi16[2];
142
143 int32_t i32;
144 uint8_t bi32[4];
145
146 int64_t i64;
147 uint8_t bi64[8];
148
149 double d;
150 uint8_t bd[SIZEOF_DOUBLE];
151
152 float f;
153 uint8_t bf[SIZEOF_FLOAT];
154 } pun_t;
155
156 static inline void patch1(void *dst, ScmSmallInt pos,
157 uint8_t *src, ScmSmallInt size, void *lim)
158 {
159 if (dst + pos + size > lim) {
160 Scm_Error("filler position out of range: %ld", pos);
161 }
162 memcpy(dst+pos, src, size);
163 }
164
165 /*
166 * Copy CODE to the scratch pad, starting from START-th byte up to right before
167 * END-th byte, from the pad's TSTART-th position.
168 *
169 * TEND is, when non-zero, it must be greater than TSTART+(END-START). The
170 * scratch pad area is allocated up to TEND, and filled with zero after the
171 * code.
172 *
173 * Then the pad is patched according to PATCHER, as explained below.
174 *
175 * Finally, the code is called from the entry offset ENTRY.
176 *
177 * PATCHER has the following list:
178 * ((<pos> <type> <value>) ...)
179 *
180 * <pos> - specifies the position in the byte array to be filled.
181 *
182 * <type> - one of the following symbols:
183 * o : ScmObj. <value>'s ScmObj is used as is.
184 * p : pointer. <value> is <dlptr>.
185 * i : integer (intptr_t). <value> must be an integral type.
186 * its integer value is used.
187 * b : byte. <value> must be an integer [0..255].
188 * i16 : 16bit integer. <value> must be an integral type.
189 * i32 : 32bit integer. <value> must be an integral type.
190 * i64 : 64bit integer. <value> must be an integral type.
191 * d : double. <value> must be a real number.
192 * f : float. <value> must be a real number.
193 * s : string <value> must be a string type. Pointer to the cstring
194 * is used.
195 *
196 * <value> - Scheme value to pass.
197 *
198 * RETTYPE is also a symbol similar to <type>, plus 'v' as no value.
199 */
200
201 ScmObj Scm__VMCallNative(ScmVM *vm,
202 ScmSmallInt tstart,
203 ScmSmallInt tend,
204 ScmUVector *code,
205 ScmSmallInt start,
206 ScmSmallInt end,
207 ScmSmallInt entry,
208 ScmObj patcher,
209 ScmObj rettype)
210 {
211 init_code_cache(vm);
212
213 SCM_ASSERT(SCM_U8VECTORP(code));
214
215 ScmSmallInt uvsize = SCM_UVECTOR_SIZE(code);
216 SCM_CHECK_START_END(start, end, uvsize);
217
218 size_t codesize = tstart + end - start;
219 if (entry < 0 || (size_t)entry >= codesize) {
220 Scm_Error("entry out of range: %ld", entry);
221 }
222
223 size_t realcodesize = codesize;
224 if (tend > (ScmSmallInt)codesize) realcodesize = tend;
225
226 void *codepad = allocate_code_cache(vm, realcodesize);
227 if (tstart > 0) memset(codepad, 0, tstart);
228 memcpy(codepad + tstart,
229 SCM_UVECTOR_ELEMENTS(code)+start,
230 end - start);
231 if (realcodesize > codesize) {
232 memset(codepad + codesize, 0, realcodesize - codesize);
233 codesize = realcodesize;
234 }
235
236 ScmObj result = SCM_UNDEFINED;
237
238 SCM_UNWIND_PROTECT {
239 /*
240 * Patch it
241 */
242 void *limit = codepad + codesize;
243
244 ScmObj cp;
245 SCM_FOR_EACH(cp, patcher) {
246 ScmObj e = SCM_CAR(cp);
247 if (Scm_Length(e) != 3) {
248 Scm_Error("malformed filler entry: %S", e);
249 }
250 ScmObj s_pos = SCM_CAR(e);
251 ScmObj type = SCM_CADR(e);
252 ScmObj val = SCM_CAR(SCM_CDDR(e));
253
254 if (!SCM_INTP(s_pos) || !SCM_SYMBOLP(type)) {
255 Scm_Error("bad filler entry: %S", e);
256 }
257 ScmSmallInt pos = SCM_INT_VALUE(s_pos);
258
259 pun_t pun;
260
261 if (SCM_EQ(type, sym_o)) {
262 pun.n = (intptr_t)val;
263 patch1(codepad, pos, pun.bn, SIZEOF_INTPTR_T, limit);
264 } else if (SCM_EQ(type, sym_p)) {
265 if (!Scm_DLPtrP(val)) SCM_TYPE_ERROR(val, "dlptr");
266 pun.n = SCM_FOREIGN_POINTER_REF(intptr_t, val);
267 patch1(codepad, pos, pun.bn, SIZEOF_INTPTR_T, limit);
268 } else if (SCM_EQ(type, sym_i)) {
269 pun.n = Scm_IntegerToIntptr(val);
270 patch1(codepad, pos, pun.bn, SIZEOF_INTPTR_T, limit);
271 } else if (SCM_EQ(type, sym_b)) {
272 if (!SCM_INTP(val)) SCM_TYPE_ERROR(val, "fixnum");
273 uint8_t byte = SCM_INT_VALUE(val);
274 patch1(codepad, pos, &byte, 1, limit);
275 } else if (SCM_EQ(type, sym_i16)) {
276 if (!SCM_INTP(val)) SCM_TYPE_ERROR(val, "fixnum");
277 pun.i16 = SCM_INT_VALUE(val);
278 patch1(codepad, pos, pun.bi16, 2, limit);
279 } else if (SCM_EQ(type, sym_i32)) {
280 pun.i32 = Scm_GetInteger32(val);
281 patch1(codepad, pos, pun.bi32, 4, limit);
282 } else if (SCM_EQ(type, sym_i64)) {
283 pun.i64 = Scm_GetInteger64(val);
284 patch1(codepad, pos, pun.bi64, 8, limit);
285 } else if (SCM_EQ(type, sym_d)) {
286 pun.d = Scm_GetDouble(val);
287 patch1(codepad, pos, pun.bd, SIZEOF_DOUBLE, limit);
288 } else if (SCM_EQ(type, sym_f)) {
289 pun.f = (float)Scm_GetDouble(val);
290 patch1(codepad, pos, pun.bf, SIZEOF_FLOAT, limit);
291 } else if (SCM_EQ(type, sym_s)) {
292 /* NB: If the callee retains the pointer, we need malloc. */
293 if (!SCM_STRINGP(val)) SCM_TYPE_ERROR(val, "string");
294 pun.n = (intptr_t)Scm_GetStringConst(SCM_STRING(val));
295 patch1(codepad, pos, pun.bn, SIZEOF_INTPTR_T, limit);
296 } else {
297 Scm_Error("unknown patch type: %S", type);
298 }
299 }
300
301 /*
302 * Call the code
303 */
304 void *entryPtr = get_entry_address(vm->codeCache, codepad + entry);
305 if (SCM_EQ(rettype, sym_d)) {
306 double r = ((double (*)())entryPtr)();
307 result = Scm_VMReturnFlonum(r);
308 } else if (SCM_EQ(rettype, sym_f)) {
309 float r = ((float (*)())entryPtr)();
310 result = Scm_VMReturnFlonum((double)r);
311 } else if (SCM_EQ(rettype, sym_s)) {
312 intptr_t r = ((intptr_t (*)())entryPtr)();
313 result = SCM_MAKE_STR_COPYING((const char*)r);
314 } else if (SCM_EQ(rettype, sym_i)) {
315 intptr_t r = ((intptr_t (*)())entryPtr)();
316 result = Scm_IntptrToInteger(r);
317 } else if (SCM_EQ(rettype, sym_b)) {
318 uint8_t r = ((uint8_t (*)())entryPtr)();
319 result = SCM_MAKE_INT(r);
320 } else if (SCM_EQ(rettype, sym_o)) {
321 intptr_t r = ((intptr_t (*)())entryPtr)();
322 result = SCM_OBJ(r); /* trust the caller */
323 } else if (SCM_EQ(rettype, sym_v)) {
324 ((void (*)())entryPtr)();
325 } else {
326 Scm_Error("unknown return type: %S", rettype);
327 }
328 } SCM_WHEN_ERROR {
329 free_code_cache(vm, codepad);
330 SCM_NEXT_HANDLER;
331 } SCM_END_PROTECT;
332
333 free_code_cache(vm, codepad);
334 return result;
335 }
336
337 /*======================================================================
338 * JIT support
339 */
340
341 /* Allocate executable page and copy the machine code in CODE,
342 returns an executable <memory-region>.
343 */
344
345 ScmObj Scm__AllocateCodePage(ScmU8Vector *code)
346 {
347 ScmMemoryRegion *wpad, *xpad;
348 long codesize = SCM_U8VECTOR_SIZE(code);
349 long pagesize = sys_getpagesize();
350 long padsize = ((codesize+pagesize-1)/pagesize)*pagesize;
351 Scm_SysMmapWX(padsize, &wpad, &xpad);
352 memcpy(wpad->ptr, SCM_U8VECTOR_ELEMENTS(code), codesize);
353 /* TODO: If writable page and executable page are mapped into
354 two regions, we can unmap writable page here. Currently we don't
355 have an interface to do so explicitly, leaving GC finalizer to
356 do the job.
357 */
358 return SCM_OBJ(xpad);
359 }
360
361
362 /*======================================================================
363 * Initialization
364 */
365
366 void Scm__InitNative(void)
367 {
368 /* symbols for type */
369 sym_o = SCM_INTERN("o");
370 sym_p = SCM_INTERN("p");
371 sym_b = SCM_INTERN("b");
372 sym_i = SCM_INTERN("i");
373 sym_i16 = SCM_INTERN("i16");
374 sym_i32 = SCM_INTERN("i32");
375 sym_i64 = SCM_INTERN("i64");
376 sym_d = SCM_INTERN("d");
377 sym_f = SCM_INTERN("f");
378 sym_s = SCM_INTERN("s");
379 sym_v = SCM_INTERN("v");
380 }