Bug 21821 – Optimizer assumes immutables do not change, but they can in @system code
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-04-12T06:50:21Z
Last change time
2023-02-07T15:23:14Z
Keywords
backend, pull, wrong-code
Assigned to
No Owner
Creator
Walter Bright
Comments
Comment #0 by bugzilla — 2021-04-12T06:50:21Z
Consider this code from core.lifetime:
void copyEmplacex(ref immutable(S) source, ref immutable(S) target) @system {
import core.stdc.string : memcpy;
memcpy(cast(S*) &target, cast(S*) &source, S.sizeof);
(cast() target).__xpostblit();
}
Note the last line casts away immutability, and the call to __xpostblit() modifies the supposedly immutable `target`. The part of the optimizer that fails this is the function Symbol_isAffected() in backend/symbol.d
The fix is to set a flag in `funcsym_p` when it is @safe, and enable the check for immutability.
Comment #1 by dlang-bot — 2021-04-12T07:16:08Z
@WalterBright created dlang/dmd pull request #12424 "fix Issue 21821 - Optimizer assumes immutables do not change, but the…" fixing this issue:
- fix Issue 21821 - Optimizer assumes immutables do not change, but they can in @system code
https://github.com/dlang/dmd/pull/12424
Comment #2 by pro.mathias.lang — 2021-04-12T08:34:59Z
Where is this specified ? `immutable` should never change, that's the point!
I've never seen, nor heard, that `immutable` could be violated in `@system`.
This code in `core.lifetime` exhibits UB and should be changed to apply `immutable` after the `memcpy`, e.g. as `assummeUnique` does.
Comment #3 by dlang-bot — 2023-02-07T15:23:14Z
dlang/dmd pull request #12424 "fix Issue 21821 - Optimizer assumes immutables do not change, but the…" was merged into master:
- ba1f1c3222a91ef6a56caa18afa3bf7464d9e422 by Walter Bright:
fix Issue 21821 - Optimizer assumes immutables do not change, but they can in @system code
https://github.com/dlang/dmd/pull/12424