Commit Graph

309 Commits

Author SHA1 Message Date
Joel Dice
af9288f4ee don't abort when parsing malformed UTF8 strings
Previously, we would abort the process if we encountered a truncated
multibyte character in parseUtf8NonAscii (called by the JNI method
NewStringUTF).  Now we simply terminate the string at that point.
2011-04-07 14:26:54 -06:00
Joel Dice
b0ae6343ad provide proper implementations of JVM_GetDeclaredClasses, JVM_GetDeclaringClass 2011-03-31 19:47:26 -06:00
Joel Dice
ef86530080 call static initializer of superclass before that of class itself
Also, assume any class which has an ancestor class which has a static
initializer needs initialization even if it doesn't have one itself,
per the Java Language Spec.
2011-03-31 19:43:49 -06:00
Joel Dice
1c7abe782d specify valid code source for system classes
This enables use of a class's protection domain to determine what JAR
or directory it came from.
2011-03-31 19:38:44 -06:00
Joel Dice
8d9412c1e8 remove redundant statement in parseUtf8
VM heap allocated memory is already zero'd out, so there's no need to
explicitly end strings with a null character.
2011-03-31 19:10:20 -06:00
Joel Dice
9fe41b2afc only return declared interfaces from Class.getInterfaces
The result of Class.getInterfaces should not include interfaces
declared to be implemented/extended by superclasses/superinterfaces,
only those declared by the class itself.  This is important because it
influences how java.io.ObjectStreamClass calculates serial version
IDs.
2011-03-27 20:29:31 -06:00
Joel Dice
ad79bbcbd5 update class loader map when creating new array class
This ensures that we don't create redundant array classes later.
2011-03-26 23:21:37 -06:00
Joel Dice
ba0cc803a6 implement various JVM_* methods
This includes a proper implementation of JVM_ActiveProcessorCount, as
well as JVM_SetLength and JVM_NewMultiArray.  Also, we now accept up
to JNI_VERSION_1_6 in JVM_IsSupportedJNIVersion.
2011-03-26 11:15:52 -06:00
Joel Dice
838cf9fdd1 avoid calling doCollect recursively
We must not allocate heap objects from doCollect, since it might
trigger a GC while one is already in progress, which can cause trouble
when we're still queuing up objects to finalize, among other things.
To avoid this, I've added extra fields to the finalizer and cleaner
types which we can use to link instances up during GC without
allocating new memory.
2011-03-25 19:11:38 -06:00
Joel Dice
61552b6b8a check for and handle instances of sun.misc.Cleaner during GC
OpenJDK uses an alternative to Object.finalize for resource cleanup in
the form of sun.misc.Cleaner.  Normally, OpenJDK's
java.lang.ref.Reference.ReferenceHandler thread handles this, calling
Cleaner.clean on any instances it finds in its "pending" queue.
However, Avian handles reference queuing internally, so it never
actually adds anything to that queue, so the VM must call
Cleaner.clean itself.
2011-03-19 15:10:52 -06:00
Joel Dice
7004c0ddf3 various fixes and additions to increase app compatiblity
The main changes here are:

  * fixes for runtime annotation support

  * proper support for runtime generic type introspection

  * throw NoClassDefFoundErrors instead of ClassNotFoundExceptions
    where appropriate
2011-03-17 21:42:15 -06:00
Joel Dice
366dfc009c fix mode=stress thinko and GC safety issue in machine.cpp 2011-03-17 08:49:41 -06:00
Joel Dice
7152c3fdb3 handle volatile fields properly in JNI Get/Set methods
This commit ensures that we use the proper memory barriers or locking
necessary to preserve volatile semantics for such fields when accessed
or updated via JNI.
2011-03-15 19:34:00 -06:00
Joel Dice
453ceb42ab implement lazy class/field/method resolution in JIT compiler
Unlike the interpreter, the JIT compiler tries to resolve all the
symbols referenced by a method when compiling that method.  However,
this can backfire if a symbol cannot be resolved: we end up throwing
an e.g. NoClassDefFoundError for code which may never be executed.
This is particularly troublesome for code which supports multiple
APIs, choosing one at runtime.

