From patchwork Tue Apr 14 15:17:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: William Tu X-Patchwork-Id: 1270418 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.133; helo=hemlock.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=DpD8/diF; dkim-atps=neutral Received: from hemlock.osuosl.org (smtp2.osuosl.org [140.211.166.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 491pyT5rlMz9sT0 for ; Wed, 15 Apr 2020 01:17:52 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by hemlock.osuosl.org (Postfix) with ESMTP id 9AA6287CEE; Tue, 14 Apr 2020 15:17:49 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from hemlock.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 2sQvwpFY4SkD; Tue, 14 Apr 2020 15:17:41 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by hemlock.osuosl.org (Postfix) with ESMTP id EDF7387834; Tue, 14 Apr 2020 15:17:40 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id ABAEFC1D7D; Tue, 14 Apr 2020 15:17:40 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from hemlock.osuosl.org (smtp2.osuosl.org [140.211.166.133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 324DAC0172 for ; Tue, 14 Apr 2020 15:17:39 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by hemlock.osuosl.org (Postfix) with ESMTP id 1CA9887834 for ; Tue, 14 Apr 2020 15:17:39 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from hemlock.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id mhjqCzwMP9Ku for ; Tue, 14 Apr 2020 15:17:37 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by hemlock.osuosl.org (Postfix) with ESMTPS id C2E2F877B5 for ; Tue, 14 Apr 2020 15:17:37 +0000 (UTC) Received: by mail-pg1-f196.google.com with SMTP id t11so25741pgg.2 for ; Tue, 14 Apr 2020 08:17:37 -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:mime-version :content-transfer-encoding; bh=RjFOQnPnPir4E2CC70YeWN7S+OmYgsgGV86J0VSplBY=; b=DpD8/diFkMy3AyqYvhcmehwrpubsGpGDrWtso2IR4EjO+V0hLecUEGL5sU9d6duECD GwSouWmgWj+NJN50YECc63K/iXRmg0B9GfckbklU74H48JITbuyKqJObuKHuRN4YflFB vjLxVoimdejMSp0tqIVsUg790l5faZV1DpA3Nd5VHo0rHSzL44jZQ463NxbT5JWM06eN St7TI9hpjuj7b5coTp0GRiyhguK+6mNtEH7iaiDW15Ox2SY/GZyuZ7Z5u/QiodhoWZvV AeYqrfzdCu7tkA38+IRnnTW3UuFFtefQtf6A9Mrf4K/965i45TT1SEY3IjSXFGwmDmPB p1ow== 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:mime-version :content-transfer-encoding; bh=RjFOQnPnPir4E2CC70YeWN7S+OmYgsgGV86J0VSplBY=; b=VAVKbAak2f+cQ4dOl1KX1CQhrogksglqhYvs3i/ZEtOa9saJtZsfSqazWalc0Y59YQ +1Y0a6Gho3GcpouMXSmHI03I7bQcAt/i+C7IbtaApUgxVbEp1+smVfDaC1D7OzJMzERG Z01wiRpPD3UvTcqr/karGkcWp8CgGNeQG7/TKYSOSjXZkLDrp47nO5qtRRk4Y1bWn69y r3MsVTjWMl60XASvm/y5NBdzWgOe4vCaaH6qHbU27c26O7fpU8DnaXss7NelQVh9EHqi KkWnnAD6VfAkj6uUal0SxDY6QY0cDVMq2AxJbN7FPzlVWVylYhA4+HgMPt5VClgr8bOX faPg== X-Gm-Message-State: AGi0PuadPs2NejwwaUQpO8EJJXvnlPSWKxQLhrJiE4jRtN/sVJXWGXSA dQGyyftGv5PhMwVYThmC/XHkmts+ X-Google-Smtp-Source: APiQypKR53N/8bL0klI7B7qM3/yGTnb1u98dJHswnO8a/Q5ZnIgqqRuccclrj1mLwzKfXyoSohPjlw== X-Received: by 2002:aa7:9218:: with SMTP id 24mr5837733pfo.312.1586877456469; Tue, 14 Apr 2020 08:17:36 -0700 (PDT) Received: from sc9-mailhost2.vmware.com (c-76-21-95-192.hsd1.ca.comcast.net. [76.21.95.192]) by smtp.gmail.com with ESMTPSA id r9sm10970078pfg.2.2020.04.14.08.17.35 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 14 Apr 2020 08:17:35 -0700 (PDT) From: William Tu To: dev@openvswitch.org Date: Tue, 14 Apr 2020 08:17:04 -0700 Message-Id: <1586877424-63431-1-git-send-email-u9012063@gmail.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Subject: [ovs-dev] [PATCHv3] 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: , 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/674901331 Signed-off-by: William Tu Tested-by: Yifeng Sun Reviewed-by: Yifeng Sun --- v3: - use memcpy to avoid gcc10 warning below lib/fatal-signal.c: In function ‘send_backtrace_to_monitor’: lib/fatal-signal.c:234:13: warning: ‘__builtin_strncat’ output may be truncated copying 32 bytes from a string of length 1535 [ ]8;;https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wstringop-truncation-Wstringop-truncation ]8;;] 234 | strncat(line, unw_bt[i].func, UNW_MAX_FUNCN); | ^~~~~~~ --- 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..bbb31ef27517 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], fn_name[UNW_MAX_FUNCN]; 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(fn_name, 0, sizeof fn_name); + memset(offset_str, 0, sizeof offset_str); + memset(ip_str, ' ', sizeof ip_str); + ip_str[sizeof(ip_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, "<"); + memcpy(fn_name, unw_bt[i].func, UNW_MAX_FUNCN - 1); + strcat(line, fn_name); + strcat(line, "+0x"); + strcat(line, offset_str); + strcat(line, ">\n"); vlog_direct_write_to_log_file_unsafe(line); } }