Skip to content

Commit

Permalink
Merge pull request #283 from dicej/findclass-onload
Browse files Browse the repository at this point in the history
fix case of JNIEnv::FindClass called from JNI_OnLoad
  • Loading branch information
joshuawarner32 committed Jul 2, 2014
2 parents 97e2ef8 + 5d3c612 commit 60df08d
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 87 deletions.
19 changes: 5 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,20 +388,19 @@ the following, starting from the Avian directory:
git clone https://android.googlesource.com/platform/external/openssl \
external/openssl
(cd external/openssl && \
git checkout 7b972f1aa23172c4430ada7f3236fa1fd9b31756)
git checkout 1417357d893849c4b6afdd98c32b6ca1b4b19a8b)

git clone https://android.googlesource.com/platform/external/zlib \
external/zlib
(cd external/zlib && \
git checkout 15b6223aa57a347ce113729253802cb2fdeb4ad0)

git clone git://git.openssl.org/openssl.git openssl-upstream
(cd openssl-upstream && \
git checkout OpenSSL_1_0_1e)
(cd openssl-upstream && git checkout OpenSSL_1_0_1h)

git clone https://github.com/dicej/android-libcore64 libcore

curl -Of http://oss.readytalk.com/avian/expat-2.1.0.tar.gz
curl -Of http://oss.readytalk.com/avian-web/expat-2.1.0.tar.gz
(cd external && tar xzf ../expat-2.1.0.tar.gz && mv expat-2.1.0 expat)

