D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 12779 - [REG2.066a] -inline makes wrong code under some conditions
Summary: [REG2.066a] -inline makes wrong code under some conditions
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 regression
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2014-05-21 12:51 UTC by jiki
Modified: 2014-05-24 08:00 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description jiki 2014-05-21 12:51:18 UTC
This issue is triggered by:
https://github.com/D-Programming-Language/dmd/pull/2561

I have struggled to reduce it though it is rather complicated.

SOURCE FILES:
*lib.d ---------
module lib;

void test( string data) {
    auto ret = new Foo();
    ret.data = data;
    assert(ret.data.length < 1_000_000); // OK!
    ret.check(); // NG
}

class Foo {
    // Removing this or changing order of these members
    // affects the runtime behavior ...
    version (defined) {
        int[int] unusedAA;
    }
    
    string data;
    
    void check() {
        assert(data.length < 1_000_000); // NG!
    }
}

*main.d ---------
import lib;

void main()
{
    string data = "text";
    test( data);
}

COMMANDS(compile and run):
dmd.exe -lib -version=defined lib.d
dmd.exe -g  -inline inlinebug.d lib.lib
inlinebug.exe

OUTPUT:
core.exception.AssertError@lib.d(20): Assertion failure
----------------
0x00402487 in _d_assert
0x004020DB in void lib.Foo.check()
0x0040206C in _Dmain
Comment 1 jiki 2014-05-21 13:03:52 UTC
Whether inlining the function 'test' or not makes difference.
However, other conditions affect strange.
Comment 2 Kenji Hara 2014-05-24 07:07:39 UTC
If you use the lib.lib binary, you should emit same version definitions to compile the `main.d`. Otherwise, the class instance size of Foo will be different in each compilation units (lib.d and main.d), then the inlined code will access invalid memory in runtime.

In other words, this is expected result. Your compilation steps are just wrong, so the generated execution binary will cause invalid result.
Comment 3 Kenji Hara 2014-05-24 07:18:21 UTC
Note that, this is unavoidable issue of the languages which supports separate compilation for old simple linkers. If each compilation units is compiled under different conditions, the consistency of the binaries won't be guaranteed. To do it, we should have "smart linker" that checks the compilation consistency, and add enough meta information into the object files to check it.
Comment 4 jiki 2014-05-24 08:00:17 UTC
I see... thank you.
I'll change my mind.