Skip to content

Give objects of empty classes storage in the generated C [GH #560] - #570

Open
lexx42xxel wants to merge 2 commits into
edgcpp:mainfrom
lexx42xxel:fix/empty-class-object-storage
Open

lexx42xxel wants to merge 2 commits into
edgcpp:mainfrom
lexx42xxel:fix/empty-class-object-storage

Conversation

@lexx42xxel

Copy link
Copy Markdown

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-overflow on main; no report with the fix or with g++.

Checklist

  • Open issue: not yet; reported in triage discussion Objects of empty classes have size zero in generated C (memset overwrites neighbours) #560, per CONTRIBUTING.
    Commits are tagged [GH #560] with the discussion number; happy to retag once an issue is opened.
  • Commits are tagged [GH #560].
  • Design and alternatives are described above.
  • No measurable compile-time/memory impact expected (localized lowering change); see the bench job.
  • Changes are localized (src/c_gen_be.c, src/il_to_str.c).
  • EDG style; comments updated.
  • Changes entry: src/pending_changes/gh-560-empty-class-object-storage.txt.
  • Tests fail before and pass after: empty-class-object-storage.
  • 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.

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>
@lexx42xxel
lexx42xxel requested a review from a team November 20, 2026 07:48
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 123522f and 6df76dd.

Files ignored due to path filters: 4

  • tests/expectations/edg_x86_32/edg.log is excluded by !**/*.log
  • tests/expectations/edg_x86_32_cp/edg.log is excluded by !**/*.log
  • tests/expectations/edg_x86_64/edg.log is excluded by !**/*.log
  • tests/expectations/edg_x86_64/imported.log is excluded by !**/*.log
Files selected for processing: 14

  • src/c_gen_be.c
  • src/il_to_str.c
  • src/pending_changes/gh-560-empty-class-object-storage.txt
  • tests/tests/edg/ccs/coroutines/.gnats20948-paramcopy.rto/edg_x86_64_cp.1.1.txt
  • tests/tests/edg/cmeerw/.gnats-26621-run.rto/default.1.1.txt
  • tests/tests/edg/cmeerw/.gnats-26621-run.rto/edg_x86_32.1.1.txt
  • tests/tests/edg/cmeerw/.gnats-26621-run.rto/edg_x86_32_cp.1.1.txt
  • tests/tests/imported/gnu/cpp/coroutines/.pr106188.rto/default.1.1.txt
  • tests/tests/imported/gnu/cpp/coroutines/.pr106188.rto/edg_x86_32.1.1.txt
  • tests/tests/imported/gnu/cpp/coroutines/torture/.pr115905.rto/default.1.1.txt
  • tests/tests/imported/gnu/cpp/coroutines/torture/.pr115905.rto/edg_x86_32.1.1.txt
  • tests/tests/regressions/.empty-class-object-storage.rto/default.1.1.txt
  • tests/tests/regressions/.empty-class-object-storage.rto/default.1.2.txt
  • tests/tests/regressions/empty-class-object-storage.sft.cpp
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 47dd459ecf95:

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 47dd459ecf95:

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.
@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 6df76dd09fb1:

CI success.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants