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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8E2C2C43334 for ; Wed, 15 Jun 2022 08:31:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245041AbiFOIbM (ORCPT ); Wed, 15 Jun 2022 04:31:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245032AbiFOIa5 (ORCPT ); Wed, 15 Jun 2022 04:30:57 -0400 Received: from mail-yw1-x1131.google.com (mail-yw1-x1131.google.com [IPv6:2607:f8b0:4864:20::1131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C773457BE for ; Wed, 15 Jun 2022 01:30:55 -0700 (PDT) Received: by mail-yw1-x1131.google.com with SMTP id 00721157ae682-30c2f288f13so53591837b3.7 for ; Wed, 15 Jun 2022 01:30:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=yBrLrrbB7N+dJmN/Zm3Jq2wM5G5mIz03WeoDowy0F+c=; b=dhDlTYQSLiKIQdlGoTCb6bey/8VFAgeBXR92Cpgqpx/p0x6EIjheozl59uoXsVIZiQ ED4+qGQKJJmefgVxMqB4RN+c6kvA0jZ9M+ZKEKrdNEhLv5ep/mwj551cDQysqHCNrla9 LfoV/LMYATPCf52uimgjnRbyk9m3FUiPmaON4+fCzNwH3CpE/CEE1K5tb7d5WEPN2J5m Qgi8V87ORNFD1c8k84SadUbE7EaI3kS5e3lObMqK5sRUIYMPNmD1oAwrnK3/U141iCFh OKMIUiJAMvG2CBZUKUcO8u2MSvBliXG3TNdUEFsAEdbm+Vm3EyofqJaGnQKbZiEXetu4 GhBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=yBrLrrbB7N+dJmN/Zm3Jq2wM5G5mIz03WeoDowy0F+c=; b=hX6HEvNBWkTbYOlsdDw1PtHJ9TZKG2rSRABfg0Dyo3a+jfFBJiiMdHO12Gj2l2UHmv pp2SbODtdM5jKiIBXETn858nMFjPh09lL+mX6xa1GlaVYJix9vP+wvTl6dOlt4GcUcQx wlloy4set0CeqRF1LnAfZyveleH6CG9tkHo75qZ/oExUOJsvVbLK44EykoFN6VI145rJ CvOQLpR0ORQtxzV+24hWOxiCE+MbvQoJ7CRf+GXP9gAh6ELE+v8Gg7iFZv++G8kbDdEc CKN/0mys4BUIDWTy7a9fecSb9mn6r3jeEeDHz9/nHitXs0XQKuFPo0uAD4RhPWD5GJkl 8KFw== X-Gm-Message-State: AJIora/FlmPhCueeMc1ienjPJdEt8rjHn92FmvkdqMx7gHRMBkVJIdD3 ClG7u9DHG4PPdpyR+cvxP9ZOFvpofivwfM9coEf0GA== X-Google-Smtp-Source: AGRyM1tMKLl238Lto9ecz9/dxuNSJvC5VIAUdSklLaNmA+R25yiebC9xkOG2Vsb3kIlZnLJOU7IQB7OW9S6A+RRgdpY= X-Received: by 2002:a81:db53:0:b0:30c:49c0:7f3f with SMTP id b19-20020a81db53000000b0030c49c07f3fmr10032711ywn.461.1655281854115; Wed, 15 Jun 2022 01:30:54 -0700 (PDT) MIME-Version: 1.0 References: <20220614144853.3693273-1-glider@google.com> <20220614214039.GA25951@gate.crashing.org> In-Reply-To: <20220614214039.GA25951@gate.crashing.org> From: Alexander Potapenko Date: Wed, 15 Jun 2022 10:30:17 +0200 Message-ID: Subject: Re: [PATCH] [RFC] Initialization of unused function parameters To: Segher Boessenkool Cc: Linus Torvalds , Evgenii Stepanov , Kees Cook , Marco Elver , Nathan Chancellor , Nick Desaulniers , Thomas Gleixner , Vitaly Buka , Linux Kernel Mailing List , linux-toolchains Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-toolchains@vger.kernel.org On Tue, Jun 14, 2022 at 11:45 PM Segher Boessenkool wrote: > > Hi! > > On Tue, Jun 14, 2022 at 10:19:53PM +0200, Alexander Potapenko wrote: > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > char *kmalloc(int size); > > > > char *kmalloc_or_not(int flag, int size, char *p) { > > if (flag) > > return kmalloc(size); > > else > > return p; > > } > > > > char global[16]; > > > > char *p(int flag) { > > char *c; > > int size; > > if (flag) > > return kmalloc_or_not(1, 4, c); > > else > > return kmalloc_or_not(0, size, global); > > } > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > Since C11, lvalue conversion of an automatic variable that does not have > its address taken is explicitly undefined behaviour (6.3.2.1/2). So in > function "p", both where "c" and where "size" are passed causes UB (so > that executing "p" always causes UB btw). Thanks for this reference to the standard. I've received another one off-list, which lets the variables be address-taken: 6.2.4/6: "If an initialization is specified for the object, it is performed each time the declaration or compound literal is reached in the execution of the block; otherwise, the value becomes indeterminate each time the declaration is reached." 3.19.2/1: "indeterminate value: either an unspecified value or a trap representation" 6.2.6.1/5: "Certain object representations need not represent a value of the object type. If the stored value of an object has such a representation and is read by an lvalue expression that does not have character type, the behavior is undefined. If such a representation is produced by a side effect that modifies all or any part of the object by an lvalue expression that does not have character type, the behavior is undefined. [Footnote: Thus, an automatic variable can be initialized to a trap representation without causing undefined behavior, but the value of the variable cannot be used until a proper value is stored in it.] Such a representation is called a trap representation." > > In this example `size` is passed into kmalloc_or_not() initialized, > > however it is never used, so the code probably has defined behavior. > > No such luck: the passing itself already causes UB. Looks like I've been missing this fact all the time. > GCC does not warn, it has already optimised the code to what you expect > by the time this warning is done. If you use -fno-inline it does warn > for both "c" and "size" (via -Wmaybe-uninitialized). > > But it is still UB! All bets are off, no compiler can do any correct > translation of your program, since there *is none*. Then it makes sense for us to report non-trivial cases where uninitialized values are actually passed to functions. As Evgenii mentions, trivial inlinable cases are optimized away before KMSAN instrumentation kicks in, so we won't be reporting them. > > Segher --=20 Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Stra=C3=9Fe, 33 80636 M=C3=BCnchen Gesch=C3=A4ftsf=C3=BChrer: Paul Manicle, Liana Sebastian Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg Diese E-Mail ist vertraulich. Falls Sie diese f=C3=A4lschlicherweise erhalten haben sollten, leiten Sie diese bitte nicht an jemand anderes weiter, l=C3=B6schen Sie alle Kopien und Anh=C3=A4nge davon und lassen Sie = mich bitte wissen, dass die E-Mail an die falsche Person gesendet wurde. This e-mail is confidential. If you received this communication by mistake, please don't forward it to anyone else, please erase all copies and attachments, and please let me know that it has gone to the wrong person.