Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: sandbox/linux/seccomp-bpf/errorcode.h

Issue 10833044: Refactored ErrorCode into it's own class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased, and removed "protected" as requested by jln Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 #ifndef ERRORCODE_H__
6 #define ERRORCODE_H__
7
8 namespace playground2 {
9
10 class ErrorCode {
jln (very slow on Chromium) 2012/07/27 19:46:30 Do you mind adding a descriptive comment for the c
11 public:
12 enum {
13 // Allow this system call.
14 ERR_ALLOWED = 0x0000,
15
16 // Deny the system call with a particular "errno" value.
17 ERR_MIN_ERRNO = 1,
18 ERR_MAX_ERRNO = 4095,
19
20 // This code should never be used directly, it is used internally only.
21 ERR_INVALID = -1,
22 };
23
24 // TrapFnc is a pointer to a function that handles Seccomp traps in
25 // user-space. The seccomp policy can request that a trap handler gets
26 // installed; it does so by returning a suitable ErrorCode() from the
27 // syscallEvaluator. See the ErrorCode() constructor for how to pass in
28 // the function pointer.
29 // Please note that TrapFnc is executed from signal context and must be
30 // async-signal safe:
31 // http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html
32 typedef intptr_t (*TrapFnc)(const struct arch_seccomp_data& args, void *aux);
33
34 enum ArgType {
35 TP_32BIT, TP_64BIT,
36 };
37
38 enum Operation {
39 OP_EQUAL, OP_GREATER, OP_GREATER_EQUAL, OP_HAS_BITS,
40 OP_NUM_OPS,
41 };
42
43 // We can either wrap a symbolic ErrorCode (i.e. ERR_XXX enum values), an
44 // errno value (in the range 1..4095), a pointer to a TrapFnc callback
45 // handling a SECCOMP_RET_TRAP trap, or a complex constraint.
46 // All of the commonly used values are stored in the "err_" field. So, code
47 // that is using the ErrorCode class typically operates on a single 32bit
48 // field.
49 // This is not only quite efficient, it also makes the API really easy to
50 // use.
51 ErrorCode(int err = ERR_INVALID);
jln (very slow on Chromium) 2012/07/27 19:46:30 Style: please make a default constructor call a co
52
jln (very slow on Chromium) 2012/07/27 19:46:30 Please add an explicit copy constructor (since I t
53 // Destructor
54 ~ErrorCode() { }
55
56 bool isEqual(const ErrorCode& err) const;
jln (very slow on Chromium) 2012/07/27 19:46:30 Since this is a new class, would you mind capitali
57 bool lessThan(const ErrorCode& err) const;
58
59 struct LessThan {
60 bool operator()(const ErrorCode& a, const ErrorCode& b) const {
jln (very slow on Chromium) 2012/07/27 19:46:30 I think this is better declared outside of the cla
61 return a.lessThan(b);
62 }
63 };
64
65 private:
66 friend class CodeGen;
jln (very slow on Chromium) 2012/07/27 19:46:30 Nit: this class doesn't exist for now. Feel free t
67 friend class Sandbox;
68 friend class Verifier;
69
70 static void die(const char *msg) __attribute__((noreturn));
71
72 // If we are wrapping a callback, we must assign a unique id. This id is
73 // how the kernel tells us which one of our different SECCOMP_RET_TRAP
74 // cases has been triggered.
75 ErrorCode(TrapFnc fnc, const void *aux, uint16_t id);
76
77 // Some system calls require inspection of arguments. This constructor
78 // allows us to specify additional constraints.
79 ErrorCode(int argno, ArgType width, Operation op, uint64_t value,
80 const ErrorCode *passed, const ErrorCode *failed);
81
82 enum {
jln (very slow on Chromium) 2012/07/27 19:46:30 Style: enum and typedefs should be at the start of
83 ET_INVALID, ET_SIMPLE, ET_TRAP, ET_COND,
84 } errorType_;
85
86 union {
87 // Fields needed for SECCOMP_RET_TRAP callbacks
88 struct {
89 TrapFnc fnc_;
90 void *aux_;
91 };
92
93 // Fields needed when inspecting additional arguments.
94 struct {
95 uint64_t value_;
96 int argno_;
jln (very slow on Chromium) 2012/07/27 19:46:30 Please add some documentation to those members. Ma
97 ArgType width_;
98 Operation op_;
99 const ErrorCode *passed_;
100 const ErrorCode *failed_;
101 };
102 };
103
104 // 32bit field used for all possible types of ErrorCode values. This is
105 // the value that uniquely identifies any ErrorCode and it (typically) can
106 // be emitted directly into a BPF filter program.
107 uint32_t err_;
108
109 };
110
111 } // namespace
112
113 #endif // ERRORCODE_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698