immunio 0.15.4 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (454) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +0 -27
  3. data/ext/immunio/Rakefile +9 -0
  4. data/lib/immunio/plugins/active_record.rb +1 -1
  5. data/lib/immunio/plugins/active_record_relation.rb +1 -1
  6. data/lib/immunio/plugins/environment_reporter.rb +20 -0
  7. data/lib/immunio/rufus_lua_ext/ref.rb +1 -3
  8. data/lib/immunio/version.rb +1 -1
  9. data/lib/immunio/vm.rb +1 -2
  10. data/lua-hooks/Makefile +97 -0
  11. data/lua-hooks/ext/all.c +41 -52
  12. data/lua-hooks/ext/all.o +0 -0
  13. data/lua-hooks/ext/libinjection/libinjection_html5.o +0 -0
  14. data/lua-hooks/ext/libinjection/libinjection_sqli.o +0 -0
  15. data/lua-hooks/ext/libinjection/libinjection_xss.o +0 -0
  16. data/lua-hooks/ext/libinjection/lualib.c +2 -2
  17. data/lua-hooks/ext/lpeg/lpcap.c +2 -2
  18. data/lua-hooks/ext/lpeg/lpcap.o +0 -0
  19. data/lua-hooks/ext/lpeg/lpcode.c +2 -2
  20. data/lua-hooks/ext/lpeg/lpcode.h +1 -1
  21. data/lua-hooks/ext/lpeg/lpcode.o +0 -0
  22. data/lua-hooks/ext/lpeg/lpprint.o +0 -0
  23. data/lua-hooks/ext/lpeg/lptree.c +2 -2
  24. data/lua-hooks/ext/lpeg/lptypes.h +1 -1
  25. data/lua-hooks/ext/lpeg/lpvm.c +2 -2
  26. data/lua-hooks/ext/lpeg/lpvm.o +0 -0
  27. data/lua-hooks/ext/lua-cmsgpack/lua_cmsgpack.c +16 -3
  28. data/lua-hooks/ext/lua-snapshot/snapshot.c +14 -7
  29. data/lua-hooks/ext/luajit/COPYRIGHT +56 -0
  30. data/lua-hooks/ext/luajit/Makefile +159 -0
  31. data/lua-hooks/ext/luajit/README +16 -0
  32. data/lua-hooks/ext/luajit/doc/bluequad-print.css +166 -0
  33. data/lua-hooks/ext/luajit/doc/bluequad.css +325 -0
  34. data/lua-hooks/ext/luajit/doc/changes.html +804 -0
  35. data/lua-hooks/ext/luajit/doc/contact.html +104 -0
  36. data/lua-hooks/ext/luajit/doc/ext_c_api.html +189 -0
  37. data/lua-hooks/ext/luajit/doc/ext_ffi.html +332 -0
  38. data/lua-hooks/ext/luajit/doc/ext_ffi_api.html +570 -0
  39. data/lua-hooks/ext/luajit/doc/ext_ffi_semantics.html +1261 -0
  40. data/lua-hooks/ext/luajit/doc/ext_ffi_tutorial.html +603 -0
  41. data/lua-hooks/ext/luajit/doc/ext_jit.html +201 -0
  42. data/lua-hooks/ext/luajit/doc/ext_profiler.html +365 -0
  43. data/lua-hooks/ext/luajit/doc/extensions.html +448 -0
  44. data/lua-hooks/ext/luajit/doc/faq.html +186 -0
  45. data/lua-hooks/ext/luajit/doc/img/contact.png +0 -0
  46. data/lua-hooks/ext/luajit/doc/install.html +659 -0
  47. data/lua-hooks/ext/luajit/doc/luajit.html +236 -0
  48. data/lua-hooks/ext/luajit/doc/running.html +309 -0
  49. data/lua-hooks/ext/luajit/doc/status.html +118 -0
  50. data/lua-hooks/ext/luajit/dynasm/dasm_arm.h +456 -0
  51. data/lua-hooks/ext/luajit/dynasm/dasm_arm.lua +1125 -0
  52. data/lua-hooks/ext/luajit/dynasm/dasm_arm64.h +518 -0
  53. data/lua-hooks/ext/luajit/dynasm/dasm_arm64.lua +1166 -0
  54. data/lua-hooks/ext/luajit/dynasm/dasm_mips.h +416 -0
  55. data/lua-hooks/ext/luajit/dynasm/dasm_mips.lua +953 -0
  56. data/lua-hooks/ext/luajit/dynasm/dasm_ppc.h +419 -0
  57. data/lua-hooks/ext/luajit/dynasm/dasm_ppc.lua +1919 -0
  58. data/lua-hooks/ext/luajit/dynasm/dasm_proto.h +83 -0
  59. data/lua-hooks/ext/luajit/dynasm/dasm_x64.lua +12 -0
  60. data/lua-hooks/ext/luajit/dynasm/dasm_x86.h +471 -0
  61. data/lua-hooks/ext/luajit/dynasm/dasm_x86.lua +1945 -0
  62. data/lua-hooks/ext/luajit/dynasm/dynasm.lua +1094 -0
  63. data/lua-hooks/ext/luajit/etc/luajit.1 +88 -0
  64. data/lua-hooks/ext/luajit/etc/luajit.pc +25 -0
  65. data/lua-hooks/ext/luajit/src/Makefile +697 -0
  66. data/lua-hooks/ext/luajit/src/Makefile.dep +244 -0
  67. data/lua-hooks/ext/luajit/src/host/README +4 -0
  68. data/lua-hooks/ext/luajit/src/host/buildvm +0 -0
  69. data/lua-hooks/ext/luajit/src/host/buildvm.c +518 -0
  70. data/lua-hooks/ext/luajit/src/host/buildvm.h +105 -0
  71. data/lua-hooks/ext/luajit/src/host/buildvm.o +0 -0
  72. data/lua-hooks/ext/luajit/src/host/buildvm_arch.h +7449 -0
  73. data/lua-hooks/ext/luajit/src/host/buildvm_asm.c +345 -0
  74. data/lua-hooks/ext/luajit/src/host/buildvm_asm.o +0 -0
  75. data/lua-hooks/ext/luajit/src/host/buildvm_fold.c +229 -0
  76. data/lua-hooks/ext/luajit/src/host/buildvm_fold.o +0 -0
  77. data/lua-hooks/ext/luajit/src/host/buildvm_lib.c +457 -0
  78. data/lua-hooks/ext/luajit/src/host/buildvm_lib.o +0 -0
  79. data/lua-hooks/ext/luajit/src/host/buildvm_libbc.h +45 -0
  80. data/lua-hooks/ext/luajit/src/host/buildvm_peobj.c +368 -0
  81. data/lua-hooks/ext/luajit/src/host/buildvm_peobj.o +0 -0
  82. data/lua-hooks/ext/luajit/src/host/genlibbc.lua +197 -0
  83. data/lua-hooks/ext/luajit/src/host/genminilua.lua +428 -0
  84. data/lua-hooks/ext/luajit/src/host/minilua +0 -0
  85. data/lua-hooks/ext/luajit/src/host/minilua.c +7770 -0
  86. data/lua-hooks/ext/luajit/src/host/minilua.o +0 -0
  87. data/lua-hooks/ext/luajit/src/jit/bc.lua +190 -0
  88. data/lua-hooks/ext/luajit/src/jit/bcsave.lua +661 -0
  89. data/lua-hooks/ext/luajit/src/jit/dis_arm.lua +689 -0
  90. data/lua-hooks/ext/luajit/src/jit/dis_mips.lua +428 -0
  91. data/lua-hooks/ext/luajit/src/jit/dis_mipsel.lua +17 -0
  92. data/lua-hooks/ext/luajit/src/jit/dis_ppc.lua +591 -0
  93. data/lua-hooks/ext/luajit/src/jit/dis_x64.lua +17 -0
  94. data/lua-hooks/ext/luajit/src/jit/dis_x86.lua +838 -0
  95. data/lua-hooks/ext/luajit/src/jit/dump.lua +706 -0
  96. data/lua-hooks/ext/luajit/src/jit/p.lua +310 -0
  97. data/lua-hooks/ext/luajit/src/jit/v.lua +170 -0
  98. data/lua-hooks/ext/luajit/src/jit/vmdef.lua +362 -0
  99. data/lua-hooks/ext/luajit/src/jit/zone.lua +45 -0
  100. data/lua-hooks/ext/{lua → luajit/src}/lauxlib.h +10 -17
  101. data/lua-hooks/ext/luajit/src/lib_aux.c +356 -0
  102. data/lua-hooks/ext/luajit/src/lib_aux.o +0 -0
  103. data/lua-hooks/ext/luajit/src/lib_aux_dyn.o +0 -0
  104. data/lua-hooks/ext/luajit/src/lib_base.c +664 -0
  105. data/lua-hooks/ext/luajit/src/lib_base.o +0 -0
  106. data/lua-hooks/ext/luajit/src/lib_base_dyn.o +0 -0
  107. data/lua-hooks/ext/luajit/src/lib_bit.c +180 -0
  108. data/lua-hooks/ext/luajit/src/lib_bit.o +0 -0
  109. data/lua-hooks/ext/luajit/src/lib_bit_dyn.o +0 -0
  110. data/lua-hooks/ext/luajit/src/lib_debug.c +405 -0
  111. data/lua-hooks/ext/luajit/src/lib_debug.o +0 -0
  112. data/lua-hooks/ext/luajit/src/lib_debug_dyn.o +0 -0
  113. data/lua-hooks/ext/luajit/src/lib_ffi.c +872 -0
  114. data/lua-hooks/ext/luajit/src/lib_ffi.o +0 -0
  115. data/lua-hooks/ext/luajit/src/lib_ffi_dyn.o +0 -0
  116. data/lua-hooks/ext/luajit/src/lib_init.c +55 -0
  117. data/lua-hooks/ext/luajit/src/lib_init.o +0 -0
  118. data/lua-hooks/ext/luajit/src/lib_init_dyn.o +0 -0
  119. data/lua-hooks/ext/luajit/src/lib_io.c +541 -0
  120. data/lua-hooks/ext/luajit/src/lib_io.o +0 -0
  121. data/lua-hooks/ext/luajit/src/lib_io_dyn.o +0 -0
  122. data/lua-hooks/ext/luajit/src/lib_jit.c +767 -0
  123. data/lua-hooks/ext/luajit/src/lib_jit.o +0 -0
  124. data/lua-hooks/ext/luajit/src/lib_jit_dyn.o +0 -0
  125. data/lua-hooks/ext/luajit/src/lib_math.c +230 -0
  126. data/lua-hooks/ext/luajit/src/lib_math.o +0 -0
  127. data/lua-hooks/ext/luajit/src/lib_math_dyn.o +0 -0
  128. data/lua-hooks/ext/luajit/src/lib_os.c +292 -0
  129. data/lua-hooks/ext/luajit/src/lib_os.o +0 -0
  130. data/lua-hooks/ext/luajit/src/lib_os_dyn.o +0 -0
  131. data/lua-hooks/ext/luajit/src/lib_package.c +610 -0
  132. data/lua-hooks/ext/luajit/src/lib_package.o +0 -0
  133. data/lua-hooks/ext/luajit/src/lib_package_dyn.o +0 -0
  134. data/lua-hooks/ext/luajit/src/lib_string.c +752 -0
  135. data/lua-hooks/ext/luajit/src/lib_string.o +0 -0
  136. data/lua-hooks/ext/luajit/src/lib_string_dyn.o +0 -0
  137. data/lua-hooks/ext/luajit/src/lib_table.c +307 -0
  138. data/lua-hooks/ext/luajit/src/lib_table.o +0 -0
  139. data/lua-hooks/ext/luajit/src/lib_table_dyn.o +0 -0
  140. data/lua-hooks/ext/luajit/src/libluajit.a +0 -0
  141. data/lua-hooks/ext/luajit/src/libluajit.so +0 -0
  142. data/lua-hooks/ext/luajit/src/lj.supp +26 -0
  143. data/lua-hooks/ext/luajit/src/lj_alloc.c +1398 -0
  144. data/lua-hooks/ext/luajit/src/lj_alloc.h +17 -0
  145. data/lua-hooks/ext/luajit/src/lj_alloc.o +0 -0
  146. data/lua-hooks/ext/luajit/src/lj_alloc_dyn.o +0 -0
  147. data/lua-hooks/ext/luajit/src/lj_api.c +1210 -0
  148. data/lua-hooks/ext/luajit/src/lj_api.o +0 -0
  149. data/lua-hooks/ext/luajit/src/lj_api_dyn.o +0 -0
  150. data/lua-hooks/ext/luajit/src/lj_arch.h +509 -0
  151. data/lua-hooks/ext/luajit/src/lj_asm.c +2278 -0
  152. data/lua-hooks/ext/luajit/src/lj_asm.h +17 -0
  153. data/lua-hooks/ext/luajit/src/lj_asm.o +0 -0
  154. data/lua-hooks/ext/luajit/src/lj_asm_arm.h +2217 -0
  155. data/lua-hooks/ext/luajit/src/lj_asm_dyn.o +0 -0
  156. data/lua-hooks/ext/luajit/src/lj_asm_mips.h +1833 -0
  157. data/lua-hooks/ext/luajit/src/lj_asm_ppc.h +2015 -0
  158. data/lua-hooks/ext/luajit/src/lj_asm_x86.h +2634 -0
  159. data/lua-hooks/ext/luajit/src/lj_bc.c +14 -0
  160. data/lua-hooks/ext/luajit/src/lj_bc.h +265 -0
  161. data/lua-hooks/ext/luajit/src/lj_bc.o +0 -0
  162. data/lua-hooks/ext/luajit/src/lj_bc_dyn.o +0 -0
  163. data/lua-hooks/ext/luajit/src/lj_bcdef.h +220 -0
  164. data/lua-hooks/ext/luajit/src/lj_bcdump.h +68 -0
  165. data/lua-hooks/ext/luajit/src/lj_bcread.c +457 -0
  166. data/lua-hooks/ext/luajit/src/lj_bcread.o +0 -0
  167. data/lua-hooks/ext/luajit/src/lj_bcread_dyn.o +0 -0
  168. data/lua-hooks/ext/luajit/src/lj_bcwrite.c +361 -0
  169. data/lua-hooks/ext/luajit/src/lj_bcwrite.o +0 -0
  170. data/lua-hooks/ext/luajit/src/lj_bcwrite_dyn.o +0 -0
  171. data/lua-hooks/ext/luajit/src/lj_buf.c +234 -0
  172. data/lua-hooks/ext/luajit/src/lj_buf.h +105 -0
  173. data/lua-hooks/ext/luajit/src/lj_buf.o +0 -0
  174. data/lua-hooks/ext/luajit/src/lj_buf_dyn.o +0 -0
  175. data/lua-hooks/ext/luajit/src/lj_carith.c +429 -0
  176. data/lua-hooks/ext/luajit/src/lj_carith.h +37 -0
  177. data/lua-hooks/ext/luajit/src/lj_carith.o +0 -0
  178. data/lua-hooks/ext/luajit/src/lj_carith_dyn.o +0 -0
  179. data/lua-hooks/ext/luajit/src/lj_ccall.c +984 -0
  180. data/lua-hooks/ext/luajit/src/lj_ccall.h +178 -0
  181. data/lua-hooks/ext/luajit/src/lj_ccall.o +0 -0
  182. data/lua-hooks/ext/luajit/src/lj_ccall_dyn.o +0 -0
  183. data/lua-hooks/ext/luajit/src/lj_ccallback.c +712 -0
  184. data/lua-hooks/ext/luajit/src/lj_ccallback.h +25 -0
  185. data/lua-hooks/ext/luajit/src/lj_ccallback.o +0 -0
  186. data/lua-hooks/ext/luajit/src/lj_ccallback_dyn.o +0 -0
  187. data/lua-hooks/ext/luajit/src/lj_cconv.c +752 -0
  188. data/lua-hooks/ext/luajit/src/lj_cconv.h +70 -0
  189. data/lua-hooks/ext/luajit/src/lj_cconv.o +0 -0
  190. data/lua-hooks/ext/luajit/src/lj_cconv_dyn.o +0 -0
  191. data/lua-hooks/ext/luajit/src/lj_cdata.c +288 -0
  192. data/lua-hooks/ext/luajit/src/lj_cdata.h +76 -0
  193. data/lua-hooks/ext/luajit/src/lj_cdata.o +0 -0
  194. data/lua-hooks/ext/luajit/src/lj_cdata_dyn.o +0 -0
  195. data/lua-hooks/ext/luajit/src/lj_char.c +43 -0
  196. data/lua-hooks/ext/luajit/src/lj_char.h +42 -0
  197. data/lua-hooks/ext/luajit/src/lj_char.o +0 -0
  198. data/lua-hooks/ext/luajit/src/lj_char_dyn.o +0 -0
  199. data/lua-hooks/ext/luajit/src/lj_clib.c +418 -0
  200. data/lua-hooks/ext/luajit/src/lj_clib.h +29 -0
  201. data/lua-hooks/ext/luajit/src/lj_clib.o +0 -0
  202. data/lua-hooks/ext/luajit/src/lj_clib_dyn.o +0 -0
  203. data/lua-hooks/ext/luajit/src/lj_cparse.c +1862 -0
  204. data/lua-hooks/ext/luajit/src/lj_cparse.h +65 -0
  205. data/lua-hooks/ext/luajit/src/lj_cparse.o +0 -0
  206. data/lua-hooks/ext/luajit/src/lj_cparse_dyn.o +0 -0
  207. data/lua-hooks/ext/luajit/src/lj_crecord.c +1834 -0
  208. data/lua-hooks/ext/luajit/src/lj_crecord.h +38 -0
  209. data/lua-hooks/ext/luajit/src/lj_crecord.o +0 -0
  210. data/lua-hooks/ext/luajit/src/lj_crecord_dyn.o +0 -0
  211. data/lua-hooks/ext/luajit/src/lj_ctype.c +635 -0
  212. data/lua-hooks/ext/luajit/src/lj_ctype.h +461 -0
  213. data/lua-hooks/ext/luajit/src/lj_ctype.o +0 -0
  214. data/lua-hooks/ext/luajit/src/lj_ctype_dyn.o +0 -0
  215. data/lua-hooks/ext/luajit/src/lj_debug.c +699 -0
  216. data/lua-hooks/ext/luajit/src/lj_debug.h +65 -0
  217. data/lua-hooks/ext/luajit/src/lj_debug.o +0 -0
  218. data/lua-hooks/ext/luajit/src/lj_debug_dyn.o +0 -0
  219. data/lua-hooks/ext/luajit/src/lj_def.h +365 -0
  220. data/lua-hooks/ext/luajit/src/lj_dispatch.c +557 -0
  221. data/lua-hooks/ext/luajit/src/lj_dispatch.h +138 -0
  222. data/lua-hooks/ext/luajit/src/lj_dispatch.o +0 -0
  223. data/lua-hooks/ext/luajit/src/lj_dispatch_dyn.o +0 -0
  224. data/lua-hooks/ext/luajit/src/lj_emit_arm.h +356 -0
  225. data/lua-hooks/ext/luajit/src/lj_emit_mips.h +211 -0
  226. data/lua-hooks/ext/luajit/src/lj_emit_ppc.h +238 -0
  227. data/lua-hooks/ext/luajit/src/lj_emit_x86.h +462 -0
  228. data/lua-hooks/ext/luajit/src/lj_err.c +794 -0
  229. data/lua-hooks/ext/luajit/src/lj_err.h +41 -0
  230. data/lua-hooks/ext/luajit/src/lj_err.o +0 -0
  231. data/lua-hooks/ext/luajit/src/lj_err_dyn.o +0 -0
  232. data/lua-hooks/ext/luajit/src/lj_errmsg.h +190 -0
  233. data/lua-hooks/ext/luajit/src/lj_ff.h +18 -0
  234. data/lua-hooks/ext/luajit/src/lj_ffdef.h +209 -0
  235. data/lua-hooks/ext/luajit/src/lj_ffrecord.c +1247 -0
  236. data/lua-hooks/ext/luajit/src/lj_ffrecord.h +24 -0
  237. data/lua-hooks/ext/luajit/src/lj_ffrecord.o +0 -0
  238. data/lua-hooks/ext/luajit/src/lj_ffrecord_dyn.o +0 -0
  239. data/lua-hooks/ext/luajit/src/lj_folddef.h +1138 -0
  240. data/lua-hooks/ext/luajit/src/lj_frame.h +259 -0
  241. data/lua-hooks/ext/luajit/src/lj_func.c +185 -0
  242. data/lua-hooks/ext/luajit/src/lj_func.h +24 -0
  243. data/lua-hooks/ext/luajit/src/lj_func.o +0 -0
  244. data/lua-hooks/ext/luajit/src/lj_func_dyn.o +0 -0
  245. data/lua-hooks/ext/luajit/src/lj_gc.c +845 -0
  246. data/lua-hooks/ext/luajit/src/lj_gc.h +134 -0
  247. data/lua-hooks/ext/luajit/src/lj_gc.o +0 -0
  248. data/lua-hooks/ext/luajit/src/lj_gc_dyn.o +0 -0
  249. data/lua-hooks/ext/luajit/src/lj_gdbjit.c +787 -0
  250. data/lua-hooks/ext/luajit/src/lj_gdbjit.h +22 -0
  251. data/lua-hooks/ext/luajit/src/lj_gdbjit.o +0 -0
  252. data/lua-hooks/ext/luajit/src/lj_gdbjit_dyn.o +0 -0
  253. data/lua-hooks/ext/luajit/src/lj_ir.c +505 -0
  254. data/lua-hooks/ext/luajit/src/lj_ir.h +577 -0
  255. data/lua-hooks/ext/luajit/src/lj_ir.o +0 -0
  256. data/lua-hooks/ext/luajit/src/lj_ir_dyn.o +0 -0
  257. data/lua-hooks/ext/luajit/src/lj_ircall.h +321 -0
  258. data/lua-hooks/ext/luajit/src/lj_iropt.h +161 -0
  259. data/lua-hooks/ext/luajit/src/lj_jit.h +440 -0
  260. data/lua-hooks/ext/luajit/src/lj_lex.c +482 -0
  261. data/lua-hooks/ext/luajit/src/lj_lex.h +86 -0
  262. data/lua-hooks/ext/luajit/src/lj_lex.o +0 -0
  263. data/lua-hooks/ext/luajit/src/lj_lex_dyn.o +0 -0
  264. data/lua-hooks/ext/luajit/src/lj_lib.c +303 -0
  265. data/lua-hooks/ext/luajit/src/lj_lib.h +115 -0
  266. data/lua-hooks/ext/luajit/src/lj_lib.o +0 -0
  267. data/lua-hooks/ext/luajit/src/lj_lib_dyn.o +0 -0
  268. data/lua-hooks/ext/luajit/src/lj_libdef.h +414 -0
  269. data/lua-hooks/ext/luajit/src/lj_load.c +168 -0
  270. data/lua-hooks/ext/luajit/src/lj_load.o +0 -0
  271. data/lua-hooks/ext/luajit/src/lj_load_dyn.o +0 -0
  272. data/lua-hooks/ext/luajit/src/lj_mcode.c +386 -0
  273. data/lua-hooks/ext/luajit/src/lj_mcode.h +30 -0
  274. data/lua-hooks/ext/luajit/src/lj_mcode.o +0 -0
  275. data/lua-hooks/ext/luajit/src/lj_mcode_dyn.o +0 -0
  276. data/lua-hooks/ext/luajit/src/lj_meta.c +477 -0
  277. data/lua-hooks/ext/luajit/src/lj_meta.h +38 -0
  278. data/lua-hooks/ext/luajit/src/lj_meta.o +0 -0
  279. data/lua-hooks/ext/luajit/src/lj_meta_dyn.o +0 -0
  280. data/lua-hooks/ext/luajit/src/lj_obj.c +50 -0
  281. data/lua-hooks/ext/luajit/src/lj_obj.h +976 -0
  282. data/lua-hooks/ext/luajit/src/lj_obj.o +0 -0
  283. data/lua-hooks/ext/luajit/src/lj_obj_dyn.o +0 -0
  284. data/lua-hooks/ext/luajit/src/lj_opt_dce.c +78 -0
  285. data/lua-hooks/ext/luajit/src/lj_opt_dce.o +0 -0
  286. data/lua-hooks/ext/luajit/src/lj_opt_dce_dyn.o +0 -0
  287. data/lua-hooks/ext/luajit/src/lj_opt_fold.c +2488 -0
  288. data/lua-hooks/ext/luajit/src/lj_opt_fold.o +0 -0
  289. data/lua-hooks/ext/luajit/src/lj_opt_fold_dyn.o +0 -0
  290. data/lua-hooks/ext/luajit/src/lj_opt_loop.c +449 -0
  291. data/lua-hooks/ext/luajit/src/lj_opt_loop.o +0 -0
  292. data/lua-hooks/ext/luajit/src/lj_opt_loop_dyn.o +0 -0
  293. data/lua-hooks/ext/luajit/src/lj_opt_mem.c +935 -0
  294. data/lua-hooks/ext/luajit/src/lj_opt_mem.o +0 -0
  295. data/lua-hooks/ext/luajit/src/lj_opt_mem_dyn.o +0 -0
  296. data/lua-hooks/ext/luajit/src/lj_opt_narrow.c +652 -0
  297. data/lua-hooks/ext/luajit/src/lj_opt_narrow.o +0 -0
  298. data/lua-hooks/ext/luajit/src/lj_opt_narrow_dyn.o +0 -0
  299. data/lua-hooks/ext/luajit/src/lj_opt_sink.c +245 -0
  300. data/lua-hooks/ext/luajit/src/lj_opt_sink.o +0 -0
  301. data/lua-hooks/ext/luajit/src/lj_opt_sink_dyn.o +0 -0
  302. data/lua-hooks/ext/luajit/src/lj_opt_split.c +856 -0
  303. data/lua-hooks/ext/luajit/src/lj_opt_split.o +0 -0
  304. data/lua-hooks/ext/luajit/src/lj_opt_split_dyn.o +0 -0
  305. data/lua-hooks/ext/luajit/src/lj_parse.c +2725 -0
  306. data/lua-hooks/ext/luajit/src/lj_parse.h +18 -0
  307. data/lua-hooks/ext/luajit/src/lj_parse.o +0 -0
  308. data/lua-hooks/ext/luajit/src/lj_parse_dyn.o +0 -0
  309. data/lua-hooks/ext/luajit/src/lj_profile.c +368 -0
  310. data/lua-hooks/ext/luajit/src/lj_profile.h +21 -0
  311. data/lua-hooks/ext/luajit/src/lj_profile.o +0 -0
  312. data/lua-hooks/ext/luajit/src/lj_profile_dyn.o +0 -0
  313. data/lua-hooks/ext/luajit/src/lj_recdef.h +270 -0
  314. data/lua-hooks/ext/luajit/src/lj_record.c +2554 -0
  315. data/lua-hooks/ext/luajit/src/lj_record.h +45 -0
  316. data/lua-hooks/ext/luajit/src/lj_record.o +0 -0
  317. data/lua-hooks/ext/luajit/src/lj_record_dyn.o +0 -0
  318. data/lua-hooks/ext/luajit/src/lj_snap.c +870 -0
  319. data/lua-hooks/ext/luajit/src/lj_snap.h +34 -0
  320. data/lua-hooks/ext/luajit/src/lj_snap.o +0 -0
  321. data/lua-hooks/ext/luajit/src/lj_snap_dyn.o +0 -0
  322. data/lua-hooks/ext/luajit/src/lj_state.c +300 -0
  323. data/lua-hooks/ext/luajit/src/lj_state.h +35 -0
  324. data/lua-hooks/ext/luajit/src/lj_state.o +0 -0
  325. data/lua-hooks/ext/luajit/src/lj_state_dyn.o +0 -0
  326. data/lua-hooks/ext/luajit/src/lj_str.c +197 -0
  327. data/lua-hooks/ext/luajit/src/lj_str.h +27 -0
  328. data/lua-hooks/ext/luajit/src/lj_str.o +0 -0
  329. data/lua-hooks/ext/luajit/src/lj_str_dyn.o +0 -0
  330. data/lua-hooks/ext/luajit/src/lj_strfmt.c +554 -0
  331. data/lua-hooks/ext/luajit/src/lj_strfmt.h +125 -0
  332. data/lua-hooks/ext/luajit/src/lj_strfmt.o +0 -0
  333. data/lua-hooks/ext/luajit/src/lj_strfmt_dyn.o +0 -0
  334. data/lua-hooks/ext/luajit/src/lj_strscan.c +547 -0
  335. data/lua-hooks/ext/luajit/src/lj_strscan.h +39 -0
  336. data/lua-hooks/ext/luajit/src/lj_strscan.o +0 -0
  337. data/lua-hooks/ext/luajit/src/lj_strscan_dyn.o +0 -0
  338. data/lua-hooks/ext/luajit/src/lj_tab.c +666 -0
  339. data/lua-hooks/ext/luajit/src/lj_tab.h +73 -0
  340. data/lua-hooks/ext/luajit/src/lj_tab.o +0 -0
  341. data/lua-hooks/ext/luajit/src/lj_tab_dyn.o +0 -0
  342. data/lua-hooks/ext/luajit/src/lj_target.h +164 -0
  343. data/lua-hooks/ext/luajit/src/lj_target_arm.h +270 -0
  344. data/lua-hooks/ext/luajit/src/lj_target_arm64.h +97 -0
  345. data/lua-hooks/ext/luajit/src/lj_target_mips.h +260 -0
  346. data/lua-hooks/ext/luajit/src/lj_target_ppc.h +280 -0
  347. data/lua-hooks/ext/luajit/src/lj_target_x86.h +345 -0
  348. data/lua-hooks/ext/luajit/src/lj_trace.c +859 -0
  349. data/lua-hooks/ext/luajit/src/lj_trace.h +54 -0
  350. data/lua-hooks/ext/luajit/src/lj_trace.o +0 -0
  351. data/lua-hooks/ext/luajit/src/lj_trace_dyn.o +0 -0
  352. data/lua-hooks/ext/luajit/src/lj_traceerr.h +63 -0
  353. data/lua-hooks/ext/luajit/src/lj_udata.c +34 -0
  354. data/lua-hooks/ext/luajit/src/lj_udata.h +14 -0
  355. data/lua-hooks/ext/luajit/src/lj_udata.o +0 -0
  356. data/lua-hooks/ext/luajit/src/lj_udata_dyn.o +0 -0
  357. data/lua-hooks/ext/luajit/src/lj_vm.S +2730 -0
  358. data/lua-hooks/ext/luajit/src/lj_vm.h +114 -0
  359. data/lua-hooks/ext/luajit/src/lj_vm.o +0 -0
  360. data/lua-hooks/ext/luajit/src/lj_vm_dyn.o +0 -0
  361. data/lua-hooks/ext/luajit/src/lj_vmevent.c +58 -0
  362. data/lua-hooks/ext/luajit/src/lj_vmevent.h +59 -0
  363. data/lua-hooks/ext/luajit/src/lj_vmevent.o +0 -0
  364. data/lua-hooks/ext/luajit/src/lj_vmevent_dyn.o +0 -0
  365. data/lua-hooks/ext/luajit/src/lj_vmmath.c +152 -0
  366. data/lua-hooks/ext/luajit/src/lj_vmmath.o +0 -0
  367. data/lua-hooks/ext/luajit/src/lj_vmmath_dyn.o +0 -0
  368. data/lua-hooks/ext/luajit/src/ljamalg.c +96 -0
  369. data/lua-hooks/ext/{lua → luajit/src}/lua.h +12 -7
  370. data/lua-hooks/ext/luajit/src/lua.hpp +9 -0
  371. data/lua-hooks/ext/luajit/src/luaconf.h +156 -0
  372. data/lua-hooks/ext/luajit/src/luajit +0 -0
  373. data/lua-hooks/ext/luajit/src/luajit.c +570 -0
  374. data/lua-hooks/ext/luajit/src/luajit.h +79 -0
  375. data/lua-hooks/ext/luajit/src/luajit.o +0 -0
  376. data/lua-hooks/ext/luajit/src/lualib.h +43 -0
  377. data/lua-hooks/ext/luajit/src/msvcbuild.bat +114 -0
  378. data/lua-hooks/ext/luajit/src/ps4build.bat +103 -0
  379. data/lua-hooks/ext/luajit/src/psvitabuild.bat +93 -0
  380. data/lua-hooks/ext/luajit/src/vm_arm.dasc +4585 -0
  381. data/lua-hooks/ext/luajit/src/vm_arm64.dasc +3764 -0
  382. data/lua-hooks/ext/luajit/src/vm_mips.dasc +4355 -0
  383. data/lua-hooks/ext/luajit/src/vm_ppc.dasc +5252 -0
  384. data/lua-hooks/ext/luajit/src/vm_x64.dasc +4902 -0
  385. data/lua-hooks/ext/luajit/src/vm_x86.dasc +5710 -0
  386. data/lua-hooks/ext/luajit/src/xb1build.bat +101 -0
  387. data/lua-hooks/ext/luajit/src/xedkbuild.bat +92 -0
  388. data/lua-hooks/ext/luautf8/lutf8lib.c +3 -3
  389. data/lua-hooks/lib/boot.lua +37 -2
  390. metadata +372 -69
  391. data/lua-hooks/ext/bitop/README +0 -22
  392. data/lua-hooks/ext/bitop/bit.c +0 -189
  393. data/lua-hooks/ext/extconf.rb +0 -38
  394. data/lua-hooks/ext/lua/COPYRIGHT +0 -34
  395. data/lua-hooks/ext/lua/lapi.c +0 -1087
  396. data/lua-hooks/ext/lua/lapi.h +0 -16
  397. data/lua-hooks/ext/lua/lauxlib.c +0 -652
  398. data/lua-hooks/ext/lua/lbaselib.c +0 -659
  399. data/lua-hooks/ext/lua/lcode.c +0 -831
  400. data/lua-hooks/ext/lua/lcode.h +0 -76
  401. data/lua-hooks/ext/lua/ldblib.c +0 -398
  402. data/lua-hooks/ext/lua/ldebug.c +0 -638
  403. data/lua-hooks/ext/lua/ldebug.h +0 -33
  404. data/lua-hooks/ext/lua/ldo.c +0 -519
  405. data/lua-hooks/ext/lua/ldo.h +0 -57
  406. data/lua-hooks/ext/lua/ldump.c +0 -164
  407. data/lua-hooks/ext/lua/lfunc.c +0 -174
  408. data/lua-hooks/ext/lua/lfunc.h +0 -34
  409. data/lua-hooks/ext/lua/lgc.c +0 -710
  410. data/lua-hooks/ext/lua/lgc.h +0 -110
  411. data/lua-hooks/ext/lua/linit.c +0 -38
  412. data/lua-hooks/ext/lua/liolib.c +0 -556
  413. data/lua-hooks/ext/lua/llex.c +0 -463
  414. data/lua-hooks/ext/lua/llex.h +0 -81
  415. data/lua-hooks/ext/lua/llimits.h +0 -128
  416. data/lua-hooks/ext/lua/lmathlib.c +0 -263
  417. data/lua-hooks/ext/lua/lmem.c +0 -86
  418. data/lua-hooks/ext/lua/lmem.h +0 -49
  419. data/lua-hooks/ext/lua/loadlib.c +0 -705
  420. data/lua-hooks/ext/lua/loadlib_rel.c +0 -760
  421. data/lua-hooks/ext/lua/lobject.c +0 -214
  422. data/lua-hooks/ext/lua/lobject.h +0 -381
  423. data/lua-hooks/ext/lua/lopcodes.c +0 -102
  424. data/lua-hooks/ext/lua/lopcodes.h +0 -268
  425. data/lua-hooks/ext/lua/loslib.c +0 -243
  426. data/lua-hooks/ext/lua/lparser.c +0 -1339
  427. data/lua-hooks/ext/lua/lparser.h +0 -82
  428. data/lua-hooks/ext/lua/lstate.c +0 -214
  429. data/lua-hooks/ext/lua/lstate.h +0 -169
  430. data/lua-hooks/ext/lua/lstring.c +0 -111
  431. data/lua-hooks/ext/lua/lstring.h +0 -31
  432. data/lua-hooks/ext/lua/lstrlib.c +0 -871
  433. data/lua-hooks/ext/lua/ltable.c +0 -588
  434. data/lua-hooks/ext/lua/ltable.h +0 -40
  435. data/lua-hooks/ext/lua/ltablib.c +0 -287
  436. data/lua-hooks/ext/lua/ltm.c +0 -75
  437. data/lua-hooks/ext/lua/ltm.h +0 -54
  438. data/lua-hooks/ext/lua/lua.c +0 -392
  439. data/lua-hooks/ext/lua/lua.def +0 -131
  440. data/lua-hooks/ext/lua/lua.rc +0 -28
  441. data/lua-hooks/ext/lua/lua_dll.rc +0 -26
  442. data/lua-hooks/ext/lua/luac.c +0 -200
  443. data/lua-hooks/ext/lua/luac.rc +0 -1
  444. data/lua-hooks/ext/lua/luaconf.h +0 -763
  445. data/lua-hooks/ext/lua/luaconf.h.in +0 -724
  446. data/lua-hooks/ext/lua/luaconf.h.orig +0 -763
  447. data/lua-hooks/ext/lua/lualib.h +0 -53
  448. data/lua-hooks/ext/lua/lundump.c +0 -227
  449. data/lua-hooks/ext/lua/lundump.h +0 -36
  450. data/lua-hooks/ext/lua/lvm.c +0 -767
  451. data/lua-hooks/ext/lua/lvm.h +0 -36
  452. data/lua-hooks/ext/lua/lzio.c +0 -82
  453. data/lua-hooks/ext/lua/lzio.h +0 -67
  454. data/lua-hooks/ext/lua/print.c +0 -227
