| 1 |
/* |
| 2 |
* Copyright (C)2005-2022 Haxe Foundation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 |
* DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
// primitives |
| 24 |
|
| 25 |
var c_src = "#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include \"neko_vm.h\" |
| 29 |
#include \"neko_elf.h\" |
| 30 |
#ifdef NEKO_WINDOWS |
| 31 |
# include <windows.h> |
| 32 |
#else |
| 33 |
# include <unistd.h> |
| 34 |
#endif |
| 35 |
#ifdef NEKO_MAC |
| 36 |
# include <sys/param.h> |
| 37 |
# include <mach-o/dyld.h> |
| 38 |
#endif |
| 39 |
#ifdef NEKO_BSD |
| 40 |
# include <sys/param.h> |
| 41 |
# include <sys/sysctl.h> |
| 42 |
#endif |
| 43 |
#ifdef NEKO_POSIX |
| 44 |
# include <signal.h> |
| 45 |
#endif |
| 46 |
|
| 47 |
#define default_loader neko_default_loader |
| 48 |
unsigned char program[] = %s; |
| 49 |
unsigned int program_len = %d; |
| 50 |
unsigned int program_pos = 0; |
| 51 |
|
| 52 |
static void report( neko_vm *vm, value exc, int isexc ) { |
| 53 |
int i; |
| 54 |
buffer b = alloc_buffer(NULL); |
| 55 |
value st = neko_exc_stack(vm); |
| 56 |
for(i=0;i<val_array_size(st);i++) { |
| 57 |
value s = val_array_ptr(st)[i]; |
| 58 |
buffer_append(b,\"Called from \"); |
| 59 |
if( val_is_null(s) ) |
| 60 |
buffer_append(b,\"a C function\"); |
| 61 |
else if( val_is_string(s) ) { |
| 62 |
buffer_append(b,val_string(s)); |
| 63 |
buffer_append(b,\" (no debug available)\"); |
| 64 |
} else if( val_is_array(s) && val_array_size(s) == 2 && val_is_string(val_array_ptr(s)[0]) && val_is_int(val_array_ptr(s)[1]) ) { |
| 65 |
val_buffer(b,val_array_ptr(s)[0]); |
| 66 |
buffer_append(b,\" line \"); |
| 67 |
val_buffer(b,val_array_ptr(s)[1]); |
| 68 |
} else |
| 69 |
val_buffer(b,s); |
| 70 |
buffer_append_char(b,'\\n'); |
| 71 |
} |
| 72 |
if( isexc ) |
| 73 |
buffer_append(b,\"Uncaught exception - \"); |
| 74 |
val_buffer(b,exc); |
| 75 |
fprintf(stderr,\"%s\\n\",val_string(buffer_to_string(b))); |
| 76 |
} |
| 77 |
|
| 78 |
static value read_bytecode( value str, value pos, value len ) { |
| 79 |
unsigned char* prog = program + program_pos; |
| 80 |
unsigned int available = program_len - program_pos; |
| 81 |
unsigned int mlen = val_int(len); |
| 82 |
if (mlen > available) mlen = available; |
| 83 |
memcpy(val_string(str)+val_int(pos), prog, mlen); |
| 84 |
program_pos += mlen; |
| 85 |
return alloc_int(mlen); |
| 86 |
} |
| 87 |
|
| 88 |
/* |
| 89 |
C functions corresponding to the following Neko code : |
| 90 |
|
| 91 |
module_read = $loader.loadprim(\"std@module_read\",2); |
| 92 |
module_exec = $loader.loadprim(\"std@module_exec\",1); |
| 93 |
module_val = module_read(read_bytecode,$loader); |
| 94 |
module_exec(module_val); |
| 95 |
|
| 96 |
*/ |
| 97 |
|
| 98 |
int neko_execute_self( neko_vm *vm, value mload ) { |
| 99 |
value args[] = { alloc_string(\"std@module_read\"), alloc_int(2) }; |
| 100 |
value args2[] = { alloc_string(\"std@module_exec\"), alloc_int(1) }; |
| 101 |
value args3[] = { alloc_function(read_bytecode,3,\"boot_read_bytecode\"), mload }; |
| 102 |
value exc = NULL; |
| 103 |
value module_read, module_exec, module_val; |
| 104 |
module_read = val_callEx(mload,val_field(mload,val_id(\"loadprim\")),args,2,&exc); |
| 105 |
if( exc != NULL ) { |
| 106 |
report(vm,exc,1); |
| 107 |
return 1; |
| 108 |
} |
| 109 |
module_exec = val_callEx(mload,val_field(mload,val_id(\"loadprim\")),args2,2,&exc); |
| 110 |
if( exc != NULL ) { |
| 111 |
report(vm,exc,1); |
| 112 |
return 1; |
| 113 |
} |
| 114 |
module_val = val_callEx(val_null,module_read,args3,2,&exc); |
| 115 |
if( exc != NULL ) { |
| 116 |
report(vm,exc,1); |
| 117 |
return 1; |
| 118 |
} |
| 119 |
alloc_field(val_field(mload,val_id(\"cache\")),val_id(\"_self\"),module_val); |
| 120 |
val_callEx(val_null,module_exec,&module_val,1,&exc); |
| 121 |
if( exc != NULL ) { |
| 122 |
report(vm,exc,1); |
| 123 |
return 1; |
| 124 |
} |
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
#ifdef NEKO_POSIX |
| 129 |
static void handle_signal( int signal ) { |
| 130 |
if( signal == SIGPIPE ) |
| 131 |
val_throw(alloc_string(\"Broken pipe\")); |
| 132 |
else |
| 133 |
val_throw(alloc_string(\"Segmentation fault\")); |
| 134 |
} |
| 135 |
#endif |
| 136 |
|
| 137 |
int main( int argc, char *argv[] ) { |
| 138 |
neko_vm *vm; |
| 139 |
value mload; |
| 140 |
int r; |
| 141 |
neko_global_init(); |
| 142 |
vm = neko_vm_alloc(NULL); |
| 143 |
neko_vm_select(vm); |
| 144 |
|
| 145 |
mload = default_loader(argv+1,argc-1); |
| 146 |
r = neko_execute_self(vm,mload); |
| 147 |
if( mload != NULL && val_field(mload,val_id(\"dump_prof\")) != val_null ) |
| 148 |
val_ocall0(mload,val_id(\"dump_prof\")); |
| 149 |
vm = NULL; |
| 150 |
mload = NULL; |
| 151 |
neko_vm_select(NULL); |
| 152 |
neko_global_free(); |
| 153 |
return r; |
| 154 |
}"; |
| 155 |
|
| 156 |
var elf_update_section_header = $loader.loadprim("std@elf_update_section_header_for_bytecode",3); |
| 157 |
var file_contents = $loader.loadprim("std@file_contents",1); |
| 158 |
var file_open = $loader.loadprim("std@file_open",2); |
| 159 |
var file_write = $loader.loadprim("std@file_write",4); |
| 160 |
var file_write_char = $loader.loadprim("std@file_write_char",2); |
| 161 |
var file_close = $loader.loadprim("std@file_close",1); |
| 162 |
var command = $loader.loadprim("std@sys_command",1); |
| 163 |
var system = $loader.loadprim("std@sys_string",0)(); |
| 164 |
var cwd = $loader.loadprim("std@get_cwd",0)(); |
| 165 |
var get_env = $loader.loadprim("std@get_env",1); |
| 166 |
var string_split = $loader.loadprim("std@string_split",2); |
| 167 |
var buffer_new = $loader.loadprim("std@buffer_new",0); |
| 168 |
var buffer_add = $loader.loadprim("std@buffer_add",2); |
| 169 |
var buffer_string = $loader.loadprim("std@buffer_string",1); |
| 170 |
var sprintf = $loader.loadprim("std@sprintf",2); |
| 171 |
|
| 172 |
// find a substring from then end |
| 173 |
|
| 174 |
var find = function(str,sub,pos) { |
| 175 |
var l1 = $ssize(str); |
| 176 |
var l2 = $ssize(sub); |
| 177 |
var i = l1 - pos; |
| 178 |
while( i >= 0 ) { |
| 179 |
if( $ssub(str,i,l2) == sub ) |
| 180 |
return i; |
| 181 |
i -= 1; |
| 182 |
} |
| 183 |
return null; |
| 184 |
} |
| 185 |
|
| 186 |
// find a file in a path |
| 187 |
|
| 188 |
var find_exe_in_path = function(path,file) { |
| 189 |
while( path != null ) { |
| 190 |
try { |
| 191 |
var p = path[0]; |
| 192 |
var l = $ssize(p); |
| 193 |
if ( l > 0 ) { // add trailing slash |
| 194 |
var last = $sget(p,l-1); |
| 195 |
if ( last != "/" || last != "\\" ) |
| 196 |
p = p + "/"; |
| 197 |
} |
| 198 |
var s = file_contents(p + file); |
| 199 |
if( $sget(s,0) == 35 ) // '#' |
| 200 |
$throw("Is a script"); |
| 201 |
return s; |
| 202 |
} catch e { |
| 203 |
path = path[1]; |
| 204 |
} |
| 205 |
} |
| 206 |
$throw("The bootable executable file was not found : "+file); |
| 207 |
} |
| 208 |
|
| 209 |
var find_exe_in_paths = function(paths,file) { |
| 210 |
var i = 0; |
| 211 |
var len = $asize(paths); |
| 212 |
while( i < len ) { |
| 213 |
try { |
| 214 |
return find_exe_in_path(paths[i],file); |
| 215 |
} catch e { |
| 216 |
i ++= 1; |
| 217 |
} |
| 218 |
} |
| 219 |
$throw("The bootable executable file was not found : "+file); |
| 220 |
} |
| 221 |
|
| 222 |
// bytecode = first argument |
| 223 |
|
| 224 |
var args = $loader.args; |
| 225 |
if( args[0] == "-c" ) { |
| 226 |
|
| 227 |
var file = args[1]; |
| 228 |
var bytecode = file_contents(file); |
| 229 |
var bytecode_len = $ssize(bytecode); |
| 230 |
var program_buf = buffer_new(); |
| 231 |
buffer_add(program_buf, "{"); |
| 232 |
var i = 0; |
| 233 |
while(true) { |
| 234 |
buffer_add(program_buf, $sget(bytecode, i)); |
| 235 |
i += 1; |
| 236 |
if (i < bytecode_len) { |
| 237 |
buffer_add(program_buf, ","); |
| 238 |
} else { |
| 239 |
break; |
| 240 |
} |
| 241 |
} |
| 242 |
buffer_add(program_buf, "}"); |
| 243 |
|
| 244 |
// write a C source that run the module using neko |
| 245 |
var c_name = if ($ssub(file, $ssize(file)-2, 2) == ".n") |
| 246 |
$ssub(file, 0, $ssize(file)-2) + ".c"; |
| 247 |
else |
| 248 |
file+".c"; |
| 249 |
var c_file = file_open(c_name,"wb"); |
| 250 |
c_src = sprintf(c_src, $array(buffer_string(program_buf), bytecode_len, "%s")); |
| 251 |
file_write(c_file, c_src, 0, $ssize(c_src)); |
| 252 |
file_close(c_file); |
| 253 |
|
| 254 |
} else { |
| 255 |
|
| 256 |
var exe_ext = switch system { "Windows" => ".exe" default => "" }; |
| 257 |
var boot_exe = "neko" + exe_ext; |
| 258 |
if( args[0] == "-b" ) { |
| 259 |
boot_exe = args[1]; |
| 260 |
args = $asub(args,2,$asize(args)-2); |
| 261 |
} |
| 262 |
if( $asize(args) != 1 ) |
| 263 |
$throw("Need bytecode argument"); |
| 264 |
var file = args[0]; |
| 265 |
var bytecode = file_contents(file); |
| 266 |
|
| 267 |
// load boot binary |
| 268 |
var path_sep = switch system { |
| 269 |
"Windows" => ";" |
| 270 |
default => ":" |
| 271 |
} |
| 272 |
var path = string_split(get_env("PATH"), path_sep); |
| 273 |
var boot = find_exe_in_paths($array($array(cwd,null),$loader.path,path),boot_exe); |
| 274 |
var boot_size = $ssize(boot); |
| 275 |
|
| 276 |
var dot_pos = find(file,".",1); |
| 277 |
if( dot_pos != null ) |
| 278 |
file = $ssub(file,0,dot_pos); |
| 279 |
|
| 280 |
// create executable file : |
| 281 |
// this is the content of boot.bin where is appended |
| 282 |
// the neko bytecode followed by 'NEKO' and the original exe size |
| 283 |
|
| 284 |
var out_name = file+exe_ext; |
| 285 |
var out = file_open(out_name,"wb"); |
| 286 |
var bytecode_size = $ssize(bytecode); |
| 287 |
var pad_size = (4-(boot_size&0x3)) & 0x3; |
| 288 |
|
| 289 |
file_write(out,boot,0,boot_size); |
| 290 |
boot_size += pad_size; |
| 291 |
if( pad_size >= 3 ) file_write_char(out,0x00); |
| 292 |
if( pad_size >= 2 ) file_write_char(out,0x00); |
| 293 |
if( pad_size >= 1 ) file_write_char(out,0x00); |
| 294 |
file_write(out,bytecode,0,bytecode_size) |
| 295 |
file_write(out,"NEKO",0,4); |
| 296 |
file_write_char(out,boot_size & 0xFF); |
| 297 |
file_write_char(out,(boot_size >> 8) & 0xFF); |
| 298 |
file_write_char(out,(boot_size >> 16) & 0xFF); |
| 299 |
file_write_char(out,boot_size >>> 24); |
| 300 |
file_close(out); |
| 301 |
|
| 302 |
// set execution rights |
| 303 |
|
| 304 |
switch system { |
| 305 |
"Windows" => null |
| 306 |
default => command("chmod 755 "+out_name) |
| 307 |
} |
| 308 |
|
| 309 |
// Update ELF section header (on platforms where that is appropriate) to protect |
| 310 |
// binary's bytecode from being removed by the strip program |
| 311 |
|
| 312 |
var res = elf_update_section_header(out_name,boot_size,bytecode_size+8); |
| 313 |
if( res == 0 ) |
| 314 |
$print("Trouble updating elf section header; stripping binary may lead to problems!") |
| 315 |
} |