gluon-lang / gluon

A static, type inferred and embeddable language written in Rust.
https://gluon-lang.org
MIT License
3.16k stars 145 forks source link

VM - Lifetimes in `DeSeed::new` allow aliased mutable references #933

Closed vikramnitin9 closed 1 year ago

vikramnitin9 commented 1 year ago

At this line in the vm, DeSeed::new takes a borrow to Thread with an implicit lifetime, and returns a structure with lifetime parameter 'gc.

pub fn new(thread: &Thread, context: &'gc mut ActiveThread) -> DeSeed<'gc> {

This allows us to create two mutable references to the same thread, as follows :

use gluon_vm;
use gluon_vm::serialization::DeSeed;
use gluon_vm::thread::{Thread, RootedThread};

fn main() {
    let mut thread = RootedThread::new();
    let mut dummy_thread = RootedThread::new();
    let mut context = dummy_thread.current_context();

    let mut deseed = DeSeed::new(&thread, &mut context);

    let thread_ref = &mut thread;
    let thread_ref2 = &mut deseed.thread;

    println!("{:p} {:p}", thread_ref, thread_ref2); // Same address
}

The lifetime of the borrow to Thread should also be 'gc.

&'gc Thread would fix this.

Marwes commented 1 year ago

I don't see the problem, running that code I get different addresses, which is what I would expect since deseed.thread is a cloned RootedThread. RootedThread acts like a reference count pointer (think Arc) so having multiple of them are fine.

Closing but feel free to open if you can demonstrate an issue more clearly.