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