- {
- int ret=mh_mode;
-
- CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
- switch (mode)
- {
- /* for applications: */
- case CRYPTO_MEM_CHECK_ON: /* aka MemCheck_start() */
- mh_mode = CRYPTO_MEM_CHECK_ON|CRYPTO_MEM_CHECK_ENABLE;
- disabling_thread = 0;
- break;
- case CRYPTO_MEM_CHECK_OFF: /* aka MemCheck_stop() */
- mh_mode = 0;
- disabling_thread = 0;
- break;
-
- /* switch off temporarily (for library-internal use): */
- case CRYPTO_MEM_CHECK_DISABLE: /* aka MemCheck_off() */
- if (mh_mode & CRYPTO_MEM_CHECK_ON)
- {
- mh_mode&= ~CRYPTO_MEM_CHECK_ENABLE;
- if (disabling_thread != CRYPTO_thread_id()) /* otherwise we already have the MALLOC2 lock */
- {
- /* Long-time lock CRYPTO_LOCK_MALLOC2 must not be claimed while
- * we're holding CRYPTO_LOCK_MALLOC, or we'll deadlock if
- * somebody else holds CRYPTO_LOCK_MALLOC2 (and cannot release
- * it because we block entry to this function).
- * Give them a chance, first, and then claim the locks in
- * appropriate order (long-time lock first).
- */
- CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
- /* Note that after we have waited for CRYPTO_LOCK_MALLOC2
- * and CRYPTO_LOCK_MALLOC, we'll still be in the right
- * "case" and "if" branch because MemCheck_start and
- * MemCheck_stop may never be used while there are multiple
- * OpenSSL threads. */
- CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2);
- CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
- disabling_thread=CRYPTO_thread_id();
- }
- }
- break;
- case CRYPTO_MEM_CHECK_ENABLE: /* aka MemCheck_on() */
- if (mh_mode & CRYPTO_MEM_CHECK_ON)
- {
- mh_mode|=CRYPTO_MEM_CHECK_ENABLE;
- if (disabling_thread != 0)
- {
- disabling_thread=0;
- CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2);
- }
- }
- break;
-
- default:
- break;
- }
- CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
- return(ret);
- }
-
-int CRYPTO_is_mem_check_on(void)
- {
- int ret = 0;
-
- if (mh_mode & CRYPTO_MEM_CHECK_ON)
- {
- CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
-
- ret = (mh_mode & CRYPTO_MEM_CHECK_ENABLE)
- && disabling_thread != CRYPTO_thread_id();
-
- CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
- }
- return(ret);
- }
-
-
-void CRYPTO_dbg_set_options(long bits)
- {
- options = bits;
- }
-
-long CRYPTO_dbg_get_options(void)
- {
- return options;
- }
-
-/* static int mem_cmp(MEM *a, MEM *b) */
-static int mem_cmp(void *a_void, void *b_void)
- {
- return((char *)((MEM *)a_void)->addr - (char *)((MEM *)b_void)->addr);
- }
-
-/* static unsigned long mem_hash(MEM *a) */
-static unsigned long mem_hash(void *a_void)
- {
- unsigned long ret;
-
- ret=(unsigned long)((MEM *)a_void)->addr;
-
- ret=ret*17851+(ret>>14)*7+(ret>>4)*251;
- return(ret);
- }
+{
+#ifdef OPENSSL_NO_CRYPTO_MDEBUG
+ return mode - mode;
+#else
+ int ret = mh_mode;
+
+ CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
+ switch (mode) {
+ default:
+ break;
+
+ case CRYPTO_MEM_CHECK_ON:
+ mh_mode = CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE;
+ num_disable = 0;
+ break;
+
+ case CRYPTO_MEM_CHECK_OFF:
+ mh_mode = 0;
+ num_disable = 0;
+ break;
+
+ /* switch off temporarily (for library-internal use): */
+ case CRYPTO_MEM_CHECK_DISABLE:
+ if (mh_mode & CRYPTO_MEM_CHECK_ON) {
+ CRYPTO_THREADID cur;
+ CRYPTO_THREADID_current(&cur);
+ /* see if we don't have the MALLOC2 lock already */
+ if (!num_disable
+ || CRYPTO_THREADID_cmp(&disabling_threadid, &cur)) {
+ /*
+ * Long-time lock CRYPTO_LOCK_MALLOC2 must not be claimed
+ * while we're holding CRYPTO_LOCK_MALLOC, or we'll deadlock
+ * if somebody else holds CRYPTO_LOCK_MALLOC2 (and cannot
+ * release it because we block entry to this function). Give
+ * them a chance, first, and then claim the locks in
+ * appropriate order (long-time lock first).
+ */
+ CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
+ /*
+ * Note that after we have waited for CRYPTO_LOCK_MALLOC2 and
+ * CRYPTO_LOCK_MALLOC, we'll still be in the right "case" and
+ * "if" branch because MemCheck_start and MemCheck_stop may
+ * never be used while there are multiple OpenSSL threads.
+ */
+ CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2);
+ CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);
+ mh_mode &= ~CRYPTO_MEM_CHECK_ENABLE;
+ CRYPTO_THREADID_cpy(&disabling_threadid, &cur);
+ }
+ num_disable++;
+ }
+ break;
+
+ case CRYPTO_MEM_CHECK_ENABLE:
+ if (mh_mode & CRYPTO_MEM_CHECK_ON) {
+ if (num_disable) { /* always true, or something is going wrong */
+ num_disable--;
+ if (num_disable == 0) {
+ mh_mode |= CRYPTO_MEM_CHECK_ENABLE;
+ CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2);
+ }
+ }
+ }
+ break;
+ }
+ CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);
+ return (ret);
+#endif
+}
+
+#ifndef OPENSSL_NO_CRYPTO_MDEBUG
+
+static int mem_check_on(void)
+{
+ int ret = 0;
+
+ if (mh_mode & CRYPTO_MEM_CHECK_ON) {
+ CRYPTO_THREADID cur;
+ CRYPTO_THREADID_current(&cur);
+ CRYPTO_r_lock(CRYPTO_LOCK_MALLOC);
+
+ ret = (mh_mode & CRYPTO_MEM_CHECK_ENABLE)
+ || CRYPTO_THREADID_cmp(&disabling_threadid, &cur);
+
+ CRYPTO_r_unlock(CRYPTO_LOCK_MALLOC);
+ }
+ return (ret);
+}
+
+static int mem_cmp(const MEM *a, const MEM *b)
+{
+#ifdef _WIN64
+ const char *ap = (const char *)a->addr, *bp = (const char *)b->addr;
+ if (ap == bp)
+ return 0;
+ else if (ap > bp)
+ return 1;
+ else
+ return -1;
+#else
+ return (const char *)a->addr - (const char *)b->addr;
+#endif
+}
+
+static IMPLEMENT_LHASH_COMP_FN(mem, MEM)
+
+static unsigned long mem_hash(const MEM *a)
+{
+ size_t ret;
+
+ ret = (size_t)a->addr;
+
+ ret = ret * 17851 + (ret >> 14) * 7 + (ret >> 4) * 251;
+ return (ret);
+}
+
+static IMPLEMENT_LHASH_HASH_FN(mem, MEM)