Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(464)

Side by Side Diff: sandbox/linux/seccomp-bpf-helpers/bpf_dsl.cc

Issue 299743002: Add domain-specific language for BPF policies (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Simplify slightly by making Cond into a typedef Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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/seccomp-bpf-helpers/bpf_dsl.h"
6
7 #include "base/logging.h"
8 #include "base/memory/ref_counted.h"
9 #include "sandbox/linux/seccomp-bpf/errorcode.h"
10 #include "sandbox/linux/seccomp-bpf/sandbox_bpf.h"
11
12 namespace sandbox {
13
14 namespace bpfdsl {
15
16 namespace {
17
18 class BasicCondImpl : public CondImpl {
19 public:
20 BasicCondImpl(int argno,
21 ErrorCode::ArgType is_32bit,
22 ErrorCode::Operation op,
23 uint64_t value)
24 : argno_(argno), is_32bit_(is_32bit), op_(op), value_(value) {}
25 virtual ErrorCode Compile(SandboxBPF* sb,
26 ErrorCode true_ec,
27 ErrorCode false_ec) const OVERRIDE {
28 return sb->Cond(argno_, is_32bit_, op_, value_, true_ec, false_ec);
29 }
30
31 private:
32 virtual ~BasicCondImpl() {}
33 int argno_;
34 ErrorCode::ArgType is_32bit_;
35 ErrorCode::Operation op_;
36 uint64_t value_;
37 DISALLOW_COPY_AND_ASSIGN(BasicCondImpl);
38 };
39
40 class AndCondImpl : public CondImpl {
41 public:
42 AndCondImpl(Cond lhs, Cond rhs) : lhs_(lhs), rhs_(rhs) {}
43 virtual ErrorCode Compile(SandboxBPF* sb,
44 ErrorCode true_ec,
45 ErrorCode false_ec) const OVERRIDE {
46 return lhs_->Compile(sb, rhs_->Compile(sb, true_ec, false_ec), false_ec);
47 }
48
49 private:
50 virtual ~AndCondImpl() {}
51 Cond lhs_, rhs_;
52 DISALLOW_COPY_AND_ASSIGN(AndCondImpl);
53 };
54
55 class OrCondImpl : public CondImpl {
56 public:
57 OrCondImpl(Cond lhs, Cond rhs) : lhs_(lhs), rhs_(rhs) {}
58 virtual ErrorCode Compile(SandboxBPF* sb,
59 ErrorCode true_ec,
60 ErrorCode false_ec) const OVERRIDE {
61 return lhs_->Compile(sb, true_ec, rhs_->Compile(sb, true_ec, false_ec));
62 }
63
64 private:
65 virtual ~OrCondImpl() {}
66 Cond lhs_, rhs_;
67 DISALLOW_COPY_AND_ASSIGN(OrCondImpl);
68 };
69
70 } // namespace
71
72 Cond operator&&(Cond lhs, Cond rhs) {
73 return Cond(new AndCondImpl(lhs, rhs));
74 }
75
76 Cond operator||(Cond lhs, Cond rhs) {
77 return Cond(new OrCondImpl(lhs, rhs));
78 }
79
80 Iffer::Iffer(SandboxBPF* sb) : sb_(sb) {
81 DCHECK(sb);
82 }
83
84 Iffer::Iffer(const Iffer& iffer) : sb_(iffer.sb_) {
85 }
86
87 Thener Iffer::If(Cond cond) const {
88 return Thener(sb_, cond);
89 }
90
91 Thener::Thener(SandboxBPF* sb, Cond cond) : sb_(sb), cond_(cond) {
92 }
93
94 Thener::~Thener() {}
95
96 Elser Thener::Then(ErrorCode true_ec) const {
97 return Elser(sb_, cond_, true_ec);
98 }
99
100 Elser::Elser(SandboxBPF* sb, Cond cond, ErrorCode true_ec)
101 : sb_(sb), cond_(cond), true_ec_(true_ec) {
102 }
103
104 Elser::~Elser() {}
105
106 ErrorCode Elser::Else(ErrorCode false_ec) const {
107 return cond_->Compile(sb_, true_ec_, false_ec);
108 }
109
110 } // namespace bpfdsl
111
112 bpfdsl::Iffer DSL(SandboxBPF* sb) {
113 return bpfdsl::Iffer(sb);
114 }
115
116 template <typename T>
117 Arg<T>::Arg(int num)
118 : num_(num) {
119 DCHECK(num >= 0 && num < 6);
120 }
121
122 template <typename T>
123 bpfdsl::Cond Arg<T>::operator==(const T& rhs) const {
124 const ErrorCode::ArgType arg_type =
125 (sizeof(T) <= 4) ? ErrorCode::TP_32BIT : ErrorCode::TP_64BIT;
126 return bpfdsl::Cond(
127 new bpfdsl::BasicCondImpl(num_, arg_type, ErrorCode::OP_EQUAL, rhs));
128 }
129
130 template class Arg<int>;
131
132 } // sandbox
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698