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 7941 2007-05-23 11:59:51Z twisti $
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_library_loader_entry *le;
556 hashtable_library_name_entry *ne; /* library name */
557 u4 key; /* hashkey */
558 u4 slot; /* slot in hashtable */
560 LOCK_MONITOR_ENTER(hashtable_library->header);
562 /* normally addresses are aligned to 4, 8 or 16 bytes */
564 key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
565 slot = key & (hashtable_library->size - 1);
566 le = hashtable_library->ptr[slot];
568 /* search external hash chain for the entry */
571 if (le->loader == loader)
574 le = le->hashlink; /* next element in external chain */
577 /* no loader found? create a new entry */
580 le = NEW(hashtable_library_loader_entry);
585 /* insert entry into hashtable */
588 (hashtable_library_loader_entry *) hashtable_library->ptr[slot];
589 hashtable_library->ptr[slot] = le;
591 /* update number of hashtable-entries */
593 hashtable_library->entries++;
597 /* search for library name */
602 if (ne->name == filename) {
603 LOCK_MONITOR_EXIT(hashtable_library->header);
608 ne = ne->hashlink; /* next element in external chain */
611 /* not found? add the library name to the classloader */
613 ne = NEW(hashtable_library_name_entry);
618 /* insert entry into external chain */
620 ne->hashlink = le->namelink;
623 LOCK_MONITOR_EXIT(hashtable_library->header);
625 #endif /* !defined(WITH_STATIC_CLASSPATH) */
628 /* native_library_find *********************************************************
630 Find an entry in the native library hashtable.
632 *******************************************************************************/
634 #if !defined(WITH_STATIC_CLASSPATH)
635 hashtable_library_name_entry *native_library_find(utf *filename,
636 java_objectheader *loader)
638 hashtable_library_loader_entry *le;
639 hashtable_library_name_entry *ne; /* library name */
640 u4 key; /* hashkey */
641 u4 slot; /* slot in hashtable */
643 /* normally addresses are aligned to 4, 8 or 16 bytes */
645 key = ((u4) (ptrint) loader) >> 4; /* align to 16-byte boundaries */
646 slot = key & (hashtable_library->size - 1);
647 le = hashtable_library->ptr[slot];
649 /* search external hash chain for the entry */
652 if (le->loader == loader)
655 le = le->hashlink; /* next element in external chain */
658 /* no loader found? return NULL */
663 /* search for library name */
668 if (ne->name == filename)
671 ne = ne->hashlink; /* next element in external chain */
674 /* return entry, if no entry was found, ne is NULL */
678 #endif /* !defined(WITH_STATIC_CLASSPATH) */
681 /* native_findfunction *********************************************************
683 Looks up a method (must have the same class name, method name,
684 descriptor and 'static'ness) and returns a function pointer to it.
685 Returns: function pointer or NULL (if there is no such method)
687 Remark: For faster operation, the names/descriptors are converted
688 from C strings to Unicode the first time this function is called.
690 *******************************************************************************/
692 #if defined(WITH_STATIC_CLASSPATH)
693 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
696 /* entry of table for fast string comparison */
697 struct nativecompref *n;
700 isstatic = isstatic ? true : false;
702 if (!nativecompdone) {
703 for (i = 0; i < NATIVETABLESIZE; i++) {
704 nativecomptable[i].classname =
705 utf_new_char(nativetable[i].classname);
707 nativecomptable[i].methodname =
708 utf_new_char(nativetable[i].methodname);
710 nativecomptable[i].descriptor =
711 utf_new_char(nativetable[i].descriptor);
713 nativecomptable[i].isstatic = nativetable[i].isstatic;
714 nativecomptable[i].func = nativetable[i].func;
717 nativecompdone = true;
720 for (i = 0; i < NATIVETABLESIZE; i++) {
721 n = &(nativecomptable[i]);
723 if (cname == n->classname && mname == n->methodname &&
724 desc == n->descriptor && isstatic == n->isstatic)
728 /* no function was found, throw exception */
731 new_exception_utfmessage(string_java_lang_UnsatisfiedLinkError,
736 #endif /* defined(WITH_STATIC_CLASSPATH) */
739 /* native_resolve_function *****************************************************
741 Resolves a native function, maybe from a dynamic library.
743 *******************************************************************************/
745 functionptr native_resolve_function(methodinfo *m)
750 hashtable_library_loader_entry *le;
751 hashtable_library_name_entry *ne;
752 u4 key; /* hashkey */
753 u4 slot; /* slot in hashtable */
757 if (opt_verbosejni) {
758 printf("[Dynamic-linking native method ");
759 utf_display_printable_ascii_classname(m->class->name);
761 utf_display_printable_ascii(m->name);
765 /* generate method symbol string */
767 name = native_method_symbol(m->class->name, m->name);
769 /* generate overloaded function (having the types in it's name) */
771 newname = native_make_overloaded_function(name, m->descriptor);
773 /* check the library hash entries of the classloader of the
778 /* normally addresses are aligned to 4, 8 or 16 bytes */
780 key = ((u4) (ptrint) m->class->classloader) >> 4; /* align to 16-byte */
781 slot = key & (hashtable_library->size - 1);
782 le = hashtable_library->ptr[slot];
784 /* iterate through loaders in this hash slot */
786 while ((le != NULL) && (f == NULL)) {
787 /* iterate through names in this loader */
791 while ((ne != NULL) && (f == NULL)) {
792 f = (functionptr) (ptrint) lt_dlsym(ne->handle, name->text);
795 f = (functionptr) (ptrint) lt_dlsym(ne->handle, newname->text);
807 /* If not found, try to find the native function symbol in the
811 f = native_method_find(m);
815 printf("internal ]\n");
818 #if defined(ENABLE_JVMTI)
819 /* fire Native Method Bind event */
820 if (jvmti) jvmti_NativeMethodBind(m, f, &f);
823 /* no symbol found? throw exception */
827 printf("failed ]\n");
829 exceptions_throw_unsatisfiedlinkerror(m->name);
834 #endif /* !defined(WITH_STATIC_CLASSPATH) */
837 /* native_new_and_init *********************************************************
839 Creates a new object on the heap and calls the initializer.
840 Returns the object pointer or NULL if memory is exhausted.
842 *******************************************************************************/
844 java_objectheader *native_new_and_init(classinfo *c)
847 java_objectheader *o;
850 vm_abort("native_new_and_init: c == NULL");
859 /* try to find the initializer */
861 m = class_findmethod(c, utf_init, utf_void__void);
863 /* ATTENTION: returning the object here is ok, since the class may
864 not have an initializer */
869 /* call initializer */
871 (void) vm_call_method(m, o);
877 java_objectheader *native_new_and_init_string(classinfo *c, java_objectheader *s)
880 java_objectheader *o;
883 vm_abort("native_new_and_init_string: c == NULL");
892 /* find initializer */
894 m = class_resolveclassmethod(c,
896 utf_java_lang_String__void,
900 /* initializer not found */
905 /* call initializer */
907 (void) vm_call_method(m, o, s);
913 java_objectheader *native_new_and_init_throwable(classinfo *c, java_objectheader *t)
915 java_objectheader *o;
919 vm_abort("native_new_and_init_throwable: c == NULL");
928 /* find initializer */
930 m = class_findmethod(c, utf_init, utf_java_lang_Throwable__void);
932 /* initializer not found */
937 /* call initializer */
939 (void) vm_call_method(m, o, t);
946 * These are local overrides for various environment variables in Emacs.
947 * Please do not remove this and leave it at the end of the file, where
948 * Emacs will automagically detect them.
949 * ---------------------------------------------------------------------
952 * indent-tabs-mode: t