Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F163264722
D57908.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
D57908.diff
View Options
diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c
--- a/libexec/rtld-elf/rtld.c
+++ b/libexec/rtld-elf/rtld.c
@@ -6878,31 +6878,6 @@
strlen(name))));
}
-/* malloc */
-void *
-malloc(size_t nbytes)
-{
- return (__crt_malloc(nbytes));
-}
-
-void *
-calloc(size_t num, size_t size)
-{
- return (__crt_calloc(num, size));
-}
-
-void
-free(void *cp)
-{
- __crt_free(cp);
-}
-
-void *
-realloc(void *cp, size_t nbytes)
-{
- return (__crt_realloc(cp, nbytes));
-}
-
extern int _rtld_version__FreeBSD_version __exported;
int _rtld_version__FreeBSD_version = __FreeBSD_version;
diff --git a/libexec/rtld-elf/xmalloc.c b/libexec/rtld-elf/xmalloc.c
--- a/libexec/rtld-elf/xmalloc.c
+++ b/libexec/rtld-elf/xmalloc.c
@@ -26,6 +26,8 @@
*/
#include <sys/param.h>
+#include <machine/cpu.h>
+#include <machine/cpufunc.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
@@ -35,12 +37,36 @@
#include "rtld_malloc.h"
#include "rtld_libc.h"
+static int rtld_malloc_spinlock;
+
+/*
+ * Almost always the rtld malloc is called under the write-locked rtld
+ * bind lock. Due to this, xlock() would need a single CAS to take
+ * the spinlock. But some places only own read-locked rtld bind lock,
+ * and then the spinlock protects the malloc structures from the
+ * parallel updates.
+ */
+static void
+xlock(void)
+{
+ while (atomic_cmpset_acq_int(&rtld_malloc_spinlock, 0, 1) == 0)
+ cpu_spinwait();
+}
+
+static void
+xunlock(void)
+{
+ atomic_store_rel_int(&rtld_malloc_spinlock, 0);
+}
+
void *
xcalloc(size_t number, size_t size)
{
void *p;
+ xlock();
p = __crt_calloc(number, size);
+ xunlock();
if (p == NULL) {
rtld_fdputstr(STDERR_FILENO, "Out of memory\n");
_exit(1);
@@ -51,10 +77,11 @@
void *
xmalloc(size_t size)
{
-
void *p;
+ xlock();
p = __crt_malloc(size);
+ xunlock();
if (p == NULL) {
rtld_fdputstr(STDERR_FILENO, "Out of memory\n");
_exit(1);
@@ -83,10 +110,53 @@
if (align < sizeof(void *))
align = sizeof(void *);
+ xlock();
res = __crt_aligned_alloc_offset(align, size, offset);
+ xunlock();
if (res == NULL) {
rtld_fdputstr(STDERR_FILENO, "Out of memory\n");
_exit(1);
}
return (res);
}
+
+void *
+malloc(size_t size)
+{
+ void *p;
+
+ xlock();
+ p = __crt_malloc(size);
+ xunlock();
+ return (p);
+}
+
+void *
+calloc(size_t num, size_t size)
+{
+ void *p;
+
+ xlock();
+ p = __crt_calloc(num, size);
+ xunlock();
+ return (p);
+}
+
+void
+free(void *cp)
+{
+ xlock();
+ __crt_free(cp);
+ xunlock();
+}
+
+void *
+realloc(void *cp, size_t nbytes)
+{
+ void *p;
+
+ xlock();
+ p = __crt_realloc(cp, nbytes);
+ xunlock();
+ return (p);
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Jul 22, 2:03 PM (13 h, 45 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
35366406
Default Alt Text
D57908.diff (2 KB)
Attached To
Mode
D57908: rtld: stop using alloca()
Attached
Detach File
Event Timeline
Log In to Comment