The solution is to defer to stub code for symbols which can't be
resolved at JIT compile time.  Such a stub will try again at runtime
to resolve the needed symbol and throw an appropriate error if it
still can't be found.
2011-03-15 18:07:13 -06:00
Joel Dice
e5ecb5b549 add optional avian.error.log system property
This property may be used to specify a file name to use for printing
stack traces for unhandled exceptions.  The default is stderr.
2011-03-15 17:27:17 -06:00
Joel Dice
8fb9523de5 don't try to print null exception trace in printTrace
It is possible to create an Exception with no stack trace by
overriding Throwable.fillInStackTrace, so we can't assume any given
instance will have one.
2011-03-04 15:58:10 -07:00
Joel Dice
25f1a9f1e8 fix Thread::exit/Thread::dispose race condition
There was a race between these two functions such that one thread A
would run dispose on thread B just before thread B finishes exit, with
the result that Thread::lock and/or Thread::systemThread would be
disposed twice, resulting in a crash.
2011-02-28 10:14:01 -07:00
Joel Dice
468edb97d2 work around GCC name mangling bug
It seems that older versions of GCC (4.0 and older, at least) generate
assembly files with duplicate symbols for function templates which
differ only by the attributes of the templated types.  Newer versions
have no such problem, but we need to support both, hence the
workaround in this commit of using a dedicated, non-template "alias"
function where we previously used "cast<alias_t>".
2011-02-14 11:47:59 -07:00
Joel Dice
8d50d0fd76 fix aliasing bug in util.cpp
We use a template function called "cast" to get raw access to fields
in in the VM.  In particular, we use this function in util.cpp to
treat reference fields as intptr_t fields so we can use the least
significant bit as the red/black flag in red/black tree nodes.
Unfortunately, this runs afoul of the type aliasing rules in C/C++,
and the compiler is permitted to optimize in a way that assumes such
aliasing cannot occur.  Such optimization caused all the nodes in the
tree to be black, leading to extremely unbalanced trees and thus slow
performance.

The fix in this case is to use the __may_alias__ attribute to tell the
compiler we're doing something devious.  I've also used this technique
to avoid other potential aliasing problems.  There may be others
lurking, so a complete audit of the VM might be a good idea.
2011-02-11 21:57:27 -07:00
Joel Dice
cb7dc1aeef fix various regressions due to 0.5 work 2011-02-11 21:13:11 -07:00
Joel Dice
51a1081adc remove unused Heap::Client::outOfMemory method
The heap-dump-on-OOM feature has been moved to the collect function.
2011-02-02 08:46:20 -07:00
Joel Dice
fff51bad06 more progress on PowerPC build
Also, hide frame mapping for stack unwinding (which is still
incomplete) in x86.cpp, since no other platform needs it.
2011-01-30 14:14:57 -07:00
Joel Dice
b47dfdf5bd remove debug logging 2011-01-27 21:05:22 -07:00
Joel Dice
b7157c802a fix continuations=true build 2011-01-27 11:54:41 -07:00
Joel Dice
e16d5f83af Merge remote branch 'origin/master' into r0.5 2011-01-25 17:30:21 -07:00
Joel Dice
c855224d14 fix VM abort when ClassLoader.defineClass is used in bootimage build
When loading a class which extends another class that contained a
field of primitive array type using defineClass in a bootimage=true
build, the VM was unable to find the primitive array class, and
makeArrayClass refused to create one since it should already have
existed.

The problem was that the bootimage=true build uses an empty
Machine::BootstrapClassMap, and resolveArrayClass expected to find the
primitive array classes there.  The fix is to check the
Machine::BootLoader map if we can't find it in
Machine::BootstrapClassMap.
2011-01-17 09:36:03 -07:00
Joel Dice
43cbfd3f3a support stack unwinding without using a frame pointer
Previously, we unwound the stack by following the chain of frame
pointers for normal returns, stack trace creation, and exception
unwinding.  On x86, this required reserving EBP/RBP for frame pointer
duties, making it unavailable for general computation and requiring
that it be explicitly saved and restored on entry and exit,
respectively.

On PowerPC, we use an ABI that makes the stack pointer double as a
frame pointer, so it doesn't cost us anything.  We've been using the
same convention on ARM, but it doesn't match the native calling
convention, which makes it unusable when we want to call native code
from Java and pass arguments on the stack.

