Skip to content

Commit d3f31b0

Browse files
author
Kim Barrett
committed
8347719: [REDO] Portable implementation of FORBID_C_FUNCTION and ALLOW_C_FUNCTION
Reviewed-by: tschatzl, jsjolen
1 parent 6254046 commit d3f31b0

32 files changed

+564
-163
lines changed

src/hotspot/os/aix/libodm_aix.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <string.h>
3131
#include "runtime/arguments.hpp"
3232
#include "runtime/os.hpp"
33+
#include "utilities/permitForbiddenFunctions.hpp"
3334

3435

3536
dynamicOdm::dynamicOdm() {
@@ -59,7 +60,7 @@ dynamicOdm::~dynamicOdm() {
5960
}
6061

6162

62-
void odmWrapper::clean_data() { if (_data) { free(_data); _data = nullptr; } }
63+
void odmWrapper::clean_data() { if (_data) { permit_forbidden_function::free(_data); _data = nullptr; } }
6364

6465

6566
int odmWrapper::class_offset(const char *field, bool is_aix_5)

src/hotspot/os/aix/loadlib_aix.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "logging/log.hpp"
3939
#include "utilities/debug.hpp"
4040
#include "utilities/ostream.hpp"
41+
#include "utilities/permitForbiddenFunctions.hpp"
4142

4243
// For loadquery()
4344
#include <sys/ldr.h>
@@ -58,7 +59,7 @@ class StringList {
5859
// Enlarge list. If oom, leave old list intact and return false.
5960
bool enlarge() {
6061
int cap2 = _cap + 64;
61-
char** l2 = (char**) ::realloc(_list, sizeof(char*) * cap2);
62+
char** l2 = (char**) permit_forbidden_function::realloc(_list, sizeof(char*) * cap2);
6263
if (!l2) {
6364
return false;
6465
}
@@ -76,7 +77,7 @@ class StringList {
7677
}
7778
}
7879
assert0(_cap > _num);
79-
char* s2 = ::strdup(s);
80+
char* s2 = permit_forbidden_function::strdup(s);
8081
if (!s2) {
8182
return nullptr;
8283
}
@@ -170,7 +171,7 @@ static void free_entry_list(loaded_module_t** start) {
170171
loaded_module_t* lm = *start;
171172
while (lm) {
172173
loaded_module_t* const lm2 = lm->next;
173-
::free(lm);
174+
permit_forbidden_function::free(lm);
174175
lm = lm2;
175176
}
176177
*start = nullptr;
@@ -193,7 +194,7 @@ static bool reload_table() {
193194
uint8_t* buffer = nullptr;
194195
size_t buflen = 1024;
195196
for (;;) {
196-
buffer = (uint8_t*) ::realloc(buffer, buflen);
197+
buffer = (uint8_t*) permit_forbidden_function::realloc(buffer, buflen);
197198
if (loadquery(L_GETINFO, buffer, buflen) == -1) {
198199
if (errno == ENOMEM) {
199200
buflen *= 2;
@@ -229,7 +230,7 @@ static bool reload_table() {
229230

230231
for (;;) {
231232

232-
loaded_module_t* lm = (loaded_module_t*) ::malloc(sizeof(loaded_module_t));
233+
loaded_module_t* lm = (loaded_module_t*) permit_forbidden_function::malloc(sizeof(loaded_module_t));
233234
if (!lm) {
234235
log_warning(os)("OOM.");
235236
goto cleanup;
@@ -250,7 +251,7 @@ static bool reload_table() {
250251

251252
if (!lm->path) {
252253
log_warning(os)("OOM.");
253-
free(lm);
254+
permit_forbidden_function::free(lm);
254255
goto cleanup;
255256
}
256257

@@ -272,7 +273,7 @@ static bool reload_table() {
272273
lm->member = g_stringlist.add(p_mbr_name);
273274
if (!lm->member) {
274275
log_warning(os)("OOM.");
275-
free(lm);
276+
permit_forbidden_function::free(lm);
276277
goto cleanup;
277278
}
278279
} else {
@@ -320,7 +321,7 @@ static bool reload_table() {
320321
free_entry_list(&new_list);
321322
}
322323

323-
::free(buffer);
324+
permit_forbidden_function::free(buffer);
324325

325326
return rc;
326327

src/hotspot/os/aix/os_aix.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#include "utilities/defaultStream.hpp"
7474
#include "utilities/events.hpp"
7575
#include "utilities/growableArray.hpp"
76+
#include "utilities/permitForbiddenFunctions.hpp"
7677
#include "utilities/vmError.hpp"
7778
#if INCLUDE_JFR
7879
#include "jfr/support/jfrNativeLibraryLoadEvent.hpp"
@@ -369,9 +370,9 @@ static void query_multipage_support() {
369370
// or by environment variable LDR_CNTRL (suboption DATAPSIZE). If none is given,
370371
// default should be 4K.
371372
{
372-
void* p = ::malloc(16*M);
373+
void* p = permit_forbidden_function::malloc(16*M);
373374
g_multipage_support.datapsize = os::Aix::query_pagesize(p);
374-
::free(p);
375+
permit_forbidden_function::free(p);
375376
}
376377

377378
// Query default shm page size (LDR_CNTRL SHMPSIZE).
@@ -1398,7 +1399,7 @@ static struct {
13981399
} vmem;
13991400

14001401
static void vmembk_add(char* addr, size_t size, size_t pagesize, int type) {
1401-
vmembk_t* p = (vmembk_t*) ::malloc(sizeof(vmembk_t));
1402+
vmembk_t* p = (vmembk_t*) permit_forbidden_function::malloc(sizeof(vmembk_t));
14021403
assert0(p);
14031404
if (p) {
14041405
MiscUtils::AutoCritSect lck(&vmem.cs);
@@ -1427,7 +1428,7 @@ static void vmembk_remove(vmembk_t* p0) {
14271428
for (vmembk_t** pp = &(vmem.first); *pp; pp = &((*pp)->next)) {
14281429
if (*pp == p0) {
14291430
*pp = p0->next;
1430-
::free(p0);
1431+
permit_forbidden_function::free(p0);
14311432
return;
14321433
}
14331434
}

src/hotspot/os/aix/porting_aix.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "runtime/os.hpp"
4040
#include "utilities/align.hpp"
4141
#include "utilities/debug.hpp"
42+
#include "utilities/permitForbiddenFunctions.hpp"
4243
#include <cxxabi.h>
4344
#include <sys/debug.h>
4445
#include <pthread.h>
@@ -250,7 +251,7 @@ bool AixSymbols::get_function_name (
250251
p_name[namelen-1] = '\0';
251252
}
252253
if (demangled_name != nullptr) {
253-
ALLOW_C_FUNCTION(::free, ::free(demangled_name));
254+
permit_forbidden_function::free(demangled_name);
254255
}
255256
}
256257
} else {
@@ -1081,7 +1082,7 @@ void* Aix_dlopen(const char* filename, int Flags, int *eno, const char** error_r
10811082
if (g_handletable_used == max_handletable) {
10821083
// No place in array anymore; increase array.
10831084
unsigned new_max = MAX2(max_handletable * 2, init_num_handles);
1084-
struct handletableentry* new_tab = (struct handletableentry*)::realloc(p_handletable, new_max * sizeof(struct handletableentry));
1085+
struct handletableentry* new_tab = (struct handletableentry*) permit_forbidden_function::realloc(p_handletable, new_max * sizeof(struct handletableentry));
10851086
assert(new_tab != nullptr, "no more memory for handletable");
10861087
if (new_tab == nullptr) {
10871088
*error_report = "dlopen: no more memory for handletable";

src/hotspot/os/bsd/decoder_machO.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "decoder_machO.hpp"
2828
#include "jvm.h"
2929
#include "memory/allocation.inline.hpp"
30+
#include "utilities/globalDefinitions.hpp"
31+
#include "utilities/permitForbiddenFunctions.hpp"
3032

3133
#include <cxxabi.h>
3234
#include <mach-o/loader.h>
@@ -42,9 +44,9 @@ bool MachODecoder::demangle(const char* symbol, char *buf, int buflen) {
4244
// may use different malloc/realloc mechanism that allocates 'buf'.
4345
if ((result = abi::__cxa_demangle(symbol, nullptr, nullptr, &status)) != nullptr) {
4446
jio_snprintf(buf, buflen, "%s", result);
45-
// call c library's free
46-
::free(result);
47-
return true;
47+
// call c library's free
48+
permit_forbidden_function::free(result);
49+
return true;
4850
}
4951
return false;
5052
}

src/hotspot/os/linux/decoder_linux.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025, 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
@@ -27,6 +27,7 @@
2727
#include "utilities/decoder_elf.hpp"
2828
#include "utilities/elfFile.hpp"
2929
#include "utilities/globalDefinitions.hpp"
30+
#include "utilities/permitForbiddenFunctions.hpp"
3031

3132
#include <cxxabi.h>
3233

@@ -46,9 +47,9 @@ bool ElfDecoder::demangle(const char* symbol, char *buf, int buflen) {
4647
// may use different malloc/realloc mechanism that allocates 'buf'.
4748
if ((result = abi::__cxa_demangle(symbol, nullptr, nullptr, &status)) != nullptr) {
4849
jio_snprintf(buf, buflen, "%s", result);
49-
// call c library's free
50-
ALLOW_C_FUNCTION(::free, ::free(result);)
51-
return true;
50+
// call c library's free
51+
permit_forbidden_function::free(result);
52+
return true;
5253
}
5354
return false;
5455
}

src/hotspot/os/linux/gc/z/zMountPoint_linux.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "runtime/globals.hpp"
2929
#include "runtime/os.hpp"
3030
#include "utilities/globalDefinitions.hpp"
31+
#include "utilities/permitForbiddenFunctions.hpp"
3132

3233
#include <stdio.h>
3334
#include <unistd.h>
@@ -62,11 +63,11 @@ char* ZMountPoint::get_mountpoint(const char* line, const char* filesystem) cons
6263
strcmp(line_filesystem, filesystem) != 0 ||
6364
access(line_mountpoint, R_OK|W_OK|X_OK) != 0) {
6465
// Not a matching or accessible filesystem
65-
ALLOW_C_FUNCTION(::free, ::free(line_mountpoint);)
66+
permit_forbidden_function::free(line_mountpoint);
6667
line_mountpoint = nullptr;
6768
}
6869

69-
ALLOW_C_FUNCTION(::free, ::free(line_filesystem);)
70+
permit_forbidden_function::free(line_filesystem);
7071

7172
return line_mountpoint;
7273
}
@@ -90,14 +91,14 @@ void ZMountPoint::get_mountpoints(const char* filesystem, ZArray<char*>* mountpo
9091
}
9192

9293
// readline will return malloced memory. Need raw ::free, not os::free.
93-
ALLOW_C_FUNCTION(::free, ::free(line);)
94+
permit_forbidden_function::free(line);
9495
fclose(fd);
9596
}
9697

9798
void ZMountPoint::free_mountpoints(ZArray<char*>* mountpoints) const {
9899
ZArrayIterator<char*> iter(mountpoints);
99100
for (char* mountpoint; iter.next(&mountpoint);) {
100-
ALLOW_C_FUNCTION(::free, ::free(mountpoint);) // *not* os::free
101+
permit_forbidden_function::free(mountpoint); // *not* os::free
101102
}
102103
mountpoints->clear();
103104
}

src/hotspot/os/linux/mallocInfoDcmd.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "os_linux.hpp"
2727
#include "utilities/globalDefinitions.hpp"
2828
#include "utilities/ostream.hpp"
29+
#include "utilities/permitForbiddenFunctions.hpp"
2930

3031
#include <malloc.h>
3132

@@ -35,15 +36,15 @@ void MallocInfoDcmd::execute(DCmdSource source, TRAPS) {
3536
#ifdef __GLIBC__
3637
char* buf;
3738
size_t size;
38-
ALLOW_C_FUNCTION(::open_memstream, FILE* stream = ::open_memstream(&buf, &size);)
39+
FILE* stream = ::open_memstream(&buf, &size);
3940
if (stream == nullptr) {
4041
_output->print_cr("Error: Could not call malloc_info(3)");
4142
return;
4243
}
4344

4445
int err = os::Linux::malloc_info(stream);
4546
if (err == 0) {
46-
ALLOW_C_FUNCTION(::fflush, fflush(stream);)
47+
fflush(stream);
4748
_output->print_raw(buf);
4849
_output->cr();
4950
} else if (err == -1) {
@@ -53,8 +54,8 @@ void MallocInfoDcmd::execute(DCmdSource source, TRAPS) {
5354
} else {
5455
ShouldNotReachHere();
5556
}
56-
ALLOW_C_FUNCTION(::fclose, ::fclose(stream);)
57-
ALLOW_C_FUNCTION(::free, ::free(buf);)
57+
::fclose(stream);
58+
permit_forbidden_function::free(buf);
5859
#else
5960
_output->print_cr(malloc_info_unavailable);
6061
#endif // __GLIBC__
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
25+
#ifndef OS_POSIX_FORBIDDENFUNCTIONS_POSIX_HPP
26+
#define OS_POSIX_FORBIDDENFUNCTIONS_POSIX_HPP
27+
28+
#include "utilities/compilerWarnings.hpp"
29+
30+
// For types used in the signatures.
31+
#include <stddef.h>
32+
33+
// Workaround for noreturn functions: _exit - see the clang
34+
// definition of FORBIDDEN_FUNCTION_NORETURN_ATTRIBUTE.
35+
#ifdef __clang__
36+
#include <unistd.h>
37+
#endif
38+
39+
// If needed, add os::strndup and use that instead.
40+
FORBID_C_FUNCTION(char* strndup(const char*, size_t), "don't use");
41+
42+
// These are unimplementable for Windows, and they aren't useful for a
43+
// POSIX implementation of NMT either.
44+
// https://stackoverflow.com/questions/62962839/stdaligned-alloc-missing-from-visual-studio-2019
45+
FORBID_C_FUNCTION(int posix_memalign(void**, size_t, size_t), "don't use");
46+
FORBID_C_FUNCTION(void* aligned_alloc(size_t, size_t), "don't use");
47+
48+
// realpath with a null second argument mallocs a string for the result.
49+
// With a non-null second argument, there is a risk of buffer overrun.
50+
PRAGMA_DIAG_PUSH
51+
FORBIDDEN_FUNCTION_IGNORE_CLANG_FORTIFY_WARNING
52+
FORBID_C_FUNCTION(char* realpath(const char*, char*), "use os::realpath");
53+
PRAGMA_DIAG_POP
54+
55+
// Returns a malloc'ed string.
56+
FORBID_C_FUNCTION(char* get_current_dir_name(), "use os::get_current_directory");
57+
58+
// Problematic API that should never be used.
59+
FORBID_C_FUNCTION(char* getwd(char*), "use os::get_current_directory");
60+
61+
// BSD utility that is subtly different from realloc.
62+
FORBID_C_FUNCTION(void* reallocf(void*, size_t), "use os::realloc");
63+
64+
#endif // OS_POSIX_FORBIDDENFUNCTIONS_POSIX_HPP

0 commit comments

Comments
 (0)