Skip to content
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

8286562: GCC 12 reports some compiler warnings #8646

Closed
wants to merge 14 commits into from

Conversation

YaSuenag
Copy link
Member

@YaSuenag YaSuenag commented May 11, 2022

I saw some compiler warnings when I tried to build OpenJDK with GCC 12.0.1 on Fedora 36.
As you can see, the warnings spreads several areas. Let me know if I should separate them by area.

  • -Wstringop-overflow
    • src/hotspot/share/oops/array.hpp
    • src/hotspot/share/jfr/recorder/checkpoint/types/traceid/jfrTraceIdBits.inline.hpp
In member function 'void Array<T>::at_put(int, const T&) [with T = unsigned char]',
    inlined from 'void ConstantPool::tag_at_put(int, jbyte)' at /home/ysuenaga/github-forked/jdk/src/hotspot/share/oops/constantPool.hpp:126:64,
    inlined from 'void ConstantPool::method_at_put(int, int, int)' at /home/ysuenaga/github-forked/jdk/src/hotspot/share/oops/constantPool.hpp:380:15,
    inlined from 'ConstantPool* BytecodeConstantPool::create_constant_pool(JavaThread*) const' at /home/ysuenaga/github-forked/jdk/src/hotspot/share/classfile/bytecodeAssembler.cpp:85:26:
/home/ysuenaga/github-forked/jdk/src/hotspot/share/oops/array.hpp:119:114: error: writing 1 byte into a region of size 0 [-Werror=stringop-overflow=]
  119 | void at_put(const int i, const T& x) { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); _data[i] = x; }
      | ~~~~~~~~~^~~

In file included from /home/ysuenaga/github-forked/jdk/src/hotspot/share/jfr/recorder/checkpoint/types/traceid/jfrTraceIdLoadBarrier.inline.hpp:33,
                 from /home/ysuenaga/github-forked/jdk/src/hotspot/share/jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp:30,
                 from /home/ysuenaga/github-forked/jdk/src/hotspot/share/jfr/support/jfrJdkJfrEvent.cpp:30:
In function 'void set_form(jbyte, jbyte*) [with jbyte (* op)(jbyte, jbyte) = traceid_or]',
    inlined from 'void set(jbyte, jbyte*)' at /home/ysuenaga/github-forked/jdk/src/hotspot/share/jfr/recorder/checkpoint/types/traceid/jfrTraceIdBits.inline.hpp:129:23,
    inlined from 'static void JfrTraceIdBits::store(jbyte, const T*) [with T = Klass]' at /home/ysuenaga/github-forked/jdk/src/hotspot/share/jfr/recorder/checkpoint/types/traceid/jfrTraceIdBits.inline.hpp:135:6,
    inlined from 'static void JfrTraceId::tag_as_jdk_jfr_event(const Klass*)' at /home/ysuenaga/github-forked/jdk/src/hotspot/share/jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp:106:3,
    inlined from 'static void JdkJfrEvent::tag_as(const Klass*)' at /home/ysuenaga/github-forked/jdk/src/hotspot/share/jfr/support/jfrJdkJfrEvent.cpp:176:35:
/home/ysuenaga/github-forked/jdk/src/hotspot/share/jfr/recorder/checkpoint/types/traceid/jfrTraceIdBits.inline.hpp:99:9: error: writing 1 byte into a region of size 0 [-Werror=stringop-overflow=]
   99 | *dest = op(bits, *dest);
      | ~~~~~~^~~~~~~~~~~~~~~~~
  • -Wformat-overflow
    • src/hotspot/share/opto/memnode.cpp
/home/ysuenaga/github-forked/jdk/src/hotspot/share/opto/memnode.cpp: In member function 'Node* LoadNode::eliminate_autobox(PhaseIterGVN*)':
/home/ysuenaga/github-forked/jdk/src/hotspot/share/opto/memnode.cpp:1409:53: error: '%s' directive argument is null [-Werror=format-overflow=]
 1409 | bt == T_INT || bt == T_LONG, "wrong type = %s", type2name(bt));
      | ^~~~~~~~~~~~~~~~~
  • -Wformat-truncation
    • src/java.base/unix/native/libjli/java_md_common.c
