www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3604] New: extern(C) callable function with array parameters broken

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

           Summary: extern(C) callable function with array parameters
                    broken
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: baryluk smp.if.uj.edu.pl



10:17:12 PST ---
I found a regression in 2.037. It was working in 2.032.

import core.sys.posix.unistd : pipe;

void main() {
int[2] input, output;

writefln("input: %s", input);
writefln("input.ptr: %s", input.ptr);
writefln("cast input: %s", cast(char*)input);
ttt(input);

if (pipe(input) != 0) {
  throw new Exception("can't create input pipe");
} else {
  writeln("ok");
}

}

extern(C) int ttt(int[2] x) {
  writefln("x = %s", x); 
  writefln("x[0] = %s", x[0]);
  writefln("x[1] = %s", x[1]);
  return 0;
}



this programs print:
====
input: 0 0
input.ptr: BF85E068
cast input: BF85E068
x = 0 0
x[0] = 0
x[1] = 0
object.Exception: can't create input pipe
====

but should:

====
input: 0 0
input.ptr: BF85E068
cast input: BF85E068
x = BF85E068
x[0] = 0
x[1] = 0
ok
====

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 10 2009
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3604


Witold Baryluk <baryluk smp.if.uj.edu.pl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Platform|Other                       |x86



10:31:47 PST ---
In, 2.032 (just tested) it returns:
$ ./bug3604 
input: 0 0
input.ptr: BFA00CB8
cast input: BFA00CB8
x = 0 0
x[0] = 0
x[1] = 0
ok
$

So not exactly what I written, but pipe is working.

So i tested it more precisly:

bug3604.d:
 extern(C) int tttc(int[2] x);
void main() {
   int[2] input;
   tttc(input);
}

bug3604c.c:
  #include <stdio.h>

  int tttc(int x[2]) {
    printf("just in C\n");
    printf("x=%p\n", x);
    printf("x[0]=%d\n", x[0]);
    printf("x[1]=%d\n", x[1]);
    printf("back from C\n");
  }




just in C
x=0xbf988ff8
x[0]=0
x[1]=0
back from C


just in C
x=(nil)
Segmentation fault


So it is regression.

In `strace` for "pipe" example I see for 2.032:
 pipe([3, 4])                            = 0
and for 2.037:
 pipe(0)                                 = -1 EFAULT (Bad address)

So it also passes NULL pointer (jiust like in tttc function) .

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 10 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3604


Lutger <lutger.blijdestijn gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lutger.blijdestijn gmail.co
                   |                            |m



PST ---
This is not a compiler bug, but due to the changed semantics (value instead of
reference) of static arrays the extern(C) declaration of pipe is wrong. 

See also this post:

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=18393

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 10 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3604




06:22:49 PST ---
Changing declaration of pipe to
  extern(C) int pipe(int*);
will make that D type system have less information than previous.

currently, if I would write:
   extern(C) int pipe(int[2]);
   int[1] input;
   pipe(input);
it will fail to compile because of wrong type.

But with:
   extern(C) int pipe(int*);
   int[1] input;
   pipe(input.ptr);
it will compile without any error.

I see many other functions, in core which takes array of different sizes.

How about just interpreting array parametern in extern(C) function as being
passed via reference. I don't see what was wrong in previous approach. It gives
some more verbosity to error messages.

If anyway this is new intended behaviour, please document it and fix all
headers in core.* and other extern(C) headers.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 11 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3604


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy yahoo.com



06:40:58 PST ---
What about using ref int[2] instead?

I just tested with your code modifying the signature and it works:

[steves steveslaptop testd]$ cat bug3604.d
 extern(C) int tttc(ref int[2] x);
 void main() {
        int[2] input;
           tttc(input);
 }

[steves steveslaptop testd]$ cat bug3604c.c
#include <stdio.h>

int tttc(int x[2]) {
    printf("just in C\n");
    printf("x=%p\n", x);
    printf("x[0]=%d\n", x[0]);
    printf("x[1]=%d\n", x[1]);
    printf("back from C\n");
}
[steves steveslaptop testd]$ gcc -c bug3604c.c
[steves steveslaptop testd]$ ../dmd2.037/linux/bin/dmd bug3604.d bug3604c.o 
[steves steveslaptop testd]$ ./bug3604
just in C
x=0xbffa06e0
x[0]=0
x[1]=0
back from C

I think this better captures the correct signature than what I suggested in the
post.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 11 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3604


