From patchwork Wed Mar 25 18:49:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Tu X-Patchwork-Id: 1261603 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.136; helo=silver.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=ktfDS2GQ; dkim-atps=neutral Received: from silver.osuosl.org (smtp3.osuosl.org [140.211.166.136]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48ncd05zlKz9sQt for ; Thu, 26 Mar 2020 05:50:27 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id E2BF72549C; Wed, 25 Mar 2020 18:50:23 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id t5Lc7thWopzy; Wed, 25 Mar 2020 18:50:19 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by silver.osuosl.org (Postfix) with ESMTP id C0C4C21539; Wed, 25 Mar 2020 18:50:19 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 9F8F9C1D74; Wed, 25 Mar 2020 18:50:19 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from fraxinus.osuosl.org (smtp4.osuosl.org [140.211.166.137]) by lists.linuxfoundation.org (Postfix) with ESMTP id 56F2FC0177 for ; Wed, 25 Mar 2020 18:50:18 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by fraxinus.osuosl.org (Postfix) with ESMTP id 41DD48563A for ; Wed, 25 Mar 2020 18:50:05 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from fraxinus.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 9t4hjk3VpH1t for ; Wed, 25 Mar 2020 18:50:04 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by fraxinus.osuosl.org (Postfix) with ESMTPS id 14C7C85540 for ; Wed, 25 Mar 2020 18:50:04 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id e1so1148082plt.9 for ; Wed, 25 Mar 2020 11:50:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=B8tljjiJ1GeHNKUOiSEKlvxAQvfmYsMxSakqukuFqYE=; b=ktfDS2GQCZLT9UofVMdSHy4Ep9KVCMRjr2ZPx8TVsf1qWwfzKPu5/0GJj/3v5sGCEw SJmqOA56iTZXlJ3DZEdLp1Wbkpc+KQIJ2w+BJ5kPvnA11iBEOteUatykF1XKmvHirb2j 6l//0SUrSc8J4S0rg5J0rlz0GsJfrhpbLQ1q/AFhrmmkjfoP22a744jB6HFlQuWYQaKn JQfZbbei9r6exTsQcGMY5abUxxpB5+RYNduwSXoDmNVCeDEV5iyOkobQgB8IfnsiHE5B 5uLyLgSag0FZ3WHHKNRWL03vSHYv/Iq+Ke2m0QFGKuoEDgNEQYWr59ivvpYGLhrtPXKB S8fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=B8tljjiJ1GeHNKUOiSEKlvxAQvfmYsMxSakqukuFqYE=; b=giEJGIwiolpI3IW3SHEQc+aZDg27mE5qd3J1kK4He786crvu9Ma7+1GpbPetcJTVdb az4bjgQDiflsHdRtySueKBs1HfknJK7D0vI4CRoG/D8bwEmgbX1VW5TRSYOD595D7S5a jiXNStd2XdTbS+8gWVSVWnrTlzJHchsjhTQNGP2JWxGOYfONx/8oF85ljoQ9PqEuUAQ5 vHOSnd0x8hwcaQOCRE8Kj57xUOL32TfxPJcg+tLUga3Xq2H6eJdwiRlqFyCf6XBsuv2d X7gudhwNmcTHKXqEudcY/QGJ3k2JlZo/SC3N72vbvkKAyvBkLliJZU+L+DAbw1oqxtO8 UmKg== X-Gm-Message-State: ANhLgQ1B3uTDO2hbzV+VEHsxYFPby56XKnuqIbBEbF9oV3XF+Abvijzr 0Zx6HU9KHSr6owAD1yxa4db/frta5f0= X-Google-Smtp-Source: ADFU+vuZ3H1oJYo9BpK5Vd7qUq0uwqJyf14bk9yWblfwhw5VBHa6ivmJ7+Q/06qNXyumWQ42tDX+EQ== X-Received: by 2002:a17:90a:32c5:: with SMTP id l63mr5434743pjb.47.1585162203120; Wed, 25 Mar 2020 11:50:03 -0700 (PDT) Received: from sc9-mailhost3.vmware.com (c-76-21-95-192.hsd1.ca.comcast.net. [76.21.95.192]) by smtp.gmail.com with ESMTPSA id w24sm12713pjn.14.2020.03.25.11.50.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 25 Mar 2020 11:50:02 -0700 (PDT) From: William Tu To: dev@openvswitch.org Date: Wed, 25 Mar 2020 11:49:36 -0700 Message-Id: <1585162176-84776-1-git-send-email-u9012063@gmail.com> X-Mailer: git-send-email 2.7.4 Subject: [ovs-dev] [PATCHv2] fatal-signal: Remove snprintf. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" Function snprintf is not async-signal-safe. Replace it with our own implementation. Example ovs-vswitchd.log output: 2020-03-25T01:08:19.673Z|00050|memory|INFO|handlers:2 ports:3 SIGSEGV detected, backtrace: 0x4872d9 0x7f4e2ab974b0 0x7f4e2ac5d74d <__poll+0x2d> 0x531098 0x51aefc 0x445ca9 0x5056fd 0x7f4e2b65f6ba 0x7f4e2ac6941d 0x0 <+0x0> Tested-at: https://travis-ci.org/github/williamtu/ovs-travis/builds/666875084 Signed-off-by: William Tu Tested-by: Yifeng Sun Reviewed-by: Yifeng Sun --- v2: - avoid strcat overflow buffer, switch to use strncat - some code refactor --- lib/fatal-signal.c | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/lib/fatal-signal.c b/lib/fatal-signal.c index 51cf628d994e..e033f1ec59ec 100644 --- a/lib/fatal-signal.c +++ b/lib/fatal-signal.c @@ -158,6 +158,23 @@ fatal_signal_add_hook(void (*hook_cb)(void *aux), void (*cancel_cb)(void *aux), } #ifdef HAVE_UNWIND +/* Convert unsigned long long to string. This is needed because + * using snprintf() is not async signal safe. */ +static inline int +llong_to_hex_str(unsigned long long value, char *str) +{ + int i = 0, res; + + if (value / 16 > 0) { + i = llong_to_hex_str(value / 16, str); + } + + res = value % 16; + str[i] = "0123456789abcdef"[res]; + + return i + 1; +} + /* Send the backtrace buffer to monitor thread. * * Note that this runs in the signal handling context, any system @@ -192,20 +209,32 @@ send_backtrace_to_monitor(void) { dep * sizeof(struct unw_backtrace))); } else { /* Since there is no monitor daemon running, write backtrace - * in current process. This is not asyn-signal-safe due to - * use of snprintf(). + * in current process. */ char str[] = "SIGSEGV detected, backtrace:\n"; + char ip_str[16], offset_str[6]; + char _line[64]; + char *line = (char *)_line; vlog_direct_write_to_log_file_unsafe(str); for (int i = 0; i < dep; i++) { - char line[64]; - - snprintf(line, 64, "0x%016"PRIxPTR" <%s+0x%"PRIxPTR">\n", - unw_bt[i].ip, - unw_bt[i].func, - unw_bt[i].offset); + memset(line, 0, sizeof _line); + memset(ip_str, ' ', sizeof ip_str); + memset(offset_str, 0, sizeof offset_str); + ip_str[sizeof(ip_str) - 1] = 0; + offset_str[sizeof(offset_str) - 1] = 0; + + llong_to_hex_str(unw_bt[i].ip, ip_str); + llong_to_hex_str(unw_bt[i].offset, offset_str); + + strcat(line, "0x"); + strcat(line, ip_str); + strcat(line, "<"); + strncat(line, unw_bt[i].func, UNW_MAX_FUNCN); + strcat(line, "+0x"); + strcat(line, offset_str); + strcat(line, ">\n"); vlog_direct_write_to_log_file_unsafe(line); } }