aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMeng-Ying Yang <garfield@dexon.org>2019-04-25 09:46:24 +0800
committerJhih-Ming Huang <jm.huang@cobinhood.com>2019-05-06 10:44:05 +0800
commita07ab648532edfadba1e4b616784dbae5cb3a742 (patch)
tree546417bd470bc0d1f5362d0571400e5da5445ee5
parentab89e16a223a4e8d1a67b01fae163dd647cc28cc (diff)
downloaddexon-a07ab648532edfadba1e4b616784dbae5cb3a742.tar.gz
dexon-a07ab648532edfadba1e4b616784dbae5cb3a742.tar.zst
dexon-a07ab648532edfadba1e4b616784dbae5cb3a742.zip
Remove underflow error (#385)
core: vm: sqlvm: remove underflow error
-rw-r--r--core/vm/sqlvm/errors/errors.go2
-rw-r--r--core/vm/sqlvm/runtime/instructions.go25
-rw-r--r--core/vm/sqlvm/runtime/instructions_op_test.go32
-rw-r--r--core/vm/sqlvm/runtime/instructions_tmpl_data.go24
4 files changed, 40 insertions, 43 deletions
diff --git a/core/vm/sqlvm/errors/errors.go b/core/vm/sqlvm/errors/errors.go
index e69993300..c97b3e191 100644
--- a/core/vm/sqlvm/errors/errors.go
+++ b/core/vm/sqlvm/errors/errors.go
@@ -115,7 +115,6 @@ const (
ErrorCodeInvalidOperandNum
ErrorCodeInvalidDataType
ErrorCodeOverflow
- ErrorCodeUnderflow
ErrorCodeIndexOutOfRange
ErrorCodeInvalidCastType
ErrorCodeDividedByZero
@@ -150,7 +149,6 @@ var errorCodeMap = [...]string{
ErrorCodeInvalidOperandNum: "invalid operand number",
ErrorCodeInvalidDataType: "invalid data type",
ErrorCodeOverflow: "overflow",
- ErrorCodeUnderflow: "underflow",
ErrorCodeIndexOutOfRange: "index out of range",
ErrorCodeInvalidCastType: "invalid cast type",
ErrorCodeDividedByZero: "divide by zero",
diff --git a/core/vm/sqlvm/runtime/instructions.go b/core/vm/sqlvm/runtime/instructions.go
index 8c303c160..d61bacdde 100644
--- a/core/vm/sqlvm/runtime/instructions.go
+++ b/core/vm/sqlvm/runtime/instructions.go
@@ -399,10 +399,8 @@ func flowCheck(ctx *common.Context, v decimal.Decimal, dType ast.DataType) (err
return
}
- if v.Cmp(max) > 0 {
+ if v.Cmp(max) > 0 || v.Cmp(min) < 0 {
err = se.ErrorCodeOverflow
- } else if v.Cmp(min) < 0 {
- err = se.ErrorCodeUnderflow
}
return
}
@@ -1625,21 +1623,22 @@ func (r *Raw) castInt(ctx *common.Context, origin, target ast.DataType) (err err
case ast.DataTypeMajorInt, ast.DataTypeMajorUint:
err = r.castValue(ctx, origin, target, int(tMinor)+1, signed, false)
case ast.DataTypeMajorAddress:
- r.Bytes, err = ast.DecimalEncode(origin, r.Value)
- if err != nil {
- return
+ var mockDt ast.DataType
+ if signed {
+ mockDt = ast.ComposeDataType(ast.DataTypeMajorInt, 19)
+ } else {
+ mockDt = ast.ComposeDataType(ast.DataTypeMajorUint, 19)
}
- if len(r.Bytes) > dexCommon.AddressLength {
- if r.Bytes[0]&0x80 != 0 && signed {
- err = se.ErrorCodeUnderflow
- } else {
- err = se.ErrorCodeOverflow
- }
+ if ctx.Opt.SafeMath && flowCheck(ctx, r.Value, mockDt) != nil {
+ err = se.ErrorCodeOverflow
return
}
- r.Bytes = r.shiftBytes(r.Bytes, dexCommon.AddressLength, signed, false)
+ r.Bytes, err = ast.DecimalEncode(mockDt, r.Value)
+ if err != nil {
+ return
+ }
r.Value = decimal.Zero
case ast.DataTypeMajorFixedBytes:
if tMinor != oMinor {
diff --git a/core/vm/sqlvm/runtime/instructions_op_test.go b/core/vm/sqlvm/runtime/instructions_op_test.go
index c01eb3bf2..994c508ff 100644
--- a/core/vm/sqlvm/runtime/instructions_op_test.go
+++ b/core/vm/sqlvm/runtime/instructions_op_test.go
@@ -210,7 +210,7 @@ func (s *instructionSuite) TestOpAdd() {
errors.ErrorCodeOverflow,
},
{
- "Underflow - Immediate",
+ "Overflow - Immediate",
Instruction{
Op: ADD,
Input: []*Operand{
@@ -242,10 +242,10 @@ func (s *instructionSuite) TestOpAdd() {
[]ast.DataType{},
[]Tuple{},
),
- errors.ErrorCodeUnderflow,
+ errors.ErrorCodeOverflow,
},
{
- "Underflow None Immediate",
+ "Overflow None Immediate",
Instruction{
Op: ADD,
Input: []*Operand{
@@ -277,7 +277,7 @@ func (s *instructionSuite) TestOpAdd() {
[]ast.DataType{},
[]Tuple{},
),
- errors.ErrorCodeUnderflow,
+ errors.ErrorCodeOverflow,
},
}
@@ -485,7 +485,7 @@ func (s *instructionSuite) TestOpSub() {
errors.ErrorCodeOverflow,
},
{
- "Underflow - Immediate",
+ "Overflow - Immediate",
Instruction{
Op: SUB,
Input: []*Operand{
@@ -517,10 +517,10 @@ func (s *instructionSuite) TestOpSub() {
[]ast.DataType{},
[]Tuple{},
),
- errors.ErrorCodeUnderflow,
+ errors.ErrorCodeOverflow,
},
{
- "Underflow None Immediate",
+ "Overflow None Immediate",
Instruction{
Op: SUB,
Input: []*Operand{
@@ -552,7 +552,7 @@ func (s *instructionSuite) TestOpSub() {
[]ast.DataType{},
[]Tuple{},
),
- errors.ErrorCodeUnderflow,
+ errors.ErrorCodeOverflow,
},
}
@@ -756,7 +756,7 @@ func (s *instructionSuite) TestOpMul() {
errors.ErrorCodeOverflow,
},
{
- "Underflow - Immediate",
+ "Overflow - Immediate",
Instruction{
Op: MUL,
Input: []*Operand{
@@ -788,10 +788,10 @@ func (s *instructionSuite) TestOpMul() {
[]ast.DataType{},
[]Tuple{},
),
- errors.ErrorCodeUnderflow,
+ errors.ErrorCodeOverflow,
},
{
- "Underflow None Immediate",
+ "Overflow None Immediate",
Instruction{
Op: MUL,
Input: []*Operand{
@@ -823,7 +823,7 @@ func (s *instructionSuite) TestOpMul() {
[]ast.DataType{},
[]Tuple{},
),
- errors.ErrorCodeUnderflow,
+ errors.ErrorCodeOverflow,
},
}
@@ -3560,7 +3560,7 @@ func (s *instructionSuite) TestOpRange() {
makeOperand(
true,
[]ast.DataType{
- ast.ComposeDataType(ast.DataTypeMajorUint, 1), ast.ComposeDataType(ast.DataTypeMajorUint, 1),
+ ast.ComposeDataType(ast.DataTypeMajorUint, 7), ast.ComposeDataType(ast.DataTypeMajorUint, 7),
},
[]Tuple{
{&Raw{Value: decimal.NewFromFloat(1)}, &Raw{Value: decimal.NewFromFloat(2)}},
@@ -3599,7 +3599,7 @@ func (s *instructionSuite) TestOpRange() {
makeOperand(
true,
[]ast.DataType{
- ast.ComposeDataType(ast.DataTypeMajorUint, 1), ast.ComposeDataType(ast.DataTypeMajorUint, 1),
+ ast.ComposeDataType(ast.DataTypeMajorUint, 7), ast.ComposeDataType(ast.DataTypeMajorUint, 7),
},
[]Tuple{
{&Raw{Value: decimal.NewFromFloat(1)}, &Raw{Value: decimal.NewFromFloat(0)}},
@@ -3635,7 +3635,7 @@ func (s *instructionSuite) TestOpRange() {
makeOperand(
true,
[]ast.DataType{
- ast.ComposeDataType(ast.DataTypeMajorUint, 1),
+ ast.ComposeDataType(ast.DataTypeMajorUint, 7),
},
[]Tuple{
{&Raw{Value: decimal.NewFromFloat(20)}},
@@ -3671,7 +3671,7 @@ func (s *instructionSuite) TestOpRange() {
makeOperand(
true,
[]ast.DataType{
- ast.ComposeDataType(ast.DataTypeMajorUint, 1), ast.ComposeDataType(ast.DataTypeMajorUint, 1),
+ ast.ComposeDataType(ast.DataTypeMajorUint, 7), ast.ComposeDataType(ast.DataTypeMajorUint, 7),
},
[]Tuple{
{&Raw{Value: decimal.NewFromFloat(20)}, &Raw{Value: decimal.NewFromFloat(10)}},
diff --git a/core/vm/sqlvm/runtime/instructions_tmpl_data.go b/core/vm/sqlvm/runtime/instructions_tmpl_data.go
index e6cf97a9a..23669d97a 100644
--- a/core/vm/sqlvm/runtime/instructions_tmpl_data.go
+++ b/core/vm/sqlvm/runtime/instructions_tmpl_data.go
@@ -137,8 +137,8 @@ var testData = &tmplData{
Output: &tmplOp{},
},
{
- Name: "Underflow - Immediate",
- Error: "errors.ErrorCodeUnderflow", OpCode: "ADD",
+ Name: "Overflow - Immediate",
+ Error: "errors.ErrorCodeOverflow", OpCode: "ADD",
Inputs: []*tmplOp{
{
Im: false,
@@ -154,8 +154,8 @@ var testData = &tmplData{
Output: &tmplOp{},
},
{
- Name: "Underflow None Immediate",
- Error: "errors.ErrorCodeUnderflow", OpCode: "ADD",
+ Name: "Overflow None Immediate",
+ Error: "errors.ErrorCodeOverflow", OpCode: "ADD",
Inputs: []*tmplOp{
{
Im: false,
@@ -297,8 +297,8 @@ var testData = &tmplData{
Output: &tmplOp{},
},
{
- Name: "Underflow - Immediate",
- Error: "errors.ErrorCodeUnderflow", OpCode: "SUB",
+ Name: "Overflow - Immediate",
+ Error: "errors.ErrorCodeOverflow", OpCode: "SUB",
Inputs: []*tmplOp{
{
Im: false,
@@ -314,8 +314,8 @@ var testData = &tmplData{
Output: &tmplOp{},
},
{
- Name: "Underflow None Immediate",
- Error: "errors.ErrorCodeUnderflow", OpCode: "SUB",
+ Name: "Overflow None Immediate",
+ Error: "errors.ErrorCodeOverflow", OpCode: "SUB",
Inputs: []*tmplOp{
{
Im: false,
@@ -454,8 +454,8 @@ var testData = &tmplData{
Output: &tmplOp{},
},
{
- Name: "Underflow - Immediate",
- Error: "errors.ErrorCodeUnderflow", OpCode: "MUL",
+ Name: "Overflow - Immediate",
+ Error: "errors.ErrorCodeOverflow", OpCode: "MUL",
Inputs: []*tmplOp{
{
Im: false,
@@ -471,8 +471,8 @@ var testData = &tmplData{
Output: &tmplOp{},
},
{
- Name: "Underflow None Immediate",
- Error: "errors.ErrorCodeUnderflow", OpCode: "MUL",
+ Name: "Overflow None Immediate",
+ Error: "errors.ErrorCodeOverflow", OpCode: "MUL",
Inputs: []*tmplOp{
{
Im: false,