From 05ac490cc79e2369a60da23e696b67fcd6012762 Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" <ldv@altlinux.org> Date: Fri, 2 Dec 2016 22:16:40 +0000 Subject: [PATCH] Rewrite qual_desc using bit sets As a side effect, this also fixes support of negated sets of descriptors. * defs.h (struct number_set): New forward declaration. (read_set, write_set): New variable prototypes. (is_number_in_set, qualify_read, qualify_write): New function prototypes. (QUAL_READ, QUAL_WRITE): Change to values greater than 0xff. * qualify.c: New file. * Makefile.am (strace_SOURCES): Add it. * syscall.c (qual_desc): Remove. (qual_options): Replace qual_desc with NULL. (qualify): Use qualify_read and qualify_write. (dumpio): Use is_number_in_set. * tests/options-syntax.test: Check invalid sets of descriptors. * tests/readv.test: Check dumping of negated sets of descriptors. --- Makefile.am | 1 + defs.h | 12 ++- qualify.c | 154 ++++++++++++++++++++++++++++++++++++++ syscall.c | 39 +++++----- tests/options-syntax.test | 11 +++ tests/readv.test | 65 +++++++++++++++- 6 files changed, 256 insertions(+), 26 deletions(-) create mode 100644 qualify.c diff --git a/Makefile.am b/Makefile.am index 69dd2be8..2ddb6e2d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -195,6 +195,7 @@ strace_SOURCES = \ process_vm.c \ ptp.c \ ptrace.h \ + qualify.c \ quota.c \ readahead.c \ readlink.c \ diff --git a/defs.h b/defs.h index 1e95a65b..2976c52f 100644 --- a/defs.h +++ b/defs.h @@ -289,8 +289,8 @@ struct tcb { #define QUAL_VERBOSE 0x004 /* decode the structures of this syscall */ #define QUAL_RAW 0x008 /* print all args in hex for this syscall */ #define QUAL_SIGNAL 0x010 /* report events with this signal */ -#define QUAL_READ 0x020 /* dump data read on this file descriptor */ -#define QUAL_WRITE 0x040 /* dump data written to this file descriptor */ +#define QUAL_READ 0x200 /* dump data read from this file descriptor */ +#define QUAL_WRITE 0x400 /* dump data written to this file descriptor */ #define QUAL_FAULT 0x080 /* fail this system call on purpose */ typedef uint8_t qualbits_t; @@ -664,6 +664,14 @@ extern void print_struct_statfs64(struct tcb *tcp, long, unsigned long); extern void print_ifindex(unsigned int); +struct number_set; +extern struct number_set read_set; +extern struct number_set write_set; + +extern bool is_number_in_set(unsigned int number, const struct number_set *); +extern void qualify_read(const char *); +extern void qualify_write(const char *); + extern int dm_ioctl(struct tcb *, const unsigned int, long); extern int file_ioctl(struct tcb *, const unsigned int, long); extern int fs_x_ioctl(struct tcb *, const unsigned int, long); diff --git a/qualify.c b/qualify.c new file mode 100644 index 00000000..15756517 --- /dev/null +++ b/qualify.c @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "defs.h" + +typedef unsigned int number_slot_t; +#define BITS_PER_SLOT (sizeof(number_slot_t) * 8) + +struct number_set { + number_slot_t *vec; + unsigned int nslots; + bool not; +}; + +struct number_set read_set; +struct number_set write_set; + +static void +number_setbit(const unsigned int i, number_slot_t *const vec) +{ + vec[i / BITS_PER_SLOT] |= (number_slot_t) 1 << (i % BITS_PER_SLOT); +} + +static bool +number_isset(const unsigned int i, const number_slot_t *const vec) +{ + return vec[i / BITS_PER_SLOT] & ((number_slot_t) 1 << (i % BITS_PER_SLOT)); +} + +static void +reallocate_number_set(struct number_set *const set, const unsigned int new_nslots) +{ + if (new_nslots <= set->nslots) + return; + set->vec = xreallocarray(set->vec, new_nslots, sizeof(*set->vec)); + memset(set->vec + set->nslots, 0, + sizeof(*set->vec) * (new_nslots - set->nslots)); + set->nslots = new_nslots; +} + +static void +add_number_to_set(const unsigned int number, struct number_set *const set) +{ + reallocate_number_set(set, number / BITS_PER_SLOT + 1); + number_setbit(number, set->vec); +} + +bool +is_number_in_set(const unsigned int number, const struct number_set *const set) +{ + return ((number / BITS_PER_SLOT < set->nslots) + && number_isset(number, set->vec)) ^ set->not; +} + +typedef int (*string_to_uint_func)(const char *); + +/* + * Add numbers to SET according to STR specification. + */ +static void +qualify_tokens(const char *const str, struct number_set *const set, + string_to_uint_func func, const char *const name) +{ + /* Clear the set. */ + if (set->nslots) + memset(set->vec, 0, sizeof(*set->vec) * set->nslots); + set->not = false; + + /* + * Each leading ! character means inversion + * of the remaining specification. + */ + const char *s = str; +handle_inversion: + while (*s == '!') { + set->not = !set->not; + ++s; + } + + if (strcmp(s, "none") == 0) { + /* + * No numbers are added to the set. + * Subsequent is_number_in_set invocations will return set->not. + */ + return; + } else if (strcmp(s, "all") == 0) { + s = "!none"; + goto handle_inversion; + } + + /* + * Split the string into comma separated tokens. + * For each token, find out the corresponding number + * by calling FUNC, and add that number to the set. + * The absence of tokens or a negative answer + * from FUNC is a fatal error. + */ + char *copy = xstrdup(s); + char *saveptr = NULL; + const char *token; + int number = -1; + + for (token = strtok_r(copy, ",", &saveptr); token; + token = strtok_r(NULL, ",", &saveptr)) { + number = func(token); + if (number < 0) { + error_msg_and_die("invalid %s '%s'", name, token); + } + + add_number_to_set(number, set); + } + + free(copy); + + if (number < 0) { + error_msg_and_die("invalid %s '%s'", name, str); + } +} + +void +qualify_read(const char *const str) +{ + qualify_tokens(str, &read_set, string_to_uint, "descriptor"); +} + +void +qualify_write(const char *const str) +{ + qualify_tokens(str, &write_set, string_to_uint, "descriptor"); +} diff --git a/syscall.c b/syscall.c index fed5d7cd..590a8a65 100644 --- a/syscall.c +++ b/syscall.c @@ -359,7 +359,6 @@ update_personality(struct tcb *tcp, unsigned int personality) } #endif -static int qual_desc(const char *, unsigned int, int); static int qual_fault(const char *, unsigned int, int); static int qual_signal(const char *, unsigned int, int); static int qual_syscall(const char *, unsigned int, int); @@ -381,12 +380,12 @@ static const struct qual_options { { QUAL_SIGNAL, "signal", qual_signal, "signal" }, { QUAL_SIGNAL, "signals", qual_signal, "signal" }, { QUAL_SIGNAL, "s", qual_signal, "signal" }, - { QUAL_READ, "read", qual_desc, "descriptor" }, - { QUAL_READ, "reads", qual_desc, "descriptor" }, - { QUAL_READ, "r", qual_desc, "descriptor" }, - { QUAL_WRITE, "write", qual_desc, "descriptor" }, - { QUAL_WRITE, "writes", qual_desc, "descriptor" }, - { QUAL_WRITE, "w", qual_desc, "descriptor" }, + { QUAL_READ, "read", NULL, "descriptor" }, + { QUAL_READ, "reads", NULL, "descriptor" }, + { QUAL_READ, "r", NULL, "descriptor" }, + { QUAL_WRITE, "write", NULL, "descriptor" }, + { QUAL_WRITE, "writes", NULL, "descriptor" }, + { QUAL_WRITE, "w", NULL, "descriptor" }, { QUAL_FAULT, "fault", qual_fault, "fault argument"}, { 0, NULL, NULL, NULL }, }; @@ -729,16 +728,6 @@ qual_signal(const char *s, const unsigned int bitflag, const int not) return -1; } -static int -qual_desc(const char *s, const unsigned int bitflag, const int not) -{ - int desc = string_to_uint_upto(s, 0x7fff); - if (desc < 0) - return -1; - qualify_one(desc, bitflag, not, -1, NULL); - return 0; -} - void qualify(const char *s) { @@ -762,6 +751,16 @@ qualify(const char *s) break; } } + + switch (opt->bitflag) { + case QUAL_READ: + qualify_read(s); + return; + case QUAL_WRITE: + qualify_write(s); + return; + } + not = 0; if (*s == '!') { not = 1; @@ -898,10 +897,10 @@ dumpio(struct tcb *tcp) return; int fd = tcp->u_arg[0]; - if (fd < 0 || (unsigned int) fd >= num_quals) + if (fd < 0) return; - if (qual_flags[fd] & QUAL_READ) { + if (is_number_in_set(fd, &read_set)) { switch (tcp->s_ent->sen) { case SEN_read: case SEN_pread: @@ -924,7 +923,7 @@ dumpio(struct tcb *tcp) return; } } - if (qual_flags[fd] & QUAL_WRITE) { + if (is_number_in_set(fd, &write_set)) { switch (tcp->s_ent->sen) { case SEN_write: case SEN_pwrite: diff --git a/tests/options-syntax.test b/tests/options-syntax.test index 45663114..ee8ed3df 100755 --- a/tests/options-syntax.test +++ b/tests/options-syntax.test @@ -67,6 +67,17 @@ check_e "Invalid process id: 'a'" -p 1,a check_e "Syscall 'chdir' for -b isn't supported" -b chdir check_e "Syscall 'chdir' for -b isn't supported" -b execve -b chdir +check_e "invalid descriptor '-1'" -eread=-1 +check_e "invalid descriptor '-42'" -ewrite=-42 +check_e "invalid descriptor '2147483648'" -eread=2147483648 +check_e "invalid descriptor '4294967296'" -ewrite=4294967296 +check_e "invalid descriptor 'foo'" -eread=foo +check_e "invalid descriptor ''" -ewrite= +check_e "invalid descriptor ','" -eread=, +check_e "invalid descriptor '!'" -ewrite='!' +check_e "invalid descriptor '!'" -eread='0,!' +check_e "invalid descriptor '!,'" -ewrite='!,' + check_h 'must have PROG [ARGS] or -p PID' check_h 'PROG [ARGS] must be specified with -D' -D -p $$ check_h '-c and -C are mutually exclusive' -c -C true diff --git a/tests/readv.test b/tests/readv.test index 888f1b50..dc187996 100755 --- a/tests/readv.test +++ b/tests/readv.test @@ -1,8 +1,65 @@ #!/bin/sh - +# # Check decoding and dumping of readv and writev syscalls. +# +# Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org> +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# 3. The name of the author may not be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + . "${srcdir=.}/init.sh" -maxfd="$(./print_maxfd)" -run_strace_match_diff \ - -a16 -eread="$(($maxfd - 1))" -ewrite="$maxfd" -e trace=readv,writev +run_strace_match_diff -a16 -e trace=readv,writev \ + -eread=all -ewrite='!none' +run_strace_match_diff -a16 -e trace=readv,writev \ + -eread='!none' -ewrite=all +run_strace_match_diff -a16 -e trace=readv,writev \ + -eread=none -ewrite='!all' -eread='!0,1,2' -ewrite='!0,1,2' +run_strace_match_diff -a16 -e trace=readv,writev \ + -eread='!all' -ewrite=none -eread='!0,1,2' -ewrite='!0,1,2' + +wfd="$(./print_maxfd)" +rfd="$(($wfd - 1))" +run_strace_match_diff -a16 -e trace=readv,writev \ + -eread="$rfd" -ewrite="$wfd" +run_strace_match_diff -a16 -e trace=readv,writev \ + -eread="!$rfd" -ewrite="!$wfd" -eread="$rfd" -ewrite="$wfd" + +rfds='!!!0' +[ $rfd -lt 1023 ] || rfd=1023 +i=0 +while [ $i -lt $rfd ]; do + rfds="$rfds,$i" + i=$(($i + 1)) +done + +wfds='!!!0' +[ $wfd -lt 1023 ] || wfd=1023 +i=0 +while [ $i -lt $wfd ]; do + wfds="$wfds,$i" + i=$(($i + 1)) +done + +run_strace_match_diff -a16 -e trace=readv,writev \ + -eread="$rfds" -ewrite="$wfds" -- 2.40.0