public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/5] htl: Let Mach place thread stacks
@ 2023-06-25 23:17 Sergey Bugaev
  2023-06-25 23:17 ` [PATCH 2/5] hurd: Map brk non-executable Sergey Bugaev
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: Sergey Bugaev @ 2023-06-25 23:17 UTC (permalink / raw)
  To: libc-alpha, bug-hurd

Instead of trying to allocate a thread stack at a specific address,
looping over the address space, just set the ANYWHERE flag in
vm_allocate (). The previous behavior:

- defeats ASLR (for Mach versions that support ASLR),
- is particularly slow if the lower 4 GB of the address space are mapped
  inaccessible, as we're planning to do on 64-bit Hurd,
- is just silly.

Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
---
 sysdeps/mach/htl/pt-stack-alloc.c | 35 ++++++-------------------------
 1 file changed, 6 insertions(+), 29 deletions(-)

diff --git a/sysdeps/mach/htl/pt-stack-alloc.c b/sysdeps/mach/htl/pt-stack-alloc.c
index 429ac2d9..97e6b445 100644
--- a/sysdeps/mach/htl/pt-stack-alloc.c
+++ b/sysdeps/mach/htl/pt-stack-alloc.c
@@ -19,14 +19,9 @@
 #include <errno.h>
 
 #include <mach.h>
-#include <mach/machine/vm_param.h>
 
 #include <pt-internal.h>
 
-/* The next address to use for stack allocation.  */
-static vm_address_t next_stack_base = VM_MIN_ADDRESS;
-
-
 /* Allocate a new stack of size STACKSIZE.  If successful, store the
    address of the newly allocated stack in *STACKADDR and return 0.
    Otherwise return an error code (EINVAL for an invalid stack size,
@@ -35,30 +30,12 @@ static vm_address_t next_stack_base = VM_MIN_ADDRESS;
 int
 __pthread_stack_alloc (void **stackaddr, size_t stacksize)
 {
-  vm_offset_t base;
-  int i = 0;
-
-get_stack:
-  i++;
-  for (base = next_stack_base;
-       base < VM_MAX_ADDRESS
-       && __vm_allocate (__mach_task_self (), &base,
-			 stacksize, FALSE) != KERN_SUCCESS; base += stacksize)
-    ;
-
-  if (base >= VM_MAX_ADDRESS)
-    {
-      if (i == 1)
-	{
-	  next_stack_base = VM_MIN_ADDRESS;
-	  goto get_stack;
-	}
-      else
-	return EAGAIN;
-    }
+  error_t err;
 
-  next_stack_base = base + stacksize;
+  err = __vm_allocate (__mach_task_self (), (vm_offset_t *) stackaddr,
+		       stacksize, TRUE);
 
-  (*stackaddr) = (void *) base;
-  return 0;
+  if (err == KERN_NO_SPACE)
+    err = EAGAIN;
+  return err;
 }
-- 
2.41.0


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-07-02 23:37 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-25 23:17 [PATCH 1/5] htl: Let Mach place thread stacks Sergey Bugaev
2023-06-25 23:17 ` [PATCH 2/5] hurd: Map brk non-executable Sergey Bugaev
2023-07-02 23:27   ` Samuel Thibault
2023-06-25 23:17 ` [PATCH 3/5] hurd: Fix calling vm_deallocate (NULL) Sergey Bugaev
2023-07-02 23:28   ` Samuel Thibault
2023-06-25 23:17 ` [PATCH 4/5] hurd: Fix mapping at address 0 with MAP_FIXED Sergey Bugaev
2023-07-02 23:33   ` Samuel Thibault
2023-06-25 23:17 ` [PATCH 5/5] hurd: Implement MAP_EXCL Sergey Bugaev
2023-07-02 23:37   ` Samuel Thibault
2023-07-02 23:26 ` [PATCH 1/5] htl: Let Mach place thread stacks Samuel Thibault

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).