Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / sandbox / linux / seccomp-bpf / errorcode.cc
blobebae130b6a6484622695146ebdb86232cad0a7a5
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"
10 namespace sandbox {
12 ErrorCode::ErrorCode() : error_type_(ET_INVALID), err_(SECCOMP_RET_INVALID) {
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_MIN_ERRNO... ERR_MAX_ERRNO:
22 err_ = SECCOMP_RET_ERRNO + err;
23 error_type_ = ET_SIMPLE;
24 break;
25 default:
26 if ((err & ~SECCOMP_RET_DATA) == ERR_TRACE) {
27 err_ = SECCOMP_RET_TRACE + (err & SECCOMP_RET_DATA);
28 error_type_ = ET_SIMPLE;
29 break;
31 SANDBOX_DIE("Invalid use of ErrorCode object");
35 ErrorCode::ErrorCode(uint16_t trap_id,
36 Trap::TrapFnc fnc,
37 const void* aux,
38 bool safe)
39 : error_type_(ET_TRAP),
40 fnc_(fnc),
41 aux_(const_cast<void*>(aux)),
42 safe_(safe),
43 err_(SECCOMP_RET_TRAP + trap_id) {
46 ErrorCode::ErrorCode(int argno,
47 ArgType width,
48 uint64_t mask,
49 uint64_t value,
50 const ErrorCode* passed,
51 const ErrorCode* failed)
52 : error_type_(ET_COND),
53 mask_(mask),
54 value_(value),
55 argno_(argno),
56 width_(width),
57 passed_(passed),
58 failed_(failed),
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_) {
67 return false;
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_);
75 } else {
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
84 // deterministic.
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_;
90 } else {
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_);
106 } else {
107 return false;
109 } else {
110 SANDBOX_DIE("Corrupted ErrorCode");
115 } // namespace sandbox