From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: tarantool-patches@dev.tarantool.org, tsafin@tarantool.org,
alyapunov@tarantool.org
Subject: [Tarantool-patches] [PATCH 03/11] test: avoid usleep() usage for error injections
Date: Fri, 5 Jun 2020 01:43:10 +0200 [thread overview]
Message-ID: <0403d06f3846cd3072f14e51c64d7d8f93bc2d57.1591313754.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1591313754.git.v.shpilevoy@tarantool.org>
Some error injections use usleep() to put the current thread in
sleep. For example, to simulate, that WAL thread is slow.
A few of them allowed to pass custom number of microseconds to
usleep, in form:
usleep(injection->dvalue * 1000000);
Assuming, that dvalue is a number of seconds. But usleep argument
is uint32_t, at least on Mac (it is useconds_t, whose size is 4).
It means, that too big dvalue easily overflows it.
The patch makes it use nanosleep(), in a new wrapper:
thread_sleep(). It takes a double value, and does not truncate it
to 4 bytes.
The overflow was the case for ERRINJ_VY_READ_PAGE_TIMEOUT = 9000
in test/vinyl/errinj_vylog.test.lua. And
ERRINJ_VY_RUN_WRITE_STMT_TIMEOUT = 9000 in
test/vinyl/errinj.test.lua.
Part of #4609
---
src/box/vy_run.c | 2 +-
src/box/vy_scheduler.c | 2 +-
src/lib/core/util.c | 17 +++++++++++++++++
src/trivia/util.h | 7 +++++++
4 files changed, 26 insertions(+), 2 deletions(-)
diff --git a/src/box/vy_run.c b/src/box/vy_run.c
index f8b096f6b..54cf028d0 100644
--- a/src/box/vy_run.c
+++ b/src/box/vy_run.c
@@ -914,7 +914,7 @@ vy_page_read(struct vy_page *page, const struct vy_page_info *page_info,
struct errinj *inj = errinj(ERRINJ_VY_READ_PAGE_TIMEOUT, ERRINJ_DOUBLE);
if (inj != NULL && inj->dparam > 0)
- usleep(inj->dparam * 1000000);
+ thread_sleep(inj->dparam);
ERROR_INJECT_SLEEP(ERRINJ_VY_READ_PAGE_DELAY);
diff --git a/src/box/vy_scheduler.c b/src/box/vy_scheduler.c
index cf58d5f60..a0b7ad006 100644
--- a/src/box/vy_scheduler.c
+++ b/src/box/vy_scheduler.c
@@ -1079,7 +1079,7 @@ vy_task_write_run(struct vy_task *task, bool no_compression)
struct errinj *inj = errinj(ERRINJ_VY_RUN_WRITE_STMT_TIMEOUT,
ERRINJ_DOUBLE);
if (inj != NULL && inj->dparam > 0)
- usleep(inj->dparam * 1000000);
+ thread_sleep(inj->dparam);
rc = vy_run_writer_append_stmt(&writer, entry);
if (rc != 0)
diff --git a/src/lib/core/util.c b/src/lib/core/util.c
index a65bc496c..d7f2344ed 100644
--- a/src/lib/core/util.c
+++ b/src/lib/core/util.c
@@ -407,3 +407,20 @@ double_compare_nint64(double lhs, int64_t rhs, int k)
}
return -k;
}
+
+void
+thread_sleep(double sec)
+{
+ uint64_t ns = (uint64_t)(sec * 1000000000);
+ assert(ns > 0);
+ struct timespec req;
+ struct timespec next;
+ req.tv_sec = ns / 1000000000;
+ req.tv_nsec = ns % 1000000000;
+ assert(req.tv_nsec < 1000000000);
+ int rc;
+ while ((rc = nanosleep(&req, &next)) == -1 && errno == EINTR)
+ req = next;
+ assert(rc == 0);
+ (void)rc;
+}
diff --git a/src/trivia/util.h b/src/trivia/util.h
index 973c9df33..29c7f0194 100644
--- a/src/trivia/util.h
+++ b/src/trivia/util.h
@@ -534,6 +534,13 @@ double_compare_int64(double lhs, int64_t rhs, int k)
return double_compare_nint64(lhs, rhs, k);
}
+/**
+ * Put the current thread in sleep for the given number of
+ * seconds.
+ */
+void
+thread_sleep(double sec);
+
#if !defined(__cplusplus) && !defined(static_assert)
# define static_assert _Static_assert
#endif
--
2.21.1 (Apple Git-122.3)
next prev parent reply other threads:[~2020-06-04 23:43 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-04 23:43 [Tarantool-patches] [PATCH 00/11] Enable miscelaneous sanitations Vladislav Shpilevoy
2020-06-04 23:43 ` [Tarantool-patches] [PATCH 01/11] cmake: enable misc types of UB detection in clang Vladislav Shpilevoy
2020-06-04 23:43 ` [Tarantool-patches] [PATCH 10/11] sql: fix usage of not initialized index_stat Vladislav Shpilevoy
2020-06-04 23:43 ` [Tarantool-patches] [PATCH 11/11] sql: fix mem_apply_type double type truncation Vladislav Shpilevoy
2020-06-04 23:43 ` [Tarantool-patches] [PATCH 02/11] util: introduce double_compare_nint64() Vladislav Shpilevoy
2020-06-04 23:43 ` Vladislav Shpilevoy [this message]
2020-06-04 23:43 ` [Tarantool-patches] [PATCH 04/11] vinyl: fix 0 division in case of canceled dump Vladislav Shpilevoy
2020-06-04 23:43 ` [Tarantool-patches] [PATCH 05/11] xrow: don't cast double to float unconditionally Vladislav Shpilevoy
2020-06-04 23:43 ` [Tarantool-patches] [PATCH 06/11] swim: fix zero division Vladislav Shpilevoy
2020-06-04 23:43 ` [Tarantool-patches] [PATCH 07/11] test: fix signed integer overflow in vclock test Vladislav Shpilevoy
2020-06-04 23:43 ` [Tarantool-patches] [PATCH 08/11] digest: eliminate UBs from guava() Vladislav Shpilevoy
2020-06-04 23:43 ` [Tarantool-patches] [PATCH 09/11] salad: fix UB pointer arithmetics in bps_tree Vladislav Shpilevoy
2020-06-05 22:09 ` [Tarantool-patches] [PATCH 00/11] Enable miscelaneous sanitations Timur Safin
2020-06-09 8:19 ` Cyrill Gorcunov
2020-06-09 8:28 ` Kirill Yukhin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0403d06f3846cd3072f14e51c64d7d8f93bc2d57.1591313754.git.v.shpilevoy@tarantool.org \
--to=v.shpilevoy@tarantool.org \
--cc=alyapunov@tarantool.org \
--cc=tarantool-patches@dev.tarantool.org \
--cc=tsafin@tarantool.org \
--subject='Re: [Tarantool-patches] [PATCH 03/11] test: avoid usleep() usage for error injections' \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox