xref: /aosp_15_r20/art/runtime/class_status.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2011 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 #ifndef ART_RUNTIME_CLASS_STATUS_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_CLASS_STATUS_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <iosfwd>
21*795d594fSAndroid Build Coastguard Worker #include <stdint.h>
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker // Class Status
28*795d594fSAndroid Build Coastguard Worker //
29*795d594fSAndroid Build Coastguard Worker // kNotReady: Newly allocated Class object.
30*795d594fSAndroid Build Coastguard Worker // If a Class cannot be found in the class table by FindClass, ClassLinker
31*795d594fSAndroid Build Coastguard Worker // allocates a new one in the kNotReady state and calls LoadClass. Note
32*795d594fSAndroid Build Coastguard Worker // if ClassLinker does find a Class, it may not be kResolved and ClassLinker
33*795d594fSAndroid Build Coastguard Worker // will try to push it forward toward kResolved.
34*795d594fSAndroid Build Coastguard Worker //
35*795d594fSAndroid Build Coastguard Worker // kRetired: Class object that was temporarily used until class linking time
36*795d594fSAndroid Build Coastguard Worker // when it had its final size (including vtable size) figured out and had
37*795d594fSAndroid Build Coastguard Worker // been cloned to one with the right size which will be the one used later.
38*795d594fSAndroid Build Coastguard Worker // The old one is retired and will be gc'ed once all refs to the class point
39*795d594fSAndroid Build Coastguard Worker // to the newly cloned version.
40*795d594fSAndroid Build Coastguard Worker //
41*795d594fSAndroid Build Coastguard Worker // kErrorUnresolved, kErrorResolved: Class is erroneous. We need
42*795d594fSAndroid Build Coastguard Worker // to distinguish between classes that have been resolved and classes that
43*795d594fSAndroid Build Coastguard Worker // have not. This is important because the const-class instruction needs to
44*795d594fSAndroid Build Coastguard Worker // return a previously resolved class even if its subsequent initialization
45*795d594fSAndroid Build Coastguard Worker // failed. We also need this to decide whether to wrap a previous
46*795d594fSAndroid Build Coastguard Worker // initialization failure in ClassDefNotFound error or not.
47*795d594fSAndroid Build Coastguard Worker //
48*795d594fSAndroid Build Coastguard Worker // kIdx: LoadClass populates with Class with information from
49*795d594fSAndroid Build Coastguard Worker // the DexFile, moving the status to kIdx, indicating that the
50*795d594fSAndroid Build Coastguard Worker // Class value in super_class_ has not been populated. The new Class
51*795d594fSAndroid Build Coastguard Worker // can then be inserted into the classes table.
52*795d594fSAndroid Build Coastguard Worker //
53*795d594fSAndroid Build Coastguard Worker // kLoaded: After taking a lock on Class, the ClassLinker will
54*795d594fSAndroid Build Coastguard Worker // attempt to move a kIdx class forward to kLoaded by
55*795d594fSAndroid Build Coastguard Worker // using ResolveClass to initialize the super_class_ and ensuring the
56*795d594fSAndroid Build Coastguard Worker // interfaces are resolved.
57*795d594fSAndroid Build Coastguard Worker //
58*795d594fSAndroid Build Coastguard Worker // kResolving: Class is just cloned with the right size from
59*795d594fSAndroid Build Coastguard Worker // temporary class that's acting as a placeholder for linking. The old
60*795d594fSAndroid Build Coastguard Worker // class will be retired. New class is set to this status first before
61*795d594fSAndroid Build Coastguard Worker // moving on to being resolved.
62*795d594fSAndroid Build Coastguard Worker //
63*795d594fSAndroid Build Coastguard Worker // kResolved: Still holding the lock on Class, the ClassLinker
64*795d594fSAndroid Build Coastguard Worker // shows linking is complete and fields of the Class populated by making
65*795d594fSAndroid Build Coastguard Worker // it kResolved. Java allows circularities of the form where a super
66*795d594fSAndroid Build Coastguard Worker // class has a field that is of the type of the sub class. We need to be able
67*795d594fSAndroid Build Coastguard Worker // to fully resolve super classes while resolving types for fields.
68*795d594fSAndroid Build Coastguard Worker //
69*795d594fSAndroid Build Coastguard Worker // kRetryVerificationAtRuntime: The verifier sets a class to
70*795d594fSAndroid Build Coastguard Worker // this state if it encounters a soft failure at compile time. This
71*795d594fSAndroid Build Coastguard Worker // often happens when there are unresolved classes in other dex
72*795d594fSAndroid Build Coastguard Worker // files, and this status marks a class as needing to be verified
73*795d594fSAndroid Build Coastguard Worker // again at runtime. This status is only set and seen during AOT
74*795d594fSAndroid Build Coastguard Worker // compilation, and the compiler will mark the class as resolved in the
75*795d594fSAndroid Build Coastguard Worker // image and/or oat file.
76*795d594fSAndroid Build Coastguard Worker //
77*795d594fSAndroid Build Coastguard Worker // kVerifiedNeedsAccessChecks: The verifier sets a class to this state
78*795d594fSAndroid Build Coastguard Worker // if it encounters access-checks only soft failure at compile time.
79*795d594fSAndroid Build Coastguard Worker // This happens when there are unresolved classes in other dex files,
80*795d594fSAndroid Build Coastguard Worker // and this status marks a class as verified but that will need to run
81*795d594fSAndroid Build Coastguard Worker // with access checks enabled in the interpreter.
82*795d594fSAndroid Build Coastguard Worker //
83*795d594fSAndroid Build Coastguard Worker // kVerified: The class has been verified.
84*795d594fSAndroid Build Coastguard Worker //
85*795d594fSAndroid Build Coastguard Worker // kSuperclassValidated: Types referenced by virtual method signatures have
86*795d594fSAndroid Build Coastguard Worker // been verified to match the types referenced by the virtual and interface
87*795d594fSAndroid Build Coastguard Worker // methods from superclass and interfaces that they override or implement.
88*795d594fSAndroid Build Coastguard Worker // TODO: This is similar to loading constraints in the RI but the check is too
89*795d594fSAndroid Build Coastguard Worker // weak to fully ensure type safety. For example, the `DelegateLastClassLoader`
90*795d594fSAndroid Build Coastguard Worker // can easily break the type safety if a class name is resolved differently in
91*795d594fSAndroid Build Coastguard Worker // the parent class loader.
92*795d594fSAndroid Build Coastguard Worker //
93*795d594fSAndroid Build Coastguard Worker // kInitializing: The class is being initialized by some thread. Other threads
94*795d594fSAndroid Build Coastguard Worker // trying to initialize the class shall be blocked until the initialization is
95*795d594fSAndroid Build Coastguard Worker // completed by the initializing thread.
96*795d594fSAndroid Build Coastguard Worker //
97*795d594fSAndroid Build Coastguard Worker // kInitialized: The class has been fully initialized. However, a thread that
98*795d594fSAndroid Build Coastguard Worker // needs to see its fields in their initialized state needs to check for this
99*795d594fSAndroid Build Coastguard Worker // state with an acquire load on the class status field to ensure correct
100*795d594fSAndroid Build Coastguard Worker // memory visibility.
101*795d594fSAndroid Build Coastguard Worker //
102*795d594fSAndroid Build Coastguard Worker // kVisiblyInitialized: The class has been initialized and the fields in their
103*795d594fSAndroid Build Coastguard Worker // initialized state are visible to all threads without additional memory
104*795d594fSAndroid Build Coastguard Worker // synchronization. A thread can use a relaxed load of the class status field
105*795d594fSAndroid Build Coastguard Worker // and, if it finds this state, it can safely use the class's static fields.
106*795d594fSAndroid Build Coastguard Worker // This is ensured by executing a checkpoint on all threads after the class
107*795d594fSAndroid Build Coastguard Worker // was initialized; this operation is batched due to the high checkpoint cost.
108*795d594fSAndroid Build Coastguard Worker // This state enables cheap class initialization checks in compiled managed
109*795d594fSAndroid Build Coastguard Worker // code (especially AOT-compiled; JITted code could be eventually re-JITted
110*795d594fSAndroid Build Coastguard Worker // without any checks at all) and the runtime.
111*795d594fSAndroid Build Coastguard Worker enum class ClassStatus : uint8_t {
112*795d594fSAndroid Build Coastguard Worker   kNotReady = 0,  // Zero-initialized Class object starts in this state.
113*795d594fSAndroid Build Coastguard Worker   kRetired = 1,  // Retired, should not be used. Use the newly cloned one instead.
114*795d594fSAndroid Build Coastguard Worker   kErrorResolved = 2,
115*795d594fSAndroid Build Coastguard Worker   kErrorUnresolved = 3,
116*795d594fSAndroid Build Coastguard Worker   kIdx = 4,  // Loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_.
117*795d594fSAndroid Build Coastguard Worker   kLoaded = 5,  // DEX idx values resolved.
118*795d594fSAndroid Build Coastguard Worker   kResolving = 6,  // Just cloned from temporary class object.
119*795d594fSAndroid Build Coastguard Worker   kResolved = 7,  // Part of linking.
120*795d594fSAndroid Build Coastguard Worker   kVerifying = 8,  // In the process of being verified.
121*795d594fSAndroid Build Coastguard Worker   kRetryVerificationAtRuntime = 9,  // Compile time verification failed, retry at runtime.
122*795d594fSAndroid Build Coastguard Worker   kVerifiedNeedsAccessChecks = 10,  // Compile time verification only failed for access checks.
123*795d594fSAndroid Build Coastguard Worker   kVerified = 11,  // Logically part of linking; done pre-init.
124*795d594fSAndroid Build Coastguard Worker   kSuperclassValidated = 12,  // Superclass validation part of init done.
125*795d594fSAndroid Build Coastguard Worker   kInitializing = 13,  // Class init in progress.
126*795d594fSAndroid Build Coastguard Worker   kInitialized = 14,  // Ready to go.
127*795d594fSAndroid Build Coastguard Worker   kVisiblyInitialized = 15,  // Initialized and visible to all threads.
128*795d594fSAndroid Build Coastguard Worker   kLast = kVisiblyInitialized
129*795d594fSAndroid Build Coastguard Worker };
130*795d594fSAndroid Build Coastguard Worker 
131*795d594fSAndroid Build Coastguard Worker EXPORT std::ostream& operator<<(std::ostream& os, ClassStatus rhs);
132*795d594fSAndroid Build Coastguard Worker 
133*795d594fSAndroid Build Coastguard Worker }  // namespace art
134*795d594fSAndroid Build Coastguard Worker 
135*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_CLASS_STATUS_H_
136