From a77693fb29ad607690820767615e19b7eaea8870 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Sat, 7 Jul 2007 19:06:32 -0600 Subject: [PATCH] snapshot; known bug: finalizers and weak references don't work correctly wrt tenured objects --- classpath/java/lang/LinkageError.java | 11 +++ classpath/java/lang/UnsatisfiedLinkError.java | 11 +++ src/builtin.cpp | 5 +- src/jnienv.cpp | 7 +- src/machine.cpp | 19 +++++ src/machine.h | 3 + src/run.cpp | 85 ++++++++++++------- src/types.def | 7 +- 8 files changed, 106 insertions(+), 42 deletions(-) create mode 100644 classpath/java/lang/LinkageError.java create mode 100644 classpath/java/lang/UnsatisfiedLinkError.java diff --git a/classpath/java/lang/LinkageError.java b/classpath/java/lang/LinkageError.java new file mode 100644 index 0000000000..c7716b2b28 --- /dev/null +++ b/classpath/java/lang/LinkageError.java @@ -0,0 +1,11 @@ +package java.lang; + +public class LinkageError extends Error { + public LinkageError(String message) { + super(message, null); + } + + public LinkageError() { + this(null); + } +} diff --git a/classpath/java/lang/UnsatisfiedLinkError.java b/classpath/java/lang/UnsatisfiedLinkError.java new file mode 100644 index 0000000000..840a2f9995 --- /dev/null +++ b/classpath/java/lang/UnsatisfiedLinkError.java @@ -0,0 +1,11 @@ +package java.lang; + +public class UnsatisfiedLinkError extends LinkageError { + public UnsatisfiedLinkError(String message) { + super(message); + } + + public UnsatisfiedLinkError() { + this(null); + } +} diff --git a/src/builtin.cpp b/src/builtin.cpp index 51d627f7d6..c15e4329a8 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -40,10 +40,7 @@ loadLibrary(Thread* t, jstring nameString) if (LIKELY(nameString)) { object n = *nameString; char name[stringLength(t, n) + 1]; - memcpy(name, - &byteArrayBody(t, stringBytes(t, n), stringOffset(t, n)), - stringLength(t, n)); - name[stringLength(t, n)] = 0; + stringChars(t, n, name); System::Library* lib; if (LIKELY(t->vm->system->success diff --git a/src/jnienv.cpp b/src/jnienv.cpp index 3962ecc4dc..f6d22189a3 100644 --- a/src/jnienv.cpp +++ b/src/jnienv.cpp @@ -19,12 +19,7 @@ GetStringUTFChars(Thread* t, jstring s, jboolean* isCopy) char* chars = static_cast (t->vm->system->allocate(stringLength(t, *s) + 1)); - - memcpy(chars, - &byteArrayBody(t, stringBytes(t, *s), stringOffset(t, *s)), - stringLength(t, *s)); - - chars[stringLength(t, *s)] = 0; + stringChars(t, *s, chars); if (isCopy) *isCopy = true; return chars; diff --git a/src/machine.cpp b/src/machine.cpp index 7990b625c9..e2a6988a75 100644 --- a/src/machine.cpp +++ b/src/machine.cpp @@ -331,6 +331,7 @@ collect(Thread* t, Heap::CollectionType type) void removeMonitor(Thread* t, object o) { + abort(t); hashMapRemove(t, t->vm->monitorMap, o, objectHash, referenceEqual); } @@ -670,6 +671,24 @@ makeString(Thread* t, const char* format, ...) return makeString(t, s, 0, byteArrayLength(t, s), 0); } +void +stringChars(Thread* t, object string, char* chars) +{ + object data = stringData(t, string); + if (objectClass(t, data) + == arrayBody(t, t->vm->types, Machine::ByteArrayType)) + { + memcpy(chars, + &byteArrayBody(t, data, stringOffset(t, string)), + stringLength(t, string)); + } else { + for (int i = 0; i < stringLength(t, string); ++i) { + chars[i] = charArrayBody(t, data, stringOffset(t, string) + i); + } + } + chars[stringLength(t, string)] = 0; +} + object hashMapFindNode(Thread* t, object map, object key, uint32_t (*hash)(Thread*, object), diff --git a/src/machine.h b/src/machine.h index 20ef67e945..93ecc743ff 100644 --- a/src/machine.h +++ b/src/machine.h @@ -1397,6 +1397,9 @@ makeByteArray(Thread* t, const char* format, ...); object makeString(Thread* t, const char* format, ...); +void +stringChars(Thread* t, object string, char* chars); + inline void pushObject(Thread* t, object o) { diff --git a/src/run.cpp b/src/run.cpp index ad9a35cc4f..b6aa206552 100644 --- a/src/run.cpp +++ b/src/run.cpp @@ -226,6 +226,9 @@ findInterfaceMethod(Thread* t, object method, object o) abort(t); } +object +resolveClass(Thread*, object); + inline object findMethod(Thread* t, object method, object class_) { @@ -233,12 +236,6 @@ findMethod(Thread* t, object method, object class_) methodOffset(t, method)); } -inline object -findVirtualMethod(Thread* t, object method, object o) -{ - return findMethod(t, method, objectClass(t, o)); -} - bool isSuperclass(Thread* t, object class_, object base) { @@ -451,9 +448,6 @@ addInterfaces(Thread* t, object class_, object map) } } -object -resolveClass(Thread*, object); - void parseInterfaceTable(Thread* t, Stream& s, object class_, object pool) { @@ -1204,7 +1198,10 @@ resolveClass(Thread* t, object spec) ClassFinder::Data* data = t->vm->classFinder->find (reinterpret_cast(&byteArrayBody(t, spec, 0))); - if (data) { + if (byteArrayBody(t, spec, 0) == '[') { + class_ = hashMapFind + (t, t->vm->bootstrapClassMap, spec, byteArrayHash, byteArrayEqual); + } else if (data) { if (Verbose) { fprintf(stderr, "parsing %s\n", &byteArrayBody (t, spec, 0)); @@ -1232,18 +1229,13 @@ resolveClass(Thread* t, object spec) } hashMapInsert(t, t->vm->classMap, spec, class_, byteArrayHash); - } else { - class_ = hashMapFind - (t, t->vm->bootstrapClassMap, spec, byteArrayHash, byteArrayEqual); - - if (class_ == 0) { - object message = makeString(t, "%s", &byteArrayBody(t, spec, 0)); - t->exception = makeClassNotFoundException(t, message); - } } if (class_) { hashMapInsert(t, t->vm->classMap, spec, class_, byteArrayHash); + } else { + object message = makeString(t, "%s", &byteArrayBody(t, spec, 0)); + t->exception = makeClassNotFoundException(t, message); } } @@ -2383,8 +2375,24 @@ run(Thread* t) if (LIKELY(peekObject(t, sp - parameterFootprint))) { object class_ = methodClass(t, frameMethod(t, frame)); if (isSpecialMethod(t, method, class_)) { - code = findMethod(t, method, classSuper(t, class_)); - if (UNLIKELY(exception)) goto throw_; + class_ = classSuper(t, class_); + + if (UNLIKELY(classVirtualTable(t, class_) == 0)) { + PROTECT(t, class_); + + resolveClass(t, className(t, class_)); + if (UNLIKELY(exception)) goto throw_; + + object clinit = classInitializer(t, class_); + if (clinit) { + set(t, classInitializer(t, methodClass(t, method)), 0); + code = clinit; + ip -= 3; + goto invoke; + } + } + + code = findMethod(t, method, class_); } else { code = method; } @@ -2425,10 +2433,24 @@ run(Thread* t) unsigned parameterFootprint = methodParameterFootprint(t, method); if (LIKELY(peekObject(t, sp - parameterFootprint))) { - code = findVirtualMethod - (t, method, peekObject(t, sp - parameterFootprint)); - if (UNLIKELY(exception)) goto throw_; - + object class_ = objectClass(t, peekObject(t, sp - parameterFootprint)); + + if (UNLIKELY(classVirtualTable(t, class_) == 0)) { + PROTECT(t, class_); + + resolveClass(t, className(t, class_)); + if (UNLIKELY(exception)) goto throw_; + + object clinit = classInitializer(t, class_); + if (clinit) { + set(t, classInitializer(t, methodClass(t, method)), 0); + code = clinit; + ip -= 3; + goto invoke; + } + } + + code = findMethod(t, method, class_); goto invoke; } else { exception = makeNullPointerException(t); @@ -2635,6 +2657,8 @@ run(Thread* t) == arrayBody(t, t->vm->types, Machine::FloatType)) { pushInt(t, floatValue(t, v)); + } else { + abort(t); } } goto loop; @@ -2650,6 +2674,8 @@ run(Thread* t) == arrayBody(t, t->vm->types, Machine::DoubleType)) { pushLong(t, doubleValue(t, v)); + } else { + abort(t); } } goto loop; @@ -3139,6 +3165,8 @@ run(Thread* t) } goto loop; throw_: + fprintf(stderr, "throw\n"); + pokeInt(t, t->frame + FrameIpOffset, t->ip); for (; frame >= 0; frame = frameNext(t, frame)) { if (methodFlags(t, frameMethod(t, frame)) & ACC_NATIVE) { return 0; @@ -3149,8 +3177,8 @@ run(Thread* t) if (eht) { for (unsigned i = 0; i < exceptionHandlerTableLength(t, eht); ++i) { ExceptionHandler* eh = exceptionHandlerTableBody(t, eht, i); - if (frameIp(t, frame) >= exceptionHandlerStart(eh) - and frameIp(t, frame) >= exceptionHandlerEnd(eh)) + if (frameIp(t, frame) - 1 >= exceptionHandlerStart(eh) + and frameIp(t, frame) - 1 < exceptionHandlerEnd(eh)) { object catchType = 0; if (exceptionHandlerCatchType(eh)) { @@ -3187,10 +3215,7 @@ run(Thread* t) if (throwableMessage(t, exception)) { object m = throwableMessage(t, exception); char message[stringLength(t, m) + 1]; - memcpy(message, - &byteArrayBody(t, stringBytes(t, m), stringOffset(t, m)), - stringLength(t, m)); - message[stringLength(t, m)] = 0; + stringChars(t, m, message); fprintf(stderr, ": %s\n", message); } else { fprintf(stderr, "\n"); diff --git a/src/types.def b/src/types.def index 4d23db57c1..057cd26672 100644 --- a/src/types.def +++ b/src/types.def @@ -110,7 +110,7 @@ (type string java/lang/String (extends jobject) - (object bytes) + (object data) (int32_t offset) (int32_t length) (int32_t hash)) @@ -169,9 +169,12 @@ (type noSuchMethodError java/lang/NoSuchMethodError (extends error)) -(type unsatisfiedLinkError java/lang/UnsatisifiedLinkError +(type linkageError java/lang/LinkageError (extends error)) +(type unsatisfiedLinkError java/lang/UnsatisifiedLinkError + (extends linkageError)) + (type byte java/lang/Byte (extends jobject) (int8_t value))