Fix link in German terms of service.
[chromium-blink-merge.git] / sandbox / linux / bpf_dsl / dump_bpf.cc
blobd0c8f75073c18679161830957d3279da32e1e9cb
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sandbox/linux/bpf_dsl/dump_bpf.h"
7 #include <stdio.h>
9 #include "sandbox/linux/bpf_dsl/codegen.h"
10 #include "sandbox/linux/bpf_dsl/trap_registry.h"
11 #include "sandbox/linux/system_headers/linux_filter.h"
12 #include "sandbox/linux/system_headers/linux_seccomp.h"
14 namespace sandbox {
15 namespace bpf_dsl {
17 void DumpBPF::PrintProgram(const CodeGen::Program& program) {
18 for (CodeGen::Program::const_iterator iter = program.begin();
19 iter != program.end();
20 ++iter) {
21 int ip = (int)(iter - program.begin());
22 fprintf(stderr, "%3d) ", ip);
23 switch (BPF_CLASS(iter->code)) {
24 case BPF_LD:
25 if (iter->code == BPF_LD + BPF_W + BPF_ABS) {
26 fprintf(stderr, "LOAD %d // ", (int)iter->k);
27 if (iter->k == offsetof(struct arch_seccomp_data, nr)) {
28 fprintf(stderr, "System call number\n");
29 } else if (iter->k == offsetof(struct arch_seccomp_data, arch)) {
30 fprintf(stderr, "Architecture\n");
31 } else if (iter->k ==
32 offsetof(struct arch_seccomp_data, instruction_pointer)) {
33 fprintf(stderr, "Instruction pointer (LSB)\n");
34 } else if (iter->k ==
35 offsetof(struct arch_seccomp_data, instruction_pointer) +
36 4) {
37 fprintf(stderr, "Instruction pointer (MSB)\n");
38 } else if (iter->k >= offsetof(struct arch_seccomp_data, args) &&
39 iter->k < offsetof(struct arch_seccomp_data, args) + 48 &&
40 (iter->k - offsetof(struct arch_seccomp_data, args)) % 4 ==
41 0) {
42 fprintf(
43 stderr,
44 "Argument %d (%cSB)\n",
45 (int)(iter->k - offsetof(struct arch_seccomp_data, args)) / 8,
46 (iter->k - offsetof(struct arch_seccomp_data, args)) % 8 ? 'M'
47 : 'L');
48 } else {
49 fprintf(stderr, "???\n");
51 } else {
52 fprintf(stderr, "LOAD ???\n");
54 break;
55 case BPF_JMP:
56 if (BPF_OP(iter->code) == BPF_JA) {
57 fprintf(stderr, "JMP %d\n", ip + iter->k + 1);
58 } else {
59 fprintf(stderr, "if A %s 0x%x; then JMP %d else JMP %d\n",
60 BPF_OP(iter->code) == BPF_JSET ? "&" :
61 BPF_OP(iter->code) == BPF_JEQ ? "==" :
62 BPF_OP(iter->code) == BPF_JGE ? ">=" :
63 BPF_OP(iter->code) == BPF_JGT ? ">" : "???",
64 (int)iter->k,
65 ip + iter->jt + 1, ip + iter->jf + 1);
67 break;
68 case BPF_RET:
69 fprintf(stderr, "RET 0x%x // ", iter->k);
70 if ((iter->k & SECCOMP_RET_ACTION) == SECCOMP_RET_TRAP) {
71 fprintf(stderr, "Trap #%d\n", iter->k & SECCOMP_RET_DATA);
72 } else if ((iter->k & SECCOMP_RET_ACTION) == SECCOMP_RET_ERRNO) {
73 fprintf(stderr, "errno = %d\n", iter->k & SECCOMP_RET_DATA);
74 } else if ((iter->k & SECCOMP_RET_ACTION) == SECCOMP_RET_TRACE) {
75 fprintf(stderr, "Trace #%d\n", iter->k & SECCOMP_RET_DATA);
76 } else if (iter->k == SECCOMP_RET_ALLOW) {
77 fprintf(stderr, "Allowed\n");
78 } else {
79 fprintf(stderr, "???\n");
81 break;
82 case BPF_ALU:
83 if (BPF_OP(iter->code) == BPF_NEG) {
84 fprintf(stderr, "A := -A\n");
85 } else {
86 fprintf(stderr, "A := A %s 0x%x\n",
87 BPF_OP(iter->code) == BPF_ADD ? "+" :
88 BPF_OP(iter->code) == BPF_SUB ? "-" :
89 BPF_OP(iter->code) == BPF_MUL ? "*" :
90 BPF_OP(iter->code) == BPF_DIV ? "/" :
91 BPF_OP(iter->code) == BPF_MOD ? "%" :
92 BPF_OP(iter->code) == BPF_OR ? "|" :
93 BPF_OP(iter->code) == BPF_XOR ? "^" :
94 BPF_OP(iter->code) == BPF_AND ? "&" :
95 BPF_OP(iter->code) == BPF_LSH ? "<<" :
96 BPF_OP(iter->code) == BPF_RSH ? ">>" : "???",
97 (int)iter->k);
99 break;
100 default:
101 fprintf(stderr, "???\n");
102 break;
105 return;
108 } // namespace bpf_dsl
109 } // namespace sandbox