/home/ysuenaga/github-forked/jdk/src/java.base/unix/native/libjli/java_md_common.c: In function 'Resolve':
/home/ysuenaga/github-forked/jdk/src/java.base/unix/native/libjli/java_md_common.c:132:43: error: '%s' directive output may be truncated writing up to 4095 bytes into a region of size between 2 and 4097 [-Werror=format-truncation=]
  132 | JLI_Snprintf(name, sizeof(name), "%s%c%s", indir, FILE_SEPARATOR, cmd);
      | ^~
  • -Waddress
    • src/java.base/share/native/libjli/java.c
/home/ysuenaga/github-forked/jdk/src/java.base/share/native/libjli/java.c:1629:35: error: the comparison will always evaluate as 'false' for the pointer operand in 'arg + 2' must not be NULL [-Werror=address]
 1629 | *nargv++ = ((arg + 2) == NULL) ? NULL : JLI_StringDup(arg + 2);
      |
  • -Wuse-after-free
    • src/java.base/share/native/libjli/parse_manifest.c
    • src/jdk.jpackage/linux/native/applauncher/LinuxPackage.c
    • src/java.desktop/share/native/libharfbuzz/hb-font.cc
/home/ysuenaga/github-forked/jdk/src/java.base/share/native/libjli/parse_manifest.c:292:34: error: pointer 'endpos' used after 'free' [-Werror=use-after-free]
  292 | pos = flen - (endpos - cp);
      | ~~~~~~~~^~~~~

/home/ysuenaga/github-forked/jdk/src/jdk.jpackage/linux/native/applauncher/LinuxPackage.c:180:63: error: pointer 'strBufBegin' may be used after 'realloc' [-Werror=use-after-free]
  180 | strBufNextChar = strNewBufBegin + (strBufNextChar - strBufBegin);
      | ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~

/home/ysuenaga/github-forked/jdk/src/java.desktop/share/native/libharfbuzz/hb-font.cc:2286:12: error: pointer 'trampoline' used after 'void free(void*)' [-Werror=use-after-free]
 2286 | closure->ref_count++;
      | ~~~~~~~~~^~~~~~~~~

Most of warnings can be ignored.
HarfBuzz is a third party library, so I think it is reasonable to disable warnings in Makefile.

However warnings for libjli seem to be bugs. Let me know if they should be filed as another bugs.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed (2 reviews required, with at least 1 Reviewer, 1 Author)

Issue

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/8646/head:pull/8646
$ git checkout pull/8646

Update a local copy of the PR:
$ git checkout pull/8646
$ git pull https://git.openjdk.java.net/jdk pull/8646/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 8646

View PR using the GUI difftool:
$ git pr show -t 8646

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/8646.diff

@bridgekeeper
Copy link

bridgekeeper bot commented May 11, 2022

👋 Welcome back ysuenaga! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label May 11, 2022
@openjdk
Copy link

openjdk bot commented May 11, 2022

@YaSuenag The following labels will be automatically applied to this pull request:

  • build
  • client
  • core-libs
  • hotspot

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added build build-dev@openjdk.org hotspot hotspot-dev@openjdk.org client client-libs-dev@openjdk.org core-libs core-libs-dev@openjdk.org labels May 11, 2022
@mlbridge
Copy link

mlbridge bot commented May 11, 2022

Copy link
Member

@magicus magicus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The harfbuzz disabled warning looks good, so build changes are approved. You'll still need approval for the rest of the changes.

While it's not my place really to say about the code changes, I think hiding the warnings with pragmas like this is the least attractive option. But if the code owners are okay with it...

@magicus
Copy link
Member

magicus commented May 11, 2022

/reviewers 2

@openjdk
Copy link

openjdk bot commented May 11, 2022

@YaSuenag This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8286562: GCC 12 reports some compiler warnings

