digitalmars.D.bugs - [Issue 9028] New: `main` is trated sometimes as having C linkage and sometimes as having D linkage
- d-bugmail puremagic.com (36/36) Nov 15 2012 http://d.puremagic.com/issues/show_bug.cgi?id=9028
- d-bugmail puremagic.com (13/13) Nov 15 2012 http://d.puremagic.com/issues/show_bug.cgi?id=9028
- d-bugmail puremagic.com (34/34) Nov 15 2012 http://d.puremagic.com/issues/show_bug.cgi?id=9028
- d-bugmail puremagic.com (97/97) Nov 15 2012 http://d.puremagic.com/issues/show_bug.cgi?id=9028
- d-bugmail puremagic.com (11/11) Nov 15 2012 http://d.puremagic.com/issues/show_bug.cgi?id=9028
http://d.puremagic.com/issues/show_bug.cgi?id=9028 Summary: `main` is trated sometimes as having C linkage and sometimes as having D linkage Product: D Version: D2 Platform: All OS/Version: All Status: NEW Keywords: wrong-code Severity: normal Priority: P2 Component: DMD AssignedTo: nobody puremagic.com ReportedBy: verylonglogin.reg gmail.com --- Comment #0 from Denis Shelomovskij <verylonglogin.reg gmail.com> 2012-11-15 11:59:34 MSK --- --- void main(string[] args) { if(!args.length) return; foreach(i; 0 .. 1) main(null); // ok, C linkage used foreach(i; 0 .. 1) (&main)(null); // also ok foreach(i; 0 .. 1) { auto p = &main; // D linkage used p(null); // stack corruption, results in Access Violation //asm { add ESP, 0x8; } // uncomment to fix } } --- Note: do not mark `main` as `extern(C)` because it will lead to not calling runtime initialization stuff. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 15 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9028 Maxim Fomin <maxim maxim-fomin.ru> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |maxim maxim-fomin.ru --- Comment #1 from Maxim Fomin <maxim maxim-fomin.ru> 2012-11-15 01:49:06 PST --- I don't understand what is the problem here. First of all the code runs on both win&lin using dmd 2.060 and git head from Dpaste. Secondly I don't understand how this is related to linkage. Function calls main(null) and (&main)(null) are equivalent. In the third case you call it indirectly through pointer. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 15 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9028 Denis Shelomovskij <verylonglogin.reg gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Summary|`main` is trated sometimes |`main` is trated sometimes |as having C linkage and |as having C calling |sometimes as having D |convention and sometimes as |linkage |having D convention --- Comment #2 from Denis Shelomovskij <verylonglogin.reg gmail.com> 2012-11-15 13:01:22 MSK --- (In reply to comment #1) Sorry, it's calling convention, not linkage. I mean C calling convention is used in first two cases but as `typeof(main)` tells it is `extern(D)` D calling convention is used in the last case. So `Access Violation` isn't mandatory but possible. I.e. this will print decreasing ESP: --- import std.stdio; void main(string[] args) { if(!args.length) return; size_t esp; foreach(i; 0 .. 2 ^^ 20) { auto p = &main; // D linkage used p(null); // stack corruption, results in Access Violation //asm { add ESP, 0x8; } // uncomment to fix asm { mov esp, ESP; } writefln("%X", esp); } } --- -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 15 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9028 --- Comment #3 from Maxim Fomin <maxim maxim-fomin.ru> 2012-11-15 03:11:18 PST --- Ok, I see the problem now: ------------------------- import std.stdio; void main(string[] args) { dain(args); if(!args.length) return; foreach(i; 0 .. 2) { size_t esp; main(null); // ok, C linkage used asm { mov esp, ESP; } writeln("main"); writefln("%X", esp); } foreach(i; 0 .. 2) { size_t esp; (&main)(null); // also ok asm { mov esp, ESP; } writeln("&main"); writefln("%X", esp); } foreach(i; 0 .. 2) { size_t esp; auto p = &main; // D linkage used p(null); // stack corruption, results in Access Violation //asm { add ESP, 0x8; } // uncomment to fix asm { mov esp, ESP; } writeln("p-main"); writefln("%X", esp); } } void dain(string[] args) { if(!args.length) return; foreach(i; 0 .. 2) { size_t esp; dain(null); // ok, C linkage used asm { mov esp, ESP; } writeln("dain"); writefln("%X", esp); } foreach(i; 0 .. 2) { size_t esp; (&dain)(null); // also ok asm { mov esp, ESP; } writeln("&dain"); writefln("%X", esp); } foreach(i; 0 .. 2) { size_t esp; auto p = &dain; // D linkage used p(null); // stack corruption, results in Access Violation //asm { add ESP, 0x8; } // uncomment to fix asm { mov esp, ESP; } writeln("p-dain"); writefln("%X", esp); } } ---------------------------------- dain 18FDC4 dain 18FDC4 &dain 18FDC4 &dain 18FDC4 p-dain 18FDC4 p-dain 18FDC4 main 18FE10 main 18FE10 &main 18FE10 &main 18FE10 p-main 18FE08 p-main 18FE00 This is windows output. Note in main %esp is decreasing in last loop. Same code on linux is fine (http://dpaste.dzfl.pl/6aa48fed), so this is specific to windows codegen/calling conventions. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 15 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9028 --- Comment #4 from github-bugzilla puremagic.com 2012-11-15 06:59:22 PST --- Commit pushed to master at https://github.com/D-Programming-Language/druntime https://github.com/D-Programming-Language/druntime/commit/d171d502d6cb1420140053267cb51989b0f2d07e Add full workaround for Issue 9028. Mark D main as `extern(C)` and use its symbol name to workaround Issue 9028. Also add comments about `D main` type and actual calling convention. * Issue 9028 URL: http://d.puremagic.com/issues/show_bug.cgi?id=9028 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 15 2012