From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by sourceware.org (Postfix) with ESMTPS id 92AB9385E011 for ; Thu, 1 Aug 2024 14:58:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 92AB9385E011 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 92AB9385E011 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::529 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524335; cv=none; b=qzTLImyWXqe2KUUnu+Tm+2GbMICx5oqbGkXqKEPZ6gWzVVdncwdYDOtpgQANPz3zJq/CMQFLJYc8BuaPofK1k7JFQcNp53qwI4LSQugwrl+Vmz4mh4PSJ2r98gQorIXBtspkjMez0LMEeAQdynC/7v9qIxQOR9d/4kZLX6duNt4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524335; c=relaxed/simple; bh=gdhdBSiGoaOUVIHjxWK7FOzywKNVyNWiEZQRkWrpQJw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=LCqmLpzPBJ3rVyyP4uMzqfvyknPljQ08AS8Zil+qPvF2erH+B1lbwjJ/u5HQvBXBZFT+pKxQE6Y/88LYZ1xDCm98GvujOJhQ8/of94isz2kPFzU0nlRc3nb4nC3GbMlDKPMfS4AmAfk0F5jSuJACZTl9SCLOEBg8h1B65EGQuwU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ed1-x529.google.com with SMTP id 4fb4d7f45d1cf-5a2ffc34431so5445666a12.0 for ; Thu, 01 Aug 2024 07:58:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1722524322; x=1723129122; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kn49CA4XJQhtv5cfGvpuaNyMJn61Ys4JrJlDxsXyoog=; b=CvwDbLaWIO3NSUEtiTVhey3B54mZT2HbYdyL2OcEk8kvvo/wiy+huR6WuxHRxvTng/ uPGP5Iri03vSX+l6WZmGVTLhJdvKnLb5zlF04cr78apRgZGslRi9tyhrCY7VzKsHUztI xWWTG3sPz4qtncFZ1H6tvctVit5Qth7QDpPzlajb37PGRYbXgaRX/xRfEhZ3t/FaD0p4 5l5NhtDyZEmlhxSzkUycyN/hoj4ve6YYHQbIfHCLyKklnOjj9FdxDBmpH4xM4Zhl1iro OG5Zjbtdp2+ksb5EgzHM9St588fTt4bdkynitsDWKi9hLlj2UtgR77X8teWKhc7s8GX9 ZSbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722524322; x=1723129122; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kn49CA4XJQhtv5cfGvpuaNyMJn61Ys4JrJlDxsXyoog=; b=RrQ2TdgfILNipIvUump+QGXg25hkXjHGXcGTTpe4AjN4kWD5QG6d1krACR8oe6gEln edDawT5il7jHZhgf2TMEcAsSdJirC/en42fyGrZ/qVjjVCFFSiFDqe94xcUONLAvuXyY 0gydo/352pp7J3apx1l+j9qZi48JxTqON921E9a3QNaMF8+CKA145XRdLYpS9JnTyBFz yI6utpz0WtNWwbP/L+rzihC7Lb9MGAsGy9ZoXR3fxGUgYsCvFYQTjRmBdwCxUSsI9Fu9 40BK76qdiQmZLMX4N2boLB9EfWc3DDapJ+crAnPWnerJlCGwzvvvRhaFZ2UXdmwxrxiO dyKQ== X-Gm-Message-State: AOJu0Yyq3ZXQd44C6+Zkm/5Jmd7jkcDgNucKHZbexK5H6F9Ko2sfU83j lkfLLkJqiIFi4cq48q0fy+kIXV05RAlnGBrStpN0Prg5i1rWkMxDNvNUk5usja87HVK4hFE9gkv iATAZ X-Google-Smtp-Source: AGHT+IHoJDVafXUPOsr0BgmN7Do7+A3s1qLTlc/SkPk2K1dlsBC6YRWRdTa9y6jIH3TgrG1aAu55dw== X-Received: by 2002:a05:6402:6c4:b0:5a1:71b2:e9bd with SMTP id 4fb4d7f45d1cf-5b7f57f3ac0mr475780a12.34.1722524321656; Thu, 01 Aug 2024 07:58:41 -0700 (PDT) Received: from platypus.lan ([2a04:cec2:9:dc84:3622:6733:ff49:ee91]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5ac63590592sm10252456a12.25.2024.08.01.07.58.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Aug 2024 07:58:41 -0700 (PDT) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [PATCH 014/125] gccrs: libformat_parser: Send boxed values across FFI properly Date: Thu, 1 Aug 2024 16:56:10 +0200 Message-ID: <20240801145809.366388-16-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240801145809.366388-2-arthur.cohen@embecosm.com> References: <20240801145809.366388-2-arthur.cohen@embecosm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-14.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: gcc/rust/ChangeLog: * ast/rust-fmt.cc (Pieces::~Pieces): Call libformat_parser's release function in destructor. * ast/rust-fmt.h (struct PieceSlice): Add capacity. (destroy_pieces): New. (struct Pieces): Add destructor. libgrust/ChangeLog: * libformat_parser/src/lib.rs: Leak Boxes properly for C++ to see them, add memory release function. --- gcc/rust/ast/rust-fmt.cc | 4 +- gcc/rust/ast/rust-fmt.h | 9 +++ libgrust/libformat_parser/src/lib.rs | 94 ++++++++++++++-------------- 3 files changed, 58 insertions(+), 49 deletions(-) diff --git a/gcc/rust/ast/rust-fmt.cc b/gcc/rust/ast/rust-fmt.cc index a7c4341c52d..f6ee8a20913 100644 --- a/gcc/rust/ast/rust-fmt.cc +++ b/gcc/rust/ast/rust-fmt.cc @@ -34,8 +34,10 @@ Pieces::collect (const std::string &to_parse) // auto pieces = std::vector (piece_slice.base_ptr, // piece_slice.base_ptr + piece_slice.len); - return Pieces{}; + return Pieces (piece_slice); } +Pieces::~Pieces () { destroy_pieces (slice); } + } // namespace Fmt } // namespace Rust diff --git a/gcc/rust/ast/rust-fmt.h b/gcc/rust/ast/rust-fmt.h index 7ec9a2a199d..50aeff6433e 100644 --- a/gcc/rust/ast/rust-fmt.h +++ b/gcc/rust/ast/rust-fmt.h @@ -237,6 +237,7 @@ struct PieceSlice { const Piece *base_ptr; size_t len; + size_t cap; }; extern "C" { @@ -244,11 +245,19 @@ extern "C" { PieceSlice collect_pieces (const char *input); +void destroy_pieces (PieceSlice); + } // extern "C" struct Pieces { static Pieces collect (const std::string &to_parse); + ~Pieces (); + +private: + Pieces (PieceSlice slice) : slice (slice) {} + + PieceSlice slice; }; } // namespace Fmt diff --git a/libgrust/libformat_parser/src/lib.rs b/libgrust/libformat_parser/src/lib.rs index 4bbc468c755..9b2bffed05d 100644 --- a/libgrust/libformat_parser/src/lib.rs +++ b/libgrust/libformat_parser/src/lib.rs @@ -3,21 +3,17 @@ // what's the plan? Have a function return something that can be constructed into a vector? // or an iterator? -use std::ffi::CStr; +use std::{ffi::CStr, mem}; -trait IntoFFI { - type Output; - - fn into_ffi(&self) -> Self::Output; +trait IntoFFI { + fn into_ffi(self) -> T; } -impl IntoFFI for Option +impl IntoFFI<*const T> for Option where T: Sized, { - type Output = *const T; - - fn into_ffi(&self) -> Self::Output { + fn into_ffi(self) -> *const T { match self.as_ref() { None => std::ptr::null(), Some(r) => r as *const T, @@ -40,12 +36,6 @@ mod ffi { pub end: usize, } - // impl InnerSpan { - // pub fn new(start: usize, end: usize) -> InnerSpan { - // InnerSpan { start, end } - // } - // } - /// The location and before/after width of a character whose width has changed from its source code /// representation #[derive(Copy, Clone, PartialEq, Eq)] @@ -59,35 +49,27 @@ mod ffi { pub after: usize, } - // impl InnerWidthMapping { - // pub fn new(position: usize, before: usize, after: usize) -> InnerWidthMapping { - // InnerWidthMapping { - // position, - // before, - // after, - // } - // } + // TODO: Not needed for now? + // /// Whether the input string is a literal. If yes, it contains the inner width mappings. + // #[derive(Clone, PartialEq, Eq)] + // #[repr(C)] + // enum InputStringKind { + // NotALiteral, + // Literal { + // width_mappings: Vec, + // }, // } - /// Whether the input string is a literal. If yes, it contains the inner width mappings. - #[derive(Clone, PartialEq, Eq)] - #[repr(C)] - enum InputStringKind { - NotALiteral, - Literal { - width_mappings: Vec, - }, - } - - /// The type of format string that we are parsing. - #[derive(Copy, Clone, Debug, Eq, PartialEq)] - #[repr(C)] - pub enum ParseMode { - /// A normal format string as per `format_args!`. - Format, - /// An inline assembly template string for `asm!`. - InlineAsm, - } + // TODO: Not needed for now? + // /// The type of format string that we are parsing. + // #[derive(Copy, Clone, Debug, Eq, PartialEq)] + // #[repr(C)] + // pub enum ParseMode { + // /// A normal format string as per `format_args!`. + // Format, + // /// An inline assembly template string for `asm!`. + // InlineAsm, + // } #[derive(Copy, Clone)] #[repr(C)] @@ -102,7 +84,13 @@ mod ffi { String(&'a str), /// This describes that formatting should process the next argument (as /// specified inside) for emission. - NextArgument(Box>), + NextArgument(*const Argument<'a>), + } + + impl<'a> Drop for Piece<'a> { + fn drop(&mut self) { + println!("dropping Piece: {:?}", self) + } } /// Representation of an argument specification. @@ -225,7 +213,10 @@ mod ffi { // a memory leak... should we resend the info back to the Rust lib afterwards to free it? // this is definitely the best way - store that pointer in the FFI piece and rebuild the box // in a Rust destructor - Piece::NextArgument(Box::new(Into::::into(*x))) + let ptr = Box::leak(x); + let dst = Into::::into(*ptr); + + Piece::NextArgument(&dst as *const Argument) } } } @@ -331,17 +322,18 @@ pub mod rust { use generic_format_parser::{ParseMode, Parser, Piece}; pub fn collect_pieces(input: &str) -> Vec> { - // let parser = Parser::new(); let parser = Parser::new(input, None, None, true, ParseMode::Format); parser.into_iter().collect() } } +// TODO: Should we instead make an FFIVector struct? #[repr(C)] pub struct PieceSlice { - base_ptr: *const ffi::Piece<'static /* FIXME: That's wrong */>, + base_ptr: *mut ffi::Piece<'static /* FIXME: That's wrong */>, len: usize, + cap: usize, } #[no_mangle] @@ -355,10 +347,16 @@ pub extern "C" fn collect_pieces(input: *const libc::c_char) -> PieceSlice { .map(Into::into) .collect(); - println!("debug: {:?}, {:?}", pieces.as_ptr(), pieces.len()); + println!("[ARTHUR]: debug: {:?}, {:?}", pieces.as_ptr(), pieces.len()); PieceSlice { - base_ptr: pieces.as_ptr(), len: pieces.len(), + cap: pieces.capacity(), + base_ptr: pieces.leak().as_mut_ptr(), } } + +#[no_mangle] +pub extern "C" fn destroy_pieces(PieceSlice { base_ptr, len, cap }: PieceSlice) { + let _ = unsafe { Vec::from_raw_parts(base_ptr, len, cap) }; +} -- 2.45.2