Skip to content

Commit 3cc459b

Browse files
jckingTobiHartmann
authored andcommitted
8302595: use-after-free related to GraphKit::clone_map
Reviewed-by: kvn, thartmann
1 parent 2e3cea0 commit 3cc459b

File tree

7 files changed

+47
-7
lines changed

7 files changed

+47
-7
lines changed

src/hotspot/share/opto/compile.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,8 @@ class Compile : public Phase {
936936
// Parsing, optimization
937937
PhaseGVN* initial_gvn() { return _initial_gvn; }
938938
Unique_Node_List* for_igvn() { return _for_igvn; }
939-
inline void record_for_igvn(Node* n); // Body is after class Unique_Node_List.
939+
inline void record_for_igvn(Node* n); // Body is after class Unique_Node_List in node.hpp.
940+
inline void remove_for_igvn(Node* n); // Body is after class Unique_Node_List in node.hpp.
940941
void set_initial_gvn(PhaseGVN *gvn) { _initial_gvn = gvn; }
941942
void set_for_igvn(Unique_Node_List *for_igvn) { _for_igvn = for_igvn; }
942943

src/hotspot/share/opto/graphKit.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,29 @@ SafePointNode* GraphKit::clone_map() {
735735
return clonemap;
736736
}
737737

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

739762
//-----------------------------set_map_clone-----------------------------------
740763
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
@@ -1646,7 +1646,7 @@ bool LibraryCallKit::inline_string_char_access(bool is_store) {
16461646
set_sp(old_sp);
16471647
return false;
16481648
}
1649-
old_map->destruct(&_gvn);
1649+
destruct_map_clone(old_map);
16501650
if (is_store) {
16511651
access_store_at(value, adr, TypeAryPtr::BYTES, ch, TypeInt::CHAR, T_CHAR, IN_HEAP | MO_UNORDERED | C2_MISMATCHED);
16521652
} else {
@@ -2361,7 +2361,7 @@ bool LibraryCallKit::inline_unsafe_access(bool is_store, const BasicType type, c
23612361
mismatched = true; // conservatively mark all "wide" on-heap accesses as mismatched
23622362
}
23632363

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

23672367
if (mismatched) {
@@ -2612,7 +2612,7 @@ bool LibraryCallKit::inline_unsafe_load_store(const BasicType type, const LoadSt
26122612
return false;
26132613
}
26142614

2615-
old_map->destruct(&_gvn);
2615+
destruct_map_clone(old_map);
26162616

26172617
// For CAS, unlike inline_unsafe_access, there seems no point in
26182618
// 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
@@ -1672,6 +1672,11 @@ inline void Compile::record_for_igvn(Node* n) {
16721672
_for_igvn->push(n);
16731673
}
16741674

1675+
// Inline definition of Compile::remove_for_igvn must be deferred to this point.
1676+
inline void Compile::remove_for_igvn(Node* n) {
1677+
_for_igvn->remove(n);
1678+
}
1679+
16751680
//------------------------------Node_Stack-------------------------------------
16761681
class Node_Stack {
16771682
friend class VMStructs;

src/hotspot/share/opto/phaseX.hpp

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

src/hotspot/share/opto/vectorIntrinsics.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ bool LibraryCallKit::inline_vector_mem_operation(bool is_store) {
11131113
set_result(box);
11141114
}
11151115

1116-
old_map->destruct(&_gvn);
1116+
destruct_map_clone(old_map);
11171117

11181118
if (needs_cpu_membar) {
11191119
insert_mem_bar(Op_MemBarCPUOrder);
@@ -1372,7 +1372,7 @@ bool LibraryCallKit::inline_vector_mem_masked_operation(bool is_store) {
13721372
set_result(box);
13731373
}
13741374

1375-
old_map->destruct(&_gvn);
1375+
destruct_map_clone(old_map);
13761376

13771377
if (can_access_non_heap) {
13781378
insert_mem_bar(Op_MemBarCPUOrder);
@@ -1585,7 +1585,7 @@ bool LibraryCallKit::inline_vector_gather_scatter(bool is_scatter) {
15851585
set_result(box);
15861586
}
15871587

1588-
old_map->destruct(&_gvn);
1588+
destruct_map_clone(old_map);
15891589

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

0 commit comments

Comments
 (0)