D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21821 - Optimizer assumes immutables do not change, but they can in @system code
Summary: Optimizer assumes immutables do not change, but they can in @system code
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: backend, pull, wrong-code
Depends on:
Blocks:
 
Reported: 2021-04-12 06:50 UTC by Walter Bright
Modified: 2023-02-07 15:23 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Walter Bright 2021-04-12 06:50:21 UTC
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 Dlang Bot 2021-04-12 07:16:08 UTC
@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 Mathias LANG 2021-04-12 08:34:59 UTC
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 Dlang Bot 2023-02-07 15:23:14 UTC
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