SlideShare a Scribd company logo
Metascala
A tiny DIY JVM
https://github.com/lihaoyi/Metascala
Li Haoyi
haoyi@dropbox.com
Scala Exchange
2nd Dec 2013
Who am I?
Li Haoyi
Write Python during the day
Write Scala at night
What is Metascala?
● A JVM
● in 3000 lines of Scala
● Which can load & interpret java programs
● And can interpret itself!
Size comparison
● Metascala:

~3,000 lines

● Avian JVM: ~80,000 lines
● OpenJDK:

~1,000,000 lines
Basic Usage

Create a new metascala VM
Plain Old Java Object

Captured variables are serialized
into VM’s environment

Closure’s class file
is given to VM to
load/parse/execute
Result is extracted
from VM into host
environment
No global state

Any other classes necessary to
evaluate the closure are loaded
from the current Classpath
It’s Metacircular!

Need to give the outer VM more
than the 1mb default heap

VM inside
a VM!
Simpler program avoids
initializing the scala/java std
libraries, which takes forever
under double-interpretation.

Takes a while (~10s) to produce result
Limitations
● Single-threaded
● Limited IO
● Slowww
Performance Comparison
● OpenJDK:

1.0x

● Metascala:

~100x

● Meta-Metascala: ~10000x
Why Metascala?
● Fun to explore the innards of the JVM
● An almost-fully secure Java runtime!
● Small size makes fiddling fun
Why Metascala?
● Fun to explore the innards of the JVM
● An almost-fully secure Java runtime!
● Small size makes fiddling fun
Quick Tour

Immutable Defs: ~380 loc
Native Bindings: ~650 loc
Bytecode SSA transform: ~650 loc
Runtime data structures:
820 loc
Binary heap & Copying
GC: 132 loc
DIY scala-pickling: 132 loc
“This is a VM”: 243 loc
Quick Tour: Tests
Tests for basic Java features

GC Fuzz-tests
Test Metacircularity!
Scala std lib usage
What’s a Heap?

Fig 1. A Heap
What’s a Garbage Collector?
Blit (copy) all roots to new heap
Stop when you’ve
scanned everything

Not pseudocode

Scan the already
copied things for
more things and
copy them too
Why Metascala?
● Fun to explore the innards of the JVM
● An almost-fully secure Java runtime!
● Small size makes fiddling fun
Limited Instruction Count
And Limited Memory!

Not an OOM Error!
We throw this ourselves
Explicitly defined capabilities

Every external call has to
be explicitly defined and
enabled
Security Characteristics
●
●
●
●

Finite instruction count
Finite memory
Well-defined interface to outside world
Doesn’t rely on Java Security Model at all!

● Still some holes…
Security Holes
●
●
●
●
●

Classloader can read from anywhere
Time spent classloading not accounted
Memory spent on classes not accounted
GC time not accounted
“native” methods’ time/memory not
accounted
Basic Problem
Outside World

User code resource
consumption is bounded
VM’s runtime resource
usage can be made to
grow arbitrarily large

Classes
Native method calls
User
Code
Runtime Data
Structures
Garbage Collector
Possible Solution
Put a VM Inside a VM!
Works,
... but 10000x slowdown

Outside World
Fixed Unaccounted Costs
Outside World

Classes
Native method calls
User
Code
Runtime Data
Structures
Garbage Collector
Another Possible Solution
Move more components into
virtual runtime
Difficult to bootstrap correctly
WIP

Outside World
Native method calls
Classes
Garbage
Collector
User Code
Runtime Data
Structures
Why Metascala?
● Fun to explore the innards of the JVM
● An almost-fully secure Java runtime!
● Small size makes fiddling fun
Live Demo
Ugliness
● Long compile times
● Nasty JVM Interface
● Impossible Debugging
Long compile times
●
● 100 lines/s
● Twice as slow (50 lines/s) on my older
machine!
Nasty JVM Interface
Ideal World
Initialized
User Code

Real World
Linear Initialization

Std Library

Initialized
User Code
Std Library

Clean Interfaces
VM

VM

LazyInitialization
means repeated
dives back into
lib/native code
Nasty Language
VM Interface
Java’s dirty little secret
The Verbosity of Java with the Safety of C

WTF! I’d never use these things!
You probably do What happens if you don’t have
them

Almost every Java program
ever uses these things.
Next Steps
● Maximize correctness
○ Implement Threads & IO
○ Fix bugs (GC, native calls, etc.)

● Solidify security characteristics
○ Still plenty of unaccounted-for memory/processing
○ Some can be hosted “within” VM itself

● Simplify Std-Lib/VM interface
○ Try using Android Std Lib?
Possible Experiments
● Native codegen instead of an interpreter?
○ Generate/exec native code through JNI
○ Heap is already a binary blob that can be easily
passed to native code

