andreabergia / rjvm

A tiny JVM written in Rust. Learning project
Apache License 2.0
1.47k stars 80 forks source link

Unsound VM lifetime #17

Open SpecificProtagonist opened 1 year ago

SpecificProtagonist commented 1 year ago

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:

andreabergia commented 1 year ago

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!

SpecificProtagonist commented 1 year ago

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 ^^