]> granicus.if.org Git - strace/blobdiff - filter_qualify.c
mmap_cache: add function to enable mmap_cache
[strace] / filter_qualify.c
index 3ed0cb59174ccab606e7c0a4543bd1fc47d210df..49f0d612046384aef2ef28461d56c33fed6a05e0 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
- * Copyright (c) 2016-2017 The strace developers.
+ * Copyright (c) 2016-2018 The strace developers.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 #include "number_set.h"
 #include "filter.h"
 
-struct number_set read_set;
-struct number_set write_set;
-struct number_set signal_set;
+struct number_set *read_set;
+struct number_set *write_set;
+struct number_set *signal_set;
 
-static struct number_set abbrev_set[SUPPORTED_PERSONALITIES];
-static struct number_set inject_set[SUPPORTED_PERSONALITIES];
-static struct number_set raw_set[SUPPORTED_PERSONALITIES];
-static struct number_set trace_set[SUPPORTED_PERSONALITIES];
-static struct number_set verbose_set[SUPPORTED_PERSONALITIES];
+static struct number_set *abbrev_set;
+static struct number_set *inject_set;
+static struct number_set *raw_set;
+static struct number_set *trace_set;
+static struct number_set *verbose_set;
 
 static int
 sigstr_to_uint(const char *s)
 {
-       int i;
-
        if (*s >= '0' && *s <= '9')
                return string_to_uint_upto(s, 255);
 
        if (strncasecmp(s, "SIG", 3) == 0)
                s += 3;
 
-       for (i = 0; i <= 255; ++i) {
+       for (int i = 0; i <= 255; ++i) {
                const char *name = signame(i);
 
                if (strncasecmp(name, "SIG", 3) != 0)
@@ -72,9 +70,7 @@ sigstr_to_uint(const char *s)
 static int
 find_errno_by_name(const char *name)
 {
-       unsigned int i;
-
-       for (i = 1; i < nerrnos; ++i) {
+       for (unsigned int i = 1; i < nerrnos; ++i) {
                if (errnoent[i] && (strcasecmp(name, errnoent[i]) == 0))
                        return i;
        }
@@ -87,7 +83,7 @@ parse_inject_token(const char *const token, struct inject_opts *const fopts,
                   const bool fault_tokens_only)
 {
        const char *val;
-       int intval;
+       kernel_long_t intval;
 
        if ((val = STR_STRIP_PREFIX(token, "when=")) != token) {
                /*
@@ -120,28 +116,47 @@ parse_inject_token(const char *const token, struct inject_opts *const fopts,
                        fopts->step = 0;
                }
        } else if ((val = STR_STRIP_PREFIX(token, "error=")) != token) {
-               if (fopts->rval != INJECT_OPTS_RVAL_DEFAULT)
+               if (fopts->data.flags & INJECT_F_RETVAL)
                        return false;
                intval = string_to_uint_upto(val, MAX_ERRNO_VALUE);
                if (intval < 0)
                        intval = find_errno_by_name(val);
                if (intval < 1)
                        return false;
-               fopts->rval = -intval;
+               fopts->data.rval = -intval;
+               fopts->data.flags |= INJECT_F_RETVAL;
        } else if (!fault_tokens_only
                   && (val = STR_STRIP_PREFIX(token, "retval=")) != token) {
-               if (fopts->rval != INJECT_OPTS_RVAL_DEFAULT)
+               if (fopts->data.flags & INJECT_F_RETVAL)
                        return false;
-               intval = string_to_uint(val);
+               intval = string_to_kulong(val);
                if (intval < 0)
                        return false;
-               fopts->rval = intval;
+
+#if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG && !HAVE_ARCH_DEDICATED_ERR_REG
+               if ((int) intval != intval)
+                       error_msg("Injected return value %" PRI_kld " will be"
+                                 " clipped to %d in compat personality",
+                                 intval, (int) intval);
+
+               if ((int) intval < 0 && (int) intval >= -MAX_ERRNO_VALUE)
+                       error_msg("Inadvertent injection of error %d is"
+                                 " possible in compat personality for"
+                                 " retval=%" PRI_kld,
+                                 -(int) intval, intval);
+#endif
+
+               fopts->data.rval = intval;
+               fopts->data.flags |= INJECT_F_RETVAL;
        } else if (!fault_tokens_only
                   && (val = STR_STRIP_PREFIX(token, "signal=")) != token) {
+               if (fopts->data.flags & INJECT_F_SIGNAL)
+                       return false;
                intval = sigstr_to_uint(val);
                if (intval < 1 || intval > NSIG_BYTES * 8)
                        return false;
-               fopts->signo = intval;
+               fopts->data.signo = intval;
+               fopts->data.flags |= INJECT_F_SIGNAL;
        } else {
                return false;
        }
@@ -149,72 +164,80 @@ parse_inject_token(const char *const token, struct inject_opts *const fopts,
        return true;
 }
 
-static char *
-parse_inject_expression(const char *const s, char **buf,
+static const char *
+parse_inject_expression(char *const str,
                        struct inject_opts *const fopts,
                        const bool fault_tokens_only)
 {
+       if (str[0] == '\0' || str[0] == ':')
+               return "";
+
        char *saveptr = NULL;
-       char *name = NULL;
-       char *token;
+       const char *name = strtok_r(str, ":", &saveptr);
 
-       *buf = xstrdup(s);
-       for (token = strtok_r(*buf, ":", &saveptr); token;
-            token = strtok_r(NULL, ":", &saveptr)) {
-               if (!name)
-                       name = token;
-               else if (!parse_inject_token(token, fopts, fault_tokens_only))
-                       goto parse_error;
+       char *token;
+       while ((token = strtok_r(NULL, ":", &saveptr))) {
+               if (!parse_inject_token(token, fopts, fault_tokens_only))
+                       return NULL;
        }
 
-       if (name)
-               return name;
-
-parse_error:
-       free(*buf);
-       return *buf = NULL;
+       return name;
 }
 
 static void
 qualify_read(const char *const str)
 {
-       qualify_tokens(str, &read_set, string_to_uint, "descriptor");
+       if (!read_set)
+               read_set = alloc_number_set_array(1);
+       qualify_tokens(str, read_set, string_to_uint, "descriptor");
 }
 
 static void
 qualify_write(const char *const str)
 {
-       qualify_tokens(str, &write_set, string_to_uint, "descriptor");
+       if (!write_set)
+               write_set = alloc_number_set_array(1);
+       qualify_tokens(str, write_set, string_to_uint, "descriptor");
 }
 
 static void
 qualify_signals(const char *const str)
 {
-       qualify_tokens(str, &signal_set, sigstr_to_uint, "signal");
+       if (!signal_set)
+               signal_set = alloc_number_set_array(1);
+       qualify_tokens(str, signal_set, sigstr_to_uint, "signal");
 }
 
 static void
 qualify_trace(const char *const str)
 {
-       qualify_syscall_tokens(str, trace_set, "system call");
+       if (!trace_set)
+               trace_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
+       qualify_syscall_tokens(str, trace_set);
 }
 
 static void
 qualify_abbrev(const char *const str)
 {
-       qualify_syscall_tokens(str, abbrev_set, "system call");
+       if (!abbrev_set)
+               abbrev_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
+       qualify_syscall_tokens(str, abbrev_set);
 }
 
 static void
 qualify_verbose(const char *const str)
 {
-       qualify_syscall_tokens(str, verbose_set, "system call");
+       if (!verbose_set)
+               verbose_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
+       qualify_syscall_tokens(str, verbose_set);
 }
 
 static void
 qualify_raw(const char *const str)
 {
-       qualify_syscall_tokens(str, raw_set, "system call");
+       if (!raw_set)
+               raw_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
+       qualify_syscall_tokens(str, raw_set);
 }
 
 static void
@@ -224,58 +247,58 @@ qualify_inject_common(const char *const str,
 {
        struct inject_opts opts = {
                .first = 1,
-               .step = 1,
-               .rval = INJECT_OPTS_RVAL_DEFAULT,
-               .signo = 0
+               .step = 1
        };
-       char *buf = NULL;
-       char *name = parse_inject_expression(str, &buf, &opts, fault_tokens_only);
-       if (!name) {
+       char *copy = xstrdup(str);
+       const char *name =
+               parse_inject_expression(copy, &opts, fault_tokens_only);
+       if (!name)
                error_msg_and_die("invalid %s '%s'", description, str);
-       }
+
+       struct number_set *tmp_set =
+               alloc_number_set_array(SUPPORTED_PERSONALITIES);
+       qualify_syscall_tokens(name, tmp_set);
+
+       free(copy);
 
        /* If neither of retval, error, or signal is specified, then ... */
-       if (opts.rval == INJECT_OPTS_RVAL_DEFAULT && !opts.signo) {
+       if (!opts.data.flags) {
                if (fault_tokens_only) {
                        /* in fault= syntax the default error code is ENOSYS. */
-                       opts.rval = -ENOSYS;
+                       opts.data.rval = -ENOSYS;
+                       opts.data.flags |= INJECT_F_RETVAL;
                } else {
                        /* in inject= syntax this is not allowed. */
                        error_msg_and_die("invalid %s '%s'", description, str);
                }
        }
 
-       struct number_set tmp_set[SUPPORTED_PERSONALITIES];
-       memset(tmp_set, 0, sizeof(tmp_set));
-       qualify_syscall_tokens(name, tmp_set, description);
-
-       free(buf);
-
        /*
-        * Initialize inject_vec accourding to tmp_set.
+        * Initialize inject_vec according to tmp_set.
         * Merge tmp_set into inject_set.
         */
-       unsigned int p;
-       for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
-               if (!tmp_set[p].nslots && !tmp_set[p].not) {
+       for (unsigned int p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
+               if (number_set_array_is_empty(tmp_set, p))
                        continue;
-               }
 
+               if (!inject_set) {
+                       inject_set =
+                               alloc_number_set_array(SUPPORTED_PERSONALITIES);
+               }
                if (!inject_vec[p]) {
                        inject_vec[p] = xcalloc(nsyscall_vec[p],
-                                              sizeof(*inject_vec[p]));
+                                               sizeof(*inject_vec[p]));
                }
 
-               unsigned int i;
-               for (i = 0; i < nsyscall_vec[p]; ++i) {
-                       if (is_number_in_set(i, &tmp_set[p])) {
-                               add_number_to_set(i, &inject_set[p]);
+               for (unsigned int i = 0; i < nsyscall_vec[p]; ++i) {
+                       if (is_number_in_set_array(i, tmp_set, p)) {
+                               add_number_to_set_array(i, inject_set, p);
                                inject_vec[p][i] = opts;
                        }
                }
-
-               free(tmp_set[p].vec);
        }
+
+       free_number_set_array(tmp_set, SUPPORTED_PERSONALITIES);
 }
 
 static void
@@ -319,9 +342,8 @@ void
 qualify(const char *str)
 {
        const struct qual_options *opt = qual_options;
-       unsigned int i;
 
-       for (i = 0; i < ARRAY_SIZE(qual_options); ++i) {
+       for (unsigned int i = 0; i < ARRAY_SIZE(qual_options); ++i) {
                const char *name = qual_options[i].name;
                const size_t len = strlen(name);
                const char *val = str_strip_prefix_len(str, name, len);
@@ -339,14 +361,14 @@ qualify(const char *str)
 unsigned int
 qual_flags(const unsigned int scno)
 {
-       return  (is_number_in_set(scno, &trace_set[current_personality])
+       return  (is_number_in_set_array(scno, trace_set, current_personality)
                   ? QUAL_TRACE : 0)
-               | (is_number_in_set(scno, &abbrev_set[current_personality])
+               | (is_number_in_set_array(scno, abbrev_set, current_personality)
                   ? QUAL_ABBREV : 0)
-               | (is_number_in_set(scno, &verbose_set[current_personality])
+               | (is_number_in_set_array(scno, verbose_set, current_personality)
                   ? QUAL_VERBOSE : 0)
-               | (is_number_in_set(scno, &raw_set[current_personality])
+               | (is_number_in_set_array(scno, raw_set, current_personality)
                   ? QUAL_RAW : 0)
-               | (is_number_in_set(scno, &inject_set[current_personality])
+               | (is_number_in_set_array(scno, inject_set, current_personality)
                   ? QUAL_INJECT : 0);
 }