From df77eca00eb972fb80e5108a8f615b7db42dec32 Mon Sep 17 00:00:00 2001 From: itycodes Date: Tue, 5 Nov 2024 10:39:37 +0100 Subject: [PATCH] Fix up drm_version allocations --- src/gpu/i915/mod.rs | 14 +++++++------- src/uapi/i915/mod.rs | 6 +++--- src/uapi/mod.rs | 34 +++++++++++++++++++--------------- tests/tests_uapi.rs | 8 ++++---- 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/gpu/i915/mod.rs b/src/gpu/i915/mod.rs index 290693b..9a39590 100644 --- a/src/gpu/i915/mod.rs +++ b/src/gpu/i915/mod.rs @@ -13,7 +13,7 @@ pub struct DrmDeviceNode { pub path: Option, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct GemHandle<'a> { pub handle: DrmGemHandle, pub node: &'a DrmDeviceNode, @@ -30,7 +30,7 @@ impl GemHandle<'_> { pub fn from_handle(node: &DrmDeviceNode, handle: DrmGemHandle) -> Result { // Avoid invoking GemHandle::is_valid() here to prevent the drop() method from trying to drop an invalid handle - let is_valid = uapi::i915::gem_is_valid(node.fd.as_raw_fd(), handle); + let is_valid = uapi::i915::gem_is_valid(node.fd.as_raw_fd(), &handle); if is_valid.is_ok() && is_valid.unwrap() { let res = GemHandle { @@ -43,23 +43,23 @@ impl GemHandle<'_> { } } - pub fn close(&mut self) -> Result<(), i32> { - return uapi::i915::close_gem(self.node.fd.as_raw_fd(), self.handle); + pub fn close(self) -> Result<(), i32> { + return uapi::i915::close_gem(self.node.fd.as_raw_fd(), self.handle.clone()); } pub fn has_tiling(&self) -> Result { - uapi::i915::gem_has_tiling(self.node.fd.as_raw_fd(), self.handle) + uapi::i915::gem_has_tiling(self.node.fd.as_raw_fd(), &self.handle) } pub fn is_valid(&self) -> Result { - uapi::i915::gem_is_valid(self.node.fd.as_raw_fd(), self.handle) + uapi::i915::gem_is_valid(self.node.fd.as_raw_fd(), &self.handle) } } impl Drop for GemHandle<'_> { fn drop(&mut self) { // Ignoring the close failing as an invalid Gem handle's drop is a no-op - let _ = self.close(); + _ = uapi::i915::close_gem(self.node.fd.as_raw_fd(), self.handle.clone()); } } diff --git a/src/uapi/i915/mod.rs b/src/uapi/i915/mod.rs index 61c4088..7d05da2 100644 --- a/src/uapi/i915/mod.rs +++ b/src/uapi/i915/mod.rs @@ -29,7 +29,7 @@ pub struct EngineInfo { pub capabilities: u64, } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct DrmGemHandle { pub handle: u32, } @@ -201,7 +201,7 @@ pub enum GemIoctlError { Unknown(i32), } -pub fn gem_has_tiling(fd: RawFd, handle: DrmGemHandle) -> Result { +pub fn gem_has_tiling(fd: RawFd, handle: &DrmGemHandle) -> Result { unsafe { let mut tiling = native::drm_i915_gem_get_tiling { handle: handle.handle, @@ -220,7 +220,7 @@ pub fn gem_has_tiling(fd: RawFd, handle: DrmGemHandle) -> Result Result { +pub fn gem_is_valid(fd: RawFd, handle: &DrmGemHandle) -> Result { let res = gem_has_tiling(fd, handle); if res.is_ok() && res.unwrap() { return Ok(true); diff --git a/src/uapi/mod.rs b/src/uapi/mod.rs index 0dc9dc7..dff6635 100644 --- a/src/uapi/mod.rs +++ b/src/uapi/mod.rs @@ -2,6 +2,8 @@ use std::os::fd::RawFd; use std::ffi::{CStr, OsString}; +use std::os::unix::ffi::{OsStrExt, OsStringExt}; +use std::vec; use libc; use super::native; @@ -20,36 +22,38 @@ pub struct DrmVersion { pub fn get_drm_version(fd: RawFd) -> Option { unsafe { - let name_vec = libc::malloc(128); - libc::memset(name_vec, 0, 128); - let date_vec = libc::malloc(128); - libc::memset(date_vec, 0, 128); - let desc_vec = libc::malloc(128); - libc::memset(desc_vec, 0, 128); + let mut name_vec = vec![0 as u8; 128]; + let mut date_vec = vec![0 as u8; 128]; + let mut desc_vec = vec![0 as u8; 128]; let mut version = native::drm_version { version_major: 0, version_minor: 0, version_patchlevel: 0, - name: name_vec as *mut i8, - date: date_vec as *mut i8, - desc: desc_vec as *mut i8, + name: name_vec.as_mut_ptr() as *mut i8, + date: date_vec.as_mut_ptr() as *mut i8, + desc: desc_vec.as_mut_ptr() as *mut i8, name_len: 128, date_len: 128, desc_len: 128, }; let res_val = libc::ioctl(fd, native::DRM_IOCTL_VERSION, &mut version) == 0; let res = if res_val { - let name = CStr::from_ptr(version.name).to_str().expect("Failed to convert name"); - let date = CStr::from_ptr(version.date).to_str().expect("Failed to convert date"); - let desc = CStr::from_ptr(version.desc).to_str().expect("Failed to convert desc"); + name_vec.resize(CStr::from_bytes_until_nul(name_vec.as_slice()).unwrap().to_bytes().len(), 0); + date_vec.resize(CStr::from_bytes_until_nul(date_vec.as_slice()).unwrap().to_bytes().len(), 0); + desc_vec.resize(CStr::from_bytes_until_nul(desc_vec.as_slice()).unwrap().to_bytes().len(), 0); + + let name = OsString::from_vec(name_vec); + let date = OsString::from_vec(date_vec); + let desc = OsString::from_vec(desc_vec); + let drm_version = DrmVersion { version_major: version.version_major, version_minor: version.version_minor, version_patchlevel: version.version_patchlevel, - name: OsString::from(name.to_string()), - date: OsString::from(date.to_string()), - desc: OsString::from(desc.to_string()), + name: name, + date: date, + desc: desc, }; Some(drm_version) } else {None}; diff --git a/tests/tests_uapi.rs b/tests/tests_uapi.rs index 7ae7bba..41b6f2d 100644 --- a/tests/tests_uapi.rs +++ b/tests/tests_uapi.rs @@ -63,9 +63,9 @@ fn test_i915_uapi_gem_lifecycle() { let node = i915::find_node().expect("Failed to find i915 fd"); let gem = i915::make_gem(node.fd.as_raw_fd(), 4096).expect("Failed to make gem"); assert!(gem.handle > 0); - assert!(i915::gem_is_valid(node.fd.as_raw_fd(), gem).unwrap()); - i915::close_gem(node.fd.as_raw_fd(), gem).expect("Failed to close gem"); - assert!(!i915::gem_is_valid(node.fd.as_raw_fd(), gem).unwrap()); + assert!(i915::gem_is_valid(node.fd.as_raw_fd(), &gem).unwrap()); + i915::close_gem(node.fd.as_raw_fd(), gem.clone()).expect("Failed to close gem"); + assert!(!i915::gem_is_valid(node.fd.as_raw_fd(), &gem).unwrap()); let invalid_fd = File::open("/dev/null").expect("Failed to open /dev/null"); assert!(i915::make_gem(invalid_fd.as_raw_fd(), 4096).is_none()); } @@ -75,6 +75,6 @@ fn test_i915_uapi_gem_tiling() { let node = i915::find_node().expect("Failed to find i915 fd"); let gem = i915::make_gem(node.fd.as_raw_fd(), 4096).expect("Failed to make gem"); // TODO figure out which devices this holds for - assert!(i915::gem_has_tiling(node.fd.as_raw_fd(), gem).is_ok_and(|e| e == true)); + assert!(i915::gem_has_tiling(node.fd.as_raw_fd(), &gem).is_ok_and(|e| e == true)); i915::close_gem(node.fd.as_raw_fd(), gem).expect("Failed to close gem"); } \ No newline at end of file