소스 검색

page-align initial brk value used by malloc in shared libc

this change fixes an obscure issue with some nonstandard kernels,
where the initial brk syscall returns a pointer just past the end of
bss rather than the beginning of a new page. in that case, the dynamic
linker has already reclaimed the space between the end of bss and the
page end for use by malloc, and memory corruption (allocating the same
memory twice) will occur when malloc again claims it on the first call
to brk.
Rich Felker 12 년 전
부모
커밋
b8ccf8e46b
1개의 변경된 파일5개의 추가작업 그리고 1개의 파일을 삭제
  1. 5 1
      src/malloc/malloc.c

+ 5 - 1
src/malloc/malloc.c

@@ -196,7 +196,11 @@ static int init_malloc(size_t n)
 		return 0;
 	}
 
-	mal.brk = __brk(0) + 2*SIZE_ALIGN-1 & -SIZE_ALIGN;
+	mal.brk = __brk(0);
+#ifdef SHARED
+	mal.brk = mal.brk + PAGE_SIZE-1 & -PAGE_SIZE;
+#endif
+	mal.brk = mal.brk + 2*SIZE_ALIGN-1 & -SIZE_ALIGN;
 
 	c = expand_heap(n);