www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3797] New: Function pointers need to be stricter

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797

           Summary: Function pointers need to be stricter
           Product: D
           Version: 2.040
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: yebblies gmail.com


--- Comment #0 from yebblies <yebblies gmail.com> 2010-02-12 05:34:08 PST ---
Currently functions pointers can be implicitly converted despite having
different calling conventions.  If at all possible, function pointers should
require a cast to change calling convention.
In a related problem, it is possible to implicitly convert function pointers
with different argument lists.

The following code compiles but segfaults (Access Violation) on dmd 2.040 /
winxp.

import std.stdio;

void foo(real f)
{
    writeln("bar ", f);
}

void main()
{
    void function(ubyte[4]) ptr1 = &foo;
    extern(C) void function(long, long) ptr2 = ptr1;

    ptr1([0xFF, 0xFF, 0xFF, 0xFF]);
    ptr2(3, 8);
}

Function pointers cannot, however, be cast to a pointer with a different return
type.
To solve this problem: Disable all implicit conversions between function
pointers, requiring function parameters, return type, and calling convention to
match for assignment copying.  This prevents accidental conversions and allows
template code to be aware of the correct calling convention.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 12 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797


Sobirari Muhomori <dfj1esp02 sneakemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc


--- Comment #1 from Sobirari Muhomori <dfj1esp02 sneakemail.com> 2010-10-18
10:58:09 PDT ---
*** Issue 5069 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 18 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797


Sobirari Muhomori <dfj1esp02 sneakemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |accepts-invalid
           Severity|enhancement                 |major


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 18 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797



--- Comment #2 from bearophile_hugs eml.cc 2010-10-18 11:06:24 PDT ---
Sobirari Muhomori has closed bug 5069 because it's a duplicate of this one. But
please don't ignore one thing I've written in bug 5069, about error messages,
where for a wrong usage of std.c.stdlib.qsort I have asked for a single better
error message (instead of the current two worse error messages):


Line 15: Error: cannot implicitly convert expression (& compare) of type int
function(const void*, const void*) to extern(C) int function(const void*, const
void*)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 18 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797


Sobirari Muhomori <dfj1esp02 sneakemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|major                       |critical


--- Comment #3 from Sobirari Muhomori <dfj1esp02 sneakemail.com> 2010-10-18
11:09:27 PDT ---
Uh, crash has a critical severity.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 18 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797



--- Comment #4 from Sobirari Muhomori <dfj1esp02 sneakemail.com> 2010-10-18
11:14:48 PDT ---
 The following code compiles but segfaults (Access Violation) on dmd 2.040 /
 winxp.
Is this a cross-platform bug? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Oct 18 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797



