projects
/
musl
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
implement mq_notify
[musl]
/
src
/
malloc
/
malloc.c
diff --git
a/src/malloc/malloc.c
b/src/malloc/malloc.c
index
d9a30fe
..
1a1a51f
100644
(file)
--- a/
src/malloc/malloc.c
+++ b/
src/malloc/malloc.c
@@
-1,3
+1,4
@@
+#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
@@
-179,7
+180,7
@@
fail:
return 0;
}
return 0;
}
-static int init_malloc()
+static int init_malloc(
size_t n
)
{
static int init, waiters;
int state;
{
static int init, waiters;
int state;
@@
-196,7
+197,7
@@
static int init_malloc()
mal.brk = __brk(0) + 2*SIZE_ALIGN-1 & -SIZE_ALIGN;
mal.brk = __brk(0) + 2*SIZE_ALIGN-1 & -SIZE_ALIGN;
- c = expand_heap(
1
);
+ c = expand_heap(
n
);
if (!c) {
a_store(&init, 0);
if (!c) {
a_store(&init, 0);
@@
-210,15
+211,20
@@
static int init_malloc()
a_store(&init, 2);
if (waiters) __wake(&init, -1, 1);
a_store(&init, 2);
if (waiters) __wake(&init, -1, 1);
- return
0
;
+ return
1
;
}
static int adjust_size(size_t *n)
{
/* Result of pointer difference must fit in ptrdiff_t. */
}
static int adjust_size(size_t *n)
{
/* Result of pointer difference must fit in ptrdiff_t. */
- if (*n > PTRDIFF_MAX - SIZE_ALIGN - PAGE_SIZE) {
- errno = ENOMEM;
- return -1;
+ if (*n-1 > PTRDIFF_MAX - SIZE_ALIGN - PAGE_SIZE) {
+ if (*n) {
+ errno = ENOMEM;
+ return -1;
+ } else {
+ *n = SIZE_ALIGN;
+ return 0;
+ }
}
*n = (*n + OVERHEAD + SIZE_ALIGN - 1) & SIZE_MASK;
return 0;
}
*n = (*n + OVERHEAD + SIZE_ALIGN - 1) & SIZE_MASK;
return 0;
@@
-325,10
+331,10
@@
void *malloc(size_t n)
struct chunk *c;
int i, j;
struct chunk *c;
int i, j;
- if (
!n ||
adjust_size(&n) < 0) return 0;
+ if (adjust_size(&n) < 0) return 0;
if (n > MMAP_THRESHOLD) {
if (n > MMAP_THRESHOLD) {
- size_t len = n + PAGE_SIZE - 1 & -PAGE_SIZE;
+ size_t len = n +
OVERHEAD +
PAGE_SIZE - 1 & -PAGE_SIZE;
char *base = __mmap(0, len, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (base == (void *)-1) return 0;
char *base = __mmap(0, len, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (base == (void *)-1) return 0;
@@
-342,7
+348,7
@@
void *malloc(size_t n)
for (;;) {
uint64_t mask = mal.binmap & -(1ULL<<i);
if (!mask) {
for (;;) {
uint64_t mask = mal.binmap & -(1ULL<<i);
if (!mask) {
- i
nit_malloc()
;
+ i
f (init_malloc(n) > 0) continue
;
c = expand_heap(n);
if (!c) return 0;
if (alloc_rev(c)) {
c = expand_heap(n);
if (!c) return 0;
if (alloc_rev(c)) {
@@
-377,7
+383,6
@@
void *realloc(void *p, size_t n)
void *new;
if (!p) return malloc(n);
void *new;
if (!p) return malloc(n);
- else if (!n) return free(p), (void *)0;
if (adjust_size(&n) < 0) return 0;
if (adjust_size(&n) < 0) return 0;
@@
-389,6
+394,8
@@
void *realloc(void *p, size_t n)
char *base = (char *)self - extra;
size_t oldlen = n0 + extra;
size_t newlen = n + extra;
char *base = (char *)self - extra;
size_t oldlen = n0 + extra;
size_t newlen = n + extra;
+ /* Crash on realloc of freed chunk */
+ if ((uintptr_t)base < mal.brk) *(volatile char *)0=0;
if (newlen < PAGE_SIZE && (new = malloc(n))) {
memcpy(new, p, n-OVERHEAD);
free(p);
if (newlen < PAGE_SIZE && (new = malloc(n))) {
memcpy(new, p, n-OVERHEAD);
free(p);
@@
-450,6
+457,8
@@
void free(void *p)
size_t extra = self->data[-1];
char *base = (char *)self - extra;
size_t len = CHUNK_SIZE(self) + extra;
size_t extra = self->data[-1];
char *base = (char *)self - extra;
size_t len = CHUNK_SIZE(self) + extra;
+ /* Crash on double free */
+ if ((uintptr_t)base < mal.brk) *(volatile char *)0=0;
__munmap(base, len);
return;
}
__munmap(base, len);
return;
}