● Bytecode transforms and optimizations?
○ Already in SSA form

● Continuations, Isolates, Value Classes?
● Port the whole thing to Scala.Js?
Metascala: a tiny DIY JVM
Ask me about:
● Single Static Assignment form
● Copying Garbage Collection
● sun.misc.Unsafe
● Warts of the .class file format
● Capabilities-based security
● Abandoned approaches

More Related Content

What's hot (20)

PPT
JVM, byte codes & jvm languages
Edgar Espina
 
PPTX
Coroutines in Kotlin
Jemo Mgebrishvili
 
PDF
Node.js Course 2 of 2 - Advanced techniques
Manuel Eusebio de Paz Carmona
 
PDF
Node.js Course 1 of 2 - Introduction and first steps
Manuel Eusebio de Paz Carmona
 
PDF
Ola Bini J Ruby Power On The Jvm
deimos
 
PPTX
Java Virtual Machine
profbnk
 
PPT
Byte code jvm
myrajendra
 
PDF
Node.js an Exectutive View
Manuel Eusebio de Paz Carmona
 
PDF
Java introduction with JVM architecture
atozknowledge .com
 
PDF
JVM Bytecodes
Anvay Patil
 
ODP
Double checkedlockingjavasingletons
parag
 
PDF
Node.js: Whats the Big Deal? Presented and JS Meetup Chicago
Aziz Ali
 
PPTX
Foolangjs
Amjad Masad
 
PPTX
Jvm Architecture
ThirupathiReddy Vajjala
 
PDF
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
PPT
Java-java virtual machine
Surbhi Panhalkar
 
PPT
Nashorn
hina firdaus
 
KEY
Zero mq logs
Tomas Doran
 
PDF
Fundamental of Node.JS - Internship Presentation - Week7
Devang Garach
 
PPTX
Java virtual machine
Nikhil Sharma
 
JVM, byte codes & jvm languages
Edgar Espina
 
Coroutines in Kotlin
Jemo Mgebrishvili
 
Node.js Course 2 of 2 - Advanced techniques
Manuel Eusebio de Paz Carmona
 
Node.js Course 1 of 2 - Introduction and first steps
Manuel Eusebio de Paz Carmona
 
Ola Bini J Ruby Power On The Jvm
deimos
 
Java Virtual Machine
profbnk
 
Byte code jvm
myrajendra
 
Node.js an Exectutive View
Manuel Eusebio de Paz Carmona
 
Java introduction with JVM architecture
atozknowledge .com
 
JVM Bytecodes
Anvay Patil
 
Double checkedlockingjavasingletons
parag
 
Node.js: Whats the Big Deal? Presented and JS Meetup Chicago
Aziz Ali
 
Foolangjs
Amjad Masad
 
Jvm Architecture
ThirupathiReddy Vajjala
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
Java-java virtual machine
Surbhi Panhalkar
 
Nashorn
hina firdaus
 
Zero mq logs
Tomas Doran
 
Fundamental of Node.JS - Internship Presentation - Week7
Devang Garach
 
Java virtual machine
Nikhil Sharma
 

Similar to Scala e xchange 2013 haoyi li on metascala a tiny diy jvm (20)

PDF
JVM Internals (2015)
Luiz Fernando Teston
 
PDF
Jvm internals 2015 - CorkJUG
Luiz Fernando Teston
 
PDF
A Brief study on JVM A Brief study on JVM
BRNSSPublicationHubI
 
PPTX
Java byte code & virtual machine
Laxman Puri
 
PDF
Java Virtual Machine.pdf
Uncodemy
 
PDF
Inside JVM
Chinh Ngo Nguyen
 
PDF
Ijaprr vol1-2-13-60-64tejinder
ijaprr_editor
 
PPTX
Simple tweaks to get the most out of your jvm
Jamie Coleman
 
PPTX
Simple tweaks to get the most out of your JVM
Jamie Coleman
 
PPTX
A tour of Java and the JVM
Alex Birch
 
PDF
JVM Under the Hood
Serkan Özal
 
PPTX
Jvm memory model
Yoav Avrahami
 
PPT
testing ppt
techweb08
 
PPS
Introduction to Java
Ankita Totala
 
PPTX
JVM.pptx
ParvathiPPSGRKCW
 
PDF
An Introduction to Java Compiler and Runtime
Omar Bashir
 
PDF
JVM Memory Management Details
Azul Systems Inc.
 
PPTX
Java is a new language on the mainframe
Michael Erichsen
 
PPTX
Dissecting the Hotspot JVM
Ivan Ivanov
 
JVM Internals (2015)
Luiz Fernando Teston
 
Jvm internals 2015 - CorkJUG
Luiz Fernando Teston
 
