From 508004088e418586aa6f4c3d3bec37c494ef055c Mon Sep 17 00:00:00 2001 From: Patrick Ventuzelo Date: Fri, 22 Nov 2019 17:10:54 +0100 Subject: [PATCH 1/4] check stack len before getting value --- lib/llvm-backend/src/state.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/llvm-backend/src/state.rs b/lib/llvm-backend/src/state.rs index 0d46dc5c099..486cc93572c 100644 --- a/lib/llvm-backend/src/state.rs +++ b/lib/llvm-backend/src/state.rs @@ -240,12 +240,16 @@ impl State { &self, n: usize, ) -> Result<&[(BasicValueEnum, ExtraInfo)], BinaryReaderError> { - self.stack - .get(self.stack.len() - n..) - .ok_or(BinaryReaderError { + if self.stack.len() < n { + return Err(BinaryReaderError { message: "invalid value stack", offset: -1isize as usize, - }) + }); + } + + let new_len = self.stack.len() - n; + Ok(&self.stack[new_len..]) + } pub fn popn_save_extra( From 2261f8b449746ed70231d5b8290990af0d2f5f0c Mon Sep 17 00:00:00 2001 From: Patrick Ventuzelo Date: Fri, 22 Nov 2019 17:21:15 +0100 Subject: [PATCH 2/4] cargo fmt --- lib/llvm-backend/src/state.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/llvm-backend/src/state.rs b/lib/llvm-backend/src/state.rs index 486cc93572c..5f70bb3c580 100644 --- a/lib/llvm-backend/src/state.rs +++ b/lib/llvm-backend/src/state.rs @@ -249,7 +249,6 @@ impl State { let new_len = self.stack.len() - n; Ok(&self.stack[new_len..]) - } pub fn popn_save_extra( From cd0da74b333fa96e9195c43c7191ffd7ebe02a75 Mon Sep 17 00:00:00 2001 From: Patrick Ventuzelo Date: Fri, 22 Nov 2019 17:25:17 +0100 Subject: [PATCH 3/4] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51c0983c848..a9869eab32e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## **[Unreleased]** +- [#1006](https://github.com/wasmerio/wasmer/pull/1006) Fix minor panic issue when `wasmer::compile_with` called with llvm backend - [#995](https://github.com/wasmerio/wasmer/pull/995) Detect when a global is read without being initialized (emit a proper error instead of panicking) - [#996](https://github.com/wasmerio/wasmer/pull/997) Refactored spectests, emtests and wasitests to use default compiler logic - [#992](https://github.com/wasmerio/wasmer/pull/992) Updates WAPM version to 0.4.1, fix arguments issue introduced in #990 From 49665d57975e47f449a526292333ee6647ea1ffa Mon Sep 17 00:00:00 2001 From: Patrick Ventuzelo Date: Tue, 26 Nov 2019 09:17:13 +0100 Subject: [PATCH 4/4] use checked_sub for peekn_extra --- lib/llvm-backend/src/state.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/llvm-backend/src/state.rs b/lib/llvm-backend/src/state.rs index 5f70bb3c580..fde5eb52e40 100644 --- a/lib/llvm-backend/src/state.rs +++ b/lib/llvm-backend/src/state.rs @@ -240,14 +240,11 @@ impl State { &self, n: usize, ) -> Result<&[(BasicValueEnum, ExtraInfo)], BinaryReaderError> { - if self.stack.len() < n { - return Err(BinaryReaderError { - message: "invalid value stack", - offset: -1isize as usize, - }); - } + let new_len = self.stack.len().checked_sub(n).ok_or(BinaryReaderError { + message: "invalid value stack", + offset: -1isize as usize, + })?; - let new_len = self.stack.len() - n; Ok(&self.stack[new_len..]) }