(root)/
strace-6.5/
tests-m32/
aio.c
       1  /*
       2   * Check decoding of io_* syscalls.
       3   *
       4   * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@strace.io>
       5   * Copyright (c) 2015-2023 The strace developers.
       6   * All rights reserved.
       7   *
       8   * SPDX-License-Identifier: GPL-2.0-or-later
       9   */
      10  
      11  #include "tests.h"
      12  #include "scno.h"
      13  
      14  #ifdef __NR_io_getevents
      15  # include <fcntl.h>
      16  # include <inttypes.h>
      17  # include <stdio.h>
      18  # include <time.h>
      19  # include <unistd.h>
      20  # include <linux/aio_abi.h>
      21  
      22  int
      23  main(void)
      24  {
      25  	static const long bogus_ctx =
      26  		(long) 0xface1e55deadbeefLL;
      27  
      28  	static const char data2[] =
      29  		"\0\1\2\3cat test test test 0123456789abcdef";
      30  
      31  	const unsigned int sizeof_data0 = 4096;
      32  	const unsigned int sizeof_data1 = 8192;
      33  	void *data0 = tail_alloc(sizeof_data0);
      34  	void *data1 = tail_alloc(sizeof_data1);
      35  
      36  	const struct iocb proto_cb[] = {
      37  		{
      38  			.aio_data = (unsigned long) 0xfeedface11111111ULL,
      39  			.aio_reqprio = 11,
      40  			.aio_buf = (unsigned long) data0,
      41  			.aio_offset = (unsigned long) 0xdeface1facefeedULL,
      42  			.aio_nbytes = sizeof_data0
      43  		},
      44  		{
      45  			.aio_data = (unsigned long) 0xfeedface22222222ULL,
      46  			.aio_reqprio = 22,
      47  			.aio_buf = (unsigned long) data1,
      48  			.aio_offset = (unsigned long) 0xdeface2cafef00dULL,
      49  			.aio_nbytes = sizeof_data1
      50  		}
      51  	};
      52  	const struct iocb *cb = tail_memdup(proto_cb, sizeof(proto_cb));
      53  
      54  	const struct iovec proto_iov0[] = {
      55  		{
      56  			.iov_base = data0,
      57  			.iov_len = sizeof_data0 / 4
      58  		},
      59  		{
      60  			.iov_base = data0 + sizeof_data0 / 4,
      61  			.iov_len = sizeof_data0 / 4 * 3
      62  		},
      63  	};
      64  	const struct iovec *iov0 = tail_memdup(proto_iov0, sizeof(proto_iov0));
      65  
      66  	const struct iovec proto_iov1[] = {
      67  		{
      68  			.iov_base = data1,
      69  			.iov_len = sizeof_data1 / 4
      70  		},
      71  		{
      72  			.iov_base = data1 + sizeof_data1 / 4,
      73  			.iov_len = sizeof_data1 / 4 * 3
      74  		},
      75  	};
      76  	const struct iovec *iov1 = tail_memdup(proto_iov1, sizeof(proto_iov1));
      77  
      78  	const struct iocb proto_cbv[] = {
      79  		{
      80  			.aio_data = (unsigned long) 0xfeed11111111faceULL,
      81  			.aio_lio_opcode = 7,
      82  			.aio_reqprio = 111,
      83  			.aio_buf = (unsigned long) iov0,
      84  			.aio_offset = (unsigned long) 0xdeface1facefeedULL,
      85  			.aio_nbytes = ARRAY_SIZE(proto_iov0)
      86  		},
      87  		{
      88  			.aio_data = (unsigned long) 0xfeed22222222faceULL,
      89  			.aio_lio_opcode = 7,
      90  			.aio_reqprio = 222,
      91  			.aio_buf = (unsigned long) iov1,
      92  			.aio_offset = (unsigned long) 0xdeface2cafef00dULL,
      93  			.aio_nbytes = ARRAY_SIZE(proto_iov1)
      94  		}
      95  	};
      96  	const struct iocb *cbv = tail_memdup(proto_cbv, sizeof(proto_cbv));
      97  
      98  	/* For additional decoder testing */
      99  	const struct iocb proto_cbv2[] = {
     100  		{
     101  			.aio_data = 0xbadfacedc0ffeeedULL,
     102  			.aio_key = 0xdefaced0,
     103  			.aio_lio_opcode = 0xf00d,
     104  			.aio_reqprio = 0,
     105  			.aio_fildes = 0xdefaced1,
     106  			.aio_buf = 0,
     107  		},
     108  		{
     109  			.aio_data = 0,
     110  			.aio_key = 0xdefaced0,
     111  			.aio_lio_opcode = 1,
     112  			.aio_reqprio = 0xbeef,
     113  			.aio_fildes = 0xdefaced1,
     114  			.aio_buf = 0,
     115  			/* In order to make record valid */
     116  			.aio_nbytes = (size_t) 0x1020304050607080ULL,
     117  			.aio_offset = 0xdeadda7abadc0dedULL,
     118  # ifdef HAVE_STRUCT_IOCB_AIO_FLAGS
     119  			.aio_flags = 0xfacef157,
     120  			.aio_resfd = 0xded1ca7e,
     121  # endif
     122  		},
     123  		{
     124  			.aio_data = 0,
     125  			.aio_key = 0xdefaced0,
     126  			.aio_lio_opcode = 1,
     127  			.aio_reqprio = 0xbeef,
     128  			.aio_fildes = 0xdefaced1,
     129  			.aio_buf = 0xbadc0ffeedefacedULL,
     130  			.aio_nbytes = 0x8090a0b0c0d0e0f0ULL,
     131  			.aio_offset = 0xdeadda7abadc0dedULL,
     132  		},
     133  		{
     134  			.aio_data = 0,
     135  			.aio_key = 0xdefaced0,
     136  			.aio_lio_opcode = 1,
     137  			.aio_reqprio = 0xbeef,
     138  			.aio_fildes = 0xdefaced1,
     139  			.aio_buf = (unsigned long)data2,
     140  			.aio_nbytes = sizeof(data2),
     141  			.aio_offset = 0xdeadda7abadc0dedULL,
     142  		},
     143  		{
     144  			.aio_data = 0,
     145  			.aio_key = 0xdefaced0,
     146  			.aio_lio_opcode = 8,
     147  			.aio_reqprio = 0xbeef,
     148  			.aio_fildes = 0xdefaced1,
     149  			.aio_buf = 0,
     150  			.aio_nbytes = 0x8090a0b0c0d0e0f0ULL,
     151  			.aio_offset = 0xdeadda7abadc0dedULL,
     152  		},
     153  	};
     154  	const struct iocb *cbv2 = tail_memdup(proto_cbv2, sizeof(proto_cbv2));
     155  
     156  	const struct iocb proto_cbc = {
     157  		.aio_data = (unsigned long) 0xdeadbeefbadc0dedULL,
     158  		.aio_reqprio = 99,
     159  		.aio_fildes = -42
     160  	};
     161  	const struct iocb *cbc = tail_memdup(&proto_cbc, sizeof(proto_cbc));
     162  
     163  	const long proto_cbs[] = {
     164  		(long) &cb[0], (long) &cb[1]
     165  	};
     166  	const long *cbs = tail_memdup(proto_cbs, sizeof(proto_cbs));
     167  
     168  	const long proto_cbvs[] = {
     169  		(long) &cbv[0], (long) &cbv[1],
     170  	};
     171  	const long *cbvs = tail_memdup(proto_cbvs, sizeof(proto_cbvs));
     172  
     173  	const long proto_cbvs2[] = {
     174  		(long) &cbv2[0], (long) &cbv2[1], (long) &cbv2[2],
     175  		(long) &cbv2[3], (long) &cbv2[4],
     176  		(long) NULL, (long) 0xffffffffffffffffLL,
     177  	};
     178  	const long *cbvs2 = tail_memdup(proto_cbvs2, sizeof(proto_cbvs2));
     179  
     180  	TAIL_ALLOC_OBJECT_CONST_PTR(unsigned long, ctx);
     181  	*ctx = 0;
     182  
     183  	const unsigned int nr = ARRAY_SIZE(proto_cb);
     184  	const unsigned long lnr = (unsigned long) (0xdeadbeef00000000ULL | nr);
     185  
     186  	const struct io_event *ev = tail_alloc(nr * sizeof(struct io_event));
     187  	TAIL_ALLOC_OBJECT_CONST_PTR(kernel_old_timespec_t, ts);
     188  
     189  	(void) close(0);
     190  	if (open("/dev/zero", O_RDONLY))
     191  		perror_msg_and_skip("open: %s", "/dev/zero");
     192  
     193  	long rc = syscall(__NR_io_setup, 0xdeadbeef, NULL);
     194  	printf("io_setup(%u, NULL) = %s\n", 0xdeadbeef, sprintrc(rc));
     195  
     196  	rc = syscall(__NR_io_setup, lnr, ctx + 1);
     197  	printf("io_setup(%u, %p) = %s\n", nr, ctx + 1, sprintrc(rc));
     198  
     199  	if (syscall(__NR_io_setup, lnr, ctx))
     200  		perror_msg_and_skip("io_setup");
     201  	printf("io_setup(%u, [%#lx]) = 0\n", nr, *ctx);
     202  
     203  	rc = syscall(__NR_io_submit, bogus_ctx, (long) 0xca7faceddeadf00dLL,
     204  		     NULL);
     205  	printf("io_submit(%#lx, %ld, NULL) = %s\n",
     206  	       bogus_ctx, (long) 0xca7faceddeadf00dLL, sprintrc(rc));
     207  
     208  	rc = syscall(__NR_io_submit, *ctx, nr, cbs + nr);
     209  	printf("io_submit(%#lx, %ld, %p) = %s\n",
     210  	       *ctx, (long) nr, cbs + nr, sprintrc(rc));
     211  
     212  	rc = syscall(__NR_io_submit, *ctx, -1L, cbs);
     213  	printf("io_submit(%#lx, -1, %p) = %s\n",
     214  	       *ctx, cbs, sprintrc(rc));
     215  
     216  	rc = syscall(__NR_io_submit, *ctx, nr, cbs);
     217  	if (rc != (long) nr)
     218  		perror_msg_and_skip("io_submit");
     219  	printf("io_submit(%#lx, %u, ["
     220  	       "{aio_data=%#" PRI__x64 ", aio_lio_opcode=IOCB_CMD_PREAD"
     221  		", aio_reqprio=11, aio_fildes=0, aio_buf=%p, aio_nbytes=%u"
     222  		", aio_offset=%" PRI__d64
     223  	       "}, {aio_data=%#" PRI__x64 ", aio_lio_opcode=IOCB_CMD_PREAD"
     224  		", aio_reqprio=22, aio_fildes=0, aio_buf=%p, aio_nbytes=%u"
     225  		", aio_offset=%" PRI__d64 "}]) = %s\n",
     226  	       *ctx, nr,
     227  	       cb[0].aio_data, data0, sizeof_data0, cb[0].aio_offset,
     228  	       cb[1].aio_data, data1, sizeof_data1, cb[1].aio_offset,
     229  	       sprintrc(rc));
     230  
     231  	rc = syscall(__NR_io_getevents, bogus_ctx,
     232  		     (long) 0xca7faceddeadf00dLL, (long) 0xba5e1e505ca571e0LL,
     233  		     ev + 1, NULL);
     234  	printf("io_getevents(%#lx, %ld, %ld, %p, NULL) = %s\n",
     235  	       bogus_ctx, (long) 0xca7faceddeadf00dLL,
     236  	       (long) 0xba5e1e505ca571e0LL, ev + 1, sprintrc(rc));
     237  
     238  	rc = syscall(__NR_io_getevents, bogus_ctx,
     239  		     (long) 0xca7faceddeadf00dLL, (long) 0xba5e1e505ca571e0LL,
     240  		     NULL, ts + 1);
     241  	printf("io_getevents(%#lx, %ld, %ld, NULL, %p) = %s\n",
     242  	       bogus_ctx, (long) 0xca7faceddeadf00dLL,
     243  	       (long) 0xba5e1e505ca571e0LL, ts + 1, sprintrc(rc));
     244  
     245  	ts->tv_sec = 0xdeadbeefU;
     246  	ts->tv_nsec = 0xfacefeedU;
     247  	rc = syscall(__NR_io_getevents, bogus_ctx, 0, 0, 0, ts);
     248  	printf("io_getevents(%#lx, 0, 0, NULL"
     249  	       ", {tv_sec=%lld, tv_nsec=%llu}) = %s\n",
     250  	       bogus_ctx, (long long) ts->tv_sec,
     251  	       zero_extend_signed_to_ull(ts->tv_nsec), sprintrc(rc));
     252  
     253  	ts->tv_sec = (typeof(ts->tv_sec)) 0xcafef00ddeadbeefLL;
     254  	ts->tv_nsec = (long) 0xbadc0dedfacefeedLL;
     255  	rc = syscall(__NR_io_getevents, bogus_ctx, 0, 0, 0, ts);
     256  	printf("io_getevents(%#lx, 0, 0, NULL"
     257  	       ", {tv_sec=%lld, tv_nsec=%llu}) = %s\n",
     258  	       bogus_ctx, (long long) ts->tv_sec,
     259  	       zero_extend_signed_to_ull(ts->tv_nsec), sprintrc(rc));
     260  
     261  	ts->tv_sec = 0;
     262  	ts->tv_nsec = 123456789;
     263  	rc = syscall(__NR_io_getevents, *ctx, nr, nr + 1, ev, ts);
     264  	printf("io_getevents(%#lx, %ld, %ld, ["
     265  	       "{data=%#" PRI__x64 ", obj=%p, res=%u, res2=0}, "
     266  	       "{data=%#" PRI__x64 ", obj=%p, res=%u, res2=0}"
     267  	       "], {tv_sec=0, tv_nsec=123456789}) = %s\n",
     268  	       *ctx, (long) nr, (long) (nr + 1),
     269  	       cb[0].aio_data, &cb[0], sizeof_data0,
     270  	       cb[1].aio_data, &cb[1], sizeof_data1,
     271  	       sprintrc(rc));
     272  
     273  	rc = syscall(__NR_io_cancel, bogus_ctx, NULL, NULL);
     274  	printf("io_cancel(%#lx, NULL, NULL) = %s\n", bogus_ctx, sprintrc(rc));
     275  
     276  	rc = syscall(__NR_io_cancel, *ctx, cbc + 1, ev);
     277  	printf("io_cancel(%#lx, %p, %p) = %s\n", *ctx, cbc + 1, ev,
     278  	       sprintrc(rc));
     279  
     280  	rc = syscall(__NR_io_cancel, *ctx, cbc, ev);
     281  	printf("io_cancel(%#lx, {aio_data=%#" PRI__x64
     282  		", aio_lio_opcode=IOCB_CMD_PREAD, aio_reqprio=99"
     283  		", aio_fildes=-42}, %p) = %s\n",
     284  	       *ctx, cbc->aio_data, ev, sprintrc(rc));
     285  
     286  	rc = syscall(__NR_io_submit, (unsigned long) 0xfacef157beeff00dULL,
     287  		     (long) 0xdeadc0defacefeedLL, NULL);
     288  	printf("io_submit(%#lx, %ld, NULL) = %s\n",
     289  	       (long) 0xfacef157beeff00dULL,
     290  	       (long) 0xdeadc0defacefeedLL, sprintrc(rc));
     291  
     292  	rc = syscall(__NR_io_submit, *ctx, -1L, cbvs + nr);
     293  	printf("io_submit(%#lx, %ld, %p) = %s\n",
     294  	       *ctx, -1L, cbvs + nr, sprintrc(rc));
     295  
     296  	printf("io_submit(%#lx, %ld, ["
     297  	       "{aio_data=%#" PRI__x64 ", aio_key=%u"
     298  		", aio_lio_opcode=%hu /* IOCB_CMD_??? */, aio_fildes=%d}"
     299  		", {aio_data=0, aio_key=%u, aio_lio_opcode=IOCB_CMD_PWRITE"
     300  		", aio_reqprio=IOPRIO_PRIO_VALUE(0x5 /* IOPRIO_CLASS_??? */"
     301  		", 7919), aio_fildes=%d, aio_buf=NULL"
     302  		", aio_nbytes=%" PRI__u64 ", aio_offset=%" PRI__d64
     303  # ifdef HAVE_STRUCT_IOCB_AIO_FLAGS
     304  		", aio_flags=IOCB_FLAG_RESFD|IOCB_FLAG_IOPRIO|%#x, aio_resfd=%d"
     305  # endif
     306  	       "}, {aio_data=0, aio_key=%u, aio_lio_opcode=IOCB_CMD_PWRITE"
     307  		", aio_reqprio=%hd, aio_fildes=%d, aio_buf=%#" PRI__x64
     308  		", aio_nbytes=%" PRI__u64 ", aio_offset=%" PRI__d64
     309  	       "}, {aio_data=0, aio_key=%u, aio_lio_opcode=IOCB_CMD_PWRITE"
     310  		", aio_reqprio=%hd, aio_fildes=%d"
     311  		", aio_buf=\"\\0\\1\\2\\3%.28s\"..."
     312  		", aio_nbytes=%" PRI__u64 ", aio_offset=%" PRI__d64
     313  	       "}, {aio_data=0, aio_key=%u, aio_lio_opcode=IOCB_CMD_PWRITEV"
     314  		", aio_reqprio=%hd, aio_fildes=%d, aio_buf=%#" PRI__x64
     315  		", aio_nbytes=%" PRI__u64 ", aio_offset=%" PRI__d64
     316  	       "}, NULL, %#lx, ... /* %p */]) = ",
     317  	       *ctx, 1057L,
     318  	       cbv2[0].aio_data, cbv2[0].aio_key,
     319  	       cbv2[0].aio_lio_opcode, cbv2[0].aio_fildes,
     320  	       cbv2[1].aio_key, cbv2[1].aio_fildes,
     321  	       cbv2[1].aio_nbytes, cbv2[1].aio_offset,
     322  # ifdef HAVE_STRUCT_IOCB_AIO_FLAGS
     323  	       cbv2[1].aio_flags & ~3, cbv2[1].aio_resfd,
     324  # endif
     325  	       cbv2[2].aio_key, cbv2[2].aio_reqprio, cbv2[2].aio_fildes,
     326  	       cbv2[2].aio_buf, cbv2[2].aio_nbytes, cbv2[2].aio_offset,
     327  	       cbv2[3].aio_key, cbv2[3].aio_reqprio, cbv2[3].aio_fildes,
     328  	       data2 + 4, cbv2[3].aio_nbytes, cbv2[3].aio_offset,
     329  	       cbv2[4].aio_key, cbv2[4].aio_reqprio, cbv2[4].aio_fildes,
     330  	       cbv2[4].aio_buf, cbv2[4].aio_nbytes, cbv2[4].aio_offset,
     331  	       cbvs2[6], cbvs2 + 7);
     332  	rc = syscall(__NR_io_submit, *ctx, 1057L, cbvs2);
     333  	puts(sprintrc(rc));
     334  
     335  	rc = syscall(__NR_io_submit, *ctx, nr, cbvs);
     336  	if (rc != (long) nr)
     337  		perror_msg_and_skip("io_submit");
     338  	printf("io_submit(%#lx, %u, ["
     339  	       "{aio_data=%#" PRI__x64 ", aio_lio_opcode=IOCB_CMD_PREADV"
     340  		", aio_reqprio=%hd, aio_fildes=0, "
     341  		"aio_buf=[{iov_base=%p, iov_len=%u}"
     342  	       ", {iov_base=%p, iov_len=%u}], aio_offset=%" PRI__d64 "}, "
     343  	       "{aio_data=%#" PRI__x64 ", aio_lio_opcode=IOCB_CMD_PREADV"
     344  		", aio_reqprio=%hd, aio_fildes=0"
     345  		", aio_buf=[{iov_base=%p, iov_len=%u}"
     346  		", {iov_base=%p, iov_len=%u}], aio_offset=%" PRI__d64 "}"
     347  	       "]) = %s\n",
     348  	       *ctx, nr,
     349  	       cbv[0].aio_data, cbv[0].aio_reqprio,
     350  	       iov0[0].iov_base, (unsigned int) iov0[0].iov_len,
     351  	       iov0[1].iov_base, (unsigned int) iov0[1].iov_len,
     352  	       cbv[0].aio_offset,
     353  	       cbv[1].aio_data, cbv[1].aio_reqprio,
     354  	       iov1[0].iov_base, (unsigned int) iov1[0].iov_len,
     355  	       iov1[1].iov_base, (unsigned int) iov1[1].iov_len,
     356  	       cbv[1].aio_offset,
     357  	       sprintrc(rc));
     358  
     359  	rc = syscall(__NR_io_destroy, bogus_ctx);
     360  	printf("io_destroy(%#lx) = %s\n",
     361  	       bogus_ctx, sprintrc(rc));
     362  
     363  	rc = syscall(__NR_io_destroy, *ctx);
     364  	printf("io_destroy(%#lx) = %s\n", *ctx, sprintrc(rc));
     365  
     366  	puts("+++ exited with 0 +++");
     367  	return 0;
     368  }
     369  
     370  #else
     371  
     372  SKIP_MAIN_UNDEFINED("__NR_io_getevents")
     373  
     374  #endif