2020-07-04 22:24:13 +00:00
|
|
|
/*
|
|
|
|
* CDDL HEADER START
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the terms of the
|
|
|
|
* Common Development and Distribution License (the "License").
|
|
|
|
* You may not use this file except in compliance with the License.
|
|
|
|
*
|
|
|
|
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
2022-07-11 21:16:13 +00:00
|
|
|
* or https://opensource.org/licenses/CDDL-1.0.
|
2020-07-04 22:24:13 +00:00
|
|
|
* See the License for the specific language governing permissions
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
|
|
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
|
|
* If applicable, add the following below this CDDL HEADER, with the
|
|
|
|
* fields enclosed by brackets "[]" replaced with your own identifying
|
|
|
|
* information: Portions Copyright [yyyy] [name of copyright owner]
|
|
|
|
*
|
|
|
|
* CDDL HEADER END
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
|
|
|
|
* Use is subject to license terms.
|
|
|
|
*/
|
2024-04-21 11:43:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2024, Rob Norris <robn@despairlabs.com>
|
|
|
|
*/
|
2020-07-04 22:24:13 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
2024-04-28 02:49:58 +00:00
|
|
|
#include <pthread.h>
|
2020-07-04 22:24:13 +00:00
|
|
|
|
2024-04-21 11:43:53 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#ifdef HAVE_GETTID
|
|
|
|
#define libspl_gettid() gettid()
|
|
|
|
#else
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#define libspl_gettid() ((pid_t)syscall(__NR_gettid))
|
|
|
|
#endif
|
|
|
|
#define libspl_getprogname() (program_invocation_short_name)
|
|
|
|
#define libspl_getthreadname(buf, len) \
|
|
|
|
prctl(PR_GET_NAME, (unsigned long)(buf), 0, 0, 0)
|
2024-05-09 14:42:51 +00:00
|
|
|
#elif defined(__FreeBSD__) || defined(__APPLE__)
|
|
|
|
#if !defined(__APPLE__)
|
2024-04-21 11:43:53 +00:00
|
|
|
#include <pthread_np.h>
|
|
|
|
#define libspl_gettid() pthread_getthreadid_np()
|
2024-05-09 14:42:51 +00:00
|
|
|
#endif
|
2024-04-21 11:43:53 +00:00
|
|
|
#define libspl_getprogname() getprogname()
|
|
|
|
#define libspl_getthreadname(buf, len) \
|
|
|
|
pthread_getname_np(pthread_self(), buf, len);
|
|
|
|
#endif
|
|
|
|
|
2024-04-30 00:37:29 +00:00
|
|
|
#if defined(HAVE_LIBUNWIND)
|
|
|
|
#define UNW_LOCAL_ONLY
|
|
|
|
#include <libunwind.h>
|
|
|
|
|
2024-05-10 00:19:48 +00:00
|
|
|
void
|
2024-04-30 00:37:29 +00:00
|
|
|
libspl_dump_backtrace(void)
|
|
|
|
{
|
|
|
|
unw_context_t uc;
|
|
|
|
unw_cursor_t cp;
|
|
|
|
unw_word_t ip, off;
|
|
|
|
char funcname[128];
|
|
|
|
#ifdef HAVE_LIBUNWIND_ELF
|
|
|
|
char objname[128];
|
|
|
|
unw_word_t objoff;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fprintf(stderr, "Call trace:\n");
|
|
|
|
unw_getcontext(&uc);
|
|
|
|
unw_init_local(&cp, &uc);
|
|
|
|
while (unw_step(&cp) > 0) {
|
|
|
|
unw_get_reg(&cp, UNW_REG_IP, &ip);
|
|
|
|
unw_get_proc_name(&cp, funcname, sizeof (funcname), &off);
|
|
|
|
#ifdef HAVE_LIBUNWIND_ELF
|
|
|
|
unw_get_elf_filename(&cp, objname, sizeof (objname), &objoff);
|
|
|
|
fprintf(stderr, " [0x%08lx] %s+0x%2lx (in %s +0x%2lx)\n",
|
|
|
|
ip, funcname, off, objname, objoff);
|
|
|
|
#else
|
|
|
|
fprintf(stderr, " [0x%08lx] %s+0x%2lx\n", ip, funcname, off);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#elif defined(HAVE_BACKTRACE)
|
2024-04-27 11:35:05 +00:00
|
|
|
#include <execinfo.h>
|
|
|
|
|
2024-05-10 00:19:48 +00:00
|
|
|
void
|
2024-04-27 11:35:05 +00:00
|
|
|
libspl_dump_backtrace(void)
|
|
|
|
{
|
|
|
|
void *btptrs[100];
|
|
|
|
size_t nptrs = backtrace(btptrs, 100);
|
|
|
|
char **bt = backtrace_symbols(btptrs, nptrs);
|
|
|
|
fprintf(stderr, "Call trace:\n");
|
|
|
|
for (size_t i = 0; i < nptrs; i++)
|
|
|
|
fprintf(stderr, " %s\n", bt[i]);
|
|
|
|
free(bt);
|
|
|
|
}
|
|
|
|
#else
|
2024-05-10 00:19:48 +00:00
|
|
|
void
|
|
|
|
libspl_dump_backtrace(void)
|
|
|
|
{
|
|
|
|
}
|
2024-04-27 11:35:05 +00:00
|
|
|
#endif
|
|
|
|
|
2024-05-09 14:42:51 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
static inline uint64_t
|
|
|
|
libspl_gettid(void)
|
|
|
|
{
|
|
|
|
uint64_t tid;
|
|
|
|
|
|
|
|
if (pthread_threadid_np(NULL, &tid) != 0)
|
|
|
|
tid = 0;
|
|
|
|
|
|
|
|
return (tid);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-02-03 22:35:38 +00:00
|
|
|
static boolean_t libspl_assert_ok = B_FALSE;
|
|
|
|
|
|
|
|
void
|
|
|
|
libspl_set_assert_ok(boolean_t val)
|
|
|
|
{
|
|
|
|
libspl_assert_ok = val;
|
|
|
|
}
|
2020-07-04 22:24:13 +00:00
|
|
|
|
2024-04-28 02:49:58 +00:00
|
|
|
static pthread_mutex_t assert_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
2020-07-04 22:24:13 +00:00
|
|
|
/* printf version of libspl_assert */
|
|
|
|
void
|
|
|
|
libspl_assertf(const char *file, const char *func, int line,
|
|
|
|
const char *format, ...)
|
|
|
|
{
|
2024-04-28 02:49:58 +00:00
|
|
|
pthread_mutex_lock(&assert_lock);
|
|
|
|
|
2020-07-04 22:24:13 +00:00
|
|
|
va_list args;
|
2024-04-21 11:43:53 +00:00
|
|
|
char tname[64];
|
|
|
|
|
|
|
|
libspl_getthreadname(tname, sizeof (tname));
|
|
|
|
|
|
|
|
fprintf(stderr, "ASSERT at %s:%d:%s()\n", file, line, func);
|
2020-07-04 22:24:13 +00:00
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
vfprintf(stderr, format, args);
|
|
|
|
va_end(args);
|
Reduce false positives from Static Analyzers
Both Clang's Static Analyzer and Synopsys' Coverity would ignore
assertions. Following Clang's advice, we annotate our assertions:
https://clang-analyzer.llvm.org/annotations.html#custom_assertions
This makes both Clang's Static Analyzer and Coverity properly identify
assertions. This change reduced Clang's reported defects from 246 to
180. It also reduced the false positives reported by Coverityi by 10,
while enabling Coverity to find 9 more defects that previously were
false negatives.
A couple examples of this would be CID-1524417 and CID-1524423. After
submitting a build to coverity with the modified assertions, CID-1524417
disappeared while the report for CID-1524423 no longer claimed that the
assertion tripped.
Coincidentally, it turns out that it is possible to more accurately
annotate our headers than the Coverity modelling file permits in the
case of format strings. Since we can do that and this patch annotates
headers whenever `__coverity_panic__()` would have been used in the
model file, we drop all models that use `__coverity_panic__()` from the
model file.
Upon seeing the success in eliminating false positives involving
assertions, it occurred to me that we could also modify our headers to
eliminate coverity's false positives involving byte swaps. We now have
coverity specific byteswap macros, that do nothing, to disable
Coverity's false positives when we do byte swaps. This allowed us to
also drop the byteswap definitions from the model file.
Lastly, a model file update has been done beyond the mentioned
deletions:
* The definitions of `umem_alloc_aligned()`, `umem_alloc()` andi
`umem_zalloc()` were originally implemented in a way that was
intended to inform coverity that when KM_SLEEP has been passed these
functions, they do not return NULL. A small error in how this was
done was found, so we correct it.
* Definitions for umem_cache_alloc() and umem_cache_free() have been
added.
In practice, no false positives were avoided by making these changes,
but in the interest of correctness from future coverity builds, we make
them anyway.
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Ryan Moeller <ryan@iXsystems.com>
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Closes #13902
2022-09-30 22:30:12 +00:00
|
|
|
|
2024-04-21 11:43:53 +00:00
|
|
|
fprintf(stderr, "\n"
|
|
|
|
" PID: %-8u COMM: %s\n"
|
2024-05-09 14:42:51 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
" TID: %-8" PRIu64 " NAME: %s\n",
|
|
|
|
#else
|
2024-04-21 11:43:53 +00:00
|
|
|
" TID: %-8u NAME: %s\n",
|
2024-05-09 14:42:51 +00:00
|
|
|
#endif
|
2024-04-21 11:43:53 +00:00
|
|
|
getpid(), libspl_getprogname(),
|
|
|
|
libspl_gettid(), tname);
|
|
|
|
|
2024-04-27 11:35:05 +00:00
|
|
|
libspl_dump_backtrace();
|
|
|
|
|
Reduce false positives from Static Analyzers
Both Clang's Static Analyzer and Synopsys' Coverity would ignore
assertions. Following Clang's advice, we annotate our assertions:
https://clang-analyzer.llvm.org/annotations.html#custom_assertions
This makes both Clang's Static Analyzer and Coverity properly identify
assertions. This change reduced Clang's reported defects from 246 to
180. It also reduced the false positives reported by Coverityi by 10,
while enabling Coverity to find 9 more defects that previously were
false negatives.
A couple examples of this would be CID-1524417 and CID-1524423. After
submitting a build to coverity with the modified assertions, CID-1524417
disappeared while the report for CID-1524423 no longer claimed that the
assertion tripped.
Coincidentally, it turns out that it is possible to more accurately
annotate our headers than the Coverity modelling file permits in the
case of format strings. Since we can do that and this patch annotates
headers whenever `__coverity_panic__()` would have been used in the
model file, we drop all models that use `__coverity_panic__()` from the
model file.
Upon seeing the success in eliminating false positives involving
assertions, it occurred to me that we could also modify our headers to
eliminate coverity's false positives involving byte swaps. We now have
coverity specific byteswap macros, that do nothing, to disable
Coverity's false positives when we do byte swaps. This allowed us to
also drop the byteswap definitions from the model file.
Lastly, a model file update has been done beyond the mentioned
deletions:
* The definitions of `umem_alloc_aligned()`, `umem_alloc()` andi
`umem_zalloc()` were originally implemented in a way that was
intended to inform coverity that when KM_SLEEP has been passed these
functions, they do not return NULL. A small error in how this was
done was found, so we correct it.
* Definitions for umem_cache_alloc() and umem_cache_free() have been
added.
In practice, no false positives were avoided by making these changes,
but in the interest of correctness from future coverity builds, we make
them anyway.
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Ryan Moeller <ryan@iXsystems.com>
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Closes #13902
2022-09-30 22:30:12 +00:00
|
|
|
#if !__has_feature(attribute_analyzer_noreturn) && !defined(__COVERITY__)
|
2021-06-03 17:04:13 +00:00
|
|
|
if (libspl_assert_ok) {
|
2024-04-28 02:49:58 +00:00
|
|
|
pthread_mutex_unlock(&assert_lock);
|
2020-07-04 22:24:13 +00:00
|
|
|
return;
|
|
|
|
}
|
Reduce false positives from Static Analyzers
Both Clang's Static Analyzer and Synopsys' Coverity would ignore
assertions. Following Clang's advice, we annotate our assertions:
https://clang-analyzer.llvm.org/annotations.html#custom_assertions
This makes both Clang's Static Analyzer and Coverity properly identify
assertions. This change reduced Clang's reported defects from 246 to
180. It also reduced the false positives reported by Coverityi by 10,
while enabling Coverity to find 9 more defects that previously were
false negatives.
A couple examples of this would be CID-1524417 and CID-1524423. After
submitting a build to coverity with the modified assertions, CID-1524417
disappeared while the report for CID-1524423 no longer claimed that the
assertion tripped.
Coincidentally, it turns out that it is possible to more accurately
annotate our headers than the Coverity modelling file permits in the
case of format strings. Since we can do that and this patch annotates
headers whenever `__coverity_panic__()` would have been used in the
model file, we drop all models that use `__coverity_panic__()` from the
model file.
Upon seeing the success in eliminating false positives involving
assertions, it occurred to me that we could also modify our headers to
eliminate coverity's false positives involving byte swaps. We now have
coverity specific byteswap macros, that do nothing, to disable
Coverity's false positives when we do byte swaps. This allowed us to
also drop the byteswap definitions from the model file.
Lastly, a model file update has been done beyond the mentioned
deletions:
* The definitions of `umem_alloc_aligned()`, `umem_alloc()` andi
`umem_zalloc()` were originally implemented in a way that was
intended to inform coverity that when KM_SLEEP has been passed these
functions, they do not return NULL. A small error in how this was
done was found, so we correct it.
* Definitions for umem_cache_alloc() and umem_cache_free() have been
added.
In practice, no false positives were avoided by making these changes,
but in the interest of correctness from future coverity builds, we make
them anyway.
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Ryan Moeller <ryan@iXsystems.com>
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Closes #13902
2022-09-30 22:30:12 +00:00
|
|
|
#endif
|
2020-07-04 22:24:13 +00:00
|
|
|
abort();
|
|
|
|
}
|