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

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

Issue 659723002: SyscallIterator: support C++11 range-based for loops (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync and fix style Created 6 years, 2 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
« no previous file with comments | « sandbox/linux/bpf_dsl/policy_compiler.cc ('k') | sandbox/linux/seccomp-bpf/syscall_iterator.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_ITERATOR_H__ 5 #ifndef SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_ITERATOR_H__
6 #define SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_ITERATOR_H__ 6 #define SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_ITERATOR_H__
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "sandbox/sandbox_export.h" 11 #include "sandbox/sandbox_export.h"
12 12
13 namespace sandbox { 13 namespace sandbox {
14 class SyscallIterator;
14 15
15 // Iterates over the entire system call range from 0..0xFFFFFFFFu. This 16 // Iterates over the entire system call range from 0..0xFFFFFFFFu. This
16 // iterator is aware of how system calls look like and will skip quickly 17 // iterator is aware of how system calls look like and will skip quickly
17 // over ranges that can't contain system calls. It iterates more slowly 18 // over ranges that can't contain system calls. It iterates more slowly
18 // whenever it reaches a range that is potentially problematic, returning 19 // whenever it reaches a range that is potentially problematic, returning
19 // the last invalid value before a valid range of system calls, and the 20 // the last invalid value before a valid range of system calls, and the
20 // first invalid value after a valid range of syscalls. It iterates over 21 // first invalid value after a valid range of syscalls. It iterates over
21 // individual values whenever it is in the normal range for system calls 22 // individual values whenever it is in the normal range for system calls
22 // (typically MIN_SYSCALL..MAX_SYSCALL). 23 // (typically MIN_SYSCALL..MAX_SYSCALL).
23 // If |invalid_only| is true, this iterator will only return invalid
24 // syscall numbers, but will still skip quickly over invalid ranges,
25 // returning the first invalid value in the range and then skipping
26 // to the last invalid value in the range.
27 // 24 //
28 // Example usage: 25 // Example usage:
29 // for (SyscallIterator iter(false); !iter.Done(); ) { 26 // for (uint32_t sysnum : SyscallSet::ALL) {
30 // uint32_t sysnum = iter.Next();
31 // // Do something with sysnum. 27 // // Do something with sysnum.
32 // } 28 // }
33 // 29
34 // TODO(markus): Make this a classic C++ iterator. 30 enum class SyscallSet {
31 // The "ALL" system call set contains both valid and invalid system
32 // call numbers.
33 ALL,
34
35 // The "INVALID_ONLY" system call set contains only valid system call
36 // numbers, but will still skip quickly over invalid ranges,
37 // returning the first invalid value in the range and then skipping
38 // to the last invalid value in the range.
39 INVALID_ONLY,
40
41 // TODO(mdempsky): Add ValidOnly.
42 };
43
44 // Begin returns an iterator pointing to the first element of the
45 // specified system call set.
46 SANDBOX_EXPORT SyscallIterator begin(SyscallSet set);
jln (very slow on Chromium) 2014/10/21 23:31:24 I find it a little strange to have a top-level beg
mdempsky 2014/10/22 00:15:33 We can, but it would add some more boiler plate, a
47
48 // End returns an iterator pointing to the past-the-end element of the
49 // specified system call set.
50 SANDBOX_EXPORT SyscallIterator end(SyscallSet set);
51
52 // SyscallIterator provides C++ forward iterator semantics for
53 // traversing a system call set.
35 class SANDBOX_EXPORT SyscallIterator { 54 class SANDBOX_EXPORT SyscallIterator {
36 public: 55 public:
37 explicit SyscallIterator(bool invalid_only) 56 SyscallIterator(SyscallSet set, bool done);
38 : invalid_only_(invalid_only), done_(false), num_(0) {}
39 57
40 bool Done() const { return done_; } 58 uint32_t operator*() const;
41 uint32_t Next(); 59 SyscallIterator& operator++();
jln (very slow on Chromium) 2014/10/21 23:31:24 I don't think we need / use the return value. Let'
mdempsky 2014/10/22 00:15:33 Technically, the C++ spec's requirements on iterat
60
61 friend bool operator==(const SyscallIterator& lhs,
62 const SyscallIterator& rhs);
63
64 // TODO(mdempsky): Move elsewhere.
42 static bool IsValid(uint32_t num); 65 static bool IsValid(uint32_t num);
43 66
44 private: 67 private:
45 bool invalid_only_; 68 SyscallSet set_;
46 bool done_; 69 bool done_;
jln (very slow on Chromium) 2014/10/21 23:31:24 done_ feels weird. Could we just replace it with
mdempsky 2014/10/22 00:15:33 We can't simply get rid of "bool done_", because "
47 uint32_t num_; 70 uint32_t num_;
71 };
48 72
49 DISALLOW_IMPLICIT_CONSTRUCTORS(SyscallIterator); 73 SANDBOX_EXPORT bool operator!=(const SyscallIterator& lhs,
50 }; 74 const SyscallIterator& rhs);
51 75
52 } // namespace sandbox 76 } // namespace sandbox
53 77
54 #endif // SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_ITERATOR_H__ 78 #endif // SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_ITERATOR_H__
OLDNEW
« no previous file with comments | « sandbox/linux/bpf_dsl/policy_compiler.cc ('k') | sandbox/linux/seccomp-bpf/syscall_iterator.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698