Reviewed-by: ihse, kbarrett, prr

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 83 new commits pushed to the master branch:

  • 7cb368b: 8286709: (fc) FileChannel/FileChannelImpl cleanup
  • 7eb1559: 8286045: Use ForceGC for cleaner test cases
  • e44465d: 8287336: GHA: Workflows break on patch versions
  • c10749a: 8287187: Utilize HashMap.newHashMap() in CLDRConverter
  • f235955: 8287246: DSAKeyValue should check for missing params instead of relying on KeyFactory provider
  • f58c9a6: 8287244: Add bound check in indexed memory access var handle
  • f710393: 8257810: Only First page are printed in JTable.scrollRectToVisible
  • 704b9a6: 8287107: CgroupSubsystemFactory.setCgroupV2Path asserts with freezer controller
  • 94811c0: 8286960: Test serviceability/jvmti/vthread/SuspendResume2 crashed: missing ThreadsListHandle in calling context
  • 3d6d7b7: 8287338: tools/javac/api/snippets/TestJavaxToolsSnippets.java failing tier1 on all platforms
  • ... and 73 more: https://git.openjdk.java.net/jdk/compare/c156bcc599534ae989bc9cbd001e7c150da8096c...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label May 11, 2022
@openjdk
Copy link

openjdk bot commented May 11, 2022

@magicus
The total number of required reviews for this PR (including the jcheck configuration and the last /reviewers command) is now set to 2 (with 1 of role reviewers, 1 of role authors).

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label May 11, 2022
@YaSuenag
Copy link
Member Author

Thanks @magicus for your review!

While it's not my place really to say about the code changes, I think hiding the warnings with pragmas like this is the least attractive option. But if the code owners are okay with it...

Agree, so I fixed bugs which were found out by compiler warnings in this PR - they are in libjli.
I think we can ignore the others because they are already checked in other methods (e.g. assert), or due to structure of Array class which has payload in _data[1] (and it is also checked in assert).

@YaSuenag
Copy link
Member Author

It is better to add pragma to Array::at_put() in src/hotspot/share/oops/array.hpp , but I couldn't suppress warnings. So I added pragmas to its caller - bytecodeAssembler.cpp, classFileParser.cpp, symbolTable.cpp .

JLI_Snprintf(name, sizeof(name), "%s%c%s", indir, FILE_SEPARATOR, cmd);
#pragma GCC diagnostic pop
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just replace this code rather than putting pragmas here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried several patterns, but I couldn't find out a solution other than pragmas. Do you have any ideas?

For example:

    if ((JLI_StrLen(indir) + JLI_StrLen(cmd) + 2) < sizeof(name)) {
      JLI_Snprintf(name, sizeof(name), "%s%c%s", indir, FILE_SEPARATOR, cmd);
    } else {
      return 0;
    }

Compiler warnings:

/home/ysuenaga/github-forked/jdk/src/java.base/unix/native/libjli/java_md_common.c:135:45: error: '%s' directive output may be truncated writing up to 4094 bytes into a region of size between 2 and 4096 [-Werror=format-truncation=]
  135 |       JLI_Snprintf(name, sizeof(name), "%s%c%s", indir, FILE_SEPARATOR, cmd);
      |                                             ^~
In file included from /usr/include/stdio.h:894,
                 from /home/ysuenaga/github-forked/jdk/src/java.base/share/native/libjli/java.h:29,
                 from /home/ysuenaga/github-forked/jdk/src/java.base/unix/native/libjli/java_md_common.c:26:
In function 'snprintf',
    inlined from 'Resolve' at /home/ysuenaga/github-forked/jdk/src/java.base/unix/native/libjli/java_md_common.c:135:7:
/usr/include/bits/stdio2.h:71:10: note: '__builtin___snprintf_chk' output between 2 and 8190 bytes into a destination of size 4097
   71 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |                                    __va_arg_pack ());

@@ -459,7 +459,7 @@ else

