- add sources.
[platform/framework/web/crosswalk.git] / src / sandbox / linux / seccomp-bpf / errorcode.cc
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/die.h"
6 #include "sandbox/linux/seccomp-bpf/errorcode.h"
7
8
9 namespace playground2 {
10
11 ErrorCode::ErrorCode(int err) {
12   switch (err) {
13   case ERR_ALLOWED:
14     err_ = SECCOMP_RET_ALLOW;
15     error_type_ = ET_SIMPLE;
16     break;
17   case ERR_MIN_ERRNO ... ERR_MAX_ERRNO:
18     err_ = SECCOMP_RET_ERRNO + err;
19     error_type_ = ET_SIMPLE;
20     break;
21   default:
22     SANDBOX_DIE("Invalid use of ErrorCode object");
23   }
24 }
25
26 ErrorCode::ErrorCode(Trap::TrapFnc fnc, const void *aux, bool safe,
27                      uint16_t id)
28     : error_type_(ET_TRAP),
29       fnc_(fnc),
30       aux_(const_cast<void *>(aux)),
31       safe_(safe),
32       err_(SECCOMP_RET_TRAP + id) {
33 }
34
35 ErrorCode::ErrorCode(int argno, ArgType width, Operation op, uint64_t value,
36                      const ErrorCode *passed, const ErrorCode *failed)
37     : error_type_(ET_COND),
38       value_(value),
39       argno_(argno),
40       width_(width),
41       op_(op),
42       passed_(passed),
43       failed_(failed),
44       err_(SECCOMP_RET_INVALID) {
45   if (op < 0 || op >= OP_NUM_OPS) {
46     SANDBOX_DIE("Invalid opcode in BPF sandbox rules");
47   }
48 }
49
50 bool ErrorCode::Equals(const ErrorCode& err) const {
51   if (error_type_ == ET_INVALID || err.error_type_ == ET_INVALID) {
52     SANDBOX_DIE("Dereferencing invalid ErrorCode");
53   }
54   if (error_type_ != err.error_type_) {
55     return false;
56   }
57   if (error_type_ == ET_SIMPLE || error_type_ == ET_TRAP) {
58     return err_ == err.err_;
59   } else if (error_type_ == ET_COND) {
60     return value_   == err.value_   &&
61            argno_   == err.argno_   &&
62            width_   == err.width_   &&
63            op_      == err.op_      &&
64            passed_->Equals(*err.passed_) &&
65            failed_->Equals(*err.failed_);
66   } else {
67     SANDBOX_DIE("Corrupted ErrorCode");
68   }
69 }
70
71 bool ErrorCode::LessThan(const ErrorCode& err) const {
72   // Implementing a "LessThan()" operator allows us to use ErrorCode objects
73   // as keys in STL containers; most notably, it also allows us to put them
74   // into std::set<>. Actual ordering is not important as long as it is
75   // deterministic.
76   if (error_type_ == ET_INVALID || err.error_type_ == ET_INVALID) {
77     SANDBOX_DIE("Dereferencing invalid ErrorCode");
78   }
79   if (error_type_ != err.error_type_) {
80     return error_type_ < err.error_type_;
81   } else {
82     if (error_type_ == ET_SIMPLE || error_type_ == ET_TRAP) {
83       return err_ < err.err_;
84     } else if (error_type_ == ET_COND) {
85       if (value_ != err.value_) {
86         return value_ < err.value_;
87       } else if (argno_ != err.argno_) {
88         return argno_ < err.argno_;
89       } else if (width_ != err.width_) {
90         return width_ < err.width_;
91       } else if (op_ != err.op_) {
92         return op_ < err.op_;
93       } else if (!passed_->Equals(*err.passed_)) {
94         return passed_->LessThan(*err.passed_);
95       } else if (!failed_->Equals(*err.failed_)) {
96         return failed_->LessThan(*err.failed_);
97       } else {
98         return false;
99       }
100     } else {
101       SANDBOX_DIE("Corrupted ErrorCode");
102     }
103   }
104 }
105
106 }  // namespace