@@ -0,0 +1,178 @@
1
+ /*
2
+ ** FFI C call handling.
3
+ ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
4
+ */
5
+
6
+ #ifndef _LJ_CCALL_H
7
+ #define _LJ_CCALL_H
8
+
9
+ #include "lj_obj.h"
10
+ #include "lj_ctype.h"
11
+
12
+ #if LJ_HASFFI
13
+
14
+ /* -- C calling conventions ----------------------------------------------- */
15
+
16
+ #if LJ_TARGET_X86ORX64
17
+
18
+ #if LJ_TARGET_X86
19
+ #define CCALL_NARG_GPR 2 /* For fastcall arguments. */
20
+ #define CCALL_NARG_FPR 0
21
+ #define CCALL_NRET_GPR 2
22
+ #define CCALL_NRET_FPR 1 /* For FP results on x87 stack. */
23
+ #define CCALL_ALIGN_STACKARG 0 /* Don't align argument on stack. */
24
+ #elif LJ_ABI_WIN
25
+ #define CCALL_NARG_GPR 4
26
+ #define CCALL_NARG_FPR 4
27
+ #define CCALL_NRET_GPR 1
28
+ #define CCALL_NRET_FPR 1
29
+ #define CCALL_SPS_EXTRA 4
30
+ #else
31
+ #define CCALL_NARG_GPR 6
32
+ #define CCALL_NARG_FPR 8
33
+ #define CCALL_NRET_GPR 2
34
+ #define CCALL_NRET_FPR 2
35
+ #define CCALL_VECTOR_REG 1 /* Pass vectors in registers. */
36
+ #endif
37
+
38
+ #define CCALL_SPS_FREE 1
39
+ #define CCALL_ALIGN_CALLSTATE 16
40
+
41
+ typedef LJ_ALIGN(16) union FPRArg {
42
+ double d[2];
43
+ float f[4];
44
+ uint8_t b[16];
45
+ uint16_t s[8];
46
+ int i[4];
47
+ int64_t l[2];
48
+ } FPRArg;
49
+
50
+ typedef intptr_t GPRArg;
51
+
52
+ #elif LJ_TARGET_ARM
53
+
54
+ #define CCALL_NARG_GPR 4
55
+ #define CCALL_NRET_GPR 2 /* For softfp double. */
56
+ #if LJ_ABI_SOFTFP
57
+ #define CCALL_NARG_FPR 0
58
+ #define CCALL_NRET_FPR 0
59
+ #else
60
+ #define CCALL_NARG_FPR 8
61
+ #define CCALL_NRET_FPR 4
62
+ #endif
63
+ #define CCALL_SPS_FREE 0
64
+
65
+ typedef intptr_t GPRArg;
66
+ typedef union FPRArg {
67
+ double d;
68
+ float f[2];
69
+ } FPRArg;
70
+
71
+ #elif LJ_TARGET_ARM64
72
+
73
+ #define CCALL_NARG_GPR 8
74
+ #define CCALL_NRET_GPR 2
75
+ #define CCALL_NARG_FPR 8
76
+ #define CCALL_NRET_FPR 4
77
+ #define CCALL_SPS_FREE 0
78
+
79
+ typedef intptr_t GPRArg;
80
+ typedef union FPRArg {
81
+ double d;
82
+ float f;
83
+ uint32_t u32;
84
+ } FPRArg;
85
+
86
+ #elif LJ_TARGET_PPC
87
+
88
+ #define CCALL_NARG_GPR 8
89
+ #define CCALL_NARG_FPR 8
90
+ #define CCALL_NRET_GPR 4 /* For complex double. */
91
+ #define CCALL_NRET_FPR 1
92
+ #define CCALL_SPS_EXTRA 4
93
+ #define CCALL_SPS_FREE 0
94
+
95
+ typedef intptr_t GPRArg;
96
+ typedef double FPRArg;
97
+
98
+ #elif LJ_TARGET_MIPS
99
+
100
+ #define CCALL_NARG_GPR 4
101
+ #define CCALL_NARG_FPR 2
102
+ #define CCALL_NRET_GPR 2
103
+ #define CCALL_NRET_FPR 2
104
+ #define CCALL_SPS_EXTRA 7
105
+ #define CCALL_SPS_FREE 1
106
+
107
+ typedef intptr_t GPRArg;
108
+ typedef union FPRArg {
109
+ double d;
110
+ struct { LJ_ENDIAN_LOHI(float f; , float g;) };
111
+ } FPRArg;
112
+
113
+ #else
114
+ #error "Missing calling convention definitions for this architecture"
115
+ #endif
116
+
117
+ #ifndef CCALL_SPS_EXTRA
118
+ #define CCALL_SPS_EXTRA 0
119
+ #endif
120
+ #ifndef CCALL_VECTOR_REG
121
+ #define CCALL_VECTOR_REG 0
122
+ #endif
123
+ #ifndef CCALL_ALIGN_STACKARG
124
+ #define CCALL_ALIGN_STACKARG 1
125
+ #endif
126
+ #ifndef CCALL_ALIGN_CALLSTATE
127
+ #define CCALL_ALIGN_CALLSTATE 8
128
+ #endif
129
+
130
+ #define CCALL_NUM_GPR \
131
+ (CCALL_NARG_GPR > CCALL_NRET_GPR ? CCALL_NARG_GPR : CCALL_NRET_GPR)
132
+ #define CCALL_NUM_FPR \
133
+ (CCALL_NARG_FPR > CCALL_NRET_FPR ? CCALL_NARG_FPR : CCALL_NRET_FPR)
134
+
135
+ /* Check against constants in lj_ctype.h. */
136
+ LJ_STATIC_ASSERT(CCALL_NUM_GPR <= CCALL_MAX_GPR);
137
+ LJ_STATIC_ASSERT(CCALL_NUM_FPR <= CCALL_MAX_FPR);
138
+
139
+ #define CCALL_MAXSTACK 32
140
+
141
+ /* -- C call state -------------------------------------------------------- */
142
+
143
+ typedef LJ_ALIGN(CCALL_ALIGN_CALLSTATE) struct CCallState {
144
+ void (*func)(void); /* Pointer to called function. */
145
+ uint32_t spadj; /* Stack pointer adjustment. */
146
+ uint8_t nsp; /* Number of stack slots. */
147
+ uint8_t retref; /* Return value by reference. */
148
+ #if LJ_TARGET_X64
149
+ uint8_t ngpr; /* Number of arguments in GPRs. */
150
+ uint8_t nfpr; /* Number of arguments in FPRs. */
151
+ #elif LJ_TARGET_X86
152
+ uint8_t resx87; /* Result on x87 stack: 1:float, 2:double. */
153
+ #elif LJ_TARGET_ARM64
154
+ void *retp; /* Aggregate return pointer in x8. */
155
+ #elif LJ_TARGET_PPC
156
+ uint8_t nfpr; /* Number of arguments in FPRs. */
157
+ #endif
158
+ #if LJ_32
159
+ int32_t align1;
160
+ #endif
161
+ #if CCALL_NUM_FPR
162
+ FPRArg fpr[CCALL_NUM_FPR]; /* Arguments/results in FPRs. */
163
+ #endif
164
+ GPRArg gpr[CCALL_NUM_GPR]; /* Arguments/results in GPRs. */
165
+ GPRArg stack[CCALL_MAXSTACK]; /* Stack slots. */
166
+ } CCallState;
167
+
168
+ /* -- C call handling ----------------------------------------------------- */
169
+
170
+ /* Really belongs to lj_vm.h. */
171
+ LJ_ASMF void LJ_FASTCALL lj_vm_ffi_call(CCallState *cc);
172
+
173
+ LJ_FUNC CTypeID lj_ccall_ctid_vararg(CTState *cts, cTValue *o);
174
+ LJ_FUNC int lj_ccall_func(lua_State *L, GCcdata *cd);
175
+
176
+ #endif
177
+
178
+ #endif
@@ -0,0 +1,712 @@
1
+ /*
2
+ ** FFI C callback handling.
3
+ ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
4
+ */
5
+
6
+ #include "lj_obj.h"
7
+
8
+ #if LJ_HASFFI
9
+
10
+ #include "lj_gc.h"
11
+ #include "lj_err.h"
12
+ #include "lj_tab.h"
13
+ #include "lj_state.h"
14
+ #include "lj_frame.h"
15
+ #include "lj_ctype.h"
16
+ #include "lj_cconv.h"
17
+ #include "lj_ccall.h"
18
+ #include "lj_ccallback.h"
19
+ #include "lj_target.h"
20
+ #include "lj_mcode.h"
21
+ #include "lj_trace.h"
22
+ #include "lj_vm.h"
23
+
24
+ /* -- Target-specific handling of callback slots -------------------------- */
25
+
26
+ #define CALLBACK_MCODE_SIZE (LJ_PAGESIZE * LJ_NUM_CBPAGE)
27
+
28
+ #if LJ_OS_NOJIT
29
+
30
+ /* Callbacks disabled. */
31
+ #define CALLBACK_SLOT2OFS(slot) (0*(slot))
32
+ #define CALLBACK_OFS2SLOT(ofs) (0*(ofs))
33
+ #define CALLBACK_MAX_SLOT 0
34
+
35
+ #elif LJ_TARGET_X86ORX64
36
+
37
+ #define CALLBACK_MCODE_HEAD (LJ_64 ? 8 : 0)
38
+ #define CALLBACK_MCODE_GROUP (-2+1+2+(LJ_GC64 ? 10 : 5)+(LJ_64 ? 6 : 5))
39
+
40
+ #define CALLBACK_SLOT2OFS(slot) \
41
+ (CALLBACK_MCODE_HEAD + CALLBACK_MCODE_GROUP*((slot)/32) + 4*(slot))
42
+
43
+ static MSize CALLBACK_OFS2SLOT(MSize ofs)
44
+ {
45
+ MSize group;
46
+ ofs -= CALLBACK_MCODE_HEAD;
47
+ group = ofs / (32*4 + CALLBACK_MCODE_GROUP);
48
+ return (ofs % (32*4 + CALLBACK_MCODE_GROUP))/4 + group*32;
49
+ }
50
+
51
+ #define CALLBACK_MAX_SLOT \
52
+ (((CALLBACK_MCODE_SIZE-CALLBACK_MCODE_HEAD)/(CALLBACK_MCODE_GROUP+4*32))*32)
53
+
54
+ #elif LJ_TARGET_ARM
55
+
56
+ #define CALLBACK_MCODE_HEAD 32
57
+
58
+ #elif LJ_TARGET_ARM64
59
+
60
+ #define CALLBACK_MCODE_HEAD 32
61
+
62
+ #elif LJ_TARGET_PPC
63
+
64
+ #define CALLBACK_MCODE_HEAD 24
65
+
66
+ #elif LJ_TARGET_MIPS
67
+
68
+ #define CALLBACK_MCODE_HEAD 24
69
+
70
+ #else
71
+
72
+ /* Missing support for this architecture. */
73
+ #define CALLBACK_SLOT2OFS(slot) (0*(slot))
74
+ #define CALLBACK_OFS2SLOT(ofs) (0*(ofs))
75
+ #define CALLBACK_MAX_SLOT 0
76
+
77
+ #endif
78
+
79
+ #ifndef CALLBACK_SLOT2OFS
80
+ #define CALLBACK_SLOT2OFS(slot) (CALLBACK_MCODE_HEAD + 8*(slot))
81
+ #define CALLBACK_OFS2SLOT(ofs) (((ofs)-CALLBACK_MCODE_HEAD)/8)
82
+ #define CALLBACK_MAX_SLOT (CALLBACK_OFS2SLOT(CALLBACK_MCODE_SIZE))
83
+ #endif
84
+
85
+ /* Convert callback slot number to callback function pointer. */
86
+ static void *callback_slot2ptr(CTState *cts, MSize slot)
87
+ {
88
+ return (uint8_t *)cts->cb.mcode + CALLBACK_SLOT2OFS(slot);
89
+ }
90
+
91
+ /* Convert callback function pointer to slot number. */
92
+ MSize lj_ccallback_ptr2slot(CTState *cts, void *p)
93
+ {
94
+ uintptr_t ofs = (uintptr_t)((uint8_t *)p -(uint8_t *)cts->cb.mcode);
95
+ if (ofs < CALLBACK_MCODE_SIZE) {
96
+ MSize slot = CALLBACK_OFS2SLOT((MSize)ofs);
97
+ if (CALLBACK_SLOT2OFS(slot) == (MSize)ofs)
98
+ return slot;
99
+ }
100
+ return ~0u; /* Not a known callback function pointer. */
101
+ }
102
+
103
+ /* Initialize machine code for callback function pointers. */
104
+ #if LJ_OS_NOJIT
105
+ /* Disabled callback support. */
106
+ #define callback_mcode_init(g, p) UNUSED(p)
107
+ #elif LJ_TARGET_X86ORX64
108
+ static void callback_mcode_init(global_State *g, uint8_t *page)
109
+ {
110
+ uint8_t *p = page;
111
+ uint8_t *target = (uint8_t *)(void *)lj_vm_ffi_callback;
112
+ MSize slot;
113
+ #if LJ_64
114
+ *(void **)p = target; p += 8;
115
+ #endif
116
+ for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {
117
+ /* mov al, slot; jmp group */
118
+ *p++ = XI_MOVrib | RID_EAX; *p++ = (uint8_t)slot;
119
+ if ((slot & 31) == 31 || slot == CALLBACK_MAX_SLOT-1) {
120
+ /* push ebp/rbp; mov ah, slot>>8; mov ebp, &g. */
121
+ *p++ = XI_PUSH + RID_EBP;
122
+ *p++ = XI_MOVrib | (RID_EAX+4); *p++ = (uint8_t)(slot >> 8);
123
+ #if LJ_GC64
124
+ *p++ = 0x48; *p++ = XI_MOVri | RID_EBP;
125
+ *(uint64_t *)p = (uint64_t)(g); p += 8;
126
+ #else
127
+ *p++ = XI_MOVri | RID_EBP;
128
+ *(int32_t *)p = i32ptr(g); p += 4;
129
+ #endif
130
+ #if LJ_64
131
+ /* jmp [rip-pageofs] where lj_vm_ffi_callback is stored. */
132
+ *p++ = XI_GROUP5; *p++ = XM_OFS0 + (XOg_JMP<<3) + RID_EBP;
133
+ *(int32_t *)p = (int32_t)(page-(p+4)); p += 4;
134
+ #else
135
+ /* jmp lj_vm_ffi_callback. */
136
+ *p++ = XI_JMP; *(int32_t *)p = target-(p+4); p += 4;
137
+ #endif
138
+ } else {
139
+ *p++ = XI_JMPs; *p++ = (uint8_t)((2+2)*(31-(slot&31)) - 2);
140
+ }
141
+ }
142
+ lua_assert(p - page <= CALLBACK_MCODE_SIZE);
143
+ }
144
+ #elif LJ_TARGET_ARM
145
+ static void callback_mcode_init(global_State *g, uint32_t *page)
146
+ {
147
+ uint32_t *p = page;
148
+ void *target = (void *)lj_vm_ffi_callback;
149
+ MSize slot;
150
+ /* This must match with the saveregs macro in buildvm_arm.dasc. */
151
+ *p++ = ARMI_SUB|ARMF_D(RID_R12)|ARMF_N(RID_R12)|ARMF_M(RID_PC);
152
+ *p++ = ARMI_PUSH|ARMF_N(RID_SP)|RSET_RANGE(RID_R4,RID_R11+1)|RID2RSET(RID_LR);
153
+ *p++ = ARMI_SUB|ARMI_K12|ARMF_D(RID_R12)|ARMF_N(RID_R12)|CALLBACK_MCODE_HEAD;
154
+ *p++ = ARMI_STR|ARMI_LS_P|ARMI_LS_W|ARMF_D(RID_R12)|ARMF_N(RID_SP)|(CFRAME_SIZE-4*9);
155
+ *p++ = ARMI_LDR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_R12)|ARMF_N(RID_PC);
156
+ *p++ = ARMI_LDR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_PC)|ARMF_N(RID_PC);
157
+ *p++ = u32ptr(g);
158
+ *p++ = u32ptr(target);
159
+ for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {
160
+ *p++ = ARMI_MOV|ARMF_D(RID_R12)|ARMF_M(RID_PC);
161
+ *p = ARMI_B | ((page-p-2) & 0x00ffffffu);
162
+ p++;
163
+ }
164
+ lua_assert(p - page <= CALLBACK_MCODE_SIZE);
165
+ }
166
+ #elif LJ_TARGET_ARM64
167
+ static void callback_mcode_init(global_State *g, uint32_t *page)
168
+ {
169
+ uint32_t *p = page;
170
+ void *target = (void *)lj_vm_ffi_callback;
171
+ MSize slot;
172
+ *p++ = A64I_LDRLx | A64F_D(RID_X11) | A64F_S19(4);
173
+ *p++ = A64I_LDRLx | A64F_D(RID_X10) | A64F_S19(5);
174
+ *p++ = A64I_BR | A64F_N(RID_X11);
175
+ *p++ = A64I_NOP;
176
+ ((void **)p)[0] = target;
177
+ ((void **)p)[1] = g;
178
+ p += 4;
179
+ for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {
180
+ *p++ = A64I_MOVZw | A64F_D(RID_X9) | A64F_U16(slot);
181
+ *p = A64I_B | A64F_S26((page-p) & 0x03ffffffu);
182
+ p++;
183
+ }
184
+ lua_assert(p - page <= CALLBACK_MCODE_SIZE);
185
+ }
186
+ #elif LJ_TARGET_PPC
187
+ static void callback_mcode_init(global_State *g, uint32_t *page)
188
+ {
189
+ uint32_t *p = page;
190
+ void *target = (void *)lj_vm_ffi_callback;
191
+ MSize slot;
192
+ *p++ = PPCI_LIS | PPCF_T(RID_TMP) | (u32ptr(target) >> 16);
193
+ *p++ = PPCI_LIS | PPCF_T(RID_R12) | (u32ptr(g) >> 16);
194
+ *p++ = PPCI_ORI | PPCF_A(RID_TMP)|PPCF_T(RID_TMP) | (u32ptr(target) & 0xffff);
195
+ *p++ = PPCI_ORI | PPCF_A(RID_R12)|PPCF_T(RID_R12) | (u32ptr(g) & 0xffff);
196
+ *p++ = PPCI_MTCTR | PPCF_T(RID_TMP);
197
+ *p++ = PPCI_BCTR;
198
+ for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {
199
+ *p++ = PPCI_LI | PPCF_T(RID_R11) | slot;
200
+ *p = PPCI_B | (((page-p) & 0x00ffffffu) << 2);
201
+ p++;
202
+ }
203
+ lua_assert(p - page <= CALLBACK_MCODE_SIZE);
204
+ }
205
+ #elif LJ_TARGET_MIPS
206
+ static void callback_mcode_init(global_State *g, uint32_t *page)
207
+ {
208
+ uint32_t *p = page;
209
+ void *target = (void *)lj_vm_ffi_callback;
210
+ MSize slot;
211
+ *p++ = MIPSI_SW | MIPSF_T(RID_R1)|MIPSF_S(RID_SP) | 0;
212
+ *p++ = MIPSI_LUI | MIPSF_T(RID_R3) | (u32ptr(target) >> 16);
213
+ *p++ = MIPSI_LUI | MIPSF_T(RID_R2) | (u32ptr(g) >> 16);
214
+ *p++ = MIPSI_ORI | MIPSF_T(RID_R3)|MIPSF_S(RID_R3) |(u32ptr(target)&0xffff);
215
+ *p++ = MIPSI_JR | MIPSF_S(RID_R3);
216
+ *p++ = MIPSI_ORI | MIPSF_T(RID_R2)|MIPSF_S(RID_R2) | (u32ptr(g)&0xffff);
217
+ for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {
218
+ *p = MIPSI_B | ((page-p-1) & 0x0000ffffu);
219
+ p++;
220
+ *p++ = MIPSI_LI | MIPSF_T(RID_R1) | slot;
221
+ }
222
+ lua_assert(p - page <= CALLBACK_MCODE_SIZE);
223
+ }
224
+ #else
225
+ /* Missing support for this architecture. */
226
+ #define callback_mcode_init(g, p) UNUSED(p)
227
+ #endif
228
+
229
+ /* -- Machine code management --------------------------------------------- */
230
+
231
+ #if LJ_TARGET_WINDOWS
232
+
233
+ #define WIN32_LEAN_AND_MEAN
234
+ #include <windows.h>
235
+
236
+ #elif LJ_TARGET_POSIX
237
+
238
+ #include <sys/mman.h>
239
+ #ifndef MAP_ANONYMOUS
240
+ #define MAP_ANONYMOUS MAP_ANON
241
+ #endif
242
+
243
+ #endif
244
+
245
+ /* Allocate and initialize area for callback function pointers. */
246
+ static void callback_mcode_new(CTState *cts)
247
+ {
248
+ size_t sz = (size_t)CALLBACK_MCODE_SIZE;
249
+ void *p;
250
+ if (CALLBACK_MAX_SLOT == 0)
251
+ lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);
252
+ #if LJ_TARGET_WINDOWS
253
+ p = VirtualAlloc(NULL, sz, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
254
+ if (!p)
255
+ lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);
256
+ #elif LJ_TARGET_POSIX
257
+ p = mmap(NULL, sz, (PROT_READ|PROT_WRITE), MAP_PRIVATE|MAP_ANONYMOUS,
258
+ -1, 0);
259
+ if (p == MAP_FAILED)
260
+ lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);
261
+ #else
262
+ /* Fallback allocator. Fails if memory is not executable by default. */
263
+ p = lj_mem_new(cts->L, sz);
264
+ #endif
265
+ cts->cb.mcode = p;
266
+ callback_mcode_init(cts->g, p);
267
+ lj_mcode_sync(p, (char *)p + sz);
268
+ #if LJ_TARGET_WINDOWS
269
+ {
270
+ DWORD oprot;
271
+ VirtualProtect(p, sz, PAGE_EXECUTE_READ, &oprot);
272
+ }
273
+ #elif LJ_TARGET_POSIX
274
+ mprotect(p, sz, (PROT_READ|PROT_EXEC));
275
+ #endif
276
+ }
277
+
278
+ /* Free area for callback function pointers. */
279
+ void lj_ccallback_mcode_free(CTState *cts)
280
+ {
281
+ size_t sz = (size_t)CALLBACK_MCODE_SIZE;
282
+ void *p = cts->cb.mcode;
283
+ if (p == NULL) return;
284
+ #if LJ_TARGET_WINDOWS
285
+ VirtualFree(p, 0, MEM_RELEASE);
286
+ UNUSED(sz);
287
+ #elif LJ_TARGET_POSIX
288
+ munmap(p, sz);
289
+ #else
290
+ lj_mem_free(cts->g, p, sz);
291
+ #endif
292
+ }
293
+
294
+ /* -- C callback entry ---------------------------------------------------- */
295
+
296
+ /* Target-specific handling of register arguments. Similar to lj_ccall.c. */
297
+ #if LJ_TARGET_X86
298
+
299
+ #define CALLBACK_HANDLE_REGARG \
300
+ if (!isfp) { /* Only non-FP values may be passed in registers. */ \
301
+ if (n > 1) { /* Anything > 32 bit is passed on the stack. */ \
302
+ if (!LJ_ABI_WIN) ngpr = maxgpr; /* Prevent reordering. */ \
303
+ } else if (ngpr + 1 <= maxgpr) { \
304
+ sp = &cts->cb.gpr[ngpr]; \
305
+ ngpr += n; \
306
+ goto done; \
307
+ } \
308
+ }
309
+
310
+ #elif LJ_TARGET_X64 && LJ_ABI_WIN
311
+
312
+ /* Windows/x64 argument registers are strictly positional (use ngpr). */
313
+ #define CALLBACK_HANDLE_REGARG \
314
+ if (isfp) { \
315
+ if (ngpr < maxgpr) { sp = &cts->cb.fpr[ngpr++]; UNUSED(nfpr); goto done; } \
316
+ } else { \
317
+ if (ngpr < maxgpr) { sp = &cts->cb.gpr[ngpr++]; goto done; } \
318
+ }
319
+
320
+ #elif LJ_TARGET_X64
321
+
322
+ #define CALLBACK_HANDLE_REGARG \
323
+ if (isfp) { \
324
+ if (nfpr + n <= CCALL_NARG_FPR) { \
325
+ sp = &cts->cb.fpr[nfpr]; \
326
+ nfpr += n; \
327
+ goto done; \
328
+ } \
329
+ } else { \
330
+ if (ngpr + n <= maxgpr) { \
331
+ sp = &cts->cb.gpr[ngpr]; \
332
+ ngpr += n; \
333
+ goto done; \
334
+ } \
335
+ }
336
+
337
+ #elif LJ_TARGET_ARM
338
+
339
+ #if LJ_ABI_SOFTFP
340
+
341
+ #define CALLBACK_HANDLE_REGARG_FP1 UNUSED(isfp);
342
+ #define CALLBACK_HANDLE_REGARG_FP2
343
+
344
+ #else
345
+
346
+ #define CALLBACK_HANDLE_REGARG_FP1 \
347
+ if (isfp) { \
348
+ if (n == 1) { \
349
+ if (fprodd) { \
350
+ sp = &cts->cb.fpr[fprodd-1]; \
351
+ fprodd = 0; \
352
+ goto done; \
353
+ } else if (nfpr + 1 <= CCALL_NARG_FPR) { \
354
+ sp = &cts->cb.fpr[nfpr++]; \
355
+ fprodd = nfpr; \
356
+ goto done; \
357
+ } \
358
+ } else { \
359
+ if (nfpr + 1 <= CCALL_NARG_FPR) { \
360
+ sp = &cts->cb.fpr[nfpr++]; \
361
+ goto done; \
362
+ } \
363
+ } \
364
+ fprodd = 0; /* No reordering after the first FP value is on stack. */ \
365
+ } else {
366
+
367
+ #define CALLBACK_HANDLE_REGARG_FP2 }
368
+
369
+ #endif
370
+
371
+ #define CALLBACK_HANDLE_REGARG \
372
+ CALLBACK_HANDLE_REGARG_FP1 \
373
+ if (n > 1) ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \
374
+ if (ngpr + n <= maxgpr) { \
375
+ sp = &cts->cb.gpr[ngpr]; \
376
+ ngpr += n; \
377
+ goto done; \
378
+ } CALLBACK_HANDLE_REGARG_FP2
379
+
380
+ #elif LJ_TARGET_ARM64
381
+
382
+ #define CALLBACK_HANDLE_REGARG \
383
+ if (isfp) { \
384
+ if (nfpr + n <= CCALL_NARG_FPR) { \
385
+ sp = &cts->cb.fpr[nfpr]; \
386
+ nfpr += n; \
387
+ goto done; \
388
+ } else { \
389
+ nfpr = CCALL_NARG_FPR; /* Prevent reordering. */ \
390
+ } \
391
+ } else { \
392
+ if (!LJ_TARGET_IOS && n > 1) \
393
+ ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \
394
+ if (ngpr + n <= maxgpr) { \
395
+ sp = &cts->cb.gpr[ngpr]; \
396
+ ngpr += n; \
397
+ goto done; \
398
+ } else { \
399
+ ngpr = CCALL_NARG_GPR; /* Prevent reordering. */ \
400
+ } \
401
+ }
402
+
403
+ #elif LJ_TARGET_PPC
404
+
405
+ #define CALLBACK_HANDLE_REGARG \
406
+ if (isfp) { \
407
+ if (nfpr + 1 <= CCALL_NARG_FPR) { \
408
+ sp = &cts->cb.fpr[nfpr++]; \
409
+ cta = ctype_get(cts, CTID_DOUBLE); /* FPRs always hold doubles. */ \
410
+ goto done; \
411
+ } \
412
+ } else { /* Try to pass argument in GPRs. */ \
413
+ if (n > 1) { \
414
+ lua_assert(ctype_isinteger(cta->info) && n == 2); /* int64_t. */ \
415
+ ngpr = (ngpr + 1u) & ~1u; /* Align int64_t to regpair. */ \
416
+ } \
417
+ if (ngpr + n <= maxgpr) { \
418
+ sp = &cts->cb.gpr[ngpr]; \
419
+ ngpr += n; \
420
+ goto done; \
421
+ } \
422
+ }
423
+
424
+ #define CALLBACK_HANDLE_RET \
425
+ if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
426
+ *(double *)dp = *(float *)dp; /* FPRs always hold doubles. */
427
+
428
+ #elif LJ_TARGET_MIPS
429
+
430
+ #define CALLBACK_HANDLE_REGARG \
431
+ if (isfp && nfpr < CCALL_NARG_FPR) { /* Try to pass argument in FPRs. */ \
432
+ sp = (void *)((uint8_t *)&cts->cb.fpr[nfpr] + ((LJ_BE && n==1) ? 4 : 0)); \
433
+ nfpr++; ngpr += n; \
434
+ goto done; \
435
+ } else { /* Try to pass argument in GPRs. */ \
436
+ nfpr = CCALL_NARG_FPR; \
437
+ if (n > 1) ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \
438
+ if (ngpr + n <= maxgpr) { \
439
+ sp = &cts->cb.gpr[ngpr]; \
440
+ ngpr += n; \
441
+ goto done; \
442
+ } \
443
+ }
444
+
445
+ #define CALLBACK_HANDLE_RET \
446
+ if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
447
+ ((float *)dp)[1] = *(float *)dp;
448
+
449
+ #else
450
+ #error "Missing calling convention definitions for this architecture"
451
+ #endif
452
+
453
+ /* Convert and push callback arguments to Lua stack. */
454
+ static void callback_conv_args(CTState *cts, lua_State *L)
455
+ {
456
+ TValue *o = L->top;
457
+ intptr_t *stack = cts->cb.stack;
458
+ MSize slot = cts->cb.slot;
459
+ CTypeID id = 0, rid, fid;
460
+ int gcsteps = 0;
461
+ CType *ct;
462
+ GCfunc *fn;
463
+ int fntp;
464
+ MSize ngpr = 0, nsp = 0, maxgpr = CCALL_NARG_GPR;
465
+ #if CCALL_NARG_FPR
466
+ MSize nfpr = 0;
467
+ #if LJ_TARGET_ARM
468
+ MSize fprodd = 0;
469
+ #endif
470
+ #endif
471
+
472
+ if (slot < cts->cb.sizeid && (id = cts->cb.cbid[slot]) != 0) {
473
+ ct = ctype_get(cts, id);
474
+ rid = ctype_cid(ct->info); /* Return type. x86: +(spadj<<16). */
475
+ fn = funcV(lj_tab_getint(cts->miscmap, (int32_t)slot));
476
+ fntp = LJ_TFUNC;
477
+ } else { /* Must set up frame first, before throwing the error. */
478
+ ct = NULL;
479
+ rid = 0;
480
+ fn = (GCfunc *)L;
481
+ fntp = LJ_TTHREAD;
482
+ }
483
+ /* Continuation returns from callback. */
484
+ if (LJ_FR2) {
485
+ (o++)->u64 = LJ_CONT_FFI_CALLBACK;
486
+ (o++)->u64 = rid;
487
+ o++;
488
+ } else {
489
+ o->u32.lo = LJ_CONT_FFI_CALLBACK;
490
+ o->u32.hi = rid;
491
+ o++;
492
+ }
493
+ setframe_gc(o, obj2gco(fn), fntp);
494
+ setframe_ftsz(o, ((char *)(o+1) - (char *)L->base) + FRAME_CONT);
495
+ L->top = L->base = ++o;
496
+ if (!ct)
497
+ lj_err_caller(cts->L, LJ_ERR_FFI_BADCBACK);
498
+ if (isluafunc(fn))
499
+ setcframe_pc(L->cframe, proto_bc(funcproto(fn))+1);
500
+ lj_state_checkstack(L, LUA_MINSTACK); /* May throw. */
501
+ o = L->base; /* Might have been reallocated. */
502
+
503
+ #if LJ_TARGET_X86
504
+ /* x86 has several different calling conventions. */
505
+ switch (ctype_cconv(ct->info)) {
506
+ case CTCC_FASTCALL: maxgpr = 2; break;
507
+ case CTCC_THISCALL: maxgpr = 1; break;
508
+ default: maxgpr = 0; break;
509
+ }
510
+ #endif
511
+
512
+ fid = ct->sib;
513
+ while (fid) {
514
+ CType *ctf = ctype_get(cts, fid);
515
+ if (!ctype_isattrib(ctf->info)) {
516
+ CType *cta;
517
+ void *sp;
518
+ CTSize sz;
519
+ int isfp;
520
+ MSize n;
521
+ lua_assert(ctype_isfield(ctf->info));
522
+ cta = ctype_rawchild(cts, ctf);
523
+ isfp = ctype_isfp(cta->info);
524
+ sz = (cta->size + CTSIZE_PTR-1) & ~(CTSIZE_PTR-1);
525
+ n = sz / CTSIZE_PTR; /* Number of GPRs or stack slots needed. */
526
+
527
+ CALLBACK_HANDLE_REGARG /* Handle register arguments. */
528
+
529
+ /* Otherwise pass argument on stack. */
530
+ if (CCALL_ALIGN_STACKARG && LJ_32 && sz == 8)
531
+ nsp = (nsp + 1) & ~1u; /* Align 64 bit argument on stack. */
532
+ sp = &stack[nsp];
533
+ nsp += n;
534
+
535
+ done:
536
+ if (LJ_BE && cta->size < CTSIZE_PTR)
537
+ sp = (void *)((uint8_t *)sp + CTSIZE_PTR-cta->size);
538
+ gcsteps += lj_cconv_tv_ct(cts, cta, 0, o++, sp);
539
+ }
540
+ fid = ctf->sib;
541
+ }
542
+ L->top = o;
543
+ #if LJ_TARGET_X86
544
+ /* Store stack adjustment for returns from non-cdecl callbacks. */
545
+ if (ctype_cconv(ct->info) != CTCC_CDECL) {
546
+ #if LJ_FR2
547
+ (L->base-3)->u64 |= (nsp << (16+2));
548
+ #else
549
+ (L->base-2)->u32.hi |= (nsp << (16+2));
550
+ #endif
551
+ }
552
+ #endif
553
+ while (gcsteps-- > 0)
554
+ lj_gc_check(L);
555
+ }
556
+
557
+ /* Convert Lua object to callback result. */
558
+ static void callback_conv_result(CTState *cts, lua_State *L, TValue *o)
559
+ {
560
+ #if LJ_FR2
561
+ CType *ctr = ctype_raw(cts, (uint16_t)(L->base-3)->u64);
562
+ #else
563
+ CType *ctr = ctype_raw(cts, (uint16_t)(L->base-2)->u32.hi);
564
+ #endif
565
+ #if LJ_TARGET_X86
566
+ cts->cb.gpr[2] = 0;
567
+ #endif
568
+ if (!ctype_isvoid(ctr->info)) {
569
+ uint8_t *dp = (uint8_t *)&cts->cb.gpr[0];
570
+ #if CCALL_NUM_FPR
571
+ if (ctype_isfp(ctr->info))
572
+ dp = (uint8_t *)&cts->cb.fpr[0];
573
+ #endif
574
+ lj_cconv_ct_tv(cts, ctr, dp, o, 0);
575
+ #ifdef CALLBACK_HANDLE_RET
576
+ CALLBACK_HANDLE_RET
577
+ #endif
578
+ /* Extend returned integers to (at least) 32 bits. */
579
+ if (ctype_isinteger_or_bool(ctr->info) && ctr->size < 4) {
580
+ if (ctr->info & CTF_UNSIGNED)
581
+ *(uint32_t *)dp = ctr->size == 1 ? (uint32_t)*(uint8_t *)dp :
582
+ (uint32_t)*(uint16_t *)dp;
583
+ else
584
+ *(int32_t *)dp = ctr->size == 1 ? (int32_t)*(int8_t *)dp :
585
+ (int32_t)*(int16_t *)dp;
586
+ }
587
+ #if LJ_TARGET_X86
588
+ if (ctype_isfp(ctr->info))
589
+ cts->cb.gpr[2] = ctr->size == sizeof(float) ? 1 : 2;
590
+ #endif
591
+ }
592
+ }
593
+
594
+ /* Enter callback. */
595
+ lua_State * LJ_FASTCALL lj_ccallback_enter(CTState *cts, void *cf)
596
+ {
597
+ lua_State *L = cts->L;
598
+ global_State *g = cts->g;
599
+ lua_assert(L != NULL);
600
+ if (tvref(g->jit_base)) {
601
+ setstrV(L, L->top++, lj_err_str(L, LJ_ERR_FFI_BADCBACK));
602
+ if (g->panic) g->panic(L);
603
+ exit(EXIT_FAILURE);
604
+ }
605
+ lj_trace_abort(g); /* Never record across callback. */
606
+ /* Setup C frame. */
607
+ cframe_prev(cf) = L->cframe;
608
+ setcframe_L(cf, L);
609
+ cframe_errfunc(cf) = -1;
610
+ cframe_nres(cf) = 0;
611
+ L->cframe = cf;
612
+ callback_conv_args(cts, L);
613
+ return L; /* Now call the function on this stack. */
614
+ }
615
+
616
+ /* Leave callback. */
617
+ void LJ_FASTCALL lj_ccallback_leave(CTState *cts, TValue *o)
618
+ {
619
+ lua_State *L = cts->L;
620
+ GCfunc *fn;
621
+ TValue *obase = L->base;
622
+ L->base = L->top; /* Keep continuation frame for throwing errors. */
623
+ if (o >= L->base) {
624
+ /* PC of RET* is lost. Point to last line for result conv. errors. */
625
+ fn = curr_func(L);
626
+ if (isluafunc(fn)) {
627
+ GCproto *pt = funcproto(fn);
628
+ setcframe_pc(L->cframe, proto_bc(pt)+pt->sizebc+1);
629
+ }
630
+ }
631
+ callback_conv_result(cts, L, o);
632
+ /* Finally drop C frame and continuation frame. */
633
+ L->top -= 2+2*LJ_FR2;
634
+ L->base = obase;
635
+ L->cframe = cframe_prev(L->cframe);
636
+ cts->cb.slot = 0; /* Blacklist C function that called the callback. */
637
+ }
638
+
639
+ /* -- C callback management ----------------------------------------------- */
640
+
641
+ /* Get an unused slot in the callback slot table. */
642
+ static MSize callback_slot_new(CTState *cts, CType *ct)
643
+ {
644
+ CTypeID id = ctype_typeid(cts, ct);
645
+ CTypeID1 *cbid = cts->cb.cbid;
646
+ MSize top;
647
+ for (top = cts->cb.topid; top < cts->cb.sizeid; top++)
648
+ if (LJ_LIKELY(cbid[top] == 0))
649
+ goto found;
650
+ #if CALLBACK_MAX_SLOT
651
+ if (top >= CALLBACK_MAX_SLOT)
652
+ #endif
653
+ lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);
654
+ if (!cts->cb.mcode)
655
+ callback_mcode_new(cts);
656
+ lj_mem_growvec(cts->L, cbid, cts->cb.sizeid, CALLBACK_MAX_SLOT, CTypeID1);
657
+ cts->cb.cbid = cbid;
658
+ memset(cbid+top, 0, (cts->cb.sizeid-top)*sizeof(CTypeID1));
659
+ found:
660
+ cbid[top] = id;
661
+ cts->cb.topid = top+1;
662
+ return top;
663
+ }
664
+
665
+ /* Check for function pointer and supported argument/result types. */
666
+ static CType *callback_checkfunc(CTState *cts, CType *ct)
667
+ {
668
+ int narg = 0;
669
+ if (!ctype_isptr(ct->info) || (LJ_64 && ct->size != CTSIZE_PTR))
670
+ return NULL;
671
+ ct = ctype_rawchild(cts, ct);
672
+ if (ctype_isfunc(ct->info)) {
673
+ CType *ctr = ctype_rawchild(cts, ct);
674
+ CTypeID fid = ct->sib;
675
+ if (!(ctype_isvoid(ctr->info) || ctype_isenum(ctr->info) ||
676
+ ctype_isptr(ctr->info) || (ctype_isnum(ctr->info) && ctr->size <= 8)))
677
+ return NULL;
678
+ if ((ct->info & CTF_VARARG))
679
+ return NULL;
680
+ while (fid) {
681
+ CType *ctf = ctype_get(cts, fid);
682
+ if (!ctype_isattrib(ctf->info)) {
683
+ CType *cta;
684
+ lua_assert(ctype_isfield(ctf->info));
685
+ cta = ctype_rawchild(cts, ctf);
686
+ if (!(ctype_isenum(cta->info) || ctype_isptr(cta->info) ||
687
+ (ctype_isnum(cta->info) && cta->size <= 8)) ||
688
+ ++narg >= LUA_MINSTACK-3)
689
+ return NULL;
690
+ }
691
+ fid = ctf->sib;
692
+ }
693
+ return ct;
694
+ }
695
+ return NULL;
696
+ }
697
+
698
+ /* Create a new callback and return the callback function pointer. */
699
+ void *lj_ccallback_new(CTState *cts, CType *ct, GCfunc *fn)
700
+ {
701
+ ct = callback_checkfunc(cts, ct);
702
+ if (ct) {
703
+ MSize slot = callback_slot_new(cts, ct);
704
+ GCtab *t = cts->miscmap;
705
+ setfuncV(cts->L, lj_tab_setint(cts->L, t, (int32_t)slot), fn);
706
+ lj_gc_anybarriert(cts->L, t);
707
+ return callback_slot2ptr(cts, slot);
708
+ }
709
+ return NULL; /* Bad conversion. */
710
+ }
711
+
712
+ #endif