IMPLEMENT_RANDOMIZE_STACK potential race condition? #1130

issue laanwj opened this issue on April 21, 2012
  1. laanwj commented at 11:10 AM on April 21, 2012: member

    The macro IMPLEMENT_RANDOMIZE_STACK uses a "static char" to store the number of loops. This is shared between threads (it is used in at least 9 different thread functions) and operations such as '--' are not guaranteed to be atomic. If multiple threads end up in this loop at the same time, there could be executions that would result in an infinite loop (or at least, unreasonably deep stacks that exceed the hardcoded limit of 20).

    To fix this we could use thread-local storage here (__thread /__declspec(thread) depending on the compiler...). Or find an alternative for this... slightly crazy macro.

  2. sipa commented at 2:54 PM on April 21, 2012: member

    I remember someone once mentioned that such stack randomization is even optimized away by recent compilers, so I had at a look at the generated code. My assembly-fu is not that good, but from what I understand, ThreadMessageHander for example is compiled to not more than a strangely-ordered loop. A form of tail-recursion optimization, I assume. It's easy to avoid this optimization, by adding a effectful statement after the recursive call in the IMPLEMENT_RANDOMIZE_STACK, but given that we also use -fstack-protector already, maybe we can drop this construct altogether?

  3. laanwj commented at 7:36 AM on April 22, 2012: member

    That's a pretty smart optimizer!

    Hmm if the goal is to displace the stack pointer with an arbitrary amount we could also use alloca() with a random amount.

  4. Diapolo commented at 2:14 PM on April 22, 2012: none

    Is there an option to enable ASLR with GCC, this should do the same right?

  5. laanwj commented at 8:33 AM on September 25, 2012: member

    Closing this, I don't think it's an actual problem as every usage of the macro creates a new static variable which is only used within that thread.

  6. laanwj closed this on Sep 25, 2012

  7. lateminer referenced this in commit dfebf8a7af on Jan 22, 2019
  8. lateminer referenced this in commit c582cabbd3 on Dec 25, 2019
  9. MarcoFalke locked this on Sep 8, 2021
Contributors

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-13 15:16 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me