Skip to content

Commit

Permalink
Linux: util: Free BSTR after conversion to String
Browse files Browse the repository at this point in the history
The same call to SysFreeString happens in the Windows counterpart.

Solves the following leaks found by valgrind:

    ==213075== 40 bytes in 1 blocks are definitely lost in loss record 4 of 13
    ==213075==    at 0x483A77F: malloc (vg_replace_malloc.c:307)
    ==213075==    by 0x5B03395: ???
    ==213075==    by 0x5AFC77E: ???
    ==213075==    by 0x12C766: hassle_rs::intellisense::ffi::IDxcCursor::get_display_name (macros.rs:108)
    ==213075==    by 0x116373: hassle_rs::intellisense::wrapper::DxcCursor::get_display_name (wrapper.rs:236)
    ==213075==    by 0x11286D: intellisense_tu::main (intellisense-tu.rs:33)
    ==213075==    by 0x11371A: core::ops::function::FnOnce::call_once (function.rs:227)
    ==213075==    by 0x11348D: std::sys_common::backtrace::__rust_begin_short_backtrace (backtrace.rs:137)
    ==213075==    by 0x111E10: std::rt::lang_start::{{closure}} (rt.rs:66)
    ==213075==    by 0x150A20: call_once<(),Fn<()>> (function.rs:259)
    ==213075==    by 0x150A20: do_call<&Fn<()>,i32> (panicking.rs:373)
    ==213075==    by 0x150A20: try<i32,&Fn<()>> (panicking.rs:337)
    ==213075==    by 0x150A20: catch_unwind<&Fn<()>,i32> (panic.rs:379)
    ==213075==    by 0x150A20: std::rt::lang_start_internal (rt.rs:51)
    ==213075==    by 0x111DE6: std::rt::lang_start (rt.rs:65)
    ==213075==
    ==213075== 56 (16 direct, 40 indirect) bytes in 1 blocks are definitely lost in loss record 5 of 13
    ==213075==    at 0x483ADEF: operator new(unsigned long) (vg_replace_malloc.c:342)
    ==213075==    by 0x4FD058D: ???
    ==213075==    by 0x4FC04D9: ???
    ==213075==    by 0x40112DD: call_init.part.0 (in /usr/lib/ld-2.32.so)
    ==213075==    by 0x40113C7: _dl_init (in /usr/lib/ld-2.32.so)
    ==213075==    by 0x4A100E4: _dl_catch_exception (in /usr/lib/libc-2.32.so)
    ==213075==    by 0x4015704: dl_open_worker (in /usr/lib/ld-2.32.so)
    ==213075==    by 0x4A10087: _dl_catch_exception (in /usr/lib/libc-2.32.so)
    ==213075==    by 0x4014F3D: _dl_open (in /usr/lib/ld-2.32.so)
    ==213075==    by 0x489434B: ??? (in /usr/lib/libdl-2.32.so)
    ==213075==    by 0x4A10087: _dl_catch_exception (in /usr/lib/libc-2.32.so)
    ==213075==    by 0x4A10152: _dl_catch_error (in /usr/lib/libc-2.32.so)
    ==213075==
    ==213075== 124 bytes in 3 blocks are definitely lost in loss record 9 of 13
    ==213075==    at 0x483A77F: malloc (vg_replace_malloc.c:307)
    ==213075==    by 0x5B03395: ???
    ==213075==    by 0x5AFC77E: ???
    ==213075==    by 0x12C766: hassle_rs::intellisense::ffi::IDxcCursor::get_display_name (macros.rs:108)
    ==213075==    by 0x116373: hassle_rs::intellisense::wrapper::DxcCursor::get_display_name (wrapper.rs:236)
    ==213075==    by 0x112F63: intellisense_tu::main (intellisense-tu.rs:52)
    ==213075==    by 0x11371A: core::ops::function::FnOnce::call_once (function.rs:227)
    ==213075==    by 0x11348D: std::sys_common::backtrace::__rust_begin_short_backtrace (backtrace.rs:137)
    ==213075==    by 0x111E10: std::rt::lang_start::{{closure}} (rt.rs:66)
    ==213075==    by 0x150A20: call_once<(),Fn<()>> (function.rs:259)
    ==213075==    by 0x150A20: do_call<&Fn<()>,i32> (panicking.rs:373)
    ==213075==    by 0x150A20: try<i32,&Fn<()>> (panicking.rs:337)
    ==213075==    by 0x150A20: catch_unwind<&Fn<()>,i32> (panic.rs:379)
    ==213075==    by 0x150A20: std::rt::lang_start_internal (rt.rs:51)
    ==213075==    by 0x111DE6: std::rt::lang_start (rt.rs:65)

Fixes: 94fbad7 ("Support libdxcompiler.so on Linux (#5)")
  • Loading branch information
MarijnS95 committed Nov 10, 2020
1 parent 75eb94f commit aeba1b6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod os_defs {
};

pub use winapi::um::combaseapi::CoTaskMemFree;
pub use winapi::um::oleauto::SysFreeString;
}

#[cfg(not(windows))]
Expand All @@ -26,6 +27,12 @@ mod os_defs {
// https://github.com/microsoft/DirectXShaderCompiler/blob/a8d9780046cb64a1cea842fa6fc28a250e3e2c09/include/dxc/Support/WinAdapter.h#L46
libc::free(p)
}

#[allow(non_snake_case)]
pub unsafe fn SysFreeString(p: BSTR) {
// https://github.com/microsoft/DirectXShaderCompiler/blob/a8d9780046cb64a1cea842fa6fc28a250e3e2c09/include/dxc/Support/WinAdapter.h#L48-L50
libc::free(p as _)
}
}

pub use os_defs::*;
8 changes: 5 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::os::{BSTR, HRESULT, LPSTR, LPWSTR, WCHAR};
use crate::os::{SysFreeString, BSTR, HRESULT, LPSTR, LPWSTR, WCHAR};
use crate::wrapper::*;
use thiserror::Error;

#[cfg(windows)]
use winapi::um::oleauto::{SysFreeString, SysStringLen};
use winapi::um::oleauto::SysStringLen;

pub(crate) fn to_wide(msg: &str) -> Vec<WCHAR> {
widestring::WideCString::from_str(msg).unwrap().into_vec()
Expand Down Expand Up @@ -33,7 +33,9 @@ pub(crate) fn from_bstr(string: BSTR) -> String {
// TODO (Marijn): This does NOT cover embedded NULLs:
// https://docs.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-sysstringlen#remarks
// Fortunately BSTRs are only used in names currently, which _likely_ don't include NULL characters (like binary data)
from_lpstr(string as LPSTR)
let result = from_lpstr(string as LPSTR);
unsafe { SysFreeString(string) };
result
}

pub(crate) fn from_lpstr(string: LPSTR) -> String {
Expand Down

0 comments on commit aeba1b6

Please sign in to comment.