So far, the ARM calling convention mismatch hasn't been an issue
because we've never passed more arguments from Java to native code
than would fit in registers.  However, we must now pass an extra
argument (the thread pointer) to e.g. divideLong so it can throw an
exception on divide by zero, which means the last argument must be
passed on the stack.  This will clobber the linkage area we've been
using to hold the frame pointer, so we need to stop using it.

One solution would be to use the same convention on ARM as we do on
x86, but this would introduce the same overhead of making a register
unavailable for general use and extra code at method entry and exit.

Instead, this commit removes the need for a frame pointer.  Unwinding
involves consulting a map of instruction offsets to frame sizes which
is generated at compile time.  This is necessary because stack trace
creation can happen at any time due to Thread.getStackTrace being
called by another thread, and the frame size varies during the
execution of a method.

So far, only x86(_64) is working, and continuations and tail call
optimization are probably broken.  More to come.
2011-01-16 19:05:05 -07:00
Joel Dice
afabe8e07e rework VM exception handling; throw OOMEs when appropriate
This rather large commit modifies the VM to use non-local returns to
throw exceptions instead of simply setting Thread::exception and
returning frame-by-frame as it used to.  This has several benefits:

 * Functions no longer need to check Thread::exception after each call
   which might throw an exception (which would be especially tedious
   and error-prone now that any function which allocates objects
   directly or indirectly might throw an OutOfMemoryError)

 * There's no need to audit the code for calls to functions which
   previously did not throw exceptions but later do

 * Performance should be improved slightly due to both the reduced
   need for conditionals and because undwinding now occurs in a single
   jump instead of a series of returns

The main disadvantages are:

 * Slightly higher overhead for entering and leaving the VM via the
   JNI and JDK methods

 * Non-local returns can make the code harder to read

 * We must be careful to register destructors for stack-allocated
   resources with the Thread so they can be called prior to a
   non-local return

The non-local return implementation is similar to setjmp/longjmp,
except it uses continuation-passing style to avoid the need for
cooperation from the C/C++ compiler.  Native C++ exceptions would have
also been an option, but that would introduce a dependence on
libstdc++, which we're trying to avoid for portability reasons.

Finally, this commit ensures that the VM throws an OutOfMemoryError
instead of aborting when it reaches its memory ceiling.  Currently, we
treat the ceiling as a soft limit and temporarily exceed it as
necessary to allow garbage collection and certain internal allocations
to succeed, but refuse to allocate any Java objects until the heap
size drops back below the ceiling.
2010-12-27 15:55:23 -07:00
Joel Dice
2e86f0ac57 fix race condition leading to deadlock on exit
There is a delay between when we tell the OS to start a thread and
when it actually starts, and during that time a thread might
mistakenly think it was the last to exit, try to shut down the VM, and
then block in joinAll when it finds it wasn't the last one after all.

The solution is to increment Machine::liveCount and add the new thread
to the process tree before starting it -- all while holding
Machine::stateLock for atomicity.  This helps guarantee that when
liveCount is one, we can be sure there's really only one thread
running or staged to run.
2010-12-20 19:00:23 -07:00
Joel Dice
5d5a18c482 set Thread::exception to null before creating ExceptionInInitializerError
If we don't do this, the VM will crash when it tries to create a stack
trace for the error because makeObjectArray will return null
immediately when it sees there is a pending exception.
2010-12-20 16:49:45 -07:00
Joel Dice
651ad20fc3 fix GC safety bugs 2010-12-09 22:17:57 -07:00
Joel Dice
3d49173b0b avoid inifinite recursion if java.lang.Object is missing; refactoring
When trying to create an array class, we try to resolve
java.lang.Object so we can use its vtable in the array class.
However, if Object is missing, we'll try to create and throw a
ClassNotFoundException, which requires creating an array to store the
stack trace, which requires creating an array class, which requires
resolving Object, etc..  This commit short-circuits this process by
telling resolveClass not to create and throw an exception if it can't
find Object.

While doing the above work, I noticed that the implementations of
Classpath::makeThrowable in classpath-avian.cpp and
classpath-openjdk.cpp were identical, so I made makeThrowable a
top-level function.

