OLD | NEW |
(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 SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_ITERATOR_H__ |
| 6 #define SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_ITERATOR_H__ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "sandbox/sandbox_export.h" |
| 12 |
| 13 namespace sandbox { |
| 14 |
| 15 // TODO(mdempsky): Rename this header to syscall_set.h. |
| 16 |
| 17 // Iterates over the entire system call range from 0..0xFFFFFFFFu. This |
| 18 // iterator is aware of how system calls look like and will skip quickly |
| 19 // over ranges that can't contain system calls. It iterates more slowly |
| 20 // whenever it reaches a range that is potentially problematic, returning |
| 21 // the last invalid value before a valid range of system calls, and the |
| 22 // first invalid value after a valid range of syscalls. It iterates over |
| 23 // individual values whenever it is in the normal range for system calls |
| 24 // (typically MIN_SYSCALL..MAX_SYSCALL). |
| 25 // |
| 26 // Example usage: |
| 27 // for (uint32_t sysnum : SyscallSet::All()) { |
| 28 // // Do something with sysnum. |
| 29 // } |
| 30 class SANDBOX_EXPORT SyscallSet { |
| 31 public: |
| 32 class Iterator; |
| 33 |
| 34 SyscallSet(const SyscallSet& ss) : set_(ss.set_) {} |
| 35 ~SyscallSet() {} |
| 36 |
| 37 Iterator begin() const; |
| 38 Iterator end() const; |
| 39 |
| 40 // All returns a SyscallSet that contains both valid and invalid |
| 41 // system call numbers. |
| 42 static SyscallSet All() { return SyscallSet(Set::ALL); } |
| 43 |
| 44 // InvalidOnly returns a SyscallSet that contains only invalid |
| 45 // system call numbers, but still omits numbers in the middle of a |
| 46 // range of invalid system call numbers. |
| 47 static SyscallSet InvalidOnly() { return SyscallSet(Set::INVALID_ONLY); } |
| 48 |
| 49 // IsValid returns whether |num| specifies a valid system call |
| 50 // number. |
| 51 static bool IsValid(uint32_t num); |
| 52 |
| 53 private: |
| 54 enum class Set { ALL, INVALID_ONLY }; |
| 55 |
| 56 explicit SyscallSet(Set set) : set_(set) {} |
| 57 |
| 58 Set set_; |
| 59 |
| 60 friend bool operator==(const SyscallSet&, const SyscallSet&); |
| 61 DISALLOW_ASSIGN(SyscallSet); |
| 62 }; |
| 63 |
| 64 SANDBOX_EXPORT bool operator==(const SyscallSet& lhs, const SyscallSet& rhs); |
| 65 |
| 66 // Iterator provides C++ input iterator semantics for traversing a |
| 67 // SyscallSet. |
| 68 class SyscallSet::Iterator { |
| 69 public: |
| 70 Iterator(const Iterator& it) |
| 71 : set_(it.set_), done_(it.done_), num_(it.num_) {} |
| 72 ~Iterator() {} |
| 73 |
| 74 uint32_t operator*() const; |
| 75 Iterator& operator++(); |
| 76 |
| 77 private: |
| 78 Iterator(Set set, bool done); |
| 79 |
| 80 Set set_; |
| 81 bool done_; |
| 82 uint32_t num_; |
| 83 |
| 84 friend SyscallSet; |
| 85 friend bool operator==(const Iterator&, const Iterator&); |
| 86 DISALLOW_ASSIGN(Iterator); |
| 87 }; |
| 88 |
| 89 SANDBOX_EXPORT bool operator==(const SyscallSet::Iterator& lhs, |
| 90 const SyscallSet::Iterator& rhs); |
| 91 SANDBOX_EXPORT bool operator!=(const SyscallSet::Iterator& lhs, |
| 92 const SyscallSet::Iterator& rhs); |
| 93 |
| 94 } // namespace sandbox |
| 95 |
| 96 #endif // SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_ITERATOR_H__ |
OLD | NEW |