aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-02-27 08:04:06 -0800
committerAlon Zakai <alonzakai@gmail.com>2013-02-27 08:04:06 -0800
commit5e6e6f889c7aef090ea1d5aad25b29083afb1036 (patch)
treeb4c441ccd1dbd3f81e34bab81a89b76d0f51a04b /src
parent5516f8fa58d73bc96a7e4ab01d677cdb3eb25bd1 (diff)
parent31186ef9edb06e9bc695fca2c3a14677e8f137a1 (diff)
Merge pull request #884 from vvuk/check-heap-align
Add CHECK_HEAP_ALIGN to perform runtime alignment checks
Diffstat (limited to 'src')
-rw-r--r--src/compiler.js2
-rw-r--r--src/parseTools.js26
-rw-r--r--src/preamble.js19
-rw-r--r--src/settings.js3
4 files changed, 39 insertions, 11 deletions
diff --git a/src/compiler.js b/src/compiler.js
index 0b43842e..14816f1e 100644
--- a/src/compiler.js
+++ b/src/compiler.js
@@ -185,7 +185,7 @@ if (phase == 'pre') {
print('// Note: For maximum-speed code, see "Optimizing Code" on the Emscripten wiki, https://github.com/kripken/emscripten/wiki/Optimizing-Code');
}
- if (DOUBLE_MODE || CORRECT_SIGNS || CORRECT_OVERFLOWS || CORRECT_ROUNDINGS) {
+ if (DOUBLE_MODE || CORRECT_SIGNS || CORRECT_OVERFLOWS || CORRECT_ROUNDINGS || CHECK_HEAP_ALIGN) {
print('// Note: Some Emscripten settings may limit the speed of the generated code.');
}
}
diff --git a/src/parseTools.js b/src/parseTools.js
index 6e0d6e32..117c4987 100644
--- a/src/parseTools.js
+++ b/src/parseTools.js
@@ -984,18 +984,24 @@ function checkSafeHeap() {
function getHeapOffset(offset, type, forceAsm) {
if (USE_TYPED_ARRAYS !== 2) {
return offset;
- } else {
- if (Runtime.getNativeFieldSize(type) > 4) {
- type = 'i32'; // XXX we emulate 64-bit values as 32
- }
- var shifts = Math.log(Runtime.getNativeTypeSize(type))/Math.LN2;
- offset = '(' + offset + ')';
- if (shifts != 0) {
- return '(' + offset + '>>' + shifts + ')';
+ }
+
+ if (Runtime.getNativeFieldSize(type) > 4) {
+ type = 'i32'; // XXX we emulate 64-bit values as 32
+ }
+
+ var sz = Runtime.getNativeTypeSize(type);
+ var shifts = Math.log(sz)/Math.LN2;
+ offset = '(' + offset + ')';
+ if (shifts != 0) {
+ if (CHECK_HEAP_ALIGN) {
+ return '(CHECK_ALIGN_' + sz + '(' + offset + ')>>' + shifts + ')';
} else {
- // we need to guard against overflows here, HEAP[U]8 expects a guaranteed int
- return isJSVar(offset) ? offset : '(' + offset + '|0)';
+ return '(' + offset + '>>' + shifts + ')';
}
+ } else {
+ // we need to guard against overflows here, HEAP[U]8 expects a guaranteed int
+ return isJSVar(offset) ? offset : '(' + offset + '|0)';
}
}
diff --git a/src/preamble.js b/src/preamble.js
index 6f4b49de..71b123e7 100644
--- a/src/preamble.js
+++ b/src/preamble.js
@@ -150,6 +150,25 @@ function SAFE_HEAP_COPY_HISTORY(dest, src) {
//==========================================
#endif
+#if CHECK_HEAP_ALIGN
+//========================================
+// Debugging tools - alignment check
+//========================================
+function CHECK_ALIGN_8(addr) {
+ assert((addr & 7) == 0, "address must be 8-byte aligned, is " + addr + "!");
+ return addr;
+}
+function CHECK_ALIGN_4(addr) {
+ assert((addr & 3) == 0, "address must be 4-byte aligned, is " + addr + "!");
+ return addr;
+}
+function CHECK_ALIGN_2(addr) {
+ assert((addr & 1) == 0, "address must be 2-byte aligned!");
+ return addr;
+}
+#endif
+
+
#if CHECK_OVERFLOWS
//========================================
// Debugging tools - Mathop overflows
diff --git a/src/settings.js b/src/settings.js
index 308afddc..4d742060 100644
--- a/src/settings.js
+++ b/src/settings.js
@@ -128,6 +128,9 @@ var SAFE_HEAP = 0; // Check each write to the heap, for example, this will give
// that 3 is the option you usually want here.
var SAFE_HEAP_LOG = 0; // Log out all SAFE_HEAP operations
+var CHECK_HEAP_ALIGN = 0; // Check heap accesses for alignment, but don't do as
+ // near extensive (or slow) checks as SAFE_HEAP.
+
var SAFE_DYNCALLS = 0; // Show stack traces on missing function pointer/virtual method calls
var ASM_HEAP_LOG = 0; // Simple heap logging, like SAFE_HEAP_LOG but cheaper, and in asm.js