Chromium Code Reviews| Index: sandbox/linux/seccomp-bpf/errorcode.cc |
| diff --git a/sandbox/linux/seccomp-bpf/errorcode.cc b/sandbox/linux/seccomp-bpf/errorcode.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4296a5f5fd7e18063ad752eb4e9b6fdeded65952 |
| --- /dev/null |
| +++ b/sandbox/linux/seccomp-bpf/errorcode.cc |
| @@ -0,0 +1,112 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "sandbox/linux/seccomp-bpf/sandbox_bpf.h" |
| + |
| + |
| +namespace playground2 { |
| + |
| +ErrorCode::ErrorCode(int err) { |
| + switch (err) { |
| + case ERR_INVALID: |
| + // We allow the default constructor, as it makes the ErrorCode class |
| + // much easier to use. But if we ever encounter an invalid ErrorCode |
| + // when compiling a BPF filter, we deliberately generate an invalid |
| + // program that will get flagged both by our Verifier class and by |
| + // the Linux kernel. |
| + err_ = SECCOMP_RET_INVALID; |
| + errorType_ = ET_INVALID; |
| + break; |
| + case ERR_ALLOWED: |
| + err_ = SECCOMP_RET_ALLOW; |
| + errorType_ = ET_SIMPLE; |
| + break; |
| + case ERR_MIN_ERRNO ... ERR_MAX_ERRNO: |
| + err_ = SECCOMP_RET_ERRNO + err; |
| + errorType_ = ET_SIMPLE; |
| + break; |
| + default: |
| + die("Invalid use of ErrorCode object"); |
| + } |
| +} |
| + |
| +ErrorCode::ErrorCode(ErrorCode::TrapFnc fnc, const void *aux, uint16_t id) : |
| + errorType_(ET_TRAP), |
|
jln (very slow on Chromium)
2012/07/27 19:46:30
Initializer lists should be indented 4 spaces, the
|
| + fnc_(fnc), |
| + aux_(const_cast<void *>(aux)), |
| + err_(SECCOMP_RET_TRAP + id) { |
| +} |
| + |
| + ErrorCode::ErrorCode(int argno, ArgType width, Operation op, uint64_t value, |
| + const ErrorCode *passed, const ErrorCode *failed) : |
| + errorType_(ET_COND), |
|
jln (very slow on Chromium)
2012/07/27 19:46:30
Same here.
|
| + value_(value), |
| + argno_(argno), |
| + width_(width), |
| + op_(op), |
| + passed_(passed), |
| + failed_(failed), |
| + err_(SECCOMP_RET_INVALID) { |
| + if (op < 0 || op >= OP_NUM_OPS) { |
| + die("Invalid opcode in BPF sandbox rules"); |
| + } |
| +} |
| + |
| +bool ErrorCode::isEqual(const ErrorCode &err) const { |
| + if (errorType_ == ET_INVALID || err.errorType_ == ET_INVALID) { |
| + die("Dereferencing invalid ErrorCode"); |
| + } |
| + if (errorType_ != err.errorType_) { |
| + return false; |
| + } |
| + if (errorType_ == ET_SIMPLE || errorType_ == ET_TRAP) { |
| + return err_ == err.err_; |
| + } else if (errorType_ == ET_COND) { |
| + return value_ == err.value_ && |
| + argno_ == err.argno_ && |
| + width_ == err.width_ && |
| + op_ == err.op_ && |
| + passed_->isEqual(*err.passed_) && |
| + failed_->isEqual(*err.failed_); |
| + } else { |
| + die("Corrupted ErrorCode"); |
| + } |
| +} |
| + |
| +bool ErrorCode::lessThan(const ErrorCode& err) const { |
|
jln (very slow on Chromium)
2012/07/27 19:46:30
This is quit "artificial". Do you mind adding a co
|
| + if (errorType_ == ET_INVALID || err.errorType_ == ET_INVALID) { |
| + die("Dereferencing invalid ErrorCode"); |
| + } |
| + if (errorType_ != err.errorType_) { |
| + return errorType_ < err.errorType_; |
| + } else { |
| + if (errorType_ == ET_SIMPLE || errorType_ == ET_TRAP) { |
| + return err_ < err.err_; |
| + } else if (errorType_ == ET_COND) { |
| + if (value_ != err.value_) { |
| + return value_ < err.value_; |
| + } else if (argno_ != err.argno_) { |
| + return argno_ < err.argno_; |
| + } else if (width_ != err.width_) { |
| + return width_ < err.width_; |
| + } else if (op_ != err.op_) { |
| + return op_ < err.op_; |
| + } else if (!passed_->isEqual(*err.passed_)) { |
| + return passed_->lessThan(*err.passed_); |
|
jln (very slow on Chromium)
2012/07/27 19:46:30
Do you want to add a CHECK here, to check that &er
|
| + } else if (!failed_->isEqual(*err.failed_)) { |
| + return failed_->lessThan(*err.failed_); |
| + } else { |
| + return false; |
| + } |
| + } else { |
| + die("Corrupted ErrorCode"); |
| + } |
| + } |
| +} |
| + |
| +void ErrorCode::die(const char *msg) { |
| + Sandbox::die(msg); |
| +} |
| + |
| +} // namespace |