From patchwork Tue Aug 1 13:52:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Feng X-Patchwork-Id: 129315 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp2683742vqg; Tue, 1 Aug 2023 06:54:26 -0700 (PDT) X-Google-Smtp-Source: APBJJlFfVpyBisn3nzJZVz9gtDWu3bzy4/3gQgOCfGTzXEf+npBbdqueOBcLvg/PbQ7G/Gt/5sbl X-Received: by 2002:a17:907:270d:b0:993:da87:1c81 with SMTP id w13-20020a170907270d00b00993da871c81mr3653658ejk.20.1690898066455; Tue, 01 Aug 2023 06:54:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690898066; cv=none; d=google.com; s=arc-20160816; b=PWCMX0j8dVYpNfoFyrWW0m7set7wAyKfuiPSKznjSRHlKkrL/XcFBr+XPYTTcRWl3u cAkZGPna9B8cBIwH0snaM11d6wGVbvI5CH94Vua6DnPdbdGXGPJtVCax3QDtdnvXvaFu Al1me1VRWV7+LYhGAcmdRwH7+dvwKJEZOU89qjTEkkfm1gekxXf5Wef4dJGVfkgHOCMs bZu9yjX2ZLuodeG/jpAE6f/7y+Mxgq5AQyga3fKY9Bc928VqX2U2N881XshmibbvofFF WWjzfrvYDZ4NafUrSMIC5/OKu8miN9DJPrjufBb3LLQQ2NubPNJ4hrgOrX0SRclkhlRN RTYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:cc:to:subject :message-id:date:mime-version:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=BLNuzmmSrfwxlB95SH0ZLqiHXO/M2NWYi2StJUvz2Yg=; fh=0WQtasyCjcROZSEhZBaZBwsyknwZ09ZXaXuaZ00jvaA=; b=EfPgHtqhZM6ukyX/eJuyT1gho37riHRcUiVYWHvXDwpfw6W+YP+xxBCDhMCSMQdeQ+ 3tHGpQ3s9TXuJ20NjYHb7QMVRvubD7b/zyxbLO1vhiVS4O/dD1NK6PCjnIWHEGth5v0F rqgHNSxf5YRa+ux612HjGDbmw8IGQc1BGGYsuNZYP3Fe3HISc83GsP4fmOc5+dx9/YrV iAhA4cvgGK+6JfqE30/AlV3QrToGGleBgXqsl1O4mGcPp0iCn2WUBrGUsm2jKk/lNzty 5jEw6MVPQZ+DJgpc70XkCXuWWOTit5SrQcSrIHplmbXTpQObDX6fT05tMYvyRUPbtGkA djhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="Wk9WfCe/"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id i3-20020a170906850300b0099bcbb766dbsi4298014ejx.780.2023.08.01.06.54.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Aug 2023 06:54:26 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="Wk9WfCe/"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 374D93858439 for ; Tue, 1 Aug 2023 13:54:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 374D93858439 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690898065; bh=BLNuzmmSrfwxlB95SH0ZLqiHXO/M2NWYi2StJUvz2Yg=; h=Date:Subject:To:Cc:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Wk9WfCe/3q2Yg9Lktu7wSRre8f0/YoKJHs0A8Kz6q4ldtxqliSUPCAHogdWPSGy27 KpcF0wZ5V2TEXtRjMP1eDFgRhe8uOU2TjN5cwuJn3IglkwWKUufwVs0Z/FoSMrFPBn NdghZ/JjYrgO0zviGroYkpr71Db/VzajLh8/NESw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-00364e01.pphosted.com (mx0a-00364e01.pphosted.com [148.163.135.74]) by sourceware.org (Postfix) with ESMTPS id 134E63858430 for ; Tue, 1 Aug 2023 13:53:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 134E63858430 Received: from pps.filterd (m0167069.ppops.net [127.0.0.1]) by mx0a-00364e01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 371DkDL1005866 for ; Tue, 1 Aug 2023 09:53:06 -0400 Received: from mail-vs1-f70.google.com (mail-vs1-f70.google.com [209.85.217.70]) by mx0a-00364e01.pphosted.com (PPS) with ESMTPS id 3s4uskavsk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 01 Aug 2023 09:53:05 -0400 Received: by mail-vs1-f70.google.com with SMTP id ada2fe7eead31-4475b78b19bso1009053137.3 for ; Tue, 01 Aug 2023 06:53:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690897984; x=1691502784; h=cc:to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=BLNuzmmSrfwxlB95SH0ZLqiHXO/M2NWYi2StJUvz2Yg=; b=RVq35wYkWVW2urX6Q1r+vRtCclr+J2U61EkeC/Jxj7ApGty3skxG0333QYFtD14CLw SdubrktEFrWa0l9n6HVEsAvwvymMBbuRYvf91BHwsPDGkXuiDpqkCCuibE/UZ7Pv9Til AsmTiPNSX/ciRdsFWyBxxvT0Cyl593C0Dn+qNozUnVP5uKPbGq2j1Be0aCtbmzxqasfU VoZPzK8d2D+7DHAIsmMGsF2LHcQ6RbZMyTL+BTYrFpYlQXS/jtiYrHwJs/qeMCezHQm6 dN/mi2VLL0q8qoL5I6n1Rsh6ByXDdvi+zP/7s+p1xnOtplQSv+6G2Eci1QMSgnnRSR3M yWSQ== X-Gm-Message-State: ABy/qLYwgOwO0OorZf9jWwyybx/0AMSIKy6KsziF98BzZXPtbNncu6Mg M/Owy8an/SPqJKRfh/hdZlkEliUYeEeF6w6MexXRWYaubCgy82DzVzcwjbRDEzbM1vq9Oop/ckH debkXTfF9NCyLQJ2Gsl3+MX5q25vpKCBcI58tCnRCBw== X-Received: by 2002:a67:f10b:0:b0:447:6965:7284 with SMTP id n11-20020a67f10b000000b0044769657284mr2134074vsk.8.1690897984400; Tue, 01 Aug 2023 06:53:04 -0700 (PDT) X-Received: by 2002:a67:f10b:0:b0:447:6965:7284 with SMTP id n11-20020a67f10b000000b0044769657284mr2134059vsk.8.1690897984055; Tue, 01 Aug 2023 06:53:04 -0700 (PDT) MIME-Version: 1.0 Date: Tue, 1 Aug 2023 09:52:52 -0400 Message-ID: Subject: [PATCH] analyzer: stash values for CPython plugin [PR107646] To: gcc-patches@gcc.gnu.org Cc: David Malcolm X-Proofpoint-ORIG-GUID: k8OsRKbGaa8PRlddy3L1VB-sjfhnhQi6 X-Proofpoint-GUID: k8OsRKbGaa8PRlddy3L1VB-sjfhnhQi6 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-08-01_09,2023-08-01_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=10 lowpriorityscore=10 spamscore=0 adultscore=0 phishscore=0 suspectscore=0 bulkscore=10 priorityscore=1501 clxscore=1011 mlxscore=0 malwarescore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2306200000 definitions=main-2308010126 X-Spam-Status: No, score=-9.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Eric Feng via Gcc-patches From: Eric Feng Reply-To: Eric Feng Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773035131353915662 X-GMAIL-MSGID: 1773035131353915662 Hi all, This patch adds a hook to the end of ana::on_finish_translation_unit which calls relevant stashing-related callbacks registered during plugin initialization. This feature is used to stash named types and global variables for a CPython analyzer plugin [PR107646]. Bootstrapped and tested on aarch64-unknown-linux-gnu. Does it look okay? --- gcc/analyzer/ChangeLog: * analyzer-language.cc (run_callbacks): New function. (on_finish_translation_unit): New function. * analyzer-language.h (GCC_ANALYZER_LANGUAGE_H): New include. (class translation_unit): New vfuncs. gcc/c/ChangeLog: * c-parser.cc: New functions. gcc/testsuite/ChangeLog: * gcc.dg/plugin/analyzer_cpython_plugin.c: New test. Signed-off-by: Eric Feng --- gcc/analyzer/analyzer-language.cc | 22 ++ gcc/analyzer/analyzer-language.h | 9 + gcc/c/c-parser.cc | 26 ++ .../gcc.dg/plugin/analyzer_cpython_plugin.c | 224 ++++++++++++++++++ 4 files changed, 281 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/plugin/analyzer_cpython_plugin.c diff --git a/gcc/analyzer/analyzer-language.cc b/gcc/analyzer/analyzer-language.cc index 2c8910906ee..fc41b9c17b8 100644 --- a/gcc/analyzer/analyzer-language.cc +++ b/gcc/analyzer/analyzer-language.cc @@ -35,6 +35,26 @@ static GTY (()) hash_map *analyzer_stashed_constants; #if ENABLE_ANALYZER namespace ana { +static vec + *finish_translation_unit_callbacks; + +void +register_finish_translation_unit_callback ( + finish_translation_unit_callback callback) +{ + if (!finish_translation_unit_callbacks) + vec_alloc (finish_translation_unit_callbacks, 1); + finish_translation_unit_callbacks->safe_push (callback); +} + +void +run_callbacks (logger *logger, const translation_unit &tu) +{ + for (auto const &cb : finish_translation_unit_callbacks) + { + cb (logger, tu); + } +} /* Call into TU to try to find a value for NAME. If found, stash its value within analyzer_stashed_constants. */ @@ -102,6 +122,8 @@ on_finish_translation_unit (const translation_unit &tu) the_logger.set_logger (new logger (logfile, 0, 0, *global_dc->printer)); stash_named_constants (the_logger.get_logger (), tu); + + run_callbacks (the_logger.get_logger (), tu); } /* Lookup NAME in the named constants stashed when the frontend TU finished. diff --git a/gcc/analyzer/analyzer-language.h b/gcc/analyzer/analyzer-language.h index 00f85aba041..8deea52d627 100644 --- a/gcc/analyzer/analyzer-language.h +++ b/gcc/analyzer/analyzer-language.h @@ -21,6 +21,8 @@ along with GCC; see the file COPYING3. If not see #ifndef GCC_ANALYZER_LANGUAGE_H #define GCC_ANALYZER_LANGUAGE_H +#include "analyzer/analyzer-logging.h" + #if ENABLE_ANALYZER namespace ana { @@ -35,8 +37,15 @@ class translation_unit have been seen). If it is defined and an integer (e.g. either as a macro or enum), return the INTEGER_CST value, otherwise return NULL. */ virtual tree lookup_constant_by_id (tree id) const = 0; + virtual tree lookup_type_by_id (tree id) const = 0; + virtual tree lookup_global_var_by_id (tree id) const = 0; }; +typedef void (*finish_translation_unit_callback) + (logger *, const translation_unit &); +void register_finish_translation_unit_callback ( + finish_translation_unit_callback callback); + /* Analyzer hook for frontends to call at the end of the TU. */ void on_finish_translation_unit (const translation_unit &tu); diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index 80920b31f83..f0ee55e416b 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -1695,6 +1695,32 @@ public: return NULL_TREE; } + tree + lookup_type_by_id (tree id) const final override + { + if (tree type_decl = lookup_name (id)) + { + if (TREE_CODE (type_decl) == TYPE_DECL) + { + tree record_type = TREE_TYPE (type_decl); + if (TREE_CODE (record_type) == RECORD_TYPE) + return record_type; + } + } + + return NULL_TREE; + } + + tree + lookup_global_var_by_id (tree id) const final override + { + if (tree var_decl = lookup_name (id)) + if (TREE_CODE (var_decl) == VAR_DECL) + return var_decl; + + return NULL_TREE; + } + private: /* Attempt to get an INTEGER_CST from MACRO. Only handle the simplest cases: where MACRO's definition is a single diff --git a/gcc/testsuite/gcc.dg/plugin/analyzer_cpython_plugin.c b/gcc/testsuite/gcc.dg/plugin/analyzer_cpython_plugin.c new file mode 100644 index 00000000000..285da102edb --- /dev/null +++ b/gcc/testsuite/gcc.dg/plugin/analyzer_cpython_plugin.c @@ -0,0 +1,224 @@ +/* -fanalyzer plugin for CPython extension modules */ +/* { dg-options "-g" } */ + +#define INCLUDE_MEMORY +#include "gcc-plugin.h" +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "tree.h" +#include "function.h" +#include "basic-block.h" +#include "gimple.h" +#include "gimple-iterator.h" +#include "diagnostic-core.h" +#include "graphviz.h" +#include "options.h" +#include "cgraph.h" +#include "tree-dfa.h" +#include "stringpool.h" +#include "convert.h" +#include "target.h" +#include "fold-const.h" +#include "tree-pretty-print.h" +#include "diagnostic-color.h" +#include "diagnostic-metadata.h" +#include "tristate.h" +#include "bitmap.h" +#include "selftest.h" +#include "function.h" +#include "json.h" +#include "analyzer/analyzer.h" +#include "analyzer/analyzer-language.h" +#include "analyzer/analyzer-logging.h" +#include "ordered-hash-map.h" +#include "options.h" +#include "cgraph.h" +#include "cfg.h" +#include "digraph.h" +#include "analyzer/supergraph.h" +#include "sbitmap.h" +#include "analyzer/call-string.h" +#include "analyzer/program-point.h" +#include "analyzer/store.h" +#include "analyzer/region-model.h" +#include "analyzer/call-details.h" +#include "analyzer/call-info.h" +#include "make-unique.h" + +int plugin_is_GPL_compatible; + +#if ENABLE_ANALYZER +static GTY (()) hash_map *analyzer_stashed_types; +static GTY (()) hash_map *analyzer_stashed_globals; + +namespace ana +{ +static tree pyobj_record = NULL_TREE; +static tree varobj_record = NULL_TREE; +static tree pylistobj_record = NULL_TREE; +static tree pylongobj_record = NULL_TREE; +static tree pylongtype_vardecl = NULL_TREE; +static tree pylisttype_vardecl = NULL_TREE; + +tree +get_field_by_name (tree type, const char *name) +{ + for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) + { + if (TREE_CODE (field) == FIELD_DECL) + { + const char *field_name = IDENTIFIER_POINTER (DECL_NAME (field)); + if (strcmp (field_name, name) == 0) + return field; + } + } + return NULL_TREE; +} + +static void +maybe_stash_named_type (logger *logger, const translation_unit &tu, + const char *name) +{ + LOG_FUNC_1 (logger, "name: %qs", name); + if (!analyzer_stashed_types) + analyzer_stashed_types = hash_map::create_ggc (); + + tree id = get_identifier (name); + if (tree t = tu.lookup_type_by_id (id)) + { + gcc_assert (TREE_CODE (t) == RECORD_TYPE); + analyzer_stashed_types->put (id, t); + if (logger) + logger->log ("found %qs: %qE", name, t); + } + else + { + if (logger) + logger->log ("%qs: not found", name); + } +} + +static void +maybe_stash_global_var (logger *logger, const translation_unit &tu, + const char *name) +{ + LOG_FUNC_1 (logger, "name: %qs", name); + if (!analyzer_stashed_globals) + analyzer_stashed_globals = hash_map::create_ggc (); + + tree id = get_identifier (name); + if (tree t = tu.lookup_global_var_by_id (id)) + { + gcc_assert (TREE_CODE (t) == VAR_DECL); + analyzer_stashed_globals->put (id, t); + if (logger) + logger->log ("found %qs: %qE", name, t); + } + else + { + if (logger) + logger->log ("%qs: not found", name); + } +} + +static void +stash_named_types (logger *logger, const translation_unit &tu) +{ + LOG_SCOPE (logger); + + maybe_stash_named_type (logger, tu, "PyObject"); + maybe_stash_named_type (logger, tu, "PyListObject"); + maybe_stash_named_type (logger, tu, "PyVarObject"); + maybe_stash_named_type (logger, tu, "PyLongObject"); +} + +static void +stash_global_vars (logger *logger, const translation_unit &tu) +{ + LOG_SCOPE (logger); + + maybe_stash_global_var (logger, tu, "PyLong_Type"); + maybe_stash_global_var (logger, tu, "PyList_Type"); +} + +tree +get_stashed_type_by_name (const char *name) +{ + if (!analyzer_stashed_types) + return NULL_TREE; + tree id = get_identifier (name); + if (tree *slot = analyzer_stashed_types->get (id)) + { + gcc_assert (TREE_CODE (*slot) == RECORD_TYPE); + return *slot; + } + return NULL_TREE; +} + +tree +get_stashed_global_var_by_name (const char *name) +{ + if (!analyzer_stashed_globals) + return NULL_TREE; + tree id = get_identifier (name); + if (tree *slot = analyzer_stashed_globals->get (id)) + { + gcc_assert (TREE_CODE (*slot) == VAR_DECL); + return *slot; + } + return NULL_TREE; +} + +static void +init_py_structs () +{ + if (pyobj_record == NULL_TREE) + pyobj_record = get_stashed_type_by_name ("PyObject"); + if (varobj_record == NULL_TREE) + varobj_record = get_stashed_type_by_name ("PyVarObject"); + if (pylistobj_record == NULL_TREE) + pylistobj_record = get_stashed_type_by_name ("PyListObject"); + if (pylongobj_record == NULL_TREE) + pylongobj_record = get_stashed_type_by_name ("PyLongObject"); + if (pylongtype_vardecl == NULL_TREE) + pylongtype_vardecl = get_stashed_global_var_by_name ("PyLong_Type"); + if (pylisttype_vardecl == NULL_TREE) + pylisttype_vardecl = get_stashed_global_var_by_name ("PyList_Type"); +} + +static void +cpython_analyzer_init_cb (void *gcc_data, void * /*user_data */) +{ + ana::plugin_analyzer_init_iface *iface + = (ana::plugin_analyzer_init_iface *)gcc_data; + LOG_SCOPE (iface->get_logger ()); + if (1) + inform (input_location, "got here: cpython_analyzer_init_cb"); + + init_py_structs (); + if (pyobj_record == NULL_TREE) + return; +} +} // namespace ana + +#endif /* #if ENABLE_ANALYZER */ + +int +plugin_init (struct plugin_name_args *plugin_info, + struct plugin_gcc_version *version) +{ +#if ENABLE_ANALYZER + const char *plugin_name = plugin_info->base_name; + if (1) + inform (input_location, "got here; %qs", plugin_name); + ana::register_finish_translation_unit_callback (&stash_named_types); + ana::register_finish_translation_unit_callback (&stash_global_vars); + register_callback (plugin_info->base_name, PLUGIN_ANALYZER_INIT, + ana::cpython_analyzer_init_cb, + NULL); /* void *user_data */ +#else + sorry_no_analyzer (); +#endif + return 0; +} \ No newline at end of file