Commit 7104f300 authored by Steven Rostedt's avatar Steven Rostedt Committed by Ingo Molnar

ftrace: type cast filter+verifier

The mmiotrace map had a bug that would typecast the entry from
the trace to the wrong type. That is a known danger of C typecasts,
there's absolutely zero checking done on them.

Help that problem a bit by using a GCC extension to implement a
type filter that restricts the types that a trace record can be
cast into, and by adding a dynamic check (in debug mode) to verify
the type of the entry.

This patch adds a macro to assign all entries of ftrace using the type
of the variable and checking the entry id. The typecasts are now done
in the macro for only those types that it knows about, which should
be all the types that are allowed to be read from the tracer.
Signed-off-by: default avatarSteven Rostedt <srostedt@redhat.com>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent 797d3712
...@@ -1350,7 +1350,9 @@ print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu) ...@@ -1350,7 +1350,9 @@ print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
} }
switch (entry->type) { switch (entry->type) {
case TRACE_FN: { case TRACE_FN: {
struct ftrace_entry *field = (struct ftrace_entry *)entry; struct ftrace_entry *field;
trace_assign_type(field, entry);
seq_print_ip_sym(s, field->ip, sym_flags); seq_print_ip_sym(s, field->ip, sym_flags);
trace_seq_puts(s, " ("); trace_seq_puts(s, " (");
...@@ -1363,8 +1365,9 @@ print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu) ...@@ -1363,8 +1365,9 @@ print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
} }
case TRACE_CTX: case TRACE_CTX:
case TRACE_WAKE: { case TRACE_WAKE: {
struct ctx_switch_entry *field = struct ctx_switch_entry *field;
(struct ctx_switch_entry *)entry;
trace_assign_type(field, entry);
T = field->next_state < sizeof(state_to_char) ? T = field->next_state < sizeof(state_to_char) ?
state_to_char[field->next_state] : 'X'; state_to_char[field->next_state] : 'X';
...@@ -1384,7 +1387,9 @@ print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu) ...@@ -1384,7 +1387,9 @@ print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
break; break;
} }
case TRACE_SPECIAL: { case TRACE_SPECIAL: {
struct special_entry *field = (struct special_entry *)entry; struct special_entry *field;
trace_assign_type(field, entry);
trace_seq_printf(s, "# %ld %ld %ld\n", trace_seq_printf(s, "# %ld %ld %ld\n",
field->arg1, field->arg1,
...@@ -1393,7 +1398,9 @@ print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu) ...@@ -1393,7 +1398,9 @@ print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
break; break;
} }
case TRACE_STACK: { case TRACE_STACK: {
struct stack_entry *field = (struct stack_entry *)entry; struct stack_entry *field;
trace_assign_type(field, entry);
for (i = 0; i < FTRACE_STACK_ENTRIES; i++) { for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
if (i) if (i)
...@@ -1404,7 +1411,9 @@ print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu) ...@@ -1404,7 +1411,9 @@ print_lat_fmt(struct trace_iterator *iter, unsigned int trace_idx, int cpu)
break; break;
} }
case TRACE_PRINT: { case TRACE_PRINT: {
struct print_entry *field = (struct print_entry *)entry; struct print_entry *field;
trace_assign_type(field, entry);
seq_print_ip_sym(s, field->ip, sym_flags); seq_print_ip_sym(s, field->ip, sym_flags);
trace_seq_printf(s, ": %s", field->buf); trace_seq_printf(s, ": %s", field->buf);
...@@ -1454,7 +1463,9 @@ static enum print_line_t print_trace_fmt(struct trace_iterator *iter) ...@@ -1454,7 +1463,9 @@ static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
switch (entry->type) { switch (entry->type) {
case TRACE_FN: { case TRACE_FN: {
struct ftrace_entry *field = (struct ftrace_entry *)entry; struct ftrace_entry *field;
trace_assign_type(field, entry);
ret = seq_print_ip_sym(s, field->ip, sym_flags); ret = seq_print_ip_sym(s, field->ip, sym_flags);
if (!ret) if (!ret)
...@@ -1480,8 +1491,9 @@ static enum print_line_t print_trace_fmt(struct trace_iterator *iter) ...@@ -1480,8 +1491,9 @@ static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
} }
case TRACE_CTX: case TRACE_CTX:
case TRACE_WAKE: { case TRACE_WAKE: {
struct ctx_switch_entry *field = struct ctx_switch_entry *field;
(struct ctx_switch_entry *)entry;
trace_assign_type(field, entry);
S = field->prev_state < sizeof(state_to_char) ? S = field->prev_state < sizeof(state_to_char) ?
state_to_char[field->prev_state] : 'X'; state_to_char[field->prev_state] : 'X';
...@@ -1501,7 +1513,9 @@ static enum print_line_t print_trace_fmt(struct trace_iterator *iter) ...@@ -1501,7 +1513,9 @@ static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
break; break;
} }
case TRACE_SPECIAL: { case TRACE_SPECIAL: {
struct special_entry *field = (struct special_entry *)entry; struct special_entry *field;
trace_assign_type(field, entry);
ret = trace_seq_printf(s, "# %ld %ld %ld\n", ret = trace_seq_printf(s, "# %ld %ld %ld\n",
field->arg1, field->arg1,
...@@ -1512,7 +1526,9 @@ static enum print_line_t print_trace_fmt(struct trace_iterator *iter) ...@@ -1512,7 +1526,9 @@ static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
break; break;
} }
case TRACE_STACK: { case TRACE_STACK: {
struct stack_entry *field = (struct stack_entry *)entry; struct stack_entry *field;
trace_assign_type(field, entry);
for (i = 0; i < FTRACE_STACK_ENTRIES; i++) { for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
if (i) { if (i) {
...@@ -1531,7 +1547,9 @@ static enum print_line_t print_trace_fmt(struct trace_iterator *iter) ...@@ -1531,7 +1547,9 @@ static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
break; break;
} }
case TRACE_PRINT: { case TRACE_PRINT: {
struct print_entry *field = (struct print_entry *)entry; struct print_entry *field;
trace_assign_type(field, entry);
seq_print_ip_sym(s, field->ip, sym_flags); seq_print_ip_sym(s, field->ip, sym_flags);
trace_seq_printf(s, ": %s", field->buf); trace_seq_printf(s, ": %s", field->buf);
...@@ -1562,7 +1580,9 @@ static enum print_line_t print_raw_fmt(struct trace_iterator *iter) ...@@ -1562,7 +1580,9 @@ static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
switch (entry->type) { switch (entry->type) {
case TRACE_FN: { case TRACE_FN: {
struct ftrace_entry *field = (struct ftrace_entry *)entry; struct ftrace_entry *field;
trace_assign_type(field, entry);
ret = trace_seq_printf(s, "%x %x\n", ret = trace_seq_printf(s, "%x %x\n",
field->ip, field->ip,
...@@ -1573,8 +1593,9 @@ static enum print_line_t print_raw_fmt(struct trace_iterator *iter) ...@@ -1573,8 +1593,9 @@ static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
} }
case TRACE_CTX: case TRACE_CTX:
case TRACE_WAKE: { case TRACE_WAKE: {
struct ctx_switch_entry *field = struct ctx_switch_entry *field;
(struct ctx_switch_entry *)entry;
trace_assign_type(field, entry);
S = field->prev_state < sizeof(state_to_char) ? S = field->prev_state < sizeof(state_to_char) ?
state_to_char[field->prev_state] : 'X'; state_to_char[field->prev_state] : 'X';
...@@ -1596,7 +1617,9 @@ static enum print_line_t print_raw_fmt(struct trace_iterator *iter) ...@@ -1596,7 +1617,9 @@ static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
} }
case TRACE_SPECIAL: case TRACE_SPECIAL:
case TRACE_STACK: { case TRACE_STACK: {
struct special_entry *field = (struct special_entry *)entry; struct special_entry *field;
trace_assign_type(field, entry);
ret = trace_seq_printf(s, "# %ld %ld %ld\n", ret = trace_seq_printf(s, "# %ld %ld %ld\n",
field->arg1, field->arg1,
...@@ -1607,7 +1630,9 @@ static enum print_line_t print_raw_fmt(struct trace_iterator *iter) ...@@ -1607,7 +1630,9 @@ static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
break; break;
} }
case TRACE_PRINT: { case TRACE_PRINT: {
struct print_entry *field = (struct print_entry *)entry; struct print_entry *field;
trace_assign_type(field, entry);
trace_seq_printf(s, "# %lx %s", field->ip, field->buf); trace_seq_printf(s, "# %lx %s", field->ip, field->buf);
if (entry->flags & TRACE_FLAG_CONT) if (entry->flags & TRACE_FLAG_CONT)
...@@ -1648,7 +1673,9 @@ static enum print_line_t print_hex_fmt(struct trace_iterator *iter) ...@@ -1648,7 +1673,9 @@ static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
switch (entry->type) { switch (entry->type) {
case TRACE_FN: { case TRACE_FN: {
struct ftrace_entry *field = (struct ftrace_entry *)entry; struct ftrace_entry *field;
trace_assign_type(field, entry);
SEQ_PUT_HEX_FIELD_RET(s, field->ip); SEQ_PUT_HEX_FIELD_RET(s, field->ip);
SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip); SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
...@@ -1656,8 +1683,9 @@ static enum print_line_t print_hex_fmt(struct trace_iterator *iter) ...@@ -1656,8 +1683,9 @@ static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
} }
case TRACE_CTX: case TRACE_CTX:
case TRACE_WAKE: { case TRACE_WAKE: {
struct ctx_switch_entry *field = struct ctx_switch_entry *field;
(struct ctx_switch_entry *)entry;
trace_assign_type(field, entry);
S = field->prev_state < sizeof(state_to_char) ? S = field->prev_state < sizeof(state_to_char) ?
state_to_char[field->prev_state] : 'X'; state_to_char[field->prev_state] : 'X';
...@@ -1676,7 +1704,9 @@ static enum print_line_t print_hex_fmt(struct trace_iterator *iter) ...@@ -1676,7 +1704,9 @@ static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
} }
case TRACE_SPECIAL: case TRACE_SPECIAL:
case TRACE_STACK: { case TRACE_STACK: {
struct special_entry *field = (struct special_entry *)entry; struct special_entry *field;
trace_assign_type(field, entry);
SEQ_PUT_HEX_FIELD_RET(s, field->arg1); SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
SEQ_PUT_HEX_FIELD_RET(s, field->arg2); SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
...@@ -1705,15 +1735,18 @@ static enum print_line_t print_bin_fmt(struct trace_iterator *iter) ...@@ -1705,15 +1735,18 @@ static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
switch (entry->type) { switch (entry->type) {
case TRACE_FN: { case TRACE_FN: {
struct ftrace_entry *field = (struct ftrace_entry *)entry; struct ftrace_entry *field;
trace_assign_type(field, entry);
SEQ_PUT_FIELD_RET(s, field->ip); SEQ_PUT_FIELD_RET(s, field->ip);
SEQ_PUT_FIELD_RET(s, field->parent_ip); SEQ_PUT_FIELD_RET(s, field->parent_ip);
break; break;
} }
case TRACE_CTX: { case TRACE_CTX: {
struct ctx_switch_entry *field = struct ctx_switch_entry *field;
(struct ctx_switch_entry *)entry;
trace_assign_type(field, entry);
SEQ_PUT_FIELD_RET(s, field->prev_pid); SEQ_PUT_FIELD_RET(s, field->prev_pid);
SEQ_PUT_FIELD_RET(s, field->prev_prio); SEQ_PUT_FIELD_RET(s, field->prev_prio);
...@@ -1725,7 +1758,9 @@ static enum print_line_t print_bin_fmt(struct trace_iterator *iter) ...@@ -1725,7 +1758,9 @@ static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
} }
case TRACE_SPECIAL: case TRACE_SPECIAL:
case TRACE_STACK: { case TRACE_STACK: {
struct special_entry *field = (struct special_entry *)entry; struct special_entry *field;
trace_assign_type(field, entry);
SEQ_PUT_FIELD_RET(s, field->arg1); SEQ_PUT_FIELD_RET(s, field->arg1);
SEQ_PUT_FIELD_RET(s, field->arg2); SEQ_PUT_FIELD_RET(s, field->arg2);
......
...@@ -177,6 +177,48 @@ struct trace_array { ...@@ -177,6 +177,48 @@ struct trace_array {
struct trace_array_cpu *data[NR_CPUS]; struct trace_array_cpu *data[NR_CPUS];
}; };
#define FTRACE_CMP_TYPE(var, type) \
__builtin_types_compatible_p(typeof(var), type *)
#undef IF_ASSIGN
#define IF_ASSIGN(var, entry, etype, id) \
if (FTRACE_CMP_TYPE(var, etype)) { \
var = (typeof(var))(entry); \
WARN_ON(id && (entry)->type != id); \
break; \
}
/* Will cause compile errors if type is not found. */
extern void __ftrace_bad_type(void);
/*
* The trace_assign_type is a verifier that the entry type is
* the same as the type being assigned. To add new types simply
* add a line with the following format:
*
* IF_ASSIGN(var, ent, type, id);
*
* Where "type" is the trace type that includes the trace_entry
* as the "ent" item. And "id" is the trace identifier that is
* used in the trace_type enum.
*
* If the type can have more than one id, then use zero.
*/
#define trace_assign_type(var, ent) \
do { \
IF_ASSIGN(var, ent, struct ftrace_entry, TRACE_FN); \
IF_ASSIGN(var, ent, struct ctx_switch_entry, 0); \
IF_ASSIGN(var, ent, struct trace_field_cont, TRACE_CONT); \
IF_ASSIGN(var, ent, struct stack_entry, TRACE_STACK); \
IF_ASSIGN(var, ent, struct print_entry, TRACE_PRINT); \
IF_ASSIGN(var, ent, struct special_entry, 0); \
IF_ASSIGN(var, ent, struct trace_mmiotrace_rw, \
TRACE_MMIO_RW); \
IF_ASSIGN(var, ent, struct trace_mmiotrace_map, \
TRACE_MMIO_MAP); \
IF_ASSIGN(var, ent, struct trace_boot, TRACE_BOOT); \
__ftrace_bad_type(); \
} while (0)
/* Return values for print_line callback */ /* Return values for print_line callback */
enum print_line_t { enum print_line_t {
......
...@@ -178,15 +178,17 @@ static ssize_t mmio_read(struct trace_iterator *iter, struct file *filp, ...@@ -178,15 +178,17 @@ static ssize_t mmio_read(struct trace_iterator *iter, struct file *filp,
static enum print_line_t mmio_print_rw(struct trace_iterator *iter) static enum print_line_t mmio_print_rw(struct trace_iterator *iter)
{ {
struct trace_entry *entry = iter->ent; struct trace_entry *entry = iter->ent;
struct trace_mmiotrace_rw *field = struct trace_mmiotrace_rw *field;
(struct trace_mmiotrace_rw *)entry; struct mmiotrace_rw *rw;
struct mmiotrace_rw *rw = &field->rw;
struct trace_seq *s = &iter->seq; struct trace_seq *s = &iter->seq;
unsigned long long t = ns2usecs(iter->ts); unsigned long long t = ns2usecs(iter->ts);
unsigned long usec_rem = do_div(t, 1000000ULL); unsigned long usec_rem = do_div(t, 1000000ULL);
unsigned secs = (unsigned long)t; unsigned secs = (unsigned long)t;
int ret = 1; int ret = 1;
trace_assign_type(field, entry);
rw = &field->rw;
switch (rw->opcode) { switch (rw->opcode) {
case MMIO_READ: case MMIO_READ:
ret = trace_seq_printf(s, ret = trace_seq_printf(s,
...@@ -222,13 +224,17 @@ static enum print_line_t mmio_print_rw(struct trace_iterator *iter) ...@@ -222,13 +224,17 @@ static enum print_line_t mmio_print_rw(struct trace_iterator *iter)
static enum print_line_t mmio_print_map(struct trace_iterator *iter) static enum print_line_t mmio_print_map(struct trace_iterator *iter)
{ {
struct trace_entry *entry = iter->ent; struct trace_entry *entry = iter->ent;
struct mmiotrace_map *m = (struct mmiotrace_map *)entry; struct trace_mmiotrace_map *field;
struct mmiotrace_map *m;
struct trace_seq *s = &iter->seq; struct trace_seq *s = &iter->seq;
unsigned long long t = ns2usecs(iter->ts); unsigned long long t = ns2usecs(iter->ts);
unsigned long usec_rem = do_div(t, 1000000ULL); unsigned long usec_rem = do_div(t, 1000000ULL);
unsigned secs = (unsigned long)t; unsigned secs = (unsigned long)t;
int ret; int ret;
trace_assign_type(field, entry);
m = &field->map;
switch (m->opcode) { switch (m->opcode) {
case MMIO_PROBE: case MMIO_PROBE:
ret = trace_seq_printf(s, ret = trace_seq_printf(s,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment