|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 "cds/aotMetaspace.hpp" |
| 26 | +#include "cds/aotOopChecker.hpp" |
| 27 | +#include "cds/heapShared.hpp" |
| 28 | +#include "classfile/javaClasses.hpp" |
| 29 | +#include "classfile/symbolTable.hpp" |
| 30 | +#include "classfile/vmClasses.hpp" |
| 31 | +#include "oops/instanceKlass.inline.hpp" |
| 32 | +#include "runtime/fieldDescriptor.inline.hpp" |
| 33 | +#include "utilities/debug.hpp" |
| 34 | + |
| 35 | +#if INCLUDE_CDS_JAVA_HEAP |
| 36 | + |
| 37 | +oop AOTOopChecker::get_oop_field(oop obj, const char* name, const char* sig) { |
| 38 | + Symbol* name_sym = SymbolTable::probe(name, checked_cast<int>(strlen(name))); |
| 39 | + assert(name_sym != nullptr, "Symbol must have been resolved for an existing field of this obj"); |
| 40 | + Symbol* sig_sym = SymbolTable::probe(sig, checked_cast<int>(strlen(sig))); |
| 41 | + assert(sig_sym != nullptr, "Symbol must have been resolved for an existing field of this obj"); |
| 42 | + |
| 43 | + fieldDescriptor fd; |
| 44 | + Klass* k = InstanceKlass::cast(obj->klass())->find_field(name_sym, sig_sym, &fd); |
| 45 | + assert(k != nullptr, "field must exist"); |
| 46 | + precond(!fd.is_static()); |
| 47 | + precond(fd.field_type() == T_OBJECT || fd.field_type() == T_ARRAY); |
| 48 | + return obj->obj_field(fd.offset()); |
| 49 | +} |
| 50 | + |
| 51 | +// Make sure we are not caching objects with assumptions that can be violated in |
| 52 | +// the production run. |
| 53 | +void AOTOopChecker::check(oop obj) { |
| 54 | + // Currently we only check URL objects, but more rules may be added in the future. |
| 55 | + |
| 56 | + if (obj->klass()->is_subclass_of(vmClasses::URL_klass())) { |
| 57 | + // If URL could be subclassed, obj may have new fields that we don't know about. |
| 58 | + precond(vmClasses::URL_klass()->is_final()); |
| 59 | + |
| 60 | + // URLs are referenced by the CodeSources/ProtectDomains that are cached |
| 61 | + // for AOT-linked classes loaded by the platform/app loaders. |
| 62 | + // |
| 63 | + // Do not cache any URLs whose URLStreamHandler can be overridden by the application. |
| 64 | + // - "jrt" and "file" will always use the built-in URLStreamHandler. See |
| 65 | + // java.net.URL::isOverrideable(). |
| 66 | + // - When an AOT-linked class is loaded from a JAR file, its URL is something |
| 67 | + // like file:HelloWorl.jar, and does NOT use the "jar" protocol. |
| 68 | + oop protocol = get_oop_field(obj, "protocol", "Ljava/lang/String;"); |
| 69 | + if (!java_lang_String::equals(protocol, "jrt", 3) && |
| 70 | + !java_lang_String::equals(protocol, "file", 4)) { |
| 71 | + ResourceMark rm; |
| 72 | + log_error(aot)("Must cache only URLs with jrt/file protocols but got: %s", |
| 73 | + java_lang_String::as_quoted_ascii(protocol)); |
| 74 | + HeapShared::debug_trace(); |
| 75 | + AOTMetaspace::unrecoverable_writing_error(); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +#endif //INCLUDE_CDS_JAVA_HEAP |
0 commit comments