From f19c7c49816e156a93c8a6a241f2f716bf8083e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 14 May 2015 16:14:21 +0200 Subject: [PATCH] Add protection against hardware division by 0. --- evmjit/libevmjit/Compiler.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/evmjit/libevmjit/Compiler.cpp b/evmjit/libevmjit/Compiler.cpp index 56f1904a1..75c81f9ed 100644 --- a/evmjit/libevmjit/Compiler.cpp +++ b/evmjit/libevmjit/Compiler.cpp @@ -280,6 +280,7 @@ void Compiler::compileBasicBlock(BasicBlock& _basicBlock, RuntimeManager& _runti auto d = stack.pop(); auto n = stack.pop(); auto divByZero = m_builder.CreateICmpEQ(n, Constant::get(0)); + n = m_builder.CreateSelect(divByZero, Constant::get(1), n); // protect against hardware signal auto r = m_builder.CreateUDiv(d, n); r = m_builder.CreateSelect(divByZero, Constant::get(0), r); stack.push(r); @@ -292,6 +293,7 @@ void Compiler::compileBasicBlock(BasicBlock& _basicBlock, RuntimeManager& _runti auto n = stack.pop(); auto divByZero = m_builder.CreateICmpEQ(n, Constant::get(0)); auto divByMinusOne = m_builder.CreateICmpEQ(n, Constant::get(-1)); + n = m_builder.CreateSelect(divByZero, Constant::get(1), n); // protect against hardware signal auto r = m_builder.CreateSDiv(d, n); r = m_builder.CreateSelect(divByZero, Constant::get(0), r); auto dNeg = m_builder.CreateSub(Constant::get(0), d); @@ -305,6 +307,7 @@ void Compiler::compileBasicBlock(BasicBlock& _basicBlock, RuntimeManager& _runti auto d = stack.pop(); auto n = stack.pop(); auto divByZero = m_builder.CreateICmpEQ(n, Constant::get(0)); + n = m_builder.CreateSelect(divByZero, Constant::get(1), n); // protect against hardware signal auto r = m_builder.CreateURem(d, n); r = m_builder.CreateSelect(divByZero, Constant::get(0), r); stack.push(r); @@ -316,6 +319,7 @@ void Compiler::compileBasicBlock(BasicBlock& _basicBlock, RuntimeManager& _runti auto d = stack.pop(); auto n = stack.pop(); auto divByZero = m_builder.CreateICmpEQ(n, Constant::get(0)); + n = m_builder.CreateSelect(divByZero, Constant::get(1), n); // protect against hardware signal auto r = m_builder.CreateSRem(d, n); r = m_builder.CreateSelect(divByZero, Constant::get(0), r); stack.push(r);