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