From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29563 invoked by alias); 8 Oct 2013 17:14:16 -0000 Mailing-List: contact libffi-discuss-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libffi-discuss-owner@sourceware.org Received: (qmail 29545 invoked by uid 89); 8 Oct 2013 17:14:15 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=0.8 required=5.0 tests=BAYES_20,RDNS_NONE,SPF_HELO_PASS,SPF_PASS autolearn=no version=3.3.2 X-HELO: ygrex.ru Received: from Unknown (HELO ygrex.ru) (5.19.241.250) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Tue, 08 Oct 2013 17:14:14 +0000 Received: from ygrex-mac (c-24-6-109-123.hsd1.ca.comcast.net [24.6.109.123]) (using TLSv1.2 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by ygrex.ru (Postfix) with ESMTPSA id A0150DE099 for ; Tue, 8 Oct 2013 21:14:08 +0400 (MSK) Date: Tue, 08 Oct 2013 17:14:00 -0000 From: Igor Bogomazov To: libffi-discuss@sourceware.org Subject: return value buffer malloc()'ed vs alloca()'ed Message-ID: <20131008101354.43baeeeb@ygrex-mac> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-SW-Source: 2013/txt/msg00172.txt.bz2 Hello, I've been trying to investigate valgrind warnings for a while and found an undocumented feature, please let me know if it is well-known. What I did. I modified a code given in =C2=AB2.2 Simple Example=C2=BB so that return va= lue (rc), originally declared as (int), became an (int *)malloc(sizeof(int)) so that it is resident in heap since that. Of cource, (&rc) replaced with (rc) later in the code. What I get. valgrind complaints about =C2=ABInvalid write of size 8=C2=BB while =C2=ABA= ddress 0x55ec040 is 0 bytes inside a block of size 4 alloc'd=C2=BB, it is exactly that allocated (rc) buffer. Notes. Allocating buffer for the return value using alloca() does the trick and makes valgrind silent. Further. I looked at x86/unix64.S, it is exactly the line: movq %rax, (%rdi) that causes the valgrind's warning (at .Lst_uint32) That is my question: is it necessary to allocate a buffer for the return value with alloca() and never with malloc()? --=20 Sincerely yours, Igor Bogomazov