HARFBUZZ_DISABLED_WARNINGS_gcc := type-limits missing-field-initializers strict-aliasing
HARFBUZZ_DISABLED_WARNINGS_CXX_gcc := reorder delete-non-virtual-dtor strict-overflow \
maybe-uninitialized class-memaccess unused-result extra
maybe-uninitialized class-memaccess unused-result extra use-after-free

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Globally disabling use-after-free warnings for this package seems really
questionable. If these are problems in the code, just suppressing the warning
and leaving the problems to bite us seems like a bad idea. And the problems
ought to be reported upstream to the HarfBuzz folks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what the actual warning is getting at .. can anyone explain it ?

FWIW the code is still the same in upstream harfbuzz
https://github.com/harfbuzz/harfbuzz/blob/main/src/hb-font.cc

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pasted a part of warning messages to description of this PR, all of messages are here:

In function 'void trampoline_reference(hb_trampoline_closure_t*)',
    inlined from 'void hb_font_funcs_set_glyph_func(hb_font_funcs_t*, hb_font_get_glyph_func_t, void*, hb_destroy_func_t)' at /home/ysuenaga/github-forked/jdk/src/java.desktop/share/native/libharfbuzz/hb-font.cc:2368:24:
/home/ysuenaga/github-forked/jdk/src/java.desktop/share/native/libharfbuzz/hb-font.cc:2286:12: error: pointer 'trampoline' used after 'void free(void*)' [-Werror=use-after-free]
 2286 |   closure->ref_count++;
      |   ~~~~~~~~~^~~~~~~~~
In function 'void trampoline_destroy(void*)',
    inlined from 'void trampoline_destroy(void*)' at /home/ysuenaga/github-forked/jdk/src/java.desktop/share/native/libharfbuzz/hb-font.cc:2290:1,
    inlined from 'void hb_font_funcs_set_nominal_glyph_func(hb_font_funcs_t*, hb_font_get_nominal_glyph_func_t, void*, hb_destroy_func_t)' at /home/ysuenaga/github-forked/jdk/src/java.desktop/share/native/libharfbuzz/hb-font.cc:733:1,
    inlined from 'void hb_font_funcs_set_glyph_func(hb_font_funcs_t*, hb_font_get_glyph_func_t, void*, hb_destroy_func_t)' at /home/ysuenaga/github-forked/jdk/src/java.desktop/share/native/libharfbuzz/hb-font.cc:2363:40:
/home/ysuenaga/github-forked/jdk/src/java.desktop/share/native/libharfbuzz/hb-font.cc:2299:8: note: call to 'void free(void*)' here
 2299 |   free (closure);
      |   ~~~~~^~~~~~~~~

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So upstream say it is not a problem since the analysis overlooks that it would not reach that free if it were immutable because of a previous check. I guess we disable the false positive warning for now.

PRAGMA_DISABLE_GCC_WARNING("-Wformat-overflow")

#define PRAGMA_STRINGOP_OVERFLOW_IGNORED \
PRAGMA_DISABLE_GCC_WARNING("-Wstringop-overflow")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the reported problems with format/stringop-overflow real? Or are they
false positives? If they are real then I'm not going to approve ignoring them,
unless there is some urgent reason and there are followup bug reports for
fixing them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all of warnings in HotSpot are false-positives, so I added new pragmas to compilerWarnings.hpp, and use it in HotSpot code.

JLI_Snprintf(name, sizeof(name), "%s%c%s", indir, FILE_SEPARATOR, cmd);
#pragma GCC diagnostic pop

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to just call JLI_Snprintf without the precheck and check the result to determine whether it was successful or was truncated? I think that kind of check is supposed to make gcc's truncation checker happy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The warning has gone when using return value from JLI_Snprintf() in new commit!

strBufNextChar = strNewBufBegin + (strBufNextChar - strBufBegin);
#if defined(__GNUC__) && __GNUC__ >= 12
#pragma GCC diagnostic pop
#endif

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than all this warning suppression cruft and the comment explaining why
it's okay, just compute the (strBufNextChar - strBufBegin) offset a few
lines earlier, before the realloc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did do that in new commit, and the warning has gone!

