Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug31614 035 #1302

Open
wants to merge 6 commits into
base: maint-0.3.5
Choose a base branch
from
Open

Bug31614 035 #1302

wants to merge 6 commits into from

Conversation

Labels
None yet
Projects
None yet
3 participants
@teor2345
Copy link
Contributor

@teor2345 teor2345 commented Sep 9, 2019

No description provided.

@coveralls
Copy link

@coveralls coveralls commented Sep 9, 2019

Pull Request Test Coverage Report for Build 6392

  • 3 of 3 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.003%) to 60.309%

Totals Coverage Status
Change from base Build 6284: 0.003%
Covered Lines: 42890
Relevant Lines: 71117

💛 - Coveralls

@@ -190,13 +191,15 @@ dump_stack_symbols_to_error_fds(void)
backtrace_symbols_fd(cb_buf, (int)depth, fds[i]);
}

/* The signals that we want our backtrace handler to trap */
static int trap_signals[] = { SIGSEGV, SIGILL, SIGFPE, SIGBUS, SIGSYS,
SIGIO, -1 };
Copy link
Contributor

@dgoulet-tor dgoulet-tor Sep 10, 2019

This in theory should be indented properly to follow our coding standards. I'm not going to hold up on this though to get it merged but if you have a minute, great! :)

/* We can't use tor_mutex_free() here, because lock uses err.
* This function is called while err is being shut down, so we can't
* log or take any useful action if pthread_mutex_destroy() fails. */
(void)pthread_mutex_destroy(&cb_buf_mutex);
Copy link
Contributor

@dgoulet-tor dgoulet-tor Sep 10, 2019

Is this comment accurate? It seems to me it is saying we can be in this handler while the mutex is locked? I would be a bit surprised but just in case, this can't be done. Man page states:

It shall be safe to destroy an initialized mutex that is unlocked. Attempting to destroy a locked mutex or a mutex that is referenced (for example, while being used in a pthread_cond_timedwait() or pthread_cond_wait()) by another thread results in undefined behavior.

Copy link
Contributor Author

@teor2345 teor2345 Sep 10, 2019

You're right, there are two race conditions here:

  1. log_backtrace_impl() can be called from other threads, and lock cb_buf_mutex, while the main thread is calling this function during shut down, which leads to undefined behaviour

  2. cb_buf is used in a lot of functions, which could be called concurrently:
    a) install_bt_handler
    b) dump_stack_symbols_to_error_fds
    c) crash_handler
    d) log_backtrace_impl (protected by cb_buf_mutex)
    Which could lead to corrupted stack traces.

I don't know if there is an easy way to fix this. Maybe I should just open a ticket.

@nmathewson do you have any ideas?

@teor2345 teor2345 force-pushed the bug31614_035 branch 2 times, most recently from daecd85 to 1ddc988 Sep 20, 2019
teor2345 added 2 commits Sep 24, 2019
cb_buf_mutex is statically initialised, so we can not destroy it when
we are shutting down the err subsystem. If we destroy it, and then
re-initialise tor, all our backtraces will fail.

Part of 31736, but committed in this branch to avoid merge conflicts.
teor2345 added 3 commits Sep 26, 2019
The log mutex is dynamically initialized, guarded by log_mutex_initialized.
We don't want to destroy it, because after it is destroyed, we won't see
any more logs.

If tor is re-initialized, log_mutex_initialized will still be 1. So we
won't trigger any undefined behaviour by trying to re-initialize the
log mutex.

Part of 31736, but committed in this branch to avoid merge conflicts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment