Bug 23514 – Incorrect compilation when adding a 64-bit constant to a link-time address
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2022-11-27T08:25:52Z
Last change time
2023-02-22T02:42:23Z
Keywords
pull
Assigned to
No Owner
Creator
zyedidia
Comments
Comment #0 by zyedidia — 2022-11-27T08:25:52Z
This code adds a large constant to the address of main, but the offset is truncated to 32 bits by the compiler, resulting in an incorrect result.
```
enum offset = 0xFFFF_FFFF_0000_0000UL;
extern (C) void main() {
assert((cast(ulong)&main) != (cast(ulong)&main + offset));
}
```
The assertion should pass (&main should not equal &main + 64-bit constant), but it fails.
The assertion fails with DMD master and LDC 1.30.0 (didn't test on master), but passes with GDC 10.3.0.
I will open a PR with the small fix for this shortly.
@zyedidia updated dlang/dmd pull request #14657 "Fix issue 23514 - Incorrect compilation when adding a 64-bit constant to a link-time address" fixing this issue:
- Fix issue 23514 - Incorrect compilation when adding a 64-bit constant to a link-time address
The current cast to int causes a loss of precision, which can result in
incorrect compilation.
https://github.com/dlang/dmd/pull/14657
Comment #3 by dlang-bot — 2023-02-21T14:59:48Z
dlang/dmd pull request #14657 "Fix issue 23514 - Incorrect compilation when adding a 64-bit constant to a link-time address" was merged into stable:
- 9d988bb7c27411232cb3655f674b7e0dc60ffa54 by Zachary Yedidia:
Fix issue 23514 - Incorrect compilation when adding a 64-bit constant to a link-time address
The current cast to int causes a loss of precision, which can result in
incorrect compilation.
https://github.com/dlang/dmd/pull/14657
Comment #4 by dlang-bot — 2023-02-22T02:42:23Z
dlang/dmd pull request #14904 "merge stable" was merged into master:
- cd946d37c46aa2d860ee495ef22ead00cac2e783 by Zachary Yedidia:
Fix issue 23514 - Incorrect compilation when adding a 64-bit constant to a link-time address (#14657)
* Fix issue 23514 - Incorrect compilation when adding a 64-bit constant to a link-time address
The current cast to int causes a loss of precision, which can result in
incorrect compilation.
* Add test23514.d
* Enable optimization in test
* Improve test case
* Use ulong instead of size_t
* Restrict test to 64-bit targets
* Re-enable test for 32-bit targets
* Disable test for win64 and add comment
https://github.com/dlang/dmd/pull/14904