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.
5 #include "sandbox/linux/bpf_dsl/errorcode.h"
7 #include "base/logging.h"
8 #include "sandbox/linux/system_headers/linux_seccomp.h"
13 ErrorCode::ErrorCode() : error_type_(ET_INVALID
), err_(SECCOMP_RET_INVALID
) {}
15 ErrorCode::ErrorCode(int err
) {
18 err_
= SECCOMP_RET_ALLOW
;
19 error_type_
= ET_SIMPLE
;
22 err_
= SECCOMP_RET_KILL
;
23 error_type_
= ET_SIMPLE
;
25 case ERR_MIN_ERRNO
... ERR_MAX_ERRNO
:
26 err_
= SECCOMP_RET_ERRNO
+ err
;
27 error_type_
= ET_SIMPLE
;
30 if ((err
& ~SECCOMP_RET_DATA
) == ERR_TRACE
) {
31 err_
= SECCOMP_RET_TRACE
+ (err
& SECCOMP_RET_DATA
);
32 error_type_
= ET_SIMPLE
;
35 LOG(FATAL
) << "Invalid use of ErrorCode object";
39 ErrorCode::ErrorCode(uint16_t trap_id
,
40 TrapRegistry::TrapFnc fnc
,
43 : error_type_(ET_TRAP
),
45 aux_(const_cast<void*>(aux
)),
47 err_(SECCOMP_RET_TRAP
+ trap_id
) {}
49 ErrorCode::ErrorCode(int argno
,
53 const ErrorCode
* passed
,
54 const ErrorCode
* failed
)
55 : error_type_(ET_COND
),
62 err_(SECCOMP_RET_INVALID
) {}
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";
68 if (error_type_
!= err
.error_type_
) {
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_
);
78 LOG(FATAL
) << "Corrupted ErrorCode";
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
88 if (error_type_
== ET_INVALID
|| err
.error_type_
== ET_INVALID
) {
89 LOG(FATAL
) << "Dereferencing invalid ErrorCode";
91 if (error_type_
!= err
.error_type_
) {
92 return error_type_
< err
.error_type_
;
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_
);
113 LOG(FATAL
) << "Corrupted ErrorCode";
119 } // namespace bpf_dsl
120 } // namespace sandbox