@@ -4,6 +4,7 @@ use move_binary_format::file_format::{
4
4
FunctionDefinitionIndex , empty_module
5
5
} ;
6
6
7
+ use move_core_types:: u256:: U256 ;
7
8
use move_core_types:: vm_status:: StatusCode ;
8
9
use move_binary_format:: CompiledModule ;
9
10
use move_bytecode_verifier_meter:: dummy:: DummyMeter ;
@@ -181,3 +182,115 @@ fn test_cast_no_arg() {
181
182
let _result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
182
183
}
183
184
}
185
+
186
+
187
+
188
+ #[ test]
189
+ fn test_arithmetic_correct_types ( ) {
190
+ for instr in vec ! [
191
+ Bytecode :: Add ,
192
+ Bytecode :: Sub ,
193
+ Bytecode :: Mul ,
194
+ Bytecode :: Mod ,
195
+ Bytecode :: Div ,
196
+ Bytecode :: BitOr ,
197
+ Bytecode :: BitAnd ,
198
+ Bytecode :: Xor ,
199
+ ] {
200
+ for push_ty_instr in vec ! [
201
+ Bytecode :: LdU8 ( 42 ) ,
202
+ Bytecode :: LdU16 ( 257 ) ,
203
+ Bytecode :: LdU32 ( 89 ) ,
204
+ Bytecode :: LdU64 ( 94 ) ,
205
+ Bytecode :: LdU128 ( Box :: new( 9999 ) ) ,
206
+ Bytecode :: LdU256 ( Box :: new( U256 :: from( 745_u32 ) ) ) ,
207
+ ] {
208
+ let code = vec ! [ push_ty_instr. clone( ) , push_ty_instr. clone( ) , instr. clone( ) ] ;
209
+ let module = make_module ( code) ;
210
+ let fun_context = get_fun_context ( & module) ;
211
+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
212
+ assert ! ( result. is_ok( ) ) ;
213
+ }
214
+ }
215
+ }
216
+
217
+ #[ test]
218
+ fn test_arithmetic_mismatched_types ( ) {
219
+ for instr in vec ! [
220
+ Bytecode :: Add ,
221
+ Bytecode :: Sub ,
222
+ Bytecode :: Mul ,
223
+ Bytecode :: Mod ,
224
+ Bytecode :: Div ,
225
+ Bytecode :: BitOr ,
226
+ Bytecode :: BitAnd ,
227
+ Bytecode :: Xor ,
228
+ ] {
229
+ let code = vec ! [ Bytecode :: LdU8 ( 42 ) , Bytecode :: LdU64 ( 94 ) , instr] ;
230
+ let module = make_module ( code) ;
231
+ let fun_context = get_fun_context ( & module) ;
232
+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
233
+ assert_eq ! (
234
+ result. unwrap_err( ) . major_status( ) ,
235
+ StatusCode :: INTEGER_OP_TYPE_MISMATCH_ERROR
236
+ ) ;
237
+ }
238
+ }
239
+
240
+ #[ test]
241
+ fn test_arithmetic_wrong_type ( ) {
242
+ for instr in vec ! [
243
+ Bytecode :: Add ,
244
+ Bytecode :: Sub ,
245
+ Bytecode :: Mul ,
246
+ Bytecode :: Mod ,
247
+ Bytecode :: Div ,
248
+ Bytecode :: BitOr ,
249
+ Bytecode :: BitAnd ,
250
+ Bytecode :: Xor ,
251
+ ] {
252
+ let code = vec ! [ Bytecode :: LdTrue , Bytecode :: LdU64 ( 94 ) , instr. clone( ) ] ;
253
+ let module = make_module ( code) ;
254
+ let fun_context = get_fun_context ( & module) ;
255
+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
256
+ assert_eq ! (
257
+ result. unwrap_err( ) . major_status( ) ,
258
+ StatusCode :: INTEGER_OP_TYPE_MISMATCH_ERROR
259
+ ) ;
260
+
261
+ let code = vec ! [ Bytecode :: LdU32 ( 94 ) , Bytecode :: LdFalse , instr. clone( ) ] ;
262
+ let module = make_module ( code) ;
263
+ let fun_context = get_fun_context ( & module) ;
264
+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
265
+ assert_eq ! (
266
+ result. unwrap_err( ) . major_status( ) ,
267
+ StatusCode :: INTEGER_OP_TYPE_MISMATCH_ERROR
268
+ ) ;
269
+ }
270
+ }
271
+
272
+
273
+ #[ test]
274
+ #[ should_panic]
275
+ fn test_arithmetic_too_few_args ( ) {
276
+ for instr in vec ! [
277
+ Bytecode :: Add ,
278
+ Bytecode :: Sub ,
279
+ Bytecode :: Mul ,
280
+ Bytecode :: Mod ,
281
+ Bytecode :: Div ,
282
+ Bytecode :: BitOr ,
283
+ Bytecode :: BitAnd ,
284
+ Bytecode :: Xor ,
285
+ ] {
286
+ let code = vec ! [ Bytecode :: LdU16 ( 42 ) , instr. clone( ) ] ;
287
+ let module = make_module ( code) ;
288
+ let fun_context = get_fun_context ( & module) ;
289
+ let _result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
290
+
291
+ let code = vec ! [ instr. clone( ) ] ;
292
+ let module = make_module ( code) ;
293
+ let fun_context = get_fun_context ( & module) ;
294
+ let _result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
295
+ }
296
+ }
0 commit comments