hashtable.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright 2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * https://www.openssl.org/source/license.html
  8. * or in the file LICENSE in the source distribution.
  9. */
  10. /*
  11. * Test hashtable operation.
  12. */
  13. #include <limits.h>
  14. #include <openssl/err.h>
  15. #include <openssl/bio.h>
  16. #include <internal/common.h>
  17. #include <internal/hashtable.h>
  18. #include "fuzzer.h"
  19. /*
  20. * Make the key space very small here to make lookups
  21. * easy to predict for the purposes of validation
  22. * A two byte key gives us 65536 possible entries
  23. * so we can allocate a flat table to compare to
  24. */
  25. HT_START_KEY_DEFN(fuzzer_key)
  26. HT_DEF_KEY_FIELD(fuzzkey, uint16_t)
  27. HT_END_KEY_DEFN(FUZZER_KEY)
  28. #define FZ_FLAG_ALLOCATED (1 << 0)
  29. typedef struct fuzzer_value_st {
  30. uint64_t flags;
  31. uint64_t value;
  32. } FUZZER_VALUE;
  33. IMPLEMENT_HT_VALUE_TYPE_FNS(FUZZER_VALUE, fz, static)
  34. static size_t skipped_values = 0;
  35. static size_t inserts = 0;
  36. static size_t replacements = 0;
  37. static size_t deletes = 0;
  38. static size_t flushes = 0;
  39. static size_t lookups = 0;
  40. static size_t foreaches = 0;
  41. static size_t filters = 0;
  42. static int valfound;
  43. static FUZZER_VALUE *prediction_table = NULL;
  44. static HT *fuzzer_table = NULL;
  45. /*
  46. * Operational values
  47. */
  48. #define OP_INSERT 0
  49. #define OP_DELETE 1
  50. #define OP_LOOKUP 2
  51. #define OP_FLUSH 3
  52. #define OP_FOREACH 4
  53. #define OP_FILTER 5
  54. #define OP_END 6
  55. #define OP_MASK 0x3f
  56. #define INSERT_REPLACE_MASK 0x40
  57. #define OPERATION(x) (((x) & OP_MASK) % OP_END)
  58. #define IS_REPLACE(x) ((x) & INSERT_REPLACE_MASK)
  59. static int table_iterator(HT_VALUE *v, void *arg)
  60. {
  61. uint16_t keyval = (*(uint16_t *)arg);
  62. FUZZER_VALUE *f = ossl_ht_fz_FUZZER_VALUE_from_value(v);
  63. if (f != NULL && f == &prediction_table[keyval]) {
  64. valfound = 1;
  65. return 0;
  66. }
  67. return 1;
  68. }
  69. static int filter_iterator(HT_VALUE *v, void *arg)
  70. {
  71. uint16_t keyval = (*(uint16_t *)arg);
  72. FUZZER_VALUE *f = ossl_ht_fz_FUZZER_VALUE_from_value(v);
  73. if (f != NULL && f == &prediction_table[keyval])
  74. return 1;
  75. return 0;
  76. }
  77. static void fuzz_free_cb(HT_VALUE *v)
  78. {
  79. FUZZER_VALUE *f = ossl_ht_fz_FUZZER_VALUE_from_value(v);
  80. if (f != NULL)
  81. f->flags &= ~FZ_FLAG_ALLOCATED;
  82. }
  83. int FuzzerInitialize(int *argc, char ***argv)
  84. {
  85. HT_CONFIG fuzz_conf = {NULL, fuzz_free_cb, NULL, 0, 1};
  86. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
  87. ERR_clear_error();
  88. prediction_table = OPENSSL_zalloc(sizeof(FUZZER_VALUE) * 65537);
  89. if (prediction_table == NULL)
  90. return -1;
  91. fuzzer_table = ossl_ht_new(&fuzz_conf);
  92. if (fuzzer_table == NULL) {
  93. OPENSSL_free(prediction_table);
  94. return -1;
  95. }
  96. return 0;
  97. }
  98. int FuzzerTestOneInput(const uint8_t *buf, size_t len)
  99. {
  100. uint8_t op_flags;
  101. uint16_t keyval;
  102. int rc;
  103. int rc_prediction = 1;
  104. size_t i;
  105. FUZZER_VALUE *valptr, *lval;
  106. FUZZER_KEY key;
  107. HT_VALUE *v = NULL;
  108. HT_VALUE tv;
  109. HT_VALUE_LIST *htvlist;
  110. /*
  111. * We need at least 11 bytes to be able to do anything here
  112. * 1 byte to detect the operation to perform, 2 bytes
  113. * for the lookup key, and 8 bytes of value
  114. */
  115. if (len < 11) {
  116. skipped_values++;
  117. return -1;
  118. }
  119. /*
  120. * parse out our operation flags and key
  121. */
  122. op_flags = buf[0];
  123. memcpy(&keyval, &buf[1], sizeof(uint16_t));
  124. /*
  125. * Initialize our key
  126. */
  127. HT_INIT_KEY(&key);
  128. /*
  129. * Now do our operation
  130. */
  131. switch(OPERATION(op_flags)) {
  132. case OP_INSERT:
  133. valptr = &prediction_table[keyval];
  134. /* reset our key */
  135. HT_KEY_RESET(&key);
  136. /* set the proper key value */
  137. HT_SET_KEY_FIELD(&key, fuzzkey, keyval);
  138. /* lock the table */
  139. ossl_ht_write_lock(fuzzer_table);
  140. /*
  141. * If the value to insert is already allocated
  142. * then we expect a conflict in the insert
  143. * i.e. we predict a return code of 0 instead
  144. * of 1. On replacement, we expect it to succeed
  145. * always
  146. */
  147. if (valptr->flags & FZ_FLAG_ALLOCATED) {
  148. if (!IS_REPLACE(op_flags))
  149. rc_prediction = 0;
  150. }
  151. memcpy(&valptr->value, &buf[3], sizeof(uint64_t));
  152. /*
  153. * do the insert/replace
  154. */
  155. if (IS_REPLACE(op_flags))
  156. rc = ossl_ht_fz_FUZZER_VALUE_insert(fuzzer_table, TO_HT_KEY(&key),
  157. valptr, &lval);
  158. else
  159. rc = ossl_ht_fz_FUZZER_VALUE_insert(fuzzer_table, TO_HT_KEY(&key),
  160. valptr, NULL);
  161. if (rc == -1)
  162. /* failed to grow the hash table due to too many collisions */
  163. break;
  164. /*
  165. * mark the entry as being allocated
  166. */
  167. valptr->flags |= FZ_FLAG_ALLOCATED;
  168. /*
  169. * unlock the table
  170. */
  171. ossl_ht_write_unlock(fuzzer_table);
  172. /*
  173. * Now check to make sure we did the right thing
  174. */
  175. OPENSSL_assert(rc == rc_prediction);
  176. /*
  177. * successful insertion if there wasn't a conflict
  178. */
  179. if (rc_prediction == 1)
  180. IS_REPLACE(op_flags) ? replacements++ : inserts++;
  181. break;
  182. case OP_DELETE:
  183. valptr = &prediction_table[keyval];
  184. /* reset our key */
  185. HT_KEY_RESET(&key);
  186. /* set the proper key value */
  187. HT_SET_KEY_FIELD(&key, fuzzkey, keyval);
  188. /* lock the table */
  189. ossl_ht_write_lock(fuzzer_table);
  190. /*
  191. * If the value to delete is not already allocated
  192. * then we expect a miss in the delete
  193. * i.e. we predict a return code of 0 instead
  194. * of 1
  195. */
  196. if (!(valptr->flags & FZ_FLAG_ALLOCATED))
  197. rc_prediction = 0;
  198. /*
  199. * do the delete
  200. */
  201. rc = ossl_ht_delete(fuzzer_table, TO_HT_KEY(&key));
  202. /*
  203. * unlock the table
  204. */
  205. ossl_ht_write_unlock(fuzzer_table);
  206. /*
  207. * Now check to make sure we did the right thing
  208. */
  209. OPENSSL_assert(rc == rc_prediction);
  210. /*
  211. * once the unlock is done, the table rcu will have synced
  212. * meaning the free function has run, so we can confirm now
  213. * that the valptr is no longer allocated
  214. */
  215. OPENSSL_assert(!(valptr->flags & FZ_FLAG_ALLOCATED));
  216. /*
  217. * successful deletion if there wasn't a conflict
  218. */
  219. if (rc_prediction == 1)
  220. deletes++;
  221. break;
  222. case OP_LOOKUP:
  223. valptr = &prediction_table[keyval];
  224. lval = NULL;
  225. /* reset our key */
  226. HT_KEY_RESET(&key);
  227. /* set the proper key value */
  228. HT_SET_KEY_FIELD(&key, fuzzkey, keyval);
  229. /* lock the table for reading */
  230. ossl_ht_read_lock(fuzzer_table);
  231. /*
  232. * If the value to find is not already allocated
  233. * then we expect a miss in the lookup
  234. * i.e. we predict a return code of NULL instead
  235. * of a pointer
  236. */
  237. if (!(valptr->flags & FZ_FLAG_ALLOCATED))
  238. valptr = NULL;
  239. /*
  240. * do the lookup
  241. */
  242. lval = ossl_ht_fz_FUZZER_VALUE_get(fuzzer_table, TO_HT_KEY(&key), &v);
  243. /*
  244. * unlock the table
  245. */
  246. ossl_ht_read_unlock(fuzzer_table);
  247. /*
  248. * Now check to make sure we did the right thing
  249. */
  250. OPENSSL_assert(lval == valptr);
  251. /*
  252. * if we expect a positive lookup, make sure that
  253. * we can use the _type and to_value functions
  254. */
  255. if (valptr != NULL) {
  256. OPENSSL_assert(ossl_ht_fz_FUZZER_VALUE_type(v) == 1);
  257. v = ossl_ht_fz_FUZZER_VALUE_to_value(lval, &tv);
  258. OPENSSL_assert(v->value == lval);
  259. }
  260. /*
  261. * successful lookup if we didn't expect a miss
  262. */
  263. if (valptr != NULL)
  264. lookups++;
  265. break;
  266. case OP_FLUSH:
  267. /*
  268. * only flush the table rarely
  269. */
  270. if ((flushes % 100000) != 1) {
  271. skipped_values++;
  272. flushes++;
  273. return 0;
  274. }
  275. /*
  276. * lock the table
  277. */
  278. ossl_ht_write_lock(fuzzer_table);
  279. ossl_ht_flush(fuzzer_table);
  280. ossl_ht_write_unlock(fuzzer_table);
  281. /*
  282. * now check to make sure everything is free
  283. */
  284. for (i = 0; i < USHRT_MAX; i++)
  285. OPENSSL_assert((prediction_table[i].flags & FZ_FLAG_ALLOCATED) == 0);
  286. /* good flush */
  287. flushes++;
  288. break;
  289. case OP_FOREACH:
  290. valfound = 0;
  291. valptr = &prediction_table[keyval];
  292. rc_prediction = 0;
  293. if (valptr->flags & FZ_FLAG_ALLOCATED)
  294. rc_prediction = 1;
  295. ossl_ht_foreach_until(fuzzer_table, table_iterator, &keyval);
  296. OPENSSL_assert(valfound == rc_prediction);
  297. foreaches++;
  298. break;
  299. case OP_FILTER:
  300. valptr = &prediction_table[keyval];
  301. rc_prediction = 0;
  302. if (valptr->flags & FZ_FLAG_ALLOCATED)
  303. rc_prediction = 1;
  304. htvlist = ossl_ht_filter(fuzzer_table, 1, filter_iterator, &keyval);
  305. OPENSSL_assert(htvlist->list_len == (size_t)rc_prediction);
  306. ossl_ht_value_list_free(htvlist);
  307. filters++;
  308. break;
  309. default:
  310. return -1;
  311. }
  312. return 0;
  313. }
  314. void FuzzerCleanup(void)
  315. {
  316. ossl_ht_free(fuzzer_table);
  317. OPENSSL_free(prediction_table);
  318. OPENSSL_cleanup();
  319. }