jdk
1/*
2* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*
5* This code is free software; you can redistribute it and/or modify it
6* under the terms of the GNU General Public License version 2 only, as
7* published by the Free Software Foundation.
8*
9* This code is distributed in the hope that it will be useful, but WITHOUT
10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12* version 2 for more details (a copy is included in the LICENSE file that
13* accompanied this code).
14*
15* You should have received a copy of the GNU General Public License version
16* 2 along with this work; if not, write to the Free Software Foundation,
17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18*
19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20* or visit www.oracle.com if you need additional information or have any
21* questions.
22*
23*/
24
25#include "precompiled.hpp"26#include "cds/archiveBuilder.hpp"27#include "cds/cdsHeapVerifier.hpp"28#include "classfile/classLoaderDataGraph.hpp"29#include "classfile/javaClasses.inline.hpp"30#include "logging/log.hpp"31#include "logging/logStream.hpp"32#include "memory/resourceArea.hpp"33#include "oops/fieldStreams.inline.hpp"34#include "oops/klass.inline.hpp"35#include "oops/oop.inline.hpp"36#include "runtime/fieldDescriptor.inline.hpp"37
38#if INCLUDE_CDS_JAVA_HEAP39
40// CDSHeapVerifier is used to check for problems where an archived object references a
41// static field that may be reinitialized at runtime. In the following example,
42// Foo.get.test()
43// correctly returns true when CDS disabled, but incorrectly returns false when CDS is enabled.
44//
45// class Foo {
46// final Foo archivedFoo; // this field is archived by CDS
47// Bar bar;
48// static {
49// CDS.initializeFromArchive(Foo.class);
50// if (archivedFoo == null) {
51// archivedFoo = new Foo();
52// archivedFoo.bar = Bar.bar;
53// }
54// }
55// static Foo get() { return archivedFoo; }
56// boolean test() {
57// return bar == Bar.bar;
58// }
59// }
60//
61// class Bar {
62// // this field is initialized in both CDS dump time and runtime.
63// static final Bar bar = new Bar();
64// }
65//
66// The check itself is simple:
67// [1] CDSHeapVerifier::do_klass() collects all static fields
68// [2] CDSHeapVerifier::do_entry() checks all the archived objects. None of them
69// should be in [1]
70//
71// However, it's legal for *some* static fields to be references. This leads to the
72// table of ADD_EXCL below.
73//
74// [A] In most of the cases, the module bootstrap code will update the static field
75// to point to part of the archived module graph. E.g.,
76// - java/lang/System::bootLayer
77// - jdk/internal/loader/ClassLoaders::BOOT_LOADER
78// [B] A final static String that's explicitly initialized inside <clinit>, but
79// its value is deterministic and is always the same string literal.
80// [C] A non-final static string that is assigned a string literal during class
81// initialization; this string is never changed during -Xshare:dump.
82// [D] Simple caches whose value doesn't matter.
83// [E] Other cases (see comments in-line below).
84
85CDSHeapVerifier::CDSHeapVerifier() : _archived_objs(0), _problems(0)86{
87# define ADD_EXCL(...) { static const char* e[] = {__VA_ARGS__, nullptr}; add_exclusion(e); }88
89// Unfortunately this needs to be manually maintained. If90// test/hotspot/jtreg/runtime/cds/appcds/cacheObject/ArchivedEnumTest.java fails,91// you might need to fix the core library code, or fix the ADD_EXCL entries below.92//93// class field type94ADD_EXCL("java/lang/ClassLoader", "scl"); // A95ADD_EXCL("java/lang/Module", "ALL_UNNAMED_MODULE", // A96"ALL_UNNAMED_MODULE_SET", // A97"EVERYONE_MODULE", // A98"EVERYONE_SET"); // A99
100// This is the same as java/util/ImmutableCollections::EMPTY_SET, which is archived101ADD_EXCL("java/lang/reflect/AccessFlag$Location", "EMPTY_SET"); // E102
103ADD_EXCL("java/lang/System", "bootLayer"); // A104
105// A dummy object used by HashSet. The value doesn't matter and it's never106// tested for equality.107ADD_EXCL("java/util/HashSet", "PRESENT"); // E108ADD_EXCL("jdk/internal/loader/BuiltinClassLoader", "packageToModule"); // A109ADD_EXCL("jdk/internal/loader/ClassLoaders", "BOOT_LOADER", // A110"APP_LOADER", // A111"PLATFORM_LOADER"); // A112ADD_EXCL("jdk/internal/module/Builder", "cachedVersion"); // D113ADD_EXCL("jdk/internal/module/ModuleLoaderMap$Mapper", "APP_CLASSLOADER", // A114"APP_LOADER_INDEX", // A115"PLATFORM_CLASSLOADER", // A116"PLATFORM_LOADER_INDEX"); // A117ADD_EXCL("jdk/internal/module/ServicesCatalog", "CLV"); // A118
119// This just points to an empty Map120ADD_EXCL("jdk/internal/reflect/Reflection", "methodFilterMap"); // E121
122// Integer for 0 and 1 are in java/lang/Integer$IntegerCache and are archived123ADD_EXCL("sun/invoke/util/ValueConversions", "ONE_INT", // E124"ZERO_INT"); // E125
126# undef ADD_EXCL127
128ClassLoaderDataGraph::classes_do(this);129}
130
131CDSHeapVerifier::~CDSHeapVerifier() {132if (_problems > 0) {133log_warning(cds, heap)("Scanned %d objects. Found %d case(s) where "134"an object points to a static field that may be "135"reinitialized at runtime.", _archived_objs, _problems);136}137}
138
139class CDSHeapVerifier::CheckStaticFields : public FieldClosure {140CDSHeapVerifier* _verifier;141InstanceKlass* _ik;142const char** _exclusions;143public:144CheckStaticFields(CDSHeapVerifier* verifier, InstanceKlass* ik)145: _verifier(verifier), _ik(ik) {146_exclusions = _verifier->find_exclusion(_ik);147}148
149void do_field(fieldDescriptor* fd) {150if (fd->field_type() != T_OBJECT) {151return;152}153
154oop static_obj_field = _ik->java_mirror()->obj_field(fd->offset());155if (static_obj_field != nullptr) {156Klass* klass = static_obj_field->klass();157if (_exclusions != nullptr) {158for (const char** p = _exclusions; *p != nullptr; p++) {159if (fd->name()->equals(*p)) {160return;161}162}163}164
165if (fd->is_final() && java_lang_String::is_instance(static_obj_field) && fd->has_initial_value()) {166// This field looks like like this in the Java source:167// static final SOME_STRING = "a string literal";168// This string literal has been stored in the shared string table, so it's OK169// for the archived objects to refer to it.170return;171}172if (fd->is_final() && java_lang_Class::is_instance(static_obj_field)) {173// This field points to an archived mirror.174return;175}176if (klass->has_archived_enum_objs()) {177// This klass is a subclass of java.lang.Enum. If any instance of this klass178// has been archived, we will archive all static fields of this klass.179// See HeapShared::initialize_enum_klass().180return;181}182
183// This field *may* be initialized to a different value at runtime. Remember it184// and check later if it appears in the archived object graph.185_verifier->add_static_obj_field(_ik, static_obj_field, fd->name());186}187}188};189
190// Remember all the static object fields of every class that are currently
191// loaded.
192void CDSHeapVerifier::do_klass(Klass* k) {193if (k->is_instance_klass()) {194InstanceKlass* ik = InstanceKlass::cast(k);195
196if (HeapShared::is_subgraph_root_class(ik)) {197// ik is inside one of the ArchivableStaticFieldInfo tables198// in heapShared.cpp. We assume such classes are programmed to199// update their static fields correctly at runtime.200return;201}202
203CheckStaticFields csf(this, ik);204ik->do_local_static_fields(&csf);205}206}
207
208void CDSHeapVerifier::add_static_obj_field(InstanceKlass* ik, oop field, Symbol* name) {209StaticFieldInfo info = {ik, name};210_table.put(field, info);211}
212
213inline bool CDSHeapVerifier::do_entry(oop& orig_obj, HeapShared::CachedOopInfo& value) {214_archived_objs++;215
216StaticFieldInfo* info = _table.get(orig_obj);217if (info != nullptr) {218if (value.orig_referrer() == nullptr && java_lang_String::is_instance(orig_obj)) {219// This string object is not referenced by any of the archived object graphs. It's archived220// only because it's in the interned string table. So we are not in a condition that221// should be flagged by CDSHeapVerifier.222return true; /* keep on iterating */223}224ResourceMark rm;225LogStream ls(Log(cds, heap)::warning());226ls.print_cr("Archive heap points to a static field that may be reinitialized at runtime:");227ls.print_cr("Field: %s::%s", info->_holder->name()->as_C_string(), info->_name->as_C_string());228ls.print("Value: ");229orig_obj->print_on(&ls);230ls.print_cr("--- trace begin ---");231trace_to_root(&ls, orig_obj, nullptr, &value);232ls.print_cr("--- trace end ---");233ls.cr();234_problems ++;235}236
237return true; /* keep on iterating */238}
239
240class CDSHeapVerifier::TraceFields : public FieldClosure {241oop _orig_obj;242oop _orig_field;243outputStream* _st;244
245public:246TraceFields(oop orig_obj, oop orig_field, outputStream* st)247: _orig_obj(orig_obj), _orig_field(orig_field), _st(st) {}248
249void do_field(fieldDescriptor* fd) {250if (fd->field_type() == T_OBJECT || fd->field_type() == T_ARRAY) {251oop obj_field = _orig_obj->obj_field(fd->offset());252if (obj_field == _orig_field) {253_st->print("::%s (offset = %d)", fd->name()->as_C_string(), fd->offset());254}255}256}257};258
259// Call this function (from gdb, etc) if you want to know why an object is archived.
260void CDSHeapVerifier::trace_to_root(outputStream* st, oop orig_obj) {261HeapShared::CachedOopInfo* info = HeapShared::archived_object_cache()->get(orig_obj);262if (info != nullptr) {263trace_to_root(st, orig_obj, nullptr, info);264} else {265st->print_cr("Not an archived object??");266}267}
268
269int CDSHeapVerifier::trace_to_root(outputStream* st, oop orig_obj, oop orig_field, HeapShared::CachedOopInfo* info) {270int level = 0;271if (info->orig_referrer() != nullptr) {272HeapShared::CachedOopInfo* ref = HeapShared::archived_object_cache()->get(info->orig_referrer());273assert(ref != nullptr, "sanity");274level = trace_to_root(st, info->orig_referrer(), orig_obj, ref) + 1;275} else if (java_lang_String::is_instance(orig_obj)) {276st->print_cr("[%2d] (shared string table)", level++);277}278Klass* k = orig_obj->klass();279ResourceMark rm;280st->print("[%2d] ", level);281orig_obj->print_address_on(st);282st->print(" %s", k->internal_name());283if (orig_field != nullptr) {284if (k->is_instance_klass()) {285TraceFields clo(orig_obj, orig_field, st);286InstanceKlass::cast(k)->do_nonstatic_fields(&clo);287} else {288assert(orig_obj->is_objArray(), "must be");289objArrayOop array = (objArrayOop)orig_obj;290for (int i = 0; i < array->length(); i++) {291if (array->obj_at(i) == orig_field) {292st->print(" @[%d]", i);293break;294}295}296}297}298st->cr();299
300return level;301}
302
303void CDSHeapVerifier::verify() {304CDSHeapVerifier verf;305HeapShared::archived_object_cache()->iterate(&verf);306}
307
308#endif // INCLUDE_CDS_JAVA_HEAP309