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