(cd external/expat && CFLAGS=-fPIC CXXFLAGS=-fPIC ./configure \
Expand All @@ -417,16 +416,8 @@ NB: use 'CC="gcc -fPIC" ./Configure darwin64-x86_64-cc' when building
for x86_64 OS X instead of 'CC="gcc -fPIC" ./config':

(cd openssl-upstream \
&& (for x in \
progs \
handshake_cutthrough \
jsse \
channelid \
eng_dyn_dirs \
fix_clang_build \
tls12_digests \
alpn; \
do patch -p1 < ../external/openssl/patches/$x.patch; done) \
&& (for x in ../external/openssl/patches/*.patch; \
do patch -p1 < $x; done) \
&& CC="gcc -fPIC" ./config && make)

cd ../avian
Expand Down
5 changes: 4 additions & 1 deletion classpath/java/lang/ClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,8 @@ private Collection<URL> collectResources(String name) {
}
return urls;
}


static native Class getCaller();

static native void load(String name, Class caller, boolean mapName);
}
6 changes: 2 additions & 4 deletions classpath/java/lang/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public static Runtime getRuntime() {

public void load(String path) {
if (path != null) {
load(path, false);
ClassLoader.load(path, ClassLoader.getCaller(), false);
} else {
throw new NullPointerException();
}
}

public void loadLibrary(String path) {
if (path != null) {
load(path, true);
ClassLoader.load(path, ClassLoader.getCaller(), true);
} else {
throw new NullPointerException();
}
Expand Down Expand Up @@ -120,8 +120,6 @@ private static native void exec(String[] command, long[] process)

private static native int waitFor(long pid, long tid);

private static native void load(String name, boolean mapName);

private static native void kill(long pid);

public native void gc();
Expand Down
4 changes: 2 additions & 2 deletions classpath/java/lang/System.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ public static String mapLibraryName(String name) {
private static native String doMapLibraryName(String name);

public static void load(String path) {
Runtime.getRuntime().load(path);
ClassLoader.load(path, ClassLoader.getCaller(), false);
}

public static void loadLibrary(String name) {
Runtime.getRuntime().loadLibrary(name);
ClassLoader.load(name, ClassLoader.getCaller(), true);
}

public static void gc() {
Expand Down
3 changes: 3 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ ifneq ($(android),)
platform-lflags := -lgdi32 -lshlwapi -lwsock32
else
android-cflags += -fPIC -DHAVE_SYS_UIO_H
blacklist = $(luni-native)/java_math_NativeBN.cpp

luni-cpps := $(filter-out $(blacklist),$(luni-cpps))
icu-libs := $(android)/external/icu4c/lib/libicui18n.a \
$(android)/external/icu4c/lib/libicuuc.a \
$(android)/external/icu4c/lib/libicudata.a
Expand Down
27 changes: 27 additions & 0 deletions src/avian/machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,30 @@ class Thread {
SingleProtector protector;
};

class LibraryLoadStack: public AutoResource {
public:
LibraryLoadStack(Thread* t, object classLoader)
: AutoResource(t),
next(t->libraryLoadStack),
classLoader(classLoader),
protector(t, &(this->classLoader))
{
t->libraryLoadStack = this;
}

~LibraryLoadStack() {
t->libraryLoadStack = next;
}

virtual void release() {
this->LibraryLoadStack::~LibraryLoadStack();
}

LibraryLoadStack* next;
object classLoader;
SingleProtector protector;
};

class Checkpoint {
public:
Checkpoint(Thread* t):
Expand Down Expand Up @@ -1516,6 +1540,7 @@ class Thread {
unsigned heapOffset;
Protector* protector;
ClassInitStack* classInitStack;
LibraryLoadStack* libraryLoadStack;
Resource* resource;
Checkpoint* checkpoint;
Runnable runnable;
Expand Down Expand Up @@ -1585,6 +1610,8 @@ class Classpath {
canTailCall(Thread* t, object caller, object calleeClassName,
object calleeMethodName, object calleeMethodSpec) = 0;

virtual object libraryClassLoader(Thread* t, object caller) = 0;

virtual void
shutDown(Thread* t) = 0;

Expand Down
60 changes: 30 additions & 30 deletions src/avian/target-fields.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,42 @@
# if (TARGET_BYTES_PER_WORD == 8)

#define TARGET_THREAD_EXCEPTION 80
#define TARGET_THREAD_EXCEPTIONSTACKADJUSTMENT 2256
#define TARGET_THREAD_EXCEPTIONOFFSET 2264
#define TARGET_THREAD_EXCEPTIONHANDLER 2272
#define TARGET_THREAD_EXCEPTIONSTACKADJUSTMENT 2264
#define TARGET_THREAD_EXCEPTIONOFFSET 2272
#define TARGET_THREAD_EXCEPTIONHANDLER 2280

#define TARGET_THREAD_IP 2216
#define TARGET_THREAD_STACK 2224
#define TARGET_THREAD_NEWSTACK 2232
#define TARGET_THREAD_SCRATCH 2240
#define TARGET_THREAD_CONTINUATION 2248
#define TARGET_THREAD_TAILADDRESS 2280
#define TARGET_THREAD_VIRTUALCALLTARGET 2288
#define TARGET_THREAD_VIRTUALCALLINDEX 2296
#define TARGET_THREAD_HEAPIMAGE 2304
#define TARGET_THREAD_CODEIMAGE 2312
#define TARGET_THREAD_THUNKTABLE 2320
#define TARGET_THREAD_STACKLIMIT 2368
#define TARGET_THREAD_IP 2224
#define TARGET_THREAD_STACK 2232
#define TARGET_THREAD_NEWSTACK 2240
#define TARGET_THREAD_SCRATCH 2248
#define TARGET_THREAD_CONTINUATION 2256
#define TARGET_THREAD_TAILADDRESS 2288
#define TARGET_THREAD_VIRTUALCALLTARGET 2296
#define TARGET_THREAD_VIRTUALCALLINDEX 2304
#define TARGET_THREAD_HEAPIMAGE 2312
#define TARGET_THREAD_CODEIMAGE 2320
#define TARGET_THREAD_THUNKTABLE 2328
#define TARGET_THREAD_STACKLIMIT 2376

# elif (TARGET_BYTES_PER_WORD == 4)

#define TARGET_THREAD_EXCEPTION 44
#define TARGET_THREAD_EXCEPTIONSTACKADJUSTMENT 2164
#define TARGET_THREAD_EXCEPTIONOFFSET 2168
#define TARGET_THREAD_EXCEPTIONHANDLER 2172
#define TARGET_THREAD_EXCEPTIONSTACKADJUSTMENT 2168
#define TARGET_THREAD_EXCEPTIONOFFSET 2172
#define TARGET_THREAD_EXCEPTIONHANDLER 2176

#define TARGET_THREAD_IP 2144
#define TARGET_THREAD_STACK 2148
#define TARGET_THREAD_NEWSTACK 2152
#define TARGET_THREAD_SCRATCH 2156
#define TARGET_THREAD_CONTINUATION 2160
#define TARGET_THREAD_TAILADDRESS 2176
#define TARGET_THREAD_VIRTUALCALLTARGET 2180
#define TARGET_THREAD_VIRTUALCALLINDEX 2184
#define TARGET_THREAD_HEAPIMAGE 2188
#define TARGET_THREAD_CODEIMAGE 2192
#define TARGET_THREAD_THUNKTABLE 2196
#define TARGET_THREAD_STACKLIMIT 2220
#define TARGET_THREAD_IP 2148
#define TARGET_THREAD_STACK 2152
#define TARGET_THREAD_NEWSTACK 2156
#define TARGET_THREAD_SCRATCH 2160
#define TARGET_THREAD_CONTINUATION 2164
#define TARGET_THREAD_TAILADDRESS 2180
#define TARGET_THREAD_VIRTUALCALLTARGET 2184
#define TARGET_THREAD_VIRTUALCALLINDEX 2188
#define TARGET_THREAD_HEAPIMAGE 2192
#define TARGET_THREAD_CODEIMAGE 2196
#define TARGET_THREAD_THUNKTABLE 2200
#define TARGET_THREAD_STACKLIMIT 2224

# else
# error
Expand Down
19 changes: 19 additions & 0 deletions src/classpath-android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ loadLibrary(Thread* t, object, uintptr_t* arguments)
{
object name = reinterpret_cast<object>(arguments[1]);

Thread::LibraryLoadStack stack(t, reinterpret_cast<object>(arguments[2]));

unsigned length = stringLength(t, name);
THREAD_RUNTIME_ARRAY(t, char, n, length + 1);
stringChars(t, name, RUNTIME_ARRAY_BODY(n));
Expand Down Expand Up @@ -568,6 +570,17 @@ class MyClasspath : public Classpath {
return true;
}

virtual object libraryClassLoader(Thread* t, object caller)
{
return strcmp(
"java/lang/Runtime",
reinterpret_cast<char*>(
&byteArrayBody(t, className(t, methodClass(t, caller)), 0)))
== 0
? t->libraryLoadStack->classLoader
: classLoader(t, methodClass(t, caller));
}

virtual void
shutDown(Thread*)
{
Expand Down Expand Up @@ -988,6 +1001,12 @@ register_org_apache_harmony_dalvik_NativeTestTarget(_JNIEnv*)
return 0;
}

int register_java_math_NativeBN(_JNIEnv*)
{
// ignore
return 0;
}

extern "C" AVIAN_EXPORT int64_t JNICALL
Avian_java_lang_String_compareTo
(Thread* t, object, uintptr_t* arguments)
Expand Down
50 changes: 44 additions & 6 deletions src/classpath-avian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,37 @@ class MyClasspath : public Classpath {
return fieldAtOffset<int32_t>(b, fieldOffset(t, field));
}

virtual bool
canTailCall(Thread*, object, object, object, object)
virtual bool canTailCall(Thread* t,
object,
object calleeClassName,
object calleeMethodName,
object)
{
return true;
// we can't tail call System.load[Library] or
// Runtime.load[Library] due to their use of
// ClassLoader.getCaller, which gets confused if we elide stack
// frames.

return (
(strcmp("loadLibrary",
reinterpret_cast<char*>(&byteArrayBody(t, calleeMethodName, 0)))
and strcmp("load",
reinterpret_cast<char*>(
&byteArrayBody(t, calleeMethodName, 0))))
or (strcmp(
"java/lang/System",
reinterpret_cast<char*>(&byteArrayBody(t, calleeClassName, 0)))
and strcmp("java/lang/Runtime",
reinterpret_cast<char*>(
&byteArrayBody(t, calleeClassName, 0)))));
}

virtual object libraryClassLoader(Thread* t, object caller)
{
return (methodClass(t, caller) == type(t, Machine::ClassLoaderType)
and t->libraryLoadStack)
? t->libraryLoadStack->classLoader
: classLoader(t, methodClass(t, caller));
}

virtual void
Expand Down Expand Up @@ -550,12 +577,23 @@ Avian_java_lang_System_identityHashCode
}
}

extern "C" AVIAN_EXPORT int64_t JNICALL
Avian_java_lang_ClassLoader_getCaller(Thread* t, object, uintptr_t*)
{
return reinterpret_cast<int64_t>(
getJClass(t, methodClass(t, getCaller(t, 2))));
}

extern "C" AVIAN_EXPORT void JNICALL
Avian_java_lang_Runtime_load
(Thread* t, object, uintptr_t* arguments)
Avian_java_lang_ClassLoader_load(Thread* t, object, uintptr_t* arguments)
{
object name = reinterpret_cast<object>(arguments[0]);
bool mapName = arguments[1];

Thread::LibraryLoadStack stack(
t,
classLoader(t, jclassVmClass(t, reinterpret_cast<object>(arguments[1]))));

bool mapName = arguments[2];

unsigned length = stringLength(t, name);
THREAD_RUNTIME_ARRAY(t, char, n, length + 1);
Expand Down
30 changes: 30 additions & 0 deletions src/classpath-openjdk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,32 @@ class MyClasspath : public Classpath {
(&byteArrayBody(t, calleeClassName, 0))));
}

virtual object libraryClassLoader(Thread* t, object caller)
{
#ifdef AVIAN_OPENJDK_SRC
return (methodClass(t, caller) == type(t, Machine::ClassLoaderType)
and t->libraryLoadStack)
? t->libraryLoadStack->classLoader
#else
return strcmp(
"java/lang/ClassLoader$NativeLibrary",
reinterpret_cast<char*>(
&byteArrayBody(t, className(t, methodClass(t, caller)), 0)))
== 0
? classLoader(
t,
jclassVmClass(t,
t->m->processor->invoke(
t,
resolveMethod(t,
methodClass(t, caller),
"getFromClass",
"()Ljava/lang/Class;"),
0)))
#endif
: classLoader(t, methodClass(t, caller));
}

virtual void
shutDown(Thread* t)
{
Expand Down Expand Up @@ -1845,6 +1871,10 @@ management_JNI_OnLoad(JavaVM*, void*);
void JNICALL
loadLibrary(Thread* t, object, uintptr_t* arguments)
{
Thread::LibraryLoadStack stack(
t,
classLoader(t, jclassVmClass(t, reinterpret_cast<object>(arguments[0]))));

object name = reinterpret_cast<object>(arguments[1]);
THREAD_RUNTIME_ARRAY(t, char, n, stringLength(t, name) + 1);
stringChars(t, name, RUNTIME_ARRAY_BODY(n));
Expand Down
Loading

0 comments on commit 60df08d

Please sign in to comment.