| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <assert.h> |
| 15 |
#include <inttypes.h> |
| 16 |
#include <sched.h> /* IWYU pragma: keep */ |
| 17 |
#include <setjmp.h> |
| 18 |
#include <stdarg.h> |
| 19 |
#include <stdbool.h> |
| 20 |
#include <stdlib.h> |
| 21 |
|
| 22 |
#define UNIT_TESTING |
| 23 |
#include <cmocka.h> |
| 24 |
|
| 25 |
#include <isc/assertions.h> |
| 26 |
#include <isc/log.h> |
| 27 |
#include <isc/loop.h> |
| 28 |
#include <isc/magic.h> |
| 29 |
#include <isc/mem.h> |
| 30 |
#include <isc/random.h> |
| 31 |
#include <isc/refcount.h> |
| 32 |
#include <isc/rwlock.h> |
| 33 |
#include <isc/urcu.h> |
| 34 |
#include <isc/util.h> |
| 35 |
|
| 36 |
#include <dns/log.h> |
| 37 |
#include <dns/qp.h> |
| 38 |
#include <dns/types.h> |
| 39 |
|
| 40 |
#include "qp_p.h" |
| 41 |
|
| 42 |
#include <tests/isc.h> |
| 43 |
#include <tests/qp.h> |
| 44 |
|
| 45 |
#define VERBOSE 0 |
| 46 |
#define ITEM_COUNT 12345 |
| 47 |
#define TRANSACTION_SIZE 123 |
| 48 |
#define TRANSACTION_COUNT 1234 |
| 49 |
|
| 50 |
#if VERBOSE |
| 51 |
#define TRACE(fmt, ...) \ |
| 52 |
isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_QP, \ |
| 53 |
ISC_LOG_DEBUG(7), "%s:%d:%s(): " fmt, __FILE__, \ |
| 54 |
__LINE__, __func__, ##__VA_ARGS__) |
| 55 |
#else |
| 56 |
#define TRACE(...) |
| 57 |
#endif |
| 58 |
|
| 59 |
#if VERBOSE |
| 60 |
#define ASSERT(p) \ |
| 61 |
if (!(p)) { \ |
| 62 |
TRACE("%s failed", #p); \ |
| 63 |
ok = false; \ |
| 64 |
} else |
| 65 |
#else |
| 66 |
#define ASSERT(p) assert_true(p) |
| 67 |
#endif |
| 68 |
|
| 69 |
static void |
| 70 |
setup_logging(void) { |
| 71 |
isc_result_t result; |
| 72 |
isc_logdestination_t destination; |
| 73 |
isc_logconfig_t *logconfig = NULL; |
| 74 |
|
| 75 |
isc_log_create(mctx, &lctx, &logconfig); |
| 76 |
isc_log_setcontext(lctx); |
| 77 |
dns_log_init(lctx); |
| 78 |
dns_log_setcontext(lctx); |
| 79 |
|
| 80 |
destination.file.stream = stderr; |
| 81 |
destination.file.name = NULL; |
| 82 |
destination.file.versions = ISC_LOG_ROLLNEVER; |
| 83 |
destination.file.maximum_size = 0; |
| 84 |
isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC, |
| 85 |
ISC_LOG_DYNAMIC, &destination, |
| 86 |
ISC_LOG_PRINTPREFIX | ISC_LOG_PRINTTIME | |
| 87 |
ISC_LOG_ISO8601); |
| 88 |
|
| 89 |
#if VERBOSE |
| 90 |
isc_log_setdebuglevel(lctx, 7); |
| 91 |
#endif |
| 92 |
|
| 93 |
result = isc_log_usechannel(logconfig, "stderr", |
| 94 |
ISC_LOGCATEGORY_DEFAULT, NULL); |
| 95 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 96 |
} |
| 97 |
|
| 98 |
static struct { |
| 99 |
uint32_t refcount; |
| 100 |
bool in_ro; |
| 101 |
bool in_rw; |
| 102 |
uint8_t len; |
| 103 |
dns_qpkey_t key; |
| 104 |
dns_qpkey_t ascii; |
| 105 |
} item[ITEM_COUNT]; |
| 106 |
|
| 107 |
static void |
| 108 |
item_attach(void *ctx, void *pval, uint32_t ival) { |
| 109 |
INSIST(ctx == NULL); |
| 110 |
INSIST(pval == &item[ival]); |
| 111 |
item[ival].refcount++; |
| 112 |
} |
| 113 |
|
| 114 |
static void |
| 115 |
item_detach(void *ctx, void *pval, uint32_t ival) { |
| 116 |
assert_null(ctx); |
| 117 |
assert_ptr_equal(pval, &item[ival]); |
| 118 |
assert_int_not_equal(item[ival].refcount, 0); |
| 119 |
item[ival].refcount--; |
| 120 |
} |
| 121 |
|
| 122 |
static size_t |
| 123 |
item_makekey(dns_qpkey_t key, void *ctx, void *pval, uint32_t ival) { |
| 124 |
INSIST(ctx == NULL); |
| 125 |
uintptr_t ip = (uintptr_t)pval; |
| 126 |
uintptr_t lo = (uintptr_t)item; |
| 127 |
uintptr_t hi = sizeof(item) + lo; |
| 128 |
if (!(ival < ARRAY_SIZE(item) && lo <= ip && ip < hi && |
| 129 |
pval == &item[ival])) |
| 130 |
{ |
| 131 |
ISC_INSIST(ival < ARRAY_SIZE(item)); |
| 132 |
ISC_INSIST(pval != NULL); |
| 133 |
ISC_INSIST(ip >= lo); |
| 134 |
ISC_INSIST(ip < hi); |
| 135 |
ISC_INSIST(pval == &item[ival]); |
| 136 |
} |
| 137 |
memmove(key, item[ival].key, item[ival].len); |
| 138 |
return item[ival].len; |
| 139 |
} |
| 140 |
|
| 141 |
static void |
| 142 |
testname(void *ctx, char *buf, size_t size) { |
| 143 |
REQUIRE(ctx == NULL); |
| 144 |
strlcpy(buf, "test", size); |
| 145 |
} |
| 146 |
|
| 147 |
const dns_qpmethods_t test_methods = { |
| 148 |
item_attach, |
| 149 |
item_detach, |
| 150 |
item_makekey, |
| 151 |
testname, |
| 152 |
}; |
| 153 |
|
| 154 |
static uint8_t |
| 155 |
random_byte(void) { |
| 156 |
return isc_random_uniform(SHIFT_OFFSET - SHIFT_NOBYTE) + SHIFT_NOBYTE; |
| 157 |
} |
| 158 |
|
| 159 |
static void |
| 160 |
setup_items(void) { |
| 161 |
void *pval = NULL; |
| 162 |
dns_qp_t *qp = NULL; |
| 163 |
dns_qp_create(mctx, &test_methods, NULL, &qp); |
| 164 |
for (size_t i = 0; i < ARRAY_SIZE(item); i++) { |
| 165 |
do { |
| 166 |
size_t len = isc_random_uniform(16) + 4; |
| 167 |
item[i].len = len; |
| 168 |
for (size_t off = 0; off < len; off++) { |
| 169 |
item[i].key[off] = random_byte(); |
| 170 |
} |
| 171 |
memmove(item[i].ascii, item[i].key, len); |
| 172 |
qp_test_keytoascii(item[i].ascii, len); |
| 173 |
} while (dns_qp_getkey(qp, item[i].key, item[i].len, &pval, |
| 174 |
NULL) == ISC_R_SUCCESS); |
| 175 |
assert_int_equal(dns_qp_insert(qp, &item[i], i), ISC_R_SUCCESS); |
| 176 |
} |
| 177 |
dns_qp_destroy(&qp); |
| 178 |
} |
| 179 |
|
| 180 |
static bool |
| 181 |
checkkey(dns_qpreadable_t qpr, size_t i, bool exists, const char *rubric) { |
| 182 |
bool ok = true; |
| 183 |
void *pval = NULL; |
| 184 |
uint32_t ival = ~0U; |
| 185 |
isc_result_t result; |
| 186 |
result = dns_qp_getkey(qpr, item[i].key, item[i].len, &pval, &ival); |
| 187 |
if (result == ISC_R_SUCCESS) { |
| 188 |
assert_true(exists); |
| 189 |
assert_ptr_equal(pval, &item[i]); |
| 190 |
assert_int_equal(ival, i); |
| 191 |
} else if (result == ISC_R_NOTFOUND) { |
| 192 |
assert_false(exists); |
| 193 |
assert_null(pval); |
| 194 |
assert_int_equal(ival, ~0U); |
| 195 |
} else { |
| 196 |
UNREACHABLE(); |
| 197 |
} |
| 198 |
if (!ok) { |
| 199 |
TRACE("checkkey %p %zu %s %s %s", qpr.qpr, i, |
| 200 |
exists ? "exists" : "missing", isc_result_totext(result), |
| 201 |
rubric); |
| 202 |
UNUSED(rubric); |
| 203 |
} |
| 204 |
return ok; |
| 205 |
} |
| 206 |
|
| 207 |
static bool |
| 208 |
checkallro(dns_qpreadable_t qp) { |
| 209 |
bool ok = true; |
| 210 |
for (size_t i = 0; i < ARRAY_SIZE(item); i++) { |
| 211 |
ASSERT(checkkey(qp, i, item[i].in_ro, "checkall ro")); |
| 212 |
} |
| 213 |
if (!ok) { |
| 214 |
qp_test_dumptrie(qp); |
| 215 |
TRACE("checkallro failed"); |
| 216 |
} |
| 217 |
return ok; |
| 218 |
} |
| 219 |
|
| 220 |
static bool |
| 221 |
checkallrw(dns_qpreadable_t qp) { |
| 222 |
bool ok = true; |
| 223 |
for (size_t i = 0; i < ARRAY_SIZE(item); i++) { |
| 224 |
ASSERT(checkkey(qp, i, item[i].in_rw, "checkall rw")); |
| 225 |
} |
| 226 |
if (!ok) { |
| 227 |
qp_test_dumptrie(qp); |
| 228 |
TRACE("checkallrw failed"); |
| 229 |
} |
| 230 |
return ok; |
| 231 |
} |
| 232 |
|
| 233 |
static void |
| 234 |
one_transaction(dns_qpmulti_t *qpm) { |
| 235 |
isc_result_t result; |
| 236 |
bool ok = true; |
| 237 |
|
| 238 |
dns_qpreader_t *qpo = NULL; |
| 239 |
dns_qpsnap_t *qps = NULL; |
| 240 |
dns_qpread_t qpr = { 0 }; |
| 241 |
dns_qp_t *qpw = NULL; |
| 242 |
|
| 243 |
bool snap = isc_random_uniform(2) == 0; |
| 244 |
bool update = isc_random_uniform(2) != 0; |
| 245 |
bool rollback = update && isc_random_uniform(4) == 0; |
| 246 |
size_t count = isc_random_uniform(TRANSACTION_SIZE); |
| 247 |
|
| 248 |
TRACE("transaction %s %s %s size %zu", snap ? "snapshot" : "query", |
| 249 |
update ? "update" : "write", rollback ? "rollback" : "commit", |
| 250 |
count); |
| 251 |
|
| 252 |
/* |
| 253 |
* We need to take care to avoid lock order inversion: |
| 254 |
* The write mutex must be the outermost lock if it is held. |
| 255 |
* The mutex must not be taken while the rwlock is held. |
| 256 |
*/ |
| 257 |
|
| 258 |
/* briefly take and drop mutex */ |
| 259 |
if (snap) { |
| 260 |
dns_qpmulti_snapshot(qpm, &qps); |
| 261 |
qpo = (dns_qpreader_t *)qps; |
| 262 |
} |
| 263 |
|
| 264 |
/* take mutex */ |
| 265 |
if (update) { |
| 266 |
dns_qpmulti_update(qpm, &qpw); |
| 267 |
} else { |
| 268 |
dns_qpmulti_write(qpm, &qpw); |
| 269 |
} |
| 270 |
|
| 271 |
if (!snap) { |
| 272 |
dns_qpmulti_query(qpm, &qpr); |
| 273 |
qpo = (dns_qpreader_t *)&qpr; |
| 274 |
} |
| 275 |
|
| 276 |
for (size_t n = 0; n < count; n++) { |
| 277 |
size_t i = isc_random_uniform(ARRAY_SIZE(item)); |
| 278 |
|
| 279 |
ASSERT(checkkey(qpo, i, item[i].in_ro, "before ro")); |
| 280 |
ASSERT(checkkey(qpw, i, item[i].in_rw, "before rw")); |
| 281 |
|
| 282 |
if (item[i].in_rw) { |
| 283 |
/* TRACE("delete %zu %.*s", i, |
| 284 |
item[i].len, item[i].ascii); */ |
| 285 |
void *pvald = NULL; |
| 286 |
uint32_t ivald = 0; |
| 287 |
result = dns_qp_deletekey(qpw, item[i].key, item[i].len, |
| 288 |
&pvald, &ivald); |
| 289 |
ASSERT(result == ISC_R_SUCCESS); |
| 290 |
ASSERT(pvald == &item[i]); |
| 291 |
ASSERT(ivald == i); |
| 292 |
item[i].in_rw = false; |
| 293 |
} else { |
| 294 |
/* TRACE("insert %zu %.*s", i, |
| 295 |
item[i].len, item[i].ascii); */ |
| 296 |
result = dns_qp_insert(qpw, &item[i], i); |
| 297 |
ASSERT(result == ISC_R_SUCCESS); |
| 298 |
item[i].in_rw = true; |
| 299 |
} |
| 300 |
|
| 301 |
ASSERT(checkkey(qpo, i, item[i].in_ro, "after ro")); |
| 302 |
ASSERT(checkkey(qpw, i, item[i].in_rw, "after rw")); |
| 303 |
|
| 304 |
if (!ok) { |
| 305 |
TRACE("mutate %zu/%zu failed", n, count); |
| 306 |
qp_test_dumptrie(qpo); |
| 307 |
qp_test_dumptrie(qpw); |
| 308 |
} |
| 309 |
assert_true(ok); |
| 310 |
} |
| 311 |
|
| 312 |
assert_true(checkallro(qpo)); |
| 313 |
assert_true(checkallrw(qpw)); |
| 314 |
|
| 315 |
if (!snap) { |
| 316 |
dns_qpread_destroy(qpm, &qpr); |
| 317 |
} |
| 318 |
|
| 319 |
if (rollback) { |
| 320 |
TRACE("transaction rollback"); |
| 321 |
dns_qpmulti_rollback(qpm, &qpw); |
| 322 |
/* mutex is now dropped */ |
| 323 |
dns_qpmulti_query(qpm, &qpr); |
| 324 |
for (size_t i = 0; i < ARRAY_SIZE(item); i++) { |
| 325 |
if (snap) { |
| 326 |
ASSERT(checkkey(qps, i, item[i].in_ro, |
| 327 |
"rollback ro")); |
| 328 |
} |
| 329 |
item[i].in_rw = item[i].in_ro; |
| 330 |
ASSERT(checkkey(&qpr, i, item[i].in_rw, "rollback rw")); |
| 331 |
} |
| 332 |
dns_qpread_destroy(qpm, &qpr); |
| 333 |
} else { |
| 334 |
TRACE("transaction commit"); |
| 335 |
dns_qpmulti_commit(qpm, &qpw); |
| 336 |
/* mutex is now dropped */ |
| 337 |
dns_qpmulti_query(qpm, &qpr); |
| 338 |
for (size_t i = 0; i < ARRAY_SIZE(item); i++) { |
| 339 |
if (snap) { |
| 340 |
ASSERT(checkkey(qps, i, item[i].in_ro, |
| 341 |
"commit ro")); |
| 342 |
} |
| 343 |
item[i].in_ro = item[i].in_rw; |
| 344 |
ASSERT(checkkey(&qpr, i, item[i].in_rw, "commit rw")); |
| 345 |
} |
| 346 |
dns_qpread_destroy(qpm, &qpr); |
| 347 |
} |
| 348 |
|
| 349 |
if (snap) { |
| 350 |
TRACE("snapshot destroy"); |
| 351 |
/* takes mutex briefly */ |
| 352 |
dns_qpsnap_destroy(qpm, &qps); |
| 353 |
} |
| 354 |
|
| 355 |
TRACE("completed %s %s %s size %zu", snap ? "snapshot" : "query", |
| 356 |
update ? "update" : "write", rollback ? "rollback" : "commit", |
| 357 |
count); |
| 358 |
|
| 359 |
if (!ok) { |
| 360 |
TRACE("transaction failed"); |
| 361 |
dns_qpmulti_query(qpm, &qpr); |
| 362 |
qp_test_dumptrie(&qpr); |
| 363 |
dns_qpread_destroy(qpm, &qpr); |
| 364 |
} |
| 365 |
assert_true(ok); |
| 366 |
} |
| 367 |
|
| 368 |
static void |
| 369 |
many_transactions(void *arg) { |
| 370 |
UNUSED(arg); |
| 371 |
|
| 372 |
dns_qpmulti_t *qpm = NULL; |
| 373 |
dns_qpmulti_create(mctx, &test_methods, NULL, &qpm); |
| 374 |
qpm->writer.write_protect = true; |
| 375 |
|
| 376 |
for (size_t n = 0; n < TRANSACTION_COUNT; n++) { |
| 377 |
TRACE("transaction %zu", n); |
| 378 |
one_transaction(qpm); |
| 379 |
rcu_quiescent_state(); |
| 380 |
} |
| 381 |
|
| 382 |
dns_qpmulti_destroy(&qpm); |
| 383 |
isc_loopmgr_shutdown(loopmgr); |
| 384 |
} |
| 385 |
|
| 386 |
ISC_RUN_TEST_IMPL(qpmulti) { |
| 387 |
setup_loopmgr(NULL); |
| 388 |
setup_logging(); |
| 389 |
setup_items(); |
| 390 |
isc_loop_setup(isc_loop_main(loopmgr), many_transactions, NULL); |
| 391 |
isc_loopmgr_run(loopmgr); |
| 392 |
rcu_barrier(); |
| 393 |
isc_loopmgr_destroy(&loopmgr); |
| 394 |
isc_log_destroy(&dns_lctx); |
| 395 |
} |
| 396 |
|
| 397 |
ISC_TEST_LIST_START |
| 398 |
ISC_TEST_ENTRY(qpmulti) |
| 399 |
ISC_TEST_LIST_END |
| 400 |
|
| 401 |
ISC_TEST_MAIN |