112 // or retransform classes in this module. Only one of them is adding the
113 // default read edges to the unnamed modules of the boot and app class loaders
114 // with an upcall to jdk.internal.module.Modules.transformedByAgent.
115 // At the same time, another thread can instrument the module classes by
116 // injecting dependencies that require the default read edges for resolution.
117 if (this->has_default_read_edges() && !m->is_named()) {
118 ClassLoaderData* cld = m->loader_data();
119 if (cld->is_the_null_class_loader_data() || cld->is_system_class_loader_data()) {
120 return true; // default read edge
121 }
122 }
123 if (!has_reads()) {
124 return false;
125 } else {
126 return _reads->contains(m);
127 }
128 }
129
130 // Add a new module to this module's reads list
131 void ModuleEntry::add_read(ModuleEntry* m) {
132 MutexLocker m1(Module_lock);
133 if (m == NULL) {
134 set_can_read_all_unnamed();
135 } else {
136 if (_reads == NULL) {
137 // Lazily create a module's reads list
138 _reads = new (ResourceObj::C_HEAP, mtModule)GrowableArray<ModuleEntry*>(MODULE_READS_SIZE, true);
139 }
140
141 // Determine, based on this newly established read edge to module m,
142 // if this module's read list should be walked at a GC safepoint.
143 set_read_walk_required(m->loader_data());
144
145 // Establish readability to module m
146 _reads->append_if_missing(m);
147 }
148 }
149
150 // If the module's loader, that a read edge is being established to, is
151 // not the same loader as this module's and is not one of the 3 builtin
152 // class loaders, then this module's reads list must be walked at GC
153 // safepoint. Modules have the same life cycle as their defining class
154 // loaders and should be removed if dead.
155 void ModuleEntry::set_read_walk_required(ClassLoaderData* m_loader_data) {
156 assert_locked_or_safepoint(Module_lock);
157 if (!_must_walk_reads &&
158 loader_data() != m_loader_data &&
159 !m_loader_data->is_builtin_class_loader_data()) {
160 _must_walk_reads = true;
161 if (log_is_enabled(Trace, modules)) {
162 ResourceMark rm;
163 log_trace(modules)("ModuleEntry::set_read_walk_required(): module %s reads list must be walked",
164 (name() != NULL) ? name()->as_C_string() : UNNAMED_MODULE);
165 }
166 }
167 }
168
169 bool ModuleEntry::has_reads() const {
170 assert_locked_or_safepoint(Module_lock);
171 return ((_reads != NULL) && !_reads->is_empty());
172 }
173
174 // Purge dead module entries out of reads list.
175 void ModuleEntry::purge_reads() {
176 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
177
178 if (_must_walk_reads && has_reads()) {
179 // This module's _must_walk_reads flag will be reset based
180 // on the remaining live modules on the reads list.
181 _must_walk_reads = false;
182
183 if (log_is_enabled(Trace, modules)) {
184 ResourceMark rm;
185 log_trace(modules)("ModuleEntry::purge_reads(): module %s reads list being walked",
186 (name() != NULL) ? name()->as_C_string() : UNNAMED_MODULE);
187 }
188
202 }
203
204 void ModuleEntry::module_reads_do(ModuleClosure* const f) {
205 assert_locked_or_safepoint(Module_lock);
206 assert(f != NULL, "invariant");
207
208 if (has_reads()) {
209 int reads_len = _reads->length();
210 for (int i = 0; i < reads_len; ++i) {
211 f->do_module(_reads->at(i));
212 }
213 }
214 }
215
216 void ModuleEntry::delete_reads() {
217 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
218 delete _reads;
219 _reads = NULL;
220 }
221
222 ModuleEntryTable::ModuleEntryTable(int table_size)
223 : Hashtable<Symbol*, mtModule>(table_size, sizeof(ModuleEntry)), _unnamed_module(NULL)
224 {
225 }
226
227 ModuleEntryTable::~ModuleEntryTable() {
228 assert_locked_or_safepoint(Module_lock);
229
230 // Walk through all buckets and all entries in each bucket,
231 // freeing each entry.
232 for (int i = 0; i < table_size(); ++i) {
233 for (ModuleEntry* m = bucket(i); m != NULL;) {
234 ModuleEntry* to_remove = m;
235 // read next before freeing.
236 m = m->next();
237
238 ResourceMark rm;
239 log_debug(modules)("ModuleEntryTable: deleting module: %s", to_remove->name() != NULL ?
240 to_remove->name()->as_C_string() : UNNAMED_MODULE);
241
242 // Clean out the C heap allocated reads list first before freeing the entry
243 to_remove->delete_reads();
244 if (to_remove->name() != NULL) {
245 to_remove->name()->decrement_refcount();
246 }
247 if (to_remove->version() != NULL) {
248 to_remove->version()->decrement_refcount();
249 }
250 if (to_remove->location() != NULL) {
251 to_remove->location()->decrement_refcount();
252 }
253
254 // Unlink from the Hashtable prior to freeing
255 unlink_entry(to_remove);
256 FREE_C_HEAP_ARRAY(char, to_remove);
257 }
258 }
259 assert(number_of_entries() == 0, "should have removed all entries");
260 assert(new_entry_free_list() == NULL, "entry present on ModuleEntryTable's free list");
261 free_buckets();
262 }
263
264 void ModuleEntryTable::create_unnamed_module(ClassLoaderData* loader_data) {
265 assert(Module_lock->owned_by_self(), "should have the Module_lock");
266
267 // Each ModuleEntryTable has exactly one unnamed module
268 if (loader_data->is_the_null_class_loader_data()) {
269 // For the boot loader, the java.lang.reflect.Module for the unnamed module
270 // is not known until a call to JVM_SetBootLoaderUnnamedModule is made. At
271 // this point initially create the ModuleEntry for the unnamed module.
272 _unnamed_module = new_entry(0, Handle(), NULL, NULL, NULL, loader_data);
273 } else {
274 // For all other class loaders the java.lang.reflect.Module for their
275 // corresponding unnamed module can be found in the java.lang.ClassLoader object.
276 oop module = java_lang_ClassLoader::unnamedModule(loader_data->class_loader());
277 _unnamed_module = new_entry(0, Handle(Thread::current(), module), NULL, NULL, NULL, loader_data);
278
279 // Store pointer to the ModuleEntry in the unnamed module's java.lang.reflect.Module
280 // object.
281 java_lang_reflect_Module::set_module_entry(module, _unnamed_module);
282 }
283
284 // Add to bucket 0, no name to hash on
285 add_entry(0, _unnamed_module);
286 }
287
288 ModuleEntry* ModuleEntryTable::new_entry(unsigned int hash, Handle module_handle, Symbol* name,
289 Symbol* version, Symbol* location,
290 ClassLoaderData* loader_data) {
291 assert(Module_lock->owned_by_self(), "should have the Module_lock");
292 ModuleEntry* entry = (ModuleEntry*) NEW_C_HEAP_ARRAY(char, entry_size(), mtModule);
293
294 // Initialize everything BasicHashtable would
295 entry->set_next(NULL);
296 entry->set_hash(hash);
297 entry->set_literal(name);
298
299 // Initialize fields specific to a ModuleEntry
300 entry->init();
301 if (name != NULL) {
302 name->increment_refcount();
303 } else {
304 // Unnamed modules can read all other unnamed modules.
305 entry->set_can_read_all_unnamed();
306 }
307
334 ModuleEntry* ModuleEntryTable::locked_create_entry_or_null(Handle module_handle,
335 Symbol* module_name,
336 Symbol* module_version,
337 Symbol* module_location,
338 ClassLoaderData* loader_data) {
339 assert(module_name != NULL, "ModuleEntryTable locked_create_entry_or_null should never be called for unnamed module.");
340 assert(Module_lock->owned_by_self(), "should have the Module_lock");
341 // Check if module already exists.
342 if (lookup_only(module_name) != NULL) {
343 return NULL;
344 } else {
345 ModuleEntry* entry = new_entry(compute_hash(module_name), module_handle, module_name,
346 module_version, module_location, loader_data);
347 add_entry(index_for(module_name), entry);
348 return entry;
349 }
350 }
351
352 // lookup_only by Symbol* to find a ModuleEntry.
353 ModuleEntry* ModuleEntryTable::lookup_only(Symbol* name) {
354 if (name == NULL) {
355 // Return this table's unnamed module
356 return unnamed_module();
357 }
358 int index = index_for(name);
359 for (ModuleEntry* m = bucket(index); m != NULL; m = m->next()) {
360 if (m->name()->fast_compare(name) == 0) {
361 return m;
362 }
363 }
364 return NULL;
365 }
366
367 // Remove dead modules from all other alive modules' reads list.
368 // This should only occur at class unloading.
369 void ModuleEntryTable::purge_all_module_reads() {
370 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
371 for (int i = 0; i < table_size(); i++) {
372 for (ModuleEntry* entry = bucket(i);
373 entry != NULL;
374 entry = entry->next()) {
375 entry->purge_reads();
376 }
377 }
|
112 // or retransform classes in this module. Only one of them is adding the
113 // default read edges to the unnamed modules of the boot and app class loaders
114 // with an upcall to jdk.internal.module.Modules.transformedByAgent.
115 // At the same time, another thread can instrument the module classes by
116 // injecting dependencies that require the default read edges for resolution.
117 if (this->has_default_read_edges() && !m->is_named()) {
118 ClassLoaderData* cld = m->loader_data();
119 if (cld->is_the_null_class_loader_data() || cld->is_system_class_loader_data()) {
120 return true; // default read edge
121 }
122 }
123 if (!has_reads()) {
124 return false;
125 } else {
126 return _reads->contains(m);
127 }
128 }
129
130 // Add a new module to this module's reads list
131 void ModuleEntry::add_read(ModuleEntry* m) {
132 // Unnamed module is special cased and can read all modules
133 if (!is_named()) {
134 return;
135 }
136
137 MutexLocker m1(Module_lock);
138 if (m == NULL) {
139 set_can_read_all_unnamed();
140 } else {
141 if (_reads == NULL) {
142 // Lazily create a module's reads list
143 _reads = new (ResourceObj::C_HEAP, mtModule)GrowableArray<ModuleEntry*>(MODULE_READS_SIZE, true);
144 }
145
146 // Determine, based on this newly established read edge to module m,
147 // if this module's read list should be walked at a GC safepoint.
148 set_read_walk_required(m->loader_data());
149
150 // Establish readability to module m
151 _reads->append_if_missing(m);
152 }
153 }
154
155 // If the module's loader, that a read edge is being established to, is
156 // not the same loader as this module's and is not one of the 3 builtin
157 // class loaders, then this module's reads list must be walked at GC
158 // safepoint. Modules have the same life cycle as their defining class
159 // loaders and should be removed if dead.
160 void ModuleEntry::set_read_walk_required(ClassLoaderData* m_loader_data) {
161 assert(is_named(), "Cannot call set_read_walk_required on unnamed module");
162 assert_locked_or_safepoint(Module_lock);
163 if (!_must_walk_reads &&
164 loader_data() != m_loader_data &&
165 !m_loader_data->is_builtin_class_loader_data()) {
166 _must_walk_reads = true;
167 if (log_is_enabled(Trace, modules)) {
168 ResourceMark rm;
169 log_trace(modules)("ModuleEntry::set_read_walk_required(): module %s reads list must be walked",
170 (name() != NULL) ? name()->as_C_string() : UNNAMED_MODULE);
171 }
172 }
173 }
174
175 // Returns true if the module has a non-empty reads list. As such, the unnamed
176 // module will return false.
177 bool ModuleEntry::has_reads() const {
178 assert_locked_or_safepoint(Module_lock);
179 return ((_reads != NULL) && !_reads->is_empty());
180 }
181
182 // Purge dead module entries out of reads list.
183 void ModuleEntry::purge_reads() {
184 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
185
186 if (_must_walk_reads && has_reads()) {
187 // This module's _must_walk_reads flag will be reset based
188 // on the remaining live modules on the reads list.
189 _must_walk_reads = false;
190
191 if (log_is_enabled(Trace, modules)) {
192 ResourceMark rm;
193 log_trace(modules)("ModuleEntry::purge_reads(): module %s reads list being walked",
194 (name() != NULL) ? name()->as_C_string() : UNNAMED_MODULE);
195 }
196
210 }
211
212 void ModuleEntry::module_reads_do(ModuleClosure* const f) {
213 assert_locked_or_safepoint(Module_lock);
214 assert(f != NULL, "invariant");
215
216 if (has_reads()) {
217 int reads_len = _reads->length();
218 for (int i = 0; i < reads_len; ++i) {
219 f->do_module(_reads->at(i));
220 }
221 }
222 }
223
224 void ModuleEntry::delete_reads() {
225 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
226 delete _reads;
227 _reads = NULL;
228 }
229
230 ModuleEntry* ModuleEntry::create_unnamed_module(ClassLoaderData* cld) {
231 // The java.lang.Module for this loader's
232 // corresponding unnamed module can be found in the java.lang.ClassLoader object.
233 oop module = java_lang_ClassLoader::unnamedModule(cld->class_loader());
234 ModuleEntry* unnamed_module = new_unnamed_module_entry(Handle(Thread::current(), module), cld);
235
236 // Store pointer to the ModuleEntry in the unnamed module's java.lang.Module
237 // object.
238 java_lang_reflect_Module::set_module_entry(module, unnamed_module);
239
240 return unnamed_module;
241 }
242
243 ModuleEntry* ModuleEntry::create_boot_unnamed_module(ClassLoaderData* cld) {
244 // For the boot loader, the java.lang.Module for the unnamed module
245 // is not known until a call to JVM_SetBootLoaderUnnamedModule is made. At
246 // this point initially create the ModuleEntry for the unnamed module.
247 ModuleEntry* unnamed_module = new_unnamed_module_entry(Handle(), cld);
248 assert(unnamed_module != NULL, "boot loader unnamed module should not be null");
249 return unnamed_module;
250 }
251
252 // When creating an unnamed module, this is called without holding the Module_lock.
253 // This is okay because the unnamed module gets created before the ClassLoaderData
254 // is available to other threads.
255 ModuleEntry* ModuleEntry::new_unnamed_module_entry(Handle module_handle, ClassLoaderData* cld) {
256 ModuleEntry* entry = (ModuleEntry*) NEW_C_HEAP_ARRAY(char, sizeof(ModuleEntry), mtModule);
257
258 // Initialize everything BasicHashtable would
259 entry->set_next(NULL);
260 entry->set_hash(0);
261 entry->set_literal(NULL);
262
263 // Initialize fields specific to a ModuleEntry
264 entry->init();
265
266 // Unnamed modules can read all other unnamed modules.
267 entry->set_can_read_all_unnamed();
268
269 if (!module_handle.is_null()) {
270 entry->set_module(cld->add_handle(module_handle));
271 }
272
273 entry->set_loader_data(cld);
274
275 TRACE_INIT_ID(entry);
276
277 return entry;
278 }
279
280 ModuleEntryTable::ModuleEntryTable(int table_size)
281 : Hashtable<Symbol*, mtModule>(table_size, sizeof(ModuleEntry))
282 {
283 }
284
285 ModuleEntryTable::~ModuleEntryTable() {
286 assert_locked_or_safepoint(Module_lock);
287
288 // Walk through all buckets and all entries in each bucket,
289 // freeing each entry.
290 for (int i = 0; i < table_size(); ++i) {
291 for (ModuleEntry* m = bucket(i); m != NULL;) {
292 ModuleEntry* to_remove = m;
293 // read next before freeing.
294 m = m->next();
295
296 ResourceMark rm;
297 log_debug(modules)("ModuleEntryTable: deleting module: %s", to_remove->name() != NULL ?
298 to_remove->name()->as_C_string() : UNNAMED_MODULE);
299
300 // Clean out the C heap allocated reads list first before freeing the entry
301 to_remove->delete_reads();
302 if (to_remove->name() != NULL) {
303 to_remove->name()->decrement_refcount();
304 }
305 if (to_remove->version() != NULL) {
306 to_remove->version()->decrement_refcount();
307 }
308 if (to_remove->location() != NULL) {
309 to_remove->location()->decrement_refcount();
310 }
311
312 // Unlink from the Hashtable prior to freeing
313 unlink_entry(to_remove);
314 FREE_C_HEAP_ARRAY(char, to_remove);
315 }
316 }
317 assert(number_of_entries() == 0, "should have removed all entries");
318 assert(new_entry_free_list() == NULL, "entry present on ModuleEntryTable's free list");
319 free_buckets();
320 }
321
322 ModuleEntry* ModuleEntryTable::new_entry(unsigned int hash, Handle module_handle, Symbol* name,
323 Symbol* version, Symbol* location,
324 ClassLoaderData* loader_data) {
325 assert(Module_lock->owned_by_self(), "should have the Module_lock");
326 ModuleEntry* entry = (ModuleEntry*) NEW_C_HEAP_ARRAY(char, entry_size(), mtModule);
327
328 // Initialize everything BasicHashtable would
329 entry->set_next(NULL);
330 entry->set_hash(hash);
331 entry->set_literal(name);
332
333 // Initialize fields specific to a ModuleEntry
334 entry->init();
335 if (name != NULL) {
336 name->increment_refcount();
337 } else {
338 // Unnamed modules can read all other unnamed modules.
339 entry->set_can_read_all_unnamed();
340 }
341
368 ModuleEntry* ModuleEntryTable::locked_create_entry_or_null(Handle module_handle,
369 Symbol* module_name,
370 Symbol* module_version,
371 Symbol* module_location,
372 ClassLoaderData* loader_data) {
373 assert(module_name != NULL, "ModuleEntryTable locked_create_entry_or_null should never be called for unnamed module.");
374 assert(Module_lock->owned_by_self(), "should have the Module_lock");
375 // Check if module already exists.
376 if (lookup_only(module_name) != NULL) {
377 return NULL;
378 } else {
379 ModuleEntry* entry = new_entry(compute_hash(module_name), module_handle, module_name,
380 module_version, module_location, loader_data);
381 add_entry(index_for(module_name), entry);
382 return entry;
383 }
384 }
385
386 // lookup_only by Symbol* to find a ModuleEntry.
387 ModuleEntry* ModuleEntryTable::lookup_only(Symbol* name) {
388 assert(name != NULL, "name cannot be NULL");
389 int index = index_for(name);
390 for (ModuleEntry* m = bucket(index); m != NULL; m = m->next()) {
391 if (m->name()->fast_compare(name) == 0) {
392 return m;
393 }
394 }
395 return NULL;
396 }
397
398 // Remove dead modules from all other alive modules' reads list.
399 // This should only occur at class unloading.
400 void ModuleEntryTable::purge_all_module_reads() {
401 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
402 for (int i = 0; i < table_size(); i++) {
403 for (ModuleEntry* entry = bucket(i);
404 entry != NULL;
405 entry = entry->next()) {
406 entry->purge_reads();
407 }
408 }
|