From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4E38E185B7A; Wed, 11 Sep 2024 12:31:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726057886; cv=none; b=lNF371zEOiBS/a3UwYA24px8jnyTheSLl9ZTDlkRkcz9FZv3BPb+WE3Orfo7U4l+Xy89cZ2AgkcNoXjuLZtvpcnuAj+bZEXgcLlCL4lf2g1l/DwOCfvwmSYCcxOJyb2NUw7RHXOHtU59y04fEf2zWYzYWYxuIlzCqTEYhBrGbvg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726057886; c=relaxed/simple; bh=eU3oOgqP7mozRvjpQC5rfw+p/h07bXakoBco4xUGrec=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ruCPtIe3XNrL9rwGHlI67L5lk2nN7hovMBrUwaAzWq/6UNiDsW8h328SM/nYNwhlDGnXYTnpwDODfHUhWXOj4xotOiSmoh82mg5tXUg8X1hSe7E5sWcwEqyTZhsPV5ZJWd+N5RwymtF6HQN86RzTFl/V0k+f8uIa7U61QZgrrtU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=T4A6vh6c; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="T4A6vh6c" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 27802C4CEC5; Wed, 11 Sep 2024 12:31:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726057883; bh=eU3oOgqP7mozRvjpQC5rfw+p/h07bXakoBco4xUGrec=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=T4A6vh6cbK6ZufKbZhNi3k1K95qYH460093V3SDwP3w3NF0b1mDzp9VAZ1trcsTaf HPYAU/BozaKvBziW+zGOBpW8jWVHUzaUILaPCAFaWiUwMlRnYSfMrYSBVopGX0WKka oDG2/p0IJAZuyOy1jiJWFNNy7f85NoRWCDus+XlOiqLkwpalyOsz6deVpSKVRDah39 SpekqTn+vU3RCz0IjjNRDzSNc7p3HrP+Z8dQK8h6ySFLB6yLW3t0BmGig7p54AqLLi fz5pGEMUZ0KCNRudvLF8WbmUscL/Ig2eHHhGDIvbLxNaIqQr80RuExsjsWDVcF4Wjc vNv9Cp51/3Dpw== Date: Wed, 11 Sep 2024 14:31:16 +0200 From: Danilo Krummrich To: Benno Lossin Cc: ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, a.hindborg@samsung.com, aliceryhl@google.com, akpm@linux-foundation.org, daniel.almeida@collabora.com, faith.ekstrand@collabora.com, boris.brezillon@collabora.com, lina@asahilina.net, mcanal@igalia.com, zhiw@nvidia.com, cjia@nvidia.com, jhubbard@nvidia.com, airlied@redhat.com, ajanulgu@redhat.com, lyude@redhat.com, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH v6 22/26] rust: alloc: implement `Cmalloc` in module allocator_test Message-ID: References: <20240816001216.26575-1-dakr@kernel.org> <20240816001216.26575-23-dakr@kernel.org> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Fri, Aug 30, 2024 at 12:25:27AM +0200, Danilo Krummrich wrote: > On Thu, Aug 29, 2024 at 07:14:18PM +0000, Benno Lossin wrote: > > On 16.08.24 02:11, Danilo Krummrich wrote: > > > > > + > > > + if layout.size() == 0 { > > > + // SAFETY: `src` has been created by `Self::alloc_store_data`. > > > > This is not true, consider: > > > > let ptr = alloc(size = 0); > > free(ptr) > > > > Alloc will return a dangling pointer due to the first if statement and > > then this function will pass it to `free_read_data`, even though it > > wasn't created by `alloc_store_data`. > > This isn't forbidden by the `Allocator` trait function's safety > > requirements. > > > > > + unsafe { Self::free_read_data(src) }; > > > + > > > + return Ok(NonNull::slice_from_raw_parts(NonNull::dangling(), 0)); > > > + } > > > + > > > + let dst = Self::alloc(layout, flags)?; > > > + > > > + // SAFETY: `src` has been created by `Self::alloc_store_data`. > > > + let data = unsafe { Self::data(src) }; > > > > Same issue here, if the allocation passed in is zero size. I think you > > have no other choice than to allocate even for zero size requests... > > Otherwise how would you know that they are zero-sized. > > Good catch - gonna fix it. Almost got me. :) I think the code is fine, callers are not allowed to pass pointers to `realloc` and `free`, which haven't been allocated with the same corresponding allocator or are dangling. > > > > > --- > > Cheers, > > Benno > > > > > + > > > + // SAFETY: `src` has previously been allocated with this `Allocator`; `dst` has just been > > > + // newly allocated. Copy up to the smaller of both sizes. > > > + unsafe { > > > + ptr::copy_nonoverlapping( > > > + src.as_ptr(), > > > + dst.as_ptr().cast(), > > > + cmp::min(layout.size(), data.size), > > > + ) > > > + }; > > > + > > > + // SAFETY: `src` has been created by `Self::alloc_store_data`. > > > + unsafe { Self::free_read_data(src) }; > > > + > > > + Ok(dst) > > > } > > > } > > > -- > > > 2.46.0 > > > > >