xref: /aosp_15_r20/art/test/712-varhandle-invocations/src/VarHandleAccessorExceptionTests.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2018 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker import java.lang.invoke.MethodHandles;
18*795d594fSAndroid Build Coastguard Worker import java.lang.invoke.VarHandle;
19*795d594fSAndroid Build Coastguard Worker import java.lang.invoke.WrongMethodTypeException;
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker // These tests cover DoVarHandleInvokeCommon in interpreter_common.cc.
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker public class VarHandleAccessorExceptionTests {
24*795d594fSAndroid Build Coastguard Worker     public static class NullReceiverTest extends VarHandleUnitTest {
25*795d594fSAndroid Build Coastguard Worker         private static final VarHandle vh = null;
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker         @Override
doTest()28*795d594fSAndroid Build Coastguard Worker         protected void doTest() {
29*795d594fSAndroid Build Coastguard Worker             try {
30*795d594fSAndroid Build Coastguard Worker                 vh.set(3);
31*795d594fSAndroid Build Coastguard Worker                 failUnreachable();
32*795d594fSAndroid Build Coastguard Worker             } catch (NullPointerException ex) {
33*795d594fSAndroid Build Coastguard Worker             }
34*795d594fSAndroid Build Coastguard Worker         }
35*795d594fSAndroid Build Coastguard Worker 
main(String[] args)36*795d594fSAndroid Build Coastguard Worker         public static void main(String[] args) {
37*795d594fSAndroid Build Coastguard Worker             new NullReceiverTest().run();
38*795d594fSAndroid Build Coastguard Worker         }
39*795d594fSAndroid Build Coastguard Worker     }
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker     public static class UnsupportedAccessModeTest extends VarHandleUnitTest {
42*795d594fSAndroid Build Coastguard Worker         private static final boolean b = true;
43*795d594fSAndroid Build Coastguard Worker         private static final VarHandle vh;
44*795d594fSAndroid Build Coastguard Worker 
45*795d594fSAndroid Build Coastguard Worker         static {
46*795d594fSAndroid Build Coastguard Worker             try {
47*795d594fSAndroid Build Coastguard Worker                 Class<?> cls = UnsupportedAccessModeTest.class;
48*795d594fSAndroid Build Coastguard Worker                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "b", boolean.class);
49*795d594fSAndroid Build Coastguard Worker             } catch (Exception e) {
50*795d594fSAndroid Build Coastguard Worker                 throw new RuntimeException(e);
51*795d594fSAndroid Build Coastguard Worker             }
52*795d594fSAndroid Build Coastguard Worker         }
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker         @Override
doTest()55*795d594fSAndroid Build Coastguard Worker         protected void doTest() {
56*795d594fSAndroid Build Coastguard Worker             // A final field should not support an VarHandle access modes which can update it
57*795d594fSAndroid Build Coastguard Worker             boolean isSupported =
58*795d594fSAndroid Build Coastguard Worker                     vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND);
59*795d594fSAndroid Build Coastguard Worker             assertFalse(isSupported);
60*795d594fSAndroid Build Coastguard Worker             try {
61*795d594fSAndroid Build Coastguard Worker                 vh.getAndBitwiseAnd(true);
62*795d594fSAndroid Build Coastguard Worker                 failUnreachable();
63*795d594fSAndroid Build Coastguard Worker             } catch (UnsupportedOperationException ex) {
64*795d594fSAndroid Build Coastguard Worker             }
65*795d594fSAndroid Build Coastguard Worker         }
66*795d594fSAndroid Build Coastguard Worker 
main(String[] args)67*795d594fSAndroid Build Coastguard Worker         public static void main(String[] args) {
68*795d594fSAndroid Build Coastguard Worker             new UnsupportedAccessModeTest().run();
69*795d594fSAndroid Build Coastguard Worker         }
70*795d594fSAndroid Build Coastguard Worker     }
71*795d594fSAndroid Build Coastguard Worker 
72*795d594fSAndroid Build Coastguard Worker     public static class WrongArgumentTypeCausingWrongMethodTypeTest extends VarHandleUnitTest {
73*795d594fSAndroid Build Coastguard Worker         private short s;
74*795d594fSAndroid Build Coastguard Worker         private static final VarHandle vh;
75*795d594fSAndroid Build Coastguard Worker 
76*795d594fSAndroid Build Coastguard Worker         static {
77*795d594fSAndroid Build Coastguard Worker             try {
78*795d594fSAndroid Build Coastguard Worker                 Class<?> cls = WrongArgumentTypeCausingWrongMethodTypeTest.class;
79*795d594fSAndroid Build Coastguard Worker                 vh = MethodHandles.lookup().findVarHandle(cls, "s", short.class);
80*795d594fSAndroid Build Coastguard Worker             } catch (Exception e) {
81*795d594fSAndroid Build Coastguard Worker                 throw new RuntimeException(e);
82*795d594fSAndroid Build Coastguard Worker             }
83*795d594fSAndroid Build Coastguard Worker         }
84*795d594fSAndroid Build Coastguard Worker 
85*795d594fSAndroid Build Coastguard Worker         @Override
doTest()86*795d594fSAndroid Build Coastguard Worker         protected void doTest() {
87*795d594fSAndroid Build Coastguard Worker             vh.set(this, (short) 0xcafe);
88*795d594fSAndroid Build Coastguard Worker             try {
89*795d594fSAndroid Build Coastguard Worker                 vh.setVolatile(this, System.out); // System.out is a PrintStream, not short!
90*795d594fSAndroid Build Coastguard Worker                 failUnreachable();
91*795d594fSAndroid Build Coastguard Worker             } catch (WrongMethodTypeException ex) {
92*795d594fSAndroid Build Coastguard Worker             }
93*795d594fSAndroid Build Coastguard Worker         }
94*795d594fSAndroid Build Coastguard Worker 
main(String[] args)95*795d594fSAndroid Build Coastguard Worker         public static void main(String[] args) {
96*795d594fSAndroid Build Coastguard Worker             new WrongArgumentTypeCausingWrongMethodTypeTest().run();
97*795d594fSAndroid Build Coastguard Worker         }
98*795d594fSAndroid Build Coastguard Worker     }
99*795d594fSAndroid Build Coastguard Worker 
100*795d594fSAndroid Build Coastguard Worker     // Too many arguments causing WMTE
101*795d594fSAndroid Build Coastguard Worker     public static class TooManyArgumentsCausingWrongMethodTypeTest extends VarHandleUnitTest {
102*795d594fSAndroid Build Coastguard Worker         private int i;
103*795d594fSAndroid Build Coastguard Worker         private static final VarHandle vh;
104*795d594fSAndroid Build Coastguard Worker 
105*795d594fSAndroid Build Coastguard Worker         static {
106*795d594fSAndroid Build Coastguard Worker             try {
107*795d594fSAndroid Build Coastguard Worker                 Class<?> cls = TooManyArgumentsCausingWrongMethodTypeTest.class;
108*795d594fSAndroid Build Coastguard Worker                 vh = MethodHandles.lookup().findVarHandle(cls, "i", int.class);
109*795d594fSAndroid Build Coastguard Worker             } catch (Exception e) {
110*795d594fSAndroid Build Coastguard Worker                 throw new RuntimeException(e);
111*795d594fSAndroid Build Coastguard Worker             }
112*795d594fSAndroid Build Coastguard Worker         }
113*795d594fSAndroid Build Coastguard Worker 
114*795d594fSAndroid Build Coastguard Worker         @Override
doTest()115*795d594fSAndroid Build Coastguard Worker         protected void doTest() {
116*795d594fSAndroid Build Coastguard Worker             vh.set(this, 0x12345678);
117*795d594fSAndroid Build Coastguard Worker             try {
118*795d594fSAndroid Build Coastguard Worker                 vh.setVolatile(this, 0x5a5a55aa, 0xc3c30f0f);
119*795d594fSAndroid Build Coastguard Worker                 failUnreachable();
120*795d594fSAndroid Build Coastguard Worker             } catch (WrongMethodTypeException ex) {
121*795d594fSAndroid Build Coastguard Worker             }
122*795d594fSAndroid Build Coastguard Worker         }
123*795d594fSAndroid Build Coastguard Worker 
main(String[] args)124*795d594fSAndroid Build Coastguard Worker         public static void main(String[] args) {
125*795d594fSAndroid Build Coastguard Worker             new TooManyArgumentsCausingWrongMethodTypeTest().run();
126*795d594fSAndroid Build Coastguard Worker         }
127*795d594fSAndroid Build Coastguard Worker     }
128*795d594fSAndroid Build Coastguard Worker 
129*795d594fSAndroid Build Coastguard Worker     public static class TooFewArgumentsCausingWrongMethodTypeTest extends VarHandleUnitTest {
130*795d594fSAndroid Build Coastguard Worker         private int i;
131*795d594fSAndroid Build Coastguard Worker         private static final VarHandle vh;
132*795d594fSAndroid Build Coastguard Worker 
133*795d594fSAndroid Build Coastguard Worker         static {
134*795d594fSAndroid Build Coastguard Worker             try {
135*795d594fSAndroid Build Coastguard Worker                 Class<?> cls = TooFewArgumentsCausingWrongMethodTypeTest.class;
136*795d594fSAndroid Build Coastguard Worker                 vh = MethodHandles.lookup().findVarHandle(cls, "i", int.class);
137*795d594fSAndroid Build Coastguard Worker             } catch (Exception e) {
138*795d594fSAndroid Build Coastguard Worker                 throw new RuntimeException(e);
139*795d594fSAndroid Build Coastguard Worker             }
140*795d594fSAndroid Build Coastguard Worker         }
141*795d594fSAndroid Build Coastguard Worker 
142*795d594fSAndroid Build Coastguard Worker         @Override
doTest()143*795d594fSAndroid Build Coastguard Worker         protected void doTest() {
144*795d594fSAndroid Build Coastguard Worker             i = 33;
145*795d594fSAndroid Build Coastguard Worker             vh.compareAndSet(this, 33, 44);
146*795d594fSAndroid Build Coastguard Worker             boolean updated = false;
147*795d594fSAndroid Build Coastguard Worker             try {
148*795d594fSAndroid Build Coastguard Worker                 updated = (boolean) vh.compareAndSet(this, 44);
149*795d594fSAndroid Build Coastguard Worker                 failUnreachable();
150*795d594fSAndroid Build Coastguard Worker             } catch (WrongMethodTypeException ex) {
151*795d594fSAndroid Build Coastguard Worker             }
152*795d594fSAndroid Build Coastguard Worker             assertFalse(updated); // Should have failed too few arguments
153*795d594fSAndroid Build Coastguard Worker         }
154*795d594fSAndroid Build Coastguard Worker 
main(String[] args)155*795d594fSAndroid Build Coastguard Worker         public static void main(String[] args) {
156*795d594fSAndroid Build Coastguard Worker             new TooFewArgumentsCausingWrongMethodTypeTest().run();
157*795d594fSAndroid Build Coastguard Worker         }
158*795d594fSAndroid Build Coastguard Worker     }
159*795d594fSAndroid Build Coastguard Worker 
160*795d594fSAndroid Build Coastguard Worker     public static class ReturnTypeCausingWrongMethodTypeTest extends VarHandleUnitTest {
161*795d594fSAndroid Build Coastguard Worker         private int i;
162*795d594fSAndroid Build Coastguard Worker         private static final VarHandle vh;
163*795d594fSAndroid Build Coastguard Worker 
164*795d594fSAndroid Build Coastguard Worker         static {
165*795d594fSAndroid Build Coastguard Worker             try {
166*795d594fSAndroid Build Coastguard Worker                 Class<?> cls = ReturnTypeCausingWrongMethodTypeTest.class;
167*795d594fSAndroid Build Coastguard Worker                 vh = MethodHandles.lookup().findVarHandle(cls, "i", int.class);
168*795d594fSAndroid Build Coastguard Worker             } catch (Exception e) {
169*795d594fSAndroid Build Coastguard Worker                 throw new RuntimeException(e);
170*795d594fSAndroid Build Coastguard Worker             }
171*795d594fSAndroid Build Coastguard Worker         }
172*795d594fSAndroid Build Coastguard Worker 
173*795d594fSAndroid Build Coastguard Worker         @Override
doTest()174*795d594fSAndroid Build Coastguard Worker         protected void doTest() {
175*795d594fSAndroid Build Coastguard Worker             i = 33;
176*795d594fSAndroid Build Coastguard Worker             vh.getAndSet(this, 44);
177*795d594fSAndroid Build Coastguard Worker             Runtime runtime = null;
178*795d594fSAndroid Build Coastguard Worker             try {
179*795d594fSAndroid Build Coastguard Worker                 runtime = (Runtime) vh.getAndSet(this, 44);
180*795d594fSAndroid Build Coastguard Worker                 failUnreachable();
181*795d594fSAndroid Build Coastguard Worker             } catch (WrongMethodTypeException ex) {
182*795d594fSAndroid Build Coastguard Worker             }
183*795d594fSAndroid Build Coastguard Worker             assertEquals(null, runtime);
184*795d594fSAndroid Build Coastguard Worker         }
185*795d594fSAndroid Build Coastguard Worker 
main(String[] args)186*795d594fSAndroid Build Coastguard Worker         public static void main(String[] args) {
187*795d594fSAndroid Build Coastguard Worker             new ReturnTypeCausingWrongMethodTypeTest().run();
188*795d594fSAndroid Build Coastguard Worker         }
189*795d594fSAndroid Build Coastguard Worker     }
190*795d594fSAndroid Build Coastguard Worker 
191*795d594fSAndroid Build Coastguard Worker     public static class UnsupportedAccessModePreemptsWrongMethodTypeExceptionTest
192*795d594fSAndroid Build Coastguard Worker             extends VarHandleUnitTest {
193*795d594fSAndroid Build Coastguard Worker         private static final boolean b = true;
194*795d594fSAndroid Build Coastguard Worker         private static final VarHandle vh;
195*795d594fSAndroid Build Coastguard Worker 
196*795d594fSAndroid Build Coastguard Worker         static {
197*795d594fSAndroid Build Coastguard Worker             try {
198*795d594fSAndroid Build Coastguard Worker                 Class<?> cls = UnsupportedAccessModePreemptsWrongMethodTypeExceptionTest.class;
199*795d594fSAndroid Build Coastguard Worker                 vh = MethodHandles.lookup().findStaticVarHandle(cls, "b", boolean.class);
200*795d594fSAndroid Build Coastguard Worker             } catch (Exception e) {
201*795d594fSAndroid Build Coastguard Worker                 throw new RuntimeException(e);
202*795d594fSAndroid Build Coastguard Worker             }
203*795d594fSAndroid Build Coastguard Worker         }
204*795d594fSAndroid Build Coastguard Worker 
205*795d594fSAndroid Build Coastguard Worker         @Override
doTest()206*795d594fSAndroid Build Coastguard Worker         protected void doTest() {
207*795d594fSAndroid Build Coastguard Worker             // A final field should not support an VarHandle access modes which can update it
208*795d594fSAndroid Build Coastguard Worker             boolean supported = vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND);
209*795d594fSAndroid Build Coastguard Worker             assertFalse(supported);
210*795d594fSAndroid Build Coastguard Worker             try {
211*795d594fSAndroid Build Coastguard Worker                 // The following is both unsupported and a wrong method type...
212*795d594fSAndroid Build Coastguard Worker                 vh.getAndBitwiseAnd(System.out);
213*795d594fSAndroid Build Coastguard Worker                 failUnreachable();
214*795d594fSAndroid Build Coastguard Worker             } catch (UnsupportedOperationException ex) {
215*795d594fSAndroid Build Coastguard Worker             }
216*795d594fSAndroid Build Coastguard Worker         }
217*795d594fSAndroid Build Coastguard Worker 
main(String[] args)218*795d594fSAndroid Build Coastguard Worker         public static void main(String[] args) {
219*795d594fSAndroid Build Coastguard Worker             new UnsupportedAccessModePreemptsWrongMethodTypeExceptionTest().run();
220*795d594fSAndroid Build Coastguard Worker         }
221*795d594fSAndroid Build Coastguard Worker     }
222*795d594fSAndroid Build Coastguard Worker 
main(String[] args)223*795d594fSAndroid Build Coastguard Worker     public static void main(String[] args) {
224*795d594fSAndroid Build Coastguard Worker         NullReceiverTest.main(args);
225*795d594fSAndroid Build Coastguard Worker         UnsupportedAccessModeTest.main(args);
226*795d594fSAndroid Build Coastguard Worker         WrongArgumentTypeCausingWrongMethodTypeTest.main(args);
227*795d594fSAndroid Build Coastguard Worker         TooManyArgumentsCausingWrongMethodTypeTest.main(args);
228*795d594fSAndroid Build Coastguard Worker         TooFewArgumentsCausingWrongMethodTypeTest.main(args);
229*795d594fSAndroid Build Coastguard Worker         ReturnTypeCausingWrongMethodTypeTest.main(args);
230*795d594fSAndroid Build Coastguard Worker         UnsupportedAccessModePreemptsWrongMethodTypeExceptionTest.main(args);
231*795d594fSAndroid Build Coastguard Worker     }
232*795d594fSAndroid Build Coastguard Worker }
233