andreabergia/rjvm

Unsound VM lifetime

SpecificProtagonist opened this issue · 2 comments

VM has a lifetime 'a. This lifetime is arbitrary – it is chosen by the caller with no restrictions. This means it can outlive the VM, but other structs assume the VM is life for 'a. This is unsound.

Segfaulting example:

    let mut vm = create_base_vm(DEFAULT_MAX_MEMORY);
    let call_stack = vm.allocate_call_stack();
    let main_method = vm
        .resolve_class_method(
            call_stack,
            "rjvm/SimpleMain",
            "main",
            "([Ljava/lang/String;)V",
        )
        .expect("should find main method");
    drop(vm);
    println!("{main_method:?}");

Possible solutions:

  • Remove lifetime from VM. All methods that produce lifetimed objects take their lifetime from the reference to the VM.
  • Remove lifetime from VM. Keep VM in an Arc. Erase lifetimes internally. Don't hand out structs referencing VM memory directly, instead hand out a wrapper object that also contains a copy of the Arc (this can be transparent to the user).

I don't think I am going to fix this honestly, it sounds too much work and I consider the project "finished" (in the sense that I learnt what I had in mind initially and I want to move on to other things).

In any case, solution 1 is pretty clear to me.

I am a bit uncertain on solution 2: what do you mean with erase lifetimes internally? Can you point out to any example or just sketch a few lines of code?

Thanks a lot!

Here's an example. I don't currently do the Arc thing though; the project is pretty unfinished (so don't judge it too harshly :3).

Happy to help ^^