Fix rust unittests when running with asan. There were two issues: * The command-line was not being freed * We were freeing memory with free that was alloced with new. It is preferred to use delete for any memory allocated with new. Bug: 528225104 Change-Id: Iadd110060b0f690cb4f4697d04445af06a6a6964 Reviewed-on: https://gn-review.googlesource.com/c/gn/+/24920 Reviewed-by: Takuto Ikuta <tikuta@google.com> Commit-Queue: Matt Stark <msta@google.com>
diff --git a/src/gn/ffi/bridge.cc b/src/gn/ffi/bridge.cc index 912855c..f119648 100644 --- a/src/gn/ffi/bridge.cc +++ b/src/gn/ffi/bridge.cc
@@ -846,6 +846,11 @@ #endif // CXXBRIDGE1_STRUCT_Session extern "C" { +void cxxbridge1$196$free_vector_buffer(::Any *ptr) noexcept { + void (*free_vector_buffer$)(::Any *) = ::free_vector_buffer; + free_vector_buffer$(ptr); +} + bool cxxbridge1$196$Err$has_error(::Err const &self) noexcept { bool (::Err::*has_error$)() const = &::Err::has_error; return (self.*has_error$)();
diff --git a/src/gn/ffi/scope.cc b/src/gn/ffi/scope.cc index 1816803..f6415bd 100644 --- a/src/gn/ffi/scope.cc +++ b/src/gn/ffi/scope.cc
@@ -85,3 +85,7 @@ Value& SetValue(Scope& scope, rust::Str ident, ParseNodePtr origin) { return *scope.SetValue(std::string_view(ident), Value(), origin.ptr); } + +void free_vector_buffer(Any* ptr) { + ::operator delete(ptr); +}
diff --git a/src/gn/ffi/scope.h b/src/gn/ffi/scope.h index 58a228b..899db8d 100644 --- a/src/gn/ffi/scope.h +++ b/src/gn/ffi/scope.h
@@ -48,4 +48,7 @@ // Adds a value slot to the scope under `ident` and returns a reference to it. Value& SetValue(Scope& scope, rust::Str ident, ParseNodePtr origin); +struct Any; +void free_vector_buffer(Any* ptr); + #endif // TOOLS_GN_FFI_SCOPE_H_
diff --git a/src/gn/starlark/crates/ffi/src/bridge.rs b/src/gn/starlark/crates/ffi/src/bridge.rs index 95fabcd..642dc76 100644 --- a/src/gn/starlark/crates/ffi/src/bridge.rs +++ b/src/gn/starlark/crates/ffi/src/bridge.rs
@@ -69,6 +69,8 @@ include!("gn/test_with_scope.h"); include!("gn/value.h"); + pub unsafe fn free_vector_buffer(ptr: *mut Any); + type Err; pub fn has_error(self: &Err) -> bool; // Dead code for production, used in tests only
diff --git a/src/gn/starlark/crates/ffi/src/slice.rs b/src/gn/starlark/crates/ffi/src/slice.rs index acc9945..f6c7b41 100644 --- a/src/gn/starlark/crates/ffi/src/slice.rs +++ b/src/gn/starlark/crates/ffi/src/slice.rs
@@ -3,7 +3,6 @@ // found in the LICENSE file. use std::{ - ffi::c_void, marker::PhantomData, ops::{Deref, DerefMut}, pin::Pin, @@ -126,15 +125,10 @@ impl<T> Drop for OwnedSlice<T> { #[inline(always)] fn drop(&mut self) { - // We don't write this function, this is the libc free function. - extern "C" { - fn free(ptr: *mut c_void); - } - - // Safety: Calling free is safe. The pointer is guarunteed to be valid and owned - // by us. + // Safety: Calling ffi_free_vector_buffer is safe. The pointer is guaranteed + // to be valid and owned by us. unsafe { - free(self.slice.raw.ptr.cast::<c_void>()); + crate::bridge::free_vector_buffer(self.slice.raw.ptr); } } }
diff --git a/src/gn/starlark/crates/rule/src/frozen_rule.rs b/src/gn/starlark/crates/rule/src/frozen_rule.rs index ee00b07..68222df 100644 --- a/src/gn/starlark/crates/rule/src/frozen_rule.rs +++ b/src/gn/starlark/crates/rule/src/frozen_rule.rs
@@ -144,4 +144,3 @@ write!(f, "<rule: {}>", self.name) } } -