From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 52996C433ED for ; Thu, 6 May 2021 14:55:09 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7D85B6101B for ; Thu, 6 May 2021 14:55:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7D85B6101B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=bsdimp.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:47314 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lefP9-0003O8-H0 for qemu-devel@archiver.kernel.org; Thu, 06 May 2021 10:55:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56746) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lefJ7-00065N-QN for qemu-devel@nongnu.org; Thu, 06 May 2021 10:48:53 -0400 Received: from mail-qk1-x731.google.com ([2607:f8b0:4864:20::731]:43529) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lefJ3-000095-JY for qemu-devel@nongnu.org; Thu, 06 May 2021 10:48:53 -0400 Received: by mail-qk1-x731.google.com with SMTP id a22so4567060qkl.10 for ; Thu, 06 May 2021 07:48:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=4Gb0I7ISeIJ1QqvZBHcd3YAVMORjuBTVTOovRqj2pUM=; b=QXKAsXRdzB6Z9p0YOiFHSVzJNKe0Z3+WxHCeAtGu+Ki/CSZc+6gKRztKHg1GYPcCUk EBIJzueuPrTdG3LH8gPG9a3dpaJl9Tld0lj3OXa9zlWE2cRaSIuZ8N+Iqi79hWpCjCIZ uik3l+bZ/25pVklG4hvu6x6XMTbcHCzO2XvGbe+AGacUUYzaYE4HAuJbsAeAgf5EzSmN bzetqBfqU4bdr6ScQ73ckMsFAqzTsMDyS+G5g+RoE59xxbB4qYFNcxMt7XYeaHzQWj4a ZKZ0nOnrsRa44bFlekEdMXtSBbhjfV8tciIBlem9RlKC+MdrtQmI55qAJPm7jAwTPQRO hqMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=4Gb0I7ISeIJ1QqvZBHcd3YAVMORjuBTVTOovRqj2pUM=; b=Em1vAg4fo0pbTpohmVGHfwkvX9FDiobNDcj1nb7yuMc3nMLtaGQhxBPjJLt1mW/oqW L1QqZyhLMp4UqTP/ylt0QtXOQdSfDgdrQWJ9MaC+E6Gzex9u/EUNwRrQlsz6cw/ZGqDO aLHuwzSusDLFz+6tdqnLUcBtILNNtYvzxvY1vpC2f7B5a0r3YVDD/OKEjY/RGhNIn2xL kyryoXIzdT58uJc1RgImbu3cQXxwdyrRZ7lVrFGzWjN5ikZL+WGI2Rqp8ekQ9ZuHUBf2 89k7vVPg2iGEenyvpcRkxjfP5pAcpdYa8CXa9wX46w68zYFPfZscf3/CvShxKeBkXVy2 oSQQ== X-Gm-Message-State: AOAM532qoGplkNfivb6G++kGLDJh7WqBjyGapZl+bKUf0GFl8Br7Tz6T A1zixnS0bQ/wMw36d5SwE0h2pJ0TRfFG+HmT7rTKNw== X-Google-Smtp-Source: ABdhPJzoB4t5FgdV/5LEzVbTgT6jcxavkAR8WxPXlAl2GKUGjqdxD/h6izTHh3Nv+CBGgNK48IoVV6xmeGpj/lhF7N8= X-Received: by 2002:a37:7685:: with SMTP id r127mr4125026qkc.359.1620312528205; Thu, 06 May 2021 07:48:48 -0700 (PDT) MIME-Version: 1.0 References: <20210506133758.1749233-1-philmd@redhat.com> <20210506133758.1749233-5-philmd@redhat.com> In-Reply-To: From: Warner Losh Date: Thu, 6 May 2021 08:48:37 -0600 Message-ID: Subject: Re: [PATCH v2 4/9] bsd-user/syscall: Replace alloca() by g_new() To: Peter Maydell Content-Type: multipart/alternative; boundary="000000000000ab554205c1aa6823" Received-SPF: none client-ip=2607:f8b0:4864:20::731; envelope-from=wlosh@bsdimp.com; helo=mail-qk1-x731.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kvm-devel , Kyle Evans , QEMU Developers , qemu-arm , qemu-ppc , Gerd Hoffmann , Paolo Bonzini , =?UTF-8?Q?Philippe_Mathieu=2DDaud=C3=A9?= Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" --000000000000ab554205c1aa6823 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, May 6, 2021 at 8:21 AM Peter Maydell wrote: > On Thu, 6 May 2021 at 15:17, Warner Losh wrote: > > > > > > > > On Thu, May 6, 2021, 7:38 AM Philippe Mathieu-Daud=C3=A9 > wrote: > >> > >> The ALLOCA(3) man-page mentions its "use is discouraged". > >> > >> Replace it by a g_new() call. > >> > >> Signed-off-by: Philippe Mathieu-Daud=C3=A9 > >> --- > >> bsd-user/syscall.c | 3 +-- > >> 1 file changed, 1 insertion(+), 2 deletions(-) > >> > >> diff --git a/bsd-user/syscall.c b/bsd-user/syscall.c > >> index 4abff796c76..dbee0385ceb 100644 > >> --- a/bsd-user/syscall.c > >> +++ b/bsd-user/syscall.c > >> @@ -355,9 +355,8 @@ abi_long do_freebsd_syscall(void *cpu_env, int num= , > abi_long arg1, > >> case TARGET_FREEBSD_NR_writev: > >> { > >> int count =3D arg3; > >> - struct iovec *vec; > >> + g_autofree struct iovec *vec =3D g_new(struct iovec, coun= t); > > > > > > Where is this freed? > > g_autofree, so it gets freed when it goes out of scope. > > https://developer.gnome.org/glib/stable/glib-Miscellaneous-Macros.html#g-= autofree Ah. I'd missed that feature and annotation... Too many years seeing patches like this in other projects where a feature like this wasn't there to save the day... This means you can ignore my other message as equally misinformed. > > > Also, alloca just moves a stack pointer, where malloc has complex > interactions. Are you sure that's a safe change here? > > alloca()ing something with size determined by the guest is > definitely not safe :-) malloc as part of "handle this syscall" > is pretty common, at least in linux-user. > Well, since this is userland emulation, the normal process boundaries will fix that. allocating from the heap is little different..., so while unsafe, it's the domain of $SOMEONE_ELSE to enforce the safety. linux-user has many similar usages for both malloc and alloca, and it's ok for the same reason. Doing a quick grep on my blitz[*] branch in the bsd-user fork shows that alloca is used almost exclusively there. There's 24 calls to alloca in the bsd-user code. There's almost no malloc calls (7) in that at all outside the image loader: all but one of them are confined to sysctl emulation with the last one used to keep track of thread state in new threads... linux-user has a similar profile (20 alloca's and 14 mallocs outside the loader), but with more mallocs in other paths than just sysctl (which isn't present)= . I had no plans on migrating to anything else... Warner --000000000000ab554205c1aa6823 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Thu, May 6, 2021 at 8:21 AM Peter = Maydell <peter.maydell@linar= o.org> wrote:
On Thu, 6 May 2021 at 15:17, Warner Losh <imp@bsdimp.com> wrote:
>
>
>
> On Thu, May 6, 2021, 7:38 AM Philippe Mathieu-Daud=C3=A9 <philmd@redhat.com> w= rote:
>>
>> The ALLOCA(3) man-page mentions its "use is discouraged"= .
>>
>> Replace it by a g_new() call.
>>
>> Signed-off-by: Philippe Mathieu-Daud=C3=A9 <philmd@redhat.com>
>> ---
>>=C2=A0 bsd-user/syscall.c | 3 +--
>>=C2=A0 1 file changed, 1 insertion(+), 2 deletions(-)
>>
>> diff --git a/bsd-user/syscall.c b/bsd-user/syscall.c
>> index 4abff796c76..dbee0385ceb 100644
>> --- a/bsd-user/syscall.c
>> +++ b/bsd-user/syscall.c
>> @@ -355,9 +355,8 @@ abi_long do_freebsd_syscall(void *cpu_env, int= num, abi_long arg1,
>>=C2=A0 =C2=A0 =C2=A0 case TARGET_FREEBSD_NR_writev:
>>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 {
>>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 int count =3D arg3= ;
>> -=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 struct iovec *vec;
>> +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 g_autofree struct iovec= *vec =3D g_new(struct iovec, count);
>
>
> Where is this freed?

g_autofree, so it gets freed when it goes out of scope.
https://developer.g= nome.org/glib/stable/glib-Miscellaneous-Macros.html#g-autofree

Ah. I'd missed that feature and annotation...= =C2=A0 Too many years seeing patches like
this in other projects = where a feature like this wasn't there to save the day...
This means you can ignore my other message as equally misinform= ed.
=C2=A0

> Also, alloca just moves a stack pointer, where malloc has complex inte= ractions. Are you sure that's a safe change here?

alloca()ing something with size determined by the guest is
definitely not safe :-) malloc as part of "handle this syscall" is pretty common, at least in linux-user.

Well, since this is userland emulation, the normal process boundaries wi= ll fix that. allocating from
the heap is little different..., so = while unsafe, it's the domain of $SOMEONE_ELSE to enforce
the= safety. linux-user has many similar usages for both malloc and alloca, and= it's ok for the
same reason.

Doing = a quick grep on my blitz[*] branch in the bsd-user fork shows that alloca i= s used almost
exclusively there. There's 24 calls to alloca i= n the bsd-user code. There's almost no malloc
calls (7) in th= at at all outside the image loader: all but one of them are confined to sys= ctl
emulation with the last one used to keep track of thread stat= e in new threads...
linux-user has a similar profile (20 alloca&#= 39;s and 14 mallocs outside the loader),
but with more mallocs in= other paths than just sysctl (which isn't present).

I had no plans on migrating to anything else...

=
Warner

--000000000000ab554205c1aa6823--