diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-02-19 17:56:40 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-02-19 17:56:40 -0800 |
commit | 6bb1dac24a0c7f7bd2af18f9c0d350cd50b0521b (patch) | |
tree | 8f987b0e3718ce9a26f51330ee6603f6e02d8edc /src/corruptionCheck.js | |
parent | 239e82b02c67a8a0e840dfa499fb3b4f1726cb61 (diff) |
allow changing frequency of corruption checks, and forcing a check irregardless of frequency
Diffstat (limited to 'src/corruptionCheck.js')
-rw-r--r-- | src/corruptionCheck.js | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/src/corruptionCheck.js b/src/corruptionCheck.js index 8698b86d..315f5cf0 100644 --- a/src/corruptionCheck.js +++ b/src/corruptionCheck.js @@ -6,6 +6,7 @@ var CorruptionChecker = { ptrs: {}, checks: 0, + checkFrequency: 1, init: function() { this.realMalloc = _malloc; @@ -33,29 +34,29 @@ var CorruptionChecker = { CorruptionChecker.ptrs[ptr] = size; CorruptionChecker.fillBuffer(allocation, size*CorruptionChecker.BUFFER_FACTOR); CorruptionChecker.fillBuffer(allocation + size*(1+CorruptionChecker.BUFFER_FACTOR), size*CorruptionChecker.BUFFER_FACTOR); - //Module.print('malloc ' + size + ' ==> ' + [ptr, allocation]); + //Module.printErr('malloc ' + size + ' ==> ' + [ptr, allocation]); return ptr; }, free: function(ptr) { if (!ptr) return; // ok to free(NULL), does nothing CorruptionChecker.checkAll(); var size = CorruptionChecker.ptrs[ptr]; - //Module.print('free ' + ptr + ' of size ' + size); + //Module.printErr('free ' + ptr + ' of size ' + size); assert(size); var allocation = ptr - size*CorruptionChecker.BUFFER_FACTOR; - //Module.print('free ' + ptr + ' of size ' + size + ' and allocation ' + allocation); + //Module.printErr('free ' + ptr + ' of size ' + size + ' and allocation ' + allocation); delete CorruptionChecker.ptrs[ptr]; CorruptionChecker.realFree(allocation); }, realloc: function(ptr, newSize) { - //Module.print('realloc ' + ptr + ' to size ' + newSize); + //Module.printErr('realloc ' + ptr + ' to size ' + newSize); if (newSize <= 0) newSize = 1; // like in malloc if (!ptr) return CorruptionChecker.malloc(newSize); // realloc(NULL, size) forwards to malloc according to the spec var size = CorruptionChecker.ptrs[ptr]; assert(size); var allocation = ptr - size*CorruptionChecker.BUFFER_FACTOR; var newPtr = CorruptionChecker.malloc(newSize); - //Module.print('realloc ' + ptr + ' to size ' + newSize + ' is now ' + newPtr); + //Module.printErr('realloc ' + ptr + ' to size ' + newSize + ' is now ' + newPtr); var newAllocation = newPtr + newSize*CorruptionChecker.BUFFER_FACTOR; HEAPU8.set(HEAPU8.subarray(ptr, ptr + Math.min(size, newSize)), newPtr); CorruptionChecker.free(ptr); @@ -75,7 +76,6 @@ var CorruptionChecker = { assert(0, 'Heap corruption detected!' + [x, buffer, size, {{{ makeGetValue('x', 0, 'i8') }}}&255, CorruptionChecker.canary(x)]); } } - CorruptionChecker.checks++; }, checkPtr: function(ptr) { var size = CorruptionChecker.ptrs[ptr]; @@ -84,7 +84,10 @@ var CorruptionChecker = { CorruptionChecker.checkBuffer(allocation, size*CorruptionChecker.BUFFER_FACTOR); CorruptionChecker.checkBuffer(allocation + size*(1+CorruptionChecker.BUFFER_FACTOR), size*CorruptionChecker.BUFFER_FACTOR); }, - checkAll: function() { + checkAll: function(force) { + CorruptionChecker.checks++; + if (!force && CorruptionChecker.checks % CorruptionChecker.checkFrequency != 0) return; + //Module.printErr('checking for corruption ' + (CorruptionChecker.checks/CorruptionChecker.checkFrequency)); for (var ptr in CorruptionChecker.ptrs) { CorruptionChecker.checkPtr(ptr, false); } |