-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8313396: Portable implementation of FORBID_C_FUNCTION and ALLOW_C_FUNCTION #22890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
993d465
e2a6324
46c098a
385c0b4
19d5162
08f2d34
77a8017
c478bda
b774f14
4dff1e4
000aca9
97a56ae
785b2cf
6f082c2
6d49abb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. | ||
| * Copyright (c) 2023, 2025, 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 | ||
|
|
@@ -27,6 +27,7 @@ | |
| #include "os_linux.hpp" | ||
| #include "utilities/globalDefinitions.hpp" | ||
| #include "utilities/ostream.hpp" | ||
| #include "utilities/permitForbiddenFunctions.hpp" | ||
|
|
||
| #include <malloc.h> | ||
|
|
||
|
|
@@ -36,15 +37,15 @@ void MallocInfoDcmd::execute(DCmdSource source, TRAPS) { | |
| #ifdef __GLIBC__ | ||
| char* buf; | ||
| size_t size; | ||
| ALLOW_C_FUNCTION(::open_memstream, FILE* stream = ::open_memstream(&buf, &size);) | ||
| FILE* stream = ::open_memstream(&buf, &size); | ||
| if (stream == nullptr) { | ||
| _output->print_cr("Error: Could not call malloc_info(3)"); | ||
| return; | ||
| } | ||
|
|
||
| int err = os::Linux::malloc_info(stream); | ||
| if (err == 0) { | ||
| ALLOW_C_FUNCTION(::fflush, fflush(stream);) | ||
| fflush(stream); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that fflush was never forbidden, so the ALLOW_C_FUNCTION was not needed. |
||
| _output->print_raw(buf); | ||
| _output->cr(); | ||
| } else if (err == -1) { | ||
|
|
@@ -54,8 +55,8 @@ void MallocInfoDcmd::execute(DCmdSource source, TRAPS) { | |
| } else { | ||
| ShouldNotReachHere(); | ||
| } | ||
| ALLOW_C_FUNCTION(::fclose, ::fclose(stream);) | ||
| ALLOW_C_FUNCTION(::free, ::free(buf);) | ||
| ::fclose(stream); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that fclose was never forbidden, so the ALLOW_C_FUNCTION was not needed. |
||
| permit_forbidden_function::free(buf); | ||
| #else | ||
| _output->print_cr(malloc_info_unavailable); | ||
| #endif // __GLIBC__ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright (c) 2024, 2025, 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 | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| * | ||
| */ | ||
|
|
||
| #ifndef OS_POSIX_FORBIDDENFUNCTIONS_POSIX_HPP | ||
| #define OS_POSIX_FORBIDDENFUNCTIONS_POSIX_HPP | ||
|
|
||
| #include "utilities/compilerWarnings.hpp" | ||
|
|
||
| #include <stddef.h> // for size_t | ||
| #include <unistd.h> // clang workaround for _exit - see FORBID macro. | ||
|
|
||
| // If needed, add os::strndup and use that instead. | ||
| FORBID_C_FUNCTION(char* strndup(const char*, size_t), "don't use"); | ||
|
|
||
| // These are unimplementable for Windows, and they aren't useful for a | ||
| // POSIX implementation of NMT either. | ||
| // https://stackoverflow.com/questions/62962839/stdaligned-alloc-missing-from-visual-studio-2019 | ||
| FORBID_C_FUNCTION(int posix_memalign(void**, size_t, size_t), "don't use"); | ||
| FORBID_C_FUNCTION(void* aligned_alloc(size_t, size_t), "don't use"); | ||
|
|
||
| // realpath with a null second argument mallocs a string for the result. | ||
| FORBID_C_FUNCTION(char* realpath(const char*, char*), "use os::realpath"); | ||
|
|
||
| // Returns a malloc'ed string. | ||
| FORBID_C_FUNCTION(char* get_current_dir_name(), "use os::get_current_directory"); | ||
|
|
||
| // Problematic API that should never be used. | ||
| FORBID_C_FUNCTION(char* getwd(char*), "use os::get_current_directory"); | ||
|
|
||
| // BSD utility that is subtly different from realloc. | ||
| FORBID_C_FUNCTION(void* reallocf(void*, size_t), "use os::realloc"); | ||
|
|
||
| #endif // OS_POSIX_FORBIDDENFUNCTIONS_POSIX_HPP |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that open_memstream was never forbidden, so the ALLOW_C_FUNCTION was not needed.