Repository navigation
Give objects of empty classes storage in the generated C [GH #560] - #570
Open
lexx42xxel wants to merge 2 commits into
Open
lexx42xxel wants to merge 2 commits into
lexx42xxel wants to merge 2 commits into
Conversation
On targets where the C-generating back end uses an empty struct for an empty class (USE_EMPTY_STRUCT_IN_GENERATED_C, for g++ ABI compatibility), complete objects of the class had size zero in the generated C, although the class has size one in C++. Distinct objects could have the same address, and writing sizeof bytes to an object (e.g., with memset or memcpy) overwrote neighbouring objects. Declare variables of such a class, and arrays of it, as arrays of char of the C++ size, and refer to them through a cast of that storage. By-value parameters of such a class are still passed with size zero (as by g++); references to them use a local array of char declared at the start of the function body. The value need not be copied, as the class has no state. Address constants with an offset into an array of such a class (e.g., "&a[3]") were put out as subscripts or pointer arithmetic on the zero-size type, which yields the address of the array itself. Add the offset to a char pointer instead. The generated C for co_await is not supported, and the expected output of some coroutine tests (cmeerw/gnats-26621-run, ccs/coroutines/ gnats20948-paramcopy, and imported gnu coroutines/pr106188 and coroutines/torture/pr115905) records the errors from compiling it. Those errors change; update the recordings. The 32-bit configurations don't use empty structs, so their output is unchanged and gets recordings of its own. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 59 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
Collaborator
|
AcknowlEDG review results updated for
|
Collaborator
|
AcknowlEDG review results updated for
CI success. |
Following review feedback on a related pull request, pass the type size to write_unsigned_num without a cast: a_targ_size_t is already a_host_large_unsigned. This keeps each call on one line. No functional change.
Collaborator
|
AcknowlEDG review results updated for
CI success. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the problem reported in triage discussion #560.
Problem and fix
On targets where the C-generating back end uses an empty struct for an
empty class (USE_EMPTY_STRUCT_IN_GENERATED_C, for g++ ABI compatibility),
complete objects of the class had size zero in the generated C, although
the class has size one in C++. Distinct objects could have the same
address, and writing sizeof bytes to an object (e.g., with memset or
memcpy) overwrote neighbouring objects.
Declare variables of such a class, and arrays of it, as arrays of char of
the C++ size, and refer to them through a cast of that storage. By-value
parameters of such a class are still passed with size zero (as by g++);
references to them use a local array of char declared at the start of the
function body. The value need not be copied, as the class has no state.
Address constants with an offset into an array of such a class (e.g.,
"&a[3]") were put out as subscripts or pointer arithmetic on the
zero-size type, which yields the address of the array itself. Add the
offset to a char pointer instead.
The generated C for co_await is not supported, and the expected output of
some coroutine tests (cmeerw/gnats-26621-run, ccs/coroutines/
gnats20948-paramcopy, and imported gnu coroutines/pr106188 and
coroutines/torture/pr115905) records the errors from compiling it. Those
errors change; update the recordings. The 32-bit configurations don't use
empty structs, so their output is unchanged and gets recordings of its own.
The imported gnu torture/pr92384 test, which checks that empty-class
parameters passed by value have distinct addresses, now passes on the 64-bit
configurations; remove its expected FAIL from expectations/edg_x86_64.
Alternatives considered
Make the generated struct non-empty. Rejected: breaks g++ layout compatibility for empty bases and members; only complete objects (variables, arrays) get char storage, members and bases are unchanged. By-value parameters keep size zero as in g++.
Tests
New:
tests/tests/regressions/empty-class-object-storage.sft.cpp.Verified failing on main and passing with the fix (IA-64 and non-IA-64 builds); reproducer in the discussion.
AddressSanitizer: the discussion's reproducer reports
stack-buffer-overflowon main; no report with the fix or with g++.Checklist
Commits are tagged
[GH #560]with the discussion number; happy to retag once an issue is opened.[GH #560].src/pending_changes/gh-560-empty-class-object-storage.txt.edg-docker-test --posture=extensive(edg_x86_64, edg_x86_64_cp, edg_x86_32, edg_x86_32_cp): no regressions; recordings updated with-W.