1# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14definitions:
15  - name: PandaAssembly
16    template: |
17      .language PandaAssembly
18  - name: aoobe_p
19    template: |
20      .record panda.ArrayIndexOutOfBoundsException<external>
21  - name: aoobe_j
22    template: |
23      .record panda.ArrayIndexOutOfBoundsException<external>
24  - name: npe_p
25    template: |
26      .record panda.NullPointerException <external>
27  - name: npe_j
28    template: |
29      .record panda.NullPointerException <external>
30tests:
31  - file-name: "ldarru.8"
32    isa:
33      title: Load from array
34      description: >
35        Load an element from array using accumulator as an index and puts it into accumulator.
36        If element size is smaller then 32 bits, it will be zero or sign extended (depending on bytecode) to i32.
37    commands:
38      - file-name: "read_arr_p"
39        isa:
40          instructions:
41            - sig: ldarru.8 v:in:u8[]
42              acc: inout:i32
43              format: [op_v_8]
44        description: Check ldarru.8 reads items from array of different size and type in Panda Assembly context.
45        code-template: |
46          #
47              movi v0, *s
48              newarr v0, v0, %s
49              ldai 0
50              ldarru.8 v0
51        check-type: exit-positive
52        template-cases:
53          - values: ['u1[]']
54          - values: ['u8[]']
55        cases:
56          - values: ['1']
57          - values: ['255']
58            tags: ['tsan']
59          - values: ['65536']
60
61      - file-name: "read_arr_j"
62        isa:
63          instructions:
64            - sig: ldarru.8 v:in:u8[]
65              acc: inout:i32
66              format: [op_v_8]
67        description: Check ldarru.8 reads items from array of different size and type in PandaAssembly context.
68        header-template: [PandaAssembly, main]
69        code-template: |
70          #
71              movi v0, *s
72              newarr v0, v0, %s
73              ldai 0
74              ldarru.8 v0
75        check-type: exit-positive
76        runner-options: ['use-pa']
77        template-cases:
78          - values: ['u1[]']
79        cases:
80          - values: ['1']
81          - values: ['255']
82            tags: ['tsan']
83          - values: ['65536']
84
85      - file-name: "primitive_types_p"
86        isa:
87          instructions:
88            - sig: ldarru.8 v:in:u8[]
89              acc: inout:i32
90              format: [op_v_8]
91        tags: ['tsan', 'verifier']
92        bugid: ['3052']
93        description: Check array of primitive types for 'ldarru.8' instruction in Panda Assembly context.
94        header-template: []
95        code-template: |
96          .array arr_u1 u1 1 { 1 }
97          .array arr_i8 i8 1 { 1 }
98          .array arr_u8 u8 1 { 1 }
99          .array arr_i16 i16 1 { 1 }
100          .array arr_u16 u16 1 { 1 }
101          .array arr_i32 i32 1 { 1 }
102          .array arr_u32 u32 1 { 1 }
103          .array arr_i64 i64 1 { 1 }
104          .array arr_u64 u64 1 { 1 }
105          .array arr_f32 f32 1 { 1 }
106          .array arr_f64 f64 1 { 1 }
107          .function i32 main() {
108              %s
109              ldai 0
110              ldarru.8 v0
111        check-type: exit-positive
112        runner-options: ['verifier-failure', 'verifier-config']
113        template-cases:
114          - values:
115            - |
116              #
117                  movi v0, 1
118                  newarr v0, v0, %s[]
119          - values:
120            - |
121              #
122                  lda.const v0, arr_%s
123        cases:
124          - values: [u1]
125            runner-options: [verifier-only, verifier-config]
126          - values: [i8]
127          - values: [u8]
128            runner-options: [verifier-only, verifier-config]
129            bugid: ['7431']
130          - values: [i16]
131          - values: [u16]
132          - values: [i32]
133          - values: [u32]
134          - values: [i64]
135          - values: [u64]
136          - values: [f32]
137          - values: [f64]
138
139      - file-name: "primitive_types_j"
140        isa:
141          instructions:
142            - sig: ldarru.8 v:in:u8[]
143              acc: inout:i32
144              format: [op_v_8]
145        tags: ['tsan', 'verifier', 'pa-verifier']
146        bugid: ['3052']
147        description: Check array of primitive types for 'ldarru.8' instruction in PandaAssembly context.
148        header-template: [PandaAssembly]
149        code-template: |
150          .array arr_u1 u1 1 { 1 }
151          .array arr_i8 i8 1 { 1 }
152          .array arr_i16 i16 1 { 1 }
153          .array arr_u16 u16 1 { 1 }
154          .array arr_i32 i32 1 { 1 }
155          .array arr_i64 i64 1 { 1 }
156          .array arr_f32 f32 1 { 1 }
157          .array arr_f64 f64 1 { 1 }
158          .function i32 main() {
159              %s
160              ldai 0
161              ldarru.8 v0
162        check-type: exit-positive
163        runner-options: [verifier-failure, verifier-config, use-pa]
164        template-cases:
165          - values:
166            - |
167              #
168                  movi v0, 1
169                  newarr v0, v0, %s[]
170          - values:
171            - |
172              #
173                  lda.const v0, arr_%s
174        cases:
175          - values: [u1]
176            runner-options: [verifier-only, verifier-config, use-pa]
177          - values: [i8]
178          - values: [i16]
179          - values: [u16]
180          - values: [i32]
181          - values: [i64]
182          - values: [f32]
183          - values: [f64]
184
185      - file-name: "read_values"
186        isa:
187          instructions:
188            - sig: ldarru.8 v:in:u8[]
189              acc: inout:i32
190              format: [op_v_8]
191        description: Check ldarru.8 reads correct items from array of primitives.
192        header-template: [xorshift32, main]
193        code-template: |
194          #
195              movi v3, 10000 # iterations
196              movi v2, *s    # index
197              movi v0, *s
198                             # array size
199              newarr v1, v0, %s     # v1 - testable array
200
201              movi v0, 1     # initial random number
202          loop:
203              call.short nextRand, v0
204              sta v0         # v0 - next random number
205          #
206              starr.8 v1, v2 # save random number in v1[v2]
207              lda v2
208              ldarru.8 v1     # get v1[v2]
209              andi 255       # Keep only low 8 bits
210              sta v4         # v4 = v1[v2]
211
212              lda v0
213              andi 255       # acc = v0 & 255 (random number)
214              ucmp v4        # Compare random number and stored value from array
215              jeqz passed
216              ldai 1
217              return
218          passed:
219              inci v3, -1
220              lda v3
221              jnez loop
222        check-type: exit-positive
223        template-cases:
224          - values: ['u1[]']
225          - values: ['u8[]']
226        cases:
227          - values: ['0', '1']
228          - values: ['254', '255']
229          - values: ['65535', '65536']
230          - values: ['7', '16']
231          - values: ['123', '255']
232          - values: ['49151', '65536']
233
234      - file-name: "read_all_values"
235        isa:
236          instructions:
237            - sig: ldarru.8 v:in:u8[]
238              acc: inout:i32
239              format: [op_v_8]
240        description: Check ldarru.8 reads correct items from array of primitives. Inspect different indexes and values.
241        code-template: |
242          #
243              movi v1, 0     # v1 - index
244              movi v0, *s
245                             # v0 is array size
246              mov v2, v0     # v2 is size too
247              newarr v0, v0, %s     # v0 - testable array
248          fill_array:
249              lda v1
250              starr.8 v0, v1 # v0[v1] = v1
251              addi 1
252              sta v1         # v1 = v1 + 1
253              jne v2, fill_array
254              movi v1, 0     # index
255          check_array:
256              lda v1
257              ldarru.8 v0
258              andi 255
259              sta v3
260              lda v1
261              andi 255
262              ucmp v3
263              jeqz ok
264              ldai 1
265              return
266          ok:
267              inci v1, 1
268              lda v1
269              jne v2, check_array
270        check-type: exit-positive
271        template-cases:
272          - values: ['u1[]']
273          - values: ['u8[]']
274        cases:
275          - values: ['100']
276          - values: ['255']
277            tags: ['tsan']
278          - values: ['65535']
279            tags: ['tsan']
280
281      - file-name: "reg_number"
282        isa:
283          instructions:
284            - sig: ldarru.8 v:in:u8[]
285              acc: inout:i32
286              format: [op_v_8]
287        check-type: empty
288        runner-options: [compile-only]
289        description: Check 'ldarru.8' instruction with different registers numbers.
290        header-template: []
291        code-template: |
292          #
293          .function i32 main() {
294              ldarru.8 %s
295          }
296        cases:
297          - values: [v0]
298          - values: [v16]
299          - values: [v128]
300          - values: [v255]
301          - values: [v256]
302            runner-options: [compile-failure]
303          - values: [v65535]
304            runner-options: [compile-failure]
305          - case-template: |
306              #
307              .function void f1(i32 a0) {
308                  ldarru.8 a0 # valid name of register
309              }
310
311              .function i32 main() {
312                  movi v0, 0
313                  call.short f1, v0
314              }
315          - case-template: |
316              #
317              .function void f1(i32 a0) {
318                  ldarru.8 a1 # invalid name of register
319              }
320
321              .function i32 main() {
322                  movi v0, 0
323                  call.short f1, v0
324              }
325            runner-options: [compile-failure]
326          - values: [a0]
327            runner-options: [compile-failure]
328          - values: [a255]
329            runner-options: [compile-failure]
330          - values: ['null']
331            runner-options: [compile-failure]
332          - values: [0]
333            runner-options: [compile-failure]
334          - values: [1.1]
335            runner-options: [compile-failure]
336          - values: ['2.2']
337            runner-options: [compile-failure]
338
339      - file-name: "arr_type"
340        isa:
341          instructions:
342            - sig: ldarru.8 v:in:u8[]
343              acc: inout:i32
344              format: [op_v_8]
345          verification:
346            - v1_array_type
347        tags: ['verifier']
348        bugid: ['2816']
349        runner-options: ['verifier-failure', 'verifier-config']
350        header-template: []
351        code-template: |
352          #
353          .record A {}
354          .record panda.String <external>
355          .record panda.Object <external>
356          .function i32 main() {
357              %s
358              ldai 0
359              ldarru.8 v0
360        check-type: exit-positive
361        description: Check 'ldarru.8' with incorrect array type. See also "rejectable_primitive_types" tests.
362        cases:
363          - values:
364              - movi v0, 0
365          - values:
366              - movi.64 v0, 0
367          - values:
368              - fmovi v0, 0
369          - values:
370              - fmovi.64 v0, 0
371          - values:
372              - |
373                #
374                    lda.type A
375                    sta.obj v0
376          - values:
377              - |
378                #
379                    lda.type A[]
380                    sta.obj v0
381          - values:
382              - |
383                #
384                    lda.type panda.String
385                    sta.obj v0
386          - values:
387              - |
388                #
389                    lda.type panda.String[]
390                    sta.obj v0
391          - values:
392              - |
393                #
394                    lda.type panda.Object
395                    sta.obj v0
396          - values:
397              - |
398                #
399                    lda.type panda.Object[]
400                    sta.obj v0
401          - values:
402              - |
403                #
404                    lda.str "string"
405                    sta.obj v0
406          - values:
407              - |
408                #
409                    movi v0, 1
410                    newarr v0, v0, panda.Object[]
411          - values:
412              - |
413                #
414                    movi v0, 1
415                    newarr v0, v0, panda.String[]
416
417      - file-name: "acc_type"
418        isa:
419          instructions:
420            - sig: ldarru.8 v:in:u8[]
421              acc: inout:i32
422              format: [op_v_8]
423          verification:
424            - acc_i32
425        tags: ['verifier']
426        bugid: ['2817']
427        runner-options: ['verifier-failure', 'verifier-config']
428        header-template: []
429        code-template: |
430          #
431          .record A {}
432          .record panda.String <external>
433          .record panda.Object <external>
434          .function i32 main() {
435              movi v0, 1
436              newarr v0, v0, u8[]
437              %s
438              ldarru.8 v0
439        check-type: exit-positive
440        description: Check 'ldarru.8' with incorrect index type.
441        cases:
442          - values:
443              - ldai.64 0
444          - values:
445              - fldai 0
446          - values:
447              - fldai.64 0
448          - values:
449              - lda.null
450          - values:
451              - |
452                #
453                    lda.type A
454          - values:
455              - |
456                #
457                    lda.type A[]
458          - values:
459              - |
460                #
461                    lda.type panda.String
462          - values:
463              - |
464                #
465                    lda.type panda.String[]
466          - values:
467              - |
468                #
469                    lda.type panda.Object
470          - values:
471              - |
472                #
473                    lda.type panda.Object[]
474          - values:
475              - |
476                #
477                    lda.str "string"
478          - values:
479              - |
480                #
481                    movi v1, 1
482                    newarr v1, v1, panda.Object[]
483                    lda.obj v1
484          - values:
485              - |
486                #
487                    movi v1, 1
488                    newarr v1, v1, panda.String[]
489                    lda.obj v1
490
491      - file-name: "arr_acc_type"
492        isa:
493          instructions:
494            - sig: ldarru.8 v:in:u8[]
495              acc: inout:i32
496              format: [op_v_8]
497          verification:
498            - v1_array_type
499        tags: ['verifier']
500        bugid: ['2816', '2817']
501        runner-options: ['verifier-failure', 'verifier-config']
502        header-template: []
503        code-template: |
504          #
505          .record A {}
506          .record panda.String <external>
507          .record panda.Object <external>
508          .function i32 main() {
509              %s
510              *s
511              ldarru.8 v0
512        check-type: exit-positive
513        description: Check 'ldarru.8' with incorrect register and accumulator types.
514        template-cases:
515          - values:
516              - movi v0, 0
517          - values:
518              - movi.64 v0, 0
519          - values:
520              - fmovi v0, 0
521          - values:
522              - fmovi.64 v0, 0
523          - values:
524              - |
525                #
526                    lda.type A
527                    sta.obj v0
528          - values:
529              - |
530                #
531                    lda.type A[]
532                    sta.obj v0
533          - values:
534              - |
535                #
536                    lda.type panda.String
537                    sta.obj v0
538          - values:
539              - |
540                #
541                    lda.type panda.String[]
542                    sta.obj v0
543          - values:
544              - |
545                #
546                    lda.type panda.Object
547                    sta.obj v0
548          - values:
549              - |
550                #
551                    lda.type panda.Object[]
552                    sta.obj v0
553          - values:
554              - |
555                #
556                    lda.str "string"
557                    sta.obj v0
558          - values:
559              - |
560                #
561                    movi v0, 1
562                    newarr v0, v0, panda.Object[]
563          - values:
564              - |
565                #
566                    movi v0, 1
567                    newarr v0, v0, panda.String[]
568        cases:
569          - values:
570              - ldai 0
571          - values:
572              - ldai.64 0
573          - values:
574              - fldai 0
575          - values:
576              - fldai.64 0
577          - values:
578              - lda.null
579          - values:
580              - |
581                #
582                    lda.type A
583          - values:
584              - |
585                #
586                    lda.type A[]
587          - values:
588              - |
589                #
590                    lda.type panda.String
591          - values:
592              - |
593                #
594                    lda.type panda.String[]
595          - values:
596              - |
597                #
598                    lda.type panda.Object
599          - values:
600              - |
601                #
602                    lda.type panda.Object[]
603          - values:
604              - |
605                #
606                    lda.str "string"
607          - values:
608              - |
609                #
610                    movi v1, 1
611                    newarr v1, v1, panda.Object[]
612                    lda.obj v1
613          - values:
614              - |
615                #
616                    movi v1, 1
617                    newarr v1, v1, panda.String[]
618                    lda.obj v1
619
620      - file-name: uninitialized_acc_regs
621        isa:
622          instructions:
623            - sig: ldarru.8 v:in:u8[]
624              acc: inout:i32
625              format: [op_v_8]
626        description: Check 'ldarru.8' with uninitialized register and accumulator.
627        tags: ['verifier']
628        bugid: ['2818']
629        runner-options: ['verifier-failure', 'verifier-config']
630        code-template: |
631            #
632                %s
633                ldarru.8 v0
634        check-type: exit-positive
635        cases:
636          - values: ['ldai 0']
637          - values:
638            - |
639              #
640                  movi v0, 1
641                  newarr v0, v0, u8[]
642          - values: ['']
643
644      - file-name: array_out_of_bound_exception_p
645        isa:
646          instructions:
647            - sig: ldarru.8 v:in:u8[]
648              acc: inout:i32
649              format: [op_v_8]
650          exceptions:
651            - x_bounds
652        description: Check 'ldarru.8' behavior when index is out of array bounds.
653        header-template: [aoobe_p, main]
654        tags: ['irtoc_ignore']
655        code-template: |
656          #
657              movi v0, *s
658              newarr v0, v0, %s
659              ldai *s
660          begin:
661              ldarru.8 v0
662          end:
663              ldai 1 # Should not reach this line
664              return
665
666          catch_AOOBE:
667              ldai 0 # Expected panda.ArrayIndexOutOfBoundsException
668              return
669
670          catch_all:
671              ldai 2 # Unexpected exception, test failed
672              return
673
674          .catch panda.ArrayIndexOutOfBoundsException, begin, end, catch_AOOBE
675          .catchall begin, end, catch_all
676        check-type: none
677        template-cases:
678            - values: ['u1[]']
679            - values: ['u8[]']
680        cases:
681          - values: [0, 0]
682            tags: ['tsan']
683          - values: [0, 1]
684          - values: [10, 10]
685          - values: [10, 128]
686          - values: [255, 255]
687          - values: [254, 255]
688          - values: [65535, 65535]
689            tags: ['tsan']
690          - values: [65535, 65536]
691          - values: [10, 0xFFFFFFFF]
692          - values: [256, 0xFFFFFFFE]
693          - values: [65536, 0xFFFFFFFD]
694            tags: ['tsan']
695          - values: [0x100000, 0xFFFFFFFC]
696          - values: [10, 0x80000000]
697          - values: [256, 0x80000001]
698          - values: [65536, 0x80000002]
699            tags: ['tsan']
700          - values: [0x100000, 0x80000003]
701
702      - file-name: array_out_of_bound_exception_j
703        isa:
704          instructions:
705            - sig: ldarru.8 v:in:u8[]
706              acc: inout:i32
707              format: [op_v_8]
708          exceptions:
709            - x_bounds
710        description: Check 'ldarru.8' behavior when index is out of array bounds.
711        tags: ['irtoc_ignore']
712        runner-options: [use-pa]
713        header-template: [PandaAssembly, aoobe_j, main]
714        code-template: |
715          #
716              movi v0, %s
717              newarr v0, v0, u1[] # The only available type in PandaAssembly context for ldarru.8
718              ldai %s
719          begin:
720              ldarru.8 v0
721          end:
722              ldai 1 # Should not reach this line
723              return
724
725          catch_AOOBE:
726              ldai 0 # Expected panda.ArrayIndexOutOfBoundsException
727              return
728
729          catch_all:
730              ldai 2 # Unexpected exception, test failed
731              return
732
733          .catch panda.ArrayIndexOutOfBoundsException, begin, end, catch_AOOBE
734          .catchall begin, end, catch_all
735        check-type: none
736        cases:
737          - values: [0, 0]
738            tags: ['tsan']
739          - values: [0, 1]
740          - values: [10, 10]
741          - values: [10, 128]
742          - values: [255, 255]
743          - values: [254, 255]
744          - values: [65535, 65535]
745            tags: ['tsan']
746          - values: [65535, 65536]
747          - values: [10, 0xFFFFFFFF]
748          - values: [256, 0xFFFFFFFE]
749          - values: [65536, 0xFFFFFFFD]
750            tags: ['tsan']
751          - values: [0x100000, 0xFFFFFFFC]
752          - values: [10, 0x80000000]
753          - values: [256, 0x80000001]
754          - values: [65536, 0x80000002]
755            tags: ['tsan']
756          - values: [0x100000, 0x80000003]
757
758      - file-name: null_pointer_p
759        isa:
760          instructions:
761            - sig: ldarru.8 v:in:u8[]
762              acc: inout:i32
763              format: [op_v_8]
764          exceptions:
765            - x_null
766        description: Check 'ldarru.8' behavior when array is null reference.
767        tags: ['irtoc_ignore']
768        header-template: [npe_p]
769        code-template: |
770          .function %s get_null() {
771              lda.null
772              return.obj
773          }
774
775          .function i32 main() {
776              call.short get_null
777              sta.obj v0
778              ldai *s
779          begin:
780              ldarru.8 v0
781          end:
782              ldai 1 # Should not reach this line
783              return
784
785          catch_NPE:
786              ldai 0 # Expected panda.NullPointerException
787              return
788
789          catch_all:
790              ldai 2 # Unexpected exception, test failed
791              return
792
793          .catch panda.NullPointerException, begin, end, catch_NPE
794          .catchall begin, end, catch_all
795        check-type: none
796        template-cases:
797          - values: ['u1[]']
798          - values: ['u8[]']
799        cases:
800          - values: [0]
801            tags: ['tsan']
802          - values: [1]
803          - values: [10]
804          - values: [128]
805          - values: [255]
806          - values: [65535]
807            tags: ['tsan']
808          - values: [0x7FFFFFFF]
809          - values: [0xFFFFFFFF]
810          - values: [0x80000000]
811
812      - file-name: null_pointer_j
813        isa:
814          instructions:
815            - sig: ldarru.8 v:in:u8[]
816              acc: inout:i32
817              format: [op_v_8]
818          exceptions:
819            - x_null
820        description: Check 'ldarru.8' behavior when array is null reference.
821        tags: ['irtoc_ignore']
822        header-template: [PandaAssembly, npe_j]
823        runner-options: [use-pa]
824        bugid: ['3047']
825        code-template: |
826          .function u1[] get_null() {
827              lda.null
828              return.obj
829          }
830
831          .function i32 main() {
832              call.short get_null
833              sta.obj v0
834              ldai %s
835          begin:
836              ldarru.8 v0
837          end:
838              ldai 1 # Should not reach this line
839              return
840
841          catch_NPE:
842              ldai 0 # Expected panda.NullPointerException
843              return
844
845          catch_all:
846              ldai 2 # Unexpected exception, test failed
847              return
848
849          .catch panda.NullPointerException, begin, end, catch_NPE
850          .catchall begin, end, catch_all
851        check-type: none
852        cases:
853          - values: [0]
854            tags: ['tsan']
855          - values: [1]
856          - values: [10]
857          - values: [128]
858          - values: [255]
859          - values: [65535]
860            tags: ['tsan']
861          - values: [0x7FFFFFFF]
862          - values: [0xFFFFFFFF]
863          - values: [0x80000000]
864