robert-w-gries / rxinu

Rust implementation of Xinu educational operating system
Apache License 2.0
33 stars 4 forks source link

Using dynamic dispatch on DoesScheduling trait object causes invalid memory reference in rustc #39

Closed robert-w-gries closed 6 years ago

robert-w-gries commented 6 years ago

I wanted to change the dynamic dispatch in process_ret() to use a DoesScheduling trait object instead of assuming the scheduler is the CoopScheduler type.

diff --git a/src/scheduling/cooperative_scheduler.rs b/src/scheduling/cooperative_scheduler.rs
index d65b2ab..4a1fbce 100644
--- a/src/scheduling/cooperative_scheduler.rs
+++ b/src/scheduling/cooperative_scheduler.rs
@@ -34,7 +34,7 @@ impl DoesScheduling for CoopScheduler {
         let stack_values: Vec<usize> = vec![
             new_proc as usize,
             process::process_ret as usize,
-            self as *const Scheduler as usize,
+            self as *const DoesScheduling as *const usize as usize,
         ];

         for (i, val) in stack_values.iter().enumerate() {
diff --git a/src/scheduling/process.rs b/src/scheduling/process.rs
index 60189a8..2077a17 100644
--- a/src/scheduling/process.rs
+++ b/src/scheduling/process.rs
@@ -98,7 +98,7 @@ impl Process {
 pub unsafe extern "C" fn process_ret() {
     use scheduling::{DoesScheduling, Scheduler};

-    let scheduler: &mut Scheduler;
+    let scheduler: &DoesScheduling;
     asm!("pop $0" : "=r"(scheduler) : : "memory" : "intel", "volatile");

     let curr_id: ProcessId = scheduler.getid();

However, running this code results in a rustc crash due to an invalid memory reference:

error: Could not compile `rxinu`.

Caused by:
  process didn't exit successfully: `rustc --crate-name rxinu src/lib.rs --crate-type staticlib --emit=dep-info,link -C debuginfo=2 --cfg feature="default" --cfg feature="serial" -C metadata=31a0723d36405193 -C extra-filename=-31a0723d36405193 --out-dir /home/rob/rxinu/target/x86_64-rxinu/debug/deps --target x86_64-rxinu -L dependency=/home/rob/rxinu/target/x86_64-rxinu/debug/deps -L dependency=/home/rob/rxinu/target/debug/deps --extern volatile=/home/rob/rxinu/target/x86_64-rxinu/debug/deps/libvolatile-39e0b219e05681b4.rlib --extern x86=/home/rob/rxinu/target/x86_64-rxinu/debug/deps/libx86-455753b7dd6b85e3.rlib --extern bitflags=/home/rob/rxinu/target/x86_64-rxinu/debug/deps/libbitflags-6b084702002cf111.rlib --extern spin=/home/rob/rxinu/target/x86_64-rxinu/debug/deps/libspin-9bd689ac3bbcfdfa.rlib --extern bit_field=/home/rob/rxinu/target/x86_64-rxinu/debug/deps/libbit_field-677c54a61c03a33f.rlib --extern linked_list_allocator=/home/rob/rxinu/target/x86_64-rxinu/debug/deps/liblinked_list_allocator-ef23ed764b8b68b8.rlib --extern rlibc=/home/rob/rxinu/target/x86_64-rxinu/debug/deps/librlibc-8eabc116dc6e8246.rlib --extern once=/home/rob/rxinu/target/x86_64-rxinu/debug/deps/libonce-2c4225e39ad50031.rlib --extern multiboot2=/home/rob/rxinu/target/x86_64-rxinu/debug/deps/libmultiboot2-542aac65ec57e3e0.rlib --extern lazy_static=/home/rob/rxinu/target/x86_64-rxinu/debug/deps/liblazy_static-870f43b7a19a5d5a.rlib --sysroot /home/rob/.xargo` (signal: 11, SIGSEGV: invalid memory reference)
make: *** [Makefile:71: cargo] Error 101
robert-w-gries commented 6 years ago

Tracking rust-lang/rust#46867