--- old/src/hotspot/share/memory/heapShared.cpp 2020-07-26 22:10:06.325538521 -0700 +++ new/src/hotspot/share/memory/heapShared.cpp 2020-07-26 22:10:05.997526174 -0700 @@ -136,17 +136,21 @@ return NULL; } - // Pre-compute object identity hash at CDS dump time. - obj->identity_hash(); - oop archived_oop = (oop)G1CollectedHeap::heap()->archive_mem_allocate(len); if (archived_oop != NULL) { Copy::aligned_disjoint_words(cast_from_oop(obj), cast_from_oop(archived_oop), len); MetaspaceShared::relocate_klass_ptr(archived_oop); - // Clear age -- it might have been set if a GC happened during -Xshare:dump - markWord mark = archived_oop->mark_raw(); - mark = mark.set_age(0); - archived_oop->set_mark_raw(mark); + // Reinitialize markword to remove age/marking/locking/etc. + // + // We need to retain the identity_hash, because it may have been used by some hashtables + // in the shared heap. This also has the side effect of pre-initializing the + // identity_hash for all shared objects, so they are less likely to be written + // into during run time, increasing the potential of memory sharing. + int hash_original = obj->identity_hash(); + archived_oop->set_mark_raw(markWord::prototype().copy_set_hash(hash_original)); + assert(archived_oop->mark().is_unlocked(), "sanity"); + int hash_archived = archived_oop->identity_hash(); + assert(hash_original == hash_archived, "Different hash codes: original %x, archived %x", hash_original, hash_archived); ArchivedObjectCache* cache = archived_object_cache(); cache->put(obj, archived_oop); log_debug(cds, heap)("Archived heap object " PTR_FORMAT " ==> " PTR_FORMAT, --- old/test/hotspot/jtreg/runtime/cds/appcds/jvmti/dumpingWithAgent/SimpleAgent.java 2020-07-26 22:10:07.525583693 -0700 +++ new/test/hotspot/jtreg/runtime/cds/appcds/jvmti/dumpingWithAgent/SimpleAgent.java 2020-07-26 22:10:07.213571948 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,8 +23,29 @@ */ import java.lang.instrument.Instrumentation; -public class SimpleAgent { +public class SimpleAgent implements Runnable { public static void premain(String agentArg, Instrumentation instrumentation) { System.out.println("inside SimpleAgent"); + + Thread t = new Thread(new SimpleAgent()); + t.setDaemon(true); + t.start(); + } + + // Test for JDK-8249276: make sure we can handle objects that are locked during + // -Xshare:dump + public void run() { + try { + while (true) { + System.out.println("Let's wait ....."); + synchronized (Object.class) { + Object.class.wait(); + } + System.out.println("Huh?? notified??"); + } + } catch (Throwable t) { + System.err.println("Unexpected: " + t); + throw new RuntimeException(t); + } } }