[Git][java-team/jboss-classfilewriter][upstream] New upstream version 1.2.3

Markus Koschany gitlab at salsa.debian.org
Sat Jul 7 20:58:42 BST 2018


Markus Koschany pushed to branch upstream at Debian Java Maintainers / jboss-classfilewriter


Commits:
55da2741 by Markus Koschany at 2018-07-07T21:53:58+02:00
New upstream version 1.2.3
- - - - -


2 changed files:

- pom.xml
- src/main/java/org/jboss/classfilewriter/ClassFile.java


Changes:

=====================================
pom.xml
=====================================
--- a/pom.xml
+++ b/pom.xml
@@ -9,7 +9,7 @@
 
     <groupId>org.jboss.classfilewriter</groupId>
     <artifactId>jboss-classfilewriter</artifactId>
-    <version>1.2.2.Final</version>
+    <version>1.2.3.Final</version>
 
     <packaging>jar</packaging>
     <description>A bytecode writer that creates .class files at runtime</description>


=====================================
src/main/java/org/jboss/classfilewriter/ClassFile.java
=====================================
--- a/src/main/java/org/jboss/classfilewriter/ClassFile.java
+++ b/src/main/java/org/jboss/classfilewriter/ClassFile.java
@@ -19,6 +19,7 @@ package org.jboss.classfilewriter;
 
 import java.io.IOException;
 import java.lang.annotation.Annotation;
+import java.lang.reflect.AccessibleObject;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
@@ -104,6 +105,8 @@ public class ClassFile implements WritableEntry {
         this.attributes.add(runtimeVisibleAnnotationsAttribute);
     }
 
+    private final static java.lang.reflect.Method defineClass1, defineClass2;
+
     public void addInterface(String iface) {
         this.interfaces.add(iface);
     }
@@ -282,7 +285,16 @@ public class ClassFile implements WritableEntry {
                 sm.checkPermission(permission);
             }
             byte[] b = toBytecode();
-            return UNSAFE.defineClass(name.replace('/', '.'), b, 0, b.length, loader, domain );
+            java.lang.reflect.Method method;
+            Object[] args;
+            if (domain == null) {
+                method = defineClass1;
+                args = new Object[] { name.replace('/', '.'), b, Integer.valueOf(0), Integer.valueOf(b.length) };
+            } else {
+                method = defineClass2;
+                args = new Object[] { name.replace('/', '.'), b, Integer.valueOf(0), Integer.valueOf(b.length), domain };
+            }
+            return (Class<?>) method.invoke(loader, args);
         } catch (RuntimeException e) {
             throw e;
         } catch (Exception e) {
@@ -368,27 +380,40 @@ public class ClassFile implements WritableEntry {
 
     // Unsafe mechanics
 
-    private static final sun.misc.Unsafe UNSAFE;
     static {
         try {
-            UNSAFE = getUnsafe();
-        } catch (Exception e) {
-            throw new Error(e);
-        }
-    }
-
-    private static Unsafe getUnsafe() {
-        if (System.getSecurityManager() != null) {
-            return new PrivilegedAction<Unsafe>() {
-                public Unsafe run() {
-                    return getUnsafe0();
+            Method[] defineClassMethods = AccessController.doPrivileged(new PrivilegedExceptionAction<Method[]>() {
+                public Method[] run() throws Exception {
+                    final sun.misc.Unsafe UNSAFE;
+                    final long overrideOffset;
+                    // first we need to grab Unsafe
+                    try {
+                        UNSAFE = getUnsafe();
+                        overrideOffset = UNSAFE.objectFieldOffset(AccessibleObject.class.getDeclaredField("override"));
+                    } catch (Exception e) {
+                        throw new Error(e);
+                    }
+                    // now we gain access to CL.defineClass methods
+                    Class<?> cl = ClassLoader.class;
+                    Method defClass1 = cl.getDeclaredMethod("defineClass", new Class[] { String.class, byte[].class, int.class,
+                        int.class });
+                    Method defClass2 = cl.getDeclaredMethod("defineClass", new Class[] { String.class, byte[].class, int.class,
+                        int.class, ProtectionDomain.class });
+                    // use Unsafe to crack open both CL.defineClass() methods (instead of using setAccessible())
+                    UNSAFE.putBoolean(defClass1, overrideOffset, true);
+                    UNSAFE.putBoolean(defClass2, overrideOffset, true);
+                    return new Method[]{defClass1, defClass2};
                 }
-            }.run();
+            });
+            // set methods to final fields
+            defineClass1 = defineClassMethods[0];
+            defineClass2 = defineClassMethods[1];
+        } catch (PrivilegedActionException pae) {
+            throw new RuntimeException("cannot initialize ClassFile", pae.getException());
         }
-        return getUnsafe0();
     }
 
-    private static Unsafe getUnsafe0()  {
+    private static Unsafe getUnsafe()  {
         try {
             Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
             theUnsafe.setAccessible(true);



View it on GitLab: https://salsa.debian.org/java-team/jboss-classfilewriter/commit/55da27413a8b9a08d186d8d3ee2651c0ed199b1c

-- 
View it on GitLab: https://salsa.debian.org/java-team/jboss-classfilewriter/commit/55da27413a8b9a08d186d8d3ee2651c0ed199b1c
You're receiving this email because of your account on salsa.debian.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/pkg-java-commits/attachments/20180707/db408bf5/attachment.html>


More information about the pkg-java-commits mailing list