Skip to content

Commit e0f2f4b

Browse files
Kim BarrettTheRealMDoerr
Kim Barrett
andcommitted
8313396: Portable implementation of FORBID_C_FUNCTION and ALLOW_C_FUNCTION
Co-authored-by: Martin Doerr <mdoerr@openjdk.org> Reviewed-by: coleenp, dholmes, jsjolen
1 parent b0c131e commit e0f2f4b

32 files changed

+528
-177
lines changed

src/hotspot/os/aix/libodm_aix.cpp

+2-1
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

+9-8
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>
@@ -55,7 +56,7 @@ class StringList {
5556
// Enlarge list. If oom, leave old list intact and return false.
5657
bool enlarge() {
5758
int cap2 = _cap + 64;
58-
char** l2 = (char**) ::realloc(_list, sizeof(char*) * cap2);
59+
char** l2 = (char**) permit_forbidden_function::realloc(_list, sizeof(char*) * cap2);
5960
if (!l2) {
6061
return false;
6162
}
@@ -73,7 +74,7 @@ class StringList {
7374
}
7475
}
7576
assert0(_cap > _num);
76-
char* s2 = ::strdup(s);
77+
char* s2 = permit_forbidden_function::strdup(s);
7778
if (!s2) {
7879
return nullptr;
7980
}
@@ -167,7 +168,7 @@ static void free_entry_list(loaded_module_t** start) {
167168
loaded_module_t* lm = *start;
168169
while (lm) {
169170
loaded_module_t* const lm2 = lm->next;
170-
::free(lm);
171+
permit_forbidden_function::free(lm);
171172
lm = lm2;
172173
}
173174
*start = nullptr;
@@ -190,7 +191,7 @@ static bool reload_table() {
190191
uint8_t* buffer = nullptr;
191192
size_t buflen = 1024;
192193
for (;;) {
193-
buffer = (uint8_t*) ::realloc(buffer, buflen);
194+
buffer = (uint8_t*) permit_forbidden_function::realloc(buffer, buflen);
194195
if (loadquery(L_GETINFO, buffer, buflen) == -1) {
195196
if (errno == ENOMEM) {
196197
buflen *= 2;
@@ -210,7 +211,7 @@ static bool reload_table() {
210211

211212
for (;;) {
212213

213-
loaded_module_t* lm = (loaded_module_t*) ::malloc(sizeof(loaded_module_t));
214+
loaded_module_t* lm = (loaded_module_t*) permit_forbidden_function::malloc(sizeof(loaded_module_t));
214215
if (!lm) {
215216
log_warning(os)("OOM.");
216217
goto cleanup;
@@ -226,7 +227,7 @@ static bool reload_table() {
226227
lm->path = g_stringlist.add(ldi->ldinfo_filename);
227228
if (!lm->path) {
228229
log_warning(os)("OOM.");
229-
free(lm);
230+
permit_forbidden_function::free(lm);
230231
goto cleanup;
231232
}
232233

@@ -248,7 +249,7 @@ static bool reload_table() {
248249
lm->member = g_stringlist.add(p_mbr_name);
249250
if (!lm->member) {
250251
log_warning(os)("OOM.");
251-
free(lm);
252+
permit_forbidden_function::free(lm);
252253
goto cleanup;
253254
}
254255
} else {
@@ -296,7 +297,7 @@ static bool reload_table() {
296297
free_entry_list(&new_list);
297298
}
298299

299-
::free(buffer);
300+
permit_forbidden_function::free(buffer);
300301

301302
return rc;
302303

src/hotspot/os/aix/os_aix.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#include "utilities/defaultStream.hpp"
7575
#include "utilities/events.hpp"
7676
#include "utilities/growableArray.hpp"
77+
#include "utilities/permitForbiddenFunctions.hpp"
7778
#include "utilities/vmError.hpp"
7879
#if INCLUDE_JFR
7980
#include "jfr/support/jfrNativeLibraryLoadEvent.hpp"
@@ -364,9 +365,9 @@ static void query_multipage_support() {
364365
// or by environment variable LDR_CNTRL (suboption DATAPSIZE). If none is given,
365366
// default should be 4K.
366367
{
367-
void* p = ::malloc(16*M);
368+
void* p = permit_forbidden_function::malloc(16*M);
368369
g_multipage_support.datapsize = os::Aix::query_pagesize(p);
369-
::free(p);
370+
permit_forbidden_function::free(p);
370371
}
371372

372373
// Query default shm page size (LDR_CNTRL SHMPSIZE).
@@ -1406,7 +1407,7 @@ static struct {
14061407
} vmem;
14071408

14081409
static void vmembk_add(char* addr, size_t size, size_t pagesize, int type) {
1409-
vmembk_t* p = (vmembk_t*) ::malloc(sizeof(vmembk_t));
1410+
vmembk_t* p = (vmembk_t*) permit_forbidden_function::malloc(sizeof(vmembk_t));
14101411
assert0(p);
14111412
if (p) {
14121413
MiscUtils::AutoCritSect lck(&vmem.cs);
@@ -1435,7 +1436,7 @@ static void vmembk_remove(vmembk_t* p0) {
14351436
for (vmembk_t** pp = &(vmem.first); *pp; pp = &((*pp)->next)) {
14361437
if (*pp == p0) {
14371438
*pp = p0->next;
1438-
::free(p0);
1439+
permit_forbidden_function::free(p0);
14391440
return;
14401441
}
14411442
}

src/hotspot/os/aix/porting_aix.cpp

+3-2
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

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 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
@@ -28,6 +28,8 @@
2828
#include "decoder_machO.hpp"
2929
#include "jvm.h"
3030
#include "memory/allocation.inline.hpp"
31+
#include "utilities/globalDefinitions.hpp"
32+
#include "utilities/permitForbiddenFunctions.hpp"
3133

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

src/hotspot/os/linux/decoder_linux.cpp

+5-4
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

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 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
@@ -29,6 +29,7 @@
2929
#include "runtime/globals.hpp"
3030
#include "runtime/os.hpp"
3131
#include "utilities/globalDefinitions.hpp"
32+
#include "utilities/permitForbiddenFunctions.hpp"
3233

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

70-
ALLOW_C_FUNCTION(::free, ::free(line_filesystem);)
71+
permit_forbidden_function::free(line_filesystem);
7172

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

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

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

src/hotspot/os/linux/mallocInfoDcmd.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 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 "os_linux.hpp"
2828
#include "utilities/globalDefinitions.hpp"
2929
#include "utilities/ostream.hpp"
30+
#include "utilities/permitForbiddenFunctions.hpp"
3031

3132
#include <malloc.h>
3233

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

4546
int err = os::Linux::malloc_info(stream);
4647
if (err == 0) {
47-
ALLOW_C_FUNCTION(::fflush, fflush(stream);)
48+
fflush(stream);
4849
_output->print_raw(buf);
4950
_output->cr();
5051
} else if (err == -1) {
@@ -54,8 +55,8 @@ void MallocInfoDcmd::execute(DCmdSource source, TRAPS) {
5455
} else {
5556
ShouldNotReachHere();
5657
}
57-
ALLOW_C_FUNCTION(::fclose, ::fclose(stream);)
58-
ALLOW_C_FUNCTION(::free, ::free(buf);)
58+
::fclose(stream);
59+
permit_forbidden_function::free(buf);
5960
#else
6061
_output->print_cr(malloc_info_unavailable);
6162
#endif // __GLIBC__
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
#include <stddef.h> // for size_t
31+
#include <unistd.h> // clang workaround for _exit - see FORBID macro.
32+
33+
// If needed, add os::strndup and use that instead.
34+
FORBID_C_FUNCTION(char* strndup(const char*, size_t), "don't use");
35+
36+
// These are unimplementable for Windows, and they aren't useful for a
37+
// POSIX implementation of NMT either.
38+
// https://stackoverflow.com/questions/62962839/stdaligned-alloc-missing-from-visual-studio-2019
39+
FORBID_C_FUNCTION(int posix_memalign(void**, size_t, size_t), "don't use");
40+
FORBID_C_FUNCTION(void* aligned_alloc(size_t, size_t), "don't use");
41+
42+
// realpath with a null second argument mallocs a string for the result.
43+
FORBID_C_FUNCTION(char* realpath(const char*, char*), "use os::realpath");
44+
45+
// Returns a malloc'ed string.
46+
FORBID_C_FUNCTION(char* get_current_dir_name(), "use os::get_current_directory");
47+
48+
// Problematic API that should never be used.
49+
FORBID_C_FUNCTION(char* getwd(char*), "use os::get_current_directory");
50+
51+
// BSD utility that is subtly different from realloc.
52+
FORBID_C_FUNCTION(void* reallocf(void*, size_t), "use os::realloc");
53+
54+
#endif // OS_POSIX_FORBIDDENFUNCTIONS_POSIX_HPP

0 commit comments

Comments
 (0)