From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org> To: tarantool-patches@dev.tarantool.org, gorcunov@gmail.com, sergepetrenko@tarantool.org Subject: [Tarantool-patches] [PATCH v2 3/4] raft: check box_raft is inited before usage Date: Tue, 10 Nov 2020 00:58:53 +0100 [thread overview] Message-ID: <657afb770bc7d6e300758dc47e403999ceb9691b.1604966200.git.v.shpilevoy@tarantool.org> (raw) In-Reply-To: <cover.1604966200.git.v.shpilevoy@tarantool.org> Since box_raft is now initialized at runtime and is used from several subsystems (memtx for snapshots; applier for accepting rows; box.info for monitoring), it may be easy to screw the intialization order and accidentally use the not initialized global raft object. This patch adds a sanity check ensuring it does not happen. The raft state is set to 0 at program start. Then any access to the global raft object firstly checks the state not being 0. The initialization order will get trickier when raft will stop using globals from replication and from box, and will be used from them more extensively. Part of #5303 --- src/box/raft.c | 12 +++++++++++- src/box/raft.h | 6 ++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/box/raft.c b/src/box/raft.c index ef93d3d99..0abeb5f5d 100644 --- a/src/box/raft.c +++ b/src/box/raft.c @@ -44,7 +44,13 @@ */ #define RAFT_RANDOM_ELECTION_FACTOR 0.1 -struct raft box_raft_global; +struct raft box_raft_global = { + /* + * Set an invalid state to validate in runtime the global raft node is + * not used before initialization. + */ + .state = 0, +}; /** * When decoding we should never trust that there is @@ -1114,4 +1120,8 @@ box_raft_free(void) */ box_raft_global.worker = NULL; raft_destroy(&box_raft_global); + /* + * Invalidate so as box_raft() would fail if any usage attempt happens. + */ + box_raft_global.state = 0; } diff --git a/src/box/raft.h b/src/box/raft.h index 18ba0f7dc..83a20f670 100644 --- a/src/box/raft.h +++ b/src/box/raft.h @@ -31,6 +31,7 @@ */ #include <stdint.h> #include <stdbool.h> +#include <assert.h> #include "tarantool_ev.h" #include "trigger.h" @@ -275,6 +276,11 @@ static inline struct raft * box_raft(void) { extern struct raft box_raft_global; + /** + * Ensure the raft node can be used. I.e. that it is properly + * initialized. Entirely for debug purposes. + */ + assert(box_raft_global.state != 0); return &box_raft_global; } -- 2.21.1 (Apple Git-122.3)
next prev parent reply other threads:[~2020-11-09 23:58 UTC|newest] Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-11-09 23:58 [Tarantool-patches] [PATCH v2 0/4] Raft module, part 1 - explicit argument Vladislav Shpilevoy 2020-11-09 23:58 ` [Tarantool-patches] [PATCH v2 1/4] fiber: introduce fiber.f_arg Vladislav Shpilevoy 2020-11-10 7:15 ` Cyrill Gorcunov 2020-11-09 23:58 ` [Tarantool-patches] [PATCH v2 2/4] raft: add explicit raft argument to all functions Vladislav Shpilevoy 2020-11-10 7:30 ` Cyrill Gorcunov 2020-11-10 22:05 ` Vladislav Shpilevoy 2020-11-09 23:58 ` Vladislav Shpilevoy [this message] 2020-11-10 7:30 ` [Tarantool-patches] [PATCH v2 3/4] raft: check box_raft is inited before usage Cyrill Gorcunov 2020-11-09 23:58 ` [Tarantool-patches] [PATCH v2 4/4] vclock: move to src/lib Vladislav Shpilevoy 2020-11-10 7:31 ` Cyrill Gorcunov 2020-11-10 8:07 ` [Tarantool-patches] [PATCH v2 0/4] Raft module, part 1 - explicit argument Serge Petrenko 2020-11-10 22:05 ` Vladislav Shpilevoy
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=657afb770bc7d6e300758dc47e403999ceb9691b.1604966200.git.v.shpilevoy@tarantool.org \ --to=v.shpilevoy@tarantool.org \ --cc=gorcunov@gmail.com \ --cc=sergepetrenko@tarantool.org \ --cc=tarantool-patches@dev.tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH v2 3/4] raft: check box_raft is inited before usage' \ /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