diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-02-19 16:32:39 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-02-19 16:32:39 -0800 |
commit | 634c9b963c109c772f3ba913d4abd328093059bc (patch) | |
tree | d0059f0b94ccf5a4f33ad933e9f9b2cb847a8ad5 | |
parent | 6dfcace48a061dd93643a4da4647591d73bdcc31 (diff) |
handle malloc(0) and free(0) in corruption checker
-rw-r--r-- | src/corruptionCheck.js | 6 | ||||
-rwxr-xr-x | tests/runner.py | 36 |
2 files changed, 41 insertions, 1 deletions
diff --git a/src/corruptionCheck.js b/src/corruptionCheck.js index ae2a0bdf..bd3b240b 100644 --- a/src/corruptionCheck.js +++ b/src/corruptionCheck.js @@ -19,8 +19,8 @@ var CorruptionChecker = { } }); }, malloc: function(size) { + if (size <= 0) size = 1; // malloc(0) sometimes happens - just allocate a larger area, no harm CorruptionChecker.checkAll(); - assert(size > 0); // some mallocs accept zero - fix your code if you want to use this tool size = (size+7)&(~7); var allocation = CorruptionChecker.realMalloc(size*(1+2*CorruptionChecker.BUFFER_FACTOR)); var ptr = allocation + size*CorruptionChecker.BUFFER_FACTOR; @@ -28,13 +28,17 @@ 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]); 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); assert(size); var allocation = ptr - size*CorruptionChecker.BUFFER_FACTOR; + //Module.print('free ' + ptr + ' of size ' + size + ' and allocation ' + allocation); delete CorruptionChecker.ptrs[ptr]; CorruptionChecker.realFree(allocation); }, diff --git a/tests/runner.py b/tests/runner.py index a2d75cfe..f0340cd0 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -7214,6 +7214,42 @@ def process(filename): for corrupt in [1]: self.do_run(src.replace('CORRUPT', str(corrupt)), 'Heap corruption detected!' if corrupt else 'All ok, 4209') + def test_corruption_2(self): + if Settings.ASM_JS: return self.skip('cannot use corruption checks in asm') + if Settings.USE_TYPED_ARRAYS != 2: return self.skip('needs ta2 for actual test') + + Settings.SAFE_HEAP = 1 + Settings.CORRUPTION_CHECK = 1 + + # test for free(0), malloc(0), etc. + src = r''' + #include <iostream> + #include <fstream> + #include <stdlib.h> + #include <stdio.h> + + void bye() { + printf("all ok\n"); + } + + int main() { + atexit(bye); + + std::string testPath = "/Script/WA-KA.txt"; + std::fstream str(testPath.c_str(), std::ios::in | std::ios::binary); + + if (str.is_open()) + { + std::cout << "open!" << std::endl; + } else { + std::cout << "missing!" << std::endl; + } + + return 1; + } + ''' + self.do_run(src, 'missing!\nall ok\n') + ### Integration tests def test_ccall(self): |