Browse Source

Reorganize some quick-and-dirty code to find out what openssl stuff is leaking, using dmalloc.

svn:r5178
Nick Mathewson 20 years ago
parent
commit
c5ee3e961e
3 changed files with 33 additions and 21 deletions
  1. 8 8
      src/common/util.c
  2. 12 12
      src/common/util.h
  3. 13 1
      src/or/main.c

+ 8 - 8
src/common/util.c

@@ -96,7 +96,7 @@ const char util_c_id[] = "$Id$";
  * ===== */
  * ===== */
 #ifdef USE_DMALLOC
 #ifdef USE_DMALLOC
  #include <dmalloc.h>
  #include <dmalloc.h>
- #define DMALLOC_FN_ARGS file, line,
+ #define DMALLOC_FN_ARGS , file, line
 #else
 #else
  #define dmalloc_strdup(file, line, string, xalloc_b) strdup(string)
  #define dmalloc_strdup(file, line, string, xalloc_b) strdup(string)
 
 
@@ -116,7 +116,7 @@ const char util_c_id[] = "$Id$";
  * ignored otherwise.
  * ignored otherwise.
  */
  */
 void *
 void *
-_tor_malloc(DMALLOC_PARAMS size_t size)
+_tor_malloc(size_t size DMALLOC_PARAMS)
 {
 {
   void *result;
   void *result;
 
 
@@ -141,9 +141,9 @@ _tor_malloc(DMALLOC_PARAMS size_t size)
  * the process on error.  (Same as calloc(size,1), but never returns NULL.)
  * the process on error.  (Same as calloc(size,1), but never returns NULL.)
  */
  */
 void *
 void *
-_tor_malloc_zero(DMALLOC_PARAMS size_t size)
+_tor_malloc_zero(size_t size DMALLOC_PARAMS)
 {
 {
-  void *result = _tor_malloc(DMALLOC_FN_ARGS size);
+  void *result = _tor_malloc(size DMALLOC_FN_ARGS);
   memset(result, 0, size);
   memset(result, 0, size);
   return result;
   return result;
 }
 }
@@ -153,7 +153,7 @@ _tor_malloc_zero(DMALLOC_PARAMS size_t size)
  * terminate. (Like realloc(ptr,size), but never returns NULL.)
  * terminate. (Like realloc(ptr,size), but never returns NULL.)
  */
  */
 void *
 void *
-_tor_realloc(DMALLOC_PARAMS void *ptr, size_t size)
+_tor_realloc(void *ptr, size_t size DMALLOC_PARAMS)
 {
 {
   void *result;
   void *result;
 
 
@@ -170,7 +170,7 @@ _tor_realloc(DMALLOC_PARAMS void *ptr, size_t size)
  * NULL.)
  * NULL.)
  */
  */
 char *
 char *
-_tor_strdup(DMALLOC_PARAMS const char *s)
+_tor_strdup(const char *s DMALLOC_PARAMS)
 {
 {
   char *dup;
   char *dup;
   tor_assert(s);
   tor_assert(s);
@@ -190,11 +190,11 @@ _tor_strdup(DMALLOC_PARAMS const char *s)
  * NULL.)
  * NULL.)
  */
  */
 char *
 char *
-_tor_strndup(DMALLOC_PARAMS const char *s, size_t n)
+_tor_strndup(const char *s, size_t n DMALLOC_PARAMS)
 {
 {
   char *dup;
   char *dup;
   tor_assert(s);
   tor_assert(s);
-  dup = _tor_malloc(DMALLOC_FN_ARGS n+1);
+  dup = _tor_malloc((n+1) DMALLOC_FN_ARGS);
   /* Performance note: Ordinarily we prefer strlcpy to strncpy.  But
   /* Performance note: Ordinarily we prefer strlcpy to strncpy.  But
    * this function gets called a whole lot, and platform strncpy is
    * this function gets called a whole lot, and platform strncpy is
    * much faster than strlcpy when strlen(s) is much longer than n.
    * much faster than strlcpy when strlen(s) is much longer than n.

+ 12 - 12
src/common/util.h

@@ -48,8 +48,8 @@
 #endif
 #endif
 
 
 #ifdef USE_DMALLOC
 #ifdef USE_DMALLOC
-#define DMALLOC_PARAMS const char *file, const int line,
-#define DMALLOC_ARGS _SHORT_FILE_, __LINE__,
+#define DMALLOC_PARAMS , const char *file, const int line
+#define DMALLOC_ARGS , _SHORT_FILE_, __LINE__
 #else
 #else
 #define DMALLOC_PARAMS
 #define DMALLOC_PARAMS
 #define DMALLOC_ARGS
 #define DMALLOC_ARGS
@@ -61,11 +61,11 @@
 #define tor_fragile_assert()
 #define tor_fragile_assert()
 
 
 /* Memory management */
 /* Memory management */
-void *_tor_malloc(DMALLOC_PARAMS size_t size);
-void *_tor_malloc_zero(DMALLOC_PARAMS size_t size);
-void *_tor_realloc(DMALLOC_PARAMS void *ptr, size_t size);
-char *_tor_strdup(DMALLOC_PARAMS const char *s);
-char *_tor_strndup(DMALLOC_PARAMS const char *s, size_t n);
+void *_tor_malloc(size_t size DMALLOC_PARAMS);
+void *_tor_malloc_zero(size_t size DMALLOC_PARAMS);
+void *_tor_realloc(void *ptr, size_t size DMALLOC_PARAMS);
+char *_tor_strdup(const char *s DMALLOC_PARAMS);
+char *_tor_strndup(const char *s, size_t n DMALLOC_PARAMS);
 #ifdef USE_DMALLOC
 #ifdef USE_DMALLOC
 extern int dmalloc_free(const char *file, const int line, void *pnt,
 extern int dmalloc_free(const char *file, const int line, void *pnt,
                         const int func_id);
                         const int func_id);
@@ -79,11 +79,11 @@ extern int dmalloc_free(const char *file, const int line, void *pnt,
 #define tor_free(p) do { if (p) {free(p); (p)=NULL;} } while (0)
 #define tor_free(p) do { if (p) {free(p); (p)=NULL;} } while (0)
 #endif
 #endif
 
 
-#define tor_malloc(size)       _tor_malloc(DMALLOC_ARGS size)
-#define tor_malloc_zero(size)  _tor_malloc_zero(DMALLOC_ARGS size)
-#define tor_realloc(ptr, size) _tor_realloc(DMALLOC_ARGS ptr, size)
-#define tor_strdup(s)          _tor_strdup(DMALLOC_ARGS s)
-#define tor_strndup(s, n)      _tor_strndup(DMALLOC_ARGS s, n)
+#define tor_malloc(size)       _tor_malloc(size DMALLOC_ARGS)
+#define tor_malloc_zero(size)  _tor_malloc_zero(size DMALLOC_ARGS)
+#define tor_realloc(ptr, size) _tor_realloc(ptr, size DMALLOC_ARGS)
+#define tor_strdup(s)          _tor_strdup(s DMALLOC_ARGS)
+#define tor_strndup(s, n)      _tor_strndup(s, n DMALLOC_ARGS)
 
 
 /* String manipulation */
 /* String manipulation */
 #define HEX_CHARACTERS "0123456789ABCDEFabcdef"
 #define HEX_CHARACTERS "0123456789ABCDEFabcdef"

+ 13 - 1
src/or/main.c

@@ -1907,10 +1907,23 @@ nt_strerror(uint32_t errnum)
 }
 }
 #endif
 #endif
 
 
+#ifdef USE_DMALLOC
+#include <openssl/crypto.h>
+static void
+_tor_dmalloc_free(void *p)
+{
+  tor_free(p);
+}
+#endif
+
 /** DOCDOC */
 /** DOCDOC */
 int
 int
 tor_main(int argc, char *argv[])
 tor_main(int argc, char *argv[])
 {
 {
+#ifdef USE_DMALLOC
+    int r = CRYPTO_set_mem_ex_functions(_tor_malloc, _tor_realloc, _tor_dmalloc_free);
+    log_fn(LOG_NOTICE, "r = %d", r);
+#endif
 #ifdef MS_WINDOWS_SERVICE
 #ifdef MS_WINDOWS_SERVICE
   backup_argv = argv;
   backup_argv = argv;
   backup_argc = argc;
   backup_argc = argc;
@@ -1963,4 +1976,3 @@ tor_main(int argc, char *argv[])
   tor_cleanup();
   tor_cleanup();
   return -1;
   return -1;
 }
 }
-