From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from paperclip.tbsaunde.org (unknown [IPv6:2600:3c03::f03c:91ff:fe6e:c625]) by sourceware.org (Postfix) with ESMTP id B3E243858D35 for ; Wed, 30 Jun 2021 05:35:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B3E243858D35 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tbsaunde.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tbsaunde.org Received: from caber.home (pool-108-24-42-131.cmdnnj.fios.verizon.net [108.24.42.131]) by paperclip.tbsaunde.org (Postfix) with ESMTPSA id 609BFC0DA; Wed, 30 Jun 2021 05:35:45 +0000 (UTC) From: Trevor Saunders To: gcc-patches@gcc.gnu.org Cc: Trevor Saunders Subject: [PATCH 1/4] add utility to poison globals that should not be used Date: Wed, 30 Jun 2021 01:35:25 -0400 Message-Id: <20210630053529.26581-1-tbsaunde@tbsaunde.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_ABUSEAT, RCVD_IN_XBL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-List-Received-Date: Wed, 30 Jun 2021 05:35:48 -0000 This provides a class to wrap globals like input_location or cfun that should not be used directly and will ideally go away. This class tracks if access to the global is currently blocked and asserts if accessed when that is not allowed. It also adds a class to mark access as blocked for the lifetime of the scope. bootstrapped and regtested on x86_64-linux-gnu, ok? Trev gcc/ChangeLog: * poison.h: New file. --- gcc/poison.h | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 gcc/poison.h diff --git a/gcc/poison.h b/gcc/poison.h new file mode 100644 index 00000000000..239ab1cb91a --- /dev/null +++ b/gcc/poison.h @@ -0,0 +1,88 @@ +/* Simple utility to poison globals that should be avoided. + + Copyright (C) 2021 the GNU Toolchain Authors + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#ifndef GCC_POISON_H +#define GCC_POISON_H + +template class auto_poison; + +/* This class is intended to be used as a transparent wrapper around the type of + a global object that we would like to stop using. */ +template +class poisonable +{ +public: + poisonable () : m_val (), m_poisoned (false) {} + explicit poisonable (T val) : m_val (val), m_poisoned (false) {} + + operator T& () + { + gcc_assert (!m_poisoned); + return m_val; + } + + poisonable &operator= (T val) + { + gcc_assert (!m_poisoned); + m_val = val; + return *this; + } + + T *operator& () + { + gcc_assert (!m_poisoned); + return &m_val; + } + + poisonable (const poisonable &) = delete; + poisonable (poisonable &&) = delete; + poisonable &operator= (const poisonable &) = delete; + poisonable &operator= (poisonable &&) = delete; + +private: + friend class auto_poison; + + T m_val; + bool m_poisoned; + }; + +/* This class provides a way to make a global inaccessible in the given scope, + and any functions called within that scope. */ +template +class auto_poison +{ +public: + auto_poison (poisonable &p) : m_target (p) + { + gcc_assert (!p.m_poisoned); + p.m_poisoned = true; + } + ~auto_poison () { m_target.m_poisoned = false; } + + auto_poison (const auto_poison &) = delete; + auto_poison (auto_poison &&) = delete; + auto_poison &operator= (const auto_poison &) = delete; + auto_poison &operator= (auto_poison &&) = delete; + +private: + poisonable &m_target; +}; + +#endif -- 2.20.1