Can JVM bytecode running on the GraalVM be instrumented by a custom `TruffleInstrument`?

Tags: , , , ,



I would like to write a Truffle instrument which can instrument Java programs
(or at least JVM bytecode programs) when they are run on GraalVM.

I have been investigating different ways to perform fine-grained
instrumentation of Java programs to support a runtime verification project.
To me, manipulating Truffle AST nodes sounds much more attractive than the
more traditional Java instrumentation techniques that I am familiar with
(e.g. bytecode rewriting, JVMTI).

Unfortunately, I am starting to worry that it isn’t possible to make a Truffle
instrument even “see” JVM bytecode. From my experiments with the GraalVM
launchers (e.g. polyglot and java), experiments with the Truffle API (e.g.
org.graalvm.polyglot.Context),
and skimming some Graal compiler source code, it seems that:

  • Truffle instruments can only “see” code which can be interpreted by a Truffle
    language (e.g. js or llvm).
  • The GraalVM distribution doesn’t support JVM bytecode via a Truffle language.
    (Rather, it supports JVM bytecode via the traditional HotSpot interpreter and
    the Graal JVMCI Compiler, which has nothing to do with Truffle.)

I’ve created a GitHub repo, dwtj/ex_graalvm_with_custom_truffle
which demonstrates some of the experiments which led me to believe this. In
particular, both scripts 10 and 11 seem to demonstrate that the Java called from
JavaScript is simply ignored by GraalVM’s simpletool.

Now, maybe this isn’t a problem with Truffle instruments in general. Maybe this
is just a limitation with this particular Truffle instrument. Is there some way
that a Truffle instrument like simpletool can be enhanced to support JVM
bytecode instrumentation using the standard Truffle API?

I’d really like to use GraalVM’s Truffle API to instrument JVM bytecode. So
please tell me I’m wrong.

Answer

Truffle instruments can only “see” code which can be interpreted by a Truffle language (e.g. js or llvm).

This is exactly correct, and JVM bytecode is currently not a Truffle language i.e. You cannot use Truffle Instrumentation for java at the moment.

However, There is work in progress at the Oracle Labs on implementing a Java bytecode interpreter as a Truffle language (code name espresso) that should be available soon.



Source: stackoverflow