GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 1,支持内嵌机器码 · dreamlike-ocean/UnsafeJava@731618d

This repository was archived by the owner on Jun 30, 2026. It is now read-only.

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pom.xml
  • readme.md
  • unsafe-jit
    • pom.xml
    • src
      • main/java/io/github/dreamlike/jit
        • AMD64Injector.java
        • Init.java
      • test/java
        • TestInjectCode.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pom.xml‎

Copy file name to clipboardExpand all lines: pom.xml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<module>unsafe-jdk</module>
4141
<module>vertx-ext</module>
4242
<module>unsafe-core</module>
43+
<module>unsafe-jit</module>
4344
</modules>
4445

4546
<properties>
Collapse file

‎readme.md‎

Copy file name to clipboardExpand all lines: readme.md
+1-1Lines changed: 1 addition & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ maven 坐标为,本库以multi-release的形式发布,所以你可以在jdk8
3737
<dependency>
3838
<groupId>io.github.dreamlike-ocean</groupId>
3939
<artifactId>unsafe-core</artifactId>
40-
<version>1.2</version>
40+
<version>1.3</version>
4141
</dependency>
4242
```
4343

Collapse file

‎unsafe-jit/pom.xml‎

Copy file name to clipboard
+73Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>io.github.dreamlike-ocean</groupId>
8+
<artifactId>unsafe-java</artifactId>
9+
<version>1.0</version>
10+
</parent>
11+
12+
<artifactId>unsafe-jit</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>${java.version}</maven.compiler.source>
16+
<maven.compiler.target>${java.version}</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>io.github.dreamlike-ocean</groupId>
23+
<artifactId>unsafe-core</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>junit</groupId>
27+
<artifactId>junit</artifactId>
28+
<version>4.13.1</version>
29+
<scope>test</scope>
30+
</dependency>
31+
</dependencies>
32+
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-compiler-plugin</artifactId>
38+
<version>3.8.1</version>
39+
<configuration>
40+
<source>${java.version}</source>
41+
<target>${java.version}</target>
42+
<compilerArgs>
43+
<arg>--enable-preview</arg>
44+
<arg>--add-modules</arg>
45+
<arg>jdk.internal.vm.ci</arg>
46+
<arg>--add-exports</arg>
47+
<arg>jdk.internal.vm.ci/jdk.vm.ci.code.site=ALL-UNNAMED</arg>
48+
<arg>--add-exports</arg>
49+
<arg>jdk.internal.vm.ci/jdk.vm.ci.hotspot=ALL-UNNAMED</arg>
50+
<arg>--add-exports</arg>
51+
<arg>jdk.internal.vm.ci/jdk.vm.ci.meta=ALL-UNNAMED</arg>
52+
<arg>--add-exports</arg>
53+
<arg>jdk.internal.vm.ci/jdk.vm.ci.runtime=ALL-UNNAMED</arg>
54+
<arg>--add-exports</arg>
55+
<arg>jdk.internal.vm.ci/jdk.vm.ci.code=ALL-UNNAMED</arg>
56+
</compilerArgs>
57+
</configuration>
58+
</plugin>
59+
60+
<plugin>
61+
<groupId>org.apache.maven.surefire</groupId>
62+
<artifactId>surefire-junit4</artifactId>
63+
<version>3.1.2</version>
64+
<configuration>
65+
<argLine>--enable-preview</argLine>
66+
<argline>-XX:+UnlockExperimentalVMOptions</argline>
67+
<argline>-XX:+EnableJVMCI</argline>
68+
</configuration>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
73+
</project>
Collapse file

‎unsafe-jit/src/main/java/io/github/dreamlike/jit/AMD64Injector.java‎

Copy file name to clipboard
+87Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package io.github.dreamlike.jit;
2+
3+
import jdk.vm.ci.code.site.DataPatch;
4+
import jdk.vm.ci.code.site.Mark;
5+
import jdk.vm.ci.code.site.Site;
6+
import jdk.vm.ci.hotspot.HotSpotCompiledCode;
7+
import jdk.vm.ci.hotspot.HotSpotCompiledNmethod;
8+
import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
9+
import jdk.vm.ci.meta.Assumptions;
10+
import jdk.vm.ci.meta.ResolvedJavaMethod;
11+
import jdk.vm.ci.runtime.JVMCI;
12+
import jdk.vm.ci.runtime.JVMCIBackend;
13+
import jdk.vm.ci.runtime.JVMCICompiler;
14+
15+
import java.lang.invoke.MethodHandles;
16+
import java.lang.reflect.Method;
17+
import java.lang.reflect.Modifier;
18+
19+
public class AMD64Injector {
20+
21+
22+
static {
23+
try {
24+
MethodHandles.lookup().ensureInitialized(Init.class);
25+
} catch (IllegalAccessException e) {
26+
throw new RuntimeException(e);
27+
}
28+
}
29+
30+
private static final JVMCIBackend backend = JVMCI.getRuntime().getHostJVMCIBackend();
31+
32+
public static void inject(Method m, byte[] machineCode) {
33+
int modifiers = m.getModifiers();
34+
if (!Modifier.isNative(modifiers)) {
35+
throw new IllegalArgumentException("method is not native");
36+
}
37+
38+
//0x41, (byte)0x81, (byte)0x7f
39+
//4字节对齐
40+
int length = machineCode.length;
41+
if (length % 4 != 0) {
42+
length += 4 - length % 4;
43+
}
44+
45+
byte[] realMachineCode = new byte[length + 3];
46+
System.arraycopy(machineCode, 0, realMachineCode, 0, machineCode.length);
47+
//参考bool NativeNMethodCmpBarrier::check_barrier
48+
//#ifdef _LP64
49+
// enum Intel_specific_constants {
50+
// instruction_code = 0x81,
51+
// instruction_size = 8,
52+
// imm_offset = 4,
53+
// instruction_rex_prefix = Assembler::REX | Assembler::REX_B,
54+
// instruction_modrm = 0x7f // [r15 + offset]
55+
// };
56+
realMachineCode[length + 0] = 0x41;
57+
realMachineCode[length + 1] = (byte) 0x81;
58+
realMachineCode[length + 2] = 0x7f;
59+
60+
ResolvedJavaMethod resolvedJavaMethod = backend.getMetaAccess().lookupJavaMethod(m);
61+
HotSpotCompiledNmethod nm = new HotSpotCompiledNmethod(
62+
m.getName(),
63+
realMachineCode,
64+
realMachineCode.length,
65+
//防止nmethod entry barrier is missing 可
66+
// 参考 https://github.com/openjdk/jdk/blob/8635642dbdfb74d2ae50a51611fd2c5980fe6e74/src/hotspot/share/jvmci/jvmciCodeInstaller.cpp#L1330的case ENTRY_BARRIER_PATCH
67+
new Site[]{new Mark(realMachineCode.length - 3, 7 /*jdk.graal.compiler.hotspot.HotSpotMarkId.ENTRY_BARRIER_PATCH*/)},
68+
new Assumptions.Assumption[0],
69+
new ResolvedJavaMethod[0],
70+
new HotSpotCompiledCode.Comment[0],
71+
new byte[0],
72+
1,
73+
new DataPatch[0],
74+
true,
75+
0,
76+
null,
77+
(HotSpotResolvedJavaMethod) resolvedJavaMethod,
78+
JVMCICompiler.INVOCATION_ENTRY_BCI,
79+
1,
80+
0,
81+
false
82+
);
83+
backend.getCodeCache().setDefaultCode(resolvedJavaMethod, nm);
84+
85+
86+
}
87+
}
Collapse file

‎unsafe-jit/src/main/java/io/github/dreamlike/jit/Init.java‎

Copy file name to clipboard
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.github.dreamlike.jit;
2+
3+
import top.dreamlike.unsafe.core.MasterKey;
4+
5+
import java.lang.invoke.MethodHandle;
6+
import java.lang.invoke.MethodHandles;
7+
import java.lang.invoke.MethodType;
8+
import java.util.Optional;
9+
10+
class Init {
11+
12+
static {
13+
try {
14+
exportsJVMCI();
15+
} catch (Throwable e) {
16+
throw new RuntimeException(e);
17+
}
18+
}
19+
private static void exportsJVMCI() throws Throwable {
20+
MethodHandles.Lookup trustedLookup = MasterKey.INSTANCE.getTrustedLookup();
21+
MethodHandle methodHandle = trustedLookup.findVirtual(Module.class, "implAddOpensToAllUnnamed", MethodType.methodType(void.class, String.class));
22+
// -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
23+
Optional<Module> jvmciModuleOptional = ModuleLayer.boot().findModule("jdk.internal.vm.ci");
24+
if (jvmciModuleOptional.isEmpty()) {
25+
throw new IllegalStateException("JVMCI module not found. Use -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI");
26+
}
27+
Module jvmciModule = jvmciModuleOptional.get();
28+
methodHandle.invoke(jvmciModule, "jdk.vm.ci.code");
29+
methodHandle.invoke(jvmciModule, "jdk.vm.ci.code.site");
30+
methodHandle.invoke(jvmciModule, "jdk.vm.ci.hotspot");
31+
methodHandle.invoke(jvmciModule, "jdk.vm.ci.meta");
32+
methodHandle.invoke(jvmciModule, "jdk.vm.ci.runtime");
33+
}
34+
}
Collapse file

‎unsafe-jit/src/test/java/TestInjectCode.java‎

Copy file name to clipboard
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import io.github.dreamlike.jit.AMD64Injector;
2+
import org.junit.Assert;
3+
import org.junit.Test;
4+
5+
import java.lang.reflect.Method;
6+
7+
public class TestInjectCode {
8+
9+
@Test
10+
public void test() throws NoSuchMethodException {
11+
Method method = NativeClass.class.getDeclaredMethod("cal");
12+
// mov eax, 20010329
13+
//ret
14+
AMD64Injector.inject(method, new byte[]{
15+
(byte) 0xb8, (byte) 0x59, 0x55, 0x31, 0x01,
16+
(byte) 0xc3
17+
}
18+
);
19+
long cal = NativeClass.cal();
20+
Assert.assertEquals(20010329,cal);
21+
}
22+
23+
public static class NativeClass {
24+
25+
public static native long cal();
26+
}
27+
28+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.