diff --git a/docker/Dockerfile b/docker/Dockerfile index 390dd4907..94b4e8acf 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -32,7 +32,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ openssl \ ldap-utils \ libldap-2.4-2 \ - libldap-dev + libldap-dev \ + libasan6 RUN curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \ sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' @@ -48,6 +49,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ libpq5 \ libpq-dev \ vim \ + python3 \ postgresql-common \ postgresql-server-dev-14 \ git diff --git a/docker/bin/ody-stop b/docker/bin/ody-stop index abbd6b47c..4f051700f 100755 --- a/docker/bin/ody-stop +++ b/docker/bin/ody-stop @@ -1,2 +1,62 @@ -#!/bin/bash -pkill odyssey || (sleep 1 && kill -9 $(pgrep odyssey)) || true +#!/usr/bin/env python3 + +import os +import time +import signal +import subprocess + + +def get_odyssey_pids(): + print(subprocess.check_output('ps aux | grep odyssey', shell=True).decode('utf-8')) + + try: + return list(map(int, subprocess.check_output(['pgrep', 'odyssey']).split())) + except subprocess.CalledProcessError: + # non-zero exit code means that there is no odyssey pids + return [] + +def terminate_gracefully(pid, timeout): + try: + os.kill(pid, signal.SIGTERM) + except ProcessLookupError: + print(f'Process {pid} already finished or doesnt ever existed') + return + + print(f'Waiting {timeout} seconds to {pid} finish after SIGTERM...', end='') + + start = time.time() + finished = False + + while time.time() - start < timeout: + try: + if 'odyssey'.encode('utf-8') not in subprocess.check_output(['ps', '-p', str(pid)], ): + finished = True + break + except subprocess.CalledProcessError: + # non-zero code means there is no process with that pid + finished = True + break + + print('.', end='') + time.sleep(0.5) + + print() + + return finished + + +def main(): + timeout = 5 + + pids = get_odyssey_pids() + print('Found odyssey pids:', ', '.join(list(map(str, pids)))) + + for pid in pids: + if not terminate_gracefully(pid, timeout): + print(f'Process {pid} didnt finish within {timeout} seconds') + exit(1) + + exit(0) + +if __name__ == "__main__": + main() diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 31a4c1ef1..5c58fbc3d 100644 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -104,8 +104,11 @@ then fi echo "" > /var/log/odyssey.log +echo 0 > /proc/sys/kernel/randomize_va_space /usr/bin/odyssey-asan /etc/odyssey/odyssey.conf +sleep 5 ody-stop +echo 2 > /proc/sys/kernel/randomize_va_space # TODO: rewrite #/shell-test/test.sh diff --git a/sources/address.c b/sources/address.c index 30b5ff3eb..c375f66e5 100644 --- a/sources/address.c +++ b/sources/address.c @@ -1,6 +1,7 @@ #include #include #include +#include /* * Odyssey. @@ -158,7 +159,8 @@ static bool od_address_check_hostname(struct sockaddr_storage *client_sa, char client_hostname[NI_MAXHOST]; - ret = getnameinfo(client_sa, sizeof(*client_sa), client_hostname, + ret = getnameinfo((const struct sockaddr *)client_sa, + sizeof(*client_sa), client_hostname, sizeof(client_hostname), NULL, 0, NI_NAMEREQD); if (ret != 0) diff --git a/sources/backend.c b/sources/backend.c index 39e5af7b9..573eba2dd 100644 --- a/sources/backend.c +++ b/sources/backend.c @@ -696,8 +696,8 @@ int od_backend_update_parameter(od_server_t *server, char *context, char *data, return 0; } -int od_backend_ready_wait(od_server_t *server, char *context, int count, - uint32_t time_ms, uint32_t ignore_errors) +int od_backend_ready_wait(od_server_t *server, char *context, uint32_t time_ms, + uint32_t ignore_errors) { od_instance_t *instance = server->global->instance; int query_rc; @@ -781,13 +781,14 @@ od_retcode_t od_backend_query_send(od_server_t *server, char *context, od_retcode_t od_backend_query(od_server_t *server, char *context, char *query, char *param, int len, uint32_t timeout, - uint32_t count, uint32_t ignore_errors) + uint32_t ignore_errors) { if (od_backend_query_send(server, context, query, param, len) == NOT_OK_RESPONSE) { return NOT_OK_RESPONSE; } - od_retcode_t rc = od_backend_ready_wait(server, context, count, timeout, - ignore_errors); + + od_retcode_t rc = + od_backend_ready_wait(server, context, timeout, ignore_errors); return rc; } diff --git a/sources/backend.h b/sources/backend.h index b583a528d..41a80fed1 100644 --- a/sources/backend.h +++ b/sources/backend.h @@ -16,11 +16,11 @@ void od_backend_error(od_server_t *, char *, char *, uint32_t); int od_backend_update_parameter(od_server_t *, char *, char *, uint32_t, int); int od_backend_ready(od_server_t *, char *, uint32_t); -int od_backend_ready_wait(od_server_t *, char *, int, uint32_t, uint32_t); +int od_backend_ready_wait(od_server_t *, char *, uint32_t, uint32_t); od_retcode_t od_backend_query_send(od_server_t *server, char *context, char *query, char *param, int len); od_retcode_t od_backend_query(od_server_t *, char *, char *, char *, int, - uint32_t, uint32_t, uint32_t); + uint32_t, uint32_t); #endif /* ODYSSEY_BACKEND_H */ diff --git a/sources/backend_sync.c b/sources/backend_sync.c index 7ce23907b..998c0053f 100644 --- a/sources/backend_sync.c +++ b/sources/backend_sync.c @@ -28,7 +28,7 @@ int od_backend_request_sync_point(od_server_t *server) /* update server sync state */ od_server_sync_request(server, 1); - return od_backend_ready_wait(server, "sync-point", 1 /*count*/, + return od_backend_ready_wait(server, "sync-point", 1000 /* timeout 1 sec */, 0 /*ignore error?*/); } \ No newline at end of file diff --git a/sources/console.c b/sources/console.c index dba1fc523..ffefa3863 100644 --- a/sources/console.c +++ b/sources/console.c @@ -276,15 +276,10 @@ static int od_console_show_err_router_stats_cb(od_error_logger_t *l, return od_console_show_router_stats_err_add(stream, l); } -static inline int od_console_show_help(od_client_t *client, - machine_msg_t *stream) +static inline int od_console_show_help(machine_msg_t *stream) { assert(stream); - char msg[OD_QRY_MAX_SZ]; - int msg_len; - va_list args; - char *message = "\n" "Console usage\n" @@ -1753,7 +1748,7 @@ static inline int od_console_show(od_client_t *client, machine_msg_t *stream, case OD_LSTATS: return od_console_show_stats(client, stream); case OD_LHELP: - return od_console_show_help(client, stream); + return od_console_show_help(stream); case OD_LPOOLS: return od_console_show_pools(client, stream, false); case OD_LPOOLS_EXTENDED: diff --git a/sources/counter.c b/sources/counter.c index e5e4880fc..af0b29454 100644 --- a/sources/counter.c +++ b/sources/counter.c @@ -36,8 +36,9 @@ od_counter_t *od_counter_create(size_t max_value) counter->size = values_count; - memset(counter->value_to_count, 0, - sizeof(od_atomic_u64_t) * counter->size); + for (size_t i = 0; i < counter->size; ++i) { + od_atomic_u64_set(&counter->value_to_count[i], 0ULL); + } return counter; } diff --git a/sources/frontend.c b/sources/frontend.c index 3969ace57..5209c5b06 100644 --- a/sources/frontend.c +++ b/sources/frontend.c @@ -988,9 +988,9 @@ od_frontend_rewrite_msg(char *data, int size, int opname_start_offset, } static od_frontend_status_t od_frontend_deploy_prepared_stmt( - od_server_t *server, od_relay_t *relay, char *ctx, char *data, - int size /* to adcance or to write? */, od_hash_t body_hash, - char *opname, int opnamelen) + od_server_t *server, __attribute__((unused)) od_relay_t *relay, + char *ctx, char *data, int size /* to adcance or to write? */, + od_hash_t body_hash, char *opname, int opnamelen) { od_route_t *route = server->route; od_instance_t *instance = server->global->instance; @@ -1096,7 +1096,6 @@ static od_frontend_status_t od_frontend_remote_client(od_relay_t *relay, "%s", kiwi_fe_type_to_string(type)); od_frontend_status_t retstatus = OD_OK; - bool forwarded = 0; switch (type) { case KIWI_FE_COPY_DONE: case KIWI_FE_COPY_FAIL: @@ -1186,7 +1185,6 @@ static od_frontend_status_t od_frontend_remote_client(od_relay_t *relay, od_dbg_printf_on_dvl_lvl( 1, "client relay %p advance msg %c\n", relay, *(char *)machine_msg_data(msg)); - forwarded = 1; } break; case KIWI_FE_PARSE: @@ -1340,7 +1338,6 @@ static od_frontend_status_t od_frontend_remote_client(od_relay_t *relay, od_dbg_printf_on_dvl_lvl( 1, "client relay %p advance msg %c\n", relay, *(char *)machine_msg_data(msg)); - forwarded = 1; } break; case KIWI_FE_EXECUTE: @@ -1353,7 +1350,6 @@ static od_frontend_status_t od_frontend_remote_client(od_relay_t *relay, uint32_t name_len; kiwi_fe_close_type_t type; int rc; - forwarded = 1; if (od_frontend_parse_close(data, size, &name, &name_len, diff --git a/sources/hba.c b/sources/hba.c index ba85f37b0..0524be9f9 100644 --- a/sources/hba.c +++ b/sources/hba.c @@ -33,8 +33,18 @@ void od_hba_reload(od_hba_t *hba, od_hba_rules_t *rules) { od_hba_lock(hba); + od_hba_rules_free(&hba->rules); + od_list_init(&hba->rules); - memcpy(&hba->rules, &rules, sizeof(rules)); + + od_list_t *i, *n; + od_list_foreach_safe(rules, i, n) + { + od_hba_rule_t *rule; + rule = od_container_of(i, od_hba_rule_t, link); + + od_hba_rules_add(&hba->rules, rule); + } od_hba_unlock(hba); } diff --git a/sources/hba_reader.c b/sources/hba_reader.c index 329843dae..3ccc64b8f 100644 --- a/sources/hba_reader.c +++ b/sources/hba_reader.c @@ -36,6 +36,7 @@ static od_keyword_t od_hba_keywords[] = { od_keyword("trust", OD_LALLOW), od_keyword("deny", OD_LDENY), od_keyword("reject", OD_LDENY), + { 0, 0, 0 }, }; static void od_hba_reader_error(od_config_reader_t *reader, char *msg) diff --git a/sources/macro.h b/sources/macro.h index a3b025744..eda448add 100644 --- a/sources/macro.h +++ b/sources/macro.h @@ -20,7 +20,7 @@ #define CONCAT_(A, B) A##B #define CONCAT(A, B) CONCAT_(A, B) -static const int64_t interval_usec = 1000000ll; +static const uint64_t interval_usec = 1000000ull; typedef int od_retcode_t; diff --git a/sources/odyssey.h b/sources/odyssey.h index 8e9da95bd..847c0f6bd 100644 --- a/sources/odyssey.h +++ b/sources/odyssey.h @@ -151,6 +151,7 @@ #include "sources/frontend.h" #include "sources/backend.h" +#include "sources/backend_sync.h" #include "sources/mdb_iamproxy.h" #endif /* ODYSSEY_H */ diff --git a/sources/reset.c b/sources/reset.c index bc3203168..35047ef3f 100644 --- a/sources/reset.c +++ b/sources/reset.c @@ -74,7 +74,7 @@ int od_reset(od_server_t *server) "not synchronized, wait for %d msec (#%d)", wait_timeout, wait_try); wait_try++; - rc = od_backend_ready_wait(server, "reset", 1, + rc = od_backend_ready_wait(server, "reset", wait_timeout, 1 /*ignore server errors*/); if (rc == NOT_OK_RESPONSE) @@ -137,7 +137,7 @@ int od_reset(od_server_t *server) char query_rlb[] = "ROLLBACK"; rc = od_backend_query( server, "reset-rollback", query_rlb, NULL, - sizeof(query_rlb), wait_timeout, 1, + sizeof(query_rlb), wait_timeout, 0 /*do not ignore server error messages*/); if (rc == NOT_OK_RESPONSE) goto error; @@ -150,7 +150,7 @@ int od_reset(od_server_t *server) char query_discard[] = "DISCARD ALL"; rc = od_backend_query( server, "reset-discard", query_discard, NULL, - sizeof(query_discard), wait_timeout, 1, + sizeof(query_discard), wait_timeout, 0 /*do not ignore server error messages*/); if (rc == NOT_OK_RESPONSE) goto error; @@ -163,7 +163,7 @@ int od_reset(od_server_t *server) "SET SESSION AUTHORIZATION DEFAULT;RESET ALL;CLOSE ALL;UNLISTEN *;SELECT pg_advisory_unlock_all();DISCARD PLANS;DISCARD SEQUENCES;DISCARD TEMP;"; rc = od_backend_query( server, "reset-discard-smart", query_discard, NULL, - sizeof(query_discard), wait_timeout, 1, + sizeof(query_discard), wait_timeout, 0 /*do not ignore server error messages*/); if (rc == NOT_OK_RESPONSE) goto error; @@ -173,7 +173,7 @@ int od_reset(od_server_t *server) server, "reset-discard-smart-string", route->rule->pool->discard_query, NULL, strlen(route->rule->pool->discard_query) + 1, - wait_timeout, 1, + wait_timeout, 0 /*do not ignore server error messages*/); if (rc == NOT_OK_RESPONSE) goto error; diff --git a/sources/rules.c b/sources/rules.c index 88428c654..66f5f2b54 100644 --- a/sources/rules.c +++ b/sources/rules.c @@ -139,52 +139,11 @@ od_group_t *od_rules_group_allocate(od_global_t *global) return group; } -static inline int od_rule_update_auth(od_route_t *route, void **argv) -{ - od_rule_t *rule = (od_rule_t *)argv[0]; - od_rule_t *group_rule = (od_rule_t *)argv[1]; - - /* auth */ - rule->auth = group_rule->auth; - rule->auth_mode = group_rule->auth_mode; - rule->auth_query = group_rule->auth_query; - rule->auth_query_db = group_rule->auth_query_db; - rule->auth_query_user = group_rule->auth_query_user; - rule->auth_common_name_default = group_rule->auth_common_name_default; - rule->auth_common_names = group_rule->auth_common_names; - rule->auth_common_names_count = group_rule->auth_common_names_count; - -#ifdef PAM_FOUND - rule->auth_pam_service = group_rule->auth_pam_service; - rule->auth_pam_data = group_rule->auth_pam_data; -#endif - -#ifdef LDAP_FOUND - rule->ldap_endpoint_name = group_rule->ldap_endpoint_name; - rule->ldap_endpoint = group_rule->ldap_endpoint; - rule->ldap_pool_timeout = group_rule->ldap_pool_timeout; - rule->ldap_pool_size = group_rule->ldap_pool_size; - rule->ldap_pool_ttl = group_rule->ldap_pool_ttl; - rule->ldap_storage_creds_list = group_rule->ldap_storage_creds_list; - rule->ldap_storage_credentials_attr = - group_rule->ldap_storage_credentials_attr; -#endif - - rule->auth_module = group_rule->auth_module; - - /* password */ - rule->password = group_rule->password; - rule->password_len = group_rule->password_len; - - return 0; -} - void od_rules_group_checker_run(void *arg) { od_group_checker_run_args *args = (od_group_checker_run_args *)arg; od_rule_t *group_rule = args->rule; od_group_t *group = group_rule->group; - od_rules_t *rules = args->rules; od_global_t *global = group->global; od_router_t *router = global->router; od_instance_t *instance = global->instance; @@ -218,6 +177,7 @@ void od_rules_group_checker_run(void *arg) machine_msg_t *msg; char *group_member; int rc; + rc = OK_RESPONSE; /* route */ od_router_status_t status; @@ -345,8 +305,6 @@ void od_rules_group_checker_run(void *arg) break; } - od_router_close(router, group_checker_client); - if (rc == NOT_OK_RESPONSE) { od_debug(&instance->logger, "group_checker", group_checker_client, server, @@ -361,10 +319,11 @@ void od_rules_group_checker_run(void *arg) if (member) free(member); } + + od_router_close(router, group_checker_client); break; } - bool have_default = false; od_list_t *i; int count_group_users = 0; od_list_foreach(&members, i) @@ -373,6 +332,13 @@ void od_rules_group_checker_run(void *arg) } char **usernames = malloc(sizeof(char *) * count_group_users); + if (usernames == NULL) { + od_error(&instance->logger, "group_checker", + group_checker_client, server, + "out of memory"); + od_router_close(router, group_checker_client); + break; + } int j = 0; od_list_foreach(&members, i) { @@ -401,13 +367,10 @@ void od_rules_group_checker_run(void *arg) group_rule->users_in_group = count_group_users; od_router_unlock(router); // Free memory without router lock - for (size_t i = 0; i < t_count; i++) { - if (t_names[i]) { - free(t_names[i]); - } + for (int i = 0; i < t_count; i++) { + free(t_names[i]); } - if (t_names) - free(t_names); + free(t_names); // Free list od_list_t *it, *n; @@ -424,9 +387,12 @@ void od_rules_group_checker_run(void *arg) od_debug(&instance->logger, "group_checker", group_checker_client, server, "group check success"); + od_router_close(router, group_checker_client); break; } + od_router_close(router, group_checker_client); + // retry } @@ -1955,7 +1921,7 @@ bool od_name_in_rule(od_rule_t *rule, char *name) { if (rule->group) { bool matched = strcmp(rule->user_name, name) == 0; - for (size_t i = 0; i < rule->users_in_group; i++) { + for (int i = 0; i < rule->users_in_group; i++) { matched |= (strcmp(rule->user_names[i], name) == 0); } return matched; diff --git a/sources/scram.c b/sources/scram.c index 4683881be..9ce147018 100644 --- a/sources/scram.c +++ b/sources/scram.c @@ -527,7 +527,7 @@ int od_scram_read_client_first_message(od_scram_state_t *scram_state, case 'p': // todo: client requires channel binding if (read_any_attribute_buf(&auth_data, &auth_data_size, NULL, NULL, NULL) == -1) { - goto error_free_client_nonce; + return -1; } auth_data--; @@ -875,6 +875,13 @@ od_scram_create_server_final_message(od_scram_state_t *scram_state) if (result == NULL) goto error; + // There is compiler warning about some wierd case + // when snprintf result is above INT_MAX + // (we dont check snprintf result, see -Wformat-truncation) + if (od_unlikely(size + 1 >= INT_MAX)) { + abort(); + } + snprintf(result, size + 1, "v=%s", signature); free(signature); diff --git a/test/odyssey/test_attribute.c b/test/odyssey/test_attribute.c index c26fd6818..22b953d88 100644 --- a/test/odyssey/test_attribute.c +++ b/test/odyssey/test_attribute.c @@ -1,5 +1,6 @@ #include "odyssey.h" +#include void test_read_attribute_buf_sanity() { @@ -11,15 +12,15 @@ void test_read_attribute_buf_sanity() char *value; size_t value_size; - assert(read_attribute_buf(&ptr, &ptr_size, 'a', &value, &value_size) == - 0); - assert(memcmp(value, "qwerty", value_size) == 0); - assert(read_attribute_buf(&ptr, &ptr_size, 'b', &value, &value_size) == - 0); - assert(memcmp(value, "", value_size) == 0); - assert(read_attribute_buf(&ptr, &ptr_size, 'c', &value, &value_size) == - 0); - assert(memcmp(value, "other", value_size) == 0); + test(read_attribute_buf(&ptr, &ptr_size, 'a', &value, &value_size) == + 0); + test(memcmp(value, "qwerty", value_size) == 0); + test(read_attribute_buf(&ptr, &ptr_size, 'b', &value, &value_size) == + 0); + test(memcmp(value, "", value_size) == 0); + test(read_attribute_buf(&ptr, &ptr_size, 'c', &value, &value_size) == + 0); + test(memcmp(value, "other", value_size) == 0); free(data); } @@ -37,18 +38,18 @@ void test_read_any_attribute_buf_sanity() char *value; size_t value_size; char attribute; - assert(read_any_attribute_buf(&ptr, &ptr_size, &attribute, - &value, &value_size) == 0); - assert(memcmp(value, "qwerty", value_size) == 0); - assert(attribute == 'a'); - assert(read_any_attribute_buf(&ptr, &ptr_size, &attribute, - &value, &value_size) == 0); - assert(memcmp(value, "", value_size) == 0); - assert(attribute == 'b'); - assert(read_any_attribute_buf(&ptr, &ptr_size, &attribute, - &value, &value_size) == 0); - assert(memcmp(value, "other", value_size) == 0); - assert(attribute == 'c'); + test(read_any_attribute_buf(&ptr, &ptr_size, &attribute, &value, + &value_size) == 0); + test(memcmp(value, "qwerty", value_size) == 0); + test(attribute == 'a'); + test(read_any_attribute_buf(&ptr, &ptr_size, &attribute, &value, + &value_size) == 0); + test(memcmp(value, "", value_size) == 0); + test(attribute == 'b'); + test(read_any_attribute_buf(&ptr, &ptr_size, &attribute, &value, + &value_size) == 0); + test(memcmp(value, "other", value_size) == 0); + test(attribute == 'c'); } { @@ -56,15 +57,15 @@ void test_read_any_attribute_buf_sanity() size_t ptr_size = data_size; char *value; size_t value_size; - assert(read_any_attribute_buf(&ptr, &ptr_size, NULL, &value, - &value_size) == 0); - assert(memcmp(value, "qwerty", value_size) == 0); - assert(read_any_attribute_buf(&ptr, &ptr_size, NULL, &value, - &value_size) == 0); - assert(memcmp(value, "", value_size) == 0); - assert(read_any_attribute_buf(&ptr, &ptr_size, NULL, &value, - &value_size) == 0); - assert(memcmp(value, "other", value_size) == 0); + test(read_any_attribute_buf(&ptr, &ptr_size, NULL, &value, + &value_size) == 0); + test(memcmp(value, "qwerty", value_size) == 0); + test(read_any_attribute_buf(&ptr, &ptr_size, NULL, &value, + &value_size) == 0); + test(memcmp(value, "", value_size) == 0); + test(read_any_attribute_buf(&ptr, &ptr_size, NULL, &value, + &value_size) == 0); + test(memcmp(value, "other", value_size) == 0); } free(data); diff --git a/test/odyssey/test_locks.c b/test/odyssey/test_locks.c index ade2fcf6a..b78f33034 100644 --- a/test/odyssey/test_locks.c +++ b/test/odyssey/test_locks.c @@ -1,9 +1,10 @@ #include "odyssey.h" +#include void test_odyssey_locks_has_noneq_hash() { - assert(ODYSSEY_CTRL_LOCK_HASH == 1337); - assert(ODYSSEY_CTRL_LOCK_HASH != ODYSSEY_EXEC_LOCK_HASH); + test(ODYSSEY_CTRL_LOCK_HASH == 1337); + test(ODYSSEY_CTRL_LOCK_HASH != ODYSSEY_EXEC_LOCK_HASH); } void odyssey_test_lock(void) diff --git a/test/odyssey/test_tdigest.c b/test/odyssey/test_tdigest.c index aa4b4f514..0ef5be279 100644 --- a/test/odyssey/test_tdigest.c +++ b/test/odyssey/test_tdigest.c @@ -2,21 +2,22 @@ #include "lrand48.h" #include #include "odyssey.h" +#include void simple_test() { td_histogram_t *histogram = td_new(100); td_add(histogram, 1, 1); td_add(histogram, 2, 1); - assert(td_value_at(histogram, 0) == 1); - assert(td_value_at(histogram, .5) == 1.5); - assert(td_value_at(histogram, 1) == 2); + test(td_value_at(histogram, 0) == 1); + test(td_value_at(histogram, .5) == 1.5); + test(td_value_at(histogram, 1) == 2); td_histogram_t *h2 = td_new(100); td_add(h2, 0, 1); td_add(h2, 3, 1); td_merge(histogram, h2); - assert(td_value_at(histogram, 0) == 0); - assert(td_value_at(histogram, .5) == 1.5); + test(td_value_at(histogram, 0) == 0); + test(td_value_at(histogram, .5) == 1.5); assert(td_value_at(histogram, 1) == 3); td_free(h2); td_free(histogram); @@ -33,11 +34,11 @@ void monotonicity_test() double last_x = -1; for (double i = 0; i <= 1; i += 1e-5) { double current_x = td_value_at(histogram, i); - assert(current_x >= last_x); + test(current_x >= last_x); last_x = current_x; double current_quantile = td_quantile_of(histogram, i); - assert(current_quantile >= last_quantile); + test(current_quantile >= last_quantile); } td_safe_free(histogram); @@ -55,8 +56,8 @@ void extreme_quantiles_test() for (size_t i = 0; i < 3; ++i) { double quantile = quantiles[i]; size_t index = floor(quantile * size); - assert(fabs(td_value_at(histogram, quantile) - - expected[index]) < 0.01); + test(fabs(td_value_at(histogram, quantile) - expected[index]) < + 0.01); } td_safe_free(histogram); } @@ -78,13 +79,13 @@ void three_point_test() double p95 = td_value_at(histogram, 0.95); double p99 = td_value_at(histogram, 0.99); - assert(p10 <= p50); - assert(p50 <= p90); - assert(p90 <= p95); - assert(p95 <= p99); + test(p10 <= p50); + test(p50 <= p90); + test(p90 <= p95); + test(p95 <= p99); - assert(x0 == p10); - assert(x2 == p99); + test(x0 == p10); + test(x2 == p99); td_safe_free(histogram); } @@ -107,8 +108,8 @@ void merge_several_digests_test() double quantiles[3] = { 0.5, 0.9, 0.99 }; for (size_t i = 0; i < 3; ++i) { - assert(fabs(td_value_at(common_hist, quantiles[i]) - - td_value_at(hists[0], quantiles[i])) < 1e-6); + test(fabs(td_value_at(common_hist, quantiles[i]) - + td_value_at(hists[0], quantiles[i])) < 1e-6); } for (size_t i = 0; i < 5; ++i) @@ -135,7 +136,7 @@ void machinarium_test_tdigest(void) fails += tdigest_random_test(); // fails approx 1/1000, so suppress flaps to impossible - assert(fails <= 3); + test(fails <= 3); machinarium_free(); } @@ -175,9 +176,9 @@ void tdigest_backward_test() } td_copy(freeze, histogram); - assert(fabs(td_value_at(freeze, 0.7) - 70) < 1); - assert(fabs(td_value_at(freeze, 0.5) - 50) < 1); - assert(fabs(td_value_at(freeze, 0.3) - 30) < 1); + test(fabs(td_value_at(freeze, 0.7) - 70) < 1); + test(fabs(td_value_at(freeze, 0.5) - 50) < 1); + test(fabs(td_value_at(freeze, 0.3) - 30) < 1); td_free(histogram); td_free(freeze); } @@ -191,9 +192,9 @@ void tdigest_forward_test() td_add(histogram, i, 1); } td_copy(freeze, histogram); - assert(fabs(td_value_at(freeze, 0.7) - 70) < 1); - assert(fabs(td_value_at(freeze, 0.5) - 50) < 1); - assert(fabs(td_value_at(freeze, 0.3) - 30) < 1); + test(fabs(td_value_at(freeze, 0.7) - 70) < 1); + test(fabs(td_value_at(freeze, 0.5) - 50) < 1); + test(fabs(td_value_at(freeze, 0.3) - 30) < 1); td_free(histogram); td_free(freeze); } diff --git a/test/odyssey/test_util.c b/test/odyssey/test_util.c index d388cc0ae..0e6f7e335 100644 --- a/test/odyssey/test_util.c +++ b/test/odyssey/test_util.c @@ -1,5 +1,6 @@ #include "odyssey.h" +#include void test_od_memtol_sanity() { @@ -12,24 +13,24 @@ void test_od_memtol_sanity() long value; value = od_memtol(ptr, data_size, &ptr, 10); - assert(value == 42); - assert(memcmp(data, " \t 42", ptr - data) == 0); + test(value == 42); + test(memcmp(data, " \t 42", ptr - data) == 0); value = od_memtol(ptr, data_size - (ptr - data), &ptr, 10); - assert(value == 12); - assert(memcmp(data, " \t 42 +12", ptr - data) == 0); + test(value == 12); + test(memcmp(data, " \t 42 +12", ptr - data) == 0); value = od_memtol(ptr, data_size - (ptr - data), &ptr, 10); - assert(value == -17); - assert(memcmp(data, " \t 42 +12\t-17", ptr - data) == 0); + test(value == -17); + test(memcmp(data, " \t 42 +12\t-17", ptr - data) == 0); value = od_memtol(ptr, data_size - (ptr - data), &ptr, 10); - assert(value == 0); - assert(memcmp(data, " \t 42 +12\t-17 -0", ptr - data) == 0); + test(value == 0); + test(memcmp(data, " \t 42 +12\t-17 -0", ptr - data) == 0); value = od_memtol(ptr, data_size - (ptr - data), &ptr, 10); - assert(value == 0); - assert(memcmp(data, " \t 42 +12\t-17 -0 +0", ptr - data) == 0); + test(value == 0); + test(memcmp(data, " \t 42 +12\t-17 -0 +0", ptr - data) == 0); free(data); } diff --git a/third_party/kiwi/kiwi.h b/third_party/kiwi/kiwi.h index 94ff510b2..e4411a2c8 100644 --- a/third_party/kiwi/kiwi.h +++ b/third_party/kiwi/kiwi.h @@ -7,6 +7,10 @@ * postgreSQL protocol interaction library. */ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + #include #include #include diff --git a/third_party/machinarium/sources/clock.c b/third_party/machinarium/sources/clock.c index 4e10cb9a2..feb170392 100644 --- a/third_party/machinarium/sources/clock.c +++ b/third_party/machinarium/sources/clock.c @@ -61,6 +61,8 @@ static int mm_clock_get_insert_position(mm_timer_t **list, int count, return low; } +#ifndef NDEBUG +// This function only used in asserts static int mm_clock_list_is_sorted(mm_timer_t **list, int count) { for (int i = 1; i < count; i++) { @@ -70,6 +72,7 @@ static int mm_clock_list_is_sorted(mm_timer_t **list, int count) } return 1; } +#endif int mm_clock_timer_add(mm_clock_t *clock, mm_timer_t *timer) { diff --git a/third_party/machinarium/sources/context.c b/third_party/machinarium/sources/context.c index 09686fbfc..ce9473ec8 100644 --- a/third_party/machinarium/sources/context.c +++ b/third_party/machinarium/sources/context.c @@ -29,13 +29,19 @@ static void mm_context_runner(void) abort(); } +static inline void write_func_ptr(void **dst, void (*func)(void)) +{ + memcpy(dst, &func, sizeof(func)); +} + static inline void **mm_context_prepare(mm_contextstack_t *stack) { void **sp; sp = (void **)(stack->pointer + stack->size); #if __amd64 *--sp = NULL; - *--sp = (void *)mm_context_runner; + // eq to *--sp = (void *)mm_context_runner + write_func_ptr(--sp, mm_context_runner); /* for x86_64 we need to place return address on stack */ sp -= 6; memset(sp, 0, sizeof(void *) * 6); diff --git a/third_party/machinarium/sources/machinarium_private.h b/third_party/machinarium/sources/machinarium_private.h index 040765e05..8820f7e5f 100644 --- a/third_party/machinarium/sources/machinarium_private.h +++ b/third_party/machinarium/sources/machinarium_private.h @@ -7,7 +7,9 @@ * cooperative multitasking engine. */ -#define _GNU_SOURCE 1 +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif #ifndef IOV_MAX #define IOV_MAX __IOV_MAX diff --git a/third_party/machinarium/sources/thread.c b/third_party/machinarium/sources/thread.c index cb21def37..4cef89beb 100644 --- a/third_party/machinarium/sources/thread.c +++ b/third_party/machinarium/sources/thread.c @@ -5,6 +5,11 @@ * cooperative multitasking engine. */ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include + #include #include