Finally, I discovered that Thread.setDaemon can only be called before
the target thread has been started, which allowed me to simplify the
code to track daemon threads in the VM.
2010-12-09 19:38:12 -07:00
Joel Dice
a5742f5985 update copyright years 2010-12-05 20:21:09 -07:00
Joel Dice
84520cde51 Merge remote branch 'origin/master' into openjdk 2010-12-01 14:40:58 -07:00
Joel Dice
4b1b3e032b add new array classes to classloader map upon creation
We were already doing this for non-array classes; we need to do it for
all classes or else risk creating duplicates.
2010-11-30 20:27:36 -07:00
Joel Dice
70a36c05b9 fix time==0 case in Unsafe.park and implement JVM_DumpThreads
Also, set name field on system threads for OpenJDK build to avoid NPEs
in Thread.getName.
2010-11-27 16:27:30 -07:00
Joel Dice
adc5c95214 fix GC safety bug in resolveClass 2010-11-27 14:44:49 -07:00
Joel Dice
e68dfe1e89 various fixes to get Eclipse 3.6 working
* add libnet.so and libnio.so to built-in libraries for openjdk-src build

 * implement sun.misc.Unsafe.park/unpark

 * implement JVM_SetClassSigners/JVM_GetClassSigners

 * etc.
2010-11-27 11:25:02 -07:00
Joel Dice
459f4d5194 fix openjdk-src bootimage build
The main change here is to use a lazily-populated vector to associate
runtime data with classes instead of referencing them directly from
the class which requires updating immutable references in the heap
image.  The other changes employ other strategies to avoid trying to
update immutable references.
2010-11-26 12:41:31 -07:00
Joel Dice
19dbc61e9f for heapdump=true builds, optionally generate dump on OOM
If the VM runs out of heap space and the "avian.heap.dump" system
property was specified at startup, the VM will write a heap dump to
the filename indicated by that property.  This dump may be analyzed
using e.g. DumpStats.java.
2010-11-18 10:55:00 -07:00
Joel Dice
aea02e545f fix race condition in interrupting/joining threads as they are exiting
My recent commit to ensure that OS resources are released immediately
upon thread exit introduced a race condition where interrupting or
joining a thread as it exited could lead to attempts to use
already-released resources.  This commit adds locking to avoid the
race.
2010-11-16 10:50:19 -07:00
Joel Dice
a611ccda6f Merge remote branch 'origin/master' into openjdk
Conflicts:
	makefile
	src/compile.cpp
	src/compiler.cpp
	src/type-generator.cpp
2010-11-16 10:18:08 -07:00
Joel Dice
64601e6f3e name VM-internal classes for heapdump=true builds
This makes heap dumps more useful since these classes are now refered
to by name instead of number.

This commit also adds a couple of utilities for parsing heap dumps:
PrintDump and DumpStats.
2010-11-16 09:31:49 -07:00
Joel Dice
f64d6c857b clean up OS resources immediately upon thread exit
Previously, we waited until the next GC to do this, but that can be
too long for workloads which create a lot of short-lived threads but
don't do much allocation.
2010-11-09 15:46:16 -07:00
Joel Dice
d0a6096eb0 add support for accessing embedded JARs as if they were directories
This allows OpenJDK to access time zone data which is normally found
under java.home, but which we must embed in the executable itself to
create a self-contained build.  The VM intercepts various file
operations, looking for paths which start with a prefix specified by
the avian.embed.prefix property and redirecting those operations to an
embedded JAR.

For example, if avian.embed.prefix is "/avian-embedded", and code
calls File.exists() with a path of
"/avian-embedded/javahomeJar/foo.txt", the VM looks for a function
named javahomeJar via dlsym, calls the function to find the memory
region containing the embeded JAR, and finally consults the JAR to see
if the file "foo.txt" exists.
2010-11-05 13:18:28 -06:00
Joel Dice
cabad6926f enable standalone OpenJDK builds
As described in readme.txt, a standalone OpenJDK build embeds all
libraries, classes, and other files needed at runtime in the resulting
binary, eliminating dependencies on external resources.
2010-11-04 11:02:09 -06:00
Joel Dice
25ca40931d keep track of original class in resolveField so we can throw an exception with a useful message 2010-10-24 11:49:59 -06:00
Joel Dice
1d4b54a9f6 fix failure to add abstract virtuals to class with no declared virtuals 2010-09-25 16:35:18 -06:00
Joel Dice
268d2de175 cache array class lookups in element class; misc. bugfixes 2010-09-25 15:54:01 -06:00
Joel Dice
89f6adc93c fix various classloading deadlocks and races 2010-09-22 13:58:46 -06:00