From 1f26e903d10ce1de27289fb69ca7724ca6718ace Mon Sep 17 00:00:00 2001 From: Ivan Gotovchits Date: Fri, 23 Aug 2019 16:36:03 -0400 Subject: [PATCH] parametrize Primus Lisp malloc with initialization strategy (#979) So far our simple-memory-allocator was always filling the allocated memory with zeros. The proposed implementation adds two parameters the first one, `*malloc-initialize-memory*` controls whether memory is initialized at all, and the second one, `*malloc-initial-value*` is the value that is used to fill in the allocated memory, when the first parameter is set to `true`. By default, we set `*malloc-initialize-memory*` to false, so that PR changes the behavior of Primus. Now the heap memory is randomized, so if it breaks your analysis the set `*malloc-initialize-memory*` to `true`. --- .../primus_lisp/lisp/simple-memory-allocator.lisp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/plugins/primus_lisp/lisp/simple-memory-allocator.lisp b/plugins/primus_lisp/lisp/simple-memory-allocator.lisp index 67d97873e..6d4613794 100644 --- a/plugins/primus_lisp/lisp/simple-memory-allocator.lisp +++ b/plugins/primus_lisp/lisp/simple-memory-allocator.lisp @@ -18,10 +18,20 @@ (defparameter *malloc-guard-pattern* 0xA5 "a byte that will be used to fill guard edges") - (defparameter *malloc-zero-sentinel* 0 "a pointer that is returned by (malloc 0)") +(defparameter *malloc-initialize-memory* false + "if true then initialize allocated memory with *malloc-initial-value*") + +(defparameter *malloc-initial-value* 0 + "initialize allocated memory with the said value") + +(defun memory/allocate (ptr len) + (if *malloc-initialize-memory* + (memory-allocate ptr n *malloc-initial-value*) + (memory-allocate ptr n))) + (defun malloc (n) "allocates a memory region of size N" (declare (external "malloc")) @@ -29,12 +39,13 @@ (if (malloc-will-reach-limit n) 0 (let ((n (+ n (* 2 *malloc-guard-edges*))) (ptr brk) - (failed (memory-allocate ptr n 0))) + (failed (memory/allocate ptr n))) (if failed 0 (set brk (+ brk n)) (malloc/fill-edges ptr n) (+ ptr *malloc-guard-edges*)))))) + ;; in our simplistic malloc implementation, free is just a nop (defun free (p) "frees the memory region pointed by P"