A Brief study on JVM A Brief study on JVM
BRNSSPublicationHubI
 
Java byte code & virtual machine
Laxman Puri
 
Java Virtual Machine.pdf
Uncodemy
 
Inside JVM
Chinh Ngo Nguyen
 
Ijaprr vol1-2-13-60-64tejinder
ijaprr_editor
 
Simple tweaks to get the most out of your jvm
Jamie Coleman
 
Simple tweaks to get the most out of your JVM
Jamie Coleman
 
A tour of Java and the JVM
Alex Birch
 
JVM Under the Hood
Serkan Özal
 
Jvm memory model
Yoav Avrahami
 
testing ppt
techweb08
 
Introduction to Java
Ankita Totala
 
An Introduction to Java Compiler and Runtime
Omar Bashir
 
JVM Memory Management Details
Azul Systems Inc.
 
Java is a new language on the mainframe
Michael Erichsen
 
Dissecting the Hotspot JVM
Ivan Ivanov
 
Ad

More from Skills Matter (20)

PDF
5 things cucumber is bad at by Richard Lawrence
Skills Matter
 
ODP
Patterns for slick database applications
Skills Matter
 
ODP
Oscar reiken jr on our success at manheim
Skills Matter
 
ODP
Progressive f# tutorials nyc dmitry mozorov & jack pappas on code quotations ...
Skills Matter
 
PDF
Cukeup nyc ian dees on elixir, erlang, and cucumberl
Skills Matter
 
PDF
Cukeup nyc peter bell on getting started with cucumber.js
Skills Matter
 
PDF
Agile testing & bdd e xchange nyc 2013 jeffrey davidson & lav pathak & sam ho...
Skills Matter
 
ODP
Progressive f# tutorials nyc rachel reese & phil trelford on try f# from zero...
Skills Matter
 
ODP
Progressive f# tutorials nyc don syme on keynote f# in the open source world
Skills Matter
 
PDF
Agile testing & bdd e xchange nyc 2013 gojko adzic on bond villain guide to s...
Skills Matter
 
PPTX
Dmitry mozorov on code quotations code as-data for f#
Skills Matter
 
PDF
A poet's guide_to_acceptance_testing
Skills Matter
 
PDF
Russ miles-cloudfoundry-deep-dive
Skills Matter
 
KEY
Serendipity-neo4j
Skills Matter
 
PDF
Simon Peyton Jones: Managing parallelism
Skills Matter
 
PDF
Plug 20110217
Skills Matter
 
PDF
Lug presentation
Skills Matter
 
PPT
I went to_a_communications_workshop_and_they_t
Skills Matter
 
PDF
Plug saiku
Skills Matter
 
PDF
Huguk lily
Skills Matter
 
5 things cucumber is bad at by Richard Lawrence
Skills Matter
 
Patterns for slick database applications
Skills Matter
 
Oscar reiken jr on our success at manheim
Skills Matter
 
Progressive f# tutorials nyc dmitry mozorov & jack pappas on code quotations ...
Skills Matter
 
Cukeup nyc ian dees on elixir, erlang, and cucumberl
Skills Matter
 
Cukeup nyc peter bell on getting started with cucumber.js
Skills Matter
 
Agile testing & bdd e xchange nyc 2013 jeffrey davidson & lav pathak & sam ho...
Skills Matter
 
Progressive f# tutorials nyc rachel reese & phil trelford on try f# from zero...
Skills Matter
 
Progressive f# tutorials nyc don syme on keynote f# in the open source world
Skills Matter
 
Agile testing & bdd e xchange nyc 2013 gojko adzic on bond villain guide to s...
Skills Matter
 
Dmitry mozorov on code quotations code as-data for f#
Skills Matter
 
A poet's guide_to_acceptance_testing
Skills Matter
 
Russ miles-cloudfoundry-deep-dive
Skills Matter
 
Serendipity-neo4j
Skills Matter
 
Simon Peyton Jones: Managing parallelism
Skills Matter
 
Plug 20110217
Skills Matter
 
Lug presentation
Skills Matter
 
I went to_a_communications_workshop_and_they_t
Skills Matter
 
Plug saiku
Skills Matter
 
Huguk lily
Skills Matter
 
Ad

Recently uploaded (20)

PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
Digital Circuits, important subject in CS
contactparinay1
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 

