aboutsummaryrefslogtreecommitdiff
path: root/src/corruptionCheck.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/corruptionCheck.js')
-rw-r--r--src/corruptionCheck.js31
1 files changed, 26 insertions, 5 deletions
diff --git a/src/corruptionCheck.js b/src/corruptionCheck.js
index d676c290..8698b86d 100644
--- a/src/corruptionCheck.js
+++ b/src/corruptionCheck.js
@@ -14,6 +14,11 @@ var CorruptionChecker = {
this.realFree = _free;
_free = Module['_free'] = this.free;
+ if (typeof _realloc != 'undefined') {
+ this.realRealloc = _realloc;
+ _realloc = Module['_realloc'] = this.realloc;
+ }
+
__ATEXIT__.push({ func: function() {
Module.printErr('No corruption detected, ran ' + CorruptionChecker.checks + ' checks.');
} });
@@ -42,17 +47,33 @@ var CorruptionChecker = {
delete CorruptionChecker.ptrs[ptr];
CorruptionChecker.realFree(allocation);
},
+ realloc: function(ptr, newSize) {
+ //Module.print('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);
+ var newAllocation = newPtr + newSize*CorruptionChecker.BUFFER_FACTOR;
+ HEAPU8.set(HEAPU8.subarray(ptr, ptr + Math.min(size, newSize)), newPtr);
+ CorruptionChecker.free(ptr);
+ return newPtr;
+ },
canary: function(x) {
return (x&127) + 10;
},
- fillBuffer: function(allocation, size) {
- for (var x = allocation; x < allocation + size; x++) {
+ fillBuffer: function(buffer, size) {
+ for (var x = buffer; x < buffer + size; x++) {
{{{ makeSetValue('x', 0, 'CorruptionChecker.canary(x)', 'i8') }}};
}
},
- checkBuffer: function(allocation, size) {
- for (var x = allocation; x < allocation + size; x++) {
- assert(({{{ makeGetValue('x', 0, 'i8') }}}&255) == CorruptionChecker.canary(x), 'Heap corruption detected!');
+ checkBuffer: function(buffer, size) {
+ for (var x = buffer; x < buffer + size; x++) {
+ if (({{{ makeGetValue('x', 0, 'i8') }}}&255) != CorruptionChecker.canary(x)) {
+ assert(0, 'Heap corruption detected!' + [x, buffer, size, {{{ makeGetValue('x', 0, 'i8') }}}&255, CorruptionChecker.canary(x)]);
+ }
}
CorruptionChecker.checks++;
},