mirror of
https://github.com/git/git.git
synced 2024-11-05 08:47:56 +01:00
0e5bba53af
It's a common pattern in git commands to allocate some memory that should last for the lifetime of the program and then not bother to free it, relying on the OS to throw it away. This keeps the code simple, and it's fast (we don't waste time traversing structures or calling free at the end of the program). But it also triggers warnings from memory-leak checkers like valgrind or LSAN. They know that the memory was still allocated at program exit, but they don't know _when_ the leaked memory stopped being useful. If it was early in the program, then it's probably a real and important leak. But if it was used right up until program exit, it's not an interesting leak and we'd like to suppress it so that we can see the real leaks. This patch introduces an UNLEAK() macro that lets us do so. To understand its design, let's first look at some of the alternatives. Unfortunately the suppression systems offered by leak-checking tools don't quite do what we want. A leak-checker basically knows two things: 1. Which blocks were allocated via malloc, and the callstack during the allocation. 2. Which blocks were left un-freed at the end of the program (and which are unreachable, but more on that later). Their suppressions work by mentioning the function or callstack of a particular allocation, and marking it as OK to leak. So imagine you have code like this: int cmd_foo(...) { /* this allocates some memory */ char *p = some_function(); printf("%s", p); return 0; } You can say "ignore allocations from some_function(), they're not leaks". But that's not right. That function may be called elsewhere, too, and we would potentially want to know about those leaks. So you can say "ignore the callstack when main calls some_function". That works, but your annotations are brittle. In this case it's only two functions, but you can imagine that the actual allocation is much deeper. If any of the intermediate code changes, you have to update the suppression. What we _really_ want to say is that "the value assigned to p at the end of the function is not a real leak". But leak-checkers can't understand that; they don't know about "p" in the first place. However, we can do something a little bit tricky if we make some assumptions about how leak-checkers work. They generally don't just report all un-freed blocks. That would report even globals which are still accessible when the leak-check is run. Instead they take some set of memory (like BSS) as a root and mark it as "reachable". Then they scan the reachable blocks for anything that looks like a pointer to a malloc'd block, and consider that block reachable. And then they scan those blocks, and so on, transitively marking anything reachable from a global as "not leaked" (or at least leaked in a different category). So we can mark the value of "p" as reachable by putting it into a variable with program lifetime. One way to do that is to just mark "p" as static. But that actually affects the run-time behavior if the function is called twice (you aren't likely to call main() twice, but some of our cmd_*() functions are called from other commands). Instead, we can trick the leak-checker by putting the value into _any_ reachable bytes. This patch keeps a global linked-list of bytes copied from "unleaked" variables. That list is reachable even at program exit, which confers recursive reachability on whatever values we unleak. In other words, you can do: int cmd_foo(...) { char *p = some_function(); printf("%s", p); UNLEAK(p); return 0; } to annotate "p" and suppress the leak report. But wait, couldn't we just say "free(p)"? In this toy example, yes. But UNLEAK()'s byte-copying strategy has several advantages over actually freeing the memory: 1. It's recursive across structures. In many cases our "p" is not just a pointer, but a complex struct whose fields may have been allocated by a sub-function. And in some cases (e.g., dir_struct) we don't even have a function which knows how to free all of the struct members. By marking the struct itself as reachable, that confers reachability on any pointers it contains (including those found in embedded structs, or reachable by walking heap blocks recursively. 2. It works on cases where we're not sure if the value is allocated or not. For example: char *p = argc > 1 ? argv[1] : some_function(); It's safe to use UNLEAK(p) here, because it's not freeing any memory. In the case that we're pointing to argv here, the reachability checker will just ignore our bytes. 3. Likewise, it works even if the variable has _already_ been freed. We're just copying the pointer bytes. If the block has been freed, the leak-checker will skip over those bytes as uninteresting. 4. Because it's not actually freeing memory, you can UNLEAK() before we are finished accessing the variable. This is helpful in cases like this: char *p = some_function(); return another_function(p); Writing this with free() requires: int ret; char *p = some_function(); ret = another_function(p); free(p); return ret; But with unleak we can just write: char *p = some_function(); UNLEAK(p); return another_function(p); This patch adds the UNLEAK() macro and enables it automatically when Git is compiled with SANITIZE=leak. In normal builds it's a noop, so we pay no runtime cost. It also adds some UNLEAK() annotations to show off how the feature works. On top of other recent leak fixes, these are enough to get t0000 and t0001 to pass when compiled with LSAN. Note the case in commit.c which actually converts a strbuf_release() into an UNLEAK. This code was already non-leaky, but the free didn't do anything useful, since we're exiting. Converting it to an annotation means that non-leak-checking builds pay no runtime cost. The cost is minimal enough that it's probably not worth going on a crusade to convert these kinds of frees to UNLEAKS. I did it here for consistency with the "sb" leak (though it would have been equally correct to go the other way, and turn them both into strbuf_release() calls). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
258 lines
5.3 KiB
C
258 lines
5.3 KiB
C
/*
|
|
* GIT - The information manager from hell
|
|
*
|
|
* Copyright (C) Linus Torvalds, 2005
|
|
*/
|
|
#include "git-compat-util.h"
|
|
#include "cache.h"
|
|
|
|
void vreportf(const char *prefix, const char *err, va_list params)
|
|
{
|
|
char msg[4096];
|
|
char *p;
|
|
|
|
vsnprintf(msg, sizeof(msg), err, params);
|
|
for (p = msg; *p; p++) {
|
|
if (iscntrl(*p) && *p != '\t' && *p != '\n')
|
|
*p = '?';
|
|
}
|
|
fprintf(stderr, "%s%s\n", prefix, msg);
|
|
}
|
|
|
|
static NORETURN void usage_builtin(const char *err, va_list params)
|
|
{
|
|
vreportf("usage: ", err, params);
|
|
exit(129);
|
|
}
|
|
|
|
static NORETURN void die_builtin(const char *err, va_list params)
|
|
{
|
|
vreportf("fatal: ", err, params);
|
|
exit(128);
|
|
}
|
|
|
|
static void error_builtin(const char *err, va_list params)
|
|
{
|
|
vreportf("error: ", err, params);
|
|
}
|
|
|
|
static void warn_builtin(const char *warn, va_list params)
|
|
{
|
|
vreportf("warning: ", warn, params);
|
|
}
|
|
|
|
static int die_is_recursing_builtin(void)
|
|
{
|
|
static int dying;
|
|
/*
|
|
* Just an arbitrary number X where "a < x < b" where "a" is
|
|
* "maximum number of pthreads we'll ever plausibly spawn" and
|
|
* "b" is "something less than Inf", since the point is to
|
|
* prevent infinite recursion.
|
|
*/
|
|
static const int recursion_limit = 1024;
|
|
|
|
dying++;
|
|
if (dying > recursion_limit) {
|
|
return 1;
|
|
} else if (dying == 2) {
|
|
warning("die() called many times. Recursion error or racy threaded death!");
|
|
return 0;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/* If we are in a dlopen()ed .so write to a global variable would segfault
|
|
* (ugh), so keep things static. */
|
|
static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
|
|
static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
|
|
static void (*error_routine)(const char *err, va_list params) = error_builtin;
|
|
static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
|
|
static int (*die_is_recursing)(void) = die_is_recursing_builtin;
|
|
|
|
void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
|
|
{
|
|
die_routine = routine;
|
|
}
|
|
|
|
void set_error_routine(void (*routine)(const char *err, va_list params))
|
|
{
|
|
error_routine = routine;
|
|
}
|
|
|
|
void (*get_error_routine(void))(const char *err, va_list params)
|
|
{
|
|
return error_routine;
|
|
}
|
|
|
|
void set_warn_routine(void (*routine)(const char *warn, va_list params))
|
|
{
|
|
warn_routine = routine;
|
|
}
|
|
|
|
void (*get_warn_routine(void))(const char *warn, va_list params)
|
|
{
|
|
return warn_routine;
|
|
}
|
|
|
|
void set_die_is_recursing_routine(int (*routine)(void))
|
|
{
|
|
die_is_recursing = routine;
|
|
}
|
|
|
|
void NORETURN usagef(const char *err, ...)
|
|
{
|
|
va_list params;
|
|
|
|
va_start(params, err);
|
|
usage_routine(err, params);
|
|
va_end(params);
|
|
}
|
|
|
|
void NORETURN usage(const char *err)
|
|
{
|
|
usagef("%s", err);
|
|
}
|
|
|
|
void NORETURN die(const char *err, ...)
|
|
{
|
|
va_list params;
|
|
|
|
if (die_is_recursing()) {
|
|
fputs("fatal: recursion detected in die handler\n", stderr);
|
|
exit(128);
|
|
}
|
|
|
|
va_start(params, err);
|
|
die_routine(err, params);
|
|
va_end(params);
|
|
}
|
|
|
|
static const char *fmt_with_err(char *buf, int n, const char *fmt)
|
|
{
|
|
char str_error[256], *err;
|
|
int i, j;
|
|
|
|
err = strerror(errno);
|
|
for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
|
|
if ((str_error[j++] = err[i++]) != '%')
|
|
continue;
|
|
if (j < sizeof(str_error) - 1) {
|
|
str_error[j++] = '%';
|
|
} else {
|
|
/* No room to double the '%', so we overwrite it with
|
|
* '\0' below */
|
|
j--;
|
|
break;
|
|
}
|
|
}
|
|
str_error[j] = 0;
|
|
snprintf(buf, n, "%s: %s", fmt, str_error);
|
|
return buf;
|
|
}
|
|
|
|
void NORETURN die_errno(const char *fmt, ...)
|
|
{
|
|
char buf[1024];
|
|
va_list params;
|
|
|
|
if (die_is_recursing()) {
|
|
fputs("fatal: recursion detected in die_errno handler\n",
|
|
stderr);
|
|
exit(128);
|
|
}
|
|
|
|
va_start(params, fmt);
|
|
die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
|
|
va_end(params);
|
|
}
|
|
|
|
#undef error_errno
|
|
int error_errno(const char *fmt, ...)
|
|
{
|
|
char buf[1024];
|
|
va_list params;
|
|
|
|
va_start(params, fmt);
|
|
error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
|
|
va_end(params);
|
|
return -1;
|
|
}
|
|
|
|
#undef error
|
|
int error(const char *err, ...)
|
|
{
|
|
va_list params;
|
|
|
|
va_start(params, err);
|
|
error_routine(err, params);
|
|
va_end(params);
|
|
return -1;
|
|
}
|
|
|
|
void warning_errno(const char *warn, ...)
|
|
{
|
|
char buf[1024];
|
|
va_list params;
|
|
|
|
va_start(params, warn);
|
|
warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
|
|
va_end(params);
|
|
}
|
|
|
|
void warning(const char *warn, ...)
|
|
{
|
|
va_list params;
|
|
|
|
va_start(params, warn);
|
|
warn_routine(warn, params);
|
|
va_end(params);
|
|
}
|
|
|
|
static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
|
|
{
|
|
char prefix[256];
|
|
|
|
/* truncation via snprintf is OK here */
|
|
if (file)
|
|
snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line);
|
|
else
|
|
snprintf(prefix, sizeof(prefix), "BUG: ");
|
|
|
|
vreportf(prefix, fmt, params);
|
|
abort();
|
|
}
|
|
|
|
#ifdef HAVE_VARIADIC_MACROS
|
|
NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...)
|
|
{
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
BUG_vfl(file, line, fmt, ap);
|
|
va_end(ap);
|
|
}
|
|
#else
|
|
NORETURN void BUG(const char *fmt, ...)
|
|
{
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
BUG_vfl(NULL, 0, fmt, ap);
|
|
va_end(ap);
|
|
}
|
|
#endif
|
|
|
|
#ifdef SUPPRESS_ANNOTATED_LEAKS
|
|
void unleak_memory(const void *ptr, size_t len)
|
|
{
|
|
static struct suppressed_leak_root {
|
|
struct suppressed_leak_root *next;
|
|
char data[FLEX_ARRAY];
|
|
} *suppressed_leaks;
|
|
struct suppressed_leak_root *root;
|
|
|
|
FLEX_ALLOC_MEM(root, data, ptr, len);
|
|
root->next = suppressed_leaks;
|
|
suppressed_leaks = root;
|
|
}
|
|
#endif
|