aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-02-19 17:28:22 -0800
committerAlon Zakai <alonzakai@gmail.com>2013-02-19 17:28:22 -0800
commit239e82b02c67a8a0e840dfa499fb3b4f1726cb61 (patch)
tree8a3b68c1d4cdbe660b1fada35e310dcbe28b71bf /src
parentf9728ef5b36fce1639af4ba6e9ac1e68321053fd (diff)
support realloc in corruption checker
Diffstat (limited to 'src')
-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++;
},