<div dir="ltr"><div><div><div>From c40ccc35dc8e40fc6dc331af3d1884a54bc72784 Mon Sep 17 00:00:00 2001</div><div>From: JingPiao Chen <<a href="mailto:chenjingpiao@gmail.com">chenjingpiao@gmail.com</a>></div><div>Date: Fri, 16 Dec 2016 00:02:55 +0800</div><div>Subject: [PATCH] tests: check decoding of LOOP_* ioctls</div><div><br></div><div>* tests/ioctl_loop.c: New file.</div><div>* tests/ioctl_loop-v.c: Likewise.</div><div>* tests/ioctl_loop.test: New test.</div><div>* tests/ioctl_loop-v.test: Likewise.</div><div>* tests/.gitignore: Add ioctl_loop and ioctl_loop-v.</div><div>* tests/Makefile.am (check_PROGRAMS): Likewise.</div><div>(DECODER_TESTS): Add ioctl_loop.test and ioctl_loop-v.test.</div><div>---</div><div> tests/.gitignore        |   2 +</div><div> tests/Makefile.am       |   4 ++</div><div> tests/ioctl_loop-v.c    |   2 +</div><div> tests/ioctl_loop-v.test |  12 ++++</div><div> tests/ioctl_loop.c      | 184 ++++++++++++++++++++++++++++++++++++++++++++++++</div><div> tests/ioctl_loop.test   |  12 ++++</div><div> 6 files changed, 216 insertions(+)</div><div> create mode 100644 tests/ioctl_loop-v.c</div><div> create mode 100755 tests/ioctl_loop-v.test</div><div> create mode 100644 tests/ioctl_loop.c</div><div> create mode 100755 tests/ioctl_loop.test</div><div><br></div><div>diff --git a/tests/.gitignore b/tests/.gitignore</div><div>index 0d7ec86..a206c75 100644</div><div>--- a/tests/.gitignore</div><div>+++ b/tests/.gitignore</div><div>@@ -118,6 +118,8 @@ ioctl_dm-v</div><div> ioctl_evdev</div><div> ioctl_evdev-v</div><div> ioctl_mtd</div><div>+ioctl_loop</div><div>+ioctl_loop-v</div><div> ioctl_rtc</div><div> ioctl_rtc-v</div><div> ioctl_uffdio</div><div>diff --git a/tests/Makefile.am b/tests/Makefile.am</div><div>index 90851a4..410d3f2 100644</div><div>--- a/tests/Makefile.am</div><div>+++ b/tests/Makefile.am</div><div>@@ -180,6 +180,8 @@ check_PROGRAMS = \</div><div> <span class="gmail-Apple-tab-span" style="white-space:pre">    </span>ioctl_evdev \</div><div> <span class="gmail-Apple-tab-span" style="white-space:pre">        </span>ioctl_evdev-v \</div><div> <span class="gmail-Apple-tab-span" style="white-space:pre">      </span>ioctl_mtd \</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>ioctl_loop \</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>ioctl_loop-v \</div><div> <span class="gmail-Apple-tab-span" style="white-space:pre">       </span>ioctl_rtc \</div><div> <span class="gmail-Apple-tab-span" style="white-space:pre">  </span>ioctl_rtc-v \</div><div> <span class="gmail-Apple-tab-span" style="white-space:pre">        </span>ioctl_uffdio \</div><div>@@ -566,6 +568,8 @@ DECODER_TESTS = \</div><div> <span class="gmail-Apple-tab-span" style="white-space:pre">   </span>ioctl_evdev.test \</div><div> <span class="gmail-Apple-tab-span" style="white-space:pre">   </span>ioctl_evdev-v.test \</div><div> <span class="gmail-Apple-tab-span" style="white-space:pre"> </span>ioctl_mtd.test \</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>ioctl_loop.test \</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>ioctl_loop-v.test \</div><div> <span class="gmail-Apple-tab-span" style="white-space:pre">  </span>ioctl_rtc.test \</div><div> <span class="gmail-Apple-tab-span" style="white-space:pre">     </span>ioctl_rtc-v.test \</div><div> <span class="gmail-Apple-tab-span" style="white-space:pre">   </span>ioctl_uffdio.test \</div><div>diff --git a/tests/ioctl_loop-v.c b/tests/ioctl_loop-v.c</div><div>new file mode 100644</div><div>index 0000000..22e7572</div><div>--- /dev/null</div><div>+++ b/tests/ioctl_loop-v.c</div><div>@@ -0,0 +1,2 @@</div><div>+#define VERBOSE 1</div><div>+#include "ioctl_loop.c"</div><div>diff --git a/tests/ioctl_loop-v.test b/tests/ioctl_loop-v.test</div><div>new file mode 100755</div><div>index 0000000..7faa859</div><div>--- /dev/null</div><div>+++ b/tests/ioctl_loop-v.test</div><div>@@ -0,0 +1,12 @@</div><div>+#!/bin/sh</div><div>+</div><div>+# Check verbose decoding LOOP_* ioctls.</div><div>+</div><div>+. "${srcdir=.}/init.sh"</div><div>+</div><div>+run_prog > /dev/null</div><div>+run_strace -a22 -veioctl $args > "$EXP"</div><div>+check_prog grep</div><div>+grep -v '^ioctl([012],' < "$LOG" > "$OUT"</div><div>+match_diff "$OUT" "$EXP"</div><div>+rm -f "$EXP" "$OUT"</div><div>diff --git a/tests/ioctl_loop.c b/tests/ioctl_loop.c</div><div>new file mode 100644</div><div>index 0000000..323468a</div><div>--- /dev/null</div><div>+++ b/tests/ioctl_loop.c</div><div>@@ -0,0 +1,184 @@</div><div>+/*</div><div>+ * This file is part of ioctl_loop strace test.</div><div>+ *</div><div>+ * Copyright (c) 2016 JingPiao Chen <<a href="mailto:chenjingpiao@gmail.com">chenjingpiao@gmail.com</a>></div><div>+ * All rights reserved.</div><div>+ *</div><div>+ * Redistribution and use in source and binary forms, with or without</div><div>+ * modification, are permitted provided that the following conditions</div><div>+ * are met:</div><div>+ * 1. Redistributions of source code must retain the above copyright</div><div>+ *    notice, this list of conditions and the following disclaimer.</div><div>+ * 2. Redistributions in binary form must reproduce the above copyright</div><div>+ *    notice, this list of conditions and the following disclaimer in the</div><div>+ *    documentation and/or other materials provided with the distribution.</div><div>+ * 3. The name of the author may not be used to endorse or promote products</div><div>+ *    derived from this software without specific prior written permission.</div><div>+ *</div><div>+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR</div><div>+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES</div><div>+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.</div><div>+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,</div><div>+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT</div><div>+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,</div><div>+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY</div><div>+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT</div><div>+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF</div><div>+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</div><div>+ */</div><div>+</div><div>+</div><div>+#include "tests.h"</div><div>+#include <stdio.h></div><div>+#include <string.h></div><div>+#include <inttypes.h></div><div>+#include <sys/ioctl.h></div><div>+#include <linux/loop.h></div><div>+</div><div>+# ifndef VERBOSE</div><div>+#  define VERBOSE 0</div><div>+# endif</div><div>+</div><div>+static const unsigned int magic = 0xdeadbeef;</div><div>+static const unsigned long lmagic = (unsigned long) 0xdeadbeefbadc0dedULL;</div><div>+</div><div>+static void</div><div>+init_magic(void *addr, const unsigned int size)</div><div>+{</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>unsigned int *p = addr;</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">       </span>const unsigned int *end = addr + size - sizeof(int);</div><div>+</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>for (; p <= end; ++p)</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">              </span>*(unsigned int *) p = magic;</div><div>+}</div><div>+</div><div>+int</div><div>+main(void)</div><div>+{</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>ioctl(-1, LOOP_SET_FD, magic);</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">        </span>printf("ioctl(-1, LOOP_SET_FD, %d) = -1 EBADF (%m)\n", magic);</div><div>+</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>ioctl(-1, LOOP_CLR_FD, 0);</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">    </span>printf("ioctl(-1, LOOP_CLR_FD) = -1 EBADF (%m)\n");</div><div>+</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>struct loop_info *const info = tail_alloc(sizeof(*info));</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>init_magic(info, sizeof(*info));</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>info->lo_encrypt_type = LO_CRYPT_NONE;</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>info->lo_flags = LO_FLAGS_READ_ONLY;</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">       </span>memset(info->lo_name, 'A', sizeof(info->lo_name));</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>memset(info->lo_encrypt_key, 'B', sizeof(info->lo_encrypt_key));</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">        </span>ioctl(-1, LOOP_SET_STATUS, info);</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>printf("ioctl(-1, LOOP_SET_STATUS, {lo_number=%d", info->lo_number);</div><div>+# if VERBOSE</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>printf(", lo_device=%#lx, lo_inode=%lu, lo_rdevice=%#lx",</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                   </span>(unsigned long) info->lo_device,</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                   </span>info->lo_inode,</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                    </span>(unsigned long) info->lo_rdevice);</div><div>+# endif /* VERBOSE */</div><div>+</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">    </span>printf(", lo_offset=%#x", info->lo_offset);</div><div>+</div><div>+# if VERBOSE</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>printf(", lo_encrypt_type=LO_CRYPT_NONE, lo_encrypt_key_size=%d",</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                   </span>info->lo_encrypt_key_size);</div><div>+#endif /* VERBOSE */</div><div>+</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">    </span>printf(", lo_flags=LO_FLAGS_READ_ONLY, lo_name=\"%.*s\"",</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                 </span>(int) sizeof(info->lo_name) - 1, info->lo_name);</div><div>+</div><div>+# if VERBOSE</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">    </span>printf(", lo_encrypt_key=\"%.*s\"",</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                       </span>(int) sizeof(info->lo_encrypt_key),</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                        </span>info->lo_encrypt_key);</div><div>+#endif /* VERBOSE */</div><div>+# if VERBOSE</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>printf(", lo_init=[%#lx, %#lx]"</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>       ", reserved=[%#x, %#x, %#x, %#x]}",</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">       </span>       info->lo_init[0], info->lo_init[1],</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>       info->reserved[0], info->reserved[1],</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>       info->reserved[2], info->reserved[3]);</div><div>+# else /* !VERBOSE */</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>printf(", ...}");</div><div>+#endif /* VERBOSE */</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">       </span>printf(") = -1 EBADF (%m)\n");</div><div>+</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>ioctl(-1, LOOP_GET_STATUS, info);</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>printf("ioctl(-1, LOOP_GET_STATUS, %#llx) = -1 EBADF (%m)\n",</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                       </span>(long long) info);</div><div>+</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">    </span>struct loop_info64 *const info64 = tail_alloc(sizeof(*info64));</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">       </span>init_magic(info64, sizeof(*info64));</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>info64->lo_flags = LO_FLAGS_READ_ONLY;</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>info64->lo_encrypt_type = LO_CRYPT_NONE;</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>memset(info64->lo_file_name, 'C', sizeof(info64->lo_file_name));</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">        </span>memset(info64->lo_crypt_name, 'D', sizeof(info64->lo_crypt_name));</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>memset(info64->lo_encrypt_key, 'E', sizeof(info64->lo_encrypt_key));</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">    </span>ioctl(-1, LOOP_SET_STATUS64, info64);</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>printf("ioctl(-1, LOOP_SET_STATUS64, ");</div><div>+# if VERBOSE</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">        </span>printf("{lo_device=%" PRIu64 ", lo_inode=%" PRIu64</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">        </span>       ", lo_rdevice=%" PRIu64 ", lo_offset=%#" PRIx64</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>       ", lo_sizelimit=%" PRIu64 ", lo_number=%" PRIu32,</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">       </span>       (uint64_t) info64->lo_device,</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">    </span>       (uint64_t) info64->lo_inode,</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>       (uint64_t) info64->lo_rdevice,</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>       (uint64_t) info64->lo_offset,</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">    </span>       (uint64_t) info64->lo_sizelimit,</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>       (uint32_t) info64->lo_number);</div><div>+#else /* !VERBOSE */</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">       </span>printf("{lo_offset=%#" PRIx64 ", lo_number=%" PRIu32,</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                     </span>(uint64_t) info64->lo_offset,</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                      </span>(uint32_t) info64->lo_number);</div><div>+#endif /* VERBOSE */</div><div>+</div><div>+# if VERBOSE</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>printf(", lo_encrypt_type=LO_CRYPT_NONE, lo_encrypt_key_size=%" PRIu32,</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                     </span>info64->lo_encrypt_key_size);</div><div>+# endif /* VERBOSE */</div><div>+</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>printf(", lo_flags=LO_FLAGS_READ_ONLY, lo_file_name=\"%.*s\"",</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                    </span>(int) sizeof(info64->lo_file_name) - 1, info64->lo_file_name);</div><div>+</div><div>+# if VERBOSE</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>printf(", lo_crypt_name=\"%.*s\", lo_encrypt_key=\"%.*s\"",</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                     </span>(int) sizeof(info64->lo_crypt_name) - 1,</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                   </span>info64->lo_crypt_name,</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                     </span>(int) sizeof(info64->lo_encrypt_key),</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                      </span>info64->lo_encrypt_key);</div><div>+# endif /* VERBOSE */</div><div>+</div><div>+# if VERBOSE</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>printf(", lo_init=[%#" PRIx64 ", %#" PRIx64 "]}",</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                       </span>(uint64_t) info64->lo_init[0],</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                     </span>(uint64_t) info64->lo_init[1]);</div><div>+# else /* !VERBOSE */</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">       </span>printf(", ...}");</div><div>+# endif /* VERBOSE */</div><div>+</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>printf(") = -1 EBADF (%m)\n");</div><div>+</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>ioctl(-1, LOOP_GET_STATUS64, info64);</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>printf("ioctl(-1, LOOP_GET_STATUS64, %#llx) = -1 EBADF (%m)\n",</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">                     </span>(long long) info64);</div><div>+</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>ioctl(-1, LOOP_CHANGE_FD, magic);</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>printf("ioctl(-1, LOOP_CHANGE_FD, %d) = -1 EBADF (%m)\n", magic);</div><div>+</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>ioctl(-1, LOOP_SET_CAPACITY, 0);</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>printf("ioctl(-1, LOOP_SET_CAPACITY) = -1 EBADF (%m)\n");</div><div>+</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>ioctl(-1, LOOP_SET_DIRECT_IO, lmagic);</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">        </span>printf("ioctl(-1, LOOP_SET_DIRECT_IO, %lu) = -1 EBADF (%m)\n", lmagic);</div><div>+</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>ioctl(-1, LOOP_CTL_ADD, magic);</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">       </span>printf("ioctl(-1, LOOP_CTL_ADD, %d) = -1 EBADF (%m)\n", magic);</div><div>+</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>ioctl(-1, LOOP_CTL_REMOVE, magic);</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">    </span>printf("ioctl(-1, LOOP_CTL_REMOVE, %d) = -1 EBADF (%m)\n", magic);</div><div>+</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">  </span>ioctl(-1, LOOP_CTL_GET_FREE, 0);</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>printf("ioctl(-1, LOOP_CTL_GET_FREE) = -1 EBADF (%m)\n");</div><div>+</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>puts("+++ exited with 0 +++");</div><div>+<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>return 0;</div><div>+}</div><div>diff --git a/tests/ioctl_loop.test b/tests/ioctl_loop.test</div><div>new file mode 100755</div><div>index 0000000..112cd63</div><div>--- /dev/null</div><div>+++ b/tests/ioctl_loop.test</div><div>@@ -0,0 +1,12 @@</div><div>+#!/bin/sh</div><div>+</div><div>+# Check decoding of LOOP_* ioctls.</div><div>+</div><div>+. "${srcdir=.}/init.sh"</div><div>+</div><div>+run_prog > /dev/null</div><div>+run_strace -a22 -eioctl $args > "$EXP"</div><div>+check_prog grep</div><div>+grep -v '^ioctl([012],' < "$LOG" > "$OUT"</div><div>+match_diff "$OUT" "$EXP"</div><div>+rm -f "$EXP" "$OUT"</div><div>-- </div><div>2.7.4</div></div></div><div><br></div></div>