• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2021 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: java_header
16    template: |
17      .language Java
18
19      .record java.lang.Object <external>
20      .record java.lang.String <external>
21      .record java.lang.Exception <external>
22      .record java.lang.RuntimeException <external>
23      .record java.lang.ClassCastException <external>
24      .record java.io.Serializable <external>
25
26      .record R <> {}
27      .record I <java.interface> {}
28      .record Q <java.extends=R, java.implements=I> {}
29      .record A <java.annotation, java.annotation.type=runtime> {}
30
31  - name: pandasm_header
32    template: |
33      .language PandaAssembly
34
35      .record panda.Object <external>
36      .record panda.String <external>
37      .record panda.ClassCastException <external>
38
39      .record R {}
40      .record Q {}
41
42
43tests:
44  - file-name: "checkcast"
45    isa:
46      title: Check cast
47      description: >
48        Resolve object type by specified id and if an object in accumulator can be cast to the
49        resolved type, accumulator content remains unchanged. Otherwise ClassCastException is thrown.
50      instructions:
51        - sig: checkcast type_id
52          acc: in:ref
53          format: [op_id_16]
54    commands:
55
56      - file-name: "null_to_any_type_pa"
57        tags: ['tsan']
58        description: Check that null object reference can be cast to any type
59        isa:
60          description: >
61            'null' object reference can be cast to every type.
62        header-template: ['pandasm_header']
63        check-type: empty
64        code-template: |
65
66          .function i32 main() {
67          try_begin:
68            lda.null
69            sta.obj v1
70            %s
71            jne.obj v1, try_end # check that acc == null
72            ldai 0
73            return
74          try_end:
75            ldai 2
76            return
77          .catch panda.ClassCastException, try_begin, try_end, try_end
78          }
79        cases:
80          - values:
81            - checkcast panda.Object
82          - values:
83            - checkcast panda.String
84          - values:
85            - checkcast R
86          - values:
87            - |
88              checkcast R
89              checkcast Q
90          - values:
91            - |
92              checkcast panda.ClassCastException
93              checkcast R
94
95
96      - file-name: "null_to_array_type_pa"
97        description: Check that null object reference can be cast to any array type
98        isa:
99          description: >
100            'null' object reference can be cast to every type.
101        header-template: ['pandasm_header']
102        check-type: empty
103        bugid: ["1710"]
104        code-template: |
105
106          .function i32 main() {
107          try_begin:
108            lda.null
109            sta.obj v1
110            %s
111            jne.obj v1, try_end # check that acc == null
112            ldai 0
113            return
114          try_end:
115            ldai 2
116            return
117          .catch panda.ClassCastException, try_begin, try_end, try_end
118          }
119        cases:
120          - values:
121            - checkcast panda.Object[]
122          - values:
123            - checkcast panda.String[]
124          - values:
125            - checkcast R[]
126          - values:
127            - |
128              checkcast R[]
129              checkcast Q[]
130          - values:
131            - |
132              checkcast panda.ClassCastException[]
133              checkcast R[]
134
135
136      - file-name: "against_base_type_pa"
137        description: Check that any object can be cast to the base type
138        isa:
139          description: Object of type O can be cast to type T if O is the same as T or is subtype of T.
140        header-template: ['pandasm_header']
141        check-type: empty
142        tags: ['tsan']
143        code-template: |
144
145          .function i32 main() {
146          try_begin:
147            %s
148            %s
149            checkcast panda.Object
150            jne.obj v1, try_end  # check that acc isn't changed
151            ldai 0
152            return
153          try_end:
154            ldai 2
155            return
156          .catch panda.ClassCastException, try_begin, try_end, try_end
157          }
158        cases:
159          - values:
160            - newobj v1, panda.ClassCastException
161            - lda.obj v1
162          - values:
163            - newobj v1, R
164            - lda.obj v1
165          - values:
166            - lda.str ""
167            - sta.obj v1
168          - values:
169            - lda.str "abc"
170            - sta.obj v1
171          - values:
172            - lda.type R
173            - sta.obj v1
174
175
176      - file-name: "against_same_type_pa"
177        description: Check that any object can be cast to its type
178        isa:
179          description: Object of type O can be cast to type T if O is the same as T or is subtype of T.
180        header-template: ['pandasm_header']
181        check-type: empty
182        code-template: |
183
184          .function i32 main() {
185          try_begin:
186            %s
187            %s
188            checkcast %s
189            jne.obj v1, try_end  # check that acc hasn't been changed
190            ldai 0
191            return
192          try_end:
193            ldai 2
194            return
195          .catch panda.ClassCastException, try_begin, try_end, try_end
196          }
197        cases:
198          - values:
199            - newobj v1, panda.ClassCastException
200            - lda.obj v1
201            - panda.ClassCastException
202          - values:
203            - newobj v1, R
204            - lda.obj v1
205            - R
206          - values:
207            - lda.str ""
208            - sta.obj v1
209            - panda.String
210          - values:
211            - lda.str "abc"
212            - sta.obj v1
213            - panda.String
214
215
216      - file-name: "array_against_base_type_pa"
217        description: Check that any array can be cast to base type
218        isa:
219          description: >
220            Object of type O can be cast to type T if O is the same as T or is subtype of T. For arrays O can be cast
221            to T if T is a root type in type hierarchy or T is such array that O array elements are the same or subtype of
222            T array elements.
223        header-template: ['pandasm_header']
224        check-type: empty
225        code-template: |
226
227          .function i32 main() {
228          try_begin:
229            movi v0, 10
230            newarr v1, v0, %s[]
231            lda.obj v1
232            checkcast panda.Object
233            jne.obj v1, try_end # check that acc isn't changed
234            ldai 0
235            return
236          try_end:
237            ldai 2
238            return
239          .catch panda.ClassCastException, try_begin, try_end, try_end
240          }
241        cases:
242          - values:
243            - panda.ClassCastException
244          - values:
245            - R
246          - values:
247            - panda.String
248
249
250      - file-name: "array_against_its_type_pa"
251        description: Check that any array can be cast to its type
252        isa:
253          description: >
254            For arrays O can be cast
255            to T if T is a root type in type hierarchy or T is such array that O array elements are the same or subtype of
256            T array elements.
257        header-template: ['pandasm_header']
258        check-type: empty
259        bugid: ["1710"]
260        code-template: |
261
262          .function i32 main() {
263          try_begin:
264            movi v0, 10
265            newarr v1, v0, %s[]
266            lda.obj v1
267            checkcast %s[]
268            jne.obj v1, try_end # check that acc isn't changed
269            ldai 0
270            return
271          try_end:
272            ldai 2
273            return
274          .catch panda.ClassCastException, try_begin, try_end, try_end
275          }
276        cases:
277          - values:
278            - panda.ClassCastException
279            - panda.ClassCastException
280          - values:
281            - R
282            - R
283          - values:
284            - panda.String
285            - panda.String
286
287
288      - file-name: "array_against_basetype_array_pa"
289        description: Check that any array can be cast to base type array
290        isa:
291          description: >
292            For arrays O can be cast
293            to T if T is a root type in type hierarchy or T is such array that O array elements are the same or subtype of
294            T array elements.
295        header-template: ['pandasm_header']
296        check-type: empty
297        bugid: ["1710"]
298        code-template: |
299
300          .function i32 main() {
301          try_begin:
302            movi v0, 10
303            newarr v1, v0, %s[]
304            lda.obj v1
305            checkcast panda.Object[]
306            jne.obj v1, try_end # check that acc isn't changed
307            ldai 0
308            return
309          try_end:
310            ldai 2
311            return
312          .catch panda.ClassCastException, try_begin, try_end, try_end
313          }
314        cases:
315          - values:
316            - panda.ClassCastException
317          - values:
318            - R
319          - values:
320            - panda.String
321
322
323      - file-name: "against_other_type_pa"
324        description: Check that ClassCastException is thrown when object cannot be cast to a type
325        isa:
326          description: >
327            Otherwise ClassCastException is thrown.
328            Object of type O can be cast to type T if O is the same as T or is subtype of T. For arrays O can be cast
329            to T if T is a root type in type hierarchy or T is such array that O array elements are the same or subtype of
330            T array elements.
331          exceptions:
332            - x_cast
333        header-template: ['pandasm_header']
334        check-type: empty
335        code-template: |
336
337          .function i32 main() {
338          try_begin:
339            %s
340            checkcast %s
341            ldai 1
342            return
343          try_end:
344            ldai 0
345            return
346          .catch panda.ClassCastException, try_begin, try_end, try_end
347          }
348        cases:
349          - values:
350            - |
351              newobj v0, Q
352              lda.obj v0
353            - Q[]
354            bugid: ["1710"]
355          - values:
356            - |
357              newobj v0, Q
358              lda.obj v0
359            - R[]
360            bugid: ["1710"]
361          - values:
362            - |
363              newobj v0, R
364              lda.obj v0
365            - Q
366          - values:
367            - lda.str ""
368            - panda.ClassCastException
369          - values:
370            - lda.str "abc"
371            - R
372          - values:
373            - lda.type R
374            - R
375
376
377      - file-name: "array_against_other_type_pa"
378        description: Check that ClassCastException is thrown when array cannot be cast to a type
379        isa:
380          description: >
381            Otherwise ClassCastException is thrown.
382            Object of type O can be cast to type T if O is the same as T or is subtype of T. For arrays O can be cast
383            to T if T is a root type in type hierarchy or T is such array that O array elements are the same or subtype of
384            T array elements.
385          exceptions:
386            - x_cast
387        header-template: ['pandasm_header']
388        check-type: empty
389        code-template: |
390
391          .function i32 main() {
392          try_begin:
393            movi v0, 10
394            newarr v0, v0, %s
395            lda.obj v0
396            checkcast %s
397            ldai 1
398            return
399          try_end:
400            ldai 0
401            return
402          .catch panda.ClassCastException, try_begin, try_end, try_end
403          }
404        cases:
405          - values:
406            - Q[]
407            - Q
408          - values:
409            - Q[]
410            - R
411          - values:
412            - R[]
413            - Q[]
414            bugid: ["1710"]
415          - values:
416            - panda.String[]
417            - panda.ClassCastException[]
418            bugid: ["1710"]
419
420
421      - file-name: "with_wrong_acc_value_pa"
422        description: Check that verification fails when accumulator contains wrong value, not an object or null
423        isa:
424          verification:
425            - acc_obj_or_null
426        header-template: ['pandasm_header']
427        check-type: empty
428        tags: ['verifier']
429        runner-options: ['verifier-failure', 'verifier-debug-config']
430        code-template: |
431
432          .function i32 main() {
433            %s
434            checkcast panda.Object
435            ldai 0
436            return
437          }
438        cases:
439          - values:
440            - ldai -1
441          - values:
442            - ldai.64 1
443          - values:
444            - fldai.64 -1.1
445
446
447      - file-name: "with_wrong_type_id_pa"
448        description: Check that verification fails when accumulator contains wrong value, not an object or null
449        isa:
450          verification:
451            - type_id_any_object
452        header-template: ['pandasm_header']
453        check-type: empty
454        runner-options: ['compile-failure']
455        code-template: |
456
457          .function i32 main() {
458            newobj v0, panda.Object
459            lda.obj v0
460            checkcast %s
461            ldai 0
462            return
463          }
464        cases:
465          - values:
466            - "0x7fffffff"
467          - values:
468            - "0x1234567890"
469          - values:
470            - 3.1415926
471          - values:
472            - "\"abc\""
473
474
475