Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / sandbox / linux / bpf_dsl / dump_bpf.cc
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.
4
5 #include "sandbox/linux/bpf_dsl/dump_bpf.h"
6
7 #include <stdio.h>
8
9 #include "sandbox/linux/bpf_dsl/trap_registry.h"
10 #include "sandbox/linux/seccomp-bpf/codegen.h"
11 #include "sandbox/linux/seccomp-bpf/linux_seccomp.h"
12
13 namespace sandbox {
14 namespace bpf_dsl {
15
16 void DumpBPF::PrintProgram(const CodeGen::Program& program) {
17   for (CodeGen::Program::const_iterator iter = program.begin();
18        iter != program.end();
19        ++iter) {
20     int ip = (int)(iter - program.begin());
21     fprintf(stderr, "%3d) ", ip);
22     switch (BPF_CLASS(iter->code)) {
23       case BPF_LD:
24         if (iter->code == BPF_LD + BPF_W + BPF_ABS) {
25           fprintf(stderr, "LOAD %d  // ", (int)iter->k);
26           if (iter->k == offsetof(struct arch_seccomp_data, nr)) {
27             fprintf(stderr, "System call number\n");
28           } else if (iter->k == offsetof(struct arch_seccomp_data, arch)) {
29             fprintf(stderr, "Architecture\n");
30           } else if (iter->k ==
31                      offsetof(struct arch_seccomp_data, instruction_pointer)) {
32             fprintf(stderr, "Instruction pointer (LSB)\n");
33           } else if (iter->k ==
34                      offsetof(struct arch_seccomp_data, instruction_pointer) +
35                          4) {
36             fprintf(stderr, "Instruction pointer (MSB)\n");
37           } else if (iter->k >= offsetof(struct arch_seccomp_data, args) &&
38                      iter->k < offsetof(struct arch_seccomp_data, args) + 48 &&
39                      (iter->k - offsetof(struct arch_seccomp_data, args)) % 4 ==
40                          0) {
41             fprintf(
42                 stderr,
43                 "Argument %d (%cSB)\n",
44                 (int)(iter->k - offsetof(struct arch_seccomp_data, args)) / 8,
45                 (iter->k - offsetof(struct arch_seccomp_data, args)) % 8 ? 'M'
46                                                                          : 'L');
47           } else {
48             fprintf(stderr, "???\n");
49           }
50         } else {
51           fprintf(stderr, "LOAD ???\n");
52         }
53         break;
54       case BPF_JMP:
55         if (BPF_OP(iter->code) == BPF_JA) {
56           fprintf(stderr, "JMP %d\n", ip + iter->k + 1);
57         } else {
58           fprintf(stderr, "if A %s 0x%x; then JMP %d else JMP %d\n",
59               BPF_OP(iter->code) == BPF_JSET ? "&" :
60               BPF_OP(iter->code) == BPF_JEQ ? "==" :
61               BPF_OP(iter->code) == BPF_JGE ? ">=" :
62               BPF_OP(iter->code) == BPF_JGT ? ">"  : "???",
63               (int)iter->k,
64               ip + iter->jt + 1, ip + iter->jf + 1);
65         }
66         break;
67       case BPF_RET:
68         fprintf(stderr, "RET 0x%x  // ", iter->k);
69         if ((iter->k & SECCOMP_RET_ACTION) == SECCOMP_RET_TRAP) {
70           fprintf(stderr, "Trap #%d\n", iter->k & SECCOMP_RET_DATA);
71         } else if ((iter->k & SECCOMP_RET_ACTION) == SECCOMP_RET_ERRNO) {
72           fprintf(stderr, "errno = %d\n", iter->k & SECCOMP_RET_DATA);
73         } else if ((iter->k & SECCOMP_RET_ACTION) == SECCOMP_RET_TRACE) {
74           fprintf(stderr, "Trace #%d\n", iter->k & SECCOMP_RET_DATA);
75         } else if (iter->k == SECCOMP_RET_ALLOW) {
76           fprintf(stderr, "Allowed\n");
77         } else {
78           fprintf(stderr, "???\n");
79         }
80         break;
81       case BPF_ALU:
82         fprintf(stderr, BPF_OP(iter->code) == BPF_NEG
83             ? "A := -A\n" : "A := A %s 0x%x\n",
84             BPF_OP(iter->code) == BPF_ADD ? "+"  :
85             BPF_OP(iter->code) == BPF_SUB ? "-"  :
86             BPF_OP(iter->code) == BPF_MUL ? "*"  :
87             BPF_OP(iter->code) == BPF_DIV ? "/"  :
88             BPF_OP(iter->code) == BPF_MOD ? "%"  :
89             BPF_OP(iter->code) == BPF_OR  ? "|"  :
90             BPF_OP(iter->code) == BPF_XOR ? "^"  :
91             BPF_OP(iter->code) == BPF_AND ? "&"  :
92             BPF_OP(iter->code) == BPF_LSH ? "<<" :
93             BPF_OP(iter->code) == BPF_RSH ? ">>" : "???",
94             (int)iter->k);
95         break;
96       default:
97         fprintf(stderr, "???\n");
98         break;
99     }
100   }
101   return;
102 }
103
104 }  // namespace bpf_dsl
105 }  // namespace sandbox