Skip to content

Commit 1b2016b

Browse files
andreeaflorescualxiord
authored andcommitted
implement Drop for KvmRunWrapper
In KvmRunWrapper we are mmaping memory for the kvm_run wrapper. The memory needs to be unmapped before dropping the reference to KvmRunWrapper. Signed-off-by: Andreea Florescu <[email protected]>
1 parent b7e33cf commit 1b2016b

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

src/ioctls/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ impl CpuId {
223223
/// threads as raw pointers do not implement `Send` and `Sync`.
224224
pub struct KvmRunWrapper {
225225
kvm_run_ptr: *mut u8,
226+
// This field is need so we can `munmap` the memory mapped to hold `kvm_run`.
227+
mmap_size: usize,
226228
}
227229

228230
// Send and Sync aren't automatically inherited for the raw address pointer.
@@ -257,6 +259,7 @@ impl KvmRunWrapper {
257259

258260
Ok(KvmRunWrapper {
259261
kvm_run_ptr: addr as *mut u8,
262+
mmap_size: size,
260263
})
261264
}
262265

@@ -273,6 +276,16 @@ impl KvmRunWrapper {
273276
}
274277
}
275278

279+
impl Drop for KvmRunWrapper {
280+
fn drop(&mut self) {
281+
// This is safe because we mmap the area at kvm_run_ptr ourselves,
282+
// and nobody else is holding a reference to it.
283+
unsafe {
284+
libc::munmap(self.kvm_run_ptr as *mut libc::c_void, self.mmap_size);
285+
}
286+
}
287+
}
288+
276289
#[cfg(test)]
277290
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
278291
mod tests {

src/ioctls/vcpu.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,7 @@ mod tests {
10421042
vcpu: unsafe { File::from_raw_fd(-1) },
10431043
kvm_run_ptr: KvmRunWrapper {
10441044
kvm_run_ptr: mmap_anonymous(10),
1045+
mmap_size: 10,
10451046
},
10461047
};
10471048

tests/coverage

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
90.9
1+
91

0 commit comments

Comments
 (0)