andreabergia / rjvm

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

API allows using data managed by one VM with another VM (unsound) #18

Open SpecificProtagonist opened 1 year ago

SpecificProtagonist commented 1 year ago

The API allows data (CallStack, AbstractObject, ClassRefm …) managed by one VM to be used with another VM. This allows e.g. storing a pointer to an object on the heap of vm1 in a static field in the heap of vm2. If a garbage collection then triggers on vm1, the static field on vm2 now points to cleared/invalid data.

andreabergia commented 1 year ago

That is a good observation, however I am unsure how to fix it. If you have any ideas on how to do it, I would love the guidance. :-)

More precisely, I have some idea on how to fix it at runtime, but is there any way to fix it at compile time? If I changed the Vm definition so that it does not have a lifetime, but each method returns data that is bound to the lifetime of the Vm, for example:

impl Vm {
      pub <'a> fn allocate_call_stack(&'a mut self) -> &'a mut CallStack<'a>
}

that still would not fix the issue, no?

SpecificProtagonist commented 1 year ago

Yep, even with closure trickery you can't limit a lifetime to a single object, and even if you'd try to do something with a const generic parameter, different VMs having different types would limit what you could do with them (e.g. can't them into a collection). So runtime it is (or alternatively making the VM a singleton :/ ).