From f2eb0a94955a45497001c6a42efa8a610864399f Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 15 Apr 2021 21:15:07 +0300 Subject: [PATCH 1/4] use F_NOEXPORTRUNTIME exception for __rtti_base --- lib/loader/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 3eb8950baa..1a75e8ec7d 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -88,7 +88,7 @@ function postInstantiate(extendedExports, instance) { const __pin = exports.__pin || F_NOEXPORTRUNTIME; const __unpin = exports.__unpin || F_NOEXPORTRUNTIME; const __collect = exports.__collect || F_NOEXPORTRUNTIME; - const __rtti_base = exports.__rtti_base || ~0; // oob if not present + const __rtti_base = exports.__rtti_base || F_NOEXPORTRUNTIME; extendedExports.__new = __new; extendedExports.__pin = __pin; From 8c3a94704fe4a983637bac99c2810793d630f008 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 15 Apr 2021 21:26:59 +0300 Subject: [PATCH 2/4] wrap __rtti_base to function --- lib/loader/index.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 1a75e8ec7d..35ec663ccc 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -88,7 +88,8 @@ function postInstantiate(extendedExports, instance) { const __pin = exports.__pin || F_NOEXPORTRUNTIME; const __unpin = exports.__unpin || F_NOEXPORTRUNTIME; const __collect = exports.__collect || F_NOEXPORTRUNTIME; - const __rtti_base = exports.__rtti_base || F_NOEXPORTRUNTIME; + const __rtti_base = exports.__rtti_base; + const rttiBase = __rtti_base ? function () { return __rtti_base; } : F_NOEXPORTRUNTIME; extendedExports.__new = __new; extendedExports.__pin = __pin; @@ -98,9 +99,10 @@ function postInstantiate(extendedExports, instance) { /** Gets the runtime type info for the given id. */ function getInfo(id) { const U32 = new Uint32Array(memory.buffer); - const count = U32[__rtti_base >>> 2]; + const rtBase = rttiBase(); + const count = U32[rtBase >>> 2]; if ((id >>>= 0) >= count) throw Error(`invalid id: ${id}`); - return U32[(__rtti_base + 4 >>> 2) + id * 2]; + return U32[(rtBase + 4 >>> 2) + id * 2]; } /** Gets and validate runtime type info for the given id for array like objects */ @@ -113,9 +115,10 @@ function postInstantiate(extendedExports, instance) { /** Gets the runtime base id for the given id. */ function getBase(id) { const U32 = new Uint32Array(memory.buffer); - const count = U32[__rtti_base >>> 2]; + const rtBase = rttiBase(); + const count = U32[rtBase >>> 2]; if ((id >>>= 0) >= count) throw Error(`invalid id: ${id}`); - return U32[(__rtti_base + 4 >>> 2) + id * 2 + 1]; + return U32[(rtBase + 4 >>> 2) + id * 2 + 1]; } /** Gets the runtime alignment of a collection's values. */ @@ -284,7 +287,7 @@ function postInstantiate(extendedExports, instance) { function __instanceof(ptr, baseId) { const U32 = new Uint32Array(memory.buffer); let id = U32[ptr + ID_OFFSET >>> 2]; - if (id <= U32[__rtti_base >>> 2]) { + if (id <= U32[rttiBase() >>> 2]) { do { if (id == baseId) return true; id = getBase(id); From 6c53b9ba6b34476c5bf1c7a1fac9d9be754d4165 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 15 Apr 2021 21:31:48 +0300 Subject: [PATCH 3/4] use rttiBase only once --- lib/loader/index.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 35ec663ccc..5a375eb3a0 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -99,10 +99,9 @@ function postInstantiate(extendedExports, instance) { /** Gets the runtime type info for the given id. */ function getInfo(id) { const U32 = new Uint32Array(memory.buffer); - const rtBase = rttiBase(); - const count = U32[rtBase >>> 2]; + const count = U32[rttiBase() >>> 2]; if ((id >>>= 0) >= count) throw Error(`invalid id: ${id}`); - return U32[(rtBase + 4 >>> 2) + id * 2]; + return U32[(__rtti_base + 4 >>> 2) + id * 2]; } /** Gets and validate runtime type info for the given id for array like objects */ @@ -115,10 +114,9 @@ function postInstantiate(extendedExports, instance) { /** Gets the runtime base id for the given id. */ function getBase(id) { const U32 = new Uint32Array(memory.buffer); - const rtBase = rttiBase(); - const count = U32[rtBase >>> 2]; + const count = U32[rttiBase() >>> 2]; if ((id >>>= 0) >= count) throw Error(`invalid id: ${id}`); - return U32[(rtBase + 4 >>> 2) + id * 2 + 1]; + return U32[(__rtti_base + 4 >>> 2) + id * 2 + 1]; } /** Gets the runtime alignment of a collection's values. */ From 6be028fac670f06a09fd693aee20157b6eb1f51c Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Thu, 15 Apr 2021 21:40:11 +0300 Subject: [PATCH 4/4] refactor --- lib/loader/index.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 5a375eb3a0..b1e01f5323 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -89,7 +89,9 @@ function postInstantiate(extendedExports, instance) { const __unpin = exports.__unpin || F_NOEXPORTRUNTIME; const __collect = exports.__collect || F_NOEXPORTRUNTIME; const __rtti_base = exports.__rtti_base; - const rttiBase = __rtti_base ? function () { return __rtti_base; } : F_NOEXPORTRUNTIME; + const getRttiCount = __rtti_base + ? function (arr) { return arr[__rtti_base >>> 2]; } + : F_NOEXPORTRUNTIME; extendedExports.__new = __new; extendedExports.__pin = __pin; @@ -99,7 +101,7 @@ function postInstantiate(extendedExports, instance) { /** Gets the runtime type info for the given id. */ function getInfo(id) { const U32 = new Uint32Array(memory.buffer); - const count = U32[rttiBase() >>> 2]; + const count = getRttiCount(U32); if ((id >>>= 0) >= count) throw Error(`invalid id: ${id}`); return U32[(__rtti_base + 4 >>> 2) + id * 2]; } @@ -114,7 +116,7 @@ function postInstantiate(extendedExports, instance) { /** Gets the runtime base id for the given id. */ function getBase(id) { const U32 = new Uint32Array(memory.buffer); - const count = U32[rttiBase() >>> 2]; + const count = getRttiCount(U32); if ((id >>>= 0) >= count) throw Error(`invalid id: ${id}`); return U32[(__rtti_base + 4 >>> 2) + id * 2 + 1]; } @@ -285,7 +287,7 @@ function postInstantiate(extendedExports, instance) { function __instanceof(ptr, baseId) { const U32 = new Uint32Array(memory.buffer); let id = U32[ptr + ID_OFFSET >>> 2]; - if (id <= U32[rttiBase() >>> 2]) { + if (id <= getRttiCount(U32)) { do { if (id == baseId) return true; id = getBase(id);