8296453: Simplify resource_area uses in ClassPathDirEntry::open_stream

Reviewed-by: dholmes, phh
This commit is contained in:
Xin Liu 2022-11-16 22:40:04 +00:00
parent 95c390ec75
commit 2159170b41
4 changed files with 17 additions and 10 deletions

View File

@ -255,7 +255,7 @@ ClassFileStream* ClassPathDirEntry::open_stream(JavaThread* current, const char*
int file_handle = os::open(path, 0, 0);
if (file_handle != -1) {
// read contents into resource array
u1* buffer = NEW_RESOURCE_ARRAY(u1, st.st_size);
u1* buffer = NEW_RESOURCE_ARRAY_IN_THREAD(current, u1, st.st_size);
size_t num_read = ::read(file_handle, (char*) buffer, st.st_size);
// close file
::close(file_handle);
@ -264,7 +264,11 @@ ClassFileStream* ClassPathDirEntry::open_stream(JavaThread* current, const char*
if (UsePerfData) {
ClassLoader::perf_sys_classfile_bytes_read()->inc(num_read);
}
FREE_RESOURCE_ARRAY(char, path, path_len);
#ifdef ASSERT
// Freeing path is a no-op here as buffer prevents it from being reclaimed. But we keep it for
// debug builds so that we guard against use-after-free bugs.
FREE_RESOURCE_ARRAY_IN_THREAD(current, char, path, path_len);
#endif
// Resource allocated
return new ClassFileStream(buffer,
st.st_size,
@ -273,7 +277,7 @@ ClassFileStream* ClassPathDirEntry::open_stream(JavaThread* current, const char*
}
}
}
FREE_RESOURCE_ARRAY(char, path, path_len);
FREE_RESOURCE_ARRAY_IN_THREAD(current, char, path, path_len);
return NULL;
}

View File

@ -423,7 +423,7 @@ extern char* resource_allocate_bytes(Thread* thread, size_t size,
AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
extern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_size,
AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
extern void resource_free_bytes( char *old, size_t size );
extern void resource_free_bytes( Thread* thread, char *old, size_t size );
//----------------------------------------------------------------------
// Base class for objects allocated in the resource area.
@ -548,7 +548,10 @@ protected:
(new_size) * sizeof(type), AllocFailStrategy::RETURN_NULL)
#define FREE_RESOURCE_ARRAY(type, old, size)\
resource_free_bytes((char*)(old), (size) * sizeof(type))
resource_free_bytes(Thread::current(), (char*)(old), (size) * sizeof(type))
#define FREE_RESOURCE_ARRAY_IN_THREAD(thread, type, old, size)\
resource_free_bytes(thread, (char*)(old), (size) * sizeof(type))
#define FREE_FAST(old)\
/* nop */

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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
return (char*)Thread::current()->resource_area()->Arealloc(old, old_size, new_size, alloc_failmode);
}
extern void resource_free_bytes( char *old, size_t size ) {
Thread::current()->resource_area()->Afree(old, size);
extern void resource_free_bytes( Thread* thread, char *old, size_t size ) {
thread->resource_area()->Afree(old, size);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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)
template <class E, MEMFLAGS F>
void ResourceStack<E, F>::free(E* addr, size_t bytes)
{
resource_free_bytes((char*) addr, bytes);
resource_free_bytes(Thread::current(), (char*) addr, bytes);
}
template <class E, MEMFLAGS F>