1 /* src/native/native.c - table of native functions
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 $Id: native.c 7918 2007-05-20 20:42:18Z michi $
35 #if !defined(WITH_STATIC_CLASSPATH)
41 #include "mm/memory.h"
43 #include "native/jni.h"
44 #include "native/native.h"
46 #include "native/vm/nativevm.h"
48 #include "threads/lock-common.h"
50 #include "toolbox/avl.h"
51 #include "toolbox/hashtable.h"
52 #include "toolbox/logging.h"
54 #include "vm/builtin.h"
55 #include "vm/exceptions.h"
56 #include "vm/global.h"
57 #include "vm/stringlocal.h"
60 #include "vm/jit/asmpart.h"
61 #include "vm/jit/jit.h"
63 #include "vmcore/loader.h"
64 #include "vmcore/options.h"
65 #include "vm/resolve.h"
67 #if defined(ENABLE_JVMTI)
68 #include "native/jvmti/cacaodbg.h"
72 /* include table of native functions ******************************************/
74 #if defined(WITH_STATIC_CLASSPATH)
75 # include "native/nativetable.inc"
79 /* tables for methods *********************************************************/
81 #if defined(WITH_STATIC_CLASSPATH)
82 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
84 /* table for fast string comparison */
85 static nativecompref nativecomptable[NATIVETABLESIZE];
87 /* string comparsion table initialized */
88 static bool nativecompdone = false;
92 /* global variables ***********************************************************/
94 static avl_tree_t *tree_native_methods;
95 static hashtable *hashtable_library;
98 /* prototypes *****************************************************************/
100 static s4 native_tree_native_methods_comparator(const void *treenode, const void *node);
103 /* native_init *****************************************************************
105 Initializes the native subsystem.
107 *******************************************************************************/
109 bool native_init(void)
111 #if !defined(WITH_STATIC_CLASSPATH)
112 /* initialize libltdl */
115 vm_abort("native_init: lt_dlinit failed: %s\n", lt_dlerror());
117 /* initialize library hashtable, 10 entries should be enough */
119 hashtable_library = NEW(hashtable);
121 hashtable_create(hashtable_library, 10);
124 /* initialize the native methods table */
126 tree_native_methods = avl_create(&native_tree_native_methods_comparator);
128 /* register the intern native functions */
132 /* everything's ok */
138 /* native_tree_native_methods_comparator ***************************************
140 Comparison function for AVL tree of native methods.
143 treenode....node in the tree
144 node........node to compare with tree-node
149 *******************************************************************************/
151 static s4 native_tree_native_methods_comparator(const void *treenode, const void *node)
153 const native_methods_node_t *treenmn;
154 const native_methods_node_t *nmn;
159 /* these are for walking the tree */
161 if (treenmn->classname < nmn->classname)
163 else if (treenmn->classname > nmn->classname)
166 if (treenmn->name < nmn->name)
168 else if (treenmn->name > nmn->name)
171 if (treenmn->descriptor < nmn->descriptor)
173 else if (treenmn->descriptor > nmn->descriptor)
176 /* all pointers are equal, we have found the entry */
182 /* native_make_overloaded_function *********************************************
186 *******************************************************************************/
188 #if !defined(WITH_STATIC_CLASSPATH)
189 static utf *native_make_overloaded_function(utf *name, utf *descriptor)
201 dumpsize = dump_size();
203 utf_ptr = descriptor->text;
204 namelen = strlen(name->text) + strlen("__") + strlen("0");
206 /* calculate additional length */
208 while ((c = utf_nextu2(&utf_ptr)) != ')') {
225 while (utf_nextu2(&utf_ptr) != ';')
236 /* reallocate memory */
238 i = strlen(name->text);
240 newname = DMNEW(char, namelen);
241 MCOPY(newname, name->text, char, i);
243 utf_ptr = descriptor->text;
248 while ((c = utf_nextu2(&utf_ptr)) != ')') {
266 while ((c = utf_nextu2(&utf_ptr)) != ';')
267 if (((c >= 'a') && (c <= 'z')) ||
268 ((c >= 'A') && (c <= 'Z')) ||
269 ((c >= '0') && (c <= '9')))
287 /* make a utf-string */
289 u = utf_new_char(newname);
293 dump_release(dumpsize);
299 /* native_insert_char **********************************************************
301 Inserts the passed UTF character into the native method name. If
302 necessary it is escaped properly.
304 *******************************************************************************/
306 static s4 native_insert_char(char *name, u4 pos, u2 c)
314 /* replace '/' or '.' with '_' */
319 /* escape sequence for '_' is '_1' */
325 /* escape sequence for ';' is '_2' */
331 /* escape sequence for '[' is '_1' */
340 /* unicode character */
344 for (i = 0; i < 4; ++i) {
346 name[pos + 4 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
355 /* return the new buffer index */
361 /* native_method_symbol ********************************************************
363 Generate a method-symbol string out of the class name and the
366 *******************************************************************************/
368 static utf *native_method_symbol(utf *classname, utf *methodname)
381 dumpsize = dump_size();
383 /* Calculate length of native function name. We multiply the
384 class and method name length by 6 as this is the maxium
385 escape-sequence that can be generated (unicode). */
389 utf_get_number_of_u2s(classname) * 6 +
391 utf_get_number_of_u2s(methodname) * 6 +
394 /* allocate memory */
396 name = DMNEW(char, namelen);
398 /* generate name of native functions */
400 strcpy(name, "Java_");
401 pos = strlen("Java_");
403 utf_ptr = classname->text;
404 utf_endptr = UTF_END(classname);
406 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
408 pos = native_insert_char(name, pos, c);
411 /* seperator between class and method */
415 utf_ptr = methodname->text;
416 utf_endptr = UTF_END(methodname);
418 for (; utf_ptr < utf_endptr; utf_ptr++, pos++) {
420 pos = native_insert_char(name, pos, c);
427 /* check for an buffer overflow */
429 assert(pos <= namelen);
431 /* make a utf-string */
433 u = utf_new_char(name);
437 dump_release(dumpsize);
443 /* native_method_register ******************************************************
445 Register a native method in the native method table.
447 *******************************************************************************/
449 void native_method_register(utf *classname, JNINativeMethod *methods, s4 count)
451 native_methods_node_t *nmn;
456 /* insert all methods passed */
458 for (i = 0; i < count; i++) {
459 if (opt_verbosejni) {
460 printf("[Registering JNI native method ");
461 utf_display_printable_ascii_classname(classname);
462 printf(".%s]\n", methods[i].name);
465 /* generate the utf8 names */
467 name = utf_new_char(methods[i].name);
468 descriptor = utf_new_char(methods[i].signature);
470 /* allocate a new tree node */
472 nmn = NEW(native_methods_node_t);
474 nmn->classname = classname;
476 nmn->descriptor = descriptor;
477 nmn->function = (functionptr) (ptrint) methods[i].fnPtr;
479 /* insert the method into the tree */
481 avl_insert(tree_native_methods, nmn);
486 /* native_method_find **********************************************************
488 Find a native method in the native method table.
490 *******************************************************************************/
492 static functionptr native_method_find(methodinfo *m)
494 native_methods_node_t tmpnmn;
495 native_methods_node_t *nmn;
497 /* fill the temporary structure used for searching the tree */
499 tmpnmn.classname = m->class->name;
500 tmpnmn.name = m->name;
501 tmpnmn.descriptor = m->descriptor;
503 /* find the entry in the AVL-tree */
505 nmn = avl_find(tree_native_methods, &tmpnmn);
510 return nmn->function;
514 /* native_library_open *********************************************************
516 Open a native library with the given utf8 name.
518 *******************************************************************************/
520 #if !defined(WITH_STATIC_CLASSPATH)
521 lt_dlhandle native_library_open(utf *filename)
525 /* try to open the library */
527 handle = lt_dlopen(filename->text);
529 if (handle == NULL) {
532 log_print("native_library_load: lt_dlopen failed: ");
533 log_print(lt_dlerror());
545 /* native_library_add **********************************************************
547 Adds an entry to the native library hashtable.
549 *******************************************************************************/
551 #if !defined(WITH_STATIC_CLASSPATH)
552 void native_library_add(utf *filename, java_objectheader *loader,
555 hashtable_classloader_entry *cle;
556 hashtable_library_loader_entry *le;
557 hashtable_library_name_entry *ne; /* library name */
558 u4 key; /* hashkey */
559 u4 slot; /* slot in hashtable */
561 LOCK_MONITOR_ENTER(hashtable_library->header);
563 /* insert loader into the classloader hashtable */
565 cle = loader_hashtable_classloader_add(loader);
567 /* normally addresses are aligned to 4, 8 or 16 bytes */
569 key = ((u4) (ptrint) cle) >> 4; /* align to 16-byte boundaries */
570 slot = key & (hashtable_library->size - 1);
571 le = hashtable_library->ptr[slot];
573 /* search external hash chain for the entry */
579 le = le->hashlink; /* next element in external chain */
582 /* no loader found? create a new entry */
585 le = NEW(hashtable_library_loader_entry);
590 /* insert entry into hashtable */
593 (hashtable_library_loader_entry *) hashtable_library->ptr[slot];
594 hashtable_library->ptr[slot] = le;
596 /* update number of hashtable-entries */
598 hashtable_library->entries++;
602 /* search for library name */
607 if (ne->name == filename) {
608 LOCK_MONITOR_EXIT(hashtable_library->header);
613 ne = ne->hashlink; /* next element in external chain */
616 /* not found? add the library name to the classloader */
618 ne = NEW(hashtable_library_name_entry);
623 /* insert entry into external chain */
625 ne->hashlink = le->namelink;
628 LOCK_MONITOR_EXIT(hashtable_library->header);
630 #endif /* !defined(WITH_STATIC_CLASSPATH) */
633 /* native_library_find *********************************************************
635 Find an entry in the native library hashtable.
637 *******************************************************************************/
639 #if !defined(WITH_STATIC_CLASSPATH)
640 hashtable_library_name_entry *native_library_find(utf *filename,
641 java_objectheader *loader)
643 hashtable_classloader_entry *cle;
644 hashtable_library_loader_entry *le;
645 hashtable_library_name_entry *ne; /* library name */
646 u4 key; /* hashkey */
647 u4 slot; /* slot in hashtable */
649 /* search loader in the classloader hashtable */
651 cle = loader_hashtable_classloader_find(loader);
656 /* normally addresses are aligned to 4, 8 or 16 bytes */
658 key = ((u4) (ptrint) cle) >> 4; /* align to 16-byte boundaries */
659 slot = key & (hashtable_library->size - 1);
660 le = hashtable_library->ptr[slot];
662 /* search external hash chain for the entry */
668 le = le->hashlink; /* next element in external chain */
671 /* no loader found? return NULL */
676 /* search for library name */
681 if (ne->name == filename)
684 ne = ne->hashlink; /* next element in external chain */
687 /* return entry, if no entry was found, ne is NULL */
691 #endif /* !defined(WITH_STATIC_CLASSPATH) */
694 /* native_findfunction *********************************************************
696 Looks up a method (must have the same class name, method name,
697 descriptor and 'static'ness) and returns a function pointer to it.
698 Returns: function pointer or NULL (if there is no such method)
700 Remark: For faster operation, the names/descriptors are converted
701 from C strings to Unicode the first time this function is called.
703 *******************************************************************************/
705 #if defined(WITH_STATIC_CLASSPATH)
706 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
709 /* entry of table for fast string comparison */
710 struct nativecompref *n;
713 isstatic = isstatic ? true : false;
715 if (!nativecompdone) {
716 for (i = 0; i < NATIVETABLESIZE; i++) {
717 nativecomptable[i].classname =
718 utf_new_char(nativetable[i].classname);
720 nativecomptable[i].methodname =
721 utf_new_char(nativetable[i].methodname);
723 nativecomptable[i].descriptor =
724 utf_new_char(nativetable[i].descriptor);
726 nativecomptable[i].isstatic = nativetable[i].isstatic;
727 nativecomptable[i].func = nativetable[i].func;
730 nativecompdone = true;
733 for (i = 0; i < NATIVETABLESIZE; i++) {
734 n = &(nativecomptable[i]);
736 if (cname == n->classname && mname == n->methodname &&
737 desc == n->descriptor && isstatic == n->isstatic)
741 /* no function was found, throw exception */
744 new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
749 #endif /* defined(WITH_STATIC_CLASSPATH) */
752 /* native_resolve_function *****************************************************
754 Resolves a native function, maybe from a dynamic library.
756 *******************************************************************************/
758 functionptr native_resolve_function(methodinfo *m)
763 hashtable_library_loader_entry *le;
764 hashtable_library_name_entry *ne;
765 u4 key; /* hashkey */
766 u4 slot; /* slot in hashtable */
770 if (opt_verbosejni) {
771 printf("[Dynamic-linking native method ");
772 utf_display_printable_ascii_classname(m->class->name);
774 utf_display_printable_ascii(m->name);
778 /* generate method symbol string */
780 name = native_method_symbol(m->class->name, m->name);
782 /* generate overloaded function (having the types in it's name) */
784 newname = native_make_overloaded_function(name, m->descriptor);
786 /* check the library hash entries of the classloader of the
791 /* normally addresses are aligned to 4, 8 or 16 bytes */
793 key = ((u4) (ptrint) m->class->classloader) >> 4; /* align to 16-byte */
794 slot = key & (hashtable_library->size - 1);
795 le = hashtable_library->ptr[slot];
797 /* iterate through loaders in this hash slot */
799 while ((le != NULL) && (f == NULL)) {
800 /* iterate through names in this loader */
804 while ((ne != NULL) && (f == NULL)) {
805 f = (functionptr) (ptrint) lt_dlsym(ne->handle, name->text);
808 f = (functionptr) (ptrint) lt_dlsym(ne->handle, newname->text);
820 /* If not found, try to find the native function symbol in the
824 f = native_method_find(m);
828 printf("internal ]\n");
831 #if defined(ENABLE_JVMTI)
832 /* fire Native Method Bind event */
833 if (jvmti) jvmti_NativeMethodBind(m, f, &f);
836 /* no symbol found? throw exception */
840 printf("failed ]\n");
842 exceptions_throw_unsatisfiedlinkerror(m->name);
847 #endif /* !defined(WITH_STATIC_CLASSPATH) */
850 /* native_new_and_init *********************************************************
852 Creates a new object on the heap and calls the initializer.
853 Returns the object pointer or NULL if memory is exhausted.
855 *******************************************************************************/
857 java_objectheader *native_new_and_init(classinfo *c)
860 java_objectheader *o;
863 vm_abort("native_new_and_init: c == NULL");
872 /* try to find the initializer */
874 m = class_findmethod(c, utf_init, utf_void__void);
876 /* ATTENTION: returning the object here is ok, since the class may
877 not have an initializer */
882 /* call initializer */
884 (void) vm_call_method(m, o);
890 java_objectheader *native_new_and_init_string(classinfo *c, java_objectheader *s)
893 java_objectheader *o;
896 vm_abort("native_new_and_init_string: c == NULL");
905 /* find initializer */
907 m = class_resolveclassmethod(c,
909 utf_java_lang_String__void,
913 /* initializer not found */
918 /* call initializer */
920 (void) vm_call_method(m, o, s);
926 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
929 java_objectheader *o;
932 vm_abort("native_new_and_init_int: c == NULL");
941 /* find initializer */
943 m = class_resolveclassmethod(c, utf_init, utf_int__void, NULL, true);
945 /* initializer not found */
950 /* call initializer */
952 (void) vm_call_method(m, o, i);
958 java_objectheader *native_new_and_init_throwable(classinfo *c, java_objectheader *t)
960 java_objectheader *o;
964 vm_abort("native_new_and_init_throwable: c == NULL");
973 /* find initializer */
975 m = class_findmethod(c, utf_init, utf_java_lang_Throwable__void);
977 /* initializer not found */
982 /* call initializer */
984 (void) vm_call_method(m, o, t);
991 * These are local overrides for various environment variables in Emacs.
992 * Please do not remove this and leave it at the end of the file, where
993 * Emacs will automagically detect them.
994 * ---------------------------------------------------------------------
997 * indent-tabs-mode: t