Skip to content

Commit 2159170

Browse files
author
Xin Liu
committed
8296453: Simplify resource_area uses in ClassPathDirEntry::open_stream
Reviewed-by: dholmes, phh
1 parent 95c390e commit 2159170

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

src/hotspot/share/classfile/classLoader.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ ClassFileStream* ClassPathDirEntry::open_stream(JavaThread* current, const char*
255255
int file_handle = os::open(path, 0, 0);
256256
if (file_handle != -1) {
257257
// read contents into resource array
258-
u1* buffer = NEW_RESOURCE_ARRAY(u1, st.st_size);
258+
u1* buffer = NEW_RESOURCE_ARRAY_IN_THREAD(current, u1, st.st_size);
259259
size_t num_read = ::read(file_handle, (char*) buffer, st.st_size);
260260
// close file
261261
::close(file_handle);
@@ -264,7 +264,11 @@ ClassFileStream* ClassPathDirEntry::open_stream(JavaThread* current, const char*
264264
if (UsePerfData) {
265265
ClassLoader::perf_sys_classfile_bytes_read()->inc(num_read);
266266
}
267-
FREE_RESOURCE_ARRAY(char, path, path_len);
267+
#ifdef ASSERT
268+
// Freeing path is a no-op here as buffer prevents it from being reclaimed. But we keep it for
269+
// debug builds so that we guard against use-after-free bugs.
270+
FREE_RESOURCE_ARRAY_IN_THREAD(current, char, path, path_len);
271+
#endif
268272
// Resource allocated
269273
return new ClassFileStream(buffer,
270274
st.st_size,
@@ -273,7 +277,7 @@ ClassFileStream* ClassPathDirEntry::open_stream(JavaThread* current, const char*
273277
}
274278
}
275279
}
276-
FREE_RESOURCE_ARRAY(char, path, path_len);
280+
FREE_RESOURCE_ARRAY_IN_THREAD(current, char, path, path_len);
277281
return NULL;
278282
}
279283

src/hotspot/share/memory/allocation.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ extern char* resource_allocate_bytes(Thread* thread, size_t size,
423423
AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
424424
extern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_size,
425425
AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
426-
extern void resource_free_bytes( char *old, size_t size );
426+
extern void resource_free_bytes( Thread* thread, char *old, size_t size );
427427

428428
//----------------------------------------------------------------------
429429
// Base class for objects allocated in the resource area.
@@ -548,7 +548,10 @@ class AnyObj {
548548
(new_size) * sizeof(type), AllocFailStrategy::RETURN_NULL)
549549

550550
#define FREE_RESOURCE_ARRAY(type, old, size)\
551-
resource_free_bytes((char*)(old), (size) * sizeof(type))
551+
resource_free_bytes(Thread::current(), (char*)(old), (size) * sizeof(type))
552+
553+
#define FREE_RESOURCE_ARRAY_IN_THREAD(thread, type, old, size)\
554+
resource_free_bytes(thread, (char*)(old), (size) * sizeof(type))
552555

553556
#define FREE_FAST(old)\
554557
/* nop */

src/hotspot/share/memory/resourceArea.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2022, 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
@@ -74,6 +74,6 @@ extern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_s
7474
return (char*)Thread::current()->resource_area()->Arealloc(old, old_size, new_size, alloc_failmode);
7575
}
7676

77-
extern void resource_free_bytes( char *old, size_t size ) {
78-
Thread::current()->resource_area()->Afree(old, size);
77+
extern void resource_free_bytes( Thread* thread, char *old, size_t size ) {
78+
thread->resource_area()->Afree(old, size);
7979
}

src/hotspot/share/utilities/stack.inline.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2022, 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
@@ -252,7 +252,7 @@ E* ResourceStack<E, F>::alloc(size_t bytes)
252252
template <class E, MEMFLAGS F>
253253
void ResourceStack<E, F>::free(E* addr, size_t bytes)
254254
{
255-
resource_free_bytes((char*) addr, bytes);
255+
resource_free_bytes(Thread::current(), (char*) addr, bytes);
256256
}
257257

258258
template <class E, MEMFLAGS F>

0 commit comments

Comments
 (0)