Witold Baryluk <baryluk smp.if.uj.edu.pl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|DMD                         |druntime
         AssignedTo|nobody puremagic.com        |sean invisibleduck.org



16:47:26 PST ---
Ah, yes
  ref T[];
this sounds good. I will reassign this bug to druntime then, and hopfully
trivial fixes will be on next release.

PS. I just checked changlogs and there are nothing about this change in
compiler or spec. :/
Maybe small update to http://digitalmars.com/d/2.0/interfaceToC.html ?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 15 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3604


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com



14:47:18 PST ---
Good idea, I've updated interfaceToC.html

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




05:44:35 PST ---
Thanks.

Functions like
sys.posix.unistd: pipe, encrypt
sys.posix.stdlib: Xseed48, {e,j,n}rand48, lcong48
sys.posix.poll: poll
sys.posix.sys.time: utimes
sys.posix.sys.socket : socketpair

are currently affected in druntime (from 2.039).

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


Lars T. Kyllingstad <bugzilla kyllingen.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla kyllingen.net



10:36:17 PST ---
I ran into the problem with core.sys.posix.unistd.pipe() today.  The following
grep should have tracked down most of the problematic declarations:

$ grep -r '(.*\w\+[[][0-9]' core/*
core/sys/posix/sys/socket.d:int     socketpair(int, int, int, int[2]);
core/sys/posix/sys/socket.d:    int     socketpair(int, int, int, int[2]);
core/sys/posix/sys/socket.d:    int     socketpair(int, int, int, int[2]);
core/sys/posix/sys/socket.d:    int     socketpair(int, int, int, int[2]);
core/sys/posix/sys/time.d:int utimes(in char*, in timeval[2]); // LEGACY
core/sys/posix/sys/time.d:    int utimes(in char*, in timeval[2]); // LEGACY
core/sys/posix/sys/time.d:    int utimes(in char*, in timeval[2]);
core/sys/posix/sys/time.d:    int utimes(in char*, in timeval[2]);
core/sys/posix/stdlib.d:double erand48(ushort[3]);
core/sys/posix/stdlib.d:c_long jrand48(ushort[3]);
core/sys/posix/stdlib.d:void   lcong48(ushort[7]);
core/sys/posix/stdlib.d:c_long nrand48(ushort[3]);
core/sys/posix/stdlib.d:ushort seed48(ushort[3]);
core/sys/posix/stdlib.d:    double erand48(ushort[3]);
core/sys/posix/stdlib.d:    c_long jrand48(ushort[3]);
core/sys/posix/stdlib.d:    void   lcong48(ushort[7]);
core/sys/posix/stdlib.d:    c_long nrand48(ushort[3]);
core/sys/posix/stdlib.d:    ushort seed48(ushort[3]);
core/sys/posix/stdlib.d:    double erand48(ushort[3]);
core/sys/posix/stdlib.d:    c_long jrand48(ushort[3]);
core/sys/posix/stdlib.d:    void   lcong48(ushort[7]);
core/sys/posix/stdlib.d:    c_long nrand48(ushort[3]);
core/sys/posix/stdlib.d:    ushort seed48(ushort[3]);
core/sys/posix/stdlib.d:    double erand48(ushort[3]);
core/sys/posix/stdlib.d:    c_long jrand48(ushort[3]);
core/sys/posix/stdlib.d:    void   lcong48(ushort[7]);
core/sys/posix/stdlib.d:    c_long nrand48(ushort[3]);
core/sys/posix/stdlib.d:    ushort seed48(ushort[3]);
core/sys/posix/unistd.d:int     pipe(int[2]);
core/sys/posix/unistd.d:void       encrypt(char[64], int);
core/sys/posix/unistd.d:    void       encrypt(char[64], int);
core/sys/posix/unistd.d:    void       encrypt(char[64], int);

(I've removed a few false positives in the above.)

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


Steven Schveighoffer <schveiguy yahoo.com> changed:

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



06:34:35 PDT ---
*** Issue 4199 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 17 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3604


Brad Roberts <braddr puremagic.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |braddr puremagic.com
         Resolution|                            |FIXED



PDT ---
fixed in 2.047

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


Lars T. Kyllingstad <bugzilla kyllingen.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|FIXED                       |



04:47:33 PDT ---
It seems only the ones in stdlib and unistd have been fixed, and not the ones
in sys.socket and sys.time.

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


Lars T. Kyllingstad <bugzilla kyllingen.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |FIXED



01:55:00 PDT ---
Fixed DMD 2.048.

http://www.dsource.org/projects/druntime/changeset/336

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 13 2010