diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-09-09 20:00:52 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-09-09 20:00:52 -0700 |
commit | 1896c00715d1cbfd47a295c9564691dcf88d6032 (patch) | |
tree | d2cc60a400f2b1878b63ae3773bd605701e36676 | |
parent | 953ebf768763fe93a0af76f5007d1f69f91d6440 (diff) |
check for segfaults in SAFE_HEAP
-rw-r--r-- | src/preamble.js | 4 | ||||
-rw-r--r-- | src/settings.js | 4 | ||||
-rwxr-xr-x | tests/runner.py | 31 |
3 files changed, 37 insertions, 2 deletions
diff --git a/src/preamble.js b/src/preamble.js index a8f19d64..bbb9d684 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -24,6 +24,8 @@ var ACCEPTABLE_SAFE_HEAP_ERRORS = 0; function SAFE_HEAP_ACCESS(dest, type, store, ignore) { //if (dest === A_NUMBER) Module.print ([dest, type, store] + ' ' + new Error().stack); // Something like this may be useful, in debugging + assert(dest >= STACK_ROOT, 'segmentation fault: null pointer, or below normal memory'); + #if USE_TYPED_ARRAYS // When using typed arrays, reads over the top of TOTAL_MEMORY will fail silently, so we must // correct that by growing TOTAL_MEMORY as needed. Without typed arrays, memory is a normal @@ -643,7 +645,7 @@ var base = intArrayFromString('(null)'); // So printing %s of NULL gives '(null) // Also this ensures we leave 0 as an invalid address, 'NULL' STATICTOP = base.length; for (var i = 0; i < base.length; i++) { - {{{ makeSetValue(0, 'i', 'base[i]', 'i8') }}} + {{{ makeSetValue(0, 'i', 'base[i]', 'i8', null, null, null, 1) }}} } Module['HEAP'] = HEAP; diff --git a/src/settings.js b/src/settings.js index 9f63622d..fe532bda 100644 --- a/src/settings.js +++ b/src/settings.js @@ -104,7 +104,9 @@ var CATCH_EXIT_CODE = 0; // If set, causes exit() to throw an exception object w // terminated with an error message. // Generated code debugging options -var SAFE_HEAP = 0; // Check each write to the heap against a list of blocked addresses +var SAFE_HEAP = 0; // Check each write to the heap, for example, this will give a clear + // error on what would be segfaults in a native build (like deferencing + // 0). See preamble.js for the actual checks performed. // If equal to 2, done on a line-by-line basis according to // SAFE_HEAP_LINES, checking only the specified lines. // If equal to 3, checking all *but* the specified lines. Note diff --git a/tests/runner.py b/tests/runner.py index b81cee9a..a089d7b8 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -2108,6 +2108,37 @@ c5,de,15,8a ''' self.do_run(src, '*11,74,32,1012*\n*11*\n*22*') + def test_segfault(self): + Settings.SAFE_HEAP = 1 + + for addr in ['0', '7', 'new D2()']: + print addr + src = r''' + #include <stdio.h> + + struct Classey { + virtual void doIt() = 0; + }; + + struct D1 : Classey { + virtual void doIt() { printf("fleefl\n"); } + }; + + struct D2 : Classey { + virtual void doIt() { printf("marfoosh\n"); } + }; + + int main(int argc, char **argv) + { + Classey *p = argc == 100 ? new D1() : (Classey*)%s; + + p->doIt(); + + return 0; + } + ''' % addr + self.do_run(src, 'segmentation fault' if addr.isdigit() else 'marfoosh') + def test_dynamic_cast(self): if self.emcc_args is None: return self.skip('need libcxxabi') |