You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

light-jdk8u20-b22.patch 149KB

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