Home | History | Annotate | Download | only in darwin
      1 #include <assert.h>
      2 #include <aio.h>
      3 #include <fcntl.h>
      4 #include <stdio.h>
      5 #include <string.h>
      6 #include <unistd.h>
      7 int x;
      8 
      9 int main(void)
     10 {
     11    #define LEN 10
     12    char buf[LEN];
     13 
     14    struct aiocb a;
     15    struct sigevent s;
     16 
     17    // Not sure if the sigevent is even looked at by aio_*... just zero it.
     18    memset(&s, 0, sizeof(struct sigevent));
     19 
     20    a.aio_fildes     = -1;
     21    a.aio_offset     = 0;
     22    a.aio_buf        = NULL;
     23    a.aio_nbytes     = LEN;
     24    a.aio_reqprio    = 0;
     25    a.aio_sigevent   = s;
     26    a.aio_lio_opcode = 0;   // ignored
     27 
     28    //------------------------------------------------------------------------
     29    // The cases where aiocbp itself points to bogus memory is handled in
     30    // memcheck/tests/darwin/scalar.c, so we don't check that here.
     31 
     32    //------------------------------------------------------------------------
     33    // XXX: This causes an unexpected undef value error later, at the XXX mark.
     34    //      Not sure why, it shouldn't.
     35    // assert( aio_return(&a) < 0);  // (aiocbp hasn't been inited)
     36 
     37    //------------------------------------------------------------------------
     38    assert( aio_read(&a) < 0);       // invalid fd
     39 
     40    //------------------------------------------------------------------------
     41    a.aio_fildes = open("aio.c", O_RDONLY);
     42    assert(a.aio_fildes >= 0);
     43 
     44    assert( aio_read(&a) < 0);       // unaddressable aio_buf
     45 
     46    //------------------------------------------------------------------------
     47    a.aio_buf = buf;
     48 
     49    assert( aio_read(&a) == 0 );
     50 
     51    assert( aio_read(&a)  < 0 );     // (don't crash on the repeated &a)
     52 
     53    while (0 != aio_error(&a)) { };
     54 
     55    if (buf[0] == buf[9]) x++;       // undefined -- aio_return() not called yet
     56 
     57    assert( aio_return(&a) > 0 );    // XXX: (undefined value error here)
     58 
     59    if (buf[0] == buf[9]) x++;
     60 
     61    assert( aio_return(&a) < 0 );    // (repeated aio_return();  fails because
     62                                     // Valgrind can't find &a in the table)
     63 
     64    //------------------------------------------------------------------------
     65    a.aio_buf    = 0;
     66    a.aio_fildes = creat("mytmpfile", S_IRUSR|S_IWUSR);
     67    assert(a.aio_fildes >= 0);
     68 
     69    assert( aio_write(&a) < 0);      // unaddressable aio_buf
     70 
     71    //------------------------------------------------------------------------
     72    a.aio_buf = buf;
     73 
     74    assert( aio_write(&a) == 0 );
     75 
     76    assert( aio_write(&a)  < 0 );    // (don't crash on the repeated &a)
     77 
     78    while (0 != aio_error(&a)) { };
     79 
     80    assert( aio_return(&a) > 0 );
     81 
     82    assert( aio_return(&a) < 0 );    // (repeated aio_return();  fails because
     83                                     // Valgrind can't find &a in the table)
     84 
     85    unlink("mytmpfile");
     86 
     87    return x;
     88 };
     89 
     90 
     91 
     92