@@ -1626,7 +1626,7 @@ TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***parg
for (i = 0; i < jargc; i++) {
const char *arg = jargv[i];
if (arg[0] == '-' && arg[1] == 'J') {
*nargv++ = ((arg + 2) == NULL) ? NULL : JLI_StringDup(arg + 2);
*nargv++ = (arg[2] == '\0') ? NULL : JLI_StringDup(arg + 2);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the client expects NULL strings in the result, or if the NULL value should be an empty string? If empty strings are okay, this would be simpler without the conditional, just dup from arg + 2 to the terminating byte (which might be immediate).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NULL affects as a loop stopper in ParseArguments() as following:

static jboolean
ParseArguments(int *pargc, char ***pargv,
               int *pmode, char **pwhat,
               int *pret, const char *jrepath)
{
    int argc = *pargc;
    char **argv = *pargv;
    int mode = LM_UNKNOWN;
    char *arg;

    *pret = 0;

    while ((arg = *argv) != 0 && *arg == '-') {

But I'm not sure it is valid, I think it might be discussed as another issue.

@mlbridge
Copy link

mlbridge bot commented May 11, 2022

Mailing list message from Magnus Ihse Bursie on build-dev:

On 2022-05-11 16:01, Kim Barrett wrote:

Globally disabling use-after-free warnings for this package seems really
questionable. If these are problems in the code, just suppressing the warning
and leaving the problems to bite us seems like a bad idea. And the problems
ought to be reported upstream to the HarfBuzz folks.

I agree that an upstream report would be nice, but it has been a
long-standing policy of not changing 3rd party code to fix warnings, but
instead suppress them. (Most of the time we compile with a much larger
set of warnings enabled than the upstream's own build does, so we
trigger warnings they never even see.)

/Magnus

@YaSuenag
Copy link
Member Author

Thanks for all to review this PR! I think we should separate this issue as following:

  • Suppress warnings
    • make/modules/java.desktop/lib/Awt2dLibraries.gmk
    • src/hotspot/share/classfile/bytecodeAssembler.cpp
    • src/hotspot/share/classfile/classFileParser.cpp
    • src/hotspot/share/jfr/recorder/checkpoint/types/traceid/jfrTraceIdBits.inline.hpp
    • src/hotspot/share/opto/memnode.cpp
    • src/hotspot/share/opto/type.cpp
    • src/hotspot/share/utilities/compilerWarnings.hpp
    • src/hotspot/share/utilities/compilerWarnings_gcc.hpp
    • src/java.base/unix/native/libjli/java_md_common.c
  • Bug fixes
    • src/java.base/share/native/libjli/java.c
    • src/java.base/share/native/libjli/parse_manifest.c
    • src/jdk.jpackage/linux/native/applauncher/LinuxPackage.c

I want to include the change of Awt2dLibraries.gmk (HarfBuzz) in this PR because it is 3rd party library.

I will separate in above if I do not hear any objections, and this issue (PR) handles "suppress warnings" only.

@magicus
Copy link
Member

magicus commented May 12, 2022

@YaSuenag From my PoV this sounds like a good suggestion.

@kimbarrett
Copy link

@YaSuenag From my PoV this sounds like a good suggestion.

+1

@prrace
Copy link
Contributor

prrace commented May 12, 2022

I will see what upstream thinks about the harfbuzz warning but in the mean time we can just disable it.

@YaSuenag
Copy link
Member Author

I've sent another review request for bug fixes as #8694 and #8696 , and I reverted change for them from this PR.
Could you review this PR to suppress warnings which we can ignore?

@openjdk openjdk bot added the ready Pull request is ready to be integrated label May 16, 2022
@YaSuenag
Copy link
Member Author

@magicus @prrace Thanks for your review!

Can I get the review from HotSpot folks? @kimbarrett

@kimbarrett
Copy link

Can I get the review from HotSpot folks? @kimbarrett

Already working on it. There are some I don't understand yet.

Copy link

@kimbarrett kimbarrett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions for code changes instead of warning suppression. And some warnings that I don't yet understand and don't think should be suppressed without more details or investigation.

src/hotspot/share/classfile/classFileParser.cpp Outdated Show resolved Hide resolved
*dest = op(bits, *dest);
PRAGMA_DIAG_POP

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see no stringop here. I'm still trying to understand what the compiler is complaining about.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess GCC cannot understand assert(dest != NULL immediately preceding it.

In file included from /home/ysuenaga/github-forked/jdk/src/hotspot/share/jfr/recorder/checkpoint/types/traceid/jfrTraceIdLoadBarrier.inline.hpp:33,
                 from /home/ysuenaga/github-forked/jdk/src/hotspot/share/jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp:30,
                 from /home/ysuenaga/github-forked/jdk/src/hotspot/share/jfr/support/jfrJdkJfrEvent.cpp:30:
In function 'void set_form(jbyte, jbyte*) [with jbyte (* op)(jbyte, jbyte) = traceid_or]',
    inlined from 'void set(jbyte, jbyte*)' at /home/ysuenaga/github-forked/jdk/src/hotspot/share/jfr/recorder/checkpoint/types/traceid/jfrTraceIdBits.inline.hpp:129:23,
    inlined from 'static void JfrTraceIdBits::store(jbyte, const T*) [with T = Klass]' at /home/ysuenaga/github-forked/jdk/src/hotspot/share/jfr/recorder/checkpoint/types/traceid/jfrTraceIdBits.inline.hpp:135:6,
    inlined from 'static void JfrTraceId::tag_as_jdk_jfr_event(const Klass*)' at /home/ysuenaga/github-forked/jdk/src/hotspot/share/jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp:106:3,
    inlined from 'static void JdkJfrEvent::tag_as(const Klass*)' at /home/ysuenaga/github-forked/jdk/src/hotspot/share/jfr/support/jfrJdkJfrEvent.cpp:176:35:
/home/ysuenaga/github-forked/jdk/src/hotspot/share/jfr/recorder/checkpoint/types/traceid/jfrTraceIdBits.inline.hpp:99:9: error: writing 1 byte into a region of size 0 [-Werror=stringop-overflow=]
   99 |   *dest = op(bits, *dest);
      |   ~~~~~~^~~~~~~~~~~~~~~~~

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this warning has anything to do with that NULL check. But I'm
still not understanding what it is warning about. The "region of size 0" part
of the warning message seems important, but I'm not (yet?) seeing how it could
be coming up with that. The code involved here is pretty contorted...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be GCC bug...

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99578

This issue is for integer literal, but Comment 29 mentions address calculation (e.g. NULL + offset) - it is similar the problem in jfrTraceIdBits.inline.hp because dest comes from low_addr() (addr + low_offset).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the similarity. That gcc bug is about literals used as addresses,
which get treated (suggested inappropriately) as NULL+offset, with NULL+offset
being a cause of warnings. I don't see that happening in our case. That is,
in our addr + low_offset, addr doesn't seem to be either a literal or NULL
that I can see.

It's hard for me to investigate this any further just by perusing the code, so
I'm in the process of getting set up to build with gcc12.x. That will let me
do some experiments. It may take me a few days to get to that point though.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent some time looking into this one. I agree there is a false positive
here, and there doesn't seem to be a better solution than suppressing the
warning. I would prefer the change below, rather than what's proposed. Also
eliminate the changes to utilities/compilerWarnings files. This is a very
gcc-specific issue; there's no reason to generalize the solution. The reason
for relocating the suppression is to be able to describe the issue in more
detail in a context where that description makes sense.

template <typename T>
inline void JfrTraceIdBits::store(jbyte bits, const T* ptr) {
  assert(ptr != NULL, "invariant");
  // gcc12 warns "writing 1 byte into a region of size 0" when T == Klass.
  // The warning seems to be a false positive.  And there is no warning for
  // other types that use the same mechanisms.  The warning also sometimes
  // goes away with minor code perturbations, such as replacing function calls
  // with equivalent code directly inlined.
  PRAGMA_DIAG_PUSH
  PRAGMA_DISABLE_GCC_WARNING("-Wstringop-overflow")
  set(bits, traceid_tag_byte(ptr));
  PRAGMA_DIAG_POP
}

src/hotspot/share/opto/memnode.cpp Outdated Show resolved Hide resolved
src/hotspot/share/opto/type.cpp Outdated Show resolved Hide resolved
src/hotspot/share/classfile/bytecodeAssembler.cpp Outdated Show resolved Hide resolved
@YaSuenag
Copy link
Member Author

In case of stringop-overflow errors (bytecodeAssembler.cpp, classFileParser.cpp, symbolTable.cpp) noted that offset of Array<T>::_data might be [-2147483648, -1].

/home/ysuenaga/github-forked/jdk/src/hotspot/share/oops/array.hpp:46:7: note: at offset [-2147483648, -1] into destination object 'Array<unsigned char>::_data' of size [0, 9223372036854775799]
   46 |   T   _data[1];                                // the array memory
      |       ^~~~~

The warning has gone with following patch to cast i to unsigned int. However I'm not sure it is the best solution because _data can be accessed with int index in other places, ando also it is natural to use int as array index. And also I think these warnings are false-positive.

diff --git a/src/hotspot/share/oops/array.hpp b/src/hotspot/share/oops/array.hpp
index 428c2e63384..ba146bb7813 100644
--- a/src/hotspot/share/oops/array.hpp
+++ b/src/hotspot/share/oops/array.hpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 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
@@ -113,7 +113,7 @@ protected:
   bool contains(const T& x) const      { return index_of(x) >= 0; }

   T    at(int i) const                 { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); return _data[i]; }
-  void at_put(const int i, const T& x) { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); _data[i] = x; }
+  void at_put(const int i, const T& x) { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); _data[static_cast<unsigned int>(i)] = x; }
   T*   adr_at(const int i)             { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); return &_data[i]; }
   int  find(const T& x)                { return index_of(x); }

