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=-1.4 required=3.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, UNWANTED_LANGUAGE_BODY 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 CAD111F910 for ; Fri, 18 Nov 2022 07:58:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1668758297; bh=p2mJUEhrGixidIDHX4IrvKKUpzIH7J9LSf0VNoMsnDI=; h=From:To:Subject:Date:From; b=0uCCShRnSdlIBQ+2+zmf/bDiHknvWomKg6Te2ITNgx2q68W7h7d8TFiWuPt4r1Gwn dRMRvxe5N5Tx4jUXNsk1sJill7a4QmpeZgzWFjhDbnMbNq6tz6Vq+Em0A89exeO8H2 wKI0ScyBKgW7GHvhsRc7Wciyw3zWEcSEIWSCPAwY= From: Eric Wong To: mwrap-perl@80x24.org Subject: [PATCH] eliminate alloca(3) use Date: Fri, 18 Nov 2022 07:58:17 +0000 Message-Id: <20221118075817.1817328-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: alloca(3) is difficult for the compiler and static checkers to deal with, and we already reserve TSD space for storing path names. --- Mwrap.xs | 2 +- mwrap_core.h | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Mwrap.xs b/Mwrap.xs index b515894..367a4c3 100644 --- a/Mwrap.xs +++ b/Mwrap.xs @@ -153,7 +153,7 @@ mwrap_get(loc) PREINIT: STRLEN len; const char *str; - struct src_loc *k = (void *)kbuf; + struct src_loc *k = &tsd.src_loc; uintptr_t p; struct cds_lfht_iter iter; struct cds_lfht_node *cur; diff --git a/mwrap_core.h b/mwrap_core.h index 3abc3dd..09b7930 100644 --- a/mwrap_core.h +++ b/mwrap_core.h @@ -178,9 +178,12 @@ struct alloc_hdr { }; /* $PATHNAME:$LINENO */ -static MWRAP_TSD char kbuf[ - sizeof(struct src_loc) + PATH_MAX + sizeof(":") + UINT2STR_MAX -]; +static MWRAP_TSD union { + char kbuf[ + sizeof(struct src_loc) + PATH_MAX + sizeof(":") + UINT2STR_MAX + ]; + struct src_loc src_loc; +} tsd; static struct alloc_hdr *ptr2hdr(void *p) { @@ -290,7 +293,7 @@ static struct src_loc *assign_line(size_t size, const char *file, unsigned line) len = strlen(file); if (len > PATH_MAX) len = PATH_MAX; - k = (void *)kbuf; + k = &tsd.src_loc; k->total = size; dst = mempcpy(k->k, file, len); *dst++ = ':'; @@ -311,7 +314,6 @@ static struct src_loc * update_stats_rcu_lock(size_t *generation, size_t size, uintptr_t caller) { struct src_loc *k, *ret = 0; - static const size_t xlen = sizeof(caller); struct cds_lfht *t = CMM_LOAD_SHARED(totals); const COP *cop = NULL; @@ -326,11 +328,11 @@ update_stats_rcu_lock(size_t *generation, size_t size, uintptr_t caller) ret = assign_line(size, OutCopFILE(cop), CopLINE(cop)); #endif /* MWRAP_PERL */ if (!ret) { - k = alloca(sizeof(*k) + xlen); + k = &tsd.src_loc; k->total = size; - memcpy(k->k, &caller, xlen); + memcpy(k->k, &caller, sizeof(caller)); k->capa = 0; - hash_loc(k, xlen); + hash_loc(k, sizeof(caller)); ret = totals_add_rcu(k); } out: @@ -672,7 +674,8 @@ static void dump_destructor(void) *dump_path) { char *end = strchr(dump_path, ','); if (end) { - char *tmp = alloca(end - dump_path + 1); + char *tmp = tsd.kbuf; + assert((end - dump_path) < sizeof(tsd.kbuf)); end = mempcpy(tmp, dump_path, end - dump_path); *end = 0; dump_path = tmp;