Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 98757505 authored by Elliott Hughes's avatar Elliott Hughes
Browse files

Kill the global references in the OpenGL wrappers.

Just use jniThrowException instead. Note that it would be trivial to throw
seemingly more appropriate exceptions (NullPointerException and
OutOfMemoryException in particular), but I'm only attempting to preserve
existing behavior here.

I also found shadowing bugs in some of the special-case functions, which
would previously always have leaked memory.

This also moves an accidental change to a generated file (ActivityThread ->
AppGlobals) into the generator, so it won't be overwritten in future.

Change-Id: Iab570310b568cb406c60dd0e2b8211f8a36ae590
parent 626d865d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ mkdir -p out/android/util
echo "package android.graphics;" > out/android/graphics/Canvas.java
echo "public interface Canvas {}" >> out/android/graphics/Canvas.java

echo "package android.app; import android.content.pm.IPackageManager; public class ActivityThread { public static final ActivityThread currentActivityThread() { return null; } public static final String currentPackageName(){ return null; } public static IPackageManager getPackageManager() { return null;} }" > out/android/app/ActivityThread.java
echo "package android.app; import android.content.pm.IPackageManager; public class AppGlobals { public static IPackageManager getPackageManager() { return null;} }" > out/android/app/AppGlobals.java
# echo "package android.content; import android.content.pm.PackageManager; public interface Context { public PackageManager getPackageManager(); }" > out/android/content/Context.java
echo "package android.content.pm; public class ApplicationInfo {public int targetSdkVersion;}" > out/android/content/pm/ApplicationInfo.java
echo "package android.content.pm; public interface IPackageManager {ApplicationInfo getApplicationInfo(java.lang.String packageName, int flags) throws android.os.RemoteException;}" > out/android/content/pm/IPackageManager.java
+89 −117
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ public class JniCodeEmitter {
        } else if (baseType.equals("void")) {
            // nothing.
        } else {
            throw new RuntimeException("Uknown primitive basetype " + baseType);
            throw new RuntimeException("Unknown primitive basetype " + baseType);
        }
        return jniName;
    }
