From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id E711F1F545 for ; Sun, 11 Jun 2023 21:33:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=yhbt.net; s=selector1; t=1686519208; bh=c3jfXlZPS53FFY9OEegykz33vIPyNzEvZDMtWnWu7nU=; h=From:To:Subject:Date:In-Reply-To:References:From; b=SoZvgLGiilB850aHSm9XPBTnkKI7IAk60zPywphXmYIuaK/uFAQtoGzq41rBbxSGB doYVH8tCzAlu9Q2XSToGatM+KlMC20PZCbf++Qkx9RoiU1UuUNkgDrzk9SN6IcrwCl g7PICJPtkEIBL8bbWqqh/H8ztfcMD7vkIEDafT5o= From: Eric Wong To: raindrops-public@yhbt.net Subject: [PATCH 3/4] linux_inet_diag: get rid of alloca usage Date: Sun, 11 Jun 2023 21:33:27 +0000 Message-Id: <20230611213328.379546-4-bofh@yhbt.net> In-Reply-To: <20230611213328.379546-1-bofh@yhbt.net> References: <20230611213328.379546-1-bofh@yhbt.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: alloca makes stack usage unpredictable and life difficult for static analysis tools and compilers. The 46 bytes of INET6_ADDRSTRLEN is fine to keep on stack, but page size can be several MB large in some architectures (but typically 4K on common architectures). Thus we handle page size-ed allocations via `rb_str_tmp_new'. `rb_str_tmp_new' has been in public Ruby headers since the 1.9 days and used by the core `zlib', `digest', and `zlib' extensions, so it should be safe to use (and `rb_str_resize' is used in many more C extensions). --- ext/raindrops/linux_inet_diag.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/ext/raindrops/linux_inet_diag.c b/ext/raindrops/linux_inet_diag.c index e1ae62a..2d4f503 100644 --- a/ext/raindrops/linux_inet_diag.c +++ b/ext/raindrops/linux_inet_diag.c @@ -212,24 +212,25 @@ static void bug_warn_nogvl(const char *fmt, ...) static struct listen_stats *stats_for(st_table *table, struct inet_diag_msg *r) { char *host, *key, *port, *old_key; - size_t alloca_len; struct listen_stats *stats; socklen_t hostlen; socklen_t portlen = (socklen_t)sizeof("65535"); int n; const void *src = r->id.idiag_src; + char buf[INET6_ADDRSTRLEN]; + size_t buf_len; switch (r->idiag_family) { case AF_INET: { hostlen = INET_ADDRSTRLEN; - alloca_len = hostlen + portlen; - host = key = alloca(alloca_len); + buf_len = hostlen + portlen; + host = key = buf; break; } case AF_INET6: { hostlen = INET6_ADDRSTRLEN; - alloca_len = 1 + hostlen + 1 + portlen; - key = alloca(alloca_len); + buf_len = 1 + hostlen + 1 + portlen; + key = buf; host = key + 1; break; } @@ -269,7 +270,7 @@ static struct listen_stats *stats_for(st_table *table, struct inet_diag_msg *r) old_key = key; if (r->idiag_state == TCP_ESTABLISHED) { - n = snprintf(key, alloca_len, "%s:%u", + n = snprintf(key, buf_len, "%s:%u", addr_any(r->idiag_family), ntohs(r->id.idiag_sport)); if (n <= 0) { @@ -615,7 +616,7 @@ static VALUE tcp_listener_stats(int argc, VALUE *argv, VALUE self) { VALUE rv = rb_hash_new(); struct nogvl_args args; - VALUE addrs, sock; + VALUE addrs, sock, buf; rb_scan_args(argc, argv, "02", &addrs, &sock); @@ -624,8 +625,9 @@ static VALUE tcp_listener_stats(int argc, VALUE *argv, VALUE self) * buffer for recvmsg() later, we already checked for * OPLEN <= page_size at initialization */ + buf = rb_str_buf_new(page_size); args.iov[2].iov_len = OPLEN; - args.iov[2].iov_base = alloca(page_size); + args.iov[2].iov_base = RSTRING_PTR(buf); args.table = NULL; sock = NIL_P(sock) ? rb_funcall(cIDSock, id_new, 0) : rb_io_get_io(sock); @@ -672,6 +674,7 @@ static VALUE tcp_listener_stats(int argc, VALUE *argv, VALUE self) rb_hash_foreach(rv, drop_placeholders, Qfalse); /* let GC deal with corner cases */ + rb_str_resize(buf, 0); if (argc < 2) rb_io_close(sock); return rv; }