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

Silence false-positive warning for malformed placement new on GCC 11.1 #52123

Merged
merged 1 commit into from
Sep 15, 2021

Conversation

lightspot21
Copy link
Contributor

@lightspot21 lightspot21 commented Aug 26, 2021

This closes #52119. Right now I am moving the offset to its own variable in order to calm down GCC's warning. Better ideas always welcome.

@lightspot21
Copy link
Contributor Author

lightspot21 commented Aug 26, 2021

I did quite a bit of research on why this warning pops up here.

More specifically, it's because GCC thinks that mem_new is the beginning of a new allocation, but suddenly there is a subtraction, causing it to think that we want to place the SafeNumeric outside the allocation. This seems to be the case, as GCC complains only on subtraction, not addition.

GCC doesn't know though (and cannot know, because the allocation is dynamic) that in this case, the pointer returned by Memory::alloc_static() is already offset by 16 bytes, so writing "behind" it is actually fine, as the memory needed is already allocated. (IMHO it's kinda weird though, returning a pointer in the middle of an allocation - maybe this can be refactored somehow?)

Assigning to a new variable fools the check, because the compiler thinks that this is the beginning of a separate array, and no such subtraction is done.

Therefore, the warning seems to be a false positive logic-wise, but is valid because it's not immediately clear to an outside observer that the subtraction is OK. I propose that we stay with the variables along with a comment, as they both satisfy GCC and make it clear that the subtraction is fine and done for a purpose.

Any better ideas welcome. @Calinou @godotengine/core ?

@lightspot21 lightspot21 changed the title WIP: Fix placement new warning on GCC 11.1 Fix placement new warning on GCC 11.1 Aug 26, 2021
@lightspot21 lightspot21 force-pushed the fix-placement-new-warning branch 2 times, most recently from e02a094 to f25ccc7 Compare August 30, 2021 22:34
@lightspot21 lightspot21 force-pushed the fix-placement-new-warning branch from f25ccc7 to be887bf Compare August 31, 2021 20:11
@lightspot21 lightspot21 removed the request for review from a team August 31, 2021 20:15
@lightspot21 lightspot21 force-pushed the fix-placement-new-warning branch from be887bf to fde8532 Compare August 31, 2021 20:18
@lightspot21
Copy link
Contributor Author

@godotengine/core ?

@lightspot21 lightspot21 force-pushed the fix-placement-new-warning branch from fde8532 to a4d01a9 Compare September 6, 2021 11:02
@lightspot21
Copy link
Contributor Author

@nathanfranke please let me know if I changed this correctly

@nathanfranke
Copy link
Contributor

@nathanfranke please let me know if I changed this correctly

Yes, looks fine. Although maybe another core contributor can look at it, since I think there are some exceptions or issues that can arise from using references.

@akien-mga akien-mga requested review from a team and RandomShaper September 14, 2021 08:10
@RandomShaper
Copy link
Member

Considering that the reason for those interim pointers will most likely be no apparent to a future reader of that code, I'd suggest adding a comment about their purpose. However, if we take into account the size of the base patch plus comments and also that the goal is to silence a really broken warning, I'd suggest going with this:

  • Around the top of the file:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wplacement-new"
  • Around the bottom of the file:
#pragma GCC diagnostic pop

Anyone expected to maintain this file will know how to understand it.

@lightspot21 lightspot21 force-pushed the fix-placement-new-warning branch from a4d01a9 to fa25d0e Compare September 14, 2021 16:15
@lightspot21
Copy link
Contributor Author

@RandomShaper done

@lightspot21 lightspot21 force-pushed the fix-placement-new-warning branch 3 times, most recently from 8bb4e4e to 83a3be0 Compare September 14, 2021 17:37
core/templates/cowdata.h Outdated Show resolved Hide resolved
core/templates/cowdata.h Outdated Show resolved Hide resolved
@akien-mga
Copy link
Member

Might be worth amending the commit title as we're no longer fixing the warning but silencing it as a false positive.

On latest (11.1 as of this commit) GCC, the following warning is
continuously issued during build:
warning: placement new constructing an object of type
'SafeNumeric<unsigned int>' and size '4' in a region of type
'uint32_t*' {aka 'unsigned int*'} and size '0' [-Wplacement-new=]

This happens because on 98ceb60 the new operator override used
was dropped and replaced with standard placement new. GCC sees the
subtraction from the pointer and complains as it thinks that the
SafeNumeric is placed outside an allocation, not knowing that the
address requested is already inside one.

After suggestions, the false positive is silenced, with no other
changes.
@lightspot21 lightspot21 force-pushed the fix-placement-new-warning branch from 83a3be0 to abef2b7 Compare September 14, 2021 21:07
@lightspot21 lightspot21 changed the title Fix placement new warning on GCC 11.1 Silence false-positive warning for malformed placement new on GCC 11.1 Sep 14, 2021
@lightspot21
Copy link
Contributor Author

Might be worth amending the commit title as we're no longer fixing the warning but silencing it as a false positive.

@akien-mga done and done

@akien-mga akien-mga merged commit 3705ad7 into godotengine:master Sep 15, 2021
@akien-mga
Copy link
Member

Thanks!

@akien-mga akien-mga added cherrypick:3.3 cherrypick:3.x Considered for cherry-picking into a future 3.x release and removed cherrypick:3.x Considered for cherry-picking into a future 3.x release cherrypick:3.3 labels Sep 15, 2021
@lightspot21 lightspot21 deleted the fix-placement-new-warning branch September 15, 2021 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Placement new warnings generated under GCC 11.1
5 participants