From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.1 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id B0F3C1FB02 for ; Thu, 15 Dec 2022 20:52:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1671137579; bh=4Kj+INPLwOwzFMKd2HAnLZb0CX1YhMc6X94ec1xMz54=; h=From:To:Subject:Date:In-Reply-To:References:From; b=lHLmR1dpasQpQfvYX9vfQzbTKDz4b/vXiO0fZRjvg1NwtdDfnZUk9epUucHElnpsF j83lRq7GbzBK7xJRcEZ0JJUFyM3fOlGKaG0u6hd3PJtMPh51Of36tplQZmbaJ+YDQN A2i56UP8YSkOTMKiBKZONGoYvO0K2Eo/twPK/uUI= From: Eric Wong To: mwrap-perl@80x24.org Subject: [PATCH 19/19] avoid -Warray-bounds warning, avoid stack overallocation Date: Thu, 15 Dec 2022 20:52:55 +0000 Message-Id: <20221215205255.27840-20-e@80x24.org> In-Reply-To: <20221215205255.27840-1-e@80x24.org> References: <20221215205255.27840-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Compilers don't like seeing `sl.bt[-1]', so we give backtrace(3) the same address by another means. We'll also save allocating one pointer off the stack by capping the union padding. --- mwrap_core.h | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/mwrap_core.h b/mwrap_core.h index 1b3d98f..3fd67f1 100644 --- a/mwrap_core.h +++ b/mwrap_core.h @@ -134,6 +134,7 @@ static void *my_mempcpy(void *dest, const void *src, size_t n) #define RETURN_ADDRESS(nr) \ __builtin_extract_return_addr(__builtin_return_address(nr)) + #define SRC_LOC_BT(bt) union stk_bt bt; do { \ uint32_t depth = locating ? 1 : bt_req_depth; \ switch (depth) { \ @@ -142,7 +143,7 @@ static void *my_mempcpy(void *dest, const void *src, size_t n) default: /* skip 1st level of BT since thats our function */ \ mwrap_assert(bt_req_depth <= MWRAP_BT_MAX); \ ++locating; \ - long n = (long)backtrace(&bt.sl.bt[-1], bt_req_depth); \ + long n = (long)backtrace(bt_dst(&bt), bt_req_depth); \ --locating; \ bt.sl.bt_len = n <= 1 ? 0 : (uint32_t)n - 1; \ if (n > 1) mwrap_assert(bt.sl.bt[0] == RETURN_ADDRESS(0)); \ @@ -209,9 +210,27 @@ union stk_sf { union stk_bt { struct src_loc sl; - char buf_[sizeof(struct src_loc) + sizeof(void *) * MWRAP_BT_MAX]; + /* we subtract one level from MWRAP_BT_MAX since we discard one + * level of backtrace(3) (see below for why) */ + char buf_[sizeof(struct src_loc) + sizeof(void *) * (MWRAP_BT_MAX-1)]; }; +/* + * we discard the 1st-level of the backtrace(3) since it's our *alloc + * function (and therefore uninteresting), so we want backtrace(3) to + * write to bt->sl.bt[-1] so that bt->sl.bt[0] is the first interesting + * thing. + */ +#ifdef static_assert +static_assert(offsetof(struct src_loc, lineno) + sizeof(void *) == + offsetof(struct src_loc, bt), + "bt lineno is is bt[-1]"); +#endif +static void **bt_dst(union stk_bt *bt) +{ + return (void **)&bt->sl.lineno; +} + static struct alloc_hdr *ptr2hdr(void *p) { return (struct alloc_hdr *)((uintptr_t)p - sizeof(struct alloc_hdr));