From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 130187 invoked by alias); 16 Jun 2016 19:15:13 -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 130177 invoked by uid 89); 16 Jun 2016 19:15:12 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=BAYES_00,KAM_LAZY_DOMAIN_SECURITY,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=H*F:U*rth, 4242, ffi_cif, ffih X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 16 Jun 2016 19:15:02 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0E51880087; Thu, 16 Jun 2016 19:15:01 +0000 (UTC) Received: from anchor.twiddle.net (vpn-239-44.phx2.redhat.com [10.3.239.44]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u5GJF0LY005769; Thu, 16 Jun 2016 15:15:00 -0400 Subject: Re: Function returning uint16 To: =?UTF-8?Q?St=c3=a9phane_Glondu?= , libffi-discuss@sourceware.org References: <5762BFD6.1020704@glondu.net> From: Richard Henderson Message-ID: <862a29c4-6759-44e9-89c0-59270b35d514@redhat.com> Date: Thu, 16 Jun 2016 19:15:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.1.0 MIME-Version: 1.0 In-Reply-To: <5762BFD6.1020704@glondu.net> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-IsSubscribed: yes X-SW-Source: 2016/txt/msg00029.txt.bz2 On 06/16/2016 08:03 AM, Stéphane Glondu wrote: > Hello, > > I am trying to debug > > https://github.com/ocamllabs/ocaml-ctypes/issues/404 > > and I realized that the following code (on amd64): > > #include > #include > #include > > uint16_t retrieve() { > return 0x4242; > } > > int main() { > uint16_t r[2] = { 0xdead, 0xbeef }; > ffi_cif cif; > printf("r = {%x, %x}\n", r[0], r[1]); > ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 0, &ffi_type_uint16, NULL); > ffi_call(&cif, FFI_FN(retrieve), &r[0], NULL); > printf("r = {%x, %x}\n", r[0], r[1]); > return 0; > } > > returns: > > r = {dead, beef} > r = {4242, 0} > > Is that expected? I don't expect r[1] to be overwritten... It is expected. In the documentation, In most situations, @samp{libffi} will handle promotion according to the ABI. However, for historical reasons, there is a special case with return values that must be handled by your code. In particular, for integral (not @code{struct}) types that are narrower than the system register size, the return value will be widened by @samp{libffi}. @samp{libffi} provides a type, @code{ffi_arg}, that can be used as the return type. For example, if the CIF was defined with a return type of @code{char}, @samp{libffi} will try to store a full @code{ffi_arg} into the return value. r~