From ff5354b5b596dd936c74bc651ab09afc3ea5a1de Mon Sep 17 00:00:00 2001
From: jD91mZM2 <me@krake.one>
Date: Sat, 18 Jul 2020 15:03:23 +0200
Subject: [PATCH] Fix mmap when using out-of-place address

---
 src/scheme/memory.rs | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/scheme/memory.rs b/src/scheme/memory.rs
index 88f9db3..4bb7506 100644
--- a/src/scheme/memory.rs
+++ b/src/scheme/memory.rs
@@ -73,15 +73,16 @@ impl Scheme for MemoryScheme {
             while i < grants.len()  {
                 let grant = &mut grants[i];
 
-                let mut grant_start = grant.start_address().get();
-                let mut grant_len = ((grant.size() + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE;
-                let mut grant_end = grant_start + grant_len;
+                let grant_start = grant.start_address().get();
+                let grant_len = ((grant.size() + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE;
+                let grant_end = grant_start + grant_len;
 
-                if grant_end <= to_address {
+                if to_address < grant_start || grant_end <= to_address {
                     // grant has nothing to do with the memory to map, and thus we can safely just
                     // go on to the next one.
 
-                    if !fixed {
+                    if grant_start >= crate::USER_GRANT_OFFSET && !fixed {
+                        // don't ignore addresses outside of the automatic grant offset
                         to_address = grant_end;
                     }
                     i += 1;
@@ -96,6 +97,7 @@ impl Scheme for MemoryScheme {
                     // insert a new grant at the end (if not MapFlags::MAP_FIXED).
 
                     if fixed_noreplace {
+                        println!("grant: conflicts with: {:#x} - {:#x}", grant_start, grant_end);
                         return Err(Error::new(EEXIST));
                     } else if fixed {
                         /*
-- 
GitLab