OLD | NEW |
| (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/bpf_dsl/errorcode.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "sandbox/linux/system_headers/linux_seccomp.h" | |
9 | |
10 namespace sandbox { | |
11 namespace bpf_dsl { | |
12 | |
13 ErrorCode::ErrorCode() : error_type_(ET_INVALID), err_(SECCOMP_RET_INVALID) {} | |
14 | |
15 ErrorCode::ErrorCode(int err) { | |
16 switch (err) { | |
17 case ERR_ALLOWED: | |
18 err_ = SECCOMP_RET_ALLOW; | |
19 error_type_ = ET_SIMPLE; | |
20 break; | |
21 case ERR_KILL: | |
22 err_ = SECCOMP_RET_KILL; | |
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 if ((err & ~SECCOMP_RET_DATA) == ERR_TRACE) { | |
31 err_ = SECCOMP_RET_TRACE + (err & SECCOMP_RET_DATA); | |
32 error_type_ = ET_SIMPLE; | |
33 break; | |
34 } | |
35 LOG(FATAL) << "Invalid use of ErrorCode object"; | |
36 } | |
37 } | |
38 | |
39 ErrorCode::ErrorCode(uint16_t trap_id, | |
40 TrapRegistry::TrapFnc fnc, | |
41 const void* aux, | |
42 bool safe) | |
43 : error_type_(ET_TRAP), | |
44 fnc_(fnc), | |
45 aux_(const_cast<void*>(aux)), | |
46 safe_(safe), | |
47 err_(SECCOMP_RET_TRAP + trap_id) {} | |
48 | |
49 ErrorCode::ErrorCode(int argno, | |
50 ArgType width, | |
51 uint64_t mask, | |
52 uint64_t value, | |
53 const ErrorCode* passed, | |
54 const ErrorCode* failed) | |
55 : error_type_(ET_COND), | |
56 mask_(mask), | |
57 value_(value), | |
58 argno_(argno), | |
59 width_(width), | |
60 passed_(passed), | |
61 failed_(failed), | |
62 err_(SECCOMP_RET_INVALID) {} | |
63 | |
64 bool ErrorCode::Equals(const ErrorCode& err) const { | |
65 if (error_type_ == ET_INVALID || err.error_type_ == ET_INVALID) { | |
66 LOG(FATAL) << "Dereferencing invalid ErrorCode"; | |
67 } | |
68 if (error_type_ != err.error_type_) { | |
69 return false; | |
70 } | |
71 if (error_type_ == ET_SIMPLE || error_type_ == ET_TRAP) { | |
72 return err_ == err.err_; | |
73 } else if (error_type_ == ET_COND) { | |
74 return mask_ == err.mask_ && value_ == err.value_ && argno_ == err.argno_ && | |
75 width_ == err.width_ && passed_->Equals(*err.passed_) && | |
76 failed_->Equals(*err.failed_); | |
77 } else { | |
78 LOG(FATAL) << "Corrupted ErrorCode"; | |
79 return false; | |
80 } | |
81 } | |
82 | |
83 bool ErrorCode::LessThan(const ErrorCode& err) const { | |
84 // Implementing a "LessThan()" operator allows us to use ErrorCode objects | |
85 // as keys in STL containers; most notably, it also allows us to put them | |
86 // into std::set<>. Actual ordering is not important as long as it is | |
87 // deterministic. | |
88 if (error_type_ == ET_INVALID || err.error_type_ == ET_INVALID) { | |
89 LOG(FATAL) << "Dereferencing invalid ErrorCode"; | |
90 } | |
91 if (error_type_ != err.error_type_) { | |
92 return error_type_ < err.error_type_; | |
93 } else { | |
94 if (error_type_ == ET_SIMPLE || error_type_ == ET_TRAP) { | |
95 return err_ < err.err_; | |
96 } else if (error_type_ == ET_COND) { | |
97 if (mask_ != err.mask_) { | |
98 return mask_ < err.mask_; | |
99 } else if (value_ != err.value_) { | |
100 return value_ < err.value_; | |
101 } else if (argno_ != err.argno_) { | |
102 return argno_ < err.argno_; | |
103 } else if (width_ != err.width_) { | |
104 return width_ < err.width_; | |
105 } else if (!passed_->Equals(*err.passed_)) { | |
106 return passed_->LessThan(*err.passed_); | |
107 } else if (!failed_->Equals(*err.failed_)) { | |
108 return failed_->LessThan(*err.failed_); | |
109 } else { | |
110 return false; | |
111 } | |
112 } else { | |
113 LOG(FATAL) << "Corrupted ErrorCode"; | |
114 return false; | |
115 } | |
116 } | |
117 } | |
118 | |
119 } // namespace bpf_dsl | |
120 } // namespace sandbox | |
OLD | NEW |