jeff_export.h 20 KB

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