shithub: mc

Download patch

ref: 32048b6b17efd288f303045e3d1417ca21c39901
parent: b6afe89b0d5928f317e8d554cec49d6e8f40a3d8
author: Ori Bernstein <[email protected]>
date: Thu Jan 9 16:17:42 EST 2014

Add magic value checking for headers.

--- a/libstd/alloc.myr
+++ b/libstd/alloc.myr
@@ -39,7 +39,9 @@
 	const zbytealloc	: (sz:size	-> byte#)
 	const bytefree	: (m:byte#, sz:size	-> void)
 
+
 ;;
+extern const put : (b : byte[:], args : ... -> size)
 
 /* null pointers. only used internally. */
 const Zbyteptr	= 0 castto(byte#)
@@ -57,6 +59,7 @@
 
 type slheader = struct
 	cap	: size	/* capacity in bytes */
+	magic	: size	/* magic check value */
 ;;
 
 type bucket = struct
@@ -122,18 +125,30 @@
 
 	phdr = p castto(slheader#)
 	phdr.cap = allocsz(sz) - align(sizeof(slheader), Align)
+	phdr.magic = (0xdeadbeefbadf00d castto(size))
 
 	prest = align((p castto(size)) + sizeof(slheader), Align)
 	-> prest castto(byte#)
 }
 
+const checkhdr = {p
+	var phdr, addr
+
+	addr = p castto(size)
+	addr -= align(sizeof(slheader), Align)
+	phdr = addr castto(slheader#)
+	assert(phdr.magic == (0xdeadbeefbadf00d castto(size)), "corrupt memory\n")
+}
+
 /* Frees a slice */
 generic slfree	 = {sl
 	var head
+
 	if sl.len == 0
 		->
 	;;
 
+	checkhdr(sl castto(byte#))
 	head = (sl castto(byte#)) castto(size)
 	head -= align(sizeof(slheader), Align)
 	bytefree(head castto(byte#), slcap(sl castto(byte#)))