1 /* 2 * Copyright (c) 2015, 2020, 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 "jvm.h" 27 #include "jimage.hpp" 28 #include "classfile/classListParser.hpp" 29 #include "classfile/classLoaderExt.hpp" 30 #include "classfile/javaClasses.inline.hpp" 31 #include "classfile/symbolTable.hpp" 32 #include "classfile/systemDictionary.hpp" 33 #include "classfile/systemDictionaryShared.hpp" 34 #include "logging/log.hpp" 35 #include "logging/logTag.hpp" 36 #include "memory/lambdaFormInvokers.hpp" 37 #include "memory/metaspaceShared.hpp" 38 #include "memory/resourceArea.hpp" 39 #include "runtime/handles.inline.hpp" 40 #include "runtime/javaCalls.hpp" 41 #include "utilities/defaultStream.hpp" 42 #include "utilities/hashtable.inline.hpp" 43 #include "utilities/macros.hpp" 44 45 ClassListParser* ClassListParser::_instance = NULL; 46 47 ClassListParser::ClassListParser(const char* file) { 48 assert(_instance == NULL, "must be singleton"); 49 _instance = this; 50 _classlist_file = file; 51 _file = NULL; 52 // Use os::open() because neither fopen() nor os::fopen() 53 // can handle long path name on Windows. 54 int fd = os::open(file, O_RDONLY, S_IREAD); 55 if (fd != -1) { 56 // Obtain a File* from the file descriptor so that fgets() 57 // can be used in parse_one_line() 58 _file = os::open(fd, "r"); 59 } 60 if (_file == NULL) { 61 char errmsg[JVM_MAXPATHLEN]; 62 os::lasterror(errmsg, JVM_MAXPATHLEN); 63 vm_exit_during_initialization("Loading classlist failed", errmsg); 64 } 65 _line_no = 0; 66 _interfaces = new (ResourceObj::C_HEAP, mtClass) GrowableArray<int>(10, mtClass); 67 } 68 69 ClassListParser::~ClassListParser() { 70 if (_file) { 71 fclose(_file); 72 } 73 _instance = NULL; 74 } 75 76 bool ClassListParser::parse_one_line() { 77 for (;;) { 78 if (fgets(_line, sizeof(_line), _file) == NULL) { 79 return false; 80 } 81 ++ _line_no; 82 _line_len = (int)strlen(_line); 83 if (_line_len > _max_allowed_line_len) { 84 error("input line too long (must be no longer than %d chars)", _max_allowed_line_len); 85 } 86 if (*_line == '#') { // comment 87 continue; 88 } 89 // The line is output TRACE_RESOLVE 90 if (strncmp(_line, LambdaFormInvokers::lambda_form_invoker_tag(), 91 strlen(LambdaFormInvokers::lambda_form_invoker_tag())) == 0) { 92 LambdaFormInvokers::append(os::strdup((const char*)_line, mtInternal)); 93 continue; 94 } 95 break; 96 } 97 98 _id = _unspecified; 99 _super = _unspecified; 100 _interfaces->clear(); 101 _source = NULL; 102 _interfaces_specified = false; 103 104 { 105 int len = (int)strlen(_line); 106 int i; 107 // Replace \t\r\n with ' ' 108 for (i=0; i<len; i++) { 109 if (_line[i] == '\t' || _line[i] == '\r' || _line[i] == '\n') { 110 _line[i] = ' '; 111 } 112 } 113 114 // Remove trailing newline/space 115 while (len > 0) { 116 if (_line[len-1] == ' ') { 117 _line[len-1] = '\0'; 118 len --; 119 } else { 120 break; 121 } 122 } 123 _line_len = len; 124 _class_name = _line; 125 } 126 127 if ((_token = strchr(_line, ' ')) == NULL) { 128 // No optional arguments are specified. 129 return true; 130 } 131 132 // Mark the end of the name, and go to the next input char 133 *_token++ = '\0'; 134 135 while (*_token) { 136 skip_whitespaces(); 137 138 if (parse_int_option("id:", &_id)) { 139 continue; 140 } else if (parse_int_option("super:", &_super)) { 141 check_already_loaded("Super class", _super); 142 continue; 143 } else if (skip_token("interfaces:")) { 144 int i; 145 while (try_parse_int(&i)) { 146 check_already_loaded("Interface", i); 147 _interfaces->append(i); 148 } 149 } else if (skip_token("source:")) { 150 skip_whitespaces(); 151 _source = _token; 152 char* s = strchr(_token, ' '); 153 if (s == NULL) { 154 break; // end of input line 155 } else { 156 *s = '\0'; // mark the end of _source 157 _token = s+1; 158 } 159 } else { 160 error("Unknown input"); 161 } 162 } 163 164 // if src is specified 165 // id super interfaces must all be specified 166 // loader may be specified 167 // else 168 // # the class is loaded from classpath 169 // id may be specified 170 // super, interfaces, loader must not be specified 171 return true; 172 } 173 174 void ClassListParser::skip_whitespaces() { 175 while (*_token == ' ' || *_token == '\t') { 176 _token ++; 177 } 178 } 179 180 void ClassListParser::skip_non_whitespaces() { 181 while (*_token && *_token != ' ' && *_token != '\t') { 182 _token ++; 183 } 184 } 185 186 void ClassListParser::parse_int(int* value) { 187 skip_whitespaces(); 188 if (sscanf(_token, "%i", value) == 1) { 189 skip_non_whitespaces(); 190 if (*value < 0) { 191 error("Error: negative integers not allowed (%d)", *value); 192 } 193 } else { 194 error("Error: expected integer"); 195 } 196 } 197 198 bool ClassListParser::try_parse_int(int* value) { 199 skip_whitespaces(); 200 if (sscanf(_token, "%i", value) == 1) { 201 skip_non_whitespaces(); 202 return true; 203 } 204 return false; 205 } 206 207 bool ClassListParser::skip_token(const char* option_name) { 208 size_t len = strlen(option_name); 209 if (strncmp(_token, option_name, len) == 0) { 210 _token += len; 211 return true; 212 } else { 213 return false; 214 } 215 } 216 217 bool ClassListParser::parse_int_option(const char* option_name, int* value) { 218 if (skip_token(option_name)) { 219 if (*value != _unspecified) { 220 error("%s specified twice", option_name); 221 } else { 222 parse_int(value); 223 return true; 224 } 225 } 226 return false; 227 } 228 229 void ClassListParser::print_specified_interfaces() { 230 const int n = _interfaces->length(); 231 jio_fprintf(defaultStream::error_stream(), "Currently specified interfaces[%d] = {\n", n); 232 for (int i=0; i<n; i++) { 233 InstanceKlass* k = lookup_class_by_id(_interfaces->at(i)); 234 jio_fprintf(defaultStream::error_stream(), " %4d = %s\n", _interfaces->at(i), k->name()->as_klass_external_name()); 235 } 236 jio_fprintf(defaultStream::error_stream(), "}\n"); 237 } 238 239 void ClassListParser::print_actual_interfaces(InstanceKlass* ik) { 240 int n = ik->local_interfaces()->length(); 241 jio_fprintf(defaultStream::error_stream(), "Actual interfaces[%d] = {\n", n); 242 for (int i = 0; i < n; i++) { 243 InstanceKlass* e = ik->local_interfaces()->at(i); 244 jio_fprintf(defaultStream::error_stream(), " %s\n", e->name()->as_klass_external_name()); 245 } 246 jio_fprintf(defaultStream::error_stream(), "}\n"); 247 } 248 249 void ClassListParser::error(const char* msg, ...) { 250 va_list ap; 251 va_start(ap, msg); 252 int error_index = _token - _line; 253 if (error_index >= _line_len) { 254 error_index = _line_len - 1; 255 } 256 if (error_index < 0) { 257 error_index = 0; 258 } 259 260 jio_fprintf(defaultStream::error_stream(), 261 "An error has occurred while processing class list file %s %d:%d.\n", 262 _classlist_file, _line_no, (error_index + 1)); 263 jio_vfprintf(defaultStream::error_stream(), msg, ap); 264 265 if (_line_len <= 0) { 266 jio_fprintf(defaultStream::error_stream(), "\n"); 267 } else { 268 jio_fprintf(defaultStream::error_stream(), ":\n"); 269 for (int i=0; i<_line_len; i++) { 270 char c = _line[i]; 271 if (c == '\0') { 272 jio_fprintf(defaultStream::error_stream(), "%s", " "); 273 } else { 274 jio_fprintf(defaultStream::error_stream(), "%c", c); 275 } 276 } 277 jio_fprintf(defaultStream::error_stream(), "\n"); 278 for (int i=0; i<error_index; i++) { 279 jio_fprintf(defaultStream::error_stream(), "%s", " "); 280 } 281 jio_fprintf(defaultStream::error_stream(), "^\n"); 282 } 283 284 vm_exit_during_initialization("class list format error.", NULL); 285 va_end(ap); 286 } 287 288 // This function is used for loading classes for customized class loaders 289 // during archive dumping. 290 InstanceKlass* ClassListParser::load_class_from_source(Symbol* class_name, TRAPS) { 291 #if !(defined(_LP64) && (defined(LINUX) || defined(__APPLE__))) 292 // The only supported platforms are: (1) Linux/64-bit and (2) Solaris/64-bit and 293 // (3) MacOSX/64-bit 294 // This #if condition should be in sync with the areCustomLoadersSupportedForCDS 295 // method in test/lib/jdk/test/lib/Platform.java. 296 error("AppCDS custom class loaders not supported on this platform"); 297 #endif 298 299 if (!is_super_specified()) { 300 error("If source location is specified, super class must be also specified"); 301 } 302 if (!is_id_specified()) { 303 error("If source location is specified, id must be also specified"); 304 } 305 if (strncmp(_class_name, "java/", 5) == 0) { 306 log_info(cds)("Prohibited package for non-bootstrap classes: %s.class from %s", 307 _class_name, _source); 308 return NULL; 309 } 310 311 InstanceKlass* k = ClassLoaderExt::load_class(class_name, _source, CHECK_NULL); 312 313 if (k != NULL) { 314 if (k->local_interfaces()->length() != _interfaces->length()) { 315 print_specified_interfaces(); 316 print_actual_interfaces(k); 317 error("The number of interfaces (%d) specified in class list does not match the class file (%d)", 318 _interfaces->length(), k->local_interfaces()->length()); 319 } 320 321 bool added = SystemDictionaryShared::add_unregistered_class(k, CHECK_NULL); 322 if (!added) { 323 // We allow only a single unregistered class for each unique name. 324 error("Duplicated class %s", _class_name); 325 } 326 327 // This tells JVM_FindLoadedClass to not find this class. 328 k->set_shared_classpath_index(UNREGISTERED_INDEX); 329 k->clear_shared_class_loader_type(); 330 } 331 332 return k; 333 } 334 335 Klass* ClassListParser::load_current_class(TRAPS) { 336 TempNewSymbol class_name_symbol = SymbolTable::new_symbol(_class_name); 337 338 Klass* klass = NULL; 339 if (!is_loading_from_source()) { 340 // Load classes for the boot/platform/app loaders only. 341 if (is_super_specified()) { 342 error("If source location is not specified, super class must not be specified"); 343 } 344 if (are_interfaces_specified()) { 345 error("If source location is not specified, interface(s) must not be specified"); 346 } 347 348 bool non_array = !Signature::is_array(class_name_symbol); 349 350 JavaValue result(T_OBJECT); 351 if (non_array) { 352 // At this point, we are executing in the context of the boot loader. We 353 // cannot call Class.forName because that is context dependent and 354 // would load only classes for the boot loader. 355 // 356 // Instead, let's call java_system_loader().loadClass() directly, which will 357 // delegate to the correct loader (boot, platform or app) depending on 358 // the class name. 359 360 Handle s = java_lang_String::create_from_symbol(class_name_symbol, CHECK_NULL); 361 // ClassLoader.loadClass() wants external class name format, i.e., convert '/' chars to '.' 362 Handle ext_class_name = java_lang_String::externalize_classname(s, CHECK_NULL); 363 Handle loader = Handle(THREAD, SystemDictionary::java_system_loader()); 364 365 JavaCalls::call_virtual(&result, 366 loader, //SystemDictionary::java_system_loader(), 367 SystemDictionary::ClassLoader_klass(), 368 vmSymbols::loadClass_name(), 369 vmSymbols::string_class_signature(), 370 ext_class_name, 371 THREAD); // <-- failure is handled below 372 } else { 373 // array classes are not supported in class list. 374 THROW_NULL(vmSymbols::java_lang_ClassNotFoundException()); 375 } 376 assert(result.get_type() == T_OBJECT, "just checking"); 377 oop obj = (oop) result.get_jobject(); 378 if (!HAS_PENDING_EXCEPTION && (obj != NULL)) { 379 klass = java_lang_Class::as_Klass(obj); 380 } else { // load classes in bootclasspath/a 381 if (HAS_PENDING_EXCEPTION) { 382 CLEAR_PENDING_EXCEPTION; 383 } 384 385 if (non_array) { 386 Klass* k = SystemDictionary::resolve_or_null(class_name_symbol, CHECK_NULL); 387 if (k != NULL) { 388 klass = k; 389 } else { 390 if (!HAS_PENDING_EXCEPTION) { 391 THROW_NULL(vmSymbols::java_lang_ClassNotFoundException()); 392 } 393 } 394 } 395 } 396 } else { 397 // If "source:" tag is specified, all super class and super interfaces must be specified in the 398 // class list file. 399 klass = load_class_from_source(class_name_symbol, CHECK_NULL); 400 } 401 402 if (klass != NULL && klass->is_instance_klass() && is_id_specified()) { 403 InstanceKlass* ik = InstanceKlass::cast(klass); 404 int id = this->id(); 405 SystemDictionaryShared::update_shared_entry(ik, id); 406 InstanceKlass** old_ptr = table()->lookup(id); 407 if (old_ptr != NULL) { 408 error("Duplicated ID %d for class %s", id, _class_name); 409 } 410 table()->add(id, ik); 411 } 412 413 return klass; 414 } 415 416 bool ClassListParser::is_loading_from_source() { 417 return (_source != NULL); 418 } 419 420 InstanceKlass* ClassListParser::lookup_class_by_id(int id) { 421 InstanceKlass** klass_ptr = table()->lookup(id); 422 if (klass_ptr == NULL) { 423 error("Class ID %d has not been defined", id); 424 } 425 assert(*klass_ptr != NULL, "must be"); 426 return *klass_ptr; 427 } 428 429 430 InstanceKlass* ClassListParser::lookup_super_for_current_class(Symbol* super_name) { 431 if (!is_loading_from_source()) { 432 return NULL; 433 } 434 435 InstanceKlass* k = lookup_class_by_id(super()); 436 if (super_name != k->name()) { 437 error("The specified super class %s (id %d) does not match actual super class %s", 438 k->name()->as_klass_external_name(), super(), 439 super_name->as_klass_external_name()); 440 } 441 return k; 442 } 443 444 InstanceKlass* ClassListParser::lookup_interface_for_current_class(Symbol* interface_name) { 445 if (!is_loading_from_source()) { 446 return NULL; 447 } 448 449 const int n = _interfaces->length(); 450 if (n == 0) { 451 error("Class %s implements the interface %s, but no interface has been specified in the input line", 452 _class_name, interface_name->as_klass_external_name()); 453 ShouldNotReachHere(); 454 } 455 456 int i; 457 for (i=0; i<n; i++) { 458 InstanceKlass* k = lookup_class_by_id(_interfaces->at(i)); 459 if (interface_name == k->name()) { 460 return k; 461 } 462 } 463 464 // interface_name is not specified by the "interfaces:" keyword. 465 print_specified_interfaces(); 466 error("The interface %s implemented by class %s does not match any of the specified interface IDs", 467 interface_name->as_klass_external_name(), _class_name); 468 ShouldNotReachHere(); 469 return NULL; 470 }