1 /* src/native/native.c - table of native functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31 #if defined(ENABLE_LTDL) && defined(HAVE_LTDL_H)
39 #include "mm/memory.h"
41 #include "native/jni.h"
42 #include "native/native.h"
44 #include "native/vm/nativevm.h"
46 #include "threads/lock-common.h"
48 #include "toolbox/avl.h"
49 #include "toolbox/hashtable.h"
50 #include "toolbox/logging.h"
52 #include "vm/builtin.h"
53 #include "vm/exceptions.h"
54 #include "vm/global.h"
55 #include "vm/stringlocal.h"
58 #include "vm/jit/asmpart.h"
59 #include "vm/jit/jit.h"
61 #include "vmcore/loader.h"
62 #include "vmcore/options.h"
63 #include "vm/resolve.h"
65 #if defined(ENABLE_JVMTI)
66 #include "native/jvmti/cacaodbg.h"
70 /* global variables ***********************************************************/
72 static avl_tree_t *tree_native_methods;
74 #if defined(ENABLE_LTDL)
75 static hashtable *hashtable_library;
79 /* prototypes *****************************************************************/
81 static s4 native_tree_native_methods_comparator(const void *treenode, const void *node);
84 /* native_init *****************************************************************
86 Initializes the native subsystem.
88 *******************************************************************************/
90 bool native_init(void)
92 TRACESUBSYSTEMINITIALIZATION("native_init");
94 #if defined(ENABLE_LTDL)
95 /* initialize libltdl */
98 vm_abort("native_init: lt_dlinit failed: %s\n", lt_dlerror());
100 /* initialize library hashtable, 10 entries should be enough */
102 hashtable_library = NEW(hashtable);
104 hashtable_create(hashtable_library, 10);
107 /* initialize the native methods table */
109 tree_native_methods = avl_create(&native_tree_native_methods_comparator);
111 /* everything's ok */
117 /* native_tree_native_methods_comparator ***************************************
119 Comparison function for AVL tree of native methods.
122 treenode....node in the tree
123 node........node to compare with tree-node
128 *******************************************************************************/
130 static s4 native_tree_native_methods_comparator(const void *treenode, const void *node)
132 const native_methods_node_t *treenmn;
133 const native_methods_node_t *nmn;
138 /* these are for walking the tree */
140 if (treenmn->classname < nmn->classname)
142 else if (treenmn->classname > nmn->classname)
145 if (treenmn->name < nmn->name)
147 else if (treenmn->name > nmn->name)
150 if (treenmn->descriptor < nmn->descriptor)
152 else if (treenmn->descriptor > nmn->descriptor)
155 /* all pointers are equal, we have found the entry */
161 /* native_make_overloaded_function *********************************************
165 *******************************************************************************/
167 static utf *native_make_overloaded_function(utf *name, utf *descriptor)
181 utf_ptr = descriptor->text;
182 namelen = strlen(name->text) + strlen("__") + strlen("0");
184 /* calculate additional length */
186 while ((c = utf_nextu2(&utf_ptr)) != ')') {
203 while (utf_nextu2(&utf_ptr) != ';')
214 /* reallocate memory */
216 i = strlen(name->text);
218 newname = DMNEW(char, namelen);
219 MCOPY(newname, name->text, char, i);
221 utf_ptr = descriptor->text;
226 while ((c = utf_nextu2(&utf_ptr)) != ')') {
244 while ((c = utf_nextu2(&utf_ptr)) != ';')
245 if (((c >= 'a') && (c <= 'z')) ||
246 ((c >= 'A') && (c <= 'Z')) ||
247 ((c >= '0') && (c <= '9')))
265 /* make a utf-string */
267 u = utf_new_char(newname);
277 /* native_insert_char **********************************************************
279 Inserts the passed UTF character into the native method name. If
280 necessary it is escaped properly.
282 *******************************************************************************/
284 static s4 native_insert_char(char *name, u4 pos, u2 c)
292 /* replace '/' or '.' with '_' */
297 /* escape sequence for '_' is '_1' */
303 /* escape sequence for ';' is '_2' */
309 /* escape sequence for '[' is '_1' */
318 /* unicode character */
322 for (i = 0; i < 4; ++i) {
324 name[pos + 4 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
333 /* return the new buffer index */
339 /* native_method_symbol ********************************************************
341 Generate a method-symbol string out of the class name and the
344 *******************************************************************************/
346 static utf *native_method_symbol(utf *classname, utf *methodname)
361 /* Calculate length of native function name. We multiply the
362 class and method name length by 6 as this is the maxium
363 escape-sequence that can be generated (unicode). */
367 utf_get_number_of_u2s(classname) * 6 +
369 utf_get_number_of_u2s(methodname) * 6 +
372 /* allocate memory */
374 name = DMNEW(char, namelen);
376 /* generate name of native functions */
378 strcpy(name, "Java_");
379 pos = strlen("Java_");
381 utf_ptr = classname->text;
382 utf_endptr = UTF_END(classname);
384 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
386 pos = native_insert_char(name, pos, c);
389 /* seperator between class and method */
393 utf_ptr = methodname->text;
394 utf_endptr = UTF_END(methodname);
396 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
398 pos = native_insert_char(name, pos, c);
405 /* check for an buffer overflow */
407 assert(pos <= namelen);
409 /* make a utf-string */
411 u = utf_new_char(name);
421 /* native_method_register ******************************************************
423 Register a native method in the native method table.
425 *******************************************************************************/
427 void native_method_register(utf *classname, const JNINativeMethod *methods,
430 native_methods_node_t *nmn;
435 /* insert all methods passed */
437 for (i = 0; i < count; i++) {
438 if (opt_verbosejni) {
439 printf("[Registering JNI native method ");
440 utf_display_printable_ascii_classname(classname);
441 printf(".%s]\n", methods[i].name);
444 /* generate the utf8 names */
446 name = utf_new_char(methods[i].name);
447 descriptor = utf_new_char(methods[i].signature);
449 /* allocate a new tree node */
451 nmn = NEW(native_methods_node_t);
453 nmn->classname = classname;
455 nmn->descriptor = descriptor;
456 nmn->function = (functionptr) (ptrint) methods[i].fnPtr;
458 /* insert the method into the tree */
460 avl_insert(tree_native_methods, nmn);
465 /* native_method_find **********************************************************
467 Find a native method in the native method table.
469 *******************************************************************************/
471 static functionptr native_method_find(methodinfo *m)
473 native_methods_node_t tmpnmn;
474 native_methods_node_t *nmn;
476 /* fill the temporary structure used for searching the tree */
478 tmpnmn.classname = m->clazz->name;
479 tmpnmn.name = m->name;
480 tmpnmn.descriptor = m->descriptor;
482 /* find the entry in the AVL-tree */
484 nmn = avl_find(tree_native_methods, &tmpnmn);
489 return nmn->function;
493 /* native_method_resolve *******************************************************
495 Resolves a native method, maybe from a dynamic library.
498 m ... methodinfo of the native Java method to resolve
501 pointer to the resolved method (symbol)
503 *******************************************************************************/
505 functionptr native_method_resolve(methodinfo *m)
510 #if defined(ENABLE_LTDL)
512 hashtable_library_loader_entry *le;
513 hashtable_library_name_entry *ne;
514 u4 key; /* hashkey */
515 u4 slot; /* slot in hashtable */
517 #if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
518 methodinfo *method_findNative;
524 if (opt_verbosejni) {
525 printf("[Dynamic-linking native method ");
526 utf_display_printable_ascii_classname(m->clazz->name);
528 utf_display_printable_ascii(m->name);
532 /* generate method symbol string */
534 name = native_method_symbol(m->clazz->name, m->name);
536 /* generate overloaded function (having the types in it's name) */
538 newname = native_make_overloaded_function(name, m->descriptor);
540 /* check the library hash entries of the classloader of the
545 #if defined(ENABLE_LTDL)
546 /* Get the classloader. */
548 cl = class_get_classloader(m->clazz);
550 /* normally addresses are aligned to 4, 8 or 16 bytes */
552 key = ((u4) (ptrint) cl) >> 4; /* align to 16-byte */
553 slot = key & (hashtable_library->size - 1);
554 le = hashtable_library->ptr[slot];
556 /* iterate through loaders in this hash slot */
558 while ((le != NULL) && (f == NULL)) {
559 /* iterate through names in this loader */
563 while ((ne != NULL) && (f == NULL)) {
564 f = (functionptr) (ptrint) lt_dlsym(ne->handle, name->text);
567 f = (functionptr) (ptrint) lt_dlsym(ne->handle, newname->text);
575 # if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
577 /* We can resolve the function directly from
578 java.lang.ClassLoader as it's a static function. */
579 /* XXX should be done in native_init */
582 class_resolveclassmethod(class_java_lang_ClassLoader,
584 utf_java_lang_ClassLoader_java_lang_String__J,
585 class_java_lang_ClassLoader,
588 if (method_findNative == NULL)
591 /* try the normal name */
593 s = javastring_new(name);
595 f = (functionptr) (intptr_t) vm_call_method_long(method_findNative,
598 /* if not found, try the mangled name */
601 s = javastring_new(newname);
603 f = (functionptr) (intptr_t) vm_call_method_long(method_findNative,
614 /* If not found, try to find the native function symbol in the
618 f = native_method_find(m);
622 printf("internal ]\n");
625 #if defined(ENABLE_JVMTI)
626 /* fire Native Method Bind event */
627 if (jvmti) jvmti_NativeMethodBind(m, f, &f);
630 /* no symbol found? throw exception */
634 printf("failed ]\n");
636 exceptions_throw_unsatisfiedlinkerror(m->name);
643 /* native_library_open *********************************************************
645 Open a native library with the given utf8 name.
648 filename ... filename of the library to open
651 handle of the opened library
653 *******************************************************************************/
655 #if defined(ENABLE_LTDL)
656 lt_dlhandle native_library_open(utf *filename)
660 if (opt_verbosejni) {
661 printf("[Loading native library ");
662 utf_display_printable_ascii(filename);
666 /* try to open the library */
668 handle = lt_dlopen(filename->text);
670 if (handle == NULL) {
672 printf("failed ]\n");
676 log_print("native_library_open: lt_dlopen failed: ");
677 log_print(lt_dlerror());
692 /* native_library_close ********************************************************
694 Close the native library of the given handle.
697 handle ... handle of the open library
699 *******************************************************************************/
701 #if defined(ENABLE_LTDL)
702 void native_library_close(lt_dlhandle handle)
706 if (opt_verbosejni) {
707 printf("[Unloading native library ");
708 /* utf_display_printable_ascii(filename); */
712 /* Close the library. */
714 result = lt_dlclose(handle);
719 log_print("native_library_close: lt_dlclose failed: ");
720 log_print(lt_dlerror());
728 /* native_library_add **********************************************************
730 Adds an entry to the native library hashtable.
732 *******************************************************************************/
734 #if defined(ENABLE_LTDL)
735 void native_library_add(utf *filename, classloader_t *loader, lt_dlhandle handle)
737 hashtable_library_loader_entry *le;
738 hashtable_library_name_entry *ne; /* library name */
739 u4 key; /* hashkey */
740 u4 slot; /* slot in hashtable */
742 LOCK_MONITOR_ENTER(hashtable_library->header);
744 /* normally addresses are aligned to 4, 8 or 16 bytes */
746 key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
747 slot = key & (hashtable_library->size - 1);
748 le = hashtable_library->ptr[slot];
750 /* search external hash chain for the entry */
753 if (le->loader == loader)
756 le = le->hashlink; /* next element in external chain */
759 /* no loader found? create a new entry */
762 le = NEW(hashtable_library_loader_entry);
767 /* insert entry into hashtable */
770 (hashtable_library_loader_entry *) hashtable_library->ptr[slot];
771 hashtable_library->ptr[slot] = le;
773 /* update number of hashtable-entries */
775 hashtable_library->entries++;
779 /* search for library name */
784 if (ne->name == filename) {
785 LOCK_MONITOR_EXIT(hashtable_library->header);
790 ne = ne->hashlink; /* next element in external chain */
793 /* not found? add the library name to the classloader */
795 ne = NEW(hashtable_library_name_entry);
800 /* insert entry into external chain */
802 ne->hashlink = le->namelink;
805 LOCK_MONITOR_EXIT(hashtable_library->header);
810 /* native_library_find *********************************************************
812 Find an entry in the native library hashtable.
814 *******************************************************************************/
816 #if defined(ENABLE_LTDL)
817 hashtable_library_name_entry *native_library_find(utf *filename,
818 classloader_t *loader)
820 hashtable_library_loader_entry *le;
821 hashtable_library_name_entry *ne; /* library name */
822 u4 key; /* hashkey */
823 u4 slot; /* slot in hashtable */
825 /* normally addresses are aligned to 4, 8 or 16 bytes */
827 key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
828 slot = key & (hashtable_library->size - 1);
829 le = hashtable_library->ptr[slot];
831 /* search external hash chain for the entry */
834 if (le->loader == loader)
837 le = le->hashlink; /* next element in external chain */
840 /* no loader found? return NULL */
845 /* search for library name */
850 if (ne->name == filename)
853 ne = ne->hashlink; /* next element in external chain */
856 /* return entry, if no entry was found, ne is NULL */
863 /* native_library_load *********************************************************
865 Load a native library and initialize it, if possible.
868 name ... name of the library
869 cl ..... classloader which loads this library
872 1 ... library loaded successfully
875 *******************************************************************************/
877 int native_library_load(JNIEnv *env, utf *name, classloader_t *cl)
879 #if defined(ENABLE_LTDL)
881 # if defined(ENABLE_JNI)
887 exceptions_throw_nullpointerexception();
891 /* Is the library already loaded? */
893 if (native_library_find(name, cl) != NULL)
896 /* Open the library. */
898 handle = native_library_open(name);
903 # if defined(ENABLE_JNI)
904 /* Resolve JNI_OnLoad function. */
906 onload = lt_dlsym(handle, "JNI_OnLoad");
908 if (onload != NULL) {
909 JNIEXPORT int32_t (JNICALL *JNI_OnLoad) (JavaVM *, void *);
912 JNI_OnLoad = (JNIEXPORT int32_t (JNICALL *)(JavaVM *, void *)) (ptrint) onload;
914 (*env)->GetJavaVM(env, &vm);
916 version = JNI_OnLoad(vm, NULL);
918 /* If the version is not 1.2 and not 1.4 the library cannot be
921 if ((version != JNI_VERSION_1_2) && (version != JNI_VERSION_1_4)) {
928 /* Insert the library name into the library hash. */
930 native_library_add(name, cl, handle);
934 vm_abort("native_library_load: not available");
936 /* Keep compiler happy. */
943 /* native_new_and_init *********************************************************
945 Creates a new object on the heap and calls the initializer.
946 Returns the object pointer or NULL if memory is exhausted.
948 *******************************************************************************/
950 java_handle_t *native_new_and_init(classinfo *c)
956 vm_abort("native_new_and_init: c == NULL");
965 /* try to find the initializer */
967 m = class_findmethod(c, utf_init, utf_void__void);
969 /* ATTENTION: returning the object here is ok, since the class may
970 not have an initializer */
975 /* call initializer */
977 (void) vm_call_method(m, o);
983 java_handle_t *native_new_and_init_string(classinfo *c, java_handle_t *s)
989 vm_abort("native_new_and_init_string: c == NULL");
998 /* find initializer */
1000 m = class_findmethod(c, utf_init, utf_java_lang_String__void);
1002 /* initializer not found */
1007 /* call initializer */
1009 (void) vm_call_method(m, o, s);
1016 * These are local overrides for various environment variables in Emacs.
1017 * Please do not remove this and leave it at the end of the file, where
1018 * Emacs will automagically detect them.
1019 * ---------------------------------------------------------------------
1022 * indent-tabs-mode: t
1026 * vim:noexpandtab:sw=4:ts=4: