Skip to content

Commit 5c78c7c

Browse files
committed
8366341: [BACKOUT] JDK-8365256: RelocIterator should use indexes instead of pointers
Reviewed-by: ayang
1 parent b0f5b23 commit 5c78c7c

File tree

4 files changed

+69
-64
lines changed

4 files changed

+69
-64
lines changed

src/hotspot/share/code/codeBlob.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ CodeBlob::CodeBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size
128128
int mutable_data_size) :
129129
_oop_maps(nullptr), // will be set by set_oop_maps() call
130130
_name(name),
131-
_mutable_data(nullptr),
131+
_mutable_data(header_begin() + size), // default value is blob_end()
132132
_size(size),
133133
_relocation_size(align_up(cb->total_relocation_size(), oopSize)),
134134
_content_offset(CodeBlob::align_code_offset(header_size)),
@@ -158,8 +158,10 @@ CodeBlob::CodeBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size
158158
if (_mutable_data == nullptr) {
159159
vm_exit_out_of_memory(_mutable_data_size, OOM_MALLOC_ERROR, "codebuffer: no space for mutable data");
160160
}
161+
} else {
162+
// We need unique and valid not null address
163+
assert(_mutable_data == blob_end(), "sanity");
161164
}
162-
assert(_mutable_data != nullptr || _mutable_data_size == 0, "No mutable data => mutable data size is 0");
163165

164166
set_oop_maps(oop_maps);
165167
}
@@ -168,7 +170,7 @@ CodeBlob::CodeBlob(const char* name, CodeBlobKind kind, CodeBuffer* cb, int size
168170
CodeBlob::CodeBlob(const char* name, CodeBlobKind kind, int size, uint16_t header_size) :
169171
_oop_maps(nullptr),
170172
_name(name),
171-
_mutable_data(nullptr),
173+
_mutable_data(header_begin() + size), // default value is blob_end()
172174
_size(size),
173175
_relocation_size(0),
174176
_content_offset(CodeBlob::align_code_offset(header_size)),
@@ -182,9 +184,9 @@ CodeBlob::CodeBlob(const char* name, CodeBlobKind kind, int size, uint16_t heade
182184
_kind(kind),
183185
_caller_must_gc_arguments(false)
184186
{
185-
assert(_mutable_data == nullptr && _mutable_data_size == 0, "invariant");
186187
assert(is_aligned(size, oopSize), "unaligned size");
187188
assert(is_aligned(header_size, oopSize), "unaligned size");
189+
assert(_mutable_data == blob_end(), "sanity");
188190
}
189191

190192
void CodeBlob::restore_mutable_data(address reloc_data) {
@@ -195,7 +197,7 @@ void CodeBlob::restore_mutable_data(address reloc_data) {
195197
vm_exit_out_of_memory(_mutable_data_size, OOM_MALLOC_ERROR, "codebuffer: no space for mutable data");
196198
}
197199
} else {
198-
_mutable_data = nullptr;
200+
_mutable_data = blob_end(); // default value
199201
}
200202
if (_relocation_size > 0) {
201203
assert(_mutable_data_size > 0, "relocation is part of mutable data section");
@@ -204,13 +206,17 @@ void CodeBlob::restore_mutable_data(address reloc_data) {
204206
}
205207

206208
void CodeBlob::purge() {
207-
os::free(_mutable_data);
208-
_mutable_data = nullptr;
209-
_mutable_data_size = 0;
210-
delete _oop_maps;
211-
_oop_maps = nullptr;
212-
_relocation_size = 0;
213-
209+
assert(_mutable_data != nullptr, "should never be null");
210+
if (_mutable_data != blob_end()) {
211+
os::free(_mutable_data);
212+
_mutable_data = blob_end(); // Valid not null address
213+
_mutable_data_size = 0;
214+
_relocation_size = 0;
215+
}
216+
if (_oop_maps != nullptr) {
217+
delete _oop_maps;
218+
_oop_maps = nullptr;
219+
}
214220
NOT_PRODUCT(_asm_remarks.clear());
215221
NOT_PRODUCT(_dbg_strings.clear());
216222
}

src/hotspot/share/code/nmethod.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,8 +1327,8 @@ nmethod::nmethod(
13271327
"wrong mutable data size: %d != %d + %d",
13281328
_mutable_data_size, _relocation_size, metadata_size);
13291329

1330-
// native wrapper does not have read-only data
1331-
_immutable_data = nullptr;
1330+
// native wrapper does not have read-only data but we need unique not null address
1331+
_immutable_data = blob_end();
13321332
_immutable_data_size = 0;
13331333
_nul_chk_table_offset = 0;
13341334
_handler_table_offset = 0;
@@ -1510,7 +1510,8 @@ nmethod::nmethod(
15101510
assert(immutable_data != nullptr, "required");
15111511
_immutable_data = immutable_data;
15121512
} else {
1513-
_immutable_data = nullptr;
1513+
// We need unique not null address
1514+
_immutable_data = blob_end();
15141515
}
15151516
CHECKED_CAST(_nul_chk_table_offset, uint16_t, (align_up((int)dependencies->size_in_bytes(), oopSize)));
15161517
CHECKED_CAST(_handler_table_offset, uint16_t, (_nul_chk_table_offset + align_up(nul_chk_table->size_in_bytes(), oopSize)));
@@ -2146,14 +2147,15 @@ void nmethod::purge(bool unregister_nmethod) {
21462147
delete ec;
21472148
ec = next;
21482149
}
2149-
2150-
delete _pc_desc_container;
2150+
if (_pc_desc_container != nullptr) {
2151+
delete _pc_desc_container;
2152+
}
21512153
delete[] _compiled_ic_data;
21522154

2153-
os::free(_immutable_data);
2154-
_immutable_data = nullptr;
2155-
_immutable_data_size = 0;
2156-
2155+
if (_immutable_data != blob_end()) {
2156+
os::free(_immutable_data);
2157+
_immutable_data = blob_end(); // Valid not null address
2158+
}
21572159
if (unregister_nmethod) {
21582160
Universe::heap()->unregister_nmethod(this);
21592161
}

src/hotspot/share/code/relocInfo.cpp

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ void relocInfo::change_reloc_info_for_address(RelocIterator *itr, address pc, re
116116
// ----------------------------------------------------------------------------------------------------
117117
// Implementation of RelocIterator
118118

119+
// A static dummy to serve as a safe pointer when there is no relocation info.
120+
static relocInfo dummy_relocInfo = relocInfo(relocInfo::none, 0);
121+
119122
void RelocIterator::initialize(nmethod* nm, address begin, address limit) {
120123
initialize_misc();
121124

@@ -127,9 +130,14 @@ void RelocIterator::initialize(nmethod* nm, address begin, address limit) {
127130
guarantee(nm != nullptr, "must be able to deduce nmethod from other arguments");
128131

129132
_code = nm;
130-
_base = nm->relocation_begin();
131-
_current = -1;
132-
_len = nm->relocation_end() - _base;
133+
if (nm->relocation_size() == 0) {
134+
_current = &dummy_relocInfo - 1;
135+
_end = &dummy_relocInfo;
136+
} else {
137+
assert(((nm->relocation_begin() != nullptr) && (nm->relocation_end() != nullptr)), "valid start and end pointer");
138+
_current = nm->relocation_begin() - 1;
139+
_end = nm->relocation_end();
140+
}
133141
_addr = nm->content_begin();
134142

135143
// Initialize code sections.
@@ -148,21 +156,11 @@ void RelocIterator::initialize(nmethod* nm, address begin, address limit) {
148156
}
149157

150158

151-
RelocIterator::RelocIterator(relocInfo& ri) {
152-
initialize_misc();
153-
_base = &ri;
154-
_len = 1;
155-
_current = -1;
156-
_limit = nullptr;
157-
_addr = 0;
158-
}
159-
160159
RelocIterator::RelocIterator(CodeSection* cs, address begin, address limit) {
161160
initialize_misc();
162161
assert(((cs->locs_start() != nullptr) && (cs->locs_end() != nullptr)), "valid start and end pointer");
163-
_base = cs->locs_start();
164-
_len = cs->locs_end() - _base;
165-
_current = -1;
162+
_current = cs->locs_start() - 1;
163+
_end = cs->locs_end();
166164
_addr = cs->start();
167165
_code = nullptr; // Not cb->blob();
168166

@@ -188,9 +186,8 @@ RelocIterator::RelocIterator(CodeBlob* cb) {
188186
} else {
189187
_code = nullptr;
190188
}
191-
_base = cb->relocation_begin();
192-
_len = cb->relocation_end() - _base;
193-
_current = -1;
189+
_current = cb->relocation_begin() - 1;
190+
_end = cb->relocation_end();
194191
_addr = cb->content_begin();
195192

196193
_section_start[CodeBuffer::SECT_CONSTS] = cb->content_begin();
@@ -219,7 +216,7 @@ void RelocIterator::set_limits(address begin, address limit) {
219216

220217
// the limit affects this next stuff:
221218
if (begin != nullptr) {
222-
int backup;
219+
relocInfo* backup;
223220
address backup_addr;
224221
while (true) {
225222
backup = _current;
@@ -241,12 +238,12 @@ void RelocIterator::set_limits(address begin, address limit) {
241238
// very efficiently (a single extra halfword). Larger chunks of
242239
// relocation data need a halfword header to hold their size.
243240
void RelocIterator::advance_over_prefix() {
244-
if (current()->is_datalen()) {
245-
_data = (short*) current()->data();
246-
_datalen = current()->datalen();
241+
if (_current->is_datalen()) {
242+
_data = (short*) _current->data();
243+
_datalen = _current->datalen();
247244
_current += _datalen + 1; // skip the embedded data & header
248245
} else {
249-
_databuf = current()->immediate();
246+
_databuf = _current->immediate();
250247
_data = &_databuf;
251248
_datalen = 1;
252249
_current++; // skip the header
@@ -353,9 +350,9 @@ void Relocation::const_verify_data_value(address x) {
353350

354351
RelocationHolder Relocation::spec_simple(relocInfo::relocType rtype) {
355352
if (rtype == relocInfo::none) return RelocationHolder::none;
356-
relocInfo ri(rtype, 0);
357-
RelocIterator itr(ri);
358-
itr.next();
353+
relocInfo ri = relocInfo(rtype, 0);
354+
RelocIterator itr;
355+
itr.set_current(ri);
359356
itr.reloc();
360357
return itr._rh;
361358
}
@@ -842,7 +839,7 @@ void RelocIterator::print_current_on(outputStream* st) {
842839
return;
843840
}
844841
st->print("relocInfo@" INTPTR_FORMAT " [type=%d(%s) addr=" INTPTR_FORMAT " offset=%d",
845-
p2i(current()), type(), relocInfo::type_name((relocInfo::relocType) type()), p2i(_addr), current()->addr_offset());
842+
p2i(_current), type(), relocInfo::type_name((relocInfo::relocType) type()), p2i(_addr), _current->addr_offset());
846843
if (current()->format() != 0)
847844
st->print(" format=%d", current()->format());
848845
if (datalen() == 1) {
@@ -993,7 +990,7 @@ void RelocIterator::print_current_on(outputStream* st) {
993990

994991
void RelocIterator::print_on(outputStream* st) {
995992
RelocIterator save_this = (*this);
996-
relocInfo* scan = current_no_check();
993+
relocInfo* scan = _current;
997994
if (!has_current()) scan += 1; // nothing to scan here!
998995

999996
bool skip_next = has_current();
@@ -1003,7 +1000,7 @@ void RelocIterator::print_on(outputStream* st) {
10031000
skip_next = false;
10041001

10051002
st->print(" @" INTPTR_FORMAT ": ", p2i(scan));
1006-
relocInfo* newscan = current_no_check()+1;
1003+
relocInfo* newscan = _current+1;
10071004
if (!has_current()) newscan -= 1; // nothing to scan here!
10081005
while (scan < newscan) {
10091006
st->print("%04x", *(short*)scan & 0xFFFF);

src/hotspot/share/code/relocInfo.hpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -562,13 +562,12 @@ class RelocIterator : public StackObj {
562562

563563
private:
564564
address _limit; // stop producing relocations after this _addr
565-
relocInfo* _base; // base pointer into relocInfo array
566-
int _current; // current index
567-
int _len; // length
565+
relocInfo* _current; // the current relocation information
566+
relocInfo* _end; // end marker; we're done iterating when _current == _end
568567
nmethod* _code; // compiled method containing _addr
569568
address _addr; // instruction to which the relocation applies
570-
short* _data; // pointer to the relocation's data
571569
short _databuf; // spare buffer for compressed data
570+
short* _data; // pointer to the relocation's data
572571
short _datalen; // number of halfwords in _data
573572

574573
// Base addresses needed to compute targets of section_word_type relocs.
@@ -579,14 +578,15 @@ class RelocIterator : public StackObj {
579578
_datalen = !b ? -1 : 0;
580579
DEBUG_ONLY(_data = nullptr);
581580
}
581+
void set_current(relocInfo& ri) {
582+
_current = &ri;
583+
set_has_current(true);
584+
}
582585

583586
RelocationHolder _rh; // where the current relocation is allocated
584587

585-
relocInfo* current_no_check() const { return &_base[_current]; }
586-
relocInfo* current() const {
587-
assert(has_current(), "must have current");
588-
return current_no_check();
589-
}
588+
relocInfo* current() const { assert(has_current(), "must have current");
589+
return _current; }
590590

591591
void set_limits(address begin, address limit);
592592

@@ -597,7 +597,6 @@ class RelocIterator : public StackObj {
597597
void initialize(nmethod* nm, address begin, address limit);
598598

599599
RelocIterator() { initialize_misc(); }
600-
RelocIterator(relocInfo& ri);
601600

602601
public:
603602
// constructor
@@ -608,24 +607,25 @@ class RelocIterator : public StackObj {
608607
// get next reloc info, return !eos
609608
bool next() {
610609
_current++;
611-
assert(_current <= _len, "must not overrun relocInfo");
612-
if (_current == _len) {
610+
assert(_current <= _end, "must not overrun relocInfo");
611+
if (_current == _end) {
613612
set_has_current(false);
614613
return false;
615614
}
616615
set_has_current(true);
617616

618-
if (current()->is_prefix()) {
617+
if (_current->is_prefix()) {
619618
advance_over_prefix();
620619
assert(!current()->is_prefix(), "only one prefix at a time");
621620
}
622621

623-
_addr += current()->addr_offset();
622+
_addr += _current->addr_offset();
624623

625624
if (_limit != nullptr && _addr >= _limit) {
626625
set_has_current(false);
627626
return false;
628627
}
628+
629629
return true;
630630
}
631631

0 commit comments

Comments
 (0)