Skip to content

Commit

Permalink
sys redesign; jdk 17
Browse files Browse the repository at this point in the history
  • Loading branch information
Karlatemp committed Sep 15, 2021
1 parent 26827f4 commit 98912e3
Show file tree
Hide file tree
Showing 20 changed files with 383 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
*/
@SuppressWarnings({"unused", "BooleanMethodIsAlwaysInverted", "RedundantSuppression", "Since15", "DefaultAnnotationParam", "JavaDoc"})
public abstract class Unsafe {
private static Unsafe theUnsafe;
static Unsafe theUnsafe;
static final Unsafe INSTANCE = getUnsafe0();

/**
* is Java9+
Expand Down Expand Up @@ -92,12 +93,12 @@ public boolean isJava9() {
@Contract(pure = false)
public static Unsafe getUnsafe() {
SecurityCheck.LIMITER.preGetUnsafe();
return getUnsafe0();
return INSTANCE;
}

static Unsafe getUnsafe0() {
if (theUnsafe == null) {
return theUnsafe = (Unsafe) UsfAccessor.allocateUnsafe();
UsfAccessor.initialize();
}
return theUnsafe;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// internal
public class UsfAccessor {
static abstract class UsfAccessorSpi {
abstract Object allocateUnsafe();
abstract void initialize();

abstract Consumer<Object> allocateObjectInitializer();
}
Expand All @@ -27,8 +27,8 @@ static UsfAccessorSpi spi() {
}

// Internal service for load Unsafe.
protected static Object allocateUnsafe() {
return spi().allocateUnsafe();
protected static void initialize() {
spi().initialize();
}

protected static Consumer<Object> allocateObjectInitializer() {
Expand Down
10 changes: 1 addition & 9 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
#
# Copyright 2019-2021 Mamoe Technologies and contributors.
#
# 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
# Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
#
# https://github.com/mamoe/mirai/blob/master/LICENSE
#
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
Expand Down
9 changes: 5 additions & 4 deletions impl/codegen/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ dependencies {
implementation(project(':api'))

// https://mvnrepository.com/artifact/org.ow2.asm/asm
testCompile group: 'org.ow2.asm', name: 'asm', version: '9.1'
testCompile group: 'org.ow2.asm', name: 'asm-commons', version: '9.1'
testCompile group: 'org.ow2.asm', name: 'asm-util', version: '9.1'
testCompile group: 'org.ow2.asm', name: 'asm-tree', version: '9.1'
testImplementation group: 'org.ow2.asm', name: 'asm', version: '9.2'
testImplementation group: 'org.ow2.asm', name: 'asm-commons', version: '9.2'
testImplementation group: 'org.ow2.asm', name: 'asm-util', version: '9.2'
testImplementation group: 'org.ow2.asm', name: 'asm-tree', version: '9.2'

}

task runCodeGen(type: JavaExec) {
dependsOn(':impl.jdk8:classes')
dependsOn(':impl.jdk9:classes')
dependsOn(':impl.jdk17:classes')

classpath = sourceSets.test.runtimeClasspath
main = 'io.github.karlatemp.unsafeaccessor.RunCodeGen'
Expand Down
21 changes: 21 additions & 0 deletions impl/jdk17/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
plugins {
id 'java'
id 'java-library'
}

println("JDK 17")

ext.set('versionOverrided', true)

compileJava {
sourceCompatibility = JavaVersion.VERSION_16
targetCompatibility = JavaVersion.VERSION_16
options.compilerArgs.addAll([
'--add-exports', 'java.base/jdk.internal.misc=ALL-UNNAMED',
'--add-exports', 'java.base/jdk.internal.access=ALL-UNNAMED'
])
}

dependencies {
api(project(':impl.jdk9'))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.karlatemp.unsafeaccessor;

class UsfAllocImpl17 extends UsfAllocImpl9 {
@SuppressWarnings("JavaReflectionMemberAccess")
@Override
UsfAlloc checkSelectedRequirement() throws Exception {
try {
Class.forName("jdk.internal.misc.Unsafe").getMethod(
"defineAnonymousClass",
Class.class,
byte[].class,
Object[].class
);
} catch (NoSuchMethodException ignore) {
return this;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.karlatemp.unsafeaccessor;

import java.lang.invoke.MethodHandles;

class UsfImpl17 extends Impl9 {
@Override
public Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches) {
try {
MethodHandles.Lookup lookup = Root.RootLookupHolder.trustedIn(hostClass).in(hostClass);
if (cpPatches == null) {
return lookup.defineHiddenClass(data, false).lookupClass();
}
return lookup.defineHiddenClassWithClassData(data, cpPatches, false).lookupClass();
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
3 changes: 2 additions & 1 deletion impl/jdk8/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
plugins {
id 'java'
id 'java-library'
}

dependencies {
implementation(project(':api'))
api(project(':api'))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.github.karlatemp.unsafeaccessor;

abstract class UsfAlloc {

Unsafe newUnsafe(UsfAllocCtx ctx) throws Exception {
return ctx.newUsfImpl(this);
}

void prepare(UsfAllocCtx ctx) throws Exception {
}

void destroyLimit(UsfAllocCtx ctx) throws Exception {
}

void completed(UsfAllocCtx ctx) throws Exception {
}

ModuleAccess newModuleAccess(UsfAllocCtx ctx) throws Exception {
return new ModuleAccessImpl.Noop();
}

UsfAlloc checkSelectedRequirement() throws Exception {
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.github.karlatemp.unsafeaccessor;

import java.util.function.Consumer;
import java.util.function.Supplier;

class UsfAllocCtx {
private DynClassLoader loader;

void putUnsafeInstance(Unsafe usf) {
Unsafe.theUnsafe = usf;
}

void putModuleAccess(ModuleAccess ma) {
Root.Secret.MACCESS = ma;
}

DynClassLoader loader() {
if (loader == null)
return loader = new DynClassLoader();
return loader;
}

@SuppressWarnings("rawtypes")
static class DynClassLoader extends ClassLoader implements Supplier, Consumer {
Object env;

DynClassLoader() {
super(DynClassLoader.class.getClassLoader());
}

@Override
public Object get() {
return env;
}

@Override
public void accept(Object o) {
env = o;
}

Class<?> define(byte[] code) {
return defineClass(null, code, 0, code.length, null);
}

Class<?> defineAndLoad(byte[] data) throws ClassNotFoundException {
return Class.forName(define(data).getName(), true, this);
}
}

String namespace;
Class<?>[] ACCESS_C;

Unsafe newUsfImpl(UsfAlloc thiz) throws Exception {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.github.karlatemp.unsafeaccessor;

class UsfAllocImpl8 extends UsfAlloc {
}
3 changes: 2 additions & 1 deletion impl/jdk9/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'java'
id 'java-library'
}

println("JDK 9")
Expand All @@ -16,5 +17,5 @@ compileJava {
}

dependencies {
implementation(project(':api'))
api(project(':impl.jdk8'))
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ public void addOpensToAllUnnamed(Object m, String pkg) {

@Override
public void addOpensToAllUnnamed(Object m, Iterator<String> packages) {
JLA.addOpensToAllUnnamed((Module) m, packages);
while (packages.hasNext()) {
JLA.addOpensToAllUnnamed((Module) m, packages.next());
}
}

@Override
Expand Down
Loading

0 comments on commit 98912e3

Please sign in to comment.