Scala e xchange 2013 haoyi li on metascala a tiny diy jvm

  • 1. Metascala A tiny DIY JVM https://github.com/lihaoyi/Metascala Li Haoyi [email protected] Scala Exchange 2nd Dec 2013
  • 2. Who am I? Li Haoyi Write Python during the day Write Scala at night
  • 3. What is Metascala? ● A JVM ● in 3000 lines of Scala ● Which can load & interpret java programs ● And can interpret itself!
  • 4. Size comparison ● Metascala: ~3,000 lines ● Avian JVM: ~80,000 lines ● OpenJDK: ~1,000,000 lines
  • 5. Basic Usage Create a new metascala VM Plain Old Java Object Captured variables are serialized into VM’s environment Closure’s class file is given to VM to load/parse/execute Result is extracted from VM into host environment No global state Any other classes necessary to evaluate the closure are loaded from the current Classpath
  • 6. It’s Metacircular! Need to give the outer VM more than the 1mb default heap VM inside a VM! Simpler program avoids initializing the scala/java std libraries, which takes forever under double-interpretation. Takes a while (~10s) to produce result
  • 8. Performance Comparison ● OpenJDK: 1.0x ● Metascala: ~100x ● Meta-Metascala: ~10000x
  • 9. Why Metascala? ● Fun to explore the innards of the JVM ● An almost-fully secure Java runtime! ● Small size makes fiddling fun
  • 10. Why Metascala? ● Fun to explore the innards of the JVM ● An almost-fully secure Java runtime! ● Small size makes fiddling fun
  • 11. Quick Tour Immutable Defs: ~380 loc Native Bindings: ~650 loc Bytecode SSA transform: ~650 loc Runtime data structures: 820 loc Binary heap & Copying GC: 132 loc DIY scala-pickling: 132 loc “This is a VM”: 243 loc
  • 12. Quick Tour: Tests Tests for basic Java features GC Fuzz-tests Test Metacircularity! Scala std lib usage
  • 13. What’s a Heap? Fig 1. A Heap
  • 14. What’s a Garbage Collector? Blit (copy) all roots to new heap Stop when you’ve scanned everything Not pseudocode Scan the already copied things for more things and copy them too
  • 15. Why Metascala? ● Fun to explore the innards of the JVM ● An almost-fully secure Java runtime! ● Small size makes fiddling fun
  • 17. And Limited Memory! Not an OOM Error! We throw this ourselves
  • 18. Explicitly defined capabilities Every external call has to be explicitly defined and enabled
  • 19. Security Characteristics ● ● ● ● Finite instruction count Finite memory Well-defined interface to outside world Doesn’t rely on Java Security Model at all! ● Still some holes…
  • 20. Security Holes ● ● ● ● ● Classloader can read from anywhere Time spent classloading not accounted Memory spent on classes not accounted GC time not accounted “native” methods’ time/memory not accounted
  • 21. Basic Problem Outside World User code resource consumption is bounded VM’s runtime resource usage can be made to grow arbitrarily large Classes Native method calls User Code Runtime Data Structures Garbage Collector
  • 22. Possible Solution Put a VM Inside a VM! Works, ... but 10000x slowdown Outside World Fixed Unaccounted Costs Outside World Classes Native method calls User Code Runtime Data Structures Garbage Collector
  • 23. Another Possible Solution Move more components into virtual runtime Difficult to bootstrap correctly WIP Outside World Native method calls Classes Garbage Collector User Code Runtime Data Structures
  • 24. Why Metascala? ● Fun to explore the innards of the JVM ● An almost-fully secure Java runtime! ● Small size makes fiddling fun
  • 26. Ugliness ● Long compile times ● Nasty JVM Interface ● Impossible Debugging
  • 27. Long compile times ● ● 100 lines/s ● Twice as slow (50 lines/s) on my older machine!
  • 28. Nasty JVM Interface Ideal World Initialized User Code Real World Linear Initialization Std Library Initialized User Code Std Library Clean Interfaces VM VM LazyInitialization means repeated dives back into lib/native code Nasty Language VM Interface
  • 29. Java’s dirty little secret The Verbosity of Java with the Safety of C WTF! I’d never use these things!
  • 30. You probably do What happens if you don’t have them Almost every Java program ever uses these things.
  • 31. Next Steps ● Maximize correctness ○ Implement Threads & IO ○ Fix bugs (GC, native calls, etc.) ● Solidify security characteristics ○ Still plenty of unaccounted-for memory/processing ○ Some can be hosted “within” VM itself ● Simplify Std-Lib/VM interface ○ Try using Android Std Lib?
  • 32. Possible Experiments ● Native codegen instead of an interpreter? ○ Generate/exec native code through JNI ○ Heap is already a binary blob that can be easily passed to native code ● Bytecode transforms and optimizations? ○ Already in SSA form ● Continuations, Isolates, Value Classes? ● Port the whole thing to Scala.Js?
  • 33. Metascala: a tiny DIY JVM Ask me about: ● Single Static Assignment form ● Copying Garbage Collection ● sun.misc.Unsafe ● Warts of the .class file format ● Capabilities-based security ● Abandoned approaches