I think it is better than disable stringop-overflow in cpp files if we cannot disable it in array.hpp (I tried it, but GCC does not seem to disable it with _Pragma in this case.).
@kimbarrett What do you think?

*dest = op(bits, *dest);
PRAGMA_DIAG_POP

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent some time looking into this one. I agree there is a false positive
here, and there doesn't seem to be a better solution than suppressing the
warning. I would prefer the change below, rather than what's proposed. Also
eliminate the changes to utilities/compilerWarnings files. This is a very
gcc-specific issue; there's no reason to generalize the solution. The reason
for relocating the suppression is to be able to describe the issue in more
detail in a context where that description makes sense.

template <typename T>
inline void JfrTraceIdBits::store(jbyte bits, const T* ptr) {
  assert(ptr != NULL, "invariant");
  // gcc12 warns "writing 1 byte into a region of size 0" when T == Klass.
  // The warning seems to be a false positive.  And there is no warning for
  // other types that use the same mechanisms.  The warning also sometimes
  // goes away with minor code perturbations, such as replacing function calls
  // with equivalent code directly inlined.
  PRAGMA_DIAG_PUSH
  PRAGMA_DISABLE_GCC_WARNING("-Wstringop-overflow")
  set(bits, traceid_tag_byte(ptr));
  PRAGMA_DIAG_POP
}

}
}

