FreeBSD regtest: add fakes for older versions in scalar
[valgrind.git] / memcheck / tests / freebsd / aio.c
blobbf7b964aec5fcbf9ba80f1e3a1f121854d9e3a07
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 #include <sys/stat.h>
8 #include <time.h>
9 #include <errno.h>
10 int x;
12 int main(void)
14 #define LEN 10
15 char buf[LEN];
17 struct aiocb a;
18 struct sigevent s;
20 memset(&a, 0, sizeof(struct aiocb));
21 // Not sure if the sigevent is even looked at by aio_*... just zero it.
22 memset(&s, 0, sizeof(struct sigevent));
24 a.aio_fildes = -1;
25 a.aio_offset = 0;
26 a.aio_buf = NULL;
27 a.aio_nbytes = LEN;
28 a.aio_reqprio = 0;
29 //a.aio_sigevent = s;
30 a.aio_lio_opcode = 0; // ignored
32 //------------------------------------------------------------------------
33 // The cases where aiocbp itself points to bogus memory is handled in
34 // memcheck/tests/darwin/scalar.c, so we don't check that here.
36 //------------------------------------------------------------------------
37 // XXX: This causes an unexpected undef value error later, at the XXX mark.
38 // Not sure why, it shouldn't.
39 // assert( aio_return(&a) < 0); // (iocb hasn't been inited)
41 //------------------------------------------------------------------------
42 assert( aio_read(&a) < 0); // invalid fd
44 //------------------------------------------------------------------------
45 a.aio_fildes = open("aio.c", O_RDONLY);
46 assert(a.aio_fildes >= 0);
48 // unaddressable aio_buf
49 //assert(aio_read(&a) == 0);
50 //assert(aio_return(&a) == -1);
52 //------------------------------------------------------------------------
53 a.aio_buf = buf;
55 assert( aio_read(&a) == 0 );
57 // also failed on macOS
58 // (don't crash on the repeated &a)
59 // assert( aio_read(&a) < 0 );
61 // undefined -- aio_return() not called yet
62 if (buf[0] == buf[9]) x++;
64 int try_count = 0;
65 int res = aio_error(&a);
66 while (0 != res && try_count < 1000) {
67 ++try_count;
68 struct timespec rq = { 0, 1000 };
69 nanosleep(&rq, NULL);
70 res = aio_error(&a);
73 assert(try_count < 1000);
75 assert( aio_return(&a) > 0 ); // XXX: (undefined value error here)
77 if (buf[0] == buf[9]) x++;
79 #if 0
80 assert( aio_return(&a) < 0 ); // (repeated aio_return(); fails because
81 // Valgrind can't find &a in the table)
82 #endif
84 //------------------------------------------------------------------------
85 a.aio_buf = 0;
86 a.aio_fildes = creat("mytmpfile", S_IRUSR|S_IWUSR);
87 assert(a.aio_fildes >= 0);
89 // unaddressable aio_buf
90 //assert( aio_write(&a) == 0);
91 //assert(aio_return(&a) == -1);
93 //------------------------------------------------------------------------
94 a.aio_buf = buf;
96 assert( aio_write(&a) == 0 );
98 // (don't crash on the repeated &a)
99 //assert( aio_write(&a) < 0 );
101 try_count = 0;
102 res = aio_error(&a);
103 while (0 != res && try_count < 1000) {
104 ++try_count;
105 struct timespec rq = { 0, 1000 };
106 nanosleep(&rq, NULL);
107 res = aio_error(&a);
110 assert(try_count < 1000);
112 assert( aio_return(&a) > 0 );
114 #if 0
115 assert( aio_return(&a) < 0 ); // (repeated aio_return(); fails because
116 // Valgrind can't find &a in the table)
117 #endif
119 unlink("mytmpfile");
121 return x;