@@ -200,15 +200,9 @@ public class JniCodeEmitter {
                if (emitExceptionCheck) {
                    out.println(iii + indent + "_exception = 1;");
                }
                out.println(iii + indent +
                            (mUseCPlusPlus ? "_env" : "(*_env)") +
                            "->ThrowNew(" +
                            (mUseCPlusPlus ? "" : "_env, ") +
                            "IAEClass, " +
                            "\"" +
                            (isBuffer ?
                             "remaining()" : "length - " + offset) +
                            " < needed\");");
                out.println(iii + indent + "jniThrowException(_env, " +
                        "\"java/lang/IllegalArgumentException\", " +
                        "\"" + (isBuffer ? "remaining()" : "length - " + offset) + " < needed\");");
                out.println(iii + indent + "goto exit;");
                needsExit = true;
                out.println(iii + "}");
@@ -350,30 +344,26 @@ public class JniCodeEmitter {
                        index += 3;
                        continue;
                    }
                            out.println(iii + "if (" + remaining + " < " +
                                        checks[index + 2] +
                                        ") {");
                    out.println(iii + "if (" + remaining + " < " + checks[index + 2] + ") {");
                    if (emitExceptionCheck) {
                        out.println(iii + indent + "_exception = 1;");
                    }
                    String exceptionClassName = "IAEClass";
                    String exceptionClassName = "java/lang/IllegalArgumentException";
                    // If the "check" keyword was of the form
                    // "check_<class name>", use the class name in the
                    // exception to be thrown
                    int underscore = checks[index].indexOf('_');
                    if (underscore >= 0) {
                    exceptionClassName = checks[index].substring(underscore + 1) + "Class";
                        String abbr = checks[index].substring(underscore + 1);
                        if (abbr.equals("AIOOBE")) {
                            exceptionClassName = "java/lang/ArrayIndexOutOfBoundsException";
                        } else {
                            throw new RuntimeException("unknown exception abbreviation: " + abbr);
                        }
                            out.println(iii + indent +
                                        (mUseCPlusPlus ? "_env" : "(*_env)") +
                                        "->ThrowNew(" +
                                        (mUseCPlusPlus ? "" : "_env, ") +
                        exceptionClassName + ", " +
                                        "\"" +
                                        (isBuffer ?
                                         "remaining()" : "length - " + offset) +
                                        " < " + checks[index + 2] +
                                        "\");");
                    }
                    out.println(iii + indent + "jniThrowException(_env, " +
                            "\"" + exceptionClassName + "\", " +
                            "\"" + (isBuffer ? "remaining()" : "length - " + offset) + " < " + checks[index + 2] + "\");");

                    out.println(iii + indent + "goto exit;");
                    needsExit = true;
@@ -385,26 +375,16 @@ public class JniCodeEmitter {

                    if (!lastWasIfcheck) {
                        out.println(iii + "int _needed;");
                                out.println(iii +
                                            "switch (" +
                                            checks[index + 3] +
                                            ") {");
                        out.println(iii + "switch (" + checks[index + 3] + ") {");
                    }

                    for (int i = 0; i < matches.length; i++) {
                        out.println("#if defined(" + matches[i] + ")");
                                out.println(iii +
                                            "    case " +
                                            matches[i] +
                                            ":");
                        out.println(iii + "    case " + matches[i] + ":");
                        out.println("#endif // defined(" + matches[i] + ")");
                    }
                            out.println(iii +
                                        "        _needed = " +
                                        checks[index + 2] +
                                        ";");
                            out.println(iii +
                                        "        break;");
                    out.println(iii + "        _needed = " + checks[index + 2] + ";");
                    out.println(iii + "        break;");

                    lastWasIfcheck = true;
                    index += 5;
@@ -421,8 +401,7 @@ public class JniCodeEmitter {
                    // ignore
                    index += 1;
                } else {
                            System.out.println("Error: unknown keyword \"" +
                                               checks[index] + "\"");
                    System.out.println("Error: unknown keyword \"" + checks[index] + "\"");
                    System.exit(0);
                }
            }
@@ -817,7 +796,7 @@ public class JniCodeEmitter {
        boolean isUnsupported = isUnsupportedFunc(cfunc);
        if (isUnsupported) {
            out.println(indent +
                        "_env->ThrowNew(UOEClass,");
                        "jniThrowException(_env, \"java/lang/UnsupportedOperationException\",");
            out.println(indent +
                        "    \"" + cfunc.getName() + "\");");
            if (!isVoid) {
@@ -834,7 +813,7 @@ public class JniCodeEmitter {
            out.println(indent +
                        "if (! supportsExtension(_env, _this, have_" + requiresExtension + "ID)) {");
            out.println(indent + indent +
                        "_env->ThrowNew(UOEClass,");
                        "jniThrowException(_env, \"java/lang/UnsupportedOperationException\",");
            out.println(indent + indent +
                        "    \"" + cfunc.getName() + "\");");
            if (isVoid) {
@@ -945,7 +924,8 @@ public class JniCodeEmitter {
                CType type = cfunc.getArgType(jfunc.getArgCIndex(idx));
                String decl = type.getDeclaration();
                out.println(indent + "if (!" + cname + ") {");
                out.println(indent + "    _env->ThrowNew(IAEClass, \"" + cname + " == null\");");
                out.println(indent + "    jniThrowException(_env, " +
                        "\"java/lang/IllegalArgumentException\", \"" + cname + " == null\");");
                out.println(indent + "    goto exit;");
                needsExit = true;
                out.println(indent + "}");
@@ -978,13 +958,9 @@ public class JniCodeEmitter {
                    if (emitExceptionCheck) {
                        out.println(indent + indent + "_exception = 1;");
                    }
                    out.println(indent + "    " +
                                (mUseCPlusPlus ? "_env" : "(*_env)") +
                                "->ThrowNew(" +
                                (mUseCPlusPlus ? "" : "_env, ") +
                                "IAEClass, " +
                                "\"" + cname +
                                " == null\");");
                    out.println(indent + "    jniThrowException(_env, " +
                            "\"java/lang/IllegalArgumentException\", " +
                            "\"" + cname + " == null\");");
                    out.println(indent + "    goto exit;");
                    needsExit = true;
                    out.println(indent + "}");
@@ -993,12 +969,8 @@ public class JniCodeEmitter {
                    if (emitExceptionCheck) {
                        out.println(indent + indent + "_exception = 1;");
                    }
                    out.println(indent + "    " +
                                (mUseCPlusPlus ? "_env" : "(*_env)") +
                                "->ThrowNew(" +
                                (mUseCPlusPlus ? "" : "_env, ") +
                                "IAEClass, " +
                                "\"" + offset + " < 0\");");
                    out.println(indent + "    jniThrowException(_env, " +
                            "\"java/lang/IllegalArgumentException\", \"" + offset + " < 0\");");
                    out.println(indent + "    goto exit;");
                    needsExit = true;
                    out.println(indent + "}");
+13 −36
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

// This source file is automatically generated

#include "jni.h"
#include "JNIHelp.h"
#include <android_runtime/AndroidRuntime.h>
#include <utils/misc.h>

@@ -27,10 +29,6 @@ static int initialized = 0;

static jclass nioAccessClass;
static jclass bufferClass;
static jclass OOMEClass;
static jclass UOEClass;
static jclass IAEClass;
static jclass AIOOBEClass;
static jmethodID getBasePointerID;
static jmethodID getBaseArrayID;
static jmethodID getBaseArrayOffsetID;
@@ -41,7 +39,7 @@ static jfieldID elementSizeShiftID;
/* Cache method IDs each time the class is loaded. */

static void
nativeClassInitBuffer(JNIEnv *_env)
nativeClassInit(JNIEnv *_env, jclass glImplClass)
{
    jclass nioAccessClassLocal = _env->FindClass("java/nio/NIOAccess");
    nioAccessClass = (jclass) _env->NewGlobalRef(nioAccessClassLocal);
@@ -63,26 +61,6 @@ nativeClassInitBuffer(JNIEnv *_env)
}


static void
nativeClassInit(JNIEnv *_env, jclass glImplClass)
{
    nativeClassInitBuffer(_env);

    jclass IAEClassLocal =
        _env->FindClass("java/lang/IllegalArgumentException");
    jclass OOMEClassLocal =
         _env->FindClass("java/lang/OutOfMemoryError");
    jclass UOEClassLocal =
         _env->FindClass("java/lang/UnsupportedOperationException");
    jclass AIOOBEClassLocal =
         _env->FindClass("java/lang/ArrayIndexOutOfBoundsException");

    IAEClass = (jclass) _env->NewGlobalRef(IAEClassLocal);
    OOMEClass = (jclass) _env->NewGlobalRef(OOMEClassLocal);
    UOEClass = (jclass) _env->NewGlobalRef(UOEClassLocal);
    AIOOBEClass = (jclass) _env->NewGlobalRef(AIOOBEClassLocal);
}

static void *
getPointer(JNIEnv *_env, jobject buffer, jarray *array, jint *remaining)
{
@@ -122,4 +100,3 @@ releasePointer(JNIEnv *_env, jarray array, void *data, jboolean commit)
}

// --------------------------------------------------------------------------
+15 −37
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

// This source file is automatically generated

#include "jni.h"
#include "JNIHelp.h"
#include <android_runtime/AndroidRuntime.h>
#include <utils/misc.h>

@@ -40,10 +42,6 @@ static int initialized = 0;

static jclass nioAccessClass;
static jclass bufferClass;
static jclass OOMEClass;
static jclass UOEClass;
static jclass IAEClass;
static jclass AIOOBEClass;
static jmethodID getBasePointerID;
static jmethodID getBaseArrayID;
static jmethodID getBaseArrayOffsetID;
@@ -54,7 +52,7 @@ static jfieldID elementSizeShiftID;
/* Cache method IDs each time the class is loaded. */

static void
nativeClassInitBuffer(JNIEnv *_env)
nativeClassInit(JNIEnv *_env, jclass glImplClass)
{
    jclass nioAccessClassLocal = _env->FindClass("java/nio/NIOAccess");
    nioAccessClass = (jclass) _env->NewGlobalRef(nioAccessClassLocal);
@@ -75,26 +73,6 @@ nativeClassInitBuffer(JNIEnv *_env)
        _env->GetFieldID(bufferClass, "_elementSizeShift", "I");
}

static void
nativeClassInit(JNIEnv *_env, jclass glImplClass)
{
    nativeClassInitBuffer(_env);

    jclass IAEClassLocal =
        _env->FindClass("java/lang/IllegalArgumentException");
    jclass OOMEClassLocal =
         _env->FindClass("java/lang/OutOfMemoryError");
    jclass UOEClassLocal =
         _env->FindClass("java/lang/UnsupportedOperationException");
    jclass AIOOBEClassLocal =
         _env->FindClass("java/lang/ArrayIndexOutOfBoundsException");

    IAEClass = (jclass) _env->NewGlobalRef(IAEClassLocal);
    OOMEClass = (jclass) _env->NewGlobalRef(OOMEClassLocal);
    UOEClass = (jclass) _env->NewGlobalRef(UOEClassLocal);
    AIOOBEClass = (jclass) _env->NewGlobalRef(AIOOBEClassLocal);
}

static void *
getPointer(JNIEnv *_env, jobject buffer, jarray *array, jint *remaining)
{
@@ -140,7 +118,8 @@ getDirectBufferPointer(JNIEnv *_env, jobject buffer) {
        jint elementSizeShift = _env->GetIntField(buffer, elementSizeShiftID);
        buf += position << elementSizeShift;
    } else {
        _env->ThrowNew(IAEClass, "Must use a native order direct Buffer");
        jniThrowException(_env, "java/lang/IllegalArgumentException",
                          "Must use a native order direct Buffer");
    }
    return (void*) buf;
}
@@ -153,4 +132,3 @@ getNumCompressedTextureFormats() {
}

// --------------------------------------------------------------------------
+15 −37
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

// This source file is automatically generated

#include "jni.h"
#include "JNIHelp.h"
#include <android_runtime/AndroidRuntime.h>
#include <utils/misc.h>

@@ -36,10 +38,6 @@ static int initialized = 0;

static jclass nioAccessClass;
static jclass bufferClass;
static jclass OOMEClass;
static jclass UOEClass;
static jclass IAEClass;
static jclass AIOOBEClass;
static jmethodID getBasePointerID;
static jmethodID getBaseArrayID;
static jmethodID getBaseArrayOffsetID;
@@ -50,7 +48,7 @@ static jfieldID elementSizeShiftID;
/* Cache method IDs each time the class is loaded. */

static void
nativeClassInitBuffer(JNIEnv *_env)
nativeClassInit(JNIEnv *_env, jclass glImplClass)
{
    jclass nioAccessClassLocal = _env->FindClass("java/nio/NIOAccess");
    nioAccessClass = (jclass) _env->NewGlobalRef(nioAccessClassLocal);
@@ -72,26 +70,6 @@ nativeClassInitBuffer(JNIEnv *_env)
}


static void
nativeClassInit(JNIEnv *_env, jclass glImplClass)
{
    nativeClassInitBuffer(_env);

    jclass IAEClassLocal =
        _env->FindClass("java/lang/IllegalArgumentException");
    jclass OOMEClassLocal =
         _env->FindClass("java/lang/OutOfMemoryError");
    jclass UOEClassLocal =
         _env->FindClass("java/lang/UnsupportedOperationException");
    jclass AIOOBEClassLocal =
         _env->FindClass("java/lang/ArrayIndexOutOfBoundsException");

    IAEClass = (jclass) _env->NewGlobalRef(IAEClassLocal);
    OOMEClass = (jclass) _env->NewGlobalRef(OOMEClassLocal);
    UOEClass = (jclass) _env->NewGlobalRef(UOEClassLocal);
    AIOOBEClass = (jclass) _env->NewGlobalRef(AIOOBEClassLocal);
}

static void *
getPointer(JNIEnv *_env, jobject buffer, jarray *array, jint *remaining)
{
@@ -138,9 +116,9 @@ getDirectBufferPointer(JNIEnv *_env, jobject buffer) {
        jint elementSizeShift = _env->GetIntField(buffer, elementSizeShiftID);
        buf += position << elementSizeShift;
    } else {
        _env->ThrowNew(IAEClass, "Must use a native order direct Buffer");
        jniThrowException(_env, "java/lang/IllegalArgumentException",
                          "Must use a native order direct Buffer");
    }
    return (void*) buf;
}
// --------------------------------------------------------------------------
Loading