public:

// standard operations
int length() const { return _length; }
T* data() { return _data; }
T* data() const { return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(this) + base_offset_in_bytes()); }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the const-qualifier to the data() function and then implicitly
casting it away (by casting through intptr_t) is wrong. Either don't
const-qualify (and leave it to some future use that needs such to address
appropriately), or have two functions. Also, the line length is excessive.
So this:

T* data() {
  return reinterpret_cast<T*>(
    reinterpret_cast<char*>(this) + base_offset_in_bytes());
}

and optionally add this:

const T* data() const {
  return reinterpret_cast<const T*>(
    reinterpret_cast<const char*>(this) + base_offset_in_bytes());
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @kimbarrett !

I updated around stringop-overflow warning in jfrTraceIdBits.inline.hpp , and added two data() in Array class. They works fine on my GCC 12 on Fedora 36. Could you review again?

Copy link

@kimbarrett kimbarrett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly good, but I missed a problem with an earlier part of the change. Sorry I didn't notice sooner.

if ((JLI_StrLen(indir) + JLI_StrLen(cmd) + 1) > PATH_MAX) return 0;
JLI_Snprintf(name, sizeof(name), "%s%c%s", indir, FILE_SEPARATOR, cmd);
snprintf_result = JLI_Snprintf(name, sizeof(name), "%s%c%s", indir, FILE_SEPARATOR, cmd);
if ((snprintf_result < 0) && (snprintf_result >= (int)sizeof(name))) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be || rather than &&.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I fixed it in new commit.

snprintf_result = JLI_Snprintf(name, sizeof(name), "%s%c%s", indir, FILE_SEPARATOR, cmd);
if ((snprintf_result < 0) && (snprintf_result >= (int)sizeof(name))) {
return 0;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-existing: It seems odd that this returns 0 above and below, rather than returning NULL. I think there are one or two other places in this file that are similarly using literal 0 for a null pointer, though others are using NULL. Something to report and clean up separately from this change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering about that too.
I use NULL at L134, and have filed it as JDK-8287363. I will work for it after this issue.

@YaSuenag
Copy link
Member Author

/integrate

@openjdk
Copy link

openjdk bot commented May 28, 2022

Going to push as commit 410a25d.
Since your change was applied there have been 98 commits pushed to the master branch:

  • ed8e8ac: 8284400: Improve XPath exception handling
  • d3e781d: 8287223: C1: Inlining attempt through MH::invokeBasic() with null receiver
  • 0df4748: 8287463: JFR: Disable TestDevNull.java on Windows
  • 6520843: 8287003: InputStreamReader::read() can return zero despite writing a char in the buffer
  • 63eb0b7: 8282947: JFR: Dump on shutdown live-locks in some conditions
  • f2bc447: 8271406: Kitchensink failed with "assert(early->flag() == current->flag()) failed: Should be the same"
  • be93318: 8287292: Improve TransformKey to pack more kinds of transforms efficiently
  • 777f813: 8268422: Find a better way to select releases in "New API" page
  • 22e2067: 8282182: Document algorithm used to encode aarch64 logical immediate operands.
  • 37ecbb4: 8277420: Provide a way to copy the hyperlink to a doc element to the clipboard
  • ... and 88 more: https://git.openjdk.java.net/jdk/compare/c156bcc599534ae989bc9cbd001e7c150da8096c...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label May 28, 2022
@openjdk openjdk bot closed this May 28, 2022
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels May 28, 2022
@openjdk
Copy link

openjdk bot commented May 28, 2022

@YaSuenag Pushed as commit 410a25d.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@YaSuenag YaSuenag deleted the gcc12-warnings branch May 28, 2022 02:58
@mlbridge
Copy link

mlbridge bot commented May 28, 2022

Mailing list message from David Holmes on build-dev:

The new assertion in src/hotspot/share/utilities/globalDefinitions.hpp

inline const char* type2name(BasicType t) {
assert((uint)t < T_CONFLICT + 1, "invalid type");
return type2name_tab[t];
}

is failing with test

compiler/jvmci/errors/TestInvalidDebugInfo.java

I have filed:

https://bugs.openjdk.java.net/browse/JDK-8287491

The test will probably need ProblemListing as it is causing major noise
in our CI.

David

On 28/05/2022 12:12 pm, Yasumasa Suenaga wrote:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build build-dev@openjdk.org client client-libs-dev@openjdk.org core-libs core-libs-dev@openjdk.org hotspot hotspot-dev@openjdk.org integrated Pull request has been integrated
5 participants