Unverified Commit d9d86e41 by Edward Thomson Committed by GitHub

Merge pull request #5580 from libgit2/ethomson/win32_leakcheck

msvc crtdbg -> win32 leakcheck
parents 045748bb 6d44c87a
......@@ -124,7 +124,7 @@ jobs:
env:
ARCH: amd64
CMAKE_GENERATOR: Visual Studio 16 2019
CMAKE_OPTIONS: -A x64 -DMSVC_CRTDBG=ON -DDEPRECATE_HARD=ON
CMAKE_OPTIONS: -A x64 -DWIN32_LEAKCHECK=ON -DDEPRECATE_HARD=ON
SKIP_SSH_TESTS: true
SKIP_NEGOTIATE_TESTS: true
- # Windows x86 Visual Studio
......@@ -132,7 +132,7 @@ jobs:
env:
ARCH: x86
CMAKE_GENERATOR: Visual Studio 16 2019
CMAKE_OPTIONS: -A Win32 -DMSVC_CRTDBG=ON -DDEPRECATE_HARD=ON -DUSE_SHA1=HTTPS -DUSE_BUNDLED_ZLIB=ON
CMAKE_OPTIONS: -A Win32 -DWIN32_LEAKCHECK=ON -DDEPRECATE_HARD=ON -DUSE_SHA1=HTTPS -DUSE_BUNDLED_ZLIB=ON
SKIP_SSH_TESTS: true
SKIP_NEGOTIATE_TESTS: true
- # Windows amd64 mingw
......
......@@ -89,8 +89,8 @@ IF(WIN32)
ENDIF()
IF(MSVC)
# Enable MSVC CRTDBG memory leak reporting when in debug mode.
OPTION(MSVC_CRTDBG "Enable CRTDBG memory leak reporting" OFF)
# Enable leak checking using the debugging C runtime.
OPTION(WIN32_LEAKCHECK "Enable leak reporting via crtdbg" OFF)
ENDIF()
IF (DEPRECATE_HARD)
......@@ -129,8 +129,8 @@ IF (MSVC)
SET(CRT_FLAG_RELEASE "/MD")
ENDIF()
IF (MSVC_CRTDBG)
SET(GIT_MSVC_CRTDBG 1)
IF (WIN32_LEAKCHECK)
SET(GIT_WIN32_LEAKCHECK 1)
SET(CRT_FLAG_DEBUG "${CRT_FLAG_DEBUG}")
SET(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES} Dbghelp.lib")
ENDIF()
......
......@@ -96,7 +96,7 @@ jobs:
parameters:
environmentVariables:
CMAKE_GENERATOR: Visual Studio 15 2017
CMAKE_OPTIONS: -A x64 -DMSVC_CRTDBG=ON -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON
CMAKE_OPTIONS: -A x64 -DWIN32_LEAKCHECK=ON -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON
SKIP_SSH_TESTS: true
SKIP_NEGOTIATE_TESTS: true
......@@ -109,7 +109,7 @@ jobs:
parameters:
environmentVariables:
CMAKE_GENERATOR: Visual Studio 15 2017
CMAKE_OPTIONS: -A Win32 -DMSVC_CRTDBG=ON -DDEPRECATE_HARD=ON -DUSE_SHA1=HTTPS -DUSE_BUNDLED_ZLIB=ON
CMAKE_OPTIONS: -A Win32 -DWIN32_LEAKCHECK=ON -DDEPRECATE_HARD=ON -DUSE_SHA1=HTTPS -DUSE_BUNDLED_ZLIB=ON
SKIP_SSH_TESTS: true
SKIP_NEGOTIATE_TESTS: true
......
......@@ -92,7 +92,7 @@ jobs:
parameters:
environmentVariables:
CMAKE_GENERATOR: Visual Studio 15 2017
CMAKE_OPTIONS: -A x64 -DMSVC_CRTDBG=ON -DDEPRECATE_HARD=ON
CMAKE_OPTIONS: -A x64 -DWIN32_LEAKCHECK=ON -DDEPRECATE_HARD=ON
RUN_INVASIVE_TESTS: true
SKIP_SSH_TESTS: true
......@@ -105,7 +105,7 @@ jobs:
parameters:
environmentVariables:
CMAKE_GENERATOR: Visual Studio 15 2017
CMAKE_OPTIONS: -A Win32 -DMSVC_CRTDBG=ON -DDEPRECATE_HARD=ON -DUSE_SHA1=HTTPS
CMAKE_OPTIONS: -A Win32 -DWIN32_LEAKCHECK=ON -DDEPRECATE_HARD=ON -DUSE_SHA1=HTTPS
RUN_INVASIVE_TESTS: true
SKIP_SSH_TESTS: true
......
......@@ -13,6 +13,10 @@ vNext
replaces `git_reference_is_valid_name`. Tthe former functions are
deprecated.
### Breaking CMake configuration changes
The `MVSC_CRTDBG` configuration option is now `WIN32_LEAKCHECK`.
v1.1
----
......
......@@ -9,42 +9,21 @@
#include "runtime.h"
#include "allocators/stdalloc.h"
#include "allocators/win32_crtdbg.h"
#if defined(GIT_MSVC_CRTDBG)
# include "win32/w32_stack.h"
# include "win32/w32_crtdbg_stacktrace.h"
#endif
#include "allocators/win32_leakcheck.h"
git_allocator git__allocator;
static int setup_default_allocator(void)
{
#if defined(GIT_MSVC_CRTDBG)
return git_win32_crtdbg_init_allocator(&git__allocator);
#if defined(GIT_WIN32_LEAKCHECK)
return git_win32_leakcheck_init_allocator(&git__allocator);
#else
return git_stdalloc_init_allocator(&git__allocator);
#endif
}
#if defined(GIT_MSVC_CRTDBG)
static void allocator_global_shutdown(void)
{
git_win32__crtdbg_stacktrace_cleanup();
git_win32__stack_cleanup();
}
#endif
int git_allocator_global_init(void)
{
#if defined(GIT_MSVC_CRTDBG)
git_win32__crtdbg_stacktrace_init();
git_win32__stack_init();
if (git_runtime_shutdown_register(allocator_global_shutdown) < 0)
return -1;
#endif
/*
* We don't want to overwrite any allocator which has been set before
* the init function is called.
......
......@@ -5,35 +5,34 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "win32_crtdbg.h"
#include "win32_leakcheck.h"
#if defined(GIT_MSVC_CRTDBG)
#if defined(GIT_WIN32_LEAKCHECK)
#include "win32/w32_stack.h"
#include "win32/w32_crtdbg_stacktrace.h"
#include "win32/w32_leakcheck.h"
static void *crtdbg__malloc(size_t len, const char *file, int line)
static void *leakcheck_malloc(size_t len, const char *file, int line)
{
void *ptr = _malloc_dbg(len, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
void *ptr = _malloc_dbg(len, _NORMAL_BLOCK, git_win32_leakcheck_stacktrace(1,file), line);
if (!ptr) git_error_set_oom();
return ptr;
}
static void *crtdbg__calloc(size_t nelem, size_t elsize, const char *file, int line)
static void *leakcheck_calloc(size_t nelem, size_t elsize, const char *file, int line)
{
void *ptr = _calloc_dbg(nelem, elsize, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
void *ptr = _calloc_dbg(nelem, elsize, _NORMAL_BLOCK, git_win32_leakcheck_stacktrace(1,file), line);
if (!ptr) git_error_set_oom();
return ptr;
}
static char *crtdbg__strdup(const char *str, const char *file, int line)
static char *leakcheck_strdup(const char *str, const char *file, int line)
{
char *ptr = _strdup_dbg(str, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
char *ptr = _strdup_dbg(str, _NORMAL_BLOCK, git_win32_leakcheck_stacktrace(1,file), line);
if (!ptr) git_error_set_oom();
return ptr;
}
static char *crtdbg__strndup(const char *str, size_t n, const char *file, int line)
static char *leakcheck_strndup(const char *str, size_t n, const char *file, int line)
{
size_t length = 0, alloclength;
char *ptr;
......@@ -41,7 +40,7 @@ static char *crtdbg__strndup(const char *str, size_t n, const char *file, int li
length = p_strnlen(str, n);
if (GIT_ADD_SIZET_OVERFLOW(&alloclength, length, 1) ||
!(ptr = crtdbg__malloc(alloclength, file, line)))
!(ptr = leakcheck_malloc(alloclength, file, line)))
return NULL;
if (length)
......@@ -52,13 +51,13 @@ static char *crtdbg__strndup(const char *str, size_t n, const char *file, int li
return ptr;
}
static char *crtdbg__substrdup(const char *start, size_t n, const char *file, int line)
static char *leakcheck_substrdup(const char *start, size_t n, const char *file, int line)
{
char *ptr;
size_t alloclen;
if (GIT_ADD_SIZET_OVERFLOW(&alloclen, n, 1) ||
!(ptr = crtdbg__malloc(alloclen, file, line)))
!(ptr = leakcheck_malloc(alloclen, file, line)))
return NULL;
memcpy(ptr, start, n);
......@@ -66,53 +65,53 @@ static char *crtdbg__substrdup(const char *start, size_t n, const char *file, in
return ptr;
}
static void *crtdbg__realloc(void *ptr, size_t size, const char *file, int line)
static void *leakcheck_realloc(void *ptr, size_t size, const char *file, int line)
{
void *new_ptr = _realloc_dbg(ptr, size, _NORMAL_BLOCK, git_win32__crtdbg_stacktrace(1,file), line);
void *new_ptr = _realloc_dbg(ptr, size, _NORMAL_BLOCK, git_win32_leakcheck_stacktrace(1,file), line);
if (!new_ptr) git_error_set_oom();
return new_ptr;
}
static void *crtdbg__reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
static void *leakcheck_reallocarray(void *ptr, size_t nelem, size_t elsize, const char *file, int line)
{
size_t newsize;
if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize, nelem, elsize))
return NULL;
return crtdbg__realloc(ptr, newsize, file, line);
return leakcheck_realloc(ptr, newsize, file, line);
}
static void *crtdbg__mallocarray(size_t nelem, size_t elsize, const char *file, int line)
static void *leakcheck_mallocarray(size_t nelem, size_t elsize, const char *file, int line)
{
return crtdbg__reallocarray(NULL, nelem, elsize, file, line);
return leakcheck_reallocarray(NULL, nelem, elsize, file, line);
}
static void crtdbg__free(void *ptr)
static void leakcheck_free(void *ptr)
{
free(ptr);
}
int git_win32_crtdbg_init_allocator(git_allocator *allocator)
int git_win32_leakcheck_init_allocator(git_allocator *allocator)
{
allocator->gmalloc = crtdbg__malloc;
allocator->gcalloc = crtdbg__calloc;
allocator->gstrdup = crtdbg__strdup;
allocator->gstrndup = crtdbg__strndup;
allocator->gsubstrdup = crtdbg__substrdup;
allocator->grealloc = crtdbg__realloc;
allocator->greallocarray = crtdbg__reallocarray;
allocator->gmallocarray = crtdbg__mallocarray;
allocator->gfree = crtdbg__free;
allocator->gmalloc = leakcheck_malloc;
allocator->gcalloc = leakcheck_calloc;
allocator->gstrdup = leakcheck_strdup;
allocator->gstrndup = leakcheck_strndup;
allocator->gsubstrdup = leakcheck_substrdup;
allocator->grealloc = leakcheck_realloc;
allocator->greallocarray = leakcheck_reallocarray;
allocator->gmallocarray = leakcheck_mallocarray;
allocator->gfree = leakcheck_free;
return 0;
}
#else
int git_win32_crtdbg_init_allocator(git_allocator *allocator)
int git_win32_leakcheck_init_allocator(git_allocator *allocator)
{
GIT_UNUSED(allocator);
git_error_set(GIT_EINVALID, "crtdbg memory allocator not available");
git_error_set(GIT_EINVALID, "leakcheck memory allocator not available");
return -1;
}
......
......@@ -5,13 +5,13 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_allocators_crtdbg_h
#define INCLUDE_allocators_crtdbg_h
#ifndef INCLUDE_allocators_win32_leakcheck_h
#define INCLUDE_allocators_win32_leakcheck_h
#include "common.h"
#include "alloc.h"
int git_win32_crtdbg_init_allocator(git_allocator *allocator);
int git_win32_leakcheck_init_allocator(git_allocator *allocator);
#endif
......@@ -4,7 +4,7 @@
#cmakedefine GIT_DEBUG_POOL 1
#cmakedefine GIT_TRACE 1
#cmakedefine GIT_THREADS 1
#cmakedefine GIT_MSVC_CRTDBG 1
#cmakedefine GIT_WIN32_LEAKCHECK 1
#cmakedefine GIT_ARCH_64 1
#cmakedefine GIT_ARCH_32 1
......
......@@ -31,7 +31,10 @@
#include "transports/smart.h"
#include "transports/http.h"
#include "transports/ssh.h"
#include "win32/w32_stack.h"
#ifdef GIT_WIN32
# include "win32/w32_leakcheck.h"
#endif
#ifdef GIT_OPENSSL
# include <openssl/err.h>
......@@ -65,6 +68,9 @@ static int git_libgit2_settings_global_init(void)
int git_libgit2_init(void)
{
static git_runtime_init_fn init_fns[] = {
#ifdef GIT_WIN32
git_win32_leakcheck_global_init,
#endif
git_allocator_global_init,
git_threadstate_global_init,
git_threads_global_init,
......
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_win32_w32_crtdbg_stacktrace_h__
#define INCLUDE_win32_w32_crtdbg_stacktrace_h__
#include "common.h"
#if defined(GIT_MSVC_CRTDBG)
#include <stdlib.h>
#include <crtdbg.h>
#include "git2/errors.h"
#include "strnlen.h"
/* MSVC CRTDBG memory leak reporting.
*
* We DO NOT use the "_CRTDBG_MAP_ALLOC" macro described in the MSVC
* documentation because all allocs/frees in libgit2 already go through
* the "git__" routines defined in this file. Simply using the normal
* reporting mechanism causes all leaks to be attributed to a routine
* here in util.h (ie, the actual call to calloc()) rather than the
* caller of git__calloc().
*
* Therefore, we declare a set of "git__crtdbg__" routines to replace
* the corresponding "git__" routines and re-define the "git__" symbols
* as macros. This allows us to get and report the file:line info of
* the real caller.
*
* We DO NOT replace the "git__free" routine because it needs to remain
* a function pointer because it is used as a function argument when
* setting up various structure "destructors".
*
* We also DO NOT use the "_CRTDBG_MAP_ALLOC" macro because it causes
* "free" to be remapped to "_free_dbg" and this causes problems for
* structures which define a field named "free".
*
* Finally, CRTDBG must be explicitly enabled and configured at program
* startup. See tests/main.c for an example.
*/
/**
* Initialize our memory leak tracking and de-dup data structures.
* This should ONLY be called by git_libgit2_init().
*/
void git_win32__crtdbg_stacktrace_init(void);
/**
* Shutdown our memory leak tracking and dump summary data.
* This should ONLY be called by git_libgit2_shutdown().
*
* We explicitly call _CrtDumpMemoryLeaks() during here so
* that we can compute summary data for the leaks. We print
* the stacktrace of each unique leak.
*
* This cleanup does not happen if the app calls exit()
* without calling the libgit2 shutdown code.
*
* This info we print here is independent of any automatic
* reporting during exit() caused by _CRTDBG_LEAK_CHECK_DF.
* Set it in your app if you also want traditional reporting.
*/
void git_win32__crtdbg_stacktrace_cleanup(void);
/**
* Checkpoint options.
*/
typedef enum git_win32__crtdbg_stacktrace_options {
/**
* Set checkpoint marker.
*/
GIT_WIN32__CRTDBG_STACKTRACE__SET_MARK = (1 << 0),
/**
* Dump leaks since last checkpoint marker.
* May not be combined with __LEAKS_TOTAL.
*
* Note that this may generate false positives for global TLS
* error state and other global caches that aren't cleaned up
* until the thread/process terminates. So when using this
* around a region of interest, also check the final (at exit)
* dump before digging into leaks reported here.
*/
GIT_WIN32__CRTDBG_STACKTRACE__LEAKS_SINCE_MARK = (1 << 1),
/**
* Dump leaks since init. May not be combined
* with __LEAKS_SINCE_MARK.
*/
GIT_WIN32__CRTDBG_STACKTRACE__LEAKS_TOTAL = (1 << 2),
/**
* Suppress printing during dumps.
* Just return leak count.
*/
GIT_WIN32__CRTDBG_STACKTRACE__QUIET = (1 << 3),
} git_win32__crtdbg_stacktrace_options;
/**
* Checkpoint memory state and/or dump unique stack traces of
* current memory leaks.
*
* @return number of unique leaks (relative to requested starting
* point) or error.
*/
GIT_EXTERN(int) git_win32__crtdbg_stacktrace__dump(
git_win32__crtdbg_stacktrace_options opt,
const char *label);
/**
* Construct stacktrace and append it to the global buffer.
* Return pointer to start of this string. On any error or
* lack of buffer space, just return the given file buffer
* so it will behave as usual.
*
* This should ONLY be called by our internal memory allocations
* routines.
*/
const char *git_win32__crtdbg_stacktrace(int skip, const char *file);
#endif
#endif
......@@ -5,12 +5,23 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_win32_w32_stack_h__
#define INCLUDE_win32_w32_stack_h__
#ifndef INCLUDE_win32_leakcheck_h__
#define INCLUDE_win32_leakcheck_h__
#include "common.h"
#if defined(GIT_MSVC_CRTDBG)
/* Initialize the win32 leak checking system. */
int git_win32_leakcheck_global_init(void);
#if defined(GIT_WIN32_LEAKCHECK)
#include <stdlib.h>
#include <crtdbg.h>
#include "git2/errors.h"
#include "strnlen.h"
/* Stack frames (for stack tracing, below) */
/**
* This type defines a callback to be used to augment a C stacktrace
......@@ -24,7 +35,7 @@
* (de-duped at the C# layer) stacktrace. "aux_id" 0 is reserved
* to mean no aux stacktrace data.
*/
typedef void (*git_win32__stack__aux_cb_alloc)(unsigned int *aux_id);
typedef void (*git_win32_leakcheck_stack_aux_cb_alloc)(unsigned int *aux_id);
/**
* This type defines a callback to be used to augment the output of
......@@ -38,7 +49,7 @@ typedef void (*git_win32__stack__aux_cb_alloc)(unsigned int *aux_id);
* @param aux_msg A buffer where a formatted message should be written.
* @param aux_msg_len The size of the buffer.
*/
typedef void (*git_win32__stack__aux_cb_lookup)(unsigned int aux_id, char *aux_msg, size_t aux_msg_len);
typedef void (*git_win32_leakcheck_stack_aux_cb_lookup)(unsigned int aux_id, char *aux_msg, size_t aux_msg_len);
/**
* Register an "aux" data provider to augment our C stacktrace data.
......@@ -50,15 +61,15 @@ typedef void (*git_win32__stack__aux_cb_lookup)(unsigned int aux_id, char *aux_m
* If you choose to use this feature, it should be registered during
* initialization and not changed for the duration of the process.
*/
GIT_EXTERN(int) git_win32__stack__set_aux_cb(
git_win32__stack__aux_cb_alloc cb_alloc,
git_win32__stack__aux_cb_lookup cb_lookup);
int git_win32_leakcheck_stack_set_aux_cb(
git_win32_leakcheck_stack_aux_cb_alloc cb_alloc,
git_win32_leakcheck_stack_aux_cb_lookup cb_lookup);
/**
* Maximum number of stackframes to record for a
* single stacktrace.
*/
#define GIT_WIN32__STACK__MAX_FRAMES 30
#define GIT_WIN32_LEAKCHECK_STACK_MAX_FRAMES 30
/**
* Wrapper containing the raw unprocessed stackframe
......@@ -72,24 +83,8 @@ GIT_EXTERN(int) git_win32__stack__set_aux_cb(
typedef struct {
unsigned int aux_id;
unsigned int nr_frames;
void *frames[GIT_WIN32__STACK__MAX_FRAMES];
} git_win32__stack__raw_data;
/**
* Load symbol table data. This should be done in the primary
* thread at startup (under a lock if there are other threads
* active).
*/
void git_win32__stack_init(void);
/**
* Cleanup symbol table data. This should be done in the
* primary thead at shutdown (under a lock if there are other
* threads active).
*/
void git_win32__stack_cleanup(void);
void *frames[GIT_WIN32_LEAKCHECK_STACK_MAX_FRAMES];
} git_win32_leakcheck_stack_raw_data;
/**
* Capture raw stack trace data for the current process/thread.
......@@ -98,16 +93,16 @@ void git_win32__stack_cleanup(void);
* begin with the caller of this routine. Pass 1 to begin
* with its caller. And so on.
*/
int git_win32__stack_capture(git_win32__stack__raw_data *pdata, int skip);
int git_win32_leakcheck_stack_capture(git_win32_leakcheck_stack_raw_data *pdata, int skip);
/**
* Compare 2 raw stacktraces with the usual -1,0,+1 result.
* This includes any "aux_id" values in the comparison, so that
* our de-dup is also "aux" context relative.
*/
int git_win32__stack_compare(
git_win32__stack__raw_data *d1,
git_win32__stack__raw_data *d2);
int git_win32_leakcheck_stack_compare(
git_win32_leakcheck_stack_raw_data *d1,
git_win32_leakcheck_stack_raw_data *d2);
/**
* Format raw stacktrace data into buffer WITHOUT using any mallocs.
......@@ -115,9 +110,9 @@ int git_win32__stack_compare(
* @param prefix String written before each frame; defaults to "\t".
* @param suffix String written after each frame; defaults to "\n".
*/
int git_win32__stack_format(
int git_win32_leakcheck_stack_format(
char *pbuf, size_t buf_len,
const git_win32__stack__raw_data *pdata,
const git_win32_leakcheck_stack_raw_data *pdata,
const char *prefix, const char *suffix);
/**
......@@ -131,10 +126,95 @@ int git_win32__stack_format(
* @param prefix String written before each frame; defaults to "\t".
* @param suffix String written after each frame; defaults to "\n".
*/
int git_win32__stack(
int git_win32_leakcheck_stack(
char * pbuf, size_t buf_len,
int skip,
const char *prefix, const char *suffix);
#endif /* GIT_MSVC_CRTDBG */
/* Stack tracing */
/* MSVC CRTDBG memory leak reporting.
*
* We DO NOT use the "_CRTDBG_MAP_ALLOC" macro described in the MSVC
* documentation because all allocs/frees in libgit2 already go through
* the "git__" routines defined in this file. Simply using the normal
* reporting mechanism causes all leaks to be attributed to a routine
* here in util.h (ie, the actual call to calloc()) rather than the
* caller of git__calloc().
*
* Therefore, we declare a set of "git__crtdbg__" routines to replace
* the corresponding "git__" routines and re-define the "git__" symbols
* as macros. This allows us to get and report the file:line info of
* the real caller.
*
* We DO NOT replace the "git__free" routine because it needs to remain
* a function pointer because it is used as a function argument when
* setting up various structure "destructors".
*
* We also DO NOT use the "_CRTDBG_MAP_ALLOC" macro because it causes
* "free" to be remapped to "_free_dbg" and this causes problems for
* structures which define a field named "free".
*
* Finally, CRTDBG must be explicitly enabled and configured at program
* startup. See tests/main.c for an example.
*/
/**
* Checkpoint options.
*/
typedef enum git_win32_leakcheck_stacktrace_options {
/**
* Set checkpoint marker.
*/
GIT_WIN32_LEAKCHECK_STACKTRACE_SET_MARK = (1 << 0),
/**
* Dump leaks since last checkpoint marker.
* May not be combined with _LEAKS_TOTAL.
*
* Note that this may generate false positives for global TLS
* error state and other global caches that aren't cleaned up
* until the thread/process terminates. So when using this
* around a region of interest, also check the final (at exit)
* dump before digging into leaks reported here.
*/
GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_SINCE_MARK = (1 << 1),
/**
* Dump leaks since init. May not be combined
* with _LEAKS_SINCE_MARK.
*/
GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_TOTAL = (1 << 2),
/**
* Suppress printing during dumps.
* Just return leak count.
*/
GIT_WIN32_LEAKCHECK_STACKTRACE_QUIET = (1 << 3),
} git_win32_leakcheck_stacktrace_options;
/**
* Checkpoint memory state and/or dump unique stack traces of
* current memory leaks.
*
* @return number of unique leaks (relative to requested starting
* point) or error.
*/
int git_win32_leakcheck_stacktrace_dump(
git_win32_leakcheck_stacktrace_options opt,
const char *label);
/**
* Construct stacktrace and append it to the global buffer.
* Return pointer to start of this string. On any error or
* lack of buffer space, just return the given file buffer
* so it will behave as usual.
*
* This should ONLY be called by our internal memory allocations
* routines.
*/
const char *git_win32_leakcheck_stacktrace(int skip, const char *file);
#endif
#endif
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "w32_stack.h"
#if defined(GIT_MSVC_CRTDBG)
#include "Windows.h"
#include "Dbghelp.h"
#include "win32/posix.h"
#include "hash.h"
static bool g_win32_stack_initialized = false;
static HANDLE g_win32_stack_process = INVALID_HANDLE_VALUE;
static git_win32__stack__aux_cb_alloc g_aux_cb_alloc = NULL;
static git_win32__stack__aux_cb_lookup g_aux_cb_lookup = NULL;
int git_win32__stack__set_aux_cb(
git_win32__stack__aux_cb_alloc cb_alloc,
git_win32__stack__aux_cb_lookup cb_lookup)
{
g_aux_cb_alloc = cb_alloc;
g_aux_cb_lookup = cb_lookup;
return 0;
}
void git_win32__stack_init(void)
{
if (!g_win32_stack_initialized) {
g_win32_stack_process = GetCurrentProcess();
SymSetOptions(SYMOPT_LOAD_LINES);
SymInitialize(g_win32_stack_process, NULL, TRUE);
g_win32_stack_initialized = true;
}
}
void git_win32__stack_cleanup(void)
{
if (g_win32_stack_initialized) {
SymCleanup(g_win32_stack_process);
g_win32_stack_process = INVALID_HANDLE_VALUE;
g_win32_stack_initialized = false;
}
}
int git_win32__stack_capture(git_win32__stack__raw_data *pdata, int skip)
{
if (!g_win32_stack_initialized) {
git_error_set(GIT_ERROR_INVALID, "git_win32_stack not initialized.");
return GIT_ERROR;
}
memset(pdata, 0, sizeof(*pdata));
pdata->nr_frames = RtlCaptureStackBackTrace(
skip+1, GIT_WIN32__STACK__MAX_FRAMES, pdata->frames, NULL);
/* If an "aux" data provider was registered, ask it to capture
* whatever data it needs and give us an "aux_id" to it so that
* we can refer to it later when reporting.
*/
if (g_aux_cb_alloc)
(g_aux_cb_alloc)(&pdata->aux_id);
return 0;
}
int git_win32__stack_compare(
git_win32__stack__raw_data *d1,
git_win32__stack__raw_data *d2)
{
return memcmp(d1, d2, sizeof(*d1));
}
int git_win32__stack_format(
char *pbuf, size_t buf_len,
const git_win32__stack__raw_data *pdata,
const char *prefix, const char *suffix)
{
#define MY_MAX_FILENAME 255
/* SYMBOL_INFO has char FileName[1] at the end. The docs say to
* to malloc it with extra space for your desired max filename.
*/
struct {
SYMBOL_INFO symbol;
char extra[MY_MAX_FILENAME + 1];
} s;
IMAGEHLP_LINE64 line;
size_t buf_used = 0;
unsigned int k;
char detail[MY_MAX_FILENAME * 2]; /* filename plus space for function name and formatting */
size_t detail_len;
if (!g_win32_stack_initialized) {
git_error_set(GIT_ERROR_INVALID, "git_win32_stack not initialized.");
return GIT_ERROR;
}
if (!prefix)
prefix = "\t";
if (!suffix)
suffix = "\n";
memset(pbuf, 0, buf_len);
memset(&s, 0, sizeof(s));
s.symbol.MaxNameLen = MY_MAX_FILENAME;
s.symbol.SizeOfStruct = sizeof(SYMBOL_INFO);
memset(&line, 0, sizeof(line));
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
for (k=0; k < pdata->nr_frames; k++) {
DWORD64 frame_k = (DWORD64)pdata->frames[k];
DWORD dwUnused;
if (SymFromAddr(g_win32_stack_process, frame_k, 0, &s.symbol) &&
SymGetLineFromAddr64(g_win32_stack_process, frame_k, &dwUnused, &line)) {
const char *pslash;
const char *pfile;
pslash = strrchr(line.FileName, '\\');
pfile = ((pslash) ? (pslash+1) : line.FileName);
p_snprintf(detail, sizeof(detail), "%s%s:%d> %s%s",
prefix, pfile, line.LineNumber, s.symbol.Name, suffix);
} else {
/* This happens when we cross into another module.
* For example, in CLAR tests, this is typically
* the CRT startup code. Just print an unknown
* frame and continue.
*/
p_snprintf(detail, sizeof(detail), "%s??%s", prefix, suffix);
}
detail_len = strlen(detail);
if (buf_len < (buf_used + detail_len + 1)) {
/* we don't have room for this frame in the buffer, so just stop. */
break;
}
memcpy(&pbuf[buf_used], detail, detail_len);
buf_used += detail_len;
}
/* "aux_id" 0 is reserved to mean no aux data. This is needed to handle
* allocs that occur before the aux callbacks were registered.
*/
if (pdata->aux_id > 0) {
p_snprintf(detail, sizeof(detail), "%saux_id: %d%s",
prefix, pdata->aux_id, suffix);
detail_len = strlen(detail);
if ((buf_used + detail_len + 1) < buf_len) {
memcpy(&pbuf[buf_used], detail, detail_len);
buf_used += detail_len;
}
/* If an "aux" data provider is still registered, ask it to append its detailed
* data to the end of ours using the "aux_id" it gave us when this de-duped
* item was created.
*/
if (g_aux_cb_lookup)
(g_aux_cb_lookup)(pdata->aux_id, &pbuf[buf_used], (buf_len - buf_used - 1));
}
return GIT_OK;
}
int git_win32__stack(
char * pbuf, size_t buf_len,
int skip,
const char *prefix, const char *suffix)
{
git_win32__stack__raw_data data;
int error;
if ((error = git_win32__stack_capture(&data, skip)) < 0)
return error;
if ((error = git_win32__stack_format(pbuf, buf_len, &data, prefix, suffix)) < 0)
return error;
return 0;
}
#endif
......@@ -164,7 +164,7 @@ void _cl_trace_cb__event_handler(
switch (ev) {
case CL_TRACE__SUITE_BEGIN:
git_trace(GIT_TRACE_TRACE, "\n\n%s\n%s: Begin Suite", HR, suite_name);
#if 0 && defined(GIT_MSVC_CRTDBG)
#if 0 && defined(GIT_WIN32_LEAKCHECK)
git_win32__crtdbg_stacktrace__dump(
GIT_WIN32__CRTDBG_STACKTRACE__SET_MARK,
suite_name);
......@@ -172,7 +172,7 @@ void _cl_trace_cb__event_handler(
break;
case CL_TRACE__SUITE_END:
#if 0 && defined(GIT_MSVC_CRTDBG)
#if 0 && defined(GIT_WIN32_LEAKCHECK)
/* As an example of checkpointing, dump leaks within this suite.
* This may generate false positives for things like the global
* TLS error state and maybe the odb cache since they aren't
......
#include "clar_libgit2.h"
#include "win32/w32_stack.h"
#include "win32/w32_crtdbg_stacktrace.h"
#include "win32/w32_leakcheck.h"
#if defined(GIT_MSVC_CRTDBG)
#if defined(GIT_WIN32_LEAKCHECK)
static void a(void)
{
char buf[10000];
cl_assert(git_win32__stack(buf, sizeof(buf), 0, NULL, NULL) == 0);
cl_assert(git_win32_leakcheck_stack(buf, sizeof(buf), 0, NULL, NULL) == 0);
#if 0
fprintf(stderr, "Stacktrace from [%s:%d]:\n%s\n", __FILE__, __LINE__, buf);
......@@ -27,7 +26,7 @@ static void c(void)
void test_trace_windows_stacktrace__basic(void)
{
#if defined(GIT_MSVC_CRTDBG)
#if defined(GIT_WIN32_LEAKCHECK)
c();
#endif
}
......@@ -35,7 +34,7 @@ void test_trace_windows_stacktrace__basic(void)
void test_trace_windows_stacktrace__leaks(void)
{
#if defined(GIT_MSVC_CRTDBG)
#if defined(GIT_WIN32_LEAKCHECK)
void * p1;
void * p2;
void * p3;
......@@ -47,85 +46,85 @@ void test_trace_windows_stacktrace__leaks(void)
/* remember outstanding leaks due to set setup
* and set mark/checkpoint.
*/
before = git_win32__crtdbg_stacktrace__dump(
GIT_WIN32__CRTDBG_STACKTRACE__QUIET |
GIT_WIN32__CRTDBG_STACKTRACE__LEAKS_TOTAL |
GIT_WIN32__CRTDBG_STACKTRACE__SET_MARK,
before = git_win32_leakcheck_stacktrace_dump(
GIT_WIN32_LEAKCHECK_STACKTRACE_QUIET |
GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_TOTAL |
GIT_WIN32_LEAKCHECK_STACKTRACE_SET_MARK,
NULL);
p1 = git__malloc(5);
leaks = git_win32__crtdbg_stacktrace__dump(
GIT_WIN32__CRTDBG_STACKTRACE__QUIET |
GIT_WIN32__CRTDBG_STACKTRACE__LEAKS_SINCE_MARK,
leaks = git_win32_leakcheck_stacktrace_dump(
GIT_WIN32_LEAKCHECK_STACKTRACE_QUIET |
GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_SINCE_MARK,
"p1");
cl_assert_equal_i(1, leaks);
p2 = git__malloc(5);
leaks = git_win32__crtdbg_stacktrace__dump(
GIT_WIN32__CRTDBG_STACKTRACE__QUIET |
GIT_WIN32__CRTDBG_STACKTRACE__LEAKS_SINCE_MARK,
leaks = git_win32_leakcheck_stacktrace_dump(
GIT_WIN32_LEAKCHECK_STACKTRACE_QUIET |
GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_SINCE_MARK,
"p1,p2");
cl_assert_equal_i(2, leaks);
p3 = git__malloc(5);
leaks = git_win32__crtdbg_stacktrace__dump(
GIT_WIN32__CRTDBG_STACKTRACE__QUIET |
GIT_WIN32__CRTDBG_STACKTRACE__LEAKS_SINCE_MARK,
leaks = git_win32_leakcheck_stacktrace_dump(
GIT_WIN32_LEAKCHECK_STACKTRACE_QUIET |
GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_SINCE_MARK,
"p1,p2,p3");
cl_assert_equal_i(3, leaks);
git__free(p2);
leaks = git_win32__crtdbg_stacktrace__dump(
GIT_WIN32__CRTDBG_STACKTRACE__QUIET |
GIT_WIN32__CRTDBG_STACKTRACE__LEAKS_SINCE_MARK,
leaks = git_win32_leakcheck_stacktrace_dump(
GIT_WIN32_LEAKCHECK_STACKTRACE_QUIET |
GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_SINCE_MARK,
"p1,p3");
cl_assert_equal_i(2, leaks);
/* move the mark. only new leaks should appear afterwards */
error = git_win32__crtdbg_stacktrace__dump(
GIT_WIN32__CRTDBG_STACKTRACE__SET_MARK,
error = git_win32_leakcheck_stacktrace_dump(
GIT_WIN32_LEAKCHECK_STACKTRACE_SET_MARK,
NULL);
/* cannot use cl_git_pass() since that may allocate memory. */
cl_assert_equal_i(0, error);
leaks = git_win32__crtdbg_stacktrace__dump(
GIT_WIN32__CRTDBG_STACKTRACE__QUIET |
GIT_WIN32__CRTDBG_STACKTRACE__LEAKS_SINCE_MARK,
leaks = git_win32_leakcheck_stacktrace_dump(
GIT_WIN32_LEAKCHECK_STACKTRACE_QUIET |
GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_SINCE_MARK,
"not_p1,not_p3");
cl_assert_equal_i(0, leaks);
p4 = git__malloc(5);
leaks = git_win32__crtdbg_stacktrace__dump(
GIT_WIN32__CRTDBG_STACKTRACE__QUIET |
GIT_WIN32__CRTDBG_STACKTRACE__LEAKS_SINCE_MARK,
leaks = git_win32_leakcheck_stacktrace_dump(
GIT_WIN32_LEAKCHECK_STACKTRACE_QUIET |
GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_SINCE_MARK,
"p4,not_p1,not_p3");
cl_assert_equal_i(1, leaks);
git__free(p1);
git__free(p3);
leaks = git_win32__crtdbg_stacktrace__dump(
GIT_WIN32__CRTDBG_STACKTRACE__QUIET |
GIT_WIN32__CRTDBG_STACKTRACE__LEAKS_SINCE_MARK,
leaks = git_win32_leakcheck_stacktrace_dump(
GIT_WIN32_LEAKCHECK_STACKTRACE_QUIET |
GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_SINCE_MARK,
"p4");
cl_assert_equal_i(1, leaks);
git__free(p4);
leaks = git_win32__crtdbg_stacktrace__dump(
GIT_WIN32__CRTDBG_STACKTRACE__QUIET |
GIT_WIN32__CRTDBG_STACKTRACE__LEAKS_SINCE_MARK,
leaks = git_win32_leakcheck_stacktrace_dump(
GIT_WIN32_LEAKCHECK_STACKTRACE_QUIET |
GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_SINCE_MARK,
"end");
cl_assert_equal_i(0, leaks);
/* confirm current absolute leaks count matches beginning value. */
after = git_win32__crtdbg_stacktrace__dump(
GIT_WIN32__CRTDBG_STACKTRACE__QUIET |
GIT_WIN32__CRTDBG_STACKTRACE__LEAKS_TOTAL,
after = git_win32_leakcheck_stacktrace_dump(
GIT_WIN32_LEAKCHECK_STACKTRACE_QUIET |
GIT_WIN32_LEAKCHECK_STACKTRACE_LEAKS_TOTAL,
"total");
cl_assert_equal_i(before, after);
#endif
}
#if defined(GIT_MSVC_CRTDBG)
#if defined(GIT_WIN32_LEAKCHECK)
static void aux_cb_alloc__1(unsigned int *aux_id)
{
static unsigned int aux_counter = 0;
......@@ -142,12 +141,12 @@ static void aux_cb_lookup__1(unsigned int aux_id, char *aux_msg, size_t aux_msg_
void test_trace_windows_stacktrace__aux1(void)
{
#if defined(GIT_MSVC_CRTDBG)
git_win32__stack__set_aux_cb(aux_cb_alloc__1, aux_cb_lookup__1);
#if defined(GIT_WIN32_LEAKCHECK)
git_win32_leakcheck_stack_set_aux_cb(aux_cb_alloc__1, aux_cb_lookup__1);
c();
c();
c();
c();
git_win32__stack__set_aux_cb(NULL, NULL);
git_win32_leakcheck_stack_set_aux_cb(NULL, NULL);
#endif
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment