選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

light-jdk8u5-b13.patch 148KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679
  1. diff --git a/src/share/vm/ci/ciObjectFactory.cpp b/src/share/vm/ci/ciObjectFactory.cpp
  2. --- a/src/share/vm/ci/ciObjectFactory.cpp
  3. +++ b/src/share/vm/ci/ciObjectFactory.cpp
  4. @@ -750,3 +750,27 @@
  5. _unloaded_instances->length(),
  6. _unloaded_klasses->length());
  7. }
  8. +
  9. +int ciObjectFactory::compare_cimetadata(ciMetadata** a, ciMetadata** b) {
  10. + Metadata* am = (*a)->constant_encoding();
  11. + Metadata* bm = (*b)->constant_encoding();
  12. + return ((am > bm) ? 1 : ((am == bm) ? 0 : -1));
  13. +}
  14. +
  15. +// (DCEVM) Resoring the ciObject arrays after class redefinition
  16. +void ciObjectFactory::resort_shared_ci_metadata() {
  17. + if (_shared_ci_metadata == NULL) return;
  18. + _shared_ci_metadata->sort(ciObjectFactory::compare_cimetadata);
  19. +
  20. +#ifdef ASSERT
  21. + if (CIObjectFactoryVerify) {
  22. + Metadata* last = NULL;
  23. + for (int j = 0; j< _shared_ci_metadata->length(); j++) {
  24. + Metadata* o = _shared_ci_metadata->at(j)->constant_encoding();
  25. + assert(last < o, "out of order");
  26. + last = o;
  27. + }
  28. + }
  29. +#endif // ASSERT
  30. +}
  31. +
  32. diff --git a/src/share/vm/ci/ciObjectFactory.hpp b/src/share/vm/ci/ciObjectFactory.hpp
  33. --- a/src/share/vm/ci/ciObjectFactory.hpp
  34. +++ b/src/share/vm/ci/ciObjectFactory.hpp
  35. @@ -90,6 +90,7 @@
  36. ciInstance* get_unloaded_instance(ciInstanceKlass* klass);
  37. + static int compare_cimetadata(ciMetadata** a, ciMetadata** b);
  38. public:
  39. static bool is_initialized() { return _initialized; }
  40. @@ -145,6 +146,8 @@
  41. void print_contents();
  42. void print();
  43. +
  44. + static void resort_shared_ci_metadata();
  45. };
  46. #endif // SHARE_VM_CI_CIOBJECTFACTORY_HPP
  47. diff --git a/src/share/vm/classfile/classFileParser.cpp b/src/share/vm/classfile/classFileParser.cpp
  48. --- a/src/share/vm/classfile/classFileParser.cpp
  49. +++ b/src/share/vm/classfile/classFileParser.cpp
  50. @@ -759,6 +759,7 @@
  51. Array<Klass*>* ClassFileParser::parse_interfaces(int length,
  52. Handle protection_domain,
  53. Symbol* class_name,
  54. + bool pick_newest,
  55. bool* has_default_methods,
  56. TRAPS) {
  57. if (length == 0) {
  58. @@ -777,7 +778,11 @@
  59. "Interface name has bad constant pool index %u in class file %s",
  60. interface_index, CHECK_NULL);
  61. if (_cp->tag_at(interface_index).is_klass()) {
  62. - interf = KlassHandle(THREAD, _cp->resolved_klass_at(interface_index));
  63. + Klass* resolved_klass = _cp->resolved_klass_at(interface_index);
  64. + if (pick_newest) {
  65. + resolved_klass = resolved_klass->newest_version();
  66. + }
  67. + interf = KlassHandle(THREAD, resolved_klass);
  68. } else {
  69. Symbol* unresolved_klass = _cp->klass_name_at(interface_index);
  70. @@ -791,6 +796,9 @@
  71. Klass* k = SystemDictionary::resolve_super_or_fail(class_name,
  72. unresolved_klass, class_loader, protection_domain,
  73. false, CHECK_NULL);
  74. + if (pick_newest) {
  75. + k = k->newest_version();
  76. + }
  77. interf = KlassHandle(THREAD, k);
  78. }
  79. @@ -3093,6 +3101,7 @@
  80. }
  81. instanceKlassHandle ClassFileParser::parse_super_class(int super_class_index,
  82. + bool pick_newest,
  83. TRAPS) {
  84. instanceKlassHandle super_klass;
  85. if (super_class_index == 0) {
  86. @@ -3109,7 +3118,11 @@
  87. // However, make sure it is not an array type.
  88. bool is_array = false;
  89. if (_cp->tag_at(super_class_index).is_klass()) {
  90. - super_klass = instanceKlassHandle(THREAD, _cp->resolved_klass_at(super_class_index));
  91. + Klass* resolved_klass = _cp->resolved_klass_at(super_class_index);
  92. + if (pick_newest) {
  93. + resolved_klass = resolved_klass->newest_version();
  94. + }
  95. + super_klass = instanceKlassHandle(THREAD, resolved_klass);
  96. if (_need_verify)
  97. is_array = super_klass->oop_is_array();
  98. } else if (_need_verify) {
  99. @@ -3658,8 +3671,10 @@
  100. instanceKlassHandle ClassFileParser::parseClassFile(Symbol* name,
  101. ClassLoaderData* loader_data,
  102. Handle protection_domain,
  103. + KlassHandle old_klass,
  104. KlassHandle host_klass,
  105. GrowableArray<Handle>* cp_patches,
  106. + GrowableArray<Symbol*>* parsed_super_symbols,
  107. TempNewSymbol& parsed_name,
  108. bool verify,
  109. TRAPS) {
  110. @@ -3672,6 +3687,7 @@
  111. JvmtiCachedClassFileData *cached_class_file = NULL;
  112. Handle class_loader(THREAD, loader_data->class_loader());
  113. bool has_default_methods = false;
  114. + bool pick_newest = !old_klass.is_null();
  115. ResourceMark rm(THREAD);
  116. ClassFileStream* cfs = stream();
  117. @@ -3688,7 +3704,7 @@
  118. init_parsed_class_attributes(loader_data);
  119. - if (JvmtiExport::should_post_class_file_load_hook()) {
  120. + if (parsed_super_symbols == NULL && JvmtiExport::should_post_class_file_load_hook()) {
  121. // Get the cached class file bytes (if any) from the class that
  122. // is being redefined or retransformed. We use jvmti_thread_state()
  123. // instead of JvmtiThreadState::state_for(jt) so we don't allocate
  124. @@ -3823,6 +3839,26 @@
  125. CHECK_(nullHandle));
  126. }
  127. + // (DCEVM) Do not parse full class file, only get super symbols and return.
  128. + if (parsed_super_symbols != NULL) {
  129. + u2 super_class_index = cfs->get_u2_fast();
  130. +
  131. + if (super_class_index != 0) {
  132. + parsed_super_symbols->append(cp->klass_name_at(super_class_index));
  133. + }
  134. +
  135. + // Interfaces
  136. + u2 itfs_len = cfs->get_u2_fast();
  137. + Array<Klass*>* local_interfaces =
  138. + parse_interfaces(itfs_len, protection_domain, _class_name, pick_newest, &has_default_methods, CHECK_NULL);
  139. +
  140. + for (int i = 0; i < local_interfaces->length(); i++) {
  141. + Klass* o = local_interfaces->at(i);
  142. + parsed_super_symbols->append(o->name());
  143. + }
  144. + return NULL;
  145. + }
  146. +
  147. Klass* preserve_this_klass; // for storing result across HandleMark
  148. // release all handles when parsing is done
  149. @@ -3849,13 +3885,14 @@
  150. u2 super_class_index = cfs->get_u2_fast();
  151. instanceKlassHandle super_klass = parse_super_class(super_class_index,
  152. + pick_newest,
  153. CHECK_NULL);
  154. // Interfaces
  155. u2 itfs_len = cfs->get_u2_fast();
  156. Array<Klass*>* local_interfaces =
  157. parse_interfaces(itfs_len, protection_domain, _class_name,
  158. - &has_default_methods, CHECK_(nullHandle));
  159. + pick_newest, &has_default_methods, CHECK_(nullHandle));
  160. u2 java_fields_count = 0;
  161. // Fields (offsets are filled in later)
  162. @@ -3897,6 +3934,9 @@
  163. true,
  164. CHECK_(nullHandle));
  165. + if (pick_newest) {
  166. + k = k->newest_version();
  167. + }
  168. KlassHandle kh (THREAD, k);
  169. super_klass = instanceKlassHandle(THREAD, kh());
  170. }
  171. @@ -4056,7 +4096,7 @@
  172. fill_oop_maps(this_klass, info.nonstatic_oop_map_count, info.nonstatic_oop_offsets, info.nonstatic_oop_counts);
  173. // Fill in has_finalizer, has_vanilla_constructor, and layout_helper
  174. - set_precomputed_flags(this_klass);
  175. + set_precomputed_flags(this_klass, old_klass);
  176. // reinitialize modifiers, using the InnerClasses attribute
  177. int computed_modifiers = this_klass->compute_modifier_flags(CHECK_(nullHandle));
  178. @@ -4283,7 +4323,7 @@
  179. }
  180. -void ClassFileParser::set_precomputed_flags(instanceKlassHandle k) {
  181. +void ClassFileParser::set_precomputed_flags(instanceKlassHandle k, KlassHandle old_klass) {
  182. Klass* super = k->super();
  183. // Check if this klass has an empty finalize method (i.e. one with return bytecode only),
  184. @@ -4291,7 +4331,9 @@
  185. if (!_has_empty_finalizer) {
  186. if (_has_finalizer ||
  187. (super != NULL && super->has_finalizer())) {
  188. - k->set_has_finalizer();
  189. + if (old_klass.is_null() || old_klass->has_finalizer()) {
  190. + k->set_has_finalizer();
  191. + }
  192. }
  193. }
  194. @@ -4307,7 +4349,7 @@
  195. // Check if this klass supports the java.lang.Cloneable interface
  196. if (SystemDictionary::Cloneable_klass_loaded()) {
  197. - if (k->is_subtype_of(SystemDictionary::Cloneable_klass())) {
  198. + if (k->is_subtype_of(SystemDictionary::Cloneable_klass()) || k->is_subtype_of(SystemDictionary::Cloneable_klass()->newest_version())) {
  199. k->set_is_cloneable();
  200. }
  201. }
  202. diff --git a/src/share/vm/classfile/classFileParser.hpp b/src/share/vm/classfile/classFileParser.hpp
  203. --- a/src/share/vm/classfile/classFileParser.hpp
  204. +++ b/src/share/vm/classfile/classFileParser.hpp
  205. @@ -214,11 +214,12 @@
  206. Array<Klass*>* parse_interfaces(int length,
  207. Handle protection_domain,
  208. Symbol* class_name,
  209. + bool pick_newest,
  210. bool* has_default_methods,
  211. TRAPS);
  212. void record_defined_class_dependencies(instanceKlassHandle defined_klass, TRAPS);
  213. - instanceKlassHandle parse_super_class(int super_class_index, TRAPS);
  214. + instanceKlassHandle parse_super_class(int super_class_index, bool pick_newest, TRAPS);
  215. // Field parsing
  216. void parse_field_attributes(u2 attributes_count,
  217. bool is_static, u2 signature_index,
  218. @@ -299,7 +300,7 @@
  219. unsigned int nonstatic_oop_map_count,
  220. int* nonstatic_oop_offsets,
  221. unsigned int* nonstatic_oop_counts);
  222. - void set_precomputed_flags(instanceKlassHandle k);
  223. + void set_precomputed_flags(instanceKlassHandle k, KlassHandle old_klass);
  224. Array<Klass*>* compute_transitive_interfaces(instanceKlassHandle super,
  225. Array<Klass*>* local_ifs, TRAPS);
  226. @@ -461,17 +462,20 @@
  227. instanceKlassHandle parseClassFile(Symbol* name,
  228. ClassLoaderData* loader_data,
  229. Handle protection_domain,
  230. + KlassHandle old_klass,
  231. TempNewSymbol& parsed_name,
  232. bool verify,
  233. TRAPS) {
  234. KlassHandle no_host_klass;
  235. - return parseClassFile(name, loader_data, protection_domain, no_host_klass, NULL, parsed_name, verify, THREAD);
  236. + return parseClassFile(name, loader_data, protection_domain, old_klass, no_host_klass, NULL, NULL, parsed_name, verify, THREAD);
  237. }
  238. instanceKlassHandle parseClassFile(Symbol* name,
  239. ClassLoaderData* loader_data,
  240. Handle protection_domain,
  241. + KlassHandle old_klass,
  242. KlassHandle host_klass,
  243. GrowableArray<Handle>* cp_patches,
  244. + GrowableArray<Symbol*>* parsed_super_symbols,
  245. TempNewSymbol& parsed_name,
  246. bool verify,
  247. TRAPS);
  248. diff --git a/src/share/vm/classfile/classLoader.cpp b/src/share/vm/classfile/classLoader.cpp
  249. --- a/src/share/vm/classfile/classLoader.cpp
  250. +++ b/src/share/vm/classfile/classLoader.cpp
  251. @@ -926,6 +926,7 @@
  252. instanceKlassHandle result = parser.parseClassFile(h_name,
  253. loader_data,
  254. protection_domain,
  255. + KlassHandle(),
  256. parsed_name,
  257. false,
  258. CHECK_(h));
  259. diff --git a/src/share/vm/classfile/dictionary.cpp b/src/share/vm/classfile/dictionary.cpp
  260. --- a/src/share/vm/classfile/dictionary.cpp
  261. +++ b/src/share/vm/classfile/dictionary.cpp
  262. @@ -145,7 +145,7 @@
  263. InstanceKlass* ik = InstanceKlass::cast(e);
  264. // Non-unloadable classes were handled in always_strong_oops_do
  265. - if (!is_strongly_reachable(loader_data, e)) {
  266. + if (!ik->is_redefining() && !is_strongly_reachable(loader_data, e)) {
  267. // Entry was not visited in phase1 (negated test from phase1)
  268. assert(!loader_data->is_the_null_class_loader_data(), "unloading entry with null class loader");
  269. ClassLoaderData* k_def_class_loader_data = ik->class_loader_data();
  270. @@ -336,6 +336,32 @@
  271. add_entry(index, entry);
  272. }
  273. +// (DCEVM) Updates the klass entry to point to the new Klass*. Necessary only for class redefinition.
  274. +bool Dictionary::update_klass(int index, unsigned int hash, Symbol* name, ClassLoaderData* loader_data, KlassHandle k, KlassHandle old_class) {
  275. +
  276. + // There are several entries for the same class in the dictionary: One extra entry for each parent classloader of the classloader of the class.
  277. + bool found = false;
  278. + for (int index = 0; index < table_size(); index++) {
  279. + for (DictionaryEntry* entry = bucket(index); entry != NULL; entry = entry->next()) {
  280. + if (entry->klass() == old_class()) {
  281. + entry->set_literal(k());
  282. + found = true;
  283. + }
  284. + }
  285. + }
  286. + return found;
  287. +}
  288. +
  289. +// (DCEVM) Undo previous updates to the system dictionary
  290. +void Dictionary::rollback_redefinition() {
  291. + for (int index = 0; index < table_size(); index++) {
  292. + for (DictionaryEntry* entry = bucket(index); entry != NULL; entry = entry->next()) {
  293. + if (entry->klass()->is_redefining()) {
  294. + entry->set_literal(entry->klass()->old_version());
  295. + }
  296. + }
  297. + }
  298. +}
  299. // This routine does not lock the system dictionary.
  300. //
  301. @@ -366,7 +392,7 @@
  302. ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
  303. DictionaryEntry* entry = get_entry(index, hash, name, loader_data);
  304. if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) {
  305. - return entry->klass();
  306. + return intercept_for_version(entry->klass());
  307. } else {
  308. return NULL;
  309. }
  310. @@ -379,7 +405,7 @@
  311. assert (index == index_for(name, loader_data), "incorrect index?");
  312. DictionaryEntry* entry = get_entry(index, hash, name, loader_data);
  313. - return (entry != NULL) ? entry->klass() : (Klass*)NULL;
  314. + return intercept_for_version((entry != NULL) ? entry->klass() : (Klass*)NULL);
  315. }
  316. @@ -391,7 +417,7 @@
  317. assert (index == index_for(name, NULL), "incorrect index?");
  318. DictionaryEntry* entry = get_entry(index, hash, name, NULL);
  319. - return (entry != NULL) ? entry->klass() : (Klass*)NULL;
  320. + return intercept_for_version((entry != NULL) ? entry->klass() : (Klass*)NULL);
  321. }
  322. diff --git a/src/share/vm/classfile/dictionary.hpp b/src/share/vm/classfile/dictionary.hpp
  323. --- a/src/share/vm/classfile/dictionary.hpp
  324. +++ b/src/share/vm/classfile/dictionary.hpp
  325. @@ -78,6 +78,10 @@
  326. void add_klass(Symbol* class_name, ClassLoaderData* loader_data,KlassHandle obj);
  327. + bool update_klass(int index, unsigned int hash, Symbol* name, ClassLoaderData* loader_data, KlassHandle k, KlassHandle old_class);
  328. +
  329. + void rollback_redefinition();
  330. +
  331. Klass* find_class(int index, unsigned int hash,
  332. Symbol* name, ClassLoaderData* loader_data);
  333. @@ -107,6 +111,11 @@
  334. return (loader_data->is_the_null_class_loader_data() || !ClassUnloading);
  335. }
  336. + // (DCEVM) During enhanced class redefinition we want old version if new is being redefined
  337. + static Klass* intercept_for_version(Klass* k) {
  338. + return (k != NULL && k->is_redefining()) ? k->old_version() : k;
  339. + }
  340. +
  341. // Unload (that is, break root links to) all unmarked classes and
  342. // loaders. Returns "true" iff something was unloaded.
  343. bool do_unloading();
  344. diff --git a/src/share/vm/classfile/javaClasses.cpp b/src/share/vm/classfile/javaClasses.cpp
  345. --- a/src/share/vm/classfile/javaClasses.cpp
  346. +++ b/src/share/vm/classfile/javaClasses.cpp
  347. @@ -1629,6 +1629,8 @@
  348. skip_throwableInit_check = true;
  349. }
  350. }
  351. + // (DCEVM): Line numbers from newest version must be used for EMCP-swapped methods
  352. + method = method->newest_version();
  353. if (method->is_hidden()) {
  354. if (skip_hidden) continue;
  355. }
  356. diff --git a/src/share/vm/classfile/loaderConstraints.cpp b/src/share/vm/classfile/loaderConstraints.cpp
  357. --- a/src/share/vm/classfile/loaderConstraints.cpp
  358. +++ b/src/share/vm/classfile/loaderConstraints.cpp
  359. @@ -446,7 +446,7 @@
  360. if (k != NULL) {
  361. // We found the class in the system dictionary, so we should
  362. // make sure that the Klass* matches what we already have.
  363. - guarantee(k == probe->klass(), "klass should be in dictionary");
  364. + guarantee(k == probe->klass()->newest_version(), "klass should be in dictionary");
  365. } else {
  366. // If we don't find the class in the system dictionary, it
  367. // has to be in the placeholders table.
  368. diff --git a/src/share/vm/classfile/systemDictionary.cpp b/src/share/vm/classfile/systemDictionary.cpp
  369. --- a/src/share/vm/classfile/systemDictionary.cpp
  370. +++ b/src/share/vm/classfile/systemDictionary.cpp
  371. @@ -174,6 +174,7 @@
  372. // can return a null klass
  373. klass = handle_resolution_exception(class_name, class_loader, protection_domain, throw_error, k_h, THREAD);
  374. }
  375. + assert(klass == NULL || klass->is_newest_version() || klass->newest_version()->is_redefining(), "must be");
  376. return klass;
  377. }
  378. @@ -216,7 +217,7 @@
  379. // Forwards to resolve_instance_class_or_null
  380. Klass* SystemDictionary::resolve_or_null(Symbol* class_name, Handle class_loader, Handle protection_domain, TRAPS) {
  381. - assert(!THREAD->is_Compiler_thread(),
  382. + assert(!THREAD->is_Compiler_thread() || JvmtiThreadState::state_for(JavaThread::current())->get_class_being_redefined() != NULL,
  383. err_msg("can not load classes with compiler thread: class=%s, classloader=%s",
  384. class_name->as_C_string(),
  385. class_loader.is_null() ? "null" : class_loader->klass()->name()->as_C_string()));
  386. @@ -1029,8 +1030,10 @@
  387. instanceKlassHandle k = ClassFileParser(st).parseClassFile(class_name,
  388. loader_data,
  389. protection_domain,
  390. + KlassHandle(),
  391. host_klass,
  392. cp_patches,
  393. + NULL,
  394. parsed_name,
  395. true,
  396. THREAD);
  397. @@ -1085,6 +1088,7 @@
  398. Handle protection_domain,
  399. ClassFileStream* st,
  400. bool verify,
  401. + KlassHandle old_class,
  402. TRAPS) {
  403. // Classloaders that support parallelism, e.g. bootstrap classloader,
  404. @@ -1112,9 +1116,15 @@
  405. instanceKlassHandle k = ClassFileParser(st).parseClassFile(class_name,
  406. loader_data,
  407. protection_domain,
  408. + old_class,
  409. parsed_name,
  410. verify,
  411. THREAD);
  412. + // (DCEVM) During enhanced class redefinition, mark loaded class as being redefined
  413. + if (!old_class.is_null() && !k.is_null()) {
  414. + k->set_redefining(true);
  415. + k->set_old_version(old_class());
  416. + }
  417. const char* pkg = "java/";
  418. if (!HAS_PENDING_EXCEPTION &&
  419. @@ -1149,10 +1159,11 @@
  420. // Add class just loaded
  421. // If a class loader supports parallel classloading handle parallel define requests
  422. // find_or_define_instance_class may return a different InstanceKlass
  423. - if (is_parallelCapable(class_loader)) {
  424. + // (DCEVM) TODO: for class redefinition the parallel version does not work, check if this is a problem?
  425. + if (is_parallelCapable(class_loader) && old_class.is_null()) {
  426. k = find_or_define_instance_class(class_name, class_loader, k, THREAD);
  427. } else {
  428. - define_instance_class(k, THREAD);
  429. + define_instance_class(k, old_class, THREAD);
  430. }
  431. }
  432. @@ -1166,7 +1177,7 @@
  433. MutexLocker mu(SystemDictionary_lock, THREAD);
  434. Klass* check = find_class(parsed_name, loader_data);
  435. - assert(check == k(), "should be present in the dictionary");
  436. + assert((check == k() && !k->is_redefining()) || (k->is_redefining() && check == k->old_version()), "should be present in the dictionary");
  437. Klass* check2 = find_class(h_name, defining_loader_data);
  438. assert(check == check2, "name inconsistancy in SystemDictionary");
  439. @@ -1386,7 +1397,11 @@
  440. }
  441. }
  442. -void SystemDictionary::define_instance_class(instanceKlassHandle k, TRAPS) {
  443. +void SystemDictionary::rollback_redefinition() {
  444. + dictionary()->rollback_redefinition();
  445. +}
  446. +
  447. +void SystemDictionary::define_instance_class(instanceKlassHandle k, KlassHandle old_class, TRAPS) {
  448. ClassLoaderData* loader_data = k->class_loader_data();
  449. Handle class_loader_h(THREAD, loader_data->class_loader());
  450. @@ -1416,7 +1431,17 @@
  451. Symbol* name_h = k->name();
  452. unsigned int d_hash = dictionary()->compute_hash(name_h, loader_data);
  453. int d_index = dictionary()->hash_to_index(d_hash);
  454. - check_constraints(d_index, d_hash, k, class_loader_h, true, CHECK);
  455. +
  456. + // (DCEVM) Update version of the Klass* in the system dictionary
  457. + // TODO: Check for thread safety!
  458. + if (!old_class.is_null()) {
  459. + bool ok = dictionary()->update_klass(d_index, d_hash, name_h, loader_data, k, old_class);
  460. + assert (ok, "must have found old class and updated!");
  461. + }
  462. + check_constraints(d_index, d_hash, k, class_loader_h, old_class.is_null(), CHECK);
  463. +
  464. + // FIXME: (DCEVM) clean this...
  465. + if(!old_class.is_null() && TraceRedefineClasses >= 3){ tty->print_cr("Class has been updated!"); }
  466. // Register class just loaded with class loader (placed in Vector)
  467. // Note we do this before updating the dictionary, as this can
  468. @@ -1449,8 +1474,9 @@
  469. }
  470. k->eager_initialize(THREAD);
  471. + // (DCEVM) Only notify jvmti if not redefining a class.
  472. // notify jvmti
  473. - if (JvmtiExport::should_post_class_load()) {
  474. + if (JvmtiExport::should_post_class_load() && old_class.is_null()) {
  475. assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
  476. JvmtiExport::post_class_load((JavaThread *) THREAD, k());
  477. @@ -1524,7 +1550,7 @@
  478. }
  479. }
  480. - define_instance_class(k, THREAD);
  481. + define_instance_class(k, KlassHandle(), THREAD);
  482. Handle linkage_exception = Handle(); // null handle
  483. @@ -1654,6 +1680,14 @@
  484. Universe::flush_dependents_on(k);
  485. }
  486. +// (DCEVM) Remove from hierarchy - Undo add_to_hierarchy.
  487. +void SystemDictionary::remove_from_hierarchy(instanceKlassHandle k) {
  488. + assert(k.not_null(), "just checking");
  489. +
  490. + // remove receiver from sibling list
  491. + k->remove_from_sibling_list();
  492. + // TODO (DCEVM): Remove from interfaces.
  493. +}
  494. // ----------------------------------------------------------------------------
  495. // GC support
  496. @@ -2000,7 +2034,7 @@
  497. // also holds array classes
  498. assert(check->oop_is_instance(), "noninstance in systemdictionary");
  499. - if ((defining == true) || (k() != check)) {
  500. + if ((defining == true) && ((k() != check) && k->old_version() != check)) {
  501. linkage_error = "loader (instance of %s): attempted duplicate class "
  502. "definition for name: \"%s\"";
  503. } else {
  504. diff --git a/src/share/vm/classfile/systemDictionary.hpp b/src/share/vm/classfile/systemDictionary.hpp
  505. --- a/src/share/vm/classfile/systemDictionary.hpp
  506. +++ b/src/share/vm/classfile/systemDictionary.hpp
  507. @@ -271,7 +271,7 @@
  508. // Resolve from stream (called by jni_DefineClass and JVM_DefineClass)
  509. static Klass* resolve_from_stream(Symbol* class_name, Handle class_loader,
  510. Handle protection_domain,
  511. - ClassFileStream* st, bool verify, TRAPS);
  512. + ClassFileStream* st, bool verify, KlassHandle old_class, TRAPS);
  513. // Lookup an already loaded class. If not found NULL is returned.
  514. static Klass* find(Symbol* class_name, Handle class_loader, Handle protection_domain, TRAPS);
  515. @@ -341,6 +341,8 @@
  516. // System loader lock
  517. static oop system_loader_lock() { return _system_loader_lock_obj; }
  518. + // (DCEVM) Remove link to hierarchy
  519. + static void remove_from_hierarchy(instanceKlassHandle k);
  520. private:
  521. // Extended Redefine classes support (tbi)
  522. static void preloaded_classes_do(KlassClosure* f);
  523. @@ -410,6 +412,9 @@
  524. initialize_wk_klasses_until((WKID) limit, start_id, THREAD);
  525. }
  526. + // (DCEVM) rollback class redefinition
  527. + static void rollback_redefinition();
  528. +
  529. public:
  530. #define WK_KLASS_DECLARE(name, symbol, option) \
  531. static Klass* name() { return check_klass_##option(_well_known_klasses[WK_KLASS_ENUM_NAME(name)]); } \
  532. @@ -615,7 +620,7 @@
  533. // after waiting, but before reentering SystemDictionary_lock
  534. // to preserve lock order semantics.
  535. static void double_lock_wait(Handle lockObject, TRAPS);
  536. - static void define_instance_class(instanceKlassHandle k, TRAPS);
  537. + static void define_instance_class(instanceKlassHandle k, KlassHandle old_class, TRAPS);
  538. static instanceKlassHandle find_or_define_instance_class(Symbol* class_name,
  539. Handle class_loader,
  540. instanceKlassHandle k, TRAPS);
  541. diff --git a/src/share/vm/classfile/verifier.cpp b/src/share/vm/classfile/verifier.cpp
  542. --- a/src/share/vm/classfile/verifier.cpp
  543. +++ b/src/share/vm/classfile/verifier.cpp
  544. @@ -189,7 +189,7 @@
  545. Symbol* name = klass->name();
  546. Klass* refl_magic_klass = SystemDictionary::reflect_MagicAccessorImpl_klass();
  547. - bool is_reflect = refl_magic_klass != NULL && klass->is_subtype_of(refl_magic_klass);
  548. + bool is_reflect = refl_magic_klass != NULL && (klass->is_subtype_of(refl_magic_klass) || klass->is_subtype_of(refl_magic_klass->newest_version()));
  549. return (should_verify_for(klass->class_loader(), should_verify_class) &&
  550. // return if the class is a bootstrapping class
  551. @@ -518,7 +518,7 @@
  552. ClassVerifier::ClassVerifier(
  553. instanceKlassHandle klass, TRAPS)
  554. - : _thread(THREAD), _exception_type(NULL), _message(NULL), _klass(klass) {
  555. + : _thread(THREAD), _exception_type(NULL), _message(NULL), _klass(klass->newest_version()), _klass_to_verify(klass) {
  556. _this_type = VerificationType::reference_type(klass->name());
  557. // Create list to hold symbols in reference area.
  558. _symbols = new GrowableArray<Symbol*>(100, 0, NULL);
  559. @@ -548,7 +548,7 @@
  560. _klass->external_name());
  561. }
  562. - Array<Method*>* methods = _klass->methods();
  563. + Array<Method*>* methods = _klass_to_verify->methods();
  564. int num_methods = methods->length();
  565. for (int index = 0; index < num_methods; index++) {
  566. diff --git a/src/share/vm/classfile/verifier.hpp b/src/share/vm/classfile/verifier.hpp
  567. --- a/src/share/vm/classfile/verifier.hpp
  568. +++ b/src/share/vm/classfile/verifier.hpp
  569. @@ -331,6 +331,7 @@
  570. VerificationType object_type() const;
  571. + instanceKlassHandle _klass_to_verify;
  572. instanceKlassHandle _klass; // the class being verified
  573. methodHandle _method; // current method being verified
  574. VerificationType _this_type; // the verification type of the current class
  575. diff --git a/src/share/vm/interpreter/linkResolver.cpp b/src/share/vm/interpreter/linkResolver.cpp
  576. --- a/src/share/vm/interpreter/linkResolver.cpp
  577. +++ b/src/share/vm/interpreter/linkResolver.cpp
  578. @@ -215,8 +215,8 @@
  579. // Klass resolution
  580. void LinkResolver::check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS) {
  581. - if (!Reflection::verify_class_access(ref_klass(),
  582. - sel_klass(),
  583. + if (!Reflection::verify_class_access(ref_klass()->newest_version(),
  584. + sel_klass()->newest_version(),
  585. true)) {
  586. ResourceMark rm(THREAD);
  587. Exceptions::fthrow(
  588. @@ -444,7 +444,7 @@
  589. // We'll check for the method name first, as that's most likely
  590. // to be false (so we'll short-circuit out of these tests).
  591. if (sel_method->name() == vmSymbols::clone_name() &&
  592. - sel_klass() == SystemDictionary::Object_klass() &&
  593. + sel_klass()->newest_version() == SystemDictionary::Object_klass()->newest_version() &&
  594. resolved_klass->oop_is_array()) {
  595. // We need to change "protected" to "public".
  596. assert(flags.is_protected(), "clone not protected?");
  597. @@ -802,7 +802,7 @@
  598. }
  599. // Final fields can only be accessed from its own class.
  600. - if (is_put && fd.access_flags().is_final() && sel_klass() != current_klass()) {
  601. + if (is_put && fd.access_flags().is_final() && sel_klass() != current_klass() && sel_klass() != current_klass()->active_version()) {
  602. THROW(vmSymbols::java_lang_IllegalAccessError());
  603. }
  604. @@ -1199,6 +1199,8 @@
  605. // recv_klass might be an arrayKlassOop but all vtables start at
  606. // the same place. The cast is to avoid virtual call and assertion.
  607. InstanceKlass* inst = (InstanceKlass*)recv_klass();
  608. + // (DCEVM) Check that the receiver is a subtype of the holder of the resolved method.
  609. + assert(inst->is_subtype_of(resolved_method->method_holder()), "receiver and resolved method holder are inconsistent");
  610. selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index));
  611. }
  612. }
  613. diff --git a/src/share/vm/memory/universe.cpp b/src/share/vm/memory/universe.cpp
  614. --- a/src/share/vm/memory/universe.cpp
  615. +++ b/src/share/vm/memory/universe.cpp
  616. @@ -159,6 +159,42 @@
  617. f(doubleArrayKlassObj());
  618. }
  619. +// (DCEVM) This method should iterate all pointers that are not within heap objects.
  620. +void Universe::root_oops_do(OopClosure *oopClosure) {
  621. +
  622. + class AlwaysTrueClosure: public BoolObjectClosure {
  623. + public:
  624. + void do_object(oop p) { ShouldNotReachHere(); }
  625. + bool do_object_b(oop p) { return true; }
  626. + };
  627. + AlwaysTrueClosure always_true;
  628. +
  629. + Universe::oops_do(oopClosure);
  630. +// ReferenceProcessor::oops_do(oopClosure); (tw) check why no longer there
  631. + JNIHandles::oops_do(oopClosure); // Global (strong) JNI handles
  632. + Threads::oops_do(oopClosure, NULL, NULL);
  633. + ObjectSynchronizer::oops_do(oopClosure);
  634. + FlatProfiler::oops_do(oopClosure);
  635. + JvmtiExport::oops_do(oopClosure);
  636. +
  637. + // Now adjust pointers in remaining weak roots. (All of which should
  638. + // have been cleared if they pointed to non-surviving objects.)
  639. + // Global (weak) JNI handles
  640. + JNIHandles::weak_oops_do(&always_true, oopClosure);
  641. +
  642. + CodeCache::oops_do(oopClosure);
  643. + StringTable::oops_do(oopClosure);
  644. +
  645. + // (DCEVM) TODO: Check if this is correct?
  646. + //CodeCache::scavenge_root_nmethods_oops_do(oopClosure);
  647. + //Management::oops_do(oopClosure);
  648. + //ref_processor()->weak_oops_do(&oopClosure);
  649. + //PSScavenge::reference_processor()->weak_oops_do(&oopClosure);
  650. +
  651. + // SO_AllClasses
  652. + SystemDictionary::oops_do(oopClosure);
  653. +}
  654. +
  655. void Universe::oops_do(OopClosure* f, bool do_all) {
  656. f->do_oop((oop*) &_int_mirror);
  657. diff --git a/src/share/vm/memory/universe.hpp b/src/share/vm/memory/universe.hpp
  658. --- a/src/share/vm/memory/universe.hpp
  659. +++ b/src/share/vm/memory/universe.hpp
  660. @@ -407,6 +407,7 @@
  661. static void run_finalizers_on_exit();
  662. // Iteration
  663. + static void root_oops_do(OopClosure *f);
  664. // Apply "f" to the addresses of all the direct heap pointers maintained
  665. // as static fields of "Universe".
  666. diff --git a/src/share/vm/oops/cpCache.cpp b/src/share/vm/oops/cpCache.cpp
  667. --- a/src/share/vm/oops/cpCache.cpp
  668. +++ b/src/share/vm/oops/cpCache.cpp
  669. @@ -336,7 +336,8 @@
  670. if (has_appendix) {
  671. const int appendix_index = f2_as_index() + _indy_resolved_references_appendix_offset;
  672. assert(appendix_index >= 0 && appendix_index < resolved_references->length(), "oob");
  673. - assert(resolved_references->obj_at(appendix_index) == NULL, "init just once");
  674. + // FIXME (DCEVM) relaxing for now...
  675. + //assert(resolved_references->obj_at(appendix_index) == NULL, "init just once");
  676. resolved_references->obj_at_put(appendix_index, appendix());
  677. }
  678. @@ -344,7 +345,8 @@
  679. if (has_method_type) {
  680. const int method_type_index = f2_as_index() + _indy_resolved_references_method_type_offset;
  681. assert(method_type_index >= 0 && method_type_index < resolved_references->length(), "oob");
  682. - assert(resolved_references->obj_at(method_type_index) == NULL, "init just once");
  683. + // FIXME (DCEVM) relaxing for now...
  684. + //assert(resolved_references->obj_at(method_type_index) == NULL, "init just once");
  685. resolved_references->obj_at_put(method_type_index, method_type());
  686. }
  687. @@ -532,6 +534,27 @@
  688. // the method is in the interesting class so the entry is interesting
  689. return true;
  690. }
  691. +
  692. +// Enhanced RedefineClasses() API support (DCEVM):
  693. +// Clear cached entry, let it be re-resolved
  694. +void ConstantPoolCacheEntry::clear_entry() {
  695. + if (_indices == constant_pool_index() ||
  696. + bytecode_1() == Bytecodes::_invokehandle ||
  697. + bytecode_1() == Bytecodes::_invokedynamic) {
  698. + // Entry wasn't initialized yet or was initialized for invokehandle/invokedynamic.
  699. + // Don't clear it since f2 will not be reset during entry re-resolution.
  700. + return;
  701. + }
  702. + _indices = constant_pool_index();
  703. + _f1 = NULL;
  704. + _f2 = 0;
  705. +
  706. + // FIXME: (DCEVM) we want to clear flags, but parameter size is actually used
  707. + // after we return from the method, before entry is re-initialized. So let's
  708. + // keep parameter size the same.
  709. + // For example, it's used in TemplateInterpreterGenerator::generate_return_entry_for
  710. + _flags &= 0x0000000f;
  711. +}
  712. #endif // INCLUDE_JVMTI
  713. void ConstantPoolCacheEntry::print(outputStream* st, int index) const {
  714. @@ -660,6 +683,14 @@
  715. }
  716. }
  717. }
  718. +
  719. +// Enhanced RedefineClasses() API support (DCEVM):
  720. +// Clear all entries
  721. +void ConstantPoolCache::clear_entries() {
  722. + for (int i = 0; i < length(); i++) {
  723. + entry_at(i)->clear_entry();
  724. + }
  725. +}
  726. #endif // INCLUDE_JVMTI
  727. diff --git a/src/share/vm/oops/cpCache.hpp b/src/share/vm/oops/cpCache.hpp
  728. --- a/src/share/vm/oops/cpCache.hpp
  729. +++ b/src/share/vm/oops/cpCache.hpp
  730. @@ -373,6 +373,10 @@
  731. bool * trace_name_printed);
  732. bool check_no_old_or_obsolete_entries();
  733. bool is_interesting_method_entry(Klass* k);
  734. +
  735. + // Enhanced RedefineClasses() API support (DCEVM):
  736. + // Clear cached entry, let it be re-resolved
  737. + void clear_entry();
  738. #endif // INCLUDE_JVMTI
  739. // Debugging & Printing
  740. @@ -472,6 +476,10 @@
  741. int methods_length, bool * trace_name_printed);
  742. bool check_no_old_or_obsolete_entries();
  743. void dump_cache();
  744. +
  745. + // Enhanced RedefineClasses() API support (DCEVM):
  746. + // Clear all entries
  747. + void clear_entries();
  748. #endif // INCLUDE_JVMTI
  749. // Deallocate - no fields to deallocate
  750. diff --git a/src/share/vm/oops/instanceKlass.cpp b/src/share/vm/oops/instanceKlass.cpp
  751. --- a/src/share/vm/oops/instanceKlass.cpp
  752. +++ b/src/share/vm/oops/instanceKlass.cpp
  753. @@ -718,7 +718,8 @@
  754. }
  755. #endif
  756. this_oop->set_init_state(linked);
  757. - if (JvmtiExport::should_post_class_prepare()) {
  758. + // (DCEVM) Must check for old version in order to prevent infinite loops.
  759. + if (JvmtiExport::should_post_class_prepare() && this_oop->old_version() == NULL /* JVMTI deadlock otherwise */) {
  760. Thread *thread = THREAD;
  761. assert(thread->is_Java_thread(), "thread->is_Java_thread()");
  762. JvmtiExport::post_class_prepare((JavaThread *) thread, this_oop());
  763. @@ -795,7 +796,9 @@
  764. // If we were to use wait() instead of waitInterruptibly() then
  765. // we might end up throwing IE from link/symbol resolution sites
  766. // that aren't expected to throw. This would wreak havoc. See 6320309.
  767. - while(this_oop->is_being_initialized() && !this_oop->is_reentrant_initialization(self)) {
  768. + // (DCEVM) Wait also for the old class version to be fully initialized.
  769. + while((this_oop->is_being_initialized() && !this_oop->is_reentrant_initialization(self))
  770. + || (this_oop->old_version() != NULL && InstanceKlass::cast(this_oop->old_version())->is_being_initialized())) {
  771. wait = true;
  772. ol.waitUninterruptibly(CHECK);
  773. }
  774. @@ -1051,6 +1054,18 @@
  775. return false;
  776. }
  777. +bool InstanceKlass::implements_interface_any_version(Klass* k) const {
  778. + k = k->newest_version();
  779. + if (this->newest_version() == k) return true;
  780. + assert(k->is_interface(), "should be an interface class");
  781. + for (int i = 0; i < transitive_interfaces()->length(); i++) {
  782. + if (transitive_interfaces()->at(i)->newest_version() == k) {
  783. + return true;
  784. + }
  785. + }
  786. + return false;
  787. +}
  788. +
  789. bool InstanceKlass::is_same_or_direct_interface(Klass *k) const {
  790. // Verify direct super interface
  791. if (this == k) return true;
  792. @@ -1314,6 +1329,18 @@
  793. }
  794. }
  795. +void InstanceKlass::store_update_information(GrowableArray<int> &values) {
  796. + int *arr = NEW_C_HEAP_ARRAY(int, values.length(), mtClass);
  797. + for (int i=0; i<values.length(); i++) {
  798. + arr[i] = values.at(i);
  799. + }
  800. + set_update_information(arr);
  801. +}
  802. +
  803. +void InstanceKlass::clear_update_information() {
  804. + FREE_C_HEAP_ARRAY(int, update_information(), mtClass);
  805. + set_update_information(NULL);
  806. +}
  807. void InstanceKlass::do_local_static_fields(FieldClosure* cl) {
  808. for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
  809. @@ -1797,6 +1824,18 @@
  810. return id;
  811. }
  812. +bool InstanceKlass::update_jmethod_id(Method* method, jmethodID newMethodID) {
  813. + size_t idnum = (size_t)method->method_idnum();
  814. + jmethodID* jmeths = methods_jmethod_ids_acquire();
  815. + size_t length; // length assigned as debugging crumb
  816. + jmethodID id = NULL;
  817. + if (jmeths != NULL && // If there is a cache
  818. + (length = (size_t)jmeths[0]) > idnum) { // and if it is long enough,
  819. + jmeths[idnum+1] = newMethodID; // Set method id (may be NULL)
  820. + return true;
  821. + }
  822. + return false;
  823. +}
  824. //
  825. // Walk the list of dependent nmethods searching for nmethods which
  826. @@ -1875,6 +1914,13 @@
  827. last = b;
  828. b = b->next();
  829. }
  830. +
  831. + // (DCEVM) Hack as dependencies get wrong version of Klass*
  832. + if (this->old_version() != NULL) {
  833. + InstanceKlass::cast(this->old_version())->remove_dependent_nmethod(nm);
  834. + return;
  835. + }
  836. +
  837. #ifdef ASSERT
  838. tty->print_cr("### %s can't find dependent nmethod:", this->external_name());
  839. nm->print();
  840. @@ -2884,6 +2930,24 @@
  841. assert(is_klass(), "must be klass");
  842. Klass::print_on(st);
  843. + // (DCEVM) Output revision number and revision numbers of older / newer and oldest / newest version of this class.
  844. + if (AllowEnhancedClassRedefinition) {
  845. + st->print(BULLET"revision: %d", revision_number());
  846. + if (new_version() != NULL) {
  847. + st->print(" (newer=%d)", new_version()->revision_number());
  848. + }
  849. + if (newest_version() != new_version() && newest_version() != this) {
  850. + st->print(" (newest=%d)", newest_version()->revision_number());
  851. + }
  852. + if (old_version() != NULL) {
  853. + st->print(" (old=%d)", old_version()->revision_number());
  854. + }
  855. + if (oldest_version() != old_version() && oldest_version() != this) {
  856. + st->print(" (oldest=%d)", oldest_version()->revision_number());
  857. + }
  858. + st->cr();
  859. + }
  860. +
  861. st->print(BULLET"instance size: %d", size_helper()); st->cr();
  862. st->print(BULLET"klass size: %d", size()); st->cr();
  863. st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
  864. @@ -3219,7 +3283,7 @@
  865. }
  866. guarantee(sib->is_klass(), "should be klass");
  867. - guarantee(sib->super() == super, "siblings should have same superklass");
  868. + guarantee(sib->super() == super || super->newest_version() == SystemDictionary::Object_klass(), "siblings should have same superklass");
  869. }
  870. // Verify implementor fields
  871. @@ -3384,6 +3448,7 @@
  872. // Purge previous versions
  873. static void purge_previous_versions_internal(InstanceKlass* ik, int emcp_method_count) {
  874. + // FIXME: (DCEVM) Should we purge something?
  875. if (ik->previous_versions() != NULL) {
  876. // This klass has previous versions so see what we can cleanup
  877. // while it is safe to do so.
  878. @@ -3621,7 +3686,7 @@
  879. // Determine if InstanceKlass has a previous version.
  880. bool InstanceKlass::has_previous_version() const {
  881. - return (_previous_versions != NULL && _previous_versions->length() > 0);
  882. + return _old_version != NULL || (_previous_versions != NULL && _previous_versions->length() > 0);
  883. } // end has_previous_version()
  884. diff --git a/src/share/vm/oops/instanceKlass.hpp b/src/share/vm/oops/instanceKlass.hpp
  885. --- a/src/share/vm/oops/instanceKlass.hpp
  886. +++ b/src/share/vm/oops/instanceKlass.hpp
  887. @@ -139,6 +139,7 @@
  888. friend class VMStructs;
  889. friend class ClassFileParser;
  890. friend class CompileReplay;
  891. + friend class VM_EnhancedRedefineClasses;
  892. protected:
  893. // Constructor
  894. @@ -637,7 +638,7 @@
  895. // If the _previous_versions array is non-NULL, then this klass
  896. // has been redefined at least once even if we aren't currently
  897. // tracking a previous version.
  898. - bool has_been_redefined() const { return _previous_versions != NULL; }
  899. + bool has_been_redefined() const { return _old_version != NULL || _previous_versions != NULL; }
  900. bool has_previous_version() const;
  901. void init_previous_versions() {
  902. _previous_versions = NULL;
  903. @@ -711,6 +712,7 @@
  904. static void get_jmethod_id_length_value(jmethodID* cache, size_t idnum,
  905. size_t *length_p, jmethodID* id_p);
  906. jmethodID jmethod_id_or_null(Method* method);
  907. + bool update_jmethod_id(Method* method, jmethodID newMethodID);
  908. // annotations support
  909. Annotations* annotations() const { return _annotations; }
  910. @@ -780,6 +782,7 @@
  911. // subclass/subinterface checks
  912. bool implements_interface(Klass* k) const;
  913. bool is_same_or_direct_interface(Klass* k) const;
  914. + bool implements_interface_any_version(Klass* k) const;
  915. // Access to the implementor of an interface.
  916. Klass* implementor() const
  917. @@ -831,6 +834,10 @@
  918. void do_nonstatic_fields(FieldClosure* cl); // including inherited fields
  919. void do_local_static_fields(void f(fieldDescriptor*, TRAPS), TRAPS);
  920. + // (DCEVM) instance update information to be used in GC run
  921. + void store_update_information(GrowableArray<int> &values);
  922. + void clear_update_information();
  923. +
  924. void methods_do(void f(Method* method));
  925. void array_klasses_do(void f(Klass* k));
  926. void array_klasses_do(void f(Klass* k, TRAPS), TRAPS);
  927. diff --git a/src/share/vm/oops/klass.cpp b/src/share/vm/oops/klass.cpp
  928. --- a/src/share/vm/oops/klass.cpp
  929. +++ b/src/share/vm/oops/klass.cpp
  930. @@ -170,6 +170,13 @@
  931. set_next_link(NULL);
  932. TRACE_INIT_ID(this);
  933. + set_redefinition_flags(Klass::NoRedefinition);
  934. + set_redefining(false);
  935. + set_new_version(NULL);
  936. + set_old_version(NULL);
  937. + set_redefinition_index(-1);
  938. + set_revision_number(-1);
  939. +
  940. set_prototype_header(markOopDesc::prototype());
  941. set_biased_lock_revocation_count(0);
  942. set_last_biased_lock_bulk_revocation_time(0);
  943. @@ -375,6 +382,24 @@
  944. debug_only(verify();)
  945. }
  946. +// (DCEVM)
  947. +void Klass::remove_from_sibling_list() {
  948. + debug_only(verify();)
  949. + // remove ourselves to superklass' subklass list
  950. + InstanceKlass* super = superklass();
  951. + assert(super != NULL, "should have super");
  952. + if (super->subklass() == this) {
  953. + // first subklass
  954. + super->set_subklass(next_sibling());
  955. + } else {
  956. + Klass* sib = super->subklass();
  957. + while (sib->next_sibling() != this) {
  958. + sib = sib->next_sibling();
  959. + };
  960. + sib->set_next_sibling(next_sibling());
  961. + }
  962. +}
  963. +
  964. bool Klass::is_loader_alive(BoolObjectClosure* is_alive) {
  965. assert(ClassLoaderDataGraph::contains((address)this), "is in the metaspace");
  966. diff --git a/src/share/vm/oops/klass.hpp b/src/share/vm/oops/klass.hpp
  967. --- a/src/share/vm/oops/klass.hpp
  968. +++ b/src/share/vm/oops/klass.hpp
  969. @@ -149,6 +149,10 @@
  970. oop _java_mirror;
  971. // Superclass
  972. Klass* _super;
  973. + // Old class
  974. + Klass* _old_version;
  975. + // New class
  976. + Klass* _new_version;
  977. // First subclass (NULL if none); _subklass->next_sibling() is next one
  978. Klass* _subklass;
  979. // Sibling link (or NULL); links all subklasses of a klass
  980. @@ -164,6 +168,16 @@
  981. jint _modifier_flags; // Processed access flags, for use by Class.getModifiers.
  982. AccessFlags _access_flags; // Access flags. The class/interface distinction is stored here.
  983. + // (DCEVM) fields for enhanced class redefinition
  984. + jint _revision_number; // The revision number for redefined classes
  985. + jint _redefinition_index; // Index of this class when performing the redefinition
  986. + bool _subtype_changed;
  987. + int _redefinition_flags; // Level of class redefinition
  988. + bool _is_copying_backwards; // Does the class need to copy fields backwards? => possibly overwrite itself?
  989. + bool _original_field_offsets_changed; // Did the original field offsets of this class change during class redefinition?
  990. + int * _update_information; // Update information
  991. + bool _is_redefining;
  992. +
  993. // Biased locking implementation and statistics
  994. // (the 64-bit chunk goes first, to avoid some fragmentation)
  995. jlong _last_biased_lock_bulk_revocation_time;
  996. @@ -208,6 +222,53 @@
  997. Array<Klass*>* secondary_supers() const { return _secondary_supers; }
  998. void set_secondary_supers(Array<Klass*>* k) { _secondary_supers = k; }
  999. + // BEGIN class redefinition utilities
  1000. +
  1001. + // double links between new and old version of a class
  1002. + Klass* old_version() const { return _old_version; }
  1003. + void set_old_version(Klass* klass) { assert(_old_version == NULL || klass == NULL, "Can only be set once!"); _old_version = klass; }
  1004. + Klass* new_version() const { return _new_version; }
  1005. + void set_new_version(Klass* klass) { assert(_new_version == NULL || klass == NULL, "Can only be set once!"); _new_version = klass; }
  1006. +
  1007. + // A subtype of this class is no longer a subtype
  1008. + bool has_subtype_changed() const { return _subtype_changed; }
  1009. + void set_subtype_changed(bool b) { assert(is_newest_version() || new_version()->is_newest_version(), "must be newest or second newest version");
  1010. + _subtype_changed = b; }
  1011. + // state of being redefined
  1012. + int redefinition_index() const { return _redefinition_index; }
  1013. + void set_redefinition_index(int index) { _redefinition_index = index; }
  1014. + void set_redefining(bool b) { _is_redefining = b; }
  1015. + bool is_redefining() const { return _is_redefining; }
  1016. + int redefinition_flags() const { return _redefinition_flags; }
  1017. + bool check_redefinition_flag(int flags) const { return (_redefinition_flags & flags) != 0; }
  1018. + void set_redefinition_flags(int flags) { _redefinition_flags = flags; }
  1019. + void set_redefinition_flag(int flag) { _redefinition_flags |= flag; }
  1020. + void clear_redefinition_flag(int flag) { _redefinition_flags &= ~flag; }
  1021. + bool is_copying_backwards() const { return _is_copying_backwards; }
  1022. + void set_copying_backwards(bool b) { _is_copying_backwards = b; }
  1023. +
  1024. + // update information
  1025. + int *update_information() const { return _update_information; }
  1026. + void set_update_information(int *info) { _update_information = info; }
  1027. +
  1028. + // Revision number for redefined classes, -1 for originally loaded classes
  1029. + bool was_redefined() const { return _revision_number != -1; }
  1030. + jint revision_number() const { return _revision_number; }
  1031. + void set_revision_number(jint number) { _revision_number = number; }
  1032. +
  1033. + const Klass* oldest_version() const { return _old_version == NULL ? this : _old_version->oldest_version(); }
  1034. + Klass* oldest_version() { return _old_version == NULL ? this : _old_version->oldest_version(); }
  1035. +
  1036. + const Klass* newest_version() const { return _new_version == NULL ? this : _new_version->newest_version(); }
  1037. + Klass* newest_version() { return _new_version == NULL ? this : _new_version->newest_version(); }
  1038. +
  1039. + const Klass* active_version() const { return _new_version == NULL || _new_version->is_redefining() ? this : _new_version->active_version(); }
  1040. + Klass* active_version() { return _new_version == NULL || _new_version->is_redefining() ? this : _new_version->active_version(); }
  1041. +
  1042. + bool is_newest_version() const { return _new_version == NULL; }
  1043. +
  1044. + // END class redefinition utilities
  1045. +
  1046. // Return the element of the _super chain of the given depth.
  1047. // If there is no such element, return either NULL or this.
  1048. Klass* primary_super_of_depth(juint i) const {
  1049. @@ -261,6 +322,7 @@
  1050. Klass* subklass() const;
  1051. Klass* next_sibling() const;
  1052. void append_to_sibling_list(); // add newly created receiver to superklass' subklass list
  1053. + void remove_from_sibling_list(); // (DCEVM) remove receiver from sibling list
  1054. void set_next_link(Klass* k) { _next_link = k; }
  1055. Klass* next_link() const { return _next_link; } // The next klass defined by the class loader.
  1056. @@ -287,6 +349,16 @@
  1057. void set_next_sibling(Klass* s);
  1058. public:
  1059. + // (DCEVM) Different class redefinition flags of code evolution.
  1060. + enum RedefinitionFlags {
  1061. + NoRedefinition, // This class is not redefined at all!
  1062. + ModifyClass = 1, // There are changes to the class meta data.
  1063. + ModifyClassSize = ModifyClass << 1, // The size of the class meta data changes.
  1064. + ModifyInstances = ModifyClassSize << 1, // There are change to the instance format.
  1065. + ModifyInstanceSize = ModifyInstances << 1, // The size of instances changes.
  1066. + RemoveSuperType = ModifyInstanceSize << 1, // A super type of this class is removed.
  1067. + MarkedAsAffected = RemoveSuperType << 1 // This class has been marked as an affected class.
  1068. + };
  1069. // Compiler support
  1070. static ByteSize super_offset() { return in_ByteSize(offset_of(Klass, _super)); }
  1071. diff --git a/src/share/vm/oops/klassVtable.cpp b/src/share/vm/oops/klassVtable.cpp
  1072. --- a/src/share/vm/oops/klassVtable.cpp
  1073. +++ b/src/share/vm/oops/klassVtable.cpp
  1074. @@ -1409,6 +1409,8 @@
  1075. void klassVtable::verify_against(outputStream* st, klassVtable* vt, int index) {
  1076. vtableEntry* vte = &vt->table()[index];
  1077. + // (DCEVM) FIXME-isd: do we need the following line?
  1078. + if (vte->method() == NULL || table()[index].method() == NULL) return;
  1079. if (vte->method()->name() != table()[index].method()->name() ||
  1080. vte->method()->signature() != table()[index].method()->signature()) {
  1081. fatal("mismatched name/signature of vtable entries");
  1082. @@ -1428,6 +1430,8 @@
  1083. void vtableEntry::verify(klassVtable* vt, outputStream* st) {
  1084. NOT_PRODUCT(FlagSetting fs(IgnoreLockingAssertions, true));
  1085. + // FIXME: (DCEVM) does not hold?
  1086. + if (method() != NULL) {
  1087. assert(method() != NULL, "must have set method");
  1088. method()->verify();
  1089. // we sub_type, because it could be a miranda method
  1090. @@ -1435,7 +1439,9 @@
  1091. #ifndef PRODUCT
  1092. print();
  1093. #endif
  1094. - fatal(err_msg("vtableEntry " PTR_FORMAT ": method is from subclass", this));
  1095. + // (DCEVM) the following fatal does not work for old versions of classes
  1096. + //fatal(err_msg("vtableEntry " PTR_FORMAT ": method is from subclass", this));
  1097. + }
  1098. }
  1099. }
  1100. diff --git a/src/share/vm/oops/method.cpp b/src/share/vm/oops/method.cpp
  1101. --- a/src/share/vm/oops/method.cpp
  1102. +++ b/src/share/vm/oops/method.cpp
  1103. @@ -1185,6 +1185,8 @@
  1104. // Reset correct method/const method, method size, and parameter info
  1105. newm->set_constMethod(newcm);
  1106. + newm->set_new_version(newm->new_version());
  1107. + newm->set_old_version(newm->old_version());
  1108. newm->constMethod()->set_code_size(new_code_length);
  1109. newm->constMethod()->set_constMethod_size(new_const_method_size);
  1110. newm->set_method_size(new_method_size);
  1111. @@ -1788,6 +1790,10 @@
  1112. // Add a method id to the jmethod_ids
  1113. jmethodID Method::make_jmethod_id(ClassLoaderData* loader_data, Method* m) {
  1114. + // FIXME: (DCEVM) ???
  1115. + if (m != m->newest_version()) {
  1116. + m = m->newest_version();
  1117. + }
  1118. ClassLoaderData* cld = loader_data;
  1119. if (!SafepointSynchronize::is_at_safepoint()) {
  1120. diff --git a/src/share/vm/oops/method.hpp b/src/share/vm/oops/method.hpp
  1121. --- a/src/share/vm/oops/method.hpp
  1122. +++ b/src/share/vm/oops/method.hpp
  1123. @@ -105,6 +105,10 @@
  1124. AccessFlags _access_flags; // Access flags
  1125. int _vtable_index; // vtable index of this method (see VtableIndexFlag)
  1126. // note: can have vtables with >2**16 elements (because of inheritance)
  1127. + // (DCEVM) Newer version of method available?
  1128. + Method* _new_version;
  1129. + Method* _old_version;
  1130. +
  1131. #ifdef CC_INTERP
  1132. int _result_index; // C++ interpreter needs for converting results to/from stack
  1133. #endif
  1134. @@ -175,6 +179,23 @@
  1135. int name_index() const { return constMethod()->name_index(); }
  1136. void set_name_index(int index) { constMethod()->set_name_index(index); }
  1137. + Method* new_version() const { return _new_version; }
  1138. + void set_new_version(Method* m) { _new_version = m; }
  1139. + Method* newest_version() { return (_new_version == NULL) ? this : _new_version->newest_version(); }
  1140. +
  1141. + Method* old_version() const { return _old_version; }
  1142. + void set_old_version(Method* m) {
  1143. + /*if (m == NULL) {
  1144. + _old_version = NULL;
  1145. + return;
  1146. + }*/
  1147. +
  1148. + assert(_old_version == NULL, "may only be set once");
  1149. + assert(this->code_size() == m->code_size(), "must have same code length");
  1150. + _old_version = m;
  1151. + }
  1152. + const Method* oldest_version() const { return (_old_version == NULL) ? this : _old_version->oldest_version(); }
  1153. +
  1154. // signature
  1155. Symbol* signature() const { return constants()->symbol_at(signature_index()); }
  1156. int signature_index() const { return constMethod()->signature_index(); }
  1157. diff --git a/src/share/vm/prims/jni.cpp b/src/share/vm/prims/jni.cpp
  1158. --- a/src/share/vm/prims/jni.cpp
  1159. +++ b/src/share/vm/prims/jni.cpp
  1160. @@ -406,6 +406,7 @@
  1161. }
  1162. Klass* k = SystemDictionary::resolve_from_stream(class_name, class_loader,
  1163. Handle(), &st, true,
  1164. + KlassHandle(),
  1165. CHECK_NULL);
  1166. if (TraceClassResolution && k != NULL) {
  1167. diff --git a/src/share/vm/prims/jvm.cpp b/src/share/vm/prims/jvm.cpp
  1168. --- a/src/share/vm/prims/jvm.cpp
  1169. +++ b/src/share/vm/prims/jvm.cpp
  1170. @@ -904,6 +904,7 @@
  1171. Klass* k = SystemDictionary::resolve_from_stream(class_name, class_loader,
  1172. protection_domain, &st,
  1173. verify != 0,
  1174. + KlassHandle(),
  1175. CHECK_NULL);
  1176. if (TraceClassResolution && k != NULL) {
  1177. diff --git a/src/share/vm/prims/jvmtiEnv.cpp b/src/share/vm/prims/jvmtiEnv.cpp
  1178. --- a/src/share/vm/prims/jvmtiEnv.cpp
  1179. +++ b/src/share/vm/prims/jvmtiEnv.cpp
  1180. @@ -42,6 +42,7 @@
  1181. #include "prims/jvmtiManageCapabilities.hpp"
  1182. #include "prims/jvmtiRawMonitor.hpp"
  1183. #include "prims/jvmtiRedefineClasses.hpp"
  1184. +#include "prims/jvmtiRedefineClasses2.hpp"
  1185. #include "prims/jvmtiTagMap.hpp"
  1186. #include "prims/jvmtiThreadState.inline.hpp"
  1187. #include "prims/jvmtiUtil.hpp"
  1188. @@ -206,8 +207,10 @@
  1189. // is_modifiable_class_ptr - pre-checked for NULL
  1190. jvmtiError
  1191. JvmtiEnv::IsModifiableClass(oop k_mirror, jboolean* is_modifiable_class_ptr) {
  1192. - *is_modifiable_class_ptr = VM_RedefineClasses::is_modifiable_class(k_mirror)?
  1193. - JNI_TRUE : JNI_FALSE;
  1194. + bool is_modifiable_class = AllowEnhancedClassRedefinition ?
  1195. + VM_EnhancedRedefineClasses::is_modifiable_class(k_mirror) :
  1196. + VM_RedefineClasses::is_modifiable_class(k_mirror);
  1197. + *is_modifiable_class_ptr = is_modifiable_class ? JNI_TRUE : JNI_FALSE;
  1198. return JVMTI_ERROR_NONE;
  1199. } /* end IsModifiableClass */
  1200. @@ -276,6 +279,11 @@
  1201. }
  1202. class_definitions[index].klass = jcls;
  1203. }
  1204. + if (AllowEnhancedClassRedefinition) {
  1205. + VM_EnhancedRedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_retransform);
  1206. + VMThread::execute(&op);
  1207. + return (op.check_error());
  1208. + }
  1209. VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_retransform);
  1210. VMThread::execute(&op);
  1211. return (op.check_error());
  1212. @@ -287,6 +295,11 @@
  1213. jvmtiError
  1214. JvmtiEnv::RedefineClasses(jint class_count, const jvmtiClassDefinition* class_definitions) {
  1215. //TODO: add locking
  1216. + if (AllowEnhancedClassRedefinition) {
  1217. + VM_EnhancedRedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_redefine);
  1218. + VMThread::execute(&op);
  1219. + return (op.check_error());
  1220. + }
  1221. VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_redefine);
  1222. VMThread::execute(&op);
  1223. return (op.check_error());
  1224. diff --git a/src/share/vm/prims/jvmtiExport.hpp b/src/share/vm/prims/jvmtiExport.hpp
  1225. --- a/src/share/vm/prims/jvmtiExport.hpp
  1226. +++ b/src/share/vm/prims/jvmtiExport.hpp
  1227. @@ -188,6 +188,7 @@
  1228. // systems as needed to relax invariant checks.
  1229. static bool _has_redefined_a_class;
  1230. friend class VM_RedefineClasses;
  1231. + friend class VM_EnhancedRedefineClasses;
  1232. inline static void set_has_redefined_a_class() {
  1233. JVMTI_ONLY(_has_redefined_a_class = true;)
  1234. }
  1235. diff --git a/src/share/vm/prims/jvmtiImpl.cpp b/src/share/vm/prims/jvmtiImpl.cpp
  1236. --- a/src/share/vm/prims/jvmtiImpl.cpp
  1237. +++ b/src/share/vm/prims/jvmtiImpl.cpp
  1238. @@ -289,6 +289,11 @@
  1239. Symbol* m_name = _method->name();
  1240. Symbol* m_signature = _method->signature();
  1241. + // (DCEVM) Go through old versions of method
  1242. + for (Method* m = _method->old_version(); m != NULL; m = m->old_version()) {
  1243. + (m->*meth_act)(_bci);
  1244. + }
  1245. +
  1246. // search previous versions if they exist
  1247. PreviousVersionWalker pvw(thread, (InstanceKlass *)ikh());
  1248. for (PreviousVersionNode * pv_node = pvw.next_previous_version();
  1249. diff --git a/src/share/vm/prims/jvmtiRedefineClasses2.cpp b/src/share/vm/prims/jvmtiRedefineClasses2.cpp
  1250. new file mode 100644
  1251. --- /dev/null
  1252. +++ b/src/share/vm/prims/jvmtiRedefineClasses2.cpp
  1253. @@ -0,0 +1,2128 @@
  1254. +/*
  1255. + * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
  1256. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  1257. + *
  1258. + * This code is free software; you can redistribute it and/or modify it
  1259. + * under the terms of the GNU General Public License version 2 only, as
  1260. + * published by the Free Software Foundation.
  1261. + *
  1262. + * This code is distributed in the hope that it will be useful, but WITHOUT
  1263. + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  1264. + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  1265. + * version 2 for more details (a copy is included in the LICENSE file that
  1266. + * accompanied this code).
  1267. + *
  1268. + * You should have received a copy of the GNU General Public License version
  1269. + * 2 along with this work; if not, write to the Free Software Foundation,
  1270. + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  1271. + *
  1272. + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  1273. + * or visit www.oracle.com if you need additional information or have any
  1274. + * questions.
  1275. + *
  1276. + */
  1277. +
  1278. +#include "precompiled.hpp"
  1279. +#include "classfile/systemDictionary.hpp"
  1280. +#include "classfile/verifier.hpp"
  1281. +#include "code/codeCache.hpp"
  1282. +#include "interpreter/oopMapCache.hpp"
  1283. +#include "interpreter/rewriter.hpp"
  1284. +#include "memory/gcLocker.hpp"
  1285. +#include "memory/universe.inline.hpp"
  1286. +#include "memory/metaspaceShared.hpp"
  1287. +#include "oops/fieldStreams.hpp"
  1288. +#include "oops/klassVtable.hpp"
  1289. +#include "prims/jvmtiImpl.hpp"
  1290. +#include "prims/jvmtiRedefineClasses2.hpp"
  1291. +#include "prims/methodComparator.hpp"
  1292. +#include "prims/jvmtiClassFileReconstituter.hpp"
  1293. +#include "runtime/deoptimization.hpp"
  1294. +#include "runtime/relocator.hpp"
  1295. +#include "utilities/bitMap.inline.hpp"
  1296. +#include "compiler/compileBroker.hpp"
  1297. +#include "oops/instanceMirrorKlass.hpp"
  1298. +#include "utilities/pair.hpp"
  1299. +
  1300. +
  1301. +Array<Method*>* VM_EnhancedRedefineClasses::_old_methods = NULL;
  1302. +Array<Method*>* VM_EnhancedRedefineClasses::_new_methods = NULL;
  1303. +int* VM_EnhancedRedefineClasses::_matching_old_methods = NULL;
  1304. +int* VM_EnhancedRedefineClasses::_matching_new_methods = NULL;
  1305. +int* VM_EnhancedRedefineClasses::_deleted_methods = NULL;
  1306. +int* VM_EnhancedRedefineClasses::_added_methods = NULL;
  1307. +int VM_EnhancedRedefineClasses::_matching_methods_length = 0;
  1308. +int VM_EnhancedRedefineClasses::_deleted_methods_length = 0;
  1309. +int VM_EnhancedRedefineClasses::_added_methods_length = 0;
  1310. +GrowableArray<instanceKlassHandle>* VM_EnhancedRedefineClasses::_affected_klasses = NULL;
  1311. +
  1312. +
  1313. +// Holds the revision number of the current class redefinition
  1314. +int VM_EnhancedRedefineClasses::_revision_number = -1;
  1315. +
  1316. +VM_EnhancedRedefineClasses::VM_EnhancedRedefineClasses(jint class_count, const jvmtiClassDefinition *class_defs, JvmtiClassLoadKind class_load_kind)
  1317. + : VM_GC_Operation(Universe::heap()->total_full_collections(), GCCause::_heap_inspection) {
  1318. + RC_TIMER_START(_timer_total);
  1319. + _class_count = class_count;
  1320. + _class_defs = class_defs;
  1321. + _class_load_kind = class_load_kind;
  1322. + _result = JVMTI_ERROR_NONE;
  1323. +}
  1324. +
  1325. +VM_EnhancedRedefineClasses::~VM_EnhancedRedefineClasses() {
  1326. + RC_TIMER_STOP(_timer_total);
  1327. +}
  1328. +
  1329. +void VM_EnhancedRedefineClasses::swap_all_method_annotations(ConstMethod* old_method, ConstMethod* new_method) {
  1330. + return; // FIXME-isd: swap annotations!
  1331. +
  1332. + AnnotationArray* save;
  1333. +
  1334. + save = old_method->method_annotations();
  1335. + old_method->set_method_annotations(new_method->method_annotations());
  1336. + new_method->set_method_annotations(save);
  1337. +
  1338. + save = old_method->parameter_annotations();
  1339. + old_method->set_parameter_annotations(new_method->parameter_annotations());
  1340. + new_method->set_parameter_annotations(save);
  1341. +
  1342. + save = old_method->default_annotations();
  1343. + old_method->set_default_annotations(new_method->default_annotations());
  1344. + new_method->set_default_annotations(save);
  1345. +
  1346. + save = old_method->type_annotations();
  1347. + old_method->set_type_annotations(new_method->type_annotations());
  1348. + new_method->set_type_annotations(save);
  1349. +}
  1350. +
  1351. +void VM_EnhancedRedefineClasses::add_affected_klasses( Klass* klass )
  1352. +{
  1353. + assert(!_affected_klasses->contains(klass), "must not occur more than once!");
  1354. + assert(klass->new_version() == NULL, "Only last version is valid entry in system dictionary");
  1355. +
  1356. + Klass* k = klass;
  1357. +
  1358. + if (k->check_redefinition_flag(Klass::MarkedAsAffected)) {
  1359. + _affected_klasses->append(klass);
  1360. + return;
  1361. + }
  1362. +
  1363. + for (juint i = 0; i < k->super_depth(); i++) {
  1364. + Klass* primary = k->primary_super_of_depth(i);
  1365. + // super_depth returns "8" for interfaces, but they don't have primaries other than Object.
  1366. + if (primary == NULL) break;
  1367. + if (primary->check_redefinition_flag(Klass::MarkedAsAffected)) {
  1368. + RC_TRACE(0x00000001, ("Found affected class: %s", k->name()->as_C_string()));
  1369. + k->set_redefinition_flag(Klass::MarkedAsAffected);
  1370. + _affected_klasses->append(klass);
  1371. + return;
  1372. + }
  1373. + }
  1374. +
  1375. + // Check secondary supers
  1376. + int cnt = k->secondary_supers()->length();
  1377. + for (int i = 0; i < cnt; i++) {
  1378. + Klass* secondary = k->secondary_supers()->at(i);
  1379. + if (secondary->check_redefinition_flag(Klass::MarkedAsAffected)) {
  1380. + RC_TRACE(0x00000001, ("Found affected class: %s", k->name()->as_C_string()));
  1381. + k->set_redefinition_flag(Klass::MarkedAsAffected);
  1382. + _affected_klasses->append(klass);
  1383. + return;
  1384. + }
  1385. + }
  1386. +}
  1387. +
  1388. +
  1389. +// Searches for all affected classes and performs a sorting such that a supertype is always before a subtype.
  1390. +jvmtiError VM_EnhancedRedefineClasses::find_sorted_affected_classes() {
  1391. +
  1392. + assert(_affected_klasses, "");
  1393. + for (int i = 0; i < _class_count; i++) {
  1394. + oop mirror = JNIHandles::resolve_non_null(_class_defs[i].klass);
  1395. + instanceKlassHandle klass_handle(Thread::current(), java_lang_Class::as_Klass(mirror));
  1396. + klass_handle->set_redefinition_flag(Klass::MarkedAsAffected);
  1397. + assert(klass_handle->new_version() == NULL, "Must be new class");
  1398. + RC_TRACE(0x00000001, ("Class being reloaded: %s", klass_handle->name()->as_C_string()));
  1399. + }
  1400. +
  1401. + // Find classes not directly redefined, but affected by a redefinition (because one of its supertypes is redefined)
  1402. + SystemDictionary::classes_do(VM_EnhancedRedefineClasses::add_affected_klasses);
  1403. + RC_TRACE(0x00000001, ("%d classes affected", _affected_klasses->length()));
  1404. +
  1405. + // Sort the affected klasses such that a supertype is always on a smaller array index than its subtype.
  1406. + jvmtiError result = do_topological_class_sorting(_class_defs, _class_count, Thread::current());
  1407. + if (RC_TRACE_ENABLED(0x00000001)) {
  1408. + RC_TRACE(0x00000001, ("Redefine order: "));
  1409. + for (int i = 0; i < _affected_klasses->length(); i++) {
  1410. + RC_TRACE(0x00000001, ("%s", _affected_klasses->at(i)->name()->as_C_string()));
  1411. + }
  1412. + }
  1413. +
  1414. + return result;
  1415. +}
  1416. +
  1417. +// Searches for the class bytes of the given class and returns them as a byte array.
  1418. +jvmtiError VM_EnhancedRedefineClasses::find_class_bytes(instanceKlassHandle the_class, const unsigned char **class_bytes, jint *class_byte_count, jboolean *not_changed) {
  1419. +
  1420. + *not_changed = false;
  1421. +
  1422. + // Search for the index in the redefinition array that corresponds to the current class
  1423. + int j;
  1424. + for (j=0; j<_class_count; j++) {
  1425. + oop mirror = JNIHandles::resolve_non_null(_class_defs[j].klass);
  1426. + Klass* the_class_oop = java_lang_Class::as_Klass(mirror);
  1427. + if (the_class_oop == the_class()) {
  1428. + break;
  1429. + }
  1430. + }
  1431. +
  1432. + if (j == _class_count) {
  1433. +
  1434. + *not_changed = true;
  1435. +
  1436. + // Redefine with same bytecodes. This is a class that is only indirectly affected by redefinition,
  1437. + // so the user did not specify a different bytecode for that class.
  1438. +
  1439. + if (the_class->get_cached_class_file_bytes() == NULL) {
  1440. + // not cached, we need to reconstitute the class file from VM representation
  1441. + constantPoolHandle constants(Thread::current(), the_class->constants());
  1442. + MonitorLockerEx ml(constants->lock()); // lock constant pool while we query it
  1443. + //ObjectLocker ol(constants, Thread::current()); // lock constant pool while we query it
  1444. +
  1445. + JvmtiClassFileReconstituter reconstituter(the_class);
  1446. + if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
  1447. + return reconstituter.get_error();
  1448. + }
  1449. +
  1450. + *class_byte_count = (jint)reconstituter.class_file_size();
  1451. + *class_bytes = (unsigned char*)reconstituter.class_file_bytes();
  1452. + } else {
  1453. +
  1454. + // it is cached, get it from the cache
  1455. + *class_byte_count = the_class->get_cached_class_file_len();
  1456. + *class_bytes = the_class->get_cached_class_file_bytes();
  1457. + }
  1458. +
  1459. + } else {
  1460. +
  1461. + // Redefine with bytecodes at index j
  1462. + *class_bytes = _class_defs[j].class_bytes;
  1463. + *class_byte_count = _class_defs[j].class_byte_count;
  1464. + }
  1465. +
  1466. + return JVMTI_ERROR_NONE;
  1467. + }
  1468. +
  1469. +// Prologue of the VM operation, called on the Java thread in parallel to normal program execution
  1470. +bool VM_EnhancedRedefineClasses::doit_prologue() {
  1471. +
  1472. + _revision_number++;
  1473. + RC_TRACE(0x00000001,
  1474. + ("Redefinition with revision number %d started!", _revision_number));
  1475. +
  1476. + assert(Thread::current()->is_Java_thread(), "must be Java thread");
  1477. + RC_TIMER_START(_timer_prologue);
  1478. +
  1479. + if (!check_arguments()) {
  1480. + RC_TIMER_STOP(_timer_prologue);
  1481. + return false;
  1482. + }
  1483. +
  1484. + // We first load new class versions in the prologue, because somewhere down the
  1485. + // call chain it is required that the current thread is a Java thread.
  1486. + _new_classes = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<instanceKlassHandle>(5, true);
  1487. +
  1488. + assert(_affected_klasses == NULL, "");
  1489. + _affected_klasses = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<instanceKlassHandle>(_class_count, true);
  1490. +
  1491. + _result = load_new_class_versions(Thread::current());
  1492. +
  1493. + RC_TRACE(0x00000001,
  1494. + ("Loaded new class versions!"));
  1495. + if (_result != JVMTI_ERROR_NONE) {
  1496. + RC_TRACE(0x00000001,
  1497. + ("error occured: %d!", _result));
  1498. + delete _new_classes;
  1499. + _new_classes = NULL;
  1500. + delete _affected_klasses;
  1501. + _affected_klasses = NULL;
  1502. + RC_TIMER_STOP(_timer_prologue);
  1503. + return false;
  1504. + }
  1505. +
  1506. + VM_GC_Operation::doit_prologue();
  1507. + RC_TIMER_STOP(_timer_prologue);
  1508. +
  1509. + RC_TRACE(0x00000001, ("doit_prologue finished!"));
  1510. + return true;
  1511. +}
  1512. +
  1513. +// Checks basic properties of the arguments of the redefinition command.
  1514. +jvmtiError VM_EnhancedRedefineClasses::check_arguments_error() {
  1515. + if (_class_defs == NULL) return JVMTI_ERROR_NULL_POINTER;
  1516. + for (int i = 0; i < _class_count; i++) {
  1517. + if (_class_defs[i].klass == NULL) return JVMTI_ERROR_INVALID_CLASS;
  1518. + if (_class_defs[i].class_byte_count == 0) return JVMTI_ERROR_INVALID_CLASS_FORMAT;
  1519. + if (_class_defs[i].class_bytes == NULL) return JVMTI_ERROR_NULL_POINTER;
  1520. + }
  1521. + return JVMTI_ERROR_NONE;
  1522. + }
  1523. +
  1524. +// Returns false and sets an result error code if the redefinition should be aborted.
  1525. +bool VM_EnhancedRedefineClasses::check_arguments() {
  1526. + jvmtiError error = check_arguments_error();
  1527. + if (error != JVMTI_ERROR_NONE || _class_count == 0) {
  1528. + _result = error;
  1529. + return false;
  1530. + }
  1531. + return true;
  1532. +}
  1533. +
  1534. +jvmtiError VM_EnhancedRedefineClasses::check_exception() const {
  1535. + Thread* THREAD = Thread::current();
  1536. + if (HAS_PENDING_EXCEPTION) {
  1537. +
  1538. + Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
  1539. + RC_TRACE_WITH_THREAD(0x00000002, THREAD, ("parse_stream exception: '%s'", ex_name->as_C_string()));
  1540. + CLEAR_PENDING_EXCEPTION;
  1541. +
  1542. + if (ex_name == vmSymbols::java_lang_UnsupportedClassVersionError()) {
  1543. + return JVMTI_ERROR_UNSUPPORTED_VERSION;
  1544. + } else if (ex_name == vmSymbols::java_lang_ClassFormatError()) {
  1545. + return JVMTI_ERROR_INVALID_CLASS_FORMAT;
  1546. + } else if (ex_name == vmSymbols::java_lang_ClassCircularityError()) {
  1547. + return JVMTI_ERROR_CIRCULAR_CLASS_DEFINITION;
  1548. + } else if (ex_name == vmSymbols::java_lang_NoClassDefFoundError()) {
  1549. + // The message will be "XXX (wrong name: YYY)"
  1550. + return JVMTI_ERROR_NAMES_DONT_MATCH;
  1551. + } else if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
  1552. + return JVMTI_ERROR_OUT_OF_MEMORY;
  1553. + } else {
  1554. + // Just in case more exceptions can be thrown..
  1555. + return JVMTI_ERROR_FAILS_VERIFICATION;
  1556. + }
  1557. + }
  1558. +
  1559. + return JVMTI_ERROR_NONE;
  1560. +}
  1561. +
  1562. +// Loads all new class versions and stores the InstanceKlass handles in an array.
  1563. +jvmtiError VM_EnhancedRedefineClasses::load_new_class_versions(TRAPS) {
  1564. +
  1565. + ResourceMark rm(THREAD);
  1566. +
  1567. + RC_TRACE(0x00000001,
  1568. + ("loading new class versions (%d)", _class_count));
  1569. +
  1570. + // Retrieve an array of all classes that need to be redefined
  1571. + jvmtiError err = find_sorted_affected_classes();
  1572. + if (err != JVMTI_ERROR_NONE) {
  1573. + RC_TRACE(0x00000001,
  1574. + ("Error finding sorted affected classes: %d", (int)err));
  1575. + return err;
  1576. + }
  1577. +
  1578. +
  1579. + JvmtiThreadState *state = JvmtiThreadState::state_for(JavaThread::current());
  1580. +
  1581. + _max_redefinition_flags = Klass::NoRedefinition;
  1582. + jvmtiError result = JVMTI_ERROR_NONE;
  1583. +
  1584. + for (int i = 0; i < _affected_klasses->length(); i++) {
  1585. + instanceKlassHandle the_class = _affected_klasses->at(i);
  1586. +
  1587. + RC_TRACE(0x00000001,
  1588. + ("Processing affected class %s (%d of %d)",
  1589. + the_class->name()->as_C_string(),
  1590. + i + 1,
  1591. + _affected_klasses->length()));
  1592. +
  1593. + the_class->link_class(THREAD);
  1594. + result = check_exception();
  1595. + if (result != JVMTI_ERROR_NONE) break;
  1596. +
  1597. + // Find new class bytes
  1598. + const unsigned char* class_bytes;
  1599. + jint class_byte_count;
  1600. + jvmtiError error;
  1601. + jboolean not_changed;
  1602. + if ((error = find_class_bytes(the_class, &class_bytes, &class_byte_count, &not_changed)) != JVMTI_ERROR_NONE) {
  1603. + RC_TRACE_WITH_THREAD(0x00000002, THREAD,
  1604. + ("Error finding class bytes: %d", (int)error));
  1605. + result = error;
  1606. + break;
  1607. + }
  1608. + assert(class_bytes != NULL && class_byte_count != 0, "Class bytes defined at this point!");
  1609. +
  1610. +
  1611. + // Set redefined class handle in JvmtiThreadState class.
  1612. + // This redefined class is sent to agent event handler for class file
  1613. + // load hook event.
  1614. + state->set_class_being_redefined(&the_class, _class_load_kind);
  1615. +
  1616. + RC_TIMER_STOP(_timer_prologue);
  1617. + RC_TIMER_START(_timer_class_loading);
  1618. +
  1619. + // Parse the stream.
  1620. + Handle the_class_loader(THREAD, the_class->class_loader());
  1621. + Handle protection_domain(THREAD, the_class->protection_domain());
  1622. + ClassFileStream st((u1*) class_bytes, class_byte_count, (char *)"__VM_EhnancedRedefineClasses__");
  1623. +
  1624. + Klass* klass =
  1625. + SystemDictionary::resolve_from_stream(
  1626. + the_class->name(),
  1627. + the_class_loader,
  1628. + protection_domain,
  1629. + &st,
  1630. + true,
  1631. + the_class,
  1632. + THREAD);
  1633. + instanceKlassHandle new_class(THREAD, klass);
  1634. +
  1635. + RC_TIMER_STOP(_timer_class_loading);
  1636. + RC_TIMER_START(_timer_prologue);
  1637. +
  1638. + // Clear class_being_redefined just to be sure.
  1639. + state->clear_class_being_redefined();
  1640. +
  1641. + result = check_exception();
  1642. + if (result != JVMTI_ERROR_NONE) break;
  1643. +
  1644. + not_changed = false;
  1645. +
  1646. +#ifdef ASSERT
  1647. +
  1648. + assert(new_class() != NULL, "Class could not be loaded!");
  1649. + assert(new_class() != the_class(), "must be different");
  1650. + assert(new_class->new_version() == NULL && new_class->old_version() != NULL, "");
  1651. +
  1652. +
  1653. + Array<Klass*>* k_interfaces = new_class->local_interfaces();
  1654. + for (int j = 0; j < k_interfaces->length(); j++) {
  1655. + assert(k_interfaces->at(j)->is_newest_version(), "just checking");
  1656. + }
  1657. +
  1658. + if (!THREAD->is_Compiler_thread()) {
  1659. + RC_TRACE(0x00000001, ("name=%s loader="INTPTR_FORMAT" protection_domain="INTPTR_FORMAT,
  1660. + the_class->name()->as_C_string(),
  1661. + (intptr_t) (oopDesc*) the_class->class_loader(),
  1662. + (intptr_t) (oopDesc*) the_class->protection_domain()));
  1663. + // If we are on the compiler thread, we must not try to resolve a class.
  1664. + Klass* systemLookup = SystemDictionary::resolve_or_null(the_class->name(), the_class->class_loader(), the_class->protection_domain(), THREAD);
  1665. +
  1666. + if (systemLookup != NULL) {
  1667. + assert(systemLookup == new_class->old_version(), "Old class must be in system dictionary!");
  1668. + Klass *subklass = new_class()->subklass();
  1669. + while (subklass != NULL) {
  1670. + assert(subklass->new_version() == NULL, "Most recent version of class!");
  1671. + subklass = subklass->next_sibling();
  1672. + }
  1673. + } else {
  1674. + // This can happen for reflection generated classes.. ?
  1675. + CLEAR_PENDING_EXCEPTION;
  1676. + }
  1677. + }
  1678. +
  1679. +#endif
  1680. +
  1681. + if (RC_TRACE_ENABLED(0x00000001)) {
  1682. + if (new_class->layout_helper() != the_class->layout_helper()) {
  1683. + RC_TRACE(0x00000001,
  1684. + ("Instance size change for class %s: new=%d old=%d",
  1685. + new_class->name()->as_C_string(),
  1686. + new_class->layout_helper(),
  1687. + the_class->layout_helper()));
  1688. + }
  1689. + }
  1690. +
  1691. + // Set the new version of the class
  1692. + new_class->set_revision_number(_revision_number);
  1693. + new_class->set_redefinition_index(i);
  1694. + the_class->set_new_version(new_class());
  1695. + _new_classes->append(new_class);
  1696. +
  1697. + assert(new_class->new_version() == NULL, "");
  1698. +
  1699. + int redefinition_flags = Klass::NoRedefinition;
  1700. +
  1701. + if (not_changed) {
  1702. + redefinition_flags = Klass::NoRedefinition;
  1703. + } else {
  1704. + redefinition_flags = calculate_redefinition_flags(new_class);
  1705. + if (redefinition_flags >= Klass::RemoveSuperType) {
  1706. + result = JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED;
  1707. + break;
  1708. + }
  1709. + }
  1710. +
  1711. + if (new_class->super() != NULL) {
  1712. + redefinition_flags = redefinition_flags | new_class->super()->redefinition_flags();
  1713. + }
  1714. +
  1715. + for (int j = 0; j<new_class->local_interfaces()->length(); j++) {
  1716. + redefinition_flags = redefinition_flags | (new_class->local_interfaces()->at(j))->redefinition_flags();
  1717. + }
  1718. +
  1719. + new_class->set_redefinition_flags(redefinition_flags);
  1720. +
  1721. + _max_redefinition_flags = _max_redefinition_flags | redefinition_flags;
  1722. +
  1723. + if ((redefinition_flags & Klass::ModifyInstances) != 0) {
  1724. + // TODO: Check if watch access flags of static fields are updated correctly.
  1725. + calculate_instance_update_information(_new_classes->at(i)());
  1726. + } else {
  1727. + // Fields were not changed, transfer special flags only
  1728. + assert(new_class->layout_helper() >> 1 == new_class->old_version()->layout_helper() >> 1, "must be equal");
  1729. + assert(new_class->fields()->length() == InstanceKlass::cast(new_class->old_version())->fields()->length(), "must be equal");
  1730. +
  1731. + JavaFieldStream old_fs(the_class);
  1732. + JavaFieldStream new_fs(new_class);
  1733. + for (; !old_fs.done() && !new_fs.done(); old_fs.next(), new_fs.next()) {
  1734. + AccessFlags flags = new_fs.access_flags();
  1735. + flags.set_is_field_modification_watched(old_fs.access_flags().is_field_modification_watched());
  1736. + flags.set_is_field_access_watched(old_fs.access_flags().is_field_access_watched());
  1737. + new_fs.set_access_flags(flags);
  1738. + }
  1739. + }
  1740. +
  1741. + if (RC_TRACE_ENABLED(0x00000001)) {
  1742. + RC_TRACE(0x00000001,
  1743. + ("Super class is %s", new_class->super()->name()->as_C_string()));
  1744. + }
  1745. +
  1746. +#ifdef ASSERT
  1747. + assert(new_class->super() == NULL || new_class->super()->new_version() == NULL, "Super klass must be newest version!");
  1748. +
  1749. + the_class->vtable()->verify(tty);
  1750. + new_class->vtable()->verify(tty);
  1751. +#endif
  1752. +
  1753. + if (i == _affected_klasses->length() - 1) {
  1754. + // This was the last class processed => check if additional classes have been loaded in the meantime
  1755. + for (int j = 0; j<_affected_klasses->length(); j++) {
  1756. +
  1757. + Klass* initial_klass = _affected_klasses->at(j)();
  1758. + Klass *initial_subklass = initial_klass->subklass();
  1759. + Klass *cur_klass = initial_subklass;
  1760. + while(cur_klass != NULL) {
  1761. +
  1762. + if(cur_klass->oop_is_instance() && cur_klass->is_newest_version() && !cur_klass->is_redefining()) {
  1763. + instanceKlassHandle handle(THREAD, cur_klass);
  1764. + if (!_affected_klasses->contains(handle)) {
  1765. +
  1766. + int k = i + 1;
  1767. + for (; k<_affected_klasses->length(); k++) {
  1768. + if (_affected_klasses->at(k)->is_subtype_of(cur_klass)) {
  1769. + break;
  1770. + }
  1771. + }
  1772. + _affected_klasses->insert_before(k, handle);
  1773. + RC_TRACE(0x00000001,
  1774. + ("Adding newly loaded class to affected classes: %s", cur_klass->name()->as_C_string()));
  1775. + }
  1776. + }
  1777. +
  1778. + cur_klass = cur_klass->next_sibling();
  1779. + }
  1780. + }
  1781. +
  1782. + int new_count = _affected_klasses->length() - 1 - i;
  1783. + if (new_count != 0) {
  1784. + RC_TRACE(0x00000001,
  1785. + ("Found new number of affected classes: %d", new_count));
  1786. + }
  1787. + }
  1788. + }
  1789. +
  1790. + if (result != JVMTI_ERROR_NONE) {
  1791. + rollback();
  1792. + return result;
  1793. + }
  1794. +
  1795. + RC_TIMER_STOP(_timer_prologue);
  1796. + RC_TIMER_START(_timer_class_linking);
  1797. + // Link and verify new classes _after_ all classes have been updated in the system dictionary!
  1798. + for (int i=0; i<_affected_klasses->length(); i++) {
  1799. + instanceKlassHandle the_class = _affected_klasses->at(i);
  1800. + instanceKlassHandle new_class(the_class->new_version());
  1801. +
  1802. + RC_TRACE(0x00000001,
  1803. + ("Linking class %d/%d %s", i, _affected_klasses->length(), the_class->name()->as_C_string()));
  1804. + new_class->link_class(THREAD);
  1805. +
  1806. + result = check_exception();
  1807. + if (result != JVMTI_ERROR_NONE) break;
  1808. + }
  1809. + RC_TIMER_STOP(_timer_class_linking);
  1810. + RC_TIMER_START(_timer_prologue);
  1811. +
  1812. + if (result != JVMTI_ERROR_NONE) {
  1813. + rollback();
  1814. + return result;
  1815. + }
  1816. +
  1817. + RC_TRACE(0x00000001, ("All classes loaded!"));
  1818. +
  1819. +#ifdef ASSERT
  1820. + for (int i=0; i<_affected_klasses->length(); i++) {
  1821. + instanceKlassHandle the_class = _affected_klasses->at(i);
  1822. + assert(the_class->new_version() != NULL, "Must have been redefined");
  1823. + instanceKlassHandle new_version = instanceKlassHandle(THREAD, the_class->new_version());
  1824. + assert(new_version->new_version() == NULL, "Must be newest version");
  1825. +
  1826. + if (!(new_version->super() == NULL || new_version->super()->new_version() == NULL)) {
  1827. + new_version()->print();
  1828. + new_version->super()->print();
  1829. + }
  1830. + assert(new_version->super() == NULL || new_version->super()->new_version() == NULL, "Super class must be newest version");
  1831. + }
  1832. +
  1833. + SystemDictionary::classes_do(check_class, THREAD);
  1834. +
  1835. +#endif
  1836. +
  1837. + RC_TRACE(0x00000001, ("Finished verification!"));
  1838. + return JVMTI_ERROR_NONE;
  1839. +}
  1840. +
  1841. +int VM_EnhancedRedefineClasses::calculate_redefinition_flags(instanceKlassHandle new_class) {
  1842. +
  1843. + int result = Klass::NoRedefinition;
  1844. + RC_TRACE(0x00000001,
  1845. + ("Comparing different class versions of class %s", new_class->name()->as_C_string()));
  1846. +
  1847. + assert(new_class->old_version() != NULL, "must have old version");
  1848. + instanceKlassHandle the_class(new_class->old_version());
  1849. +
  1850. + // Check whether class is in the error init state.
  1851. + if (the_class->is_in_error_state()) {
  1852. + // TBD #5057930: special error code is needed in 1.6
  1853. + //result = Klass::union_redefinition_level(result, Klass::Invalid);
  1854. + }
  1855. +
  1856. + int i;
  1857. +
  1858. + //////////////////////////////////////////////////////////////////////////////////////////////////////////
  1859. + // Check superclasses
  1860. + assert(new_class->super() == NULL || new_class->super()->is_newest_version(), "");
  1861. + if (the_class->super() != new_class->super()) {
  1862. + // Super class changed
  1863. + Klass* cur_klass = the_class->super();
  1864. + while (cur_klass != NULL) {
  1865. + if (!new_class->is_subclass_of(cur_klass->newest_version())) {
  1866. + RC_TRACE(0x00000001,
  1867. + ("Removed super class %s", cur_klass->name()->as_C_string()));
  1868. + result = result | Klass::RemoveSuperType | Klass::ModifyInstances | Klass::ModifyClass;
  1869. +
  1870. + if (!cur_klass->has_subtype_changed()) {
  1871. + RC_TRACE(0x00000001,
  1872. + ("Subtype changed of class %s", cur_klass->name()->as_C_string()));
  1873. + cur_klass->set_subtype_changed(true);
  1874. + }
  1875. + }
  1876. +
  1877. + cur_klass = cur_klass->super();
  1878. + }
  1879. +
  1880. + cur_klass = new_class->super();
  1881. + while (cur_klass != NULL) {
  1882. + if (!the_class->is_subclass_of(cur_klass->old_version())) {
  1883. + RC_TRACE(0x00000001,
  1884. + ("Added super class %s", cur_klass->name()->as_C_string()));
  1885. + result = result | Klass::ModifyClass | Klass::ModifyInstances;
  1886. + }
  1887. + cur_klass = cur_klass->super();
  1888. + }
  1889. + }
  1890. +
  1891. + //////////////////////////////////////////////////////////////////////////////////////////////////////////
  1892. + // Check interfaces
  1893. +
  1894. + // Interfaces removed?
  1895. + Array<Klass*>* old_interfaces = the_class->transitive_interfaces();
  1896. + for (i = 0; i<old_interfaces->length(); i++) {
  1897. + instanceKlassHandle old_interface(old_interfaces->at(i));
  1898. + if (!new_class->implements_interface_any_version(old_interface())) {
  1899. + result = result | Klass::RemoveSuperType | Klass::ModifyClass;
  1900. + RC_TRACE(0x00000001,
  1901. + ("Removed interface %s", old_interface->name()->as_C_string()));
  1902. +
  1903. + if (!old_interface->has_subtype_changed()) {
  1904. + RC_TRACE(0x00000001,
  1905. + ("Subtype changed of interface %s", old_interface->name()->as_C_string()));
  1906. + old_interface->set_subtype_changed(true);
  1907. + }
  1908. + }
  1909. + }
  1910. +
  1911. + // Interfaces added?
  1912. + Array<Klass*>* new_interfaces = new_class->transitive_interfaces();
  1913. + for (i = 0; i<new_interfaces->length(); i++) {
  1914. + if (!the_class->implements_interface_any_version(new_interfaces->at(i))) {
  1915. + result = result | Klass::ModifyClass;
  1916. + RC_TRACE(0x00000001,
  1917. + ("Added interface %s", new_interfaces->at(i)->name()->as_C_string()));
  1918. + }
  1919. + }
  1920. +
  1921. +
  1922. + // Check whether class modifiers are the same.
  1923. + jushort old_flags = (jushort) the_class->access_flags().get_flags();
  1924. + jushort new_flags = (jushort) new_class->access_flags().get_flags();
  1925. + if (old_flags != new_flags) {
  1926. + // TODO Can this have any effects?
  1927. + }
  1928. +
  1929. + // Check if the number, names, types and order of fields declared in these classes
  1930. + // are the same.
  1931. + JavaFieldStream old_fs(the_class);
  1932. + JavaFieldStream new_fs(new_class);
  1933. + for (; !old_fs.done() && !new_fs.done(); old_fs.next(), new_fs.next()) {
  1934. + // access
  1935. + old_flags = old_fs.access_flags().as_short();
  1936. + new_flags = new_fs.access_flags().as_short();
  1937. + if ((old_flags ^ new_flags) & JVM_RECOGNIZED_FIELD_MODIFIERS) {
  1938. + // TODO can this have any effect?
  1939. + }
  1940. + // offset
  1941. + if (old_fs.offset() != new_fs.offset()) {
  1942. + result = result | Klass::ModifyInstances;
  1943. + }
  1944. + // name and signature
  1945. + Symbol* name_sym1 = the_class->constants()->symbol_at(old_fs.name_index());
  1946. + Symbol* sig_sym1 = the_class->constants()->symbol_at(old_fs.signature_index());
  1947. + Symbol* name_sym2 = new_class->constants()->symbol_at(new_fs.name_index());
  1948. + Symbol* sig_sym2 = new_class->constants()->symbol_at(new_fs.signature_index());
  1949. + if (name_sym1 != name_sym2 || sig_sym1 != sig_sym2) {
  1950. + result = result | Klass::ModifyInstances;
  1951. + }
  1952. + }
  1953. +
  1954. + // If both streams aren't done then we have a differing number of
  1955. + // fields.
  1956. + if (!old_fs.done() || !new_fs.done()) {
  1957. + result = result | Klass::ModifyInstances;
  1958. + }
  1959. +
  1960. + // Do a parallel walk through the old and new methods. Detect
  1961. + // cases where they match (exist in both), have been added in
  1962. + // the new methods, or have been deleted (exist only in the
  1963. + // old methods). The class file parser places methods in order
  1964. + // by method name, but does not order overloaded methods by
  1965. + // signature. In order to determine what fate befell the methods,
  1966. + // this code places the overloaded new methods that have matching
  1967. + // old methods in the same order as the old methods and places
  1968. + // new overloaded methods at the end of overloaded methods of
  1969. + // that name. The code for this order normalization is adapted
  1970. + // from the algorithm used in InstanceKlass::find_method().
  1971. + // Since we are swapping out of order entries as we find them,
  1972. + // we only have to search forward through the overloaded methods.
  1973. + // Methods which are added and have the same name as an existing
  1974. + // method (but different signature) will be put at the end of
  1975. + // the methods with that name, and the name mismatch code will
  1976. + // handle them.
  1977. + Array<Method*>* k_old_methods(the_class->methods()); // FIXME-isd: handles???
  1978. + Array<Method*>* k_new_methods(new_class->methods());
  1979. + int n_old_methods = k_old_methods->length();
  1980. + int n_new_methods = k_new_methods->length();
  1981. +
  1982. + int ni = 0;
  1983. + int oi = 0;
  1984. + while (true) {
  1985. + Method* k_old_method;
  1986. + Method* k_new_method;
  1987. + enum { matched, added, deleted, undetermined } method_was = undetermined;
  1988. +
  1989. + if (oi >= n_old_methods) {
  1990. + if (ni >= n_new_methods) {
  1991. + break; // we've looked at everything, done
  1992. + }
  1993. + // New method at the end
  1994. + k_new_method = k_new_methods->at(ni);
  1995. + method_was = added;
  1996. + } else if (ni >= n_new_methods) {
  1997. + // Old method, at the end, is deleted
  1998. + k_old_method = k_old_methods->at(oi);
  1999. + method_was = deleted;
  2000. + } else {
  2001. + // There are more methods in both the old and new lists
  2002. + k_old_method = k_old_methods->at(oi);
  2003. + k_new_method = k_new_methods->at(ni);
  2004. + if (k_old_method->name() != k_new_method->name()) {
  2005. + // Methods are sorted by method name, so a mismatch means added
  2006. + // or deleted
  2007. + if (k_old_method->name()->fast_compare(k_new_method->name()) > 0) {
  2008. + method_was = added;
  2009. + } else {
  2010. + method_was = deleted;
  2011. + }
  2012. + } else if (k_old_method->signature() == k_new_method->signature()) {
  2013. + // Both the name and signature match
  2014. + method_was = matched;
  2015. + } else {
  2016. + // The name matches, but the signature doesn't, which means we have to
  2017. + // search forward through the new overloaded methods.
  2018. + int nj; // outside the loop for post-loop check
  2019. + for (nj = ni + 1; nj < n_new_methods; nj++) {
  2020. + Method* m = k_new_methods->at(nj);
  2021. + if (k_old_method->name() != m->name()) {
  2022. + // reached another method name so no more overloaded methods
  2023. + method_was = deleted;
  2024. + break;
  2025. + }
  2026. + if (k_old_method->signature() == m->signature()) {
  2027. + // found a match so swap the methods
  2028. + k_new_methods->at_put(ni, m);
  2029. + k_new_methods->at_put(nj, k_new_method);
  2030. + k_new_method = m;
  2031. + method_was = matched;
  2032. + break;
  2033. + }
  2034. + }
  2035. +
  2036. + if (nj >= n_new_methods) {
  2037. + // reached the end without a match; so method was deleted
  2038. + method_was = deleted;
  2039. + }
  2040. + }
  2041. + }
  2042. +
  2043. + switch (method_was) {
  2044. + case matched:
  2045. + // methods match, be sure modifiers do too
  2046. + old_flags = (jushort) k_old_method->access_flags().get_flags();
  2047. + new_flags = (jushort) k_new_method->access_flags().get_flags();
  2048. + if ((old_flags ^ new_flags) & ~(JVM_ACC_NATIVE)) {
  2049. + // TODO Can this have any effects? Probably yes on vtables?
  2050. + result = result | Klass::ModifyClass;
  2051. + }
  2052. + {
  2053. + u2 new_num = k_new_method->method_idnum();
  2054. + u2 old_num = k_old_method->method_idnum();
  2055. + if (new_num != old_num) {
  2056. + Method* idnum_owner = new_class->method_with_idnum(old_num);
  2057. + if (idnum_owner != NULL) {
  2058. + // There is already a method assigned this idnum -- switch them
  2059. + idnum_owner->set_method_idnum(new_num);
  2060. + }
  2061. + k_new_method->set_method_idnum(old_num);
  2062. + RC_TRACE(0x00008000,
  2063. + ("swapping idnum of new and old method %d / %d!", new_num, old_num);
  2064. + swap_all_method_annotations(k_old_method->constMethod(), k_new_method->constMethod()));
  2065. + }
  2066. + }
  2067. + RC_TRACE(0x00008000, ("Method matched: new: %s [%d] == old: %s [%d]",
  2068. + k_new_method->name_and_sig_as_C_string(), ni,
  2069. + k_old_method->name_and_sig_as_C_string(), oi));
  2070. + // advance to next pair of methods
  2071. + ++oi;
  2072. + ++ni;
  2073. + break;
  2074. + case added:
  2075. + // method added, see if it is OK
  2076. + new_flags = (jushort) k_new_method->access_flags().get_flags();
  2077. + if ((new_flags & JVM_ACC_PRIVATE) == 0
  2078. + // hack: private should be treated as final, but alas
  2079. + || (new_flags & (JVM_ACC_FINAL|JVM_ACC_STATIC)) == 0) {
  2080. + // new methods must be private
  2081. + result = result | Klass::ModifyClass;
  2082. + }
  2083. + {
  2084. + u2 num = new_class->next_method_idnum();
  2085. + if (num == ConstMethod::UNSET_IDNUM) {
  2086. + // cannot add any more methods
  2087. + result = result | Klass::ModifyClass;
  2088. + }
  2089. + u2 new_num = k_new_method->method_idnum();
  2090. + Method* idnum_owner = new_class->method_with_idnum(num);
  2091. + if (idnum_owner != NULL) {
  2092. + // There is already a method assigned this idnum -- switch them
  2093. + idnum_owner->set_method_idnum(new_num);
  2094. + }
  2095. + k_new_method->set_method_idnum(num);
  2096. + swap_all_method_annotations(k_old_method->constMethod(), k_new_method->constMethod());
  2097. + }
  2098. + RC_TRACE(0x00008000, ("Method added: new: %s [%d], idnum %d",
  2099. + k_new_method->name_and_sig_as_C_string(), ni, k_new_method->method_idnum()));
  2100. + ++ni; // advance to next new method
  2101. + break;
  2102. + case deleted:
  2103. + // method deleted, see if it is OK
  2104. + old_flags = (jushort) k_old_method->access_flags().get_flags();
  2105. + if ((old_flags & JVM_ACC_PRIVATE) == 0
  2106. + // hack: private should be treated as final, but alas
  2107. + || (old_flags & (JVM_ACC_FINAL|JVM_ACC_STATIC)) == 0
  2108. + ) {
  2109. + // deleted methods must be private
  2110. + result = result | Klass::ModifyClass;
  2111. + }
  2112. + RC_TRACE(0x00008000, ("Method deleted: old: %s [%d]",
  2113. + k_old_method->name_and_sig_as_C_string(), oi));
  2114. + ++oi; // advance to next old method
  2115. + break;
  2116. + default:
  2117. + ShouldNotReachHere();
  2118. + }
  2119. + }
  2120. +
  2121. + if (new_class()->size() != new_class->old_version()->size()) {
  2122. + result |= Klass::ModifyClassSize;
  2123. + }
  2124. +
  2125. + if (new_class->size_helper() != ((InstanceKlass*)(new_class->old_version()))->size_helper()) {
  2126. + result |= Klass::ModifyInstanceSize;
  2127. + }
  2128. +
  2129. + // TODO Check method bodies to be able to return NoChange?
  2130. + return result;
  2131. +}
  2132. +
  2133. +void VM_EnhancedRedefineClasses::calculate_instance_update_information(Klass* new_version) {
  2134. +
  2135. + class CalculateFieldUpdates : public FieldClosure {
  2136. +
  2137. + private:
  2138. + InstanceKlass* _old_ik;
  2139. + GrowableArray<int> _update_info;
  2140. + int _position;
  2141. + bool _copy_backwards;
  2142. +
  2143. + public:
  2144. +
  2145. + bool does_copy_backwards() {
  2146. + return _copy_backwards;
  2147. + }
  2148. +
  2149. + CalculateFieldUpdates(InstanceKlass* old_ik) :
  2150. + _old_ik(old_ik), _position(instanceOopDesc::base_offset_in_bytes()), _copy_backwards(false) {
  2151. + _update_info.append(_position);
  2152. + _update_info.append(0);
  2153. + }
  2154. +
  2155. + GrowableArray<int> &finish() {
  2156. + _update_info.append(0);
  2157. + return _update_info;
  2158. + }
  2159. +
  2160. + void do_field(fieldDescriptor* fd) {
  2161. + int alignment = fd->offset() - _position;
  2162. + if (alignment > 0) {
  2163. + // This field was aligned, so we need to make sure that we fill the gap
  2164. + fill(alignment);
  2165. + }
  2166. +
  2167. + assert(_position == fd->offset(), "must be correct offset!");
  2168. +
  2169. + fieldDescriptor old_fd;
  2170. + if (_old_ik->find_field(fd->name(), fd->signature(), false, &old_fd) != NULL) {
  2171. + // Found field in the old class, copy
  2172. + copy(old_fd.offset(), type2aelembytes(fd->field_type()));
  2173. +
  2174. + if (old_fd.offset() < fd->offset()) {
  2175. + _copy_backwards = true;
  2176. + }
  2177. +
  2178. + // Transfer special flags
  2179. + fd->set_is_field_modification_watched(old_fd.is_field_modification_watched());
  2180. + fd->set_is_field_access_watched(old_fd.is_field_access_watched());
  2181. + } else {
  2182. + // New field, fill
  2183. + fill(type2aelembytes(fd->field_type()));
  2184. + }
  2185. + }
  2186. +
  2187. + private:
  2188. +
  2189. + void fill(int size) {
  2190. + if (_update_info.length() > 0 && _update_info.at(_update_info.length() - 1) < 0) {
  2191. + (*_update_info.adr_at(_update_info.length() - 1)) -= size;
  2192. + } else {
  2193. + _update_info.append(-size);
  2194. + }
  2195. + _position += size;
  2196. + }
  2197. +
  2198. + void copy(int offset, int size) {
  2199. + int prev_end = -1;
  2200. + if (_update_info.length() > 0 && _update_info.at(_update_info.length() - 1) > 0) {
  2201. + prev_end = _update_info.at(_update_info.length() - 2) + _update_info.at(_update_info.length() - 1);
  2202. + }
  2203. +
  2204. + if (prev_end == offset) {
  2205. + (*_update_info.adr_at(_update_info.length() - 2)) += size;
  2206. + } else {
  2207. + _update_info.append(size);
  2208. + _update_info.append(offset);
  2209. + }
  2210. +
  2211. + _position += size;
  2212. + }
  2213. + };
  2214. +
  2215. + InstanceKlass* ik = InstanceKlass::cast(new_version);
  2216. + InstanceKlass* old_ik = InstanceKlass::cast(new_version->old_version());
  2217. + CalculateFieldUpdates cl(old_ik);
  2218. + ik->do_nonstatic_fields(&cl);
  2219. +
  2220. + GrowableArray<int> result = cl.finish();
  2221. + ik->store_update_information(result);
  2222. + ik->set_copying_backwards(cl.does_copy_backwards());
  2223. +
  2224. +
  2225. + if (RC_TRACE_ENABLED(0x00000001)) {
  2226. + RC_TRACE(0x00000001, ("Instance update information for %s:", new_version->name()->as_C_string()));
  2227. + if (cl.does_copy_backwards()) {
  2228. + RC_TRACE(0x00000001, ("\tDoes copy backwards!"));
  2229. + }
  2230. + for (int i=0; i<result.length(); i++) {
  2231. + int curNum = result.at(i);
  2232. + if (curNum < 0) {
  2233. + RC_TRACE(0x00000001, ("\t%d CLEAN", curNum));
  2234. + } else if (curNum > 0) {
  2235. + RC_TRACE(0x00000001, ("\t%d COPY from %d", curNum, result.at(i + 1)));
  2236. + i++;
  2237. + } else {
  2238. + RC_TRACE(0x00000001, ("\tEND"));
  2239. + }
  2240. + }
  2241. + }
  2242. +}
  2243. +
  2244. +void VM_EnhancedRedefineClasses::rollback() {
  2245. + RC_TRACE(0x00000001, ("Rolling back redefinition!"));
  2246. + SystemDictionary::rollback_redefinition();
  2247. +
  2248. + for (int i=0; i<_new_classes->length(); i++) {
  2249. + SystemDictionary::remove_from_hierarchy(_new_classes->at(i));
  2250. + }
  2251. +
  2252. + for (int i=0; i<_new_classes->length(); i++) {
  2253. + instanceKlassHandle new_class = _new_classes->at(i);
  2254. + new_class->set_redefining(false);
  2255. + new_class->old_version()->set_new_version(NULL);
  2256. + new_class->set_old_version(NULL);
  2257. + }
  2258. +
  2259. +}
  2260. +
  2261. +void VM_EnhancedRedefineClasses::swap_marks(oop first, oop second) {
  2262. + markOop first_mark = first->mark();
  2263. + markOop second_mark = second->mark();
  2264. + first->set_mark(second_mark);
  2265. + second->set_mark(first_mark);
  2266. +}
  2267. +
  2268. +class FieldCopier : public FieldClosure {
  2269. + public:
  2270. + void do_field(fieldDescriptor* fd) {
  2271. + InstanceKlass* cur = InstanceKlass::cast(fd->field_holder());
  2272. + oop cur_oop = cur->java_mirror();
  2273. +
  2274. + InstanceKlass* old = InstanceKlass::cast(cur->old_version());
  2275. + oop old_oop = old->java_mirror();
  2276. +
  2277. + fieldDescriptor result;
  2278. + bool found = old->find_local_field(fd->name(), fd->signature(), &result);
  2279. + if (found && result.is_static()) {
  2280. + RC_TRACE(0x00000001, ("Copying static field value for field %s old_offset=%d new_offset=%d",
  2281. + fd->name()->as_C_string(), result.offset(), fd->offset()));
  2282. + memcpy(cur_oop->obj_field_addr<HeapWord>(fd->offset()),
  2283. + old_oop->obj_field_addr<HeapWord>(result.offset()),
  2284. + type2aelembytes(fd->field_type()));
  2285. +
  2286. + // Static fields may have references to java.lang.Class
  2287. + if (fd->field_type() == T_OBJECT) {
  2288. + oop oop = cur_oop->obj_field(fd->offset());
  2289. + if (oop != NULL && oop->is_instanceMirror()) {
  2290. + Klass* klass = java_lang_Class::as_Klass(oop);
  2291. + if (klass != NULL && klass->oop_is_instance()) {
  2292. + assert(oop == InstanceKlass::cast(klass)->java_mirror(), "just checking");
  2293. + if (klass->new_version() != NULL) {
  2294. + oop = InstanceKlass::cast(klass->new_version())->java_mirror();
  2295. + cur_oop->obj_field_put(fd->offset(), oop);
  2296. + }
  2297. + }
  2298. + }
  2299. + }
  2300. + }
  2301. + }
  2302. +};
  2303. +
  2304. +void VM_EnhancedRedefineClasses::mark_as_scavengable(nmethod* nm) {
  2305. + if (!nm->on_scavenge_root_list()) {
  2306. + CodeCache::add_scavenge_root_nmethod(nm);
  2307. + }
  2308. +}
  2309. +
  2310. +struct StoreBarrier {
  2311. + template <class T> static void oop_store(T* p, oop v) { ::oop_store(p, v); }
  2312. +};
  2313. +
  2314. +struct StoreNoBarrier {
  2315. + template <class T> static void oop_store(T* p, oop v) { oopDesc::encode_store_heap_oop_not_null(p, v); }
  2316. +};
  2317. +
  2318. +template <class S>
  2319. +class ChangePointersOopClosure : public ExtendedOopClosure {
  2320. + // import java_lang_invoke_MemberName.*
  2321. + enum {
  2322. + REFERENCE_KIND_SHIFT = java_lang_invoke_MemberName::MN_REFERENCE_KIND_SHIFT,
  2323. + REFERENCE_KIND_MASK = java_lang_invoke_MemberName::MN_REFERENCE_KIND_MASK,
  2324. + };
  2325. +
  2326. +
  2327. + bool update_member_name(oop obj) {
  2328. + int flags = java_lang_invoke_MemberName::flags(obj);
  2329. + int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK;
  2330. + if (MethodHandles::ref_kind_is_method(ref_kind)) {
  2331. + Method* m = (Method*) java_lang_invoke_MemberName::vmtarget(obj);
  2332. + if (m != NULL && !m->method_holder()->is_newest_version()) {
  2333. + // Let's try to re-resolve method
  2334. + InstanceKlass* newest = InstanceKlass::cast(m->method_holder()->newest_version());
  2335. + Method* new_method = newest->find_method(m->name(), m->signature());
  2336. +
  2337. + // Note: we might set NULL at this point, which should force AbstractMethodError at runtime
  2338. + java_lang_invoke_MemberName::set_vmtarget(obj, new_method);
  2339. + }
  2340. + } else if (MethodHandles::ref_kind_is_field(ref_kind)) {
  2341. + Klass* k = (Klass*) java_lang_invoke_MemberName::vmtarget(obj);
  2342. + if (k == NULL) {
  2343. + return false; // Was cleared before, this MemberName is invalid.
  2344. + }
  2345. +
  2346. + if (k != NULL && !k->is_newest_version()) {
  2347. + // Let's try to re-resolve field
  2348. + fieldDescriptor fd;
  2349. + int offset = java_lang_invoke_MemberName::vmindex(obj);
  2350. + bool is_static = MethodHandles::ref_kind_is_static(ref_kind);
  2351. + InstanceKlass* ik = InstanceKlass::cast(k);
  2352. + if (ik->find_local_field_from_offset(offset, is_static, &fd)) {
  2353. + InstanceKlass* newest = InstanceKlass::cast(k->newest_version());
  2354. + fieldDescriptor fd_new;
  2355. + if (newest->find_local_field(fd.name(), fd.signature(), &fd_new)) {
  2356. + java_lang_invoke_MemberName::set_vmtarget(obj, newest);
  2357. + java_lang_invoke_MemberName::set_vmindex(obj, fd_new.offset());
  2358. + } else {
  2359. + // Matching field is not found in new version, not much we can do here.
  2360. + // JVM will crash once faulty MH is invoked.
  2361. + // However, to avoid that all DMH's using this faulty MH are cleared (set to NULL)
  2362. + // Eventually, we probably want to replace them with something more meaningful,
  2363. + // like instance throwing NoSuchFieldError or DMH that will resort to dynamic
  2364. + // field resolution (with possibility of type conversion)
  2365. + java_lang_invoke_MemberName::set_vmtarget(obj, NULL);
  2366. + java_lang_invoke_MemberName::set_vmindex(obj, 0);
  2367. + return false;
  2368. + }
  2369. + }
  2370. + }
  2371. + }
  2372. + return true;
  2373. + }
  2374. +
  2375. + bool update_direct_method_handle(oop obj) {
  2376. + // Always update member name first.
  2377. + oop mem_name = java_lang_invoke_DirectMethodHandle::member(obj);
  2378. + if (!update_member_name(mem_name)) {
  2379. + return false;
  2380. + }
  2381. +
  2382. + // Here we rely on DirectMethodHandle implementation.
  2383. + // The current implementation caches field offset in $StaticAccessor/$Accessor
  2384. + int flags = java_lang_invoke_MemberName::flags(mem_name);
  2385. + int ref_kind = (flags >> REFERENCE_KIND_SHIFT) & REFERENCE_KIND_MASK;
  2386. + if (MethodHandles::ref_kind_is_field(ref_kind)) {
  2387. + // Note: we don't care about staticBase field (which is java.lang.Class)
  2388. + // It should be processed during normal object update.
  2389. + // Update offset in StaticAccessor
  2390. + int offset = java_lang_invoke_MemberName::vmindex(mem_name);
  2391. + if (offset != 0) { // index of 0 means that field no longer exist
  2392. + if (java_lang_invoke_DirectMethodHandle_StaticAccessor::is_instance(obj)) {
  2393. + java_lang_invoke_DirectMethodHandle_StaticAccessor::set_static_offset(obj, offset);
  2394. + } else if (java_lang_invoke_DirectMethodHandle_Accessor::is_instance(obj)) {
  2395. + java_lang_invoke_DirectMethodHandle_Accessor::set_field_offset(obj, offset);
  2396. + }
  2397. + }
  2398. + }
  2399. + return true;
  2400. + }
  2401. +
  2402. + // Forward pointers to InstanceKlass and mirror class to new versions
  2403. + template <class T>
  2404. + inline void do_oop_work(T* p) {
  2405. + oop obj = oopDesc::load_decode_heap_oop(p);
  2406. + if (obj == NULL) {
  2407. + return;
  2408. + }
  2409. + if (obj->is_instanceMirror()) {
  2410. + Klass* klass = java_lang_Class::as_Klass(obj);
  2411. + if (klass != NULL && klass->oop_is_instance()) {
  2412. + assert(obj == InstanceKlass::cast(klass)->java_mirror(), "just checking");
  2413. + if (klass->new_version() != NULL) {
  2414. + obj = InstanceKlass::cast(klass->new_version())->java_mirror();
  2415. + S::oop_store(p, obj);
  2416. + }
  2417. + }
  2418. + }
  2419. +
  2420. + // JSR 292 support, uptade java.lang.invoke.MemberName instances
  2421. + if (java_lang_invoke_MemberName::is_instance(obj)) {
  2422. + update_member_name(obj);
  2423. + } else if (java_lang_invoke_DirectMethodHandle::is_instance(obj)) {
  2424. + if (!update_direct_method_handle(obj)) {
  2425. + // DMH is no longer valid, replace it with null reference.
  2426. + // See note above. We probably want to replace this with something more meaningful.
  2427. + S::oop_store(p, NULL);
  2428. + }
  2429. + }
  2430. + }
  2431. +
  2432. + virtual void do_oop(oop* o) {
  2433. + do_oop_work(o);
  2434. + }
  2435. +
  2436. + virtual void do_oop(narrowOop* o) {
  2437. + do_oop_work(o);
  2438. + }
  2439. +};
  2440. +
  2441. +void VM_EnhancedRedefineClasses::doit() {
  2442. +
  2443. + Thread *thread = Thread::current();
  2444. +
  2445. + assert((_max_redefinition_flags & Klass::RemoveSuperType) == 0, "removing super types not allowed");
  2446. +
  2447. + if (UseSharedSpaces) {
  2448. + // Sharing is enabled so we remap the shared readonly space to
  2449. + // shared readwrite, private just in case we need to redefine
  2450. + // a shared class. We do the remap during the doit() phase of
  2451. + // the safepoint to be safer.
  2452. + if (!MetaspaceShared::remap_shared_readonly_as_readwrite()) {
  2453. + RC_TRACE(0x00000001,
  2454. + ("failed to remap shared readonly space to readwrite, private"));
  2455. + _result = JVMTI_ERROR_INTERNAL;
  2456. + return;
  2457. + }
  2458. + }
  2459. +
  2460. + RC_TIMER_START(_timer_prepare_redefinition);
  2461. + for (int i = 0; i < _new_classes->length(); i++) {
  2462. + redefine_single_class(_new_classes->at(i), thread);
  2463. + }
  2464. +
  2465. + // Deoptimize all compiled code that depends on this class
  2466. + flush_dependent_code(instanceKlassHandle(Thread::current(), (Klass*)NULL), Thread::current());
  2467. +
  2468. + // Adjust constantpool caches for all classes
  2469. + // that reference methods of the evolved class.
  2470. + ClearCpoolCacheAndUnpatch clear_cpool_cache(Thread::current());
  2471. + ClassLoaderDataGraph::classes_do(&clear_cpool_cache);
  2472. +
  2473. + RC_TIMER_STOP(_timer_prepare_redefinition);
  2474. + RC_TIMER_START(_timer_heap_iteration);
  2475. +
  2476. + class ChangePointersObjectClosure : public ObjectClosure {
  2477. + private:
  2478. +
  2479. + OopClosure *_closure;
  2480. + bool _needs_instance_update;
  2481. + oop _tmp_obj;
  2482. + int _tmp_obj_size;
  2483. +
  2484. + public:
  2485. + ChangePointersObjectClosure(OopClosure *closure) : _closure(closure), _needs_instance_update(false), _tmp_obj(NULL), _tmp_obj_size(0) {}
  2486. +
  2487. + bool needs_instance_update() {
  2488. + return _needs_instance_update;
  2489. + }
  2490. +
  2491. + void copy_to_tmp(oop o) {
  2492. + int size = o->size();
  2493. + if (_tmp_obj_size < size) {
  2494. + _tmp_obj_size = size;
  2495. + _tmp_obj = (oop)resource_allocate_bytes(size * HeapWordSize);
  2496. + }
  2497. + Copy::aligned_disjoint_words((HeapWord*)o, (HeapWord*)_tmp_obj, size);
  2498. + }
  2499. +
  2500. + virtual void do_object(oop obj) {
  2501. + // FIXME: if (obj->is_instanceKlass()) return;
  2502. + if (obj->is_instanceMirror()) {
  2503. + // static fields may have references to old java.lang.Class instances, update them
  2504. + // at the same time, we don't want to update other oops in the java.lang.Class
  2505. + // Causes SIGSEGV?
  2506. + //instanceMirrorKlass::oop_fields_iterate(obj, _closure);
  2507. + } else {
  2508. + obj->oop_iterate_no_header(_closure);
  2509. + }
  2510. +
  2511. + if (obj->klass()->new_version() != NULL) {
  2512. + Klass* new_klass = obj->klass()->new_version();
  2513. + /* FIXME: if (obj->is_perm()) {
  2514. + _needs_instance_update = true;
  2515. + } else */if(new_klass->update_information() != NULL) {
  2516. + int size_diff = obj->size() - obj->size_given_klass(new_klass);
  2517. +
  2518. + // Either new size is bigger or gap is to small to be filled
  2519. + if (size_diff < 0 || (size_diff > 0 && (size_t) size_diff < CollectedHeap::min_fill_size())) {
  2520. + // We need an instance update => set back to old klass
  2521. + _needs_instance_update = true;
  2522. + } else {
  2523. + oop src = obj;
  2524. + if (new_klass->is_copying_backwards()) {
  2525. + copy_to_tmp(obj);
  2526. + src = _tmp_obj;
  2527. + }
  2528. + src->set_klass(obj->klass()->new_version());
  2529. + MarkSweep::update_fields(obj, src, new_klass->update_information());
  2530. +
  2531. + if (size_diff > 0) {
  2532. + HeapWord* dead_space = ((HeapWord *)obj) + obj->size();
  2533. + CollectedHeap::fill_with_object(dead_space, size_diff);
  2534. + }
  2535. + }
  2536. + } else {
  2537. + obj->set_klass(obj->klass()->new_version());
  2538. + }
  2539. + }
  2540. + }
  2541. + };
  2542. +
  2543. + ChangePointersOopClosure<StoreNoBarrier> oopClosureNoBarrier;
  2544. + ChangePointersOopClosure<StoreBarrier> oopClosure;
  2545. + ChangePointersObjectClosure objectClosure(&oopClosure);
  2546. +
  2547. + RC_TRACE(0x00000001, ("Before updating instances"));
  2548. + {
  2549. + // Since we may update oops inside nmethod's code blob to point to java.lang.Class in new generation, we need to
  2550. + // make sure such references are properly recognized by GC. For that, If ScavengeRootsInCode is true, we need to
  2551. + // mark such nmethod's as "scavengable".
  2552. + // For now, mark all nmethod's as scavengable that are not scavengable already
  2553. + if (ScavengeRootsInCode) {
  2554. + CodeCache::nmethods_do(mark_as_scavengable);
  2555. + }
  2556. +
  2557. + SharedHeap::heap()->gc_prologue(true);
  2558. + Universe::heap()->object_iterate(&objectClosure);
  2559. + Universe::root_oops_do(&oopClosureNoBarrier);
  2560. + SharedHeap::heap()->gc_epilogue(false);
  2561. + }
  2562. + RC_TRACE(0x00000001, ("After updating instances"));
  2563. +
  2564. + for (int i = 0; i < _new_classes->length(); i++) {
  2565. + InstanceKlass* cur = InstanceKlass::cast(_new_classes->at(i)());
  2566. + InstanceKlass* old = InstanceKlass::cast(cur->old_version());
  2567. +
  2568. + // Swap marks to have same hashcodes
  2569. + markOop cur_mark = cur->prototype_header();
  2570. + markOop old_mark = old->prototype_header();
  2571. + cur->set_prototype_header(old_mark);
  2572. + old->set_prototype_header(cur_mark);
  2573. +
  2574. + //swap_marks(cur, old);
  2575. + swap_marks(cur->java_mirror(), old->java_mirror());
  2576. +
  2577. + // Revert pool holder for old version of klass (it was updated by one of ours closure!)
  2578. + old->constants()->set_pool_holder(old);
  2579. +
  2580. + Klass* array_klasses = old->array_klasses();
  2581. + if (array_klasses != NULL) {
  2582. + assert(cur->array_klasses() == NULL, "just checking");
  2583. +
  2584. + // Transfer the array classes, otherwise we might get cast exceptions when casting array types.
  2585. + // Also, set array klasses element klass.
  2586. + cur->set_array_klasses(array_klasses);
  2587. + ObjArrayKlass::cast(array_klasses)->set_element_klass(cur);
  2588. + }
  2589. +
  2590. + // Initialize the new class! Special static initialization that does not execute the
  2591. + // static constructor but copies static field values from the old class if name
  2592. + // and signature of a static field match.
  2593. + FieldCopier copier;
  2594. + cur->do_local_static_fields(&copier); // TODO (tw): What about internal static fields??
  2595. + //java_lang_Class::set_klass(old->java_mirror(), cur); // FIXME-isd: is that correct?
  2596. + //FIXME-isd: do we need this: ??? old->set_java_mirror(cur->java_mirror());
  2597. +
  2598. + // Transfer init state
  2599. + InstanceKlass::ClassState state = old->init_state();
  2600. + if (state > InstanceKlass::linked) {
  2601. + cur->set_init_state(state);
  2602. + }
  2603. + }
  2604. +
  2605. + RC_TIMER_STOP(_timer_heap_iteration);
  2606. + RC_TIMER_START(_timer_redefinition);
  2607. + if (objectClosure.needs_instance_update()) {
  2608. + // Do a full garbage collection to update the instance sizes accordingly
  2609. + RC_TRACE(0x00000001, ("Before performing full GC!"));
  2610. + Universe::set_redefining_gc_run(true);
  2611. + notify_gc_begin(true);
  2612. + Universe::heap()->collect_as_vm_thread(GCCause::_heap_inspection);
  2613. + notify_gc_end();
  2614. + Universe::set_redefining_gc_run(false);
  2615. + RC_TRACE(0x00000001, ("GC done!"));
  2616. + }
  2617. +
  2618. + // Unmark Klass*s as "redefining"
  2619. + for (int i=0; i<_new_classes->length(); i++) {
  2620. + Klass* cur_klass = _new_classes->at(i)();
  2621. + InstanceKlass* cur = (InstanceKlass*)cur_klass;
  2622. + cur->set_redefining(false);
  2623. + cur->clear_update_information();
  2624. + }
  2625. +
  2626. + // Disable any dependent concurrent compilations
  2627. + SystemDictionary::notice_modification();
  2628. +
  2629. + // Set flag indicating that some invariants are no longer true.
  2630. + // See jvmtiExport.hpp for detailed explanation.
  2631. + JvmtiExport::set_has_redefined_a_class();
  2632. +
  2633. + // Clean up caches in the compiler interface and compiler threads
  2634. + ciObjectFactory::resort_shared_ci_metadata();
  2635. +
  2636. +#ifdef ASSERT
  2637. +
  2638. + // Universe::verify();
  2639. + // JNIHandles::verify();
  2640. +
  2641. + SystemDictionary::classes_do(check_class, thread);
  2642. +#endif
  2643. +
  2644. + RC_TIMER_STOP(_timer_redefinition);
  2645. +
  2646. + if (TraceRedefineClasses > 0) {
  2647. + tty->flush();
  2648. + }
  2649. +}
  2650. +
  2651. +void VM_EnhancedRedefineClasses::doit_epilogue() {
  2652. +
  2653. + RC_TIMER_START(_timer_vm_op_epilogue);
  2654. +
  2655. + ResourceMark mark;
  2656. +
  2657. + VM_GC_Operation::doit_epilogue();
  2658. + RC_TRACE(0x00000001, ("GC Operation epilogue finished!"));
  2659. +
  2660. + // Free the array of scratch classes
  2661. + delete _new_classes;
  2662. + _new_classes = NULL;
  2663. +
  2664. + // Free the array of affected classes
  2665. + delete _affected_klasses;
  2666. + _affected_klasses = NULL;
  2667. +
  2668. + RC_TRACE(0x00000001, ("Redefinition finished!"));
  2669. +
  2670. + RC_TIMER_STOP(_timer_vm_op_epilogue);
  2671. +}
  2672. +
  2673. +bool VM_EnhancedRedefineClasses::is_modifiable_class(oop klass_mirror) {
  2674. + // classes for primitives cannot be redefined
  2675. + if (java_lang_Class::is_primitive(klass_mirror)) {
  2676. + return false;
  2677. + }
  2678. + Klass* klass = java_lang_Class::as_Klass(klass_mirror);
  2679. + // classes for arrays cannot be redefined
  2680. + if (klass == NULL || !klass->oop_is_instance()) {
  2681. + return false;
  2682. + }
  2683. + return true;
  2684. +}
  2685. +
  2686. +#ifdef ASSERT
  2687. +
  2688. +void VM_EnhancedRedefineClasses::verify_classes(Klass* k_oop_latest, oop initiating_loader, TRAPS) {
  2689. + Klass* k_oop = k_oop_latest;
  2690. + while (k_oop != NULL) {
  2691. +
  2692. + instanceKlassHandle k_handle(THREAD, k_oop);
  2693. + Verifier::verify(k_handle, Verifier::ThrowException, true, THREAD);
  2694. + k_oop = k_oop->old_version();
  2695. + }
  2696. +}
  2697. +
  2698. +#endif
  2699. +
  2700. +// Rewrite faster byte-codes back to their slower equivalent. Undoes rewriting happening in templateTable_xxx.cpp
  2701. +// The reason is that once we zero cpool caches, we need to re-resolve all entries again. Faster bytecodes do not
  2702. +// do that, they assume that cache entry is resolved already.
  2703. +void VM_EnhancedRedefineClasses::unpatch_bytecode(Method* method) {
  2704. + RawBytecodeStream bcs(method);
  2705. + Bytecodes::Code code;
  2706. + Bytecodes::Code java_code;
  2707. + while (!bcs.is_last_bytecode()) {
  2708. + code = bcs.raw_next();
  2709. + address bcp = bcs.bcp();
  2710. +
  2711. + if (code == Bytecodes::_breakpoint) {
  2712. + int bci = method->bci_from(bcp);
  2713. + code = method->orig_bytecode_at(bci);
  2714. + java_code = Bytecodes::java_code(code);
  2715. + if (code != java_code &&
  2716. + (java_code == Bytecodes::_getfield ||
  2717. + java_code == Bytecodes::_putfield ||
  2718. + java_code == Bytecodes::_aload_0)) {
  2719. + // Let breakpoint table handling unpatch bytecode
  2720. + method->set_orig_bytecode_at(bci, java_code);
  2721. + }
  2722. + } else {
  2723. + java_code = Bytecodes::java_code(code);
  2724. + if (code != java_code &&
  2725. + (java_code == Bytecodes::_getfield ||
  2726. + java_code == Bytecodes::_putfield ||
  2727. + java_code == Bytecodes::_aload_0)) {
  2728. + *bcp = java_code;
  2729. + }
  2730. + }
  2731. +
  2732. + // Additionally, we need to unpatch bytecode at bcp+1 for fast_xaccess (which would be fast field access)
  2733. + if (code == Bytecodes::_fast_iaccess_0 || code == Bytecodes::_fast_aaccess_0 || code == Bytecodes::_fast_faccess_0) {
  2734. + Bytecodes::Code code2 = Bytecodes::code_or_bp_at(bcp + 1);
  2735. + assert(code2 == Bytecodes::_fast_igetfield ||
  2736. + code2 == Bytecodes::_fast_agetfield ||
  2737. + code2 == Bytecodes::_fast_fgetfield, "");
  2738. + *(bcp + 1) = Bytecodes::java_code(code2);
  2739. + }
  2740. + }
  2741. + }
  2742. +
  2743. +// Unevolving classes may point to old methods directly
  2744. +// from their constant pool caches, itables, and/or vtables. We
  2745. +// use the SystemDictionary::classes_do() facility and this helper
  2746. +// to fix up these pointers. Additional field offsets and vtable indices
  2747. +// in the constant pool cache entries are fixed.
  2748. +//
  2749. +// Note: We currently don't support updating the vtable in
  2750. +// arrayKlassOops. See Open Issues in jvmtiRedefineClasses.hpp.
  2751. +void VM_EnhancedRedefineClasses::ClearCpoolCacheAndUnpatch::do_klass(Klass* klass) {
  2752. + if (!klass->oop_is_instance()) {
  2753. + return;
  2754. + }
  2755. + HandleMark hm(_thread);
  2756. + InstanceKlass *ik = InstanceKlass::cast(klass);
  2757. + constantPoolHandle other_cp = constantPoolHandle(ik->constants());
  2758. +
  2759. + // Update host klass of anonymous classes (for example, produced by lambdas) to newest version.
  2760. + if (ik->is_anonymous() && !ik->host_klass()->is_newest_version()) {
  2761. + ik->set_host_klass(ik->host_klass()->newest_version());
  2762. + }
  2763. +
  2764. + for (int i = 0; i < other_cp->length(); i++) {
  2765. + if (other_cp->tag_at(i).is_klass()) {
  2766. + Klass* klass = other_cp->klass_at(i, _thread);
  2767. + if (klass->new_version() != NULL) {
  2768. + // (DCEVM) TODO: check why/if this is necessary
  2769. + other_cp->klass_at_put(i, klass->new_version());
  2770. + }
  2771. + klass = other_cp->klass_at(i, _thread);
  2772. + assert(klass->new_version() == NULL, "Must be new klass!");
  2773. + }
  2774. + }
  2775. +
  2776. + ConstantPoolCache* cp_cache = other_cp->cache();
  2777. + if (cp_cache != NULL) {
  2778. + cp_cache->clear_entries();
  2779. + }
  2780. +
  2781. + // If bytecode rewriting is enabled, we also need to unpatch bytecode to force resolution of zeroed entries
  2782. + if (RewriteBytecodes) {
  2783. + ik->methods_do(unpatch_bytecode);
  2784. + }
  2785. +}
  2786. +
  2787. +void VM_EnhancedRedefineClasses::update_jmethod_ids() {
  2788. + for (int j = 0; j < _matching_methods_length; ++j) {
  2789. + Method* old_method = _old_methods->at(_matching_old_methods[j]);
  2790. + jmethodID jmid = old_method->find_jmethod_id_or_null();
  2791. + RC_TRACE(0x00008000, ("matching method %s, jmid %d", old_method->name_and_sig_as_C_string(), jmid));
  2792. + if (old_method->new_version() != NULL && jmid == NULL) {
  2793. + // (DCEVM) Have to create jmethodID in this case
  2794. + jmid = old_method->jmethod_id();
  2795. + }
  2796. +
  2797. + if (jmid != NULL) {
  2798. + // There is a jmethodID, change it to point to the new method
  2799. + methodHandle new_method_h(_new_methods->at(_matching_new_methods[j]));
  2800. + if (old_method->new_version() == NULL) {
  2801. + methodHandle old_method_h(_old_methods->at(_matching_old_methods[j]));
  2802. + jmethodID new_jmethod_id = Method::make_jmethod_id(old_method_h->method_holder()->class_loader_data(), old_method_h());
  2803. + bool result = InstanceKlass::cast(old_method_h->method_holder())->update_jmethod_id(old_method_h(), new_jmethod_id);
  2804. + } else {
  2805. + jmethodID mid = new_method_h->jmethod_id();
  2806. + bool result = InstanceKlass::cast(new_method_h->method_holder())->update_jmethod_id(new_method_h(), jmid);
  2807. + }
  2808. + Method::change_method_associated_with_jmethod_id(jmid, new_method_h());
  2809. + assert(Method::resolve_jmethod_id(jmid) == _new_methods->at(_matching_new_methods[j]), "should be replaced");
  2810. + jmethodID mid = (_new_methods->at(_matching_new_methods[j]))->jmethod_id();
  2811. + //assert(JNIHandles::resolve_non_null((jobject)mid) == new_method_h(), "must match!");
  2812. + }
  2813. + }
  2814. +}
  2815. +
  2816. +
  2817. +// Deoptimize all compiled code that depends on this class.
  2818. +//
  2819. +// If the can_redefine_classes capability is obtained in the onload
  2820. +// phase then the compiler has recorded all dependencies from startup.
  2821. +// In that case we need only deoptimize and throw away all compiled code
  2822. +// that depends on the class.
  2823. +//
  2824. +// If can_redefine_classes is obtained sometime after the onload
  2825. +// phase then the dependency information may be incomplete. In that case
  2826. +// the first call to RedefineClasses causes all compiled code to be
  2827. +// thrown away. As can_redefine_classes has been obtained then
  2828. +// all future compilations will record dependencies so second and
  2829. +// subsequent calls to RedefineClasses need only throw away code
  2830. +// that depends on the class.
  2831. +//
  2832. +void VM_EnhancedRedefineClasses::flush_dependent_code(instanceKlassHandle k_h, TRAPS) {
  2833. + assert_locked_or_safepoint(Compile_lock);
  2834. +
  2835. + // All dependencies have been recorded from startup or this is a second or
  2836. + // subsequent use of RedefineClasses
  2837. +
  2838. + // For now deopt all
  2839. + // (tw) TODO: Improve the dependency system such that we can safely deopt only a subset of the methods
  2840. + if (0 && JvmtiExport::all_dependencies_are_recorded()) {
  2841. + Universe::flush_evol_dependents_on(k_h);
  2842. + } else {
  2843. + CodeCache::mark_all_nmethods_for_deoptimization();
  2844. +
  2845. + ResourceMark rm(THREAD);
  2846. + DeoptimizationMarker dm;
  2847. +
  2848. + // Deoptimize all activations depending on marked nmethods
  2849. + Deoptimization::deoptimize_dependents();
  2850. +
  2851. + // Make the dependent methods not entrant (in VM_Deoptimize they are made zombies)
  2852. + CodeCache::make_marked_nmethods_not_entrant();
  2853. +
  2854. + // From now on we know that the dependency information is complete
  2855. + JvmtiExport::set_all_dependencies_are_recorded(true);
  2856. + }
  2857. + }
  2858. +
  2859. +void VM_EnhancedRedefineClasses::compute_added_deleted_matching_methods() {
  2860. + Method* old_method;
  2861. + Method* new_method;
  2862. +
  2863. + _matching_old_methods = NEW_RESOURCE_ARRAY(int, _old_methods->length());
  2864. + _matching_new_methods = NEW_RESOURCE_ARRAY(int, _old_methods->length());
  2865. + _added_methods = NEW_RESOURCE_ARRAY(int, _new_methods->length());
  2866. + _deleted_methods = NEW_RESOURCE_ARRAY(int, _old_methods->length());
  2867. +
  2868. + _matching_methods_length = 0;
  2869. + _deleted_methods_length = 0;
  2870. + _added_methods_length = 0;
  2871. +
  2872. + int nj = 0;
  2873. + int oj = 0;
  2874. + while (true) {
  2875. + if (oj >= _old_methods->length()) {
  2876. + if (nj >= _new_methods->length()) {
  2877. + break; // we've looked at everything, done
  2878. + }
  2879. + // New method at the end
  2880. + new_method = _new_methods->at(nj);
  2881. + _added_methods[_added_methods_length++] = nj;
  2882. + ++nj;
  2883. + } else if (nj >= _new_methods->length()) {
  2884. + // Old method, at the end, is deleted
  2885. + old_method = _old_methods->at(oj);
  2886. + _deleted_methods[_deleted_methods_length++] = oj;
  2887. + ++oj;
  2888. + } else {
  2889. + old_method = _old_methods->at(oj);
  2890. + new_method = _new_methods->at(nj);
  2891. + if (old_method->name() == new_method->name()) {
  2892. + if (old_method->signature() == new_method->signature()) {
  2893. + _matching_old_methods[_matching_methods_length] = oj;//old_method;
  2894. + _matching_new_methods[_matching_methods_length] = nj;//new_method;
  2895. + _matching_methods_length++;
  2896. + ++nj;
  2897. + ++oj;
  2898. + } else {
  2899. + // added overloaded have already been moved to the end,
  2900. + // so this is a deleted overloaded method
  2901. + _deleted_methods[_deleted_methods_length++] = oj;//old_method;
  2902. + ++oj;
  2903. + }
  2904. + } else { // names don't match
  2905. + if (old_method->name()->fast_compare(new_method->name()) > 0) {
  2906. + // new method
  2907. + _added_methods[_added_methods_length++] = nj;//new_method;
  2908. + ++nj;
  2909. + } else {
  2910. + // deleted method
  2911. + _deleted_methods[_deleted_methods_length++] = oj;//old_method;
  2912. + ++oj;
  2913. + }
  2914. + }
  2915. + }
  2916. + }
  2917. + assert(_matching_methods_length + _deleted_methods_length == _old_methods->length(), "sanity");
  2918. + assert(_matching_methods_length + _added_methods_length == _new_methods->length(), "sanity");
  2919. + RC_TRACE(0x00008000, ("Matching methods = %d / deleted methods = %d / added methods = %d",
  2920. + _matching_methods_length, _deleted_methods_length, _added_methods_length));
  2921. +}
  2922. +
  2923. +
  2924. +
  2925. +// Install the redefinition of a class:
  2926. +// - house keeping (flushing breakpoints and caches, deoptimizing
  2927. +// dependent compiled code)
  2928. +// - adjusting constant pool caches and vtables in other classes
  2929. +void VM_EnhancedRedefineClasses::redefine_single_class(instanceKlassHandle the_new_class, TRAPS) {
  2930. +
  2931. + ResourceMark rm(THREAD);
  2932. +
  2933. + assert(the_new_class->old_version() != NULL, "Must not be null");
  2934. + assert(the_new_class->old_version()->new_version() == the_new_class(), "Must equal");
  2935. +
  2936. + instanceKlassHandle the_old_class = instanceKlassHandle(THREAD, the_new_class->old_version());
  2937. +
  2938. +#ifndef JVMTI_KERNEL
  2939. + // Remove all breakpoints in methods of this class
  2940. + JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
  2941. + jvmti_breakpoints.clearall_in_class_at_safepoint(the_old_class());
  2942. +#endif // !JVMTI_KERNEL
  2943. +
  2944. + /* FIXME
  2945. + if (the_old_class() == Universe::reflect_invoke_cache()->klass()) {
  2946. + // We are redefining java.lang.reflect.Method. Method.invoke() is
  2947. + // cached and users of the cache care about each active version of
  2948. + // the method so we have to track this previous version.
  2949. + // Do this before methods get switched
  2950. + Universe::reflect_invoke_cache()->add_previous_version(
  2951. + the_old_class->method_with_idnum(Universe::reflect_invoke_cache()->method_idnum()));
  2952. + }*/
  2953. +
  2954. + _old_methods = the_old_class->methods();
  2955. + _new_methods = the_new_class->methods();
  2956. + compute_added_deleted_matching_methods();
  2957. +
  2958. + // track which methods are EMCP for add_previous_version() call below
  2959. +
  2960. + // TODO: Check if we need the concept of EMCP?
  2961. + BitMap emcp_methods(_old_methods->length());
  2962. + int emcp_method_count = 0;
  2963. + emcp_methods.clear(); // clears 0..(length() - 1)
  2964. +
  2965. + // We need to mark methods as old!!
  2966. + check_methods_and_mark_as_obsolete(&emcp_methods, &emcp_method_count);
  2967. + update_jmethod_ids();
  2968. +
  2969. + // TODO:
  2970. + transfer_old_native_function_registrations(the_old_class);
  2971. +
  2972. +
  2973. + // JSR-292 support
  2974. +
  2975. + // Swap method handles
  2976. + MemberNameTable* mnt = the_old_class->member_names();
  2977. + assert(the_new_class->member_names() == NULL, "");
  2978. + the_new_class->set_member_names(mnt);
  2979. + the_old_class->set_member_names(NULL);
  2980. +
  2981. + // FIXME: should we add field MemberName's in this list and process all of them here?
  2982. +// if (mnt != NULL) {
  2983. +// for (int i = 0; i < mnt->length(); i++) {
  2984. +// oop mem_name = mnt->get_member_name(i);
  2985. +// if (mem_name != NULL) {
  2986. +// Method* method = (Method*) java_lang_invoke_MemberName::vmtarget(mem_name);
  2987. +//
  2988. +// // Replace the method with matching one from the new class
  2989. +// Method* new_method = the_new_class->find_method(method->name(), method->signature());
  2990. +// java_lang_invoke_MemberName::set_vmtarget(mem_name, new_method);
  2991. +// }
  2992. +// }
  2993. +// }
  2994. +
  2995. +#ifdef ASSERT
  2996. +
  2997. +// Klass* systemLookup1 = SystemDictionary::resolve_or_null(the_old_class->name(), the_old_class->class_loader(), the_old_class->protection_domain(), THREAD);
  2998. +// assert(systemLookup1 == the_new_class(), "New class must be in system dictionary!");
  2999. +
  3000. + //JNIHandles::verify();
  3001. +
  3002. +// Klass* systemLookup = SystemDictionary::resolve_or_null(the_old_class->name(), the_old_class->class_loader(), the_old_class->protection_domain(), THREAD);
  3003. +
  3004. +// assert(systemLookup == the_new_class(), "New class must be in system dictionary!");
  3005. + assert(the_new_class->old_version() != NULL, "Must not be null");
  3006. + assert(the_new_class->old_version()->new_version() == the_new_class(), "Must equal");
  3007. +
  3008. + for (int i=0; i<the_new_class->methods()->length(); i++) {
  3009. + assert((the_new_class->methods()->at(i))->method_holder() == the_new_class(), "method holder must match!");
  3010. + }
  3011. +
  3012. + // FIXME:
  3013. + //_old_methods->verify();
  3014. + //_new_methods->verify();
  3015. +
  3016. + the_new_class->vtable()->verify(tty);
  3017. + the_old_class->vtable()->verify(tty);
  3018. +
  3019. +#endif
  3020. +
  3021. + // increment the classRedefinedCount field in the_class and in any
  3022. + // direct and indirect subclasses of the_class
  3023. + increment_class_counter((InstanceKlass *)the_old_class(), THREAD);
  3024. +
  3025. +}
  3026. +
  3027. +
  3028. +void VM_EnhancedRedefineClasses::check_methods_and_mark_as_obsolete(BitMap *emcp_methods, int * emcp_method_count_p) {
  3029. + RC_TRACE(0x00000100, ("Checking matching methods for EMCP"));
  3030. + *emcp_method_count_p = 0;
  3031. + int obsolete_count = 0;
  3032. + int old_index = 0;
  3033. + for (int j = 0; j < _matching_methods_length; ++j, ++old_index) {
  3034. + Method* old_method = _old_methods->at(_matching_old_methods[j]);
  3035. + Method* new_method = _new_methods->at(_matching_new_methods[j]);
  3036. + Method* old_array_method;
  3037. +
  3038. + // Maintain an old_index into the _old_methods array by skipping
  3039. + // deleted methods
  3040. + while ((old_array_method = _old_methods->at(old_index)) != old_method) {
  3041. + ++old_index;
  3042. + }
  3043. +
  3044. + if (MethodComparator::methods_EMCP(old_method, new_method)) {
  3045. + // The EMCP definition from JSR-163 requires the bytecodes to be
  3046. + // the same with the exception of constant pool indices which may
  3047. + // differ. However, the constants referred to by those indices
  3048. + // must be the same.
  3049. + //
  3050. + // We use methods_EMCP() for comparison since constant pool
  3051. + // merging can remove duplicate constant pool entries that were
  3052. + // present in the old method and removed from the rewritten new
  3053. + // method. A faster binary comparison function would consider the
  3054. + // old and new methods to be different when they are actually
  3055. + // EMCP.
  3056. +
  3057. + // track which methods are EMCP for add_previous_version() call
  3058. + emcp_methods->set_bit(old_index);
  3059. + (*emcp_method_count_p)++;
  3060. +
  3061. + // An EMCP method is _not_ obsolete. An obsolete method has a
  3062. + // different jmethodID than the current method. An EMCP method
  3063. + // has the same jmethodID as the current method. Having the
  3064. + // same jmethodID for all EMCP versions of a method allows for
  3065. + // a consistent view of the EMCP methods regardless of which
  3066. + // EMCP method you happen to have in hand. For example, a
  3067. + // breakpoint set in one EMCP method will work for all EMCP
  3068. + // versions of the method including the current one.
  3069. +
  3070. + old_method->set_new_version(new_method);
  3071. + new_method->set_old_version(old_method);
  3072. +
  3073. + RC_TRACE(0x00000100, ("Found EMCP method %s", old_method->name_and_sig_as_C_string()));
  3074. +
  3075. + // Transfer breakpoints
  3076. + InstanceKlass *ik = InstanceKlass::cast(old_method->method_holder());
  3077. + for (BreakpointInfo* bp = ik->breakpoints(); bp != NULL; bp = bp->next()) {
  3078. + RC_TRACE(0x00000100, ("Checking breakpoint: %d / %d",
  3079. + bp->match(old_method), bp->match(new_method)));
  3080. + if (bp->match(old_method)) {
  3081. + assert(bp->match(new_method), "if old method is method, then new method must match too");
  3082. + RC_TRACE(0x00000100, ("Found a breakpoint in an old EMCP method"));
  3083. + new_method->set_breakpoint(bp->bci());
  3084. + }
  3085. + }
  3086. + } else {
  3087. + // mark obsolete methods as such
  3088. + old_method->set_is_obsolete();
  3089. + obsolete_count++;
  3090. +
  3091. + // With tracing we try not to "yack" too much. The position of
  3092. + // this trace assumes there are fewer obsolete methods than
  3093. + // EMCP methods.
  3094. + RC_TRACE(0x00000100, ("mark %s(%s) as obsolete",
  3095. + old_method->name()->as_C_string(),
  3096. + old_method->signature()->as_C_string()));
  3097. + }
  3098. + old_method->set_is_old();
  3099. + }
  3100. + for (int i = 0; i < _deleted_methods_length; ++i) {
  3101. + Method* old_method = _old_methods->at(_deleted_methods[i]);
  3102. +
  3103. + //assert(old_method->vtable_index() < 0,
  3104. + // "cannot delete methods with vtable entries");;
  3105. +
  3106. + // Mark all deleted methods as old and obsolete
  3107. + old_method->set_is_old();
  3108. + old_method->set_is_obsolete();
  3109. + ++obsolete_count;
  3110. + // With tracing we try not to "yack" too much. The position of
  3111. + // this trace assumes there are fewer obsolete methods than
  3112. + // EMCP methods.
  3113. + RC_TRACE(0x00000100, ("mark deleted %s(%s) as obsolete",
  3114. + old_method->name()->as_C_string(),
  3115. + old_method->signature()->as_C_string()));
  3116. + }
  3117. + //assert((*emcp_method_count_p + obsolete_count) == _old_methods->length(), "sanity check");
  3118. + RC_TRACE(0x00000100, ("EMCP_cnt=%d, obsolete_cnt=%d !",
  3119. + *emcp_method_count_p, obsolete_count));
  3120. +}
  3121. +
  3122. +// Increment the classRedefinedCount field in the specific InstanceKlass
  3123. +// and in all direct and indirect subclasses.
  3124. +void VM_EnhancedRedefineClasses::increment_class_counter(Klass* klass, TRAPS) {
  3125. + oop class_mirror = klass->java_mirror();
  3126. + int new_count = java_lang_Class::classRedefinedCount(class_mirror) + 1;
  3127. + java_lang_Class::set_classRedefinedCount(class_mirror, new_count);
  3128. + RC_TRACE(0x00000008, ("updated count for class=%s to %d", klass->external_name(), new_count));
  3129. +}
  3130. +
  3131. +#ifndef PRODUCT
  3132. +void VM_EnhancedRedefineClasses::check_class(Klass* k_oop, TRAPS) {
  3133. + Klass *k = k_oop;
  3134. + if (k->oop_is_instance()) {
  3135. + HandleMark hm(THREAD);
  3136. + InstanceKlass *ik = (InstanceKlass *) k;
  3137. + assert(ik->is_newest_version(), "must be latest version in system dictionary");
  3138. +
  3139. + if (ik->vtable_length() > 0) {
  3140. + ResourceMark rm(THREAD);
  3141. + assert(ik->vtable()->check_no_old_or_obsolete_entries(), "old method found");
  3142. + ik->vtable()->verify(tty, true);
  3143. + }
  3144. + }
  3145. +}
  3146. +
  3147. +#endif
  3148. +
  3149. +static bool match_second(void* value, Pair<Klass*, Klass*> elem) {
  3150. + return elem.second == value;
  3151. +}
  3152. +
  3153. +jvmtiError VM_EnhancedRedefineClasses::do_topological_class_sorting( const jvmtiClassDefinition *class_defs, int class_count, TRAPS) {
  3154. + ResourceMark mark(THREAD);
  3155. + GrowableArray<Pair<Klass*, Klass*> > links;
  3156. +
  3157. + for (int i=0; i<class_count; i++) {
  3158. +
  3159. + oop mirror = JNIHandles::resolve_non_null(class_defs[i].klass);
  3160. + instanceKlassHandle the_class(THREAD, java_lang_Class::as_Klass(mirror));
  3161. + Handle the_class_loader(THREAD, the_class->class_loader());
  3162. + Handle protection_domain(THREAD, the_class->protection_domain());
  3163. +
  3164. + ClassFileStream st((u1*) class_defs[i].class_bytes,
  3165. + class_defs[i].class_byte_count, (char *)"__VM_EnhancedRedefineClasses__");
  3166. + ClassFileParser cfp(&st);
  3167. +
  3168. +
  3169. +
  3170. + TempNewSymbol parsed_name;
  3171. + GrowableArray<Symbol*>* super_symbols = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<Symbol*>(0, true);
  3172. + cfp.parseClassFile(the_class->name(),
  3173. + the_class->class_loader_data(),
  3174. + protection_domain,
  3175. + the_class, KlassHandle(),
  3176. + NULL,
  3177. + super_symbols,
  3178. + parsed_name,
  3179. + false,
  3180. + THREAD);
  3181. +
  3182. + for (int j = 0; j < super_symbols->length(); j++) {
  3183. + Symbol* sym = super_symbols->at(j);
  3184. + Klass* super_klass = SystemDictionary::resolve_or_null(sym, the_class_loader, protection_domain, THREAD);
  3185. + if (super_klass != NULL) {
  3186. + instanceKlassHandle the_super_class(THREAD, super_klass);
  3187. + if (_affected_klasses->contains(the_super_class)) {
  3188. + links.append(Pair<Klass*, Klass*>(super_klass, the_class()));
  3189. + }
  3190. + }
  3191. + }
  3192. + delete super_symbols;
  3193. +
  3194. + assert(the_class->check_redefinition_flag(Klass::MarkedAsAffected), "");
  3195. + the_class->clear_redefinition_flag(Klass::MarkedAsAffected);
  3196. + }
  3197. +
  3198. + for (int i=0; i < _affected_klasses->length(); i++) {
  3199. + instanceKlassHandle klass = _affected_klasses->at(i);
  3200. +
  3201. + if (klass->check_redefinition_flag(Klass::MarkedAsAffected)) {
  3202. + klass->clear_redefinition_flag(Klass::MarkedAsAffected);
  3203. + Klass* superKlass = klass->super();
  3204. + if (_affected_klasses->contains(superKlass)) {
  3205. + links.append(Pair<Klass*, Klass*>(superKlass, klass()));
  3206. + }
  3207. +
  3208. + Array<Klass*>* superInterfaces = klass->local_interfaces();
  3209. + for (int j=0; j<superInterfaces->length(); j++) {
  3210. + Klass* interfaceKlass = superInterfaces->at(j);
  3211. + if (_affected_klasses->contains(interfaceKlass)) {
  3212. + links.append(Pair<Klass*, Klass*>(interfaceKlass, klass()));
  3213. + }
  3214. + }
  3215. + }
  3216. + }
  3217. +
  3218. + for (int i = 0; i < _affected_klasses->length(); i++) {
  3219. + int j;
  3220. + for (j = i; j < _affected_klasses->length(); j++) {
  3221. + // Search for node with no incoming edges
  3222. + Klass* oop = _affected_klasses->at(j)();
  3223. + int k = links.find(oop, match_second);
  3224. + if (k == -1) break;
  3225. + }
  3226. + if (j == _affected_klasses->length()) {
  3227. + return JVMTI_ERROR_CIRCULAR_CLASS_DEFINITION;
  3228. + }
  3229. +
  3230. + // Remove all links from this node
  3231. + Klass* oop = _affected_klasses->at(j)();
  3232. + int k = 0;
  3233. + while (k < links.length()) {
  3234. + if (links.adr_at(k)->first == oop) {
  3235. + links.delete_at(k);
  3236. + } else {
  3237. + k++;
  3238. + }
  3239. + }
  3240. +
  3241. + // Swap node
  3242. + instanceKlassHandle tmp = _affected_klasses->at(j);
  3243. + _affected_klasses->at_put(j, _affected_klasses->at(i));
  3244. + _affected_klasses->at_put(i, tmp);
  3245. + }
  3246. +
  3247. + return JVMTI_ERROR_NONE;
  3248. +}
  3249. +
  3250. +// This internal class transfers the native function registration from old methods
  3251. +// to new methods. It is designed to handle both the simple case of unchanged
  3252. +// native methods and the complex cases of native method prefixes being added and/or
  3253. +// removed.
  3254. +// It expects only to be used during the VM_EnhancedRedefineClasses op (a safepoint).
  3255. +//
  3256. +// This class is used after the new methods have been installed in "the_class".
  3257. +//
  3258. +// So, for example, the following must be handled. Where 'm' is a method and
  3259. +// a number followed by an underscore is a prefix.
  3260. +//
  3261. +// Old Name New Name
  3262. +// Simple transfer to new method m -> m
  3263. +// Add prefix m -> 1_m
  3264. +// Remove prefix 1_m -> m
  3265. +// Simultaneous add of prefixes m -> 3_2_1_m
  3266. +// Simultaneous removal of prefixes 3_2_1_m -> m
  3267. +// Simultaneous add and remove 1_m -> 2_m
  3268. +// Same, caused by prefix removal only 3_2_1_m -> 3_2_m
  3269. +//
  3270. +class TransferNativeFunctionRegistration {
  3271. + private:
  3272. + instanceKlassHandle the_class;
  3273. + int prefix_count;
  3274. + char** prefixes;
  3275. +
  3276. + // Recursively search the binary tree of possibly prefixed method names.
  3277. + // Iteration could be used if all agents were well behaved. Full tree walk is
  3278. + // more resilent to agents not cleaning up intermediate methods.
  3279. + // Branch at each depth in the binary tree is:
  3280. + // (1) without the prefix.
  3281. + // (2) with the prefix.
  3282. + // where 'prefix' is the prefix at that 'depth' (first prefix, second prefix,...)
  3283. + Method* search_prefix_name_space(int depth, char* name_str, size_t name_len,
  3284. + Symbol* signature) {
  3285. + Symbol* name_symbol = SymbolTable::probe(name_str, (int)name_len);
  3286. + if (name_symbol != NULL) {
  3287. + Method* method = the_class()->new_version()->lookup_method(name_symbol, signature);
  3288. + if (method != NULL) {
  3289. + // Even if prefixed, intermediate methods must exist.
  3290. + if (method->is_native()) {
  3291. + // Wahoo, we found a (possibly prefixed) version of the method, return it.
  3292. + return method;
  3293. + }
  3294. + if (depth < prefix_count) {
  3295. + // Try applying further prefixes (other than this one).
  3296. + method = search_prefix_name_space(depth+1, name_str, name_len, signature);
  3297. + if (method != NULL) {
  3298. + return method; // found
  3299. + }
  3300. +
  3301. + // Try adding this prefix to the method name and see if it matches
  3302. + // another method name.
  3303. + char* prefix = prefixes[depth];
  3304. + size_t prefix_len = strlen(prefix);
  3305. + size_t trial_len = name_len + prefix_len;
  3306. + char* trial_name_str = NEW_RESOURCE_ARRAY(char, trial_len + 1);
  3307. + strcpy(trial_name_str, prefix);
  3308. + strcat(trial_name_str, name_str);
  3309. + method = search_prefix_name_space(depth+1, trial_name_str, trial_len,
  3310. + signature);
  3311. + if (method != NULL) {
  3312. + // If found along this branch, it was prefixed, mark as such
  3313. + method->set_is_prefixed_native();
  3314. + return method; // found
  3315. + }
  3316. + }
  3317. + }
  3318. + }
  3319. + return NULL; // This whole branch bore nothing
  3320. + }
  3321. +
  3322. + // Return the method name with old prefixes stripped away.
  3323. + char* method_name_without_prefixes(Method* method) {
  3324. + Symbol* name = method->name();
  3325. + char* name_str = name->as_utf8();
  3326. +
  3327. + // Old prefixing may be defunct, strip prefixes, if any.
  3328. + for (int i = prefix_count-1; i >= 0; i--) {
  3329. + char* prefix = prefixes[i];
  3330. + size_t prefix_len = strlen(prefix);
  3331. + if (strncmp(prefix, name_str, prefix_len) == 0) {
  3332. + name_str += prefix_len;
  3333. + }
  3334. + }
  3335. + return name_str;
  3336. + }
  3337. +
  3338. + // Strip any prefixes off the old native method, then try to find a
  3339. + // (possibly prefixed) new native that matches it.
  3340. + Method* strip_and_search_for_new_native(Method* method) {
  3341. + ResourceMark rm;
  3342. + char* name_str = method_name_without_prefixes(method);
  3343. + return search_prefix_name_space(0, name_str, strlen(name_str),
  3344. + method->signature());
  3345. + }
  3346. +
  3347. + public:
  3348. +
  3349. + // Construct a native method transfer processor for this class.
  3350. + TransferNativeFunctionRegistration(instanceKlassHandle _the_class) {
  3351. + assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
  3352. +
  3353. + the_class = _the_class;
  3354. + prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count);
  3355. + }
  3356. +
  3357. + // Attempt to transfer any of the old or deleted methods that are native
  3358. + void transfer_registrations(instanceKlassHandle old_klass, int* old_methods, int methods_length) {
  3359. + for (int j = 0; j < methods_length; j++) {
  3360. + Method* old_method = old_klass->methods()->at(old_methods[j]);
  3361. +
  3362. + if (old_method->is_native() && old_method->has_native_function()) {
  3363. + Method* new_method = strip_and_search_for_new_native(old_method);
  3364. + if (new_method != NULL) {
  3365. + // Actually set the native function in the new method.
  3366. + // Redefine does not send events (except CFLH), certainly not this
  3367. + // behind the scenes re-registration.
  3368. + new_method->set_native_function(old_method->native_function(),
  3369. + !Method::native_bind_event_is_interesting);
  3370. + }
  3371. + }
  3372. + }
  3373. + }
  3374. +};
  3375. +
  3376. +// Don't lose the association between a native method and its JNI function.
  3377. +void VM_EnhancedRedefineClasses::transfer_old_native_function_registrations(instanceKlassHandle old_klass) {
  3378. + TransferNativeFunctionRegistration transfer(old_klass);
  3379. + transfer.transfer_registrations(old_klass, _deleted_methods, _deleted_methods_length);
  3380. + transfer.transfer_registrations(old_klass, _matching_old_methods, _matching_methods_length);
  3381. +}
  3382. diff --git a/src/share/vm/prims/jvmtiRedefineClasses2.hpp b/src/share/vm/prims/jvmtiRedefineClasses2.hpp
  3383. new file mode 100644
  3384. --- /dev/null
  3385. +++ b/src/share/vm/prims/jvmtiRedefineClasses2.hpp
  3386. @@ -0,0 +1,161 @@
  3387. +/*
  3388. + * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
  3389. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  3390. + *
  3391. + * This code is free software; you can redistribute it and/or modify it
  3392. + * under the terms of the GNU General Public License version 2 only, as
  3393. + * published by the Free Software Foundation.
  3394. + *
  3395. + * This code is distributed in the hope that it will be useful, but WITHOUT
  3396. + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  3397. + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  3398. + * version 2 for more details (a copy is included in the LICENSE file that
  3399. + * accompanied this code).
  3400. + *
  3401. + * You should have received a copy of the GNU General Public License version
  3402. + * 2 along with this work; if not, write to the Free Software Foundation,
  3403. + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  3404. + *
  3405. + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  3406. + * or visit www.oracle.com if you need additional information or have any
  3407. + * questions.
  3408. + *
  3409. + */
  3410. +
  3411. +#ifndef SHARE_VM_PRIMS_JVMTIENHANCEDREDEFINECLASSES_HPP
  3412. +#define SHARE_VM_PRIMS_JVMTIENHANCEDREDEFINECLASSES_HPP
  3413. +
  3414. +#include "jvmtifiles/jvmtiEnv.hpp"
  3415. +#include "memory/oopFactory.hpp"
  3416. +#include "memory/resourceArea.hpp"
  3417. +#include "oops/objArrayKlass.hpp"
  3418. +#include "oops/objArrayOop.hpp"
  3419. +#include "oops/fieldStreams.hpp"
  3420. +#include "prims/jvmtiRedefineClassesTrace.hpp"
  3421. +#include "gc_implementation/shared/vmGCOperations.hpp"
  3422. +
  3423. +// New version that allows arbitrary changes to already loaded classes.
  3424. +class VM_EnhancedRedefineClasses: public VM_GC_Operation {
  3425. + private:
  3426. +
  3427. + // These static fields are needed by SystemDictionary::classes_do()
  3428. + // facility and the adjust_cpool_cache_and_vtable() helper:
  3429. + static Array<Method*>* _old_methods;
  3430. + static Array<Method*>* _new_methods;
  3431. + static int* _matching_old_methods;
  3432. + static int* _matching_new_methods;
  3433. + static int* _deleted_methods;
  3434. + static int* _added_methods;
  3435. + static int _matching_methods_length;
  3436. + static int _deleted_methods_length;
  3437. + static int _added_methods_length;
  3438. +
  3439. + static int _revision_number;
  3440. +
  3441. + static GrowableArray<instanceKlassHandle>* _affected_klasses;
  3442. +
  3443. + // The instance fields are used to pass information from
  3444. + // doit_prologue() to doit() and doit_epilogue().
  3445. + jint _class_count;
  3446. + const jvmtiClassDefinition *_class_defs; // ptr to _class_count defs
  3447. +
  3448. + // This operation is used by both RedefineClasses and
  3449. + // RetransformClasses. Indicate which.
  3450. + JvmtiClassLoadKind _class_load_kind;
  3451. +
  3452. + GrowableArray<instanceKlassHandle>* _new_classes;
  3453. + jvmtiError _result;
  3454. + int _max_redefinition_flags;
  3455. +
  3456. + // Performance measurement support. These timers do not cover all
  3457. + // the work done for JVM/TI RedefineClasses() but they do cover
  3458. + // the heavy lifting.
  3459. + elapsedTimer _timer_total;
  3460. + elapsedTimer _timer_prologue;
  3461. + elapsedTimer _timer_class_linking;
  3462. + elapsedTimer _timer_class_loading;
  3463. + elapsedTimer _timer_prepare_redefinition;
  3464. + elapsedTimer _timer_heap_iteration;
  3465. + elapsedTimer _timer_redefinition;
  3466. + elapsedTimer _timer_vm_op_epilogue;
  3467. +
  3468. + jvmtiError find_sorted_affected_classes( );
  3469. + jvmtiError find_class_bytes(instanceKlassHandle the_class, const unsigned char **class_bytes, jint *class_byte_count, jboolean *not_changed);
  3470. + jvmtiError load_new_class_versions(TRAPS);
  3471. +
  3472. + // Figure out which new methods match old methods in name and signature,
  3473. + // which methods have been added, and which are no longer present
  3474. + void compute_added_deleted_matching_methods();
  3475. +
  3476. + // Change jmethodIDs to point to the new methods
  3477. + void update_jmethod_ids();
  3478. +
  3479. + void swap_all_method_annotations(ConstMethod* old_method, ConstMethod* new_method);
  3480. +
  3481. + static void add_affected_klasses( Klass* obj );
  3482. +
  3483. + static jvmtiError do_topological_class_sorting(const jvmtiClassDefinition *class_definitions, int class_count, TRAPS);
  3484. +
  3485. + // Install the redefinition of a class
  3486. + void redefine_single_class(instanceKlassHandle the_new_class, TRAPS);
  3487. +
  3488. + // Increment the classRedefinedCount field in the specific instanceKlass
  3489. + // and in all direct and indirect subclasses.
  3490. + void increment_class_counter(Klass* klass, TRAPS);
  3491. +
  3492. +
  3493. + void flush_dependent_code(instanceKlassHandle k_h, TRAPS);
  3494. +
  3495. + static void check_class(Klass* k_oop,/* oop initiating_loader,*/ TRAPS) PRODUCT_RETURN;
  3496. +
  3497. + static void unpatch_bytecode(Method* method);
  3498. +
  3499. +#ifdef ASSERT
  3500. + static void verify_classes(Klass* k_oop, oop initiating_loader, TRAPS);
  3501. +#endif
  3502. +
  3503. + int calculate_redefinition_flags(instanceKlassHandle new_version);
  3504. + void calculate_instance_update_information(Klass* new_version);
  3505. + void check_methods_and_mark_as_obsolete(BitMap *emcp_methods, int * emcp_method_count_p);
  3506. + static void mark_as_scavengable(nmethod* nm);
  3507. +
  3508. + bool check_arguments();
  3509. + jvmtiError check_arguments_error();
  3510. +
  3511. + class ClearCpoolCacheAndUnpatch : public KlassClosure {
  3512. + Thread* _thread;
  3513. + public:
  3514. + ClearCpoolCacheAndUnpatch(Thread* t) : _thread(t) {}
  3515. + void do_klass(Klass* k);
  3516. + };
  3517. +
  3518. + public:
  3519. + VM_EnhancedRedefineClasses(jint class_count, const jvmtiClassDefinition *class_defs, JvmtiClassLoadKind class_load_kind);
  3520. + virtual ~VM_EnhancedRedefineClasses();
  3521. +
  3522. + bool doit_prologue();
  3523. + void doit();
  3524. + void doit_epilogue();
  3525. + void rollback();
  3526. +
  3527. + jvmtiError check_exception() const;
  3528. + VMOp_Type type() const { return VMOp_RedefineClasses; }
  3529. + bool skip_operation() const { return false; }
  3530. + bool allow_nested_vm_operations() const { return true; }
  3531. + jvmtiError check_error() { return _result; }
  3532. +
  3533. + // Modifiable test must be shared between IsModifiableClass query
  3534. + // and redefine implementation
  3535. + static bool is_modifiable_class(oop klass_mirror);
  3536. +
  3537. + // Utility methods for transferring field access flags
  3538. +
  3539. + static void transfer_special_access_flags(JavaFieldStream *from, JavaFieldStream *to);
  3540. + static void transfer_special_access_flags(fieldDescriptor *from, fieldDescriptor *to);
  3541. +
  3542. + void transfer_old_native_function_registrations(instanceKlassHandle the_class);
  3543. +
  3544. + static void swap_marks(oop first, oop second);
  3545. +};
  3546. +
  3547. +#endif // SHARE_VM_PRIMS_JVMTIENHANCEDREDEFINECLASSES_HPP
  3548. diff --git a/src/share/vm/runtime/reflection.cpp b/src/share/vm/runtime/reflection.cpp
  3549. --- a/src/share/vm/runtime/reflection.cpp
  3550. +++ b/src/share/vm/runtime/reflection.cpp
  3551. @@ -519,6 +519,12 @@
  3552. AccessFlags access,
  3553. bool classloader_only,
  3554. bool protected_restriction) {
  3555. +
  3556. + // (DCEVM) Decide accessibility based on active version
  3557. + if (current_class != NULL) {
  3558. + current_class = current_class->active_version();
  3559. + }
  3560. +
  3561. // Verify that current_class can access a field of field_class, where that
  3562. // field's access bits are "access". We assume that we've already verified
  3563. // that current_class can access field_class.