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

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

Issue 10833044: Refactored ErrorCode into it's own class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Made changes requested by reviewer (jln) Created 8 years, 4 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 (c) 2012 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/sandbox_bpf.h"
6
7
8 namespace playground2 {
9
10 ErrorCode::ErrorCode(int err) {
11 switch (err) {
12 case ERR_INVALID:
13 // We allow the default constructor, as it makes the ErrorCode class
14 // much easier to use. But if we ever encounter an invalid ErrorCode
15 // when compiling a BPF filter, we deliberately generate an invalid
16 // program that will get flagged both by our Verifier class and by
17 // the Linux kernel.
18 err_ = SECCOMP_RET_INVALID;
19 error_type_ = ET_INVALID;
20 break;
21 case ERR_ALLOWED:
22 err_ = SECCOMP_RET_ALLOW;
23 error_type_ = ET_SIMPLE;
24 break;
25 case ERR_MIN_ERRNO ... ERR_MAX_ERRNO:
26 err_ = SECCOMP_RET_ERRNO + err;
27 error_type_ = ET_SIMPLE;
28 break;
29 default:
30 die("Invalid use of ErrorCode object");
31 }
32 }
33
34 ErrorCode::ErrorCode(ErrorCode::TrapFnc fnc, const void *aux, uint16_t id) :
35 error_type_(ET_TRAP),
jln (very slow on Chromium) 2012/07/30 18:50:39 See below for nit on initializer.
36 fnc_(fnc),
37 aux_(const_cast<void *>(aux)),
38 err_(SECCOMP_RET_TRAP + id) {
39 }
40
41 ErrorCode::ErrorCode(int argno, ArgType width, Operation op, uint64_t value,
42 const ErrorCode *passed, const ErrorCode *failed) :
43 error_type_(ET_COND),
jln (very slow on Chromium) 2012/07/30 18:50:39 The colon should be here. Four spaces, colon, spac
44 value_(value),
45 argno_(argno),
46 width_(width),
47 op_(op),
48 passed_(passed),
49 failed_(failed),
50 err_(SECCOMP_RET_INVALID) {
51 if (op < 0 || op >= OP_NUM_OPS) {
52 die("Invalid opcode in BPF sandbox rules");
53 }
54 }
55
56 bool ErrorCode::Equals(const ErrorCode& err) const {
57 if (error_type_ == ET_INVALID || err.error_type_ == ET_INVALID) {
58 die("Dereferencing invalid ErrorCode");
59 }
60 if (error_type_ != err.error_type_) {
61 return false;
62 }
63 if (error_type_ == ET_SIMPLE || error_type_ == ET_TRAP) {
64 return err_ == err.err_;
65 } else if (error_type_ == ET_COND) {
66 return value_ == err.value_ &&
67 argno_ == err.argno_ &&
68 width_ == err.width_ &&
69 op_ == err.op_ &&
70 passed_->Equals(*err.passed_) &&
71 failed_->Equals(*err.failed_);
72 } else {
73 die("Corrupted ErrorCode");
74 }
75 }
76
77 bool ErrorCode::LessThan(const ErrorCode& err) const {
78 // Implementing a "LessThan()" operator allows us to use ErrorCode objects
79 // as keys in STL containers; most notably, it also allows us to put them
80 // into std::set<>. Actual ordering is not important as long as it is
81 // deterministic.
82 if (error_type_ == ET_INVALID || err.error_type_ == ET_INVALID) {
83 die("Dereferencing invalid ErrorCode");
84 }
85 if (error_type_ != err.error_type_) {
86 return error_type_ < err.error_type_;
87 } else {
88 if (error_type_ == ET_SIMPLE || error_type_ == ET_TRAP) {
89 return err_ < err.err_;
90 } else if (error_type_ == ET_COND) {
91 if (value_ != err.value_) {
92 return value_ < err.value_;
93 } else if (argno_ != err.argno_) {
94 return argno_ < err.argno_;
95 } else if (width_ != err.width_) {
96 return width_ < err.width_;
97 } else if (op_ != err.op_) {
98 return op_ < err.op_;
99 } else if (!passed_->Equals(*err.passed_)) {
100 return passed_->LessThan(*err.passed_);
101 } else if (!failed_->Equals(*err.failed_)) {
102 return failed_->LessThan(*err.failed_);
103 } else {
104 return false;
105 }
106 } else {
107 die("Corrupted ErrorCode");
108 }
109 }
110 }
111
112 void ErrorCode::die(const char *msg) {
113 Sandbox::die(msg);
114 }
115
116 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698