lhash/lhash.c: switch to Thread-Sanitizer-friendly primitives.
[openssl.git] / crypto / lhash / lhash.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <openssl/crypto.h>
14 #include <openssl/lhash.h>
15 #include <openssl/err.h>
16 #include "lhash_lcl.h"
17
18 /*
19  * A hashing implementation that appears to be based on the linear hashing
20  * alogrithm:
21  * https://en.wikipedia.org/wiki/Linear_hashing
22  *
23  * Litwin, Witold (1980), "Linear hashing: A new tool for file and table
24  * addressing", Proc. 6th Conference on Very Large Databases: 212-223
25  * http://hackthology.com/pdfs/Litwin-1980-Linear_Hashing.pdf
26  *
27  * From the wikipedia article "Linear hashing is used in the BDB Berkeley
28  * database system, which in turn is used by many software systems such as
29  * OpenLDAP, using a C implementation derived from the CACM article and first
30  * published on the Usenet in 1988 by Esmond Pitt."
31  *
32  * The CACM paper is available here:
33  * https://pdfs.semanticscholar.org/ff4d/1c5deca6269cc316bfd952172284dbf610ee.pdf
34  */
35
36 #undef MIN_NODES
37 #define MIN_NODES       16
38 #define UP_LOAD         (2*LH_LOAD_MULT) /* load times 256 (default 2) */
39 #define DOWN_LOAD       (LH_LOAD_MULT) /* load times 256 (default 1) */
40
41 static int expand(OPENSSL_LHASH *lh);
42 static void contract(OPENSSL_LHASH *lh);
43 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash);
44
45 OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
46 {
47     OPENSSL_LHASH *ret;
48
49     if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
50         /*
51          * Do not set the error code, because the ERR code uses LHASH
52          * and we want to avoid possible endless error loop.
53          * CRYPTOerr(CRYPTO_F_OPENSSL_LH_NEW, ERR_R_MALLOC_FAILURE);
54          */
55         return NULL;
56     }
57     if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
58         goto err;
59     ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
60     ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
61     ret->num_nodes = MIN_NODES / 2;
62     ret->num_alloc_nodes = MIN_NODES;
63     ret->pmax = MIN_NODES / 2;
64     ret->up_load = UP_LOAD;
65     ret->down_load = DOWN_LOAD;
66     return ret;
67
68 err:
69     OPENSSL_free(ret->b);
70     OPENSSL_free(ret);
71     return NULL;
72 }
73
74 void OPENSSL_LH_free(OPENSSL_LHASH *lh)
75 {
76     unsigned int i;
77     OPENSSL_LH_NODE *n, *nn;
78
79     if (lh == NULL)
80         return;
81
82     for (i = 0; i < lh->num_nodes; i++) {
83         n = lh->b[i];
84         while (n != NULL) {
85             nn = n->next;
86             OPENSSL_free(n);
87             n = nn;
88         }
89     }
90     OPENSSL_free(lh->b);
91     OPENSSL_free(lh);
92 }
93
94 void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
95 {
96     unsigned long hash;
97     OPENSSL_LH_NODE *nn, **rn;
98     void *ret;
99
100     lh->error = 0;
101     if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
102         return NULL;        /* 'lh->error++' already done in 'expand' */
103
104     rn = getrn(lh, data, &hash);
105
106     if (*rn == NULL) {
107         if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
108             lh->error++;
109             return NULL;
110         }
111         nn->data = data;
112         nn->next = NULL;
113         nn->hash = hash;
114         *rn = nn;
115         ret = NULL;
116         lh->num_insert++;
117         lh->num_items++;
118     } else {                    /* replace same key */
119         ret = (*rn)->data;
120         (*rn)->data = data;
121         lh->num_replace++;
122     }
123     return ret;
124 }
125
126 void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
127 {
128     unsigned long hash;
129     OPENSSL_LH_NODE *nn, **rn;
130     void *ret;
131
132     lh->error = 0;
133     rn = getrn(lh, data, &hash);
134
135     if (*rn == NULL) {
136         lh->num_no_delete++;
137         return NULL;
138     } else {
139         nn = *rn;
140         *rn = nn->next;
141         ret = nn->data;
142         OPENSSL_free(nn);
143         lh->num_delete++;
144     }
145
146     lh->num_items--;
147     if ((lh->num_nodes > MIN_NODES) &&
148         (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
149         contract(lh);
150
151     return ret;
152 }
153
154 void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
155 {
156     unsigned long hash;
157     OPENSSL_LH_NODE **rn;
158     void *ret;
159
160     tsan_store((TSAN_QUALIFIER int *)&lh->error, 0);
161
162     rn = getrn(lh, data, &hash);
163
164     if (*rn == NULL) {
165         tsan_counter(&lh->num_retrieve_miss);
166         return NULL;
167     } else {
168         ret = (*rn)->data;
169         tsan_counter(&lh->num_retrieve);
170     }
171
172     return ret;
173 }
174
175 static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
176                           OPENSSL_LH_DOALL_FUNC func,
177                           OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
178 {
179     int i;
180     OPENSSL_LH_NODE *a, *n;
181
182     if (lh == NULL)
183         return;
184
185     /*
186      * reverse the order so we search from 'top to bottom' We were having
187      * memory leaks otherwise
188      */
189     for (i = lh->num_nodes - 1; i >= 0; i--) {
190         a = lh->b[i];
191         while (a != NULL) {
192             n = a->next;
193             if (use_arg)
194                 func_arg(a->data, arg);
195             else
196                 func(a->data);
197             a = n;
198         }
199     }
200 }
201
202 void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
203 {
204     doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
205 }
206
207 void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
208 {
209     doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
210 }
211
212 static int expand(OPENSSL_LHASH *lh)
213 {
214     OPENSSL_LH_NODE **n, **n1, **n2, *np;
215     unsigned int p, pmax, nni, j;
216     unsigned long hash;
217
218     nni = lh->num_alloc_nodes;
219     p = lh->p;
220     pmax = lh->pmax;
221     if (p + 1 >= pmax) {
222         j = nni * 2;
223         n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
224         if (n == NULL) {
225             lh->error++;
226             return 0;
227         }
228         lh->b = n;
229         memset(n + nni, 0, sizeof(*n) * (j - nni));
230         lh->pmax = nni;
231         lh->num_alloc_nodes = j;
232         lh->num_expand_reallocs++;
233         lh->p = 0;
234     } else {
235         lh->p++;
236     }
237
238     lh->num_nodes++;
239     lh->num_expands++;
240     n1 = &(lh->b[p]);
241     n2 = &(lh->b[p + pmax]);
242     *n2 = NULL;
243
244     for (np = *n1; np != NULL;) {
245         hash = np->hash;
246         if ((hash % nni) != p) { /* move it */
247             *n1 = (*n1)->next;
248             np->next = *n2;
249             *n2 = np;
250         } else
251             n1 = &((*n1)->next);
252         np = *n1;
253     }
254
255     return 1;
256 }
257
258 static void contract(OPENSSL_LHASH *lh)
259 {
260     OPENSSL_LH_NODE **n, *n1, *np;
261
262     np = lh->b[lh->p + lh->pmax - 1];
263     lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
264     if (lh->p == 0) {
265         n = OPENSSL_realloc(lh->b,
266                             (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
267         if (n == NULL) {
268             /* fputs("realloc error in lhash",stderr); */
269             lh->error++;
270             return;
271         }
272         lh->num_contract_reallocs++;
273         lh->num_alloc_nodes /= 2;
274         lh->pmax /= 2;
275         lh->p = lh->pmax - 1;
276         lh->b = n;
277     } else
278         lh->p--;
279
280     lh->num_nodes--;
281     lh->num_contracts++;
282
283     n1 = lh->b[(int)lh->p];
284     if (n1 == NULL)
285         lh->b[(int)lh->p] = np;
286     else {
287         while (n1->next != NULL)
288             n1 = n1->next;
289         n1->next = np;
290     }
291 }
292
293 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
294                                const void *data, unsigned long *rhash)
295 {
296     OPENSSL_LH_NODE **ret, *n1;
297     unsigned long hash, nn;
298     OPENSSL_LH_COMPFUNC cf;
299
300     hash = (*(lh->hash)) (data);
301     tsan_counter(&lh->num_hash_calls);
302     *rhash = hash;
303
304     nn = hash % lh->pmax;
305     if (nn < lh->p)
306         nn = hash % lh->num_alloc_nodes;
307
308     cf = lh->comp;
309     ret = &(lh->b[(int)nn]);
310     for (n1 = *ret; n1 != NULL; n1 = n1->next) {
311         tsan_counter(&lh->num_hash_comps);
312         if (n1->hash != hash) {
313             ret = &(n1->next);
314             continue;
315         }
316         tsan_counter(&lh->num_comp_calls);
317         if (cf(n1->data, data) == 0)
318             break;
319         ret = &(n1->next);
320     }
321     return ret;
322 }
323
324 /*
325  * The following hash seems to work very well on normal text strings no
326  * collisions on /usr/dict/words and it distributes on %2^n quite well, not
327  * as good as MD5, but still good.
328  */
329 unsigned long OPENSSL_LH_strhash(const char *c)
330 {
331     unsigned long ret = 0;
332     long n;
333     unsigned long v;
334     int r;
335
336     if ((c == NULL) || (*c == '\0'))
337         return ret;
338
339     n = 0x100;
340     while (*c) {
341         v = n | (*c);
342         n += 0x100;
343         r = (int)((v >> 2) ^ v) & 0x0f;
344         ret = (ret << r) | (ret >> (32 - r));
345         ret &= 0xFFFFFFFFL;
346         ret ^= v * v;
347         c++;
348     }
349     return (ret >> 16) ^ ret;
350 }
351
352 unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
353 {
354     return lh ? lh->num_items : 0;
355 }
356
357 unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
358 {
359     return lh->down_load;
360 }
361
362 void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
363 {
364     lh->down_load = down_load;
365 }
366
367 int OPENSSL_LH_error(OPENSSL_LHASH *lh)
368 {
369     return lh->error;
370 }