diff mbox series

[ovs-dev,PATCHv3] fatal-signal: Remove snprintf.

Message ID 1586877424-63431-1-git-send-email-u9012063@gmail.com
State Accepted
Commit 925511592a4e239250aed8cf80bd4dffe4e47486
Headers show
Series [ovs-dev,PATCHv3] fatal-signal: Remove snprintf. | expand

Commit Message

William Tu April 14, 2020, 3:17 p.m. UTC
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         <fatal_signal_handler+0x49>
  0x7f4e2ab974b0   <killpg+0x40>
  0x7f4e2ac5d74d   <__poll+0x2d>
  0x531098         <time_poll+0x108>
  0x51aefc         <poll_block+0x8c>
  0x445ca9         <udpif_revalidator+0x289>
  0x5056fd         <ovsthread_wrapper+0x7d>
  0x7f4e2b65f6ba   <start_thread+0xca>
  0x7f4e2ac6941d   <clone+0x6d>
  0x0              <+0x0>

Tested-at: https://travis-ci.org/github/williamtu/ovs-travis/builds/674901331
Signed-off-by: William Tu <u9012063@gmail.com>
---
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(-)

Comments

Yifeng Sun April 14, 2020, 10:55 p.m. UTC | #1
Thanks for fixing gcc10, looks good to me.

Tested-by: Yifeng Sun <pkusunyifeng@gmail.com>

Reviewed-by: Yifeng Sun <pkusunyifeng@gmail.com>

On Tue, Apr 14, 2020 at 8:17 AM William Tu <u9012063@gmail.com> wrote:

> 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         <fatal_signal_handler+0x49>
>   0x7f4e2ab974b0   <killpg+0x40>
>   0x7f4e2ac5d74d   <__poll+0x2d>
>   0x531098         <time_poll+0x108>
>   0x51aefc         <poll_block+0x8c>
>   0x445ca9         <udpif_revalidator+0x289>
>   0x5056fd         <ovsthread_wrapper+0x7d>
>   0x7f4e2b65f6ba   <start_thread+0xca>
>   0x7f4e2ac6941d   <clone+0x6d>
>   0x0              <+0x0>
>
> Tested-at:
> https://travis-ci.org/github/williamtu/ovs-travis/builds/674901331
> Signed-off-by
> <https://travis-ci.org/github/williamtu/ovs-travis/builds/674901331Signed-off-by>:
> William Tu <u9012063@gmail.com>
> ---
> 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);
>          }
>      }
> --
> 2.7.4
>
>
William Tu April 19, 2020, 4:20 p.m. UTC | #2
On Tue, Apr 14, 2020 at 03:55:55PM -0700, Yifeng Sun wrote:
> Thanks for fixing gcc10, looks good to me.
> 
> Tested-by: Yifeng Sun <pkusunyifeng@gmail.com>
> 
> Reviewed-by: Yifeng Sun <pkusunyifeng@gmail.com>
> 
Thanks, I applied to master.
William
diff mbox series

Patch

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);
         }
     }