Skip to content

Commit da740fa

Browse files
committed
8302595: use-after-free related to GraphKit::clone_map
Reviewed-by: rrich Backport-of: 3cc459b6c2f571987dc36fd548a2b830f0b33a0a
1 parent 790802b commit da740fa

File tree

7 files changed

+46
-6
lines changed

7 files changed

+46
-6
lines changed

src/hotspot/share/opto/compile.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,8 @@ class Compile : public Phase {
921921
// Parsing, optimization
922922
PhaseGVN* initial_gvn() { return _initial_gvn; }
923923
Unique_Node_List* for_igvn() { return _for_igvn; }
924-
inline void record_for_igvn(Node* n); // Body is after class Unique_Node_List.
924+
inline void record_for_igvn(Node* n); // Body is after class Unique_Node_List in node.hpp.
925+
inline void remove_for_igvn(Node* n); // Body is after class Unique_Node_List in node.hpp.
925926
void set_initial_gvn(PhaseGVN *gvn) { _initial_gvn = gvn; }
926927
void set_for_igvn(Unique_Node_List *for_igvn) { _for_igvn = for_igvn; }
927928

src/hotspot/share/opto/graphKit.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,29 @@ SafePointNode* GraphKit::clone_map() {
738738
return clonemap;
739739
}
740740

741+
//-----------------------------destruct_map_clone------------------------------
742+
//
743+
// Order of destruct is important to increase the likelyhood that memory can be re-used. We need
744+
// to destruct/free/delete in the exact opposite order as clone_map().
745+
void GraphKit::destruct_map_clone(SafePointNode* sfp) {
746+
if (sfp == nullptr) return;
747+
748+
Node* mem = sfp->memory();
749+
JVMState* jvms = sfp->jvms();
750+
751+
if (jvms != nullptr) {
752+
delete jvms;
753+
}
754+
755+
remove_for_igvn(sfp);
756+
gvn().clear_type(sfp);
757+
sfp->destruct(&_gvn);
758+
759+
if (mem != nullptr) {
760+
gvn().clear_type(mem);
761+
mem->destruct(&_gvn);
762+
}
763+
}
741764

742765
//-----------------------------set_map_clone-----------------------------------
743766
void GraphKit::set_map_clone(SafePointNode* m) {

src/hotspot/share/opto/graphKit.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class GraphKit : public Phase {
9494
void* barrier_set_state() const { return C->barrier_set_state(); }
9595

9696
void record_for_igvn(Node* n) const { C->record_for_igvn(n); } // delegate to Compile
97+
void remove_for_igvn(Node* n) const { C->remove_for_igvn(n); }
9798

9899
// Handy well-known nodes:
99100
Node* null() const { return zerocon(T_OBJECT); }
@@ -170,6 +171,11 @@ class GraphKit : public Phase {
170171
// Clone the existing map state. (Implements PreserveJVMState.)
171172
SafePointNode* clone_map();
172173

174+
// Reverses the work done by clone_map(). Should only be used when the node returned by
175+
// clone_map() is ultimately not used. Calling Node::destruct directly in the previously
176+
// mentioned circumstance instead of this method may result in use-after-free.
177+
void destruct_map_clone(SafePointNode* sfp);
178+
173179
// Set the map to a clone of the given one.
174180
void set_map_clone(SafePointNode* m);
175181

src/hotspot/share/opto/library_call.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,7 @@ bool LibraryCallKit::inline_string_char_access(bool is_store) {
15641564
set_sp(old_sp);
15651565
return false;
15661566
}
1567-
old_map->destruct(&_gvn);
1567+
destruct_map_clone(old_map);
15681568
if (is_store) {
15691569
access_store_at(value, adr, TypeAryPtr::BYTES, ch, TypeInt::CHAR, T_CHAR, IN_HEAP | MO_UNORDERED | C2_MISMATCHED);
15701570
} else {
@@ -2347,7 +2347,7 @@ bool LibraryCallKit::inline_unsafe_access(bool is_store, const BasicType type, c
23472347
mismatched = true; // conservatively mark all "wide" on-heap accesses as mismatched
23482348
}
23492349

2350-
old_map->destruct(&_gvn);
2350+
destruct_map_clone(old_map);
23512351
assert(!mismatched || alias_type->adr_type()->is_oopptr(), "off-heap access can't be mismatched");
23522352

23532353
if (mismatched) {
@@ -2598,7 +2598,7 @@ bool LibraryCallKit::inline_unsafe_load_store(const BasicType type, const LoadSt
25982598
return false;
25992599
}
26002600

2601-
old_map->destruct(&_gvn);
2601+
destruct_map_clone(old_map);
26022602

26032603
// For CAS, unlike inline_unsafe_access, there seems no point in
26042604
// trying to refine types. Just use the coarse types here.

src/hotspot/share/opto/node.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,11 @@ inline void Compile::record_for_igvn(Node* n) {
16471647
_for_igvn->push(n);
16481648
}
16491649

1650+
// Inline definition of Compile::remove_for_igvn must be deferred to this point.
1651+
inline void Compile::remove_for_igvn(Node* n) {
1652+
_for_igvn->remove(n);
1653+
}
1654+
16501655
//------------------------------Node_Stack-------------------------------------
16511656
class Node_Stack {
16521657
friend class VMStructs;

src/hotspot/share/opto/phaseX.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@ class PhaseTransform : public Phase {
238238
assert(t != NULL, "type must not be null");
239239
_types.map(n->_idx, t);
240240
}
241+
void clear_type(const Node* n) {
242+
if (n->_idx < _types.Size()) {
243+
_types.map(n->_idx, NULL);
244+
}
245+
}
241246
// Record an initial type for a node, the node's bottom type.
242247
void set_type_bottom(const Node* n) {
243248
// Use this for initialization when bottom_type() (or better) is not handy.

src/hotspot/share/opto/vectorIntrinsics.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ bool LibraryCallKit::inline_vector_mem_operation(bool is_store) {
868868
set_result(box);
869869
}
870870

871-
old_map->destruct(&_gvn);
871+
destruct_map_clone(old_map);
872872

873873
if (can_access_non_heap) {
874874
insert_mem_bar(Op_MemBarCPUOrder);
@@ -1006,7 +1006,7 @@ bool LibraryCallKit::inline_vector_gather_scatter(bool is_scatter) {
10061006
set_result(box);
10071007
}
10081008

1009-
old_map->destruct(&_gvn);
1009+
destruct_map_clone(old_map);
10101010

10111011
C->set_max_vector_size(MAX2(C->max_vector_size(), (uint)(num_elem * type2aelembytes(elem_bt))));
10121012
return true;

0 commit comments

Comments
 (0)