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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 27FE3C433EF for ; Mon, 15 Nov 2021 22:58:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0E50D610CF for ; Mon, 15 Nov 2021 22:58:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349754AbhKOXBn (ORCPT ); Mon, 15 Nov 2021 18:01:43 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:36984 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350747AbhKOWdT (ORCPT ); Mon, 15 Nov 2021 17:33:19 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1637015417; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=s/bbbyKfrQIZYJ5+Y1b/OY2EjywBRVrDN13bc0Ps/so=; b=FpEh+B5qsqfE9iRU6UYqd3hOCp9CgzVi1jDB8PIz2aT4/dcxUhhOkgCw6wVjsCS/dP4hdM vr+CwB3Qupw46xFFadzwBgjPXaA/e0veeA0y7U97d+QZgE6BoixHAD47sqlpPkvSZYnPtM YNhzPo6BRsW3X7YyXIffI5mAzaY2Vv4= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-237-PgHOu-c0NFOMOXdCJt35PA-1; Mon, 15 Nov 2021 17:30:16 -0500 X-MC-Unique: PgHOu-c0NFOMOXdCJt35PA-1 Received: by mail-qt1-f199.google.com with SMTP id g16-20020ac85810000000b002b212f2662aso9218005qtg.20 for ; Mon, 15 Nov 2021 14:30:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=s/bbbyKfrQIZYJ5+Y1b/OY2EjywBRVrDN13bc0Ps/so=; b=20FtN+6o0regDoBGyZ3+yYw0dkdgCwgENU25RNjbcBnBSEJNYEdTZyfkgiPmYfmaU1 ey11t0QYn6ucbWVo0JmIEU/0mC9rPfTVoO1YEHUOa/+EwMFNjrDuJQaSnUFSDC2/VUsH nG8sgfApaEaJYQJ9n+RzBEORAANDGpVFcjRXVu9udCDsA0W0HNlFXZra6x5u6UA/8hL1 s8lBSq80TmjdIIhwa105mYn7kTGP8U8O8Dr1fDkhUeTj38rRP669Y1b11Xu3aWoF5/D4 Ryv1Nbx8DMcNVClBUg/y6s8YjIWkFxHAsYrG0lxLpZ7sknvlNAih7VNGQFKYXd5Hbd9C iA5w== X-Gm-Message-State: AOAM532Q+pi+xVNS27yBDTXGmUx+D7J9eUgwDSsjdUQXxw0zFQRzoBvs 3BB06toiWs9MZ2Lb6Q+BTzAjCIMLbCXr6Nw8gyxWpN4LQ7SPUcQ3biNLvPZJznx/A/u8lO2TICc YOFvIFy8I2RbWQRCxPdyfol5VERfLNg== X-Received: by 2002:a05:622a:1341:: with SMTP id w1mr2567886qtk.293.1637015416018; Mon, 15 Nov 2021 14:30:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJyhBGs90ZZ2KwlzM+rRyIeOnPGjH/KSaldLaUfcqvqrNbHygSbYH+IdHZnP7uKHeTZvxFRhZQ== X-Received: by 2002:a05:622a:1341:: with SMTP id w1mr2567866qtk.293.1637015415807; Mon, 15 Nov 2021 14:30:15 -0800 (PST) Received: from t14s.localdomain (c-73-69-212-193.hsd1.nh.comcast.net. [73.69.212.193]) by smtp.gmail.com with ESMTPSA id 15sm8607027qtp.55.2021.11.15.14.30.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Nov 2021 14:30:15 -0800 (PST) Message-ID: <17dd1a7c290bac426b638640009f6242f7e197c1.camel@redhat.com> Subject: Re: [PATCH 2/6] Add returns_zero_on_success/failure attributes From: David Malcolm To: Peter Zijlstra , Prathamesh Kulkarni Cc: gcc-patches@gcc.gnu.org, linux-toolchains@vger.kernel.org Date: Mon, 15 Nov 2021 17:30:14 -0500 In-Reply-To: References: <20211113203732.2098220-1-dmalcolm@redhat.com> <20211113203732.2098220-4-dmalcolm@redhat.com> User-Agent: Evolution 3.38.4 (3.38.4-1.fc33) MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=dmalcolm@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-toolchains@vger.kernel.org On Mon, 2021-11-15 at 15:45 +0100, Peter Zijlstra wrote: > On Mon, Nov 15, 2021 at 12:33:16PM +0530, Prathamesh Kulkarni wrote: > > On Sun, 14 Nov 2021 at 02:07, David Malcolm via Gcc-patches > > > > +/* Handle "returns_zero_on_failure" and "returns_zero_on_success" > > > attributes; > > > +   arguments as in struct attribute_spec.handler.  */ > > > + > > > +static tree > > > +handle_returns_zero_on_attributes (tree *node, tree name, tree, > > > int, > > > +                                  bool *no_add_attrs) > > > +{ > > > +  if (!INTEGRAL_TYPE_P (TREE_TYPE (*node))) > > > +    { > > > +      error ("%qE attribute on a function not returning an > > > integral type", > > > +            name); > > > +      *no_add_attrs = true; > > > +    } > > > +  return NULL_TREE; > > Hi David, > > Just curious if a warning should be emitted if the function is marked > > with the attribute but it's return value isn't actually 0 ? > > > > There are other constants like -1 or 1 that are often used to > > indicate > > error, so maybe tweak the attribute to > > take the integer as an argument ? > > Sth like returns_int_on_success(cst) / returns_int_on_failure(cst) ? > > > > Also, would it make sense to extend it for pointers too for returning > > NULL on success / failure ? > > Please also consider that in Linux we use the 'last' page for error > code > returns. That is, a function returning a pointer could return '(void > *)-EFAULT' also see linux/err.h > Thanks. Am I right in thinking that such functions return non-NULL, giving something like: __attribute__((returns_ptr_in_range_on_success (0x1, NULL - 4096))) __attribute__((returns_ptr_in_range_on_failure (NULL - 4096, NULL - 1))) __attribute__((returns_non_null)) as attributes? (I have no idea if the above will parse, and I admit these look ugly as-is, though I suppose they could be hidden behind a macro). Looking at include/linux/err.h I see functions: static inline bool __must_check IS_ERR(__force const void *ptr) { return IS_ERR_VALUE((unsigned long)ptr); } static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr) { return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr); } so maybe attribute could refer to predicate functions, something like this: __attribute__((return_value_success_predicate(FUNCTION_DECL))) __attribute__((return_value_failure_predicate(FUNCTION_DECL))) where this case could use something like: __attribute__((return_value_failure_predicate(IS_ERR))) to express the idea "this function can succeed or fail, and the given function decl expresses whether a given return value is a failure" - or somesuch. The predicate function would probably have to be pure. Obviously I'm just brainstorming here; as noted in my reply to Prathamesh, all I need for the initial implementation of the trust boundary work is just being able to express that zero vs non-zero return is the success vs failure condition for a function. Dave