1 /*
2 * @(#)classloader.c 1.37 02/09/27
3 *
4 * Copyright 1995-1999 by Sun Microsystems, Inc.,
5 * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
6 * All rights reserved.
7 *
8 * This software is the confidential and proprietary information
9 * of Sun Microsystems, Inc. ("Confidential Information"). You
10 * shall not disclose such Confidential Information and shall use
11 * it only in accordance with the terms of the license agreement
12 * you entered into with Sun.
13 * Use is subject to license terms.
14 */
16 /*=========================================================================
17 * SYSTEM: Verifier
18 * SUBSYSTEM: class loader.
19 * FILE: classloader.c
20 * OVERVIEW: Routines for loading and resolving class definitions.
21 * These routines should not be depending upon the interpreter
22 * or the garbage collector.
23 * AUTHOR: Sheng Liang, Sun Microsystems, Inc.
24 * Modifications for JAR support and comments,
25 * Tasneem Sayeed, Sun Microsystems
26 *=======================================================================*/
28 /*=========================================================================
29 * Include files
30 *=======================================================================*/
32 #include <string.h>
33 #include <stdio.h>
34 #include <sys/types.h>
35 #include <stddef.h>
36 #include <fcntl.h>
37 #include <sys/stat.h>
38 #include <stdlib.h>
39 #include <setjmp.h>
50 #ifdef UNIX
51 #include <unistd.h>
52 #endif
55 /*=========================================================================
56 * Globals and extern declarations
57 *=======================================================================*/
74 /*=========================================================================
75 * FUNCTION: AddBinClass
76 * OVERVIEW: Used by createInternalClass1() and createFakeArrayClass()
77 * to add a class in the class table.
78 * INTERFACE:
79 * parameters: ClassClass: cb
80 *
81 * returns: nothing
82 *=======================================================================*/
83 void
85 {
106 }
107 }
114 }
115 }
121 else
125 }
128 right++;
131 }
133 nbinclasses++;
134 }
136 unlock:
138 }
141 /*=========================================================================
142 * FUNCTION: DelBinClass
143 * OVERVIEW: Intended for allowing deletion of classes from the class
144 * table.
145 *
146 * INTERFACE:
147 * parameters: ClassClass: cb
148 *
149 * returns: nothing
150 *=======================================================================*/
151 void
153 {
158 nbinclasses--;
161 }
163 }
165 }
168 /*=========================================================================
169 * FUNCTION: MakeClassSticky
170 * OVERVIEW: Used to lock certain system classes into memory during
171 * initialization.
172 *
173 * INTERFACE:
174 * parameters: ClassClass: cb
175 *
176 * returns: nothing
177 *=======================================================================*/
178 void
180 {
181 /* monitorEnter(obj_monitor(cb)); */
183 /* monitorExit(obj_monitor(cb)); */
184 }
187 /*=========================================================================
188 * FUNCTION: LoadClassFromFile
189 * OVERVIEW: Loads a .class file normally from disk or a null if it fails.
190 * When the interpreter requests for a file, it is actually
191 * looking for a classblock structure to be created, and the
192 * only way it can get one of those is by loading a compiled
193 * class.
194 * OpenCode() tries to open a .class file first. If it fails
195 * to open the file, it returns a non-zero status. If it
196 * returns a valid file descriptor, this usually means that
197 * this is a valid .class file.
198 * It then invokes createInternalClass() to actually create the
199 * internal representation of the class.
200 *
201 * INTERFACE:
202 * parameters: char*: file name
203 * char*: directory
204 * char*: class name
205 *
206 * returns: nothing
207 *=======================================================================*/
208 ClassClass *
210 {
223 /* Snarf the file into memory. */
232 /* Create the internal class */
239 }
246 failed:
252 }
255 /*=========================================================================
256 * FUNCTION: LoadClassFromZip
257 * TYPE: load class from a JAR or Zip file
258 * OVERVIEW: Called by LoadClassLocally for loading classes from a Zip.
259 *
260 * This function loads a .class file normally from a Zip or
261 * JAR file.
262 * It returns the class when it succeeds or NULL if it fails.
263 *
264 * INTERFACE:
265 * parameters: zip_t: zip file entry
266 * char*: class file name to search for loading
267 * returns: Pointer to ClassClass when it succeeds, or NULL if it fails.
268 *=======================================================================*/
271 {
286 /* Create the internal class */
292 }
295 }
300 }
302 failed:
307 }
309 }
312 /*=========================================================================
313 * FUNCTION: LoadClassLocally
314 * OVERVIEW: Find a class file that is somewhere local, and not from a
315 * class loader.
316 * It still needs to be searched using the classpath.
317 *
318 * INTERFACE:
319 * parameters: char* : class file name
320 * returns: Pointer to ClassClass when it succeeds, or NULL if it fails.
321 *=======================================================================*/
323 {
345 }
350 }
352 // if (JAR_DEBUG && verbose)
353 // jio_fprintf(stderr, "Loading classes from a ZIP file... \n");
356 }
357 }
359 }
361 }
364 /*=========================================================================
365 * FUNCTION: stat_source
366 * OVERVIEW: Unused by the Verifier, but it is too late to remove it now.
367 *
368 * INTERFACE:
369 * parameters: ClassClass *: cb
370 * struct stat*: s
371 * char*: pathbuf
372 * int: maxlen
373 * returns: char *, or NULL if it fails.
374 *=======================================================================*/
377 {
378 #define NAMEBUFLEN 255
383 /* don't bother searching if absolute */
384 /* REMIND: only here for compatibility */
389 }
393 }
394 }
396 /* parse the package name */
400 }
407 }
408 }
410 /* append the source file name */
414 }
417 }
420 /* search the class path */
427 }
430 }
431 }
432 }
434 }
437 /*=========================================================================
438 * Globals and externs for Internal Class representation
439 *=======================================================================*/
444 /* Whatever follows will be 8-byte aligned, if the structure itself is
445 8-byte aligned. */
446 };
466 /* Whatever follows will be 8-byte aligned */
477 };
497 static void
501 bool_t
507 /*=========================================================================
508 * FUNCTION: createInternalClass
509 * OVERVIEW: Invoked by LoadClassFromFile() or LoadClassFromZip() to
510 * create an internal class. See createInternalClass1() for
511 * details.
512 *
513 * INTERFACE:
514 * parameters: unsigned char *: ptr
515 * unsigned char *: end_ptr
516 * ClassClass *: cb
517 * struct Hjava_lang_ClassLoader *: loader
518 * char *: name
519 * char **: detail
520 *
521 * returns: bool_t
522 *=======================================================================*/
523 bool_t
527 {
528 bool_t res;
532 }
535 /*=========================================================================
536 * FUNCTION: JAVA_ERROR
537 * OVERVIEW: Verifier error processing function.
538 *
539 * INTERFACE:
540 * parameters: CICcontext *: context
541 * char * : name
542 *
543 * returns: nothing
544 *=======================================================================*/
551 }
554 /*=========================================================================
555 * FUNCTION: createInternalClass1
556 * OVERVIEW: Auxiliary function for creating an internal class.
557 * Invoked by createInternalClass().
558 *
559 * Creates an internal class file from the indicated data.
560 * It should be in a buffer for which the first byte is at
561 * *ptr and the last byte is just before *end_ptr.
562 * The class's classloader is indicated by the classloader
563 * argument.
564 *
565 * We go through the buffer twice. In the first pass, we
566 * determine the amount of storage needed by the class.
567 * We then allocate a single chunk of memory, free the
568 * temporary storage, and load from the buffer for the second
569 * time.
570 *
571 * Since all storage needed by the class initialization method
572 * <clinit> can be freed after the class is loaded, we count
573 * the <clinit> space needs separately and store the <clinit>
574 * method in a separate chunk of memory.
575 *
576 * INTERFACE:
577 * parameters: unsigned char *: ptr
578 * unsigned char *: end_ptr
579 * ClassClass *: cb
580 * struct Hjava_lang_ClassLoader *: loader
581 * char *: name
582 * char **: detail
583 *
584 * returns: bool_t
585 *=======================================================================*/
586 bool_t
590 {
594 /* Set up the context */
600 /* initialize the remaining fields of the context block */
615 /* We've gotten an error of some sort
616 * See comments below about zeroing these
617 * two fields before freeing the temporary
618 * buffer.
619 */
624 /* Zero out the method so that freeClass will
625 * not try to free the clinit method.
626 */
631 }
633 /* The first pass allows us to uncover any class format
634 * errors and find out the size of the buffer needed.
635 */
640 /* We must set the following two fields to zero before we free
641 * the temporary buffers, because markClassClass may scan a
642 * partially constructed class block in the second pass.
643 * If these two fields are set to zero, markClassClass will
644 * not scan the constant pool and field blocks, which may
645 * point to freed memory.
646 */
651 /* Zero out the method so that freeClass will not try
652 * to free the clinit method.
653 */
666 }
675 }
676 }
681 /* The second pass accomplishes the real task. */
686 /* Valid class - let's put it in the class table. */
690 }
693 /*=========================================================================
694 * FUNCTION: createInternalClass0
695 * OVERVIEW: Auxiliary function invoked by createInternalClass1() during
696 * the second pass to actually load the internal class file
697 * structures such as the constant pool, method blocks, field
698 * blocks and so on.
699 *
700 * INTERFACE:
701 * parameters: CICcontext *: ptr
702 * ClassClass *: cb
703 * struct Hjava_lang_ClassLoader *: loader
704 * char *: name
705 *
706 * returns: nothing
707 *=======================================================================*/
708 static void
711 {
731 /* Ignore version # so that the preverifier can work with JDK 1.4 onwards */
732 /*
733 if (ucb->major_version != JAVA_VERSION)
734 JAVA_ERROR(context, "Bad major version number");
735 */
743 /* Get the name of the class */
747 /* Conversion for Japanese filenames */
756 /* We don't trust a classloader to do the right thing. . . */
759 // if (name == NULL || !IsLegalClassname(name, FALSE)) {
760 // JAVA_ERROR(context, "Bad name");
761 // }
768 }
771 /* There's already a class with the same name and loader */
773 }
775 /* Get the super class name. */
780 // if (!IsLegalClassname(ucb->super_name, FALSE)) {
781 // JAVA_ERROR(context, "Bad superclass name");
782 // }
783 }
791 }
792 }
811 }
813 /* we'll change this below */
817 }
821 }
826 }
830 }
831 }
832 /*
833 if (fb->access & ACC_STATIC) {
834 InitializeStaticVar(fb, context);
835 }
836 */
837 }
871 }
876 }
880 }
881 }
882 }
884 }
886 /* See if there are class attributes */
894 }
908 }
912 }
917 }
927 }
938 /* Make sure that inner_class really belongs to a CLASS */
940 }
942 /* Make sure that outer_class really belongs to a CLASS */
944 }
945 }
948 }
949 }
950 }
953 /*=========================================================================
954 * FUNCTION: createFakeArrayClass
955 * OVERVIEW: Invoked by Locked_FindArrayClassFromClass() for creating
956 * a fake array class that has the specified fields.
957 *
958 * INTERFACE:
959 * parameters: char *: name
960 * int : base type
961 * int : depth (array dimension)
962 * ClassClass * : base type if T_CLASS
963 * struct Hjava_lang_ClassLoader *: class loader
964 *
965 * returns: ClassClass *
966 *=======================================================================*/
967 ClassClass *
973 {
999 /* Initialize the appropriate fields of the constant pool */
1003 /* The class is public iff its base class is public */
1007 /* Set the class field to something innocuous */
1011 }
1014 }
1017 /*=========================================================================
1018 * FUNCTION: createPrimitiveClass
1019 * OVERVIEW: Creates a class block to represent a primitive type.
1020 * NOTE: this is not added to the built-in class table, so
1021 * it should only be called once per primitive type.
1022 * See FindPrimitiveClass().
1023 *
1024 * INTERFACE:
1025 * parameters: char *: name
1026 * char : sig
1027 * unsigned char: typecode
1028 * unsigned char: slotsize
1029 * unsigned char: elementsize
1030 *
1031 * returns: ClassClass *
1032 *=======================================================================*/
1033 ClassClass *
1036 {
1057 }
1060 /*=========================================================================
1061 * FUNCTION: LoadConstantPool
1062 * OVERVIEW: Loads the constant pool given a pointer to the internal
1063 * class file.
1064 *
1065 * INTERFACE:
1066 * parameters: CICcontext *: ptr
1067 *
1068 * returns: nothing
1069 *=======================================================================*/
1071 {
1077 Java8 t1;
1082 }
1087 }
1105 }
1122 }
1132 }
1134 /* We ignore endian problems, and just load the two
1135 * values. The verifier never actually cares what
1136 * the actual value is.
1137 */
1140 i++;
1143 }
1144 /* Indicate that the next object in the constant pool cannot
1145 * be accessed independently.
1146 */
1151 }
1155 }
1156 }
1157 /* It is important to only set these after everything is setup,
1158 so that the GC sees a consistent state.*/
1162 }
1165 /*=========================================================================
1166 * FUNCTION: ReadInCode
1167 * OVERVIEW: Reads the code attributes given a pointer to the internal
1168 * class file and a pointer to the method block structure.
1169 * This includes line number and local variable tables.
1170 *
1171 * INTERFACE:
1172 * parameters: CICcontext *: ptr
1173 * struct methodblock *: mb
1174 *
1175 * returns: nothing
1176 *=======================================================================*/
1178 {
1194 }
1200 }
1215 }
1216 }
1227 }
1228 }
1231 }
1234 /*=========================================================================
1235 * FUNCTION: ReadLineTable
1236 * OVERVIEW: Reads the line number table given a pointer to the internal
1237 * class file and a pointer to the method block structure.
1238 *
1239 * INTERFACE:
1240 * parameters: CICcontext *: ptr
1241 * struct methodblock *: mb
1242 *
1243 * returns: nothing
1244 *=======================================================================*/
1246 {
1258 }
1259 }
1262 }
1266 /*=========================================================================
1267 * FUNCTION: ReadLocalVars
1268 * OVERVIEW: Reads the localvar table given a pointer to the internal
1269 * class file and a pointer to the method block structure.
1270 *
1271 * INTERFACE:
1272 * parameters: CICcontext *: ptr
1273 * struct methodblock *: mb
1274 *
1275 * returns: nothing
1276 *=======================================================================*/
1278 {
1293 }
1294 }
1297 }
1300 /*=========================================================================
1301 * FUNCTION: ReadExceptions
1302 * OVERVIEW: Reads the Exception attribute given a pointer to the internal
1303 * class file and a pointer to the method block structure.
1304 *
1305 * INTERFACE:
1306 * parameters: CICcontext *: ptr
1307 * struct methodblock *: mb
1308 *
1309 * returns: nothing
1310 *=======================================================================*/
1311 static void
1313 {
1324 }
1325 }
1328 }
1331 /*=========================================================================
1332 * FUNCTION: Signature2ArgsSize
1333 * OVERVIEW: Returns the size of arguments given a pointer to a method
1334 * signature.
1335 *
1336 * INTERFACE:
1337 * parameters: char*: method_signature
1338 *
1339 * returns: unsigned
1340 *=======================================================================*/
1342 {
1350 }
1368 /* If an array of classes, skip over class name, too. */
1371 p++;
1372 }
1378 }
1389 }
1390 }
1392 }
1395 /*=========================================================================
1396 * FUNCTION: free_clinit_memory
1397 * OVERVIEW: Frees clinit memory.
1398 *
1399 * INTERFACE:
1400 * parameters: struct methodblock *: mb
1401 *
1402 * returns: nothing
1403 *=======================================================================*/
1405 {
1406 /* This function is somewhat a hack. It fixes the problem in 1.1.3
1407 * and before. sysFree may be called on the wrong memory block if
1408 * the exception attribute comes before the code attribute.
1409 */
1410 /* If there is no exceptions attribute, or if both have already
1411 * been freed.
1412 */
1417 }
1419 }
1421 /* If both attributes exist, free the one at the lower address */
1424 else
1429 }
1432 /*=========================================================================
1433 * FUNCTION: FreeClass
1434 * OVERVIEW: Frees class.
1435 *
1436 * INTERFACE:
1437 * parameters: ClassClass *: cb
1438 *
1439 * returns: nothing
1440 *=======================================================================*/
1442 {
1451 }
1458 /* Interface method tables can be shared between child and
1459 * super classes.
1460 */
1463 }
1466 /*=========================================================================
1467 * FUNCTION: get1byte
1468 * OVERVIEW: Gets one byte from the class file.
1469 *
1470 * INTERFACE:
1471 * parameters: CICcontext *: context
1472 *
1473 * returns: value read or 0 if an error occurred.
1474 *=======================================================================*/
1476 {
1486 }
1487 }
1490 /*=========================================================================
1491 * FUNCTION: get2bytes
1492 * OVERVIEW: Gets two bytes from the class file.
1493 *
1494 * INTERFACE:
1495 * parameters: CICcontext *: context
1496 *
1497 * returns: value read or 0 if an error occurred.
1498 *=======================================================================*/
1500 {
1509 }
1510 }
1513 /*=========================================================================
1514 * FUNCTION: get4bytes
1515 * OVERVIEW: Gets four bytes from the class file.
1516 *
1517 * INTERFACE:
1518 * parameters: CICcontext *: context
1519 *
1520 * returns: value read or 0 if an error occurred.
1521 *=======================================================================*/
1523 {
1533 }
1534 }
1537 /*=========================================================================
1538 * FUNCTION: getNbytes
1539 * OVERVIEW: Gets N bytes from the class file specified by the count
1540 * parameter. If buffer is not null, it will also copy the
1541 * count number of bytes read into the buffer as well.
1542 * Note that this function seems to be always invoked with
1543 * a NULL argument for the buffer, except when it loads the
1544 * UTF8 entry from the constant pool and when the code
1545 * attribute is loaded.
1546 *
1547 * INTERFACE:
1548 * parameters: CICcontext *: context
1549 * int: count
1550 * char *: buffer
1551 *
1552 * returns: nothing
1553 *=======================================================================*/
1555 {
1562 }
1565 /*=========================================================================
1566 * FUNCTION: getAsciz
1567 * OVERVIEW: Reads the next two bytes and uses this value to look up for
1568 * the corresponding constant pool entry.
1569 * Returns null if the value is 0 and zeroOkay flag is set.
1570 *
1571 * INTERFACE:
1572 * parameters: CICcontext *: context
1573 * bool_t : zeroOkay
1574 *
1575 * returns: char * or NULL
1576 *=======================================================================*/
1578 {
1592 }
1595 /*=========================================================================
1596 * FUNCTION: getAscizFromClass
1597 * OVERVIEW: Given the constant pool index, returns the name of the class
1598 * which corresponds to this constant pool entry.
1599 *
1600 * INTERFACE:
1601 * parameters: CICcontext *: context
1602 * int: value
1603 *
1604 * returns: char * or NULL
1605 *=======================================================================*/
1607 {
1618 CONSTANT_POOL_ENTRY_RESOLVED)))
1622 CONSTANT_POOL_ENTRY_RESOLVED)) {
1627 }
1630 }
1632 }
1634 /* In order to avoid possible alignment errors, round up all sizes to
1635 * multiples of eight.
1636 */
1637 #define ROUNDUP_SIZE(s) while ((s) % 8 != 0) (s)++
1640 /*=========================================================================
1641 * FUNCTION: allocNBytes
1642 * OVERVIEW: Memory allocation function for internal class file
1643 * structures.
1644 * It calculates the number of allocations needed for the
1645 * two passes, and the allocations required for the clinit
1646 * method.
1647 *
1648 * INTERFACE:
1649 * parameters: CICcontext *: context
1650 * int : size
1651 *
1652 * returns: void *
1653 *=======================================================================*/
1655 {
1658 /* The first pass
1659 * A more sophisticated scheme could reduce the number of mallocs.
1660 */
1670 else
1674 /* The second pass */
1678 #define ALLOC_BLOCK(ptr,buf,sizelimit) \
1679 result = (ptr); \
1680 (ptr) += (size); \
1681 sysAssert((ptr) <= (buf) + (sizelimit))
1684 /* Make sure that this clinit pointer is not null */
1693 /* Make sure that this malloc pointer is not null */
1699 }
1700 }
1702 }
1705 /*=========================================================================
1706 * FUNCTION: freeBuffers
1707 * OVERVIEW: Frees buffers allocated by allocNBytes().
1708 *
1709 * INTERFACE:
1710 * parameters: CICcontext *: context
1711 *
1712 * returns: nothing
1713 *=======================================================================*/
1715 {
1724 }
1725 }
1729 /* Note: this code is here just for historical reasons. Actually, these
1730 * buffers cannot really be freed here since the data in them is still
1731 * pointed to by the class buffer (cb) and we are not yet done loading
1732 * the class. If the class loading fails, FreeClass() will free the method
1733 * blocks and the clinit memory.
1734 */
1737 /* Reset only if buffer was freed */
1739 }
1743 /* Initialize only if buffer was freed */
1745 }
1746 }
1747 }
1750 /*=========================================================================
1751 * FUNCTION: GetClassConstantClassName
1752 * OVERVIEW: Returns class name corresponding to the constant pool entry
1753 * for the given cpIndex. This is desirable in cases when we
1754 * may simply be interested in the name of the class, but may
1755 * not necessarily want to resolve the class reference if it
1756 * isn't already.
1757 *
1758 * INTERFACE:
1759 * parameters: cp_item_type *: constant pool
1760 * int : index
1761 *
1762 * returns: char *: class name
1763 *=======================================================================*/
1765 {
1772 }
1777 }
1781 }
1782 }