From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 128BF385841A for ; Thu, 1 Sep 2022 21:31:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 128BF385841A Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-446-IrFsxdN6N6qIeRTMXq7Wmg-1; Thu, 01 Sep 2022 17:31:30 -0400 X-MC-Unique: IrFsxdN6N6qIeRTMXq7Wmg-1 Received: by mail-wm1-f70.google.com with SMTP id v21-20020a05600c215500b003a83c910d83so78465wml.3 for ; Thu, 01 Sep 2022 14:31:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=5H+bOSERJftTdBSLzrDIYjSrsIzaPWzkNocAuEXuAbQ=; b=PSh4CrB+G6DR9BAvHzviZvRhtZ0YDvxHu8Yhahagcmu5HSWIDxcOI3L2xKko34Q7o7 aDF7uLR/CXaLSkgnMHKQXQmAfgg7b0l9KHxsA2nuqCpFXr8MnF6Tt26DDcE/XGRddQm6 jbE2wlZPj3xXdoxaBTfltQS3+zN1OwJqxT72Fbgtg7VbsviTqOvu0Fx2Uw8nUUSZo94H JxcmVtQI/5YkaOU8lIcK0NTvCNlKHyYVdyJCif75x5jwbsJBX71eC6JeUGFrTSn+ydha 8ckZ1KCj8a8GwljME2EAzX8OcUEV9D8kfR5mK74l/ii/w7BnpRJAgiCwMitffxxRQart 34Wg== X-Gm-Message-State: ACgBeo2QCLG278+4JqC5xFHZ91UOuNWy5bBxabQcd6jTb3tM01k0LPn5 V/zYfQals0ipAfCjZEC3Crz0ufNHAZ3W4bhVYtMHoW1psOKgES5gOhYlvXnJkDu8pm8oON61fKG mahHu6UN94jXz6CqomueXBl7EeK6aoaQP7XrlrJ7s1HomwHGoUz5M5Zgc5GJzjSGsMBSyF1DUqg == X-Received: by 2002:a05:600c:6009:b0:3a5:b069:5d34 with SMTP id az9-20020a05600c600900b003a5b0695d34mr618704wmb.115.1662067889189; Thu, 01 Sep 2022 14:31:29 -0700 (PDT) X-Google-Smtp-Source: AA6agR6pfGbAq8XppsSGBam5FevOmzyb/wC4KVnIieTlFcznSaMSYG2CeKDLuqQbffK8WUIPgoJVMQ== X-Received: by 2002:a05:600c:6009:b0:3a5:b069:5d34 with SMTP id az9-20020a05600c600900b003a5b0695d34mr618696wmb.115.1662067888884; Thu, 01 Sep 2022 14:31:28 -0700 (PDT) Received: from localhost ([31.111.84.229]) by smtp.gmail.com with ESMTPSA id n30-20020a05600c501e00b003a61306d79dsm195657wmr.41.2022.09.01.14.31.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Sep 2022 14:31:28 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Subject: [PATCH 6/9] gdb: add asserts to gdbarch_register_name Date: Thu, 1 Sep 2022 22:31:14 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 01 Sep 2022 21:31:38 -0000 This commit adds asserts to gdbarch_register_name that validate the parameters, and the return value. The interesting thing here is that gdbarch_register_name is generated by gdbarch.py, and so, to add these asserts, I need to update the generation script. I've added two new arguments for Functions and Methods (as declared in gdbarch-components.py), these arguments are 'param_checks' and 'result_checks'. Each of these new arguments can be used to list some expressions that are then used within gdb_assert calls in the generated code. The asserts that validate the API as described in the comment I added to gdbarch_register_name a few commits back; the register number passed in needs to be a valid cooked register number, and the result being returned should not be nullptr. --- gdb/gdbarch-components.py | 16 ++++++++++++++++ gdb/gdbarch.c | 6 +++++- gdb/gdbarch.py | 19 ++++++++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/gdb/gdbarch-components.py b/gdb/gdbarch-components.py index fcef090c2eb..b8dc1dd36f5 100644 --- a/gdb/gdbarch-components.py +++ b/gdb/gdbarch-components.py @@ -99,6 +99,20 @@ # argument, and NAME is the name. Note that while the names could be # auto-generated, this approach lets the "comment" field refer to # arguments in a nicer way. It is also just nicer for users. +# +# * "param_checks" - optional, a list of strings. Each string is an +# expression that is placed within a gdb_assert before the call is +# made to the Function/Method implementation. Each expression is +# something that should be true, and it is expected that the +# expression will make use of the parameters named in 'params' (though +# this is not required). +# +# * "result_checks" - optional, a list of strings. Each string is an +# expression that is placed within a gdb_assert after the call to the +# Function/Method implementation. Within each expression the variable +# 'result' can be used to reference the result of the function/method +# implementation. The 'result_checks' can only be used if the 'type' +# of this Function/Method is not 'void'. Info( type="const struct bfd_arch_info *", @@ -568,6 +582,8 @@ should never return nullptr. type="const char *", name="register_name", params=[("int", "regnr")], + param_checks=["regnr >= 0", "regnr < gdbarch_num_cooked_regs (gdbarch)"], + result_checks=["result != nullptr"], predefault="0", invalid=True, ) diff --git a/gdb/gdbarch.c b/gdb/gdbarch.c index 0edae7f6f0a..81e26362954 100644 --- a/gdb/gdbarch.c +++ b/gdb/gdbarch.c @@ -2229,9 +2229,13 @@ gdbarch_register_name (struct gdbarch *gdbarch, int regnr) { gdb_assert (gdbarch != NULL); gdb_assert (gdbarch->register_name != NULL); + gdb_assert (regnr >= 0); + gdb_assert (regnr < gdbarch_num_cooked_regs (gdbarch)); if (gdbarch_debug >= 2) gdb_printf (gdb_stdlog, "gdbarch_register_name called\n"); - return gdbarch->register_name (gdbarch, regnr); + auto result = gdbarch->register_name (gdbarch, regnr); + gdb_assert (result != nullptr); + return result; } void diff --git a/gdb/gdbarch.py b/gdb/gdbarch.py index 696b3028e6f..699ed4f69d2 100755 --- a/gdb/gdbarch.py +++ b/gdb/gdbarch.py @@ -112,6 +112,8 @@ class Function(_Component): postdefault=None, invalid=None, printer=None, + param_checks=None, + result_checks=None, ): super().__init__( comment=comment, @@ -124,6 +126,11 @@ class Function(_Component): printer=printer, params=params, ) + self.param_checks = param_checks + self.result_checks = result_checks + + if self.type == "void" and self.result_checks: + raise Exception("can't have result checks with a void return type") def ftype(self): "Return the name of the function typedef to use." @@ -444,6 +451,9 @@ with open("gdbarch.c", "w") as f: f" /* Do not check predicate: {c.get_predicate()}, allow call. */", file=f, ) + if c.param_checks: + for rule in c.param_checks: + print(f" gdb_assert ({rule});", file=f) print(" if (gdbarch_debug >= 2)", file=f) print( f""" gdb_printf (gdb_stdlog, "gdbarch_{c.name} called\\n");""", @@ -451,8 +461,15 @@ with open("gdbarch.c", "w") as f: ) print(" ", file=f, end="") if c.type != "void": - print("return ", file=f, end="") + if c.result_checks: + print("auto result = ", file=f, end="") + else: + print("return ", file=f, end="") print(f"gdbarch->{c.name} ({c.actuals()});", file=f) + if c.type != "void" and c.result_checks: + for rule in c.result_checks: + print(f" gdb_assert ({rule});", file=f) + print(" return result;", file=f) print("}", file=f) print(file=f) print("void", file=f) -- 2.25.4