Rust Java class file parser

Posted on Mar 5, 2016

Rust is all the rage these days and it seems that barely a day goes by without a post on Hacker News extolling the virtues of Rust: blazingly fast, prevents segfaults, and guarantees thread safety.

After what feels like decades of reading all this hype I figured it would be a good idea to jump aboard that hype train and try my own hand at making something with Rust that would take advantage of the features Rust gives you as a developer.

When it comes to the type of software I work on, either for my day job or hobby coding I don’t think I’d put any of it in the “systems programming” category where Rust seems to fill a niche. None of my existing projects would make sense to port so instead I decided to try and push my boundaries and dip a toe into the lower level software development world.

Rust and java logos

As I’m mainly a Java developer, and a Java developer who’s spent quite some time learning how the JVM works internally, I thought it could be a fun project to attempt to write my own JVM. Thankfully for me the JVM has a very detailed and open specification which makes it much less daunting to start than some may think.

One of the first things I thought a JVM should do is parse the Class file format as a JVM isn’t much use unless it can load some Java Bytecode to run.

I initially prototyped a naive class file parser, opening the file and reading byte by byte and switching what it did depending on what it read. While mostly functional it was obviously not a suitable approach for parsing the whole format and definitely not the “Rust” way to do it.

A little bit of research later I found a library called nom whose website describes it as “a parser combinator library with a focus on safe parsing, streaming patterns, and as much as possible zero copy” which seemed to be just what I needed.

After a few days of hacking about it actually started to form together fairly well. While there is plenty of class file features I’m still yet to implement in my parser I got it to a semi-decent state to start working on other parts.

My brief forray into Rust wasn’t too bad. It’s definitely more involved than other languages I typically use and I found the markup for lifetimes to be fairly jarring at first, making fully grokking the concept a bit more difficult.

Unfortunately the class file parser is not complete, the readme file in the repo has a list of all the main features to parse with tickboxes for parts I’ve written a parser for. Hopefully soon I’ll make some time to fully focus on this project and get it finished as the different programming style needed for a project like this, coupled with learning a new language, makes this not the kind of project I can dip in and out of.

If you’re looking for a partial Java class file parser written in Rust using nom however, feel free to check my repo out: https://github.com/Palmr/classfile-parser

And if you want to finish it off for me and send me a pull request, definitely feel free!