Ports/gdb: Use mmap instead of malloc for sigaltstack()

Stack regions can't be made volatile, which makes it impossible for
malloc to manage memory that's used for `sigaltstack()`. Let's use mmap
instead.

Co-authored-by: Idan Horowitz <idan.horowitz@gmail.com>
This commit is contained in:
Daniel Bertalan 2021-12-16 19:20:45 +01:00 committed by Brian Gianforcaro
parent bd3bbd0329
commit 9f2e8683de
Notes: sideshowbarker 2024-07-17 22:00:41 +09:00

View file

@ -0,0 +1,70 @@
diff --git a/gdbsupport/alt-stack.h b/gdbsupport/alt-stack.h
index 056ea41..b638533 100644
--- a/gdbsupport/alt-stack.h
+++ b/gdbsupport/alt-stack.h
@@ -20,7 +20,9 @@
#ifndef GDBSUPPORT_ALT_STACK_H
#define GDBSUPPORT_ALT_STACK_H
+#include "common-defs.h"
#include <signal.h>
+#include <sys/mman.h>
namespace gdb
{
@@ -36,31 +38,44 @@ class alternate_signal_stack
public:
alternate_signal_stack ()
{
+ // We can't use xmalloc here on Serenity, because stack regions
+ // do not play well with how malloc manages its memory.
#ifdef HAVE_SIGALTSTACK
- m_stack.reset ((char *) xmalloc (SIGSTKSZ));
-
- stack_t stack;
- stack.ss_sp = m_stack.get ();
- stack.ss_size = SIGSTKSZ;
- stack.ss_flags = 0;
-
- sigaltstack (&stack, &m_old_stack);
+ void *ptr = mmap (nullptr, SIGSTKSZ, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (ptr == MAP_FAILED)
+ {
+ warning ("could not mmap alternate signal stack");
+ }
+ else
+ {
+ m_stack = ptr;
+ stack_t stack;
+ stack.ss_sp = m_stack;
+ stack.ss_size = SIGSTKSZ;
+ stack.ss_flags = 0;
+
+ sigaltstack (&stack, &m_old_stack);
+ }
#endif
}
~alternate_signal_stack ()
{
#ifdef HAVE_SIGALTSTACK
- sigaltstack (&m_old_stack, nullptr);
+ if (m_stack != nullptr)
+ {
+ sigaltstack (&m_old_stack, nullptr);
+ munmap (m_stack, SIGSTKSZ);
+ }
#endif
}
DISABLE_COPY_AND_ASSIGN (alternate_signal_stack);
private:
-
#ifdef HAVE_SIGALTSTACK
- gdb::unique_xmalloc_ptr<char> m_stack;
+ void *m_stack{ nullptr };
stack_t m_old_stack;
#endif
};