jeff_export.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. /**
  6. * @file jeff-export.h
  7. * @date Wed Aug 3 18:17:30 2011
  8. *
  9. * @brief Exported interface for operating or executing JEFF files.
  10. * All interface names start with "jeff_", which is the namespace name
  11. * of this module.
  12. */
  13. #ifndef JEFF_EXPORT_H
  14. #define JEFF_EXPORT_H
  15. #include "bni.h"
  16. #include "bh_types.h"
  17. /********************************************************************
  18. * Exported internal types
  19. ********************************************************************/
  20. /**
  21. * JEFF file handle type
  22. */
  23. struct JeffFileHeaderLinked;
  24. typedef struct JeffFileHeaderLinked *jeff_file_t;
  25. /**
  26. * JEFF class type
  27. */
  28. struct JeffClassHeaderLinked;
  29. typedef struct JeffClassHeaderLinked *jeff_class_t;
  30. /**
  31. * VM instance handle type
  32. */
  33. struct JeffInstanceLocalRoot;
  34. typedef struct JeffInstanceLocalRoot *jeff_instance_t;
  35. /**
  36. * Record of one native method's definition.
  37. */
  38. struct JeffNativeMethodDef {
  39. /* Mangled name of the native method. NULL for initialization
  40. functions. */
  41. const char *mangled_name;
  42. /* Points to the native C function. */
  43. void (*func_ptr)(uint32 *);
  44. /* Return size type of the native function. */
  45. uint32 return_size_type;
  46. };
  47. /********************************************************************
  48. * Interface for operating global environment of the JEFF VM
  49. ********************************************************************/
  50. /**
  51. * Load the core library from the given file buffer and initialize the
  52. * runtime environment (global objects etc.) of the VM. The thread
  53. * calls this function becomes the supervisor thread, which belongs to
  54. * a unique supervisor instance. Currently, if this init failed,
  55. * partially initialized states of the VM runtime environment won't be
  56. * cleaned up, so the VM must be shutdown and restarted. method_defs
  57. * points to an array of native method definition records.
  58. * Initialization functions must be in the front of the array and
  59. * following native method definitions must be sorted by their mangled
  60. * names.
  61. *
  62. * @param file the JEFF file of the core library
  63. * @param file_size the size of the JEFF file of the core library
  64. * @param method_defs native method definition records
  65. * @param method_defs_num number of native method records
  66. * @param heap the heap for the current (supervisor) instance
  67. *
  68. * @return true if succeeds, otherwise the error cannot be recovered
  69. */
  70. bool
  71. jeff_runtime_init(jeff_file_t file, unsigned file_size,
  72. struct JeffNativeMethodDef *method_defs, unsigned method_defs_num,
  73. void *heap);
  74. /**
  75. * Load a JEFF file into the VM from the given file buffer. It can be
  76. * called from any VM thread.
  77. *
  78. * @param file the JEFF file to be loaded
  79. * @param size the size of the JEFF file
  80. * @param is_library whether the JEFF file is a library
  81. * @param allow_to_load a function that returns true if classes in the
  82. * given package is allowed to be loaded. The NULL function pointer
  83. * allows all packages.
  84. * @param allow_to_link a function that returns true if classes in the
  85. * given package is allowed to be linked to. The NULL function
  86. * pointer allows all packages.
  87. *
  88. * @return true if succeeds, otherwise detailed error information is
  89. * passed to vmci_diagnostic_print. The caller can catch it by
  90. * implementing that function.
  91. */
  92. bool
  93. jeff_runtime_load(jeff_file_t file, unsigned size, bool is_library,
  94. bool (*allow_to_load)(const uint8 *pname, unsigned len),
  95. bool (*allow_to_link)(const uint8 *pname, unsigned plen,
  96. const uint8 *cname, unsigned clen));
  97. /**
  98. * Unload a JEFF file from the VM. All resources related to the JEFF
  99. * file except the JEFF file itself are released. It can be called
  100. * from any VM thread.
  101. *
  102. * @param file the JEFF file to be unloaded
  103. *
  104. * @return true if succeeds, otherwise detailed error information is
  105. * passed to vmci_diagnostic_print. The caller can catch it by
  106. * implementing that function.
  107. */
  108. bool
  109. jeff_runtime_unload(jeff_file_t file);
  110. /**
  111. * Return the JEFF file with the given file uid.
  112. *
  113. * @param fuid the unique id of a loaded JEFF file
  114. *
  115. * @return the JEFF file is exists, otherwise NULL
  116. */
  117. jeff_file_t
  118. jeff_runtime_fuid_to_file(unsigned fuid);
  119. /**
  120. * Return the file uid of the given JEFF file.
  121. *
  122. * @param file a loaded JEFF file
  123. *
  124. * @return the unique id of the given JEFF file
  125. */
  126. unsigned
  127. jeff_runtime_file_to_fuid(jeff_file_t file);
  128. /**
  129. * Create a supervisor thread belonging to the supervisor instance.
  130. * Threads that may interact with VM core must be either the main
  131. * thread of supervisor instance (which calls jeff_runtime_init) or
  132. * created by this function so that VM core required data structures
  133. * can be set up correctly.
  134. *
  135. * @param start_routine the start routine of the new thread
  136. * @param arg argument to the start routine
  137. *
  138. * @return true if succeeds, false otherwise
  139. */
  140. bool
  141. jeff_runtime_create_supervisor_thread(void* (*start_routine)(void *),
  142. void *arg);
  143. /**
  144. * Create a supervisor thread belonging to the supervisor instance.
  145. * Threads that may interact with VM core must be either the main
  146. * thread of supervisor instance (which calls jeff_runtime_init) or
  147. * created by this function so that VM core required data structures
  148. * can be set up correctly.
  149. *
  150. * @param start_routine the start routine of the new thread
  151. * @param arg argument to the start routine
  152. * @param prio thread priority
  153. *
  154. * @return true if succeeds, false otherwise
  155. */
  156. bool
  157. jeff_runtime_create_supervisor_thread_with_prio(void* (*start_routine)(void *),
  158. void *arg, int prio);
  159. /********************************************************************
  160. * Interface for operating instance local environment
  161. ********************************************************************/
  162. /**
  163. * Create a VM instance with the given JEFF file as its main file,
  164. * (i.e. the file containing the main class of the VM instance). This
  165. * function can be called from any VM thread, but it must be isolated
  166. * from JEFF file's unloading operation so that the main file won't be
  167. * unloaded before it's locked by the new instance. All instance
  168. * local memory except stacks of threads are allocated from the given
  169. * heap. If succeeds, it increases reference count of the main_file
  170. * and returns the handle of the new VM instance. The new instance's
  171. * main thread will run the start_routine with argument arg. If the
  172. * cleanup_routine is not NULL, it will be called after start_routine
  173. * returns and just before the main thread exits. It will also be
  174. * called after the instance is destroied. It is guaranteed to be
  175. * called exactly once no matter how the instance terminates.
  176. *
  177. * @param main_file the main JEFF file of the new instance
  178. * @param heap the private heap of the new instance
  179. * @param stack_depth the maximal nesting levels of Java methods of
  180. * the new instance. It must be <= 16 * 1024. Otherwise the instance
  181. * creation will fail.
  182. * @param start_routine start routine of the main thread. Don't
  183. * destroy the heap or inform other thread to do this at the end of
  184. * this routine since after it returns, VM core will call destroy
  185. * functions on objects allocated in this heap (e.g. locks and
  186. * condition variables). Do the destroying or informing of destroying
  187. * in the cleanup_routine.
  188. * @param arg the instance argument that will be passed to the start
  189. * routine. It can be get or set by jeff_runtime_get_instance_arg and
  190. * jeff_runtime_set_instance arg from threads of the instance. The
  191. * caller can use it to store instance local data.
  192. * @param cleanup_routine the optional cleanup routine for the
  193. * instance, which may be NULL. It may be executed in the end of the
  194. * main thread of the created instance by this function if this
  195. * instance exits normally, or it may be executed in a thread of other
  196. * instance in case this instance is being killed by that instance.
  197. * In both cases, this routine regards it is executed in a thread of
  198. * this instance (the instance created by this function) because
  199. * jeff_runtime_get_instance_arg will always return the argument of
  200. * this instance.
  201. *
  202. * @return the VM instance handle if succeeds, NULL otherwise
  203. */
  204. jeff_instance_t
  205. jeff_runtime_create_instance(jeff_file_t main_file, void *heap,
  206. unsigned stack_depth, void* (*start_routine)(void *), void *arg,
  207. void (*cleanup_routine)(void));
  208. /**
  209. * Destroy the given VM instance and decrease the reference count of
  210. * its main file and all explicitly used JEFF files. It can be called
  211. * from any VM thread. If there are alive threads of the instance,
  212. * they will be terminated mandatorily and then the cleanup routine is
  213. * called if it's not NULL.
  214. *
  215. * @param handle the handle of the instance to be destroyed
  216. */
  217. void
  218. jeff_runtime_destroy_instance(jeff_instance_t handle);
  219. /**
  220. * Retrieve the current instance's argument.
  221. *
  222. * @return the current instance's argument
  223. */
  224. void*
  225. jeff_runtime_get_instance_arg(void);
  226. /**
  227. * Set the current instance's argument.
  228. *
  229. * @return the new argument for the current instance
  230. */
  231. void
  232. jeff_runtime_set_instance_arg(void *arg);
  233. /**
  234. * Retrieve the current instance's heap.
  235. *
  236. * @return the current instance's heap
  237. */
  238. void*
  239. jeff_runtime_get_instance_heap(void);
  240. /**
  241. * Suspend all threads of the given VM instance. This function can
  242. * only be called from thread that is not of the given VM instance.
  243. *
  244. * @param handle the handle of the instance to be suspended
  245. */
  246. void
  247. jeff_runtime_suspend_instance(jeff_instance_t handle);
  248. /**
  249. * Resume all threads of the given VM instance. This function can
  250. * only be called from thread that is not of the given VM instance.
  251. *
  252. * @param handle the handle of the instance to be resumed
  253. */
  254. void
  255. jeff_runtime_resume_instance(jeff_instance_t handle);
  256. /**
  257. * Interrupt all threads of the given VM instance. This function can
  258. * only be called from thread that is not of the given VM instance.
  259. *
  260. * @param handle the handle of the instance to be interrupted
  261. * @param by_force whether the interruption is by force
  262. */
  263. void
  264. jeff_runtime_interrupt_instance(jeff_instance_t handle, bool by_force);
  265. /**
  266. * Wait for the given VM instance to terminate.
  267. *
  268. * @param ilr the VM instance to be waited for
  269. * @param mills wait millseconds to return
  270. */
  271. void
  272. jeff_runtime_wait_for_instance(jeff_instance_t ilr, int mills);
  273. /********************************************************************
  274. * Interface for operating thread local environment
  275. ********************************************************************/
  276. /**
  277. * Return true if there is an uncaught exception (thrown during
  278. * running an application or applet command).
  279. *
  280. * @return true if there is an uncaught exception
  281. */
  282. bool
  283. jeff_runtime_check_uncaught_exception(void);
  284. /**
  285. * Print qualified name of the uncaught exception (and stack trace if
  286. * enabled) by calling vmci_diagnostic_print.
  287. */
  288. void
  289. jeff_runtime_print_uncaught_exception(void);
  290. /**
  291. * Clear the uncaught exception.
  292. */
  293. void
  294. jeff_runtime_reset_uncaught_exception(void);
  295. /**
  296. * Change current thread to a safe state (VMWAIT). After calling this
  297. * and before calling jeff_runtime_exit_safe_state, all operations
  298. * must be safe, i.e. no GC or system level resource operations are
  299. * allowed because in a safe state, the VM instance is assumed to be
  300. * able to perform GC, JDWP or termination at any time. Usually, this
  301. * function is called just before the native code is going to wait for
  302. * something and the exiting safe state function is called just after
  303. * the waiting returns.
  304. */
  305. void
  306. jeff_runtime_enter_safe_state(void);
  307. /**
  308. * Change current thread to an unsafe state (RUNNING) so that unsafe
  309. * operations can also be done.
  310. */
  311. void
  312. jeff_runtime_exit_safe_state(void);
  313. /**
  314. * Set thread local error code for the current thread.
  315. *
  316. * @param code the error code to be set
  317. */
  318. void
  319. jeff_runtime_set_error(unsigned code);
  320. /**
  321. * Get the last error code of current thread.
  322. *
  323. * @return the last error code of current thread
  324. */
  325. unsigned
  326. jeff_runtime_get_error(void);
  327. /********************************************************************
  328. * Interface for GC support
  329. ********************************************************************/
  330. /**
  331. * Traverse all objects of the given heap that are global or locate in
  332. * threads' frames and return them by calling vmci_gc_rootset_elem.
  333. * This function will suspend all threads except the current one of
  334. * the VM instance owning the given heap before traversing. It
  335. * traverses either all or none of the rootset objects, and returns
  336. * true and false respectively. If it returns false, the GC process
  337. * shouldn't proceed and is not necessary to unmark anything because
  338. * no objects are marked. The function jeff_runtime_gc_finished must
  339. * be called if and only if this function returns true so as to resume
  340. * threads that are suspended during GC process.
  341. *
  342. * @param heap the heap for which rootset objects are looked up
  343. *
  344. * @return true if succeeds, false otherwise
  345. */
  346. bool
  347. jeff_runtime_traverse_gc_rootset(void *heap);
  348. /**
  349. * Get the reference offset table of the given object. If the
  350. * returned value R >= 0, *ret points to the reference offset table of
  351. * the object and R is the number of offsets in the table. Otherwise,
  352. * if the returned value R < 0, all reference fields of the object
  353. * must be in a continuous region (usually the object is an array),
  354. * then *ret is the offset to the first field in the region and R is
  355. * the number of such fields in the region.
  356. *
  357. * @param obj pointer to the Java object
  358. * @param ret points to a pointer for storing the reference offset
  359. * table if return value >= 0, or for storing the offset to the first
  360. * object reference in the Java object if return value < 0
  361. *
  362. * @return number of offsets in the reference_offset table if >= 0, or
  363. * number of object references in the object if < 0
  364. */
  365. int
  366. jeff_object_get_reference_offsets(const jobject obj, uint16 **ret);
  367. /**
  368. * Inform the containing VM instance that GC has finished and all
  369. * suspended threads can be resumed. This function must be called if
  370. * and only if jeff_runtime_traverse_gc_rootset returns true.
  371. */
  372. void
  373. jeff_runtime_gc_finished(void);
  374. /********************************************************************
  375. * Interface for tooling support
  376. ********************************************************************/
  377. /**
  378. * This function is used to suspend the main thread of VM instance so
  379. * that debugger can have chance to connect to the VM instance, set
  380. * breakpoints and do any other debug settings. It must be called
  381. * from the main thread of VM instance at the point just after VM
  382. * instance initialization finishes and just before application code
  383. * is to be executed.
  384. */
  385. void
  386. jeff_tool_suspend_self(void);
  387. /**
  388. * Start up tool agent thread for the given VM instance. It can be
  389. * called from any VM thread.
  390. *
  391. * @param handle the VM instance for which tool agent is started up
  392. * @param queue queue of the tool agent
  393. * @return true if succeeds, false otherwise
  394. */
  395. bool
  396. jeff_tool_start_agent(jeff_instance_t handle, void *queue);
  397. /********************************************************************
  398. * Interface for toolkit support
  399. ********************************************************************/
  400. /**
  401. * Return the JEFF class pointer of the given class name.
  402. *
  403. * @param class_name the qualified class name
  404. *
  405. * @return the JEFF class pointer
  406. */
  407. jeff_class_t
  408. jeff_tool_get_jeff_class(const char *class_name);
  409. /**
  410. * Get the mangled class name of the given class.
  411. *
  412. * @param clz the JEFF class
  413. * @param buf buffer for returning the mangled name
  414. * @param buf_size size of the buffer
  415. *
  416. * @return actual size of the mangled class name including the
  417. * terminating null byte
  418. */
  419. unsigned
  420. jeff_tool_get_mangled_class_name(jeff_class_t clz, char *buf,
  421. unsigned buf_size);
  422. /**
  423. * Get class index of given class in its containing JEFF file.
  424. *
  425. * @param clz the JEFF class
  426. *
  427. * @return class index in the containing JEFF file
  428. */
  429. int
  430. jeff_tool_get_class_index(jeff_class_t clz);
  431. /**
  432. * Callback handler prototype for traversing fields of class.
  433. *
  434. * @param arg argument passed to the handler from caller
  435. * @param access_flag access flag of the method
  436. * @param name the field name
  437. * @param descriptor mangled field type descriptor
  438. * @param offset the offset of the field in the class
  439. * @param size size of the field
  440. */
  441. typedef void
  442. (*JeffToolFieldHandler)(void *arg, unsigned access_flag, const char *name,
  443. const char *descriptor, unsigned offset, unsigned size);
  444. /**
  445. * Traverse all fields of the given class, including those inherited
  446. * from super classes. The fields are traversed in the same order as
  447. * the field layout of the class.
  448. *
  449. * @param arg argument to be passed to the handler
  450. * @param clz the JEFF class
  451. * @param instance instance fields or static fielts
  452. * @param handler the callback handler for each field
  453. */
  454. void
  455. jeff_tool_foreach_field(void *arg, jeff_class_t clz, bool instance,
  456. JeffToolFieldHandler handler);
  457. /**
  458. * Callback handler prototype for traversing methods of class.
  459. *
  460. * @param arg argument passed to the handler from caller
  461. * @param access_flag access flag of the method
  462. * @param name mangled name of the method
  463. * @param descriptor mangled method arguments descriptor
  464. * @param retune_type mangled descriptor of method's return type
  465. */
  466. typedef void
  467. (*JeffToolMethodHandler)(void *arg, unsigned access_flag, const char *name,
  468. const char *descriptor, const char *return_type);
  469. /**
  470. * Traverse all methods of the given class.
  471. *
  472. * @param arg argument to be passed to the handler
  473. * @param clz the JEFF class
  474. * @param handler the callback handler for each method
  475. */
  476. void
  477. jeff_tool_foreach_method(void *arg, jeff_class_t clz,
  478. JeffToolMethodHandler handler);
  479. /**
  480. * Callback handler prototype for traversing classes of main file.
  481. *
  482. * @param arg argument passed to the handler from caller
  483. * @param clz pointer to one class in the main file
  484. */
  485. typedef void
  486. (*JeffToolClassHandler)(void *arg, jeff_class_t clz);
  487. /**
  488. * Traverse all classes of the main file.
  489. *
  490. * @param arg argument to be passed to the handler
  491. * @param handler the callback handler for each class
  492. */
  493. void
  494. jeff_tool_foreach_class(void *arg, JeffToolClassHandler handler);
  495. /********************************************************************
  496. * Interface for executing applications
  497. ********************************************************************/
  498. /**
  499. * Initialize global environment for executing Java applications.
  500. *
  501. * @return true if succeeds, false otherwise
  502. */
  503. bool
  504. jeff_application_env_init(void);
  505. /**
  506. * Find the unique class containing a public static "main
  507. * ([Ljava.lang.String;)V" method from the main JEFF file of the
  508. * current instance and execute that method.
  509. *
  510. * @param argc the number of arguments
  511. * @param argv the arguments array
  512. *
  513. * @return true if the main method is called, false otherwise (e.g. an
  514. * exception occurs when preparing the arguments Java string array)
  515. */
  516. bool
  517. jeff_application_execute(int argc, char *argv[]);
  518. /********************************************************************
  519. * Interface for executing applets
  520. ********************************************************************/
  521. /**
  522. * Initialize global environment for executing applets.
  523. *
  524. * @return true if succeeds, false otherwise
  525. */
  526. bool
  527. jeff_applet_env_init(void);
  528. /**
  529. * Start to run from com.intel.runtime.core.RuntimeContext.main with a
  530. * default message queue size and a default service class object. If
  531. * the main JEFF file of the current VM instance contains exactly one
  532. * class that is derived from com.intel.util.IntelApplet, then use it
  533. * as the default service class.
  534. *
  535. * @param queue_size the default main message queue size
  536. * @param default_service_class qualified class name of the default
  537. * service class (entry point class), which must be in the main JEFF
  538. * file. If NULL, find the default main class with rules described
  539. * above.
  540. *
  541. * @return true if succeeds, false otherwise
  542. */
  543. bool
  544. jeff_applet_start(int queue_size, const char *default_service_class);
  545. #endif