Skip to content

Comments

Generated foreach-body functions: Fix index when NOT using size_t#21705

Merged
dlang-bot merged 1 commit intodlang:masterfrom
kinke:fix_foreach_dg_types
Aug 11, 2025
Merged

Generated foreach-body functions: Fix index when NOT using size_t#21705
dlang-bot merged 1 commit intodlang:masterfrom
kinke:fix_foreach_dg_types

Conversation

@kinke
Copy link
Contributor

@kinke kinke commented Aug 11, 2025

Similar to issue #21456, the druntime autodecoding helpers pass a pointer to the size_t array index. The generated foreach-body lambda used to load its index from that pointer, without verifying that the types match. So if the user uses an int index, it used to load the first 4 bytes, thus truncating to 32 bits on little-endian, but using the 32 upper bits for 64-bit big-endian targets. Solve this by adding a cast if needed.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @kinke! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

⚠️⚠️⚠️ Warnings ⚠️⚠️⚠️

  • In preparation for migrating from Bugzilla to GitHub Issues, the issue reference syntax has changed. Please add the word "Bugzilla" to issue references. For example, Fix Bugzilla Issue 12345 or Fix Bugzilla 12345.(Reminder: the edit needs to be done in the Git commit message, not the GitHub pull request.)

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#21705"

Similar to issue dlang#21456, the druntime autodecoding helpers pass a pointer
to the `size_t` array index. The generated foreach-body lambda used to
load its index from that pointer, without verifying that the types match.
So if the user uses an `int` index, it used to load the first 4 bytes,
thus truncating to 32 bits on little-endian, but using the 32 upper bits
for 64-bit big-endian targets. Solve this by adding a cast if needed.
@kinke kinke force-pushed the fix_foreach_dg_types branch from fb38d82 to 86a877d Compare August 11, 2025 15:01
@kinke
Copy link
Contributor Author

kinke commented Aug 11, 2025

FYI @ibuclaw - I assume you've had to work around this somewhere too, to fix this for big-endian targets.

@kinke kinke marked this pull request as ready for review August 11, 2025 15:18
kinke added a commit to ldc-developers/ldc that referenced this pull request Aug 11, 2025
@ibuclaw
Copy link
Member

ibuclaw commented Aug 11, 2025

FYI @ibuclaw - I assume you've had to work around this somewhere too, to fix this for big-endian targets.

Probably, PPCBE for example is not really given much love nowadays.

Though I don't think the sparc-solaris port has raised this - I can give it a try on one of the port boxes to check the current behaviour. As this is a patch from 2013, have you tried runtime without this?

@kinke
Copy link
Contributor Author

kinke commented Aug 11, 2025

At first I thought/hoped the old fix is superfluous by now, but I've checked the LLVM IR and seen that this is still a problem - the druntime helpers passing a pointer to a size_t, and the generated foreach-function treating it as a ref to the user-specified index type. I haven't run this on a big-endian target though, wouldn't know how to (and don't wanna use qemu for that).

@ibuclaw
Copy link
Member

ibuclaw commented Aug 11, 2025

Running bootstrap on sparcv9-sun-solaris2.11, will test gdc-11 (last C++) and gdc-15.

@ibuclaw
Copy link
Member

ibuclaw commented Aug 11, 2025

Yep, both exhibit runtime failure, I do see:

warning: foreach: loop index implicitly converted from ‘size_t’ to ‘short’ [-Wdeprecated]
    3 |     foreach (short i, dchar c; "ab")
      |     ^

So that's probably why no one's hit it on that target, library is built with -Werror and druntime is bug-free.

@dlang-bot dlang-bot merged commit f345932 into dlang:master Aug 11, 2025
44 checks passed
@kinke kinke deleted the fix_foreach_dg_types branch August 11, 2025 19:59
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.

3 participants