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/seccomp-bpf/errorcode.h"
7 #include "sandbox/linux/seccomp-bpf/die.h"
8 #include "sandbox/linux/seccomp-bpf/linux_seccomp.h"
12 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
;
21 case ERR_MIN_ERRNO
... ERR_MAX_ERRNO
:
22 err_
= SECCOMP_RET_ERRNO
+ err
;
23 error_type_
= ET_SIMPLE
;
26 if ((err
& ~SECCOMP_RET_DATA
) == ERR_TRACE
) {
27 err_
= SECCOMP_RET_TRACE
+ (err
& SECCOMP_RET_DATA
);
28 error_type_
= ET_SIMPLE
;
31 SANDBOX_DIE("Invalid use of ErrorCode object");
35 ErrorCode::ErrorCode(uint16_t trap_id
,
39 : error_type_(ET_TRAP
),
41 aux_(const_cast<void*>(aux
)),
43 err_(SECCOMP_RET_TRAP
+ trap_id
) {
46 ErrorCode::ErrorCode(int argno
,
50 const ErrorCode
* passed
,
51 const ErrorCode
* failed
)
52 : error_type_(ET_COND
),
59 err_(SECCOMP_RET_INVALID
) {
62 bool ErrorCode::Equals(const ErrorCode
& err
) const {
63 if (error_type_
== ET_INVALID
|| err
.error_type_
== ET_INVALID
) {
64 SANDBOX_DIE("Dereferencing invalid ErrorCode");
66 if (error_type_
!= err
.error_type_
) {
69 if (error_type_
== ET_SIMPLE
|| error_type_
== ET_TRAP
) {
70 return err_
== err
.err_
;
71 } else if (error_type_
== ET_COND
) {
72 return mask_
== err
.mask_
&& value_
== err
.value_
&& argno_
== err
.argno_
&&
73 width_
== err
.width_
&& passed_
->Equals(*err
.passed_
) &&
74 failed_
->Equals(*err
.failed_
);
76 SANDBOX_DIE("Corrupted ErrorCode");
80 bool ErrorCode::LessThan(const ErrorCode
& err
) const {
81 // Implementing a "LessThan()" operator allows us to use ErrorCode objects
82 // as keys in STL containers; most notably, it also allows us to put them
83 // into std::set<>. Actual ordering is not important as long as it is
85 if (error_type_
== ET_INVALID
|| err
.error_type_
== ET_INVALID
) {
86 SANDBOX_DIE("Dereferencing invalid ErrorCode");
88 if (error_type_
!= err
.error_type_
) {
89 return error_type_
< err
.error_type_
;
91 if (error_type_
== ET_SIMPLE
|| error_type_
== ET_TRAP
) {
92 return err_
< err
.err_
;
93 } else if (error_type_
== ET_COND
) {
94 if (mask_
!= err
.mask_
) {
95 return mask_
< err
.mask_
;
96 } else if (value_
!= err
.value_
) {
97 return value_
< err
.value_
;
98 } else if (argno_
!= err
.argno_
) {
99 return argno_
< err
.argno_
;
100 } else if (width_
!= err
.width_
) {
101 return width_
< err
.width_
;
102 } else if (!passed_
->Equals(*err
.passed_
)) {
103 return passed_
->LessThan(*err
.passed_
);
104 } else if (!failed_
->Equals(*err
.failed_
)) {
105 return failed_
->LessThan(*err
.failed_
);
110 SANDBOX_DIE("Corrupted ErrorCode");
115 } // namespace sandbox