From patchwork Wed Mar 25 14:34:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Tu X-Patchwork-Id: 1261422 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=rlQyuvHR; 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 48nVy35RYLz9sRR for ; Thu, 26 Mar 2020 01:34:50 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by hemlock.osuosl.org (Postfix) with ESMTP id 1DD2588677; Wed, 25 Mar 2020 14:34: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 GMrIx8DeJR2C; Wed, 25 Mar 2020 14:34:48 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by hemlock.osuosl.org (Postfix) with ESMTP id 2CB0D885FB; Wed, 25 Mar 2020 14:34:48 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 019B9C1D74; Wed, 25 Mar 2020 14:34:48 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from whitealder.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 75536C0177 for ; Wed, 25 Mar 2020 14:34:46 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by whitealder.osuosl.org (Postfix) with ESMTP id 5F00E87998 for ; Wed, 25 Mar 2020 14:34:46 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from whitealder.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 60-+rdEisNk4 for ; Wed, 25 Mar 2020 14:34:45 +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 whitealder.osuosl.org (Postfix) with ESMTPS id D4DCC879C2 for ; Wed, 25 Mar 2020 14:34:45 +0000 (UTC) Received: by mail-pg1-f196.google.com with SMTP id t24so1224644pgj.7 for ; Wed, 25 Mar 2020 07:34:45 -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=7eTFPjGjdhi2NHi3FxOjryrJdmYdsn+7z2Gflr/OknY=; b=rlQyuvHRzqleObXzGWOqfvgncVIhmnunQ4s4QxE/HXJc+h7SPcdrXKZDzMguio2HQk hKLXzC635lD3HqKesRgrrfODTKJ+AN3O/KIYvX6i+rM9TdJixENZGsXSxz5CCagx28K6 Mkm2VKlyyCZwNDHezN+XnhUhV6uVJk075/7zK/jlnYG5n7GQo/ZFf1YWiM+ZG0+rbNwe dNEQjY+2PQdG5JaZmpX13dLdA/M5b2L4xc+UaD+nP20JayOzIS870f46/O9TLBPrfLFT G9OoSwKhKV+h0qJT2MZJp7oOm8Gpn6akpXb+YNtdvdkPp4VI7tSghSo9xEMrBmGBTFP7 qyAQ== 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=7eTFPjGjdhi2NHi3FxOjryrJdmYdsn+7z2Gflr/OknY=; b=GAqr4SNjUzSMxU/tX/PRG1TujcxrDcxEZAt6szmNsOmgtjFxPSkI7/7MYif+Szkfso a5cCZhLriyk/67gNE7bDhnk/u2PAdGNTvVeIfDJyDAySUKE1zkdA5mXwu4Xe57mS56ZB x5CqF3g/r2QOd2uK40IdGzPRW5sXbxh9/k4UPPaJu2xeOYVDQZMvBQlg3i/XK8rqg6S6 jvjUkuZ7qEMS2ahGHwFiqxjCI+gKpuQXeUXMa0DNX3HXydrrCpQ0zMLGxJxL1JY8llpL qC8wd0x9L1+14G8dV/mMzjVtHIoEZoq+pdVqcdiltFRT8OBv+fQ8lVxdP8+keArboDlH Vh0w== X-Gm-Message-State: ANhLgQ1dfPQus9jJe882ReQgOd0pWqN2+N9Rvw013dtwFnfLdx/iSSPU kop7K8InPycqIuqev2mA85mBT3Iy X-Google-Smtp-Source: ADFU+vtf6077h+t51Pd5NdYuMzA1CI9LmYC6yv4eDyHWl007YxYPza4qwM2zzFmB+O2D4nVSpk2+Jg== X-Received: by 2002:a63:e70d:: with SMTP id b13mr3457266pgi.8.1585146884820; Wed, 25 Mar 2020 07:34:44 -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 t66sm4666591pjb.45.2020.03.25.07.34.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 25 Mar 2020 07:34:43 -0700 (PDT) From: William Tu To: dev@openvswitch.org Date: Wed, 25 Mar 2020 07:34:13 -0700 Message-Id: <1585146853-129779-1-git-send-email-u9012063@gmail.com> X-Mailer: git-send-email 2.7.4 Cc: i.maximets@ovn.org Subject: [ovs-dev] [PATCH] 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/666596271 Signed-off-by: William Tu --- lib/fatal-signal.c | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/lib/fatal-signal.c b/lib/fatal-signal.c index 51cf628d994e..52794a0722cb 100644 --- a/lib/fatal-signal.c +++ b/lib/fatal-signal.c @@ -158,6 +158,27 @@ 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; + char res; + + if (value / 16 > 0) { + i = llong_to_hex_str(value / 16, str); + } + + res = (char)(value % 16); + if (res < 10) { + str[i] = '0' + res; + } else { + str[i] = 'a' + res - 10; + } + return i + 1; +} + /* Send the backtrace buffer to monitor thread. * * Note that this runs in the signal handling context, any system @@ -192,20 +213,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); + + line = strcat(line, "0x"); + line = strcat(line, ip_str); + line = strcat(line, "<"); + line = strcat(line, unw_bt[i].func); + line = strcat(line, "+0x"); + line = strcat(line, offset_str); + line = strcat(line, ">\n"); vlog_direct_write_to_log_file_unsafe(line); } }