get rid of most TLS use
authorRudolf Polzer <divverent@xonotic.org>
Thu, 27 Oct 2011 10:05:31 +0000 (12:05 +0200)
committerRudolf Polzer <divverent@xonotic.org>
Thu, 27 Oct 2011 10:05:31 +0000 (12:05 +0200)
d0_bignum-gmp.c
d0_bignum-openssl.c
d0_bignum-tommath.c

index 153055e..95b95e2 100644 (file)
@@ -58,11 +58,31 @@ struct d0_bignum_s
 
 static gmp_randstate_t RANDSTATE;
 static d0_bignum_t temp;
-static void *tempmutex = NULL; // hold this mutex when using RANDSTATE or temp
+static unsigned char numbuf[65536];
+static void *tempmutex = NULL; // hold this mutex when using RANDSTATE or temp or numbuf
 
 #include <time.h>
 #include <stdio.h>
 
+static void *allocate_function (size_t alloc_size)
+{
+       return d0_malloc(alloc_size);
+}
+static void *reallocate_function (void *ptr, size_t old_size, size_t new_size)
+{
+       void *data;
+       if(old_size == new_size)
+               return ptr;
+       data = d0_malloc(new_size);
+       memcpy(data, ptr, (old_size < new_size) ? old_size : new_size);
+       d0_free(ptr);
+       return data;
+}
+void deallocate_function (void *ptr, size_t size)
+{
+       d0_free(ptr);
+}
+
 D0_WARN_UNUSED_RESULT D0_BOOL d0_bignum_INITIALIZE(void)
 {
        FILE *f;
@@ -72,6 +92,8 @@ D0_WARN_UNUSED_RESULT D0_BOOL d0_bignum_INITIALIZE(void)
        tempmutex = d0_createmutex();
        d0_lockmutex(tempmutex);
 
+       mp_set_memory_functions(allocate_function, reallocate_function, deallocate_function);
+
        d0_bignum_init(&temp);
        gmp_randinit_mt(RANDSTATE);
        gmp_randseed_ui(RANDSTATE, time(NULL));
@@ -146,28 +168,48 @@ void d0_bignum_SHUTDOWN(void)
 
 D0_BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
 {
-       static __thread unsigned char numbuf[65536];
+       D0_BOOL ret;
        size_t count = 0;
+
+       d0_lockmutex(tempmutex);
        numbuf[0] = mpz_sgn(bignum->z) & 3;
        if((numbuf[0] & 3) != 0) // nonzero
        {
                count = (mpz_sizeinbase(bignum->z, 2) + 7) / 8;
                if(count > sizeof(numbuf) - 1)
+               {
+                       d0_unlockmutex(tempmutex);
                        return 0;
+               }
                mpz_export(numbuf+1, &count, 1, 1, 0, 0, bignum->z);
        }
-       return d0_iobuf_write_packet(buf, numbuf, count + 1);
+       ret = d0_iobuf_write_packet(buf, numbuf, count + 1);
+       d0_unlockmutex(tempmutex);
+       return ret;
 }
 
 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
 {
-       static __thread unsigned char numbuf[65536];
        size_t count = sizeof(numbuf);
+
+       d0_lockmutex(tempmutex);
        if(!d0_iobuf_read_packet(buf, numbuf, &count))
+       {
+               d0_unlockmutex(tempmutex);
                return NULL;
+       }
        if(count < 1)
+       {
+               d0_unlockmutex(tempmutex);
                return NULL;
-       if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
+       }
+       if(!bignum)
+               bignum = d0_bignum_new();
+       if(!bignum)
+       {
+               d0_unlockmutex(tempmutex);
+               return NULL;
+       }
        if(numbuf[0] & 3) // nonzero
        {
                mpz_import(bignum->z, count-1, 1, 1, 0, 0, numbuf+1);
@@ -178,6 +220,7 @@ d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
        {
                mpz_set_ui(bignum->z, 0);
        }
+       d0_unlockmutex(tempmutex);
        return bignum;
 }
 
@@ -428,5 +471,5 @@ d0_bignum_t *d0_bignum_gcd(d0_bignum_t *r, d0_bignum_t *s, d0_bignum_t *t, const
 
 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
 {
-       return mpz_get_str(NULL, base, x->z);
+       return mpz_get_str(NULL, base, x->z); // this allocates!
 }
index aef806c..8b52eb2 100644 (file)
@@ -60,7 +60,8 @@ struct d0_bignum_s
 
 static d0_bignum_t temp;
 static BN_CTX *ctx;
-static void *tempmutex = NULL; // hold this mutex when using ctx or temp
+static unsigned char numbuf[65536];
+static void *tempmutex = NULL; // hold this mutex when using ctx or temp or numbuf
 
 #include <time.h>
 #include <stdio.h>
@@ -201,28 +202,48 @@ void d0_bignum_SHUTDOWN(void)
 
 D0_BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
 {
-       static __thread unsigned char numbuf[65536];
+       D0_BOOL ret;
        size_t count = 0;
+
+       d0_lockmutex(tempmutex);
        numbuf[0] = BN_is_zero(&bignum->z) ? 0 : BN_is_negative(&bignum->z) ? 3 : 1;
        if((numbuf[0] & 3) != 0) // nonzero
        {
                count = BN_num_bytes(&bignum->z);
                if(count > sizeof(numbuf) - 1)
+               {
+                       d0_unlockmutex(tempmutex);
                        return 0;
+               }
                BN_bn2bin(&bignum->z, numbuf+1);
        }
-       return d0_iobuf_write_packet(buf, numbuf, count + 1);
+       ret = d0_iobuf_write_packet(buf, numbuf, count + 1);
+       d0_unlockmutex(tempmutex);
+       return ret;
 }
 
 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
 {
-       static __thread unsigned char numbuf[65536];
        size_t count = sizeof(numbuf);
+
+       d0_lockmutex(tempmutex);
        if(!d0_iobuf_read_packet(buf, numbuf, &count))
+       {
+               d0_unlockmutex(tempmutex);
                return NULL;
+       }
        if(count < 1)
+       {
+               d0_unlockmutex(tempmutex);
                return NULL;
-       if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
+       }
+       if(!bignum)
+               bignum = d0_bignum_new();
+       if(!bignum)
+       {
+               d0_unlockmutex(tempmutex);
+               return NULL;
+       }
        if(numbuf[0] & 3) // nonzero
        {
                BN_bin2bn(numbuf+1, count-1, &bignum->z);
@@ -233,6 +254,7 @@ d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
        {
                BN_zero(&bignum->z);
        }
+       d0_unlockmutex(tempmutex);
        return bignum;
 }
 
@@ -490,10 +512,18 @@ d0_bignum_t *d0_bignum_gcd(d0_bignum_t *r, d0_bignum_t *s, d0_bignum_t *t, const
 
 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
 {
+       char *s = NULL;
+       char *s2;
+       size_t n;
        if(base == 10)
-               return BN_bn2dec(&x->z);
+               s = BN_bn2dec(&x->z);
        else if(base == 16)
-               return BN_bn2hex(&x->z);
+               s = BN_bn2hex(&x->z);
        else
                assert(!"Other bases not implemented");
+       n = strlen(s) + 1;
+       s2 = d0_malloc(n);
+       memcpy(s2, s, n);
+       OPENSSL_free(s);
+       return s2;
 }
index d78329f..75392a4 100644 (file)
@@ -51,7 +51,8 @@ struct d0_bignum_s
 };
 
 static d0_bignum_t temp;
-static void *tempmutex = NULL; // hold this mutex when using temp
+static unsigned char numbuf[65536];
+static void *tempmutex = NULL; // hold this mutex when using temp or numbuf
 
 #include <stdio.h>
 
@@ -136,28 +137,47 @@ void d0_bignum_SHUTDOWN(void)
 
 D0_BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
 {
-       static __thread unsigned char numbuf[65536];
+       D0_BOOL ret;
        size_t count = 0;
+
+       d0_lockmutex(tempmutex);
        numbuf[0] = (mp_iszero(&bignum->z) ? 0 : (bignum->z.sign == MP_ZPOS) ? 1 : 3);
        if((numbuf[0] & 3) != 0) // nonzero
        {
                count = mp_unsigned_bin_size((mp_int *) &bignum->z);
                if(count > sizeof(numbuf) - 1)
+               {
+                       d0_unlockmutex(tempmutex);
                        return 0;
+               }
                mp_to_unsigned_bin((mp_int *) &bignum->z, numbuf+1);
        }
-       return d0_iobuf_write_packet(buf, numbuf, count + 1);
+       ret = d0_iobuf_write_packet(buf, numbuf, count + 1);
+       d0_unlockmutex(tempmutex);
+       return ret;
 }
 
 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
 {
-       static __thread unsigned char numbuf[65536];
        size_t count = sizeof(numbuf);
+       d0_lockmutex(tempmutex);
        if(!d0_iobuf_read_packet(buf, numbuf, &count))
+       {
+               d0_unlockmutex(tempmutex);
                return NULL;
+       }
        if(count < 1)
+       {
+               d0_unlockmutex(tempmutex);
                return NULL;
-       if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
+       }
+       if(!bignum)
+               bignum = d0_bignum_new();
+       if(!bignum)
+       {
+               d0_unlockmutex(tempmutex);
+               return NULL;
+       }
        if(numbuf[0] & 3) // nonzero
        {
                mp_read_unsigned_bin(&bignum->z, numbuf+1, count-1);
@@ -168,6 +188,7 @@ d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
        {
                mp_zero(&bignum->z);
        }
+       d0_unlockmutex(tempmutex);
        return bignum;
 }
 
@@ -259,15 +280,18 @@ static d0_bignum_t *d0_bignum_rand_0_to_limit(d0_bignum_t *r, const d0_bignum_t
        size_t n = d0_bignum_size(limit);
        size_t b = (n + 7) / 8;
        unsigned char mask = "\xFF\x7F\x3F\x1F\x0F\x07\x03\x01"[8*b - n];
-       unsigned char numbuf[65536];
        assert(b <= sizeof(numbuf));
+       d0_lockmutex(tempmutex);
        for(;;)
        {
                rand_bytes(numbuf, b);
                numbuf[0] &= mask;
                r = d0_bignum_import_unsigned(r, numbuf, b);
                if(d0_bignum_cmp(r, limit) < 0)
+               {
+                       d0_unlockmutex(tempmutex);
                        return r;
+               }
        }
 }
 
@@ -459,7 +483,10 @@ d0_bignum_t *d0_bignum_gcd(d0_bignum_t *r, d0_bignum_t *s, d0_bignum_t *t, const
 
 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
 {
-       static __thread char str[65536];
-       mp_toradix_n((mp_int *) &x->z, str, base, sizeof(str));
+       char *str;
+       int sz = 0;
+       mp_radix_size((mp_int *) &x->z, base, &sz);
+       str = d0_malloc(sz + 1);
+       mp_toradix_n((mp_int *) &x->z, str, base, sz + 1);
        return str;
 }