Skip to content

Commit f88f31d

Browse files
author
Alex Menkov
committedApr 3, 2024
8328137: PreserveAllAnnotations can cause failure of class retransformation
Reviewed-by: coleenp, sspitsyn
1 parent 021ed6a commit f88f31d

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed
 

‎src/hotspot/share/prims/jvmtiClassFileReconstituter.cpp

+23-9
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ void JvmtiClassFileReconstituter::write_field_infos() {
119119
write_signature_attribute(generic_signature_index);
120120
}
121121
if (anno != nullptr) {
122-
write_annotations_attribute("RuntimeVisibleAnnotations", anno);
122+
write_annotations_attribute("RuntimeVisibleAnnotations", "RuntimeInvisibleAnnotations", anno);
123123
}
124124
if (type_anno != nullptr) {
125-
write_annotations_attribute("RuntimeVisibleTypeAnnotations", type_anno);
125+
write_annotations_attribute("RuntimeVisibleTypeAnnotations", "RuntimeInvisibleTypeAnnotations", type_anno);
126126
}
127127
}
128128
}
@@ -380,6 +380,20 @@ void JvmtiClassFileReconstituter::write_annotations_attribute(const char* attr_n
380380
memcpy(writeable_address(length), annos->adr_at(0), length);
381381
}
382382

383+
void JvmtiClassFileReconstituter::write_annotations_attribute(const char* attr_name,
384+
const char* fallback_attr_name,
385+
AnnotationArray* annos) {
386+
TempNewSymbol sym = SymbolTable::probe(attr_name, (int)strlen(attr_name));
387+
if (sym != nullptr) {
388+
if (symbol_to_cpool_index(sym) != 0) {
389+
write_annotations_attribute(attr_name, annos);
390+
return;
391+
}
392+
}
393+
// use fallback name
394+
write_annotations_attribute(fallback_attr_name, annos);
395+
}
396+
383397
// BootstrapMethods_attribute {
384398
// u2 attribute_name_index;
385399
// u4 attribute_length;
@@ -519,10 +533,10 @@ void JvmtiClassFileReconstituter::write_record_attribute() {
519533
write_signature_attribute(component->generic_signature_index());
520534
}
521535
if (component->annotations() != nullptr) {
522-
write_annotations_attribute("RuntimeVisibleAnnotations", component->annotations());
536+
write_annotations_attribute("RuntimeVisibleAnnotations", "RuntimeInvisibleAnnotations", component->annotations());
523537
}
524538
if (component->type_annotations() != nullptr) {
525-
write_annotations_attribute("RuntimeVisibleTypeAnnotations", component->type_annotations());
539+
write_annotations_attribute("RuntimeVisibleTypeAnnotations", "RuntimeInvisibleTypeAnnotations", component->type_annotations());
526540
}
527541
}
528542
}
@@ -761,13 +775,13 @@ void JvmtiClassFileReconstituter::write_method_info(const methodHandle& method)
761775
write_signature_attribute(generic_signature_index);
762776
}
763777
if (anno != nullptr) {
764-
write_annotations_attribute("RuntimeVisibleAnnotations", anno);
778+
write_annotations_attribute("RuntimeVisibleAnnotations", "RuntimeInvisibleAnnotations", anno);
765779
}
766780
if (param_anno != nullptr) {
767-
write_annotations_attribute("RuntimeVisibleParameterAnnotations", param_anno);
781+
write_annotations_attribute("RuntimeVisibleParameterAnnotations", "RuntimeInvisibleParameterAnnotations", param_anno);
768782
}
769783
if (type_anno != nullptr) {
770-
write_annotations_attribute("RuntimeVisibleTypeAnnotations", type_anno);
784+
write_annotations_attribute("RuntimeVisibleTypeAnnotations", "RuntimeInvisibleTypeAnnotations", type_anno);
771785
}
772786
}
773787

@@ -827,10 +841,10 @@ void JvmtiClassFileReconstituter::write_class_attributes() {
827841
write_source_debug_extension_attribute();
828842
}
829843
if (anno != nullptr) {
830-
write_annotations_attribute("RuntimeVisibleAnnotations", anno);
844+
write_annotations_attribute("RuntimeVisibleAnnotations", "RuntimeInvisibleAnnotations", anno);
831845
}
832846
if (type_anno != nullptr) {
833-
write_annotations_attribute("RuntimeVisibleTypeAnnotations", type_anno);
847+
write_annotations_attribute("RuntimeVisibleTypeAnnotations", "RuntimeInvisibleTypeAnnotations", type_anno);
834848
}
835849
if (ik()->nest_host_index() != 0) {
836850
write_nest_host_attribute();

‎src/hotspot/share/prims/jvmtiClassFileReconstituter.hpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -117,6 +117,13 @@ class JvmtiClassFileReconstituter : public JvmtiConstantPoolReconstituter {
117117
void write_signature_attribute(u2 generic_signaure_index);
118118
void write_attribute_name_index(const char* name);
119119
void write_annotations_attribute(const char* attr_name, AnnotationArray* annos);
120+
// With PreserveAllAnnotations option "runtime invisible" annotations
121+
// (RuntimeInvisibleAnnotations/RuntimeInvisibleTypeAnnotations/RuntimeInvisibleParameterAnnotations)
122+
// are considered "runtime visible" and ClassFileReconstituter writes them as
123+
// RuntimeVisibleAnnotations/RuntimeVisibleTypeAnnotations/RuntimeVisibleParameterAnnotations.
124+
// This helper method is for the corner case when "runtime visible" attribute name is not presents
125+
// in the class constant pool and the annotations are written with fallback "runtime invisible" name.
126+
void write_annotations_attribute(const char* attr_name, const char* fallback_attr_name, AnnotationArray* annos);
120127
void write_bootstrapmethod_attribute();
121128
void write_nest_host_attribute();
122129
void write_nest_members_attribute();

‎test/jdk/java/lang/instrument/RetransformRecordAnnotation.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323

2424
/*
2525
* @test
26-
* @bug 8315575
26+
* @bug 8315575 8328137
2727
* @summary test that records with invisible annotation can be retransformed
2828
*
2929
* @library /test/lib
3030
* @run shell MakeJAR.sh retransformAgent
3131
* @run main/othervm -javaagent:retransformAgent.jar -Xlog:redefine+class=trace RetransformRecordAnnotation
32+
* @run main/othervm -javaagent:retransformAgent.jar -XX:+PreserveAllAnnotations -Xlog:redefine+class=trace RetransformRecordAnnotation
3233
*/
3334

3435
import java.io.File;

0 commit comments

Comments
 (0)
Please sign in to comment.