Coverage Report

Created: 2026-12-01 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebsockets/lib/plat/unix/unix-crash.c
Line
Count
Source
1
/*
2
 * libwebsockets - small server side websockets and web server implementation
3
 *
4
 * Copyright (C) 2010 - 2026 Andy Green <andy@warmcat.com>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to
8
 * deal in the Software without restriction, including without limitation the
9
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10
 * sell copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22
 * IN THE SOFTWARE.
23
 *
24
 * A backtrace on stderr when the process crashes, see
25
 * READMEs/README.lws_backtrace.md
26
 */
27
28
#if !defined(_GNU_SOURCE)
29
#define _GNU_SOURCE
30
#endif
31
#include "private-lib-core.h"
32
33
#include <execinfo.h>
34
#include <signal.h>
35
36
#define LWS_CRASH_BT_DEPTH  64
37
38
/* libexecinfo on the BSDs counts frames in size_t, glibc and macOS in int */
39
#if defined(LWS_HAVE_BACKTRACE_SIZE_T)
40
typedef size_t lws_bt_count_t;
41
#else
42
typedef int lws_bt_count_t;
43
#endif
44
45
static const int crash_sigs[] = { SIGSEGV, SIGBUS, SIGILL, SIGFPE, SIGABRT };
46
47
/*
48
 * The handler runs on its own stack, so a crash from overflowing the stack
49
 * can still say where it was
50
 */
51
static uint8_t crash_stack[64 * 1024];
52
53
/* only async-signal-safe calls from here until the handler returns */
54
55
static void
56
lws_crash_puts(const char *s)
57
0
{
58
0
  size_t n = strlen(s);
59
0
  ssize_t w;
60
61
0
  while (n) {
62
0
    w = write(2, s, n);
63
0
    if (w <= 0)
64
0
      return;
65
0
    s += w;
66
0
    n -= (size_t)w;
67
0
  }
68
0
}
69
70
static void
71
lws_crash_handler(int sig)
72
0
{
73
0
  void *bt[LWS_CRASH_BT_DEPTH];
74
0
  char num[12], *p = num + sizeof(num);
75
0
  lws_bt_count_t depth;
76
0
  int n = sig;
77
78
0
  *--p = '\0';
79
0
  do {
80
0
    *--p = (char)('0' + (n % 10));
81
0
    n /= 10;
82
0
  } while (n && p > num);
83
84
0
  lws_crash_puts("\n*** lws: fatal signal ");
85
0
  lws_crash_puts(p);
86
0
  lws_crash_puts(", backtrace:\n");
87
88
0
  depth = backtrace(bt, (lws_bt_count_t)LWS_ARRAY_SIZE(bt));
89
0
  backtrace_symbols_fd(bt, depth, 2);
90
91
0
  lws_crash_puts("*** lws: end of backtrace\n");
92
93
  /*
94
   * SA_RESETHAND put the default action back, so the process now dies of
95
   * the signal as it would have: the parent sees how it died, and the
96
   * platform makes its core file or crash report
97
   */
98
0
  raise(sig);
99
0
}
100
101
int
102
lws_crash_backtrace_install(void)
103
0
{
104
0
  struct sigaction sa, old;
105
0
  stack_t ss, oss;
106
0
  void *warm;
107
0
  size_t n;
108
109
  /*
110
   * The first backtrace() may load the unwinder, which allocates: get
111
   * that done now, not in the handler
112
   */
113
0
  (void)backtrace(&warm, 1);
114
115
  /* leave the thread's alternate signal stack alone if it has one */
116
0
  if (!sigaltstack(NULL, &oss) && (oss.ss_flags & SS_DISABLE)) {
117
0
    memset(&ss, 0, sizeof(ss));
118
0
    ss.ss_sp  = crash_stack;
119
0
    ss.ss_size  = sizeof(crash_stack);
120
0
    if (sigaltstack(&ss, NULL))
121
0
      return 1;
122
0
  }
123
124
0
  memset(&sa, 0, sizeof(sa));
125
0
  sa.sa_handler  = lws_crash_handler;
126
0
  sa.sa_flags = (int)(SA_ONSTACK | SA_RESETHAND);
127
0
  sigemptyset(&sa.sa_mask);
128
129
0
  for (n = 0; n < LWS_ARRAY_SIZE(crash_sigs); n++) {
130
    /* the application's own handling of a signal comes first */
131
0
    if (sigaction(crash_sigs[n], NULL, &old) ||
132
0
        (old.sa_flags & SA_SIGINFO) || old.sa_handler != SIG_DFL)
133
0
      continue;
134
135
0
    if (sigaction(crash_sigs[n], &sa, NULL))
136
0
      return 1;
137
0
  }
138
139
0
  return 0;
140
0
}
141
142
int
143
lws_plat_callers(uintptr_t *pcs, int max, int skip)
144
0
{
145
0
  void *bt[16];
146
0
  int n, m = 0, depth;
147
148
  /* bt[0] is us, bt[1] the function calling us */
149
0
  depth = (int)backtrace(bt, (lws_bt_count_t)LWS_ARRAY_SIZE(bt));
150
151
0
  for (n = 2 + skip; n < depth && m < max; n++)
152
0
    pcs[m++] = (uintptr_t)bt[n];
153
154
0
  return m;
155
0
}