zxh0 / jvm.go Goto Github PK
View Code? Open in Web Editor NEWA toy JVM written in Go
License: MIT License
A toy JVM written in Go
License: MIT License
/tmp go/bin/jvmgo -cp /Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home/jre/lib/rt.jar:. Main
java/util/concurrent/ConcurrentHashMap.()V bytecode:21 line:1598
java/lang/ClassLoader.(Ljava/lang/Void;Ljava/lang/ClassLoader;)V bytecode:94 line:283
java/lang/ClassLoader.(Ljava/lang/ClassLoader;)V bytecode:8 line:318
java/security/SecureClassLoader.(Ljava/lang/ClassLoader;)V bytecode:5 line:52
java/net/URLClassLoader.([Ljava/net/URL;Ljava/lang/ClassLoader;Ljava/net/URLStreamHandlerFactory;)V bytecode:5 line:207
sun/misc/Launcher$ExtClassLoader.([Ljava/io/File;)V bytecode:12 line:165
sun/misc/Launcher$ExtClassLoader$1.run()Lsun/misc/Launcher$ExtClassLoader; bytecode:39 line:148
sun/misc/Launcher$ExtClassLoader$1.run()Ljava/lang/Object; bytecode:4 line:142
java/security/AccessController.doPrivileged(Ljava/security/PrivilegedExceptionAction;)Ljava/lang/Object; bytecode:1 line:-2
sun/misc/Launcher$ExtClassLoader.getExtClassLoader()Lsun/misc/Launcher$ExtClassLoader; bytecode:15 line:141
sun/misc/Launcher.()V bytecode:7 line:71
sun/misc/Launcher.()V bytecode:18 line:57
java/lang/ClassLoader.initSystemClassLoader()V bytecode:22 line:1489
java/lang/ClassLoader.getSystemClassLoader()Ljava/lang/ClassLoader; bytecode:3 line:1475
java/lang/Class.getClassLoader0()Ljava/lang/ClassLoader; bytecode:1 line:-2
java/lang/Class.getClassLoader()Ljava/lang/ClassLoader; bytecode:4 line:609
java/lang/Class.desiredAssertionStatus()Z bytecode:4 line:2975
java/lang/Integer.()V bytecode:6 line:52
java/util/HashMap.roundUpToPowerOf2(I)I bytecode:21 line:303
java/util/HashMap.inflateTable(I)V bytecode:4 line:313
java/util/HashMap.put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; bytecode:18 line:490
sun/reflect/Reflection.()V bytecode:31 line:47
sun/misc/Unsafe.()V bytecode:15 line:49
java/util/concurrent/atomic/AtomicInteger.()V bytecode:0 line:56
java/lang/ThreadLocal.()V bytecode:0 line:89
java/io/FileInputStream.()V bytecode:0 line:63
java/lang/System.initializeSystemClass()V bytecode:37 line:1141
~shim. bytecode:0 line:-1
~shim. bytecode:0 line:-1
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x7a03e]
goroutine 1 [running]:
github.com/zxh0/jvm.go/jvmgo/jvm/interpreter._catchErr(0x2089f2080)
/tmp/go/src/github.com/zxh0/jvm.go/jvmgo/jvm/interpreter/interpreter.go:68 +0x2b6
github.com/zxh0/jvm.go/jvmgo/jvm/rtda/class.(_ConstantMethodref).GetVirtualMethod(0x208dbed80, 0x0, 0x0)
/tmp/go/src/github.com/zxh0/jvm.go/jvmgo/jvm/rtda/class/cp_methodref.go:104 +0x2e
github.com/zxh0/jvm.go/jvmgo/jvm/instructions.(_invokevirtual).Execute(0x208db9be8, 0x208dc03c0)
/tmp/go/src/github.com/zxh0/jvm.go/jvmgo/jvm/instructions/invokevirtual.go:22 +0x1b6
github.com/zxh0/jvm.go/jvmgo/jvm/interpreter._loop(0x2089f2080)
/tmp/go/src/github.com/zxh0/jvm.go/jvmgo/jvm/interpreter/interpreter.go:45 +0x13d
github.com/zxh0/jvm.go/jvmgo/jvm/interpreter.Loop(0x2089f2080)
/tmp/go/src/github.com/zxh0/jvm.go/jvmgo/jvm/interpreter/interpreter.go:19 +0xbc
github.com/zxh0/jvm.go/jvmgo/jvm.Startup(0x20836ec30)
/tmp/go/src/github.com/zxh0/jvm.go/jvmgo/jvm/jvm.go:19 +0xad
main.main()
/tmp/go/src/github.com/zxh0/jvm.go/jvmgo/main.go:14 +0x74
goroutine 5 [semacquire]:
sync.(_Cond).Wait(0x208c6e180)
/usr/local/Cellar/go/1.4.2/libexec/src/sync/cond.go:62 +0x9e
github.com/zxh0/jvm.go/jvmgo/jvm/rtda/class.(_Monitor).Wait(0x208c6e140)
/tmp/go/src/github.com/zxh0/jvm.go/jvmgo/jvm/rtda/class/obj_monitor.go:75 +0xad
github.com/zxh0/jvm.go/jvmgo/native/java/lang.wait(0x208ce6780)
/tmp/go/src/github.com/zxh0/jvm.go/jvmgo/native/java/lang/Object.go:87 +0xcb
github.com/zxh0/jvm.go/jvmgo/jvm/instructions.(*invoke_native).Execute(0x2c93e0, 0x208ce6780)
/tmp/go/src/github.com/zxh0/jvm.go/jvmgo/jvm/instructions/invoke_native.go:10 +0x6d
github.com/zxh0/jvm.go/jvmgo/jvm/interpreter._loop(0x208c718a0)
/tmp/go/src/github.com/zxh0/jvm.go/jvmgo/jvm/interpreter/interpreter.go:45 +0x13d
github.com/zxh0/jvm.go/jvmgo/jvm/interpreter.Loop(0x208c718a0)
/tmp/go/src/github.com/zxh0/jvm.go/jvmgo/jvm/interpreter/interpreter.go:19 +0xbc
created by github.com/zxh0/jvm.go/jvmgo/native/java/lang.start0
/tmp/go/src/github.com/zxh0/jvm.go/jvmgo/native/java/lang/Thread.go:102 +0x1a4
你那本数第二版什么时候出呀,记得把多线程和垃圾回收实现以下。好了记得提醒我去买
I tried to execute this code using jvmgo
import java.util.*;
class prime {
public static void main(String[] args) {
ArrayList<Integer> res = new ArrayList<Integer>();
int last = 3;
res.add(last);
while(true) {
last = last + 2;
boolean prime = true;
for(int v : res) {
if(v*v>last) break;
if(last%v == 0) {
prime = false;
break;
}
}
if(prime) {
res.add(last);
if(res.size()%100000 == 0) System.out.println(last);
if(last>9999999) break;
}
}
}
}
but it took too much time, more than 578.62
s (I terminate execution because it took too long), for comparison openjdk 8u40 gives complete output within 1.23
s
OS: 64-bit ArchLinux
Kernel version: 3.18.7-1
Processor: i3-4150
RAM: 16GB
It is considered as a bad practice to ask users to change their GOPATH to build some library/program. Because users could not install the program through go get
. More details could be found here. To quote:
Sometimes people set GOPATH to the root of their source repository and put their packages in directories relative to the repository root, such as "src/my/package". On one hand, this keeps the import paths short ("my/package" instead of "github.com/me/project/my/package"), but on the other it breaks go get and forces users to re-set their GOPATH to use the package. Don't do this.
To make it more idiomatic, I would suggest you to move every thing in src/
to the root directory and change the import paths accordingly.
jvm.go/jvmgo/rtda/heap/class_loader.go
Line 256 in 58a552e
This line of code should count the instance fields recursively. While the class.superClass.instanceSlotCount hasn't been initialized, its value is 0.
我买了你的书,最近我在做一个和jvm相关的项目,遇到了些问题,不知道可否帮助下??
I just developed an interesting JVM with Java which can run itself. The most important part of the bytecode interpreter is a switch statement. Is your approach different?
Here is my implementation:
https://gitlab.com/neoexpert/jvm
maybe jvmgo can run it? Can jvmgo run a jar file?
With my implementation you can do:
java -jar java.jar -jar java.jar HelloWorldClass
Here is the bytecode interpreter:
https://gitlab.com/neoexpert/jvm/blob/master/jvm/src/main/java/com/neoexpert/jvm/executor/RawByteCodeExecutorNativeLong.java
Bug: This exists
How to reproduce: Go to https://github.com/zxh0/jvm.go
Expected Result: No JVM written in Go using Sublime Text
Result: JVM written in Go using Sublime Text
How to fix: Delete repository, rethink life choices
my test class
public class Main{
public static void main(String args[]){
System.out.println("Hello World");
}
}
and my java version
work@work-desktop:~/workspace/tmp$ echo $JAVA_HOME
/usr/local/etc/jdk1.8.0_45
and i run javac Main.java and java Main,it succ
work@work-desktop:
/workspace/tmp$ javac Main.java/workspace/tmp$ java Main
work@work-desktop:
Hello World
but when i run
jvmgo -XuseJavaHome -cp ./ Main
it fail with such error
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x1 pc=0x80cca78]
goroutine 1 [running]:
github.com/zxh0/jvm.go/jvmgo/classpath.func·001(0x1850be90, 0x23, 0x0, 0x0, 0xb7714c70, 0x18538940, 0x0, 0x0)
/home/work/workspace/go/src/github.com/zxh0/jvm.go/jvmgo/classpath/entry_wildcard.go:18 +0x58
path/filepath.Walk(0x1850be90, 0x23, 0x1851b668, 0x0, 0x0)
/usr/local/go/src/path/filepath/path.go:392 +0x97
github.com/zxh0/jvm.go/jvmgo/classpath.newWildcardEntry(0x1850be90, 0x24, 0x4)
/home/work/workspace/go/src/github.com/zxh0/jvm.go/jvmgo/classpath/entry_wildcard.go:28 +0xa9
github.com/zxh0/jvm.go/jvmgo/classpath.(*ClassPath).parseBootAndExtClassPath(0x185300b0)
/home/work/workspace/go/src/github.com/zxh0/jvm.go/jvmgo/classpath/class_path.go:28 +0x252
github.com/zxh0/jvm.go/jvmgo/classpath.Parse(0xbfaab4ab, 0x2, 0x831af40)
/home/work/workspace/go/src/github.com/zxh0/jvm.go/jvmgo/classpath/class_path.go:16 +0x4c
github.com/zxh0/jvm.go/jvmgo/jvm.Startup(0x18538920)
/home/work/workspace/go/src/github.com/zxh0/jvm.go/jvmgo/jvm/jvm.go:32 +0xfe
main.main()
/home/work/workspace/go/src/github.com/zxh0/jvm.go/jvmgo/main.go:15 +0x68
goroutine 2 [runnable]:
runtime.forcegchelper()
/usr/local/go/src/runtime/proc.go:90
runtime.goexit()
/usr/local/go/src/runtime/asm_386.s:2287 +0x1
goroutine 3 [runnable]:
runtime.bgsweep()
/usr/local/go/src/runtime/mgc0.go:82
runtime.goexit()
/usr/local/go/src/runtime/asm_386.s:2287 +0x1
goroutine 4 [runnable]:
runtime.runfinq()
/usr/local/go/src/runtime/malloc.go:712
runtime.goexit()
/usr/local/go/src/runtime/asm_386.s:2287 +0x1
am i wrong? and how to fix that problem?
thank you!
Myself (and probably many others) would like to use the classloader and instruction decoder to do analysis on class files. Any plans to expose the different instruction structs or at least the name?
hi, i want to build a project about lightweight java vm for blockchain. who has interesting in it?
Question, has anyone successfully developed and deployed an app written in go on the jvm using websockets?
Can I ask if you made that logo yourself? It is really cool and wondered if you had original files, or svg files? I am working on a C compiler in Go* and would love a similar logo, but lack any art skills.
Hi, how's support for cross platform GUO e.g. swing?
我刚看了一些jdk11里面找不到rt.jar了,一头雾水,不知如何下手。
It would be good to clarify the legal status of using this JVM implementation and whether it is a clean room implementation of Java (http://stackoverflow.com/questions/2575362/java-jce-providers-what-does-clean-room-implementation-mean) or not.
Also it would be interesting to look at pure open source (i.e. without patent nightmares!) rt.jar to package with your VM.
One thing I've been wishing for - for an age - is a lightweight Java alternative. I.e. Java 8 syntax - but only that - different rt.jar different memory management options, everything else redesigned to allow for no bloat and fast initialization. I suppose basically Android on the desktop :-) :-) Java 9 is making some improvements but still there's a design emphasis that 'resources are cheap' - which if you're hosting in the cloud they ain't!
Anyway good luck, I hope you know what you've gotten yourself into :-)
非常抱歉用这样方式,网上实在找不到作者联系方式,才出此下策,但愿作者能看到,我是比特大陆工程师洪森,比特大陆是一家做数字货币和AI的公司,现在找高手一起做数字货币基础设施,预计需要新实现一个虚拟机,技术应该非常有挑战,工作形式也是做开源,我微信(love16898),暂时不换工作也可以加我微信,可以随时和我交流区块链、虚拟货币相关知识。
Hey there, neat project! I found it through Hacker News, and thought I'd open an issue for this comment: https://news.ycombinator.com/item?id=9203653
Basically, without a license, "All rights reserved" is what's assumed. http://choosealicense.com/ can help you pick the right license for your code, if indeed you wished to make this open source or free software.
this go code depend open jdk source code ?
Currently, there is no support for invokedynamic, I know that is a bit hard to implement, but since Java 8/lambdas, invokedynamic is very present in Bytecode.
Useful references for implementation:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.