--- Comment #5 from yebblies <yebblies gmail.com> 2010-10-18 19:29:40 PDT ---
(In reply to comment #4)
 Is this a cross-platform bug?
Yes. The problem is that pointers to functions with different calling conventions and parameters can be implicitly converted to each other. This can easily cause stack corruption and/or corrupted parameters. This is not unique to any platform. ------------------------------ import std.stdio; import std.typetuple; extern(D) { void d1() { writeln("d1"); } void d2(int a) { writeln("d2(", a, ")"); } void d3(int a, int b) { writeln("d3(", a, ", ", b, ")"); } void d4(long a, long b) { writeln("d4(", a, ", ", b, ")"); } } extern(C) { void c1() { writeln("c1"); } void c2(int a) { writeln("c2(", a, ")"); } void c3(int a, int b) { writeln("c3(", a, ", ", b, ")"); } void c4(long a, long b) { writeln("c4(", a, ", ", b, ")"); } } extern(Windows) { void w1() { writeln("w1"); } void w2(int a) { writeln("w2(", a, ")"); } void w3(int a, int b) { writeln("w3(", a, ", ", b, ")"); } void w4(long a, long b) { writeln("w4(", a, ", ", b, ")"); } } extern(C++) { void cpp1() { writeln("cpp1"); } void cpp2(int a) { writeln("cpp2(", a, ")"); } void cpp3(int a, int b) { writeln("cpp3(", a, ", ", b, ")"); } void cpp4(long a, long b) { writeln("cpp4(", a, ", ", b, ")"); } } extern(Pascal) { void p1() { writeln("p1"); } void p2(int a) { writeln("p2(", a, ")"); } void p3(int a, int b) { writeln("p3(", a, ", ", b, ")"); } void p4(long a, long b) { writeln("p4(", a, ", ", b, ")"); } } void main() { size_t stack; asm { mov stack, ESP; } writeln(stack); alias TypeTuple!(d1, d2, d3, d4, c1, c2, c3, c4, w1, w2, w3, w4, cpp1, cpp2, cpp3, cpp4, p1, p2, p3, p4) functions; auto fptr = &d3; foreach(f; functions) { fptr = &f; fptr(1, 2); } asm { mov stack, ESP; } writeln(stack); } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Oct 18 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797


Sobirari Muhomori <dfj1esp02 sneakemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg gmx.com


--- Comment #6 from Sobirari Muhomori <dfj1esp02 sneakemail.com> 2010-10-19
11:03:16 PDT ---
*** Issue 4893 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 19 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797


Sobirari Muhomori <dfj1esp02 sneakemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |2573


--- Comment #7 from Sobirari Muhomori <dfj1esp02 sneakemail.com> 2010-10-19
11:04:48 PDT ---
also
---
import core.stdc.stdio;

void f(int[] a)
{
    a[0]=42;
}

int main()
{
    immutable int[] b = [1,2,3];
    void function(const int[] a) g=&f;
    printf("%d\n",b[0]);
    g(b);
    printf("%d\n",b[0]);
    return 0;
}
---
1
42

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 19 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |timon.gehr gmx.ch


--- Comment #8 from yebblies <yebblies gmail.com> 2011-05-09 04:43:55 PDT ---
*** Issue 5827 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 09 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |samukha voliacable.com


--- Comment #9 from yebblies <yebblies gmail.com> 2011-05-13 06:00:57 PDT ---
*** Issue 5994 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 13 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
           Severity|critical                    |regression


--- Comment #10 from yebblies <yebblies gmail.com> 2011-05-13 06:08:31 PDT ---
(In reply to comment #9)
 *** Issue 5994 has been marked as a duplicate of this issue. ***
According to Don in beta mailing list:
 Regression was introduced in 2.038.
-- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 13 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797


timon.gehr gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|2.040                       |D2


--- Comment #11 from timon.gehr gmx.ch 2011-05-14 08:12:54 PDT ---
(In reply to comment #8)
 *** Issue 5827 has been marked as a duplicate of this issue. ***
Well, not exactly. But I guess it can be fixed together. My bug report was about function pointers with _different return types_ converting to each other: Timon Gehr wrote:
 The following invalid D code is accepted by DMD:
 
 import std.stdio;
 
 int a=0;
 ref int g(){
     writeln("called g");
     return ++a;
 }
 
 void main(){
     int function() f=&g; //this should issue an error!
     writeln(cast(int)&a);
     writeln(f());
 }
 
 Output:
 -144939256
 called g
 -144939256
 
 (&g is implicitly cast to "int function() ref", after that, the identical
 calling conventions for "ref int" (int*) and int result in an implicit
 reinterpret-cast from "ref int" to int.)
-- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 14 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jsancio gmail.com


--- Comment #12 from yebblies <yebblies gmail.com> 2011-06-04 22:52:55 PDT ---
*** Issue 5821 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 04 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797



--- Comment #13 from yebblies <yebblies gmail.com> 2011-06-06 19:33:41 PDT ---
*** Issue 4891 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 06 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |eatingstaples gmail.com


--- Comment #14 from yebblies <yebblies gmail.com> 2011-06-06 19:39:53 PDT ---
*** Issue 5434 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 06 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797



--- Comment #15 from yebblies <yebblies gmail.com> 2011-06-06 20:01:10 PDT ---
The reason this happens is because TypeFunction does not override constConv. 
When called on TypeFunction, TypeNext::constConv is actually called, which only
compares the return type. (next in TypeFunction is the return type)

I've created a possible fix in dmd pull 88 which defines
TypeFunction::constConv to disallow most implicit conversions between function
pointers.

The current patch allows reasonable purity, safety and nothrow conversions.  If
you're interested, please take a look and see if all cases are covered.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 06 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797


klickverbot <code klickverbot.at> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                 CC|                            |code klickverbot.at


--- Comment #16 from klickverbot <code klickverbot.at> 2011-06-07 00:32:09 PDT
---
Against all odds, I independently started to work on a fix yesterday:
https://github.com/D-Programming-Language/dmd/pull/91.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 07 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mrmocool gmx.de


--- Comment #17 from yebblies <yebblies gmail.com> 2011-06-08 00:48:14 PDT ---
*** Issue 5167 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 08 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797



--- Comment #18 from yebblies <yebblies gmail.com> 2011-06-08 22:27:18 PDT ---
https://github.com/D-Programming-Language/dmd/pull/96

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 08 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3797


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED


--- Comment #19 from Walter Bright <bugzilla digitalmars.com> 2011-09-02
02:36:39 PDT ---
https://github.com/D-Programming-Language/dmd/commit/306df8eaa6f8a987f76f401a1e03d8edf1f1e2ae

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 02 2011