OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_BPF_TESTER_COMPATIBILITY_DELEGATE_H_ | 5 #ifndef SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTER_COMPATIBILITY_DELEGATE_H_ |
6 #define SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTER_COMPATIBILITY_DELEGATE_H_ | 6 #define SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTER_COMPATIBILITY_DELEGATE_H_ |
7 | 7 |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 | 11 |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "sandbox/linux/seccomp-bpf/sandbox_bpf_compatibility_policy.h" | 13 #include "sandbox/linux/seccomp-bpf/sandbox_bpf_compatibility_policy.h" |
14 #include "sandbox/linux/seccomp-bpf/sandbox_bpf_test_runner.h" | 14 #include "sandbox/linux/seccomp-bpf/sandbox_bpf_test_runner.h" |
15 #include "sandbox/linux/tests/sandbox_test_runner.h" | 15 #include "sandbox/linux/tests/sandbox_test_runner.h" |
16 #include "sandbox/linux/tests/unit_tests.h" | 16 #include "sandbox/linux/tests/unit_tests.h" |
17 | 17 |
18 namespace sandbox { | 18 namespace sandbox { |
19 | 19 |
20 namespace internal { | |
21 | |
22 // Internal helper class to hold a value of type T. | |
23 template <typename T> | |
24 class AuxHolder { | |
25 public: | |
26 AuxHolder() : val_() {} | |
27 T* get() { return &val_; } | |
28 | |
29 private: | |
30 T val_; | |
31 }; | |
32 | |
33 // Specialization of AuxHolder for void. | |
34 // Returns a null pointer instead of allocating void. | |
35 template <> | |
36 class AuxHolder<void> { | |
37 public: | |
38 AuxHolder() {} | |
39 void* get() { return NULL; } | |
40 }; | |
41 | |
42 } // namespace internal | |
43 | |
44 // This templated class allows building a BPFTesterDelegate from a | 20 // This templated class allows building a BPFTesterDelegate from a |
45 // deprecated-style BPF policy (that is a SyscallEvaluator function pointer, | 21 // deprecated-style BPF policy (that is a SyscallEvaluator function pointer, |
46 // instead of a SandboxBPFPolicy class), specified in |policy_function| and a | 22 // instead of a SandboxBPFPolicy class), specified in |policy_function| and a |
47 // function pointer to a test in |test_function|. | 23 // function pointer to a test in |test_function|. |
48 // This allows both the policy and the test function to take a pointer to an | 24 // This allows both the policy and the test function to take a pointer to an |
49 // object of type "Aux" as a parameter. This is used to implement the BPF_TEST | 25 // object of type "Aux" as a parameter. This is used to implement the BPF_TEST |
50 // macro and should generally not be used directly. | 26 // macro and should generally not be used directly. |
51 template <class Aux = void> | 27 template <class Aux> |
52 class BPFTesterCompatibilityDelegate : public BPFTesterDelegate { | 28 class BPFTesterCompatibilityDelegate : public BPFTesterDelegate { |
53 public: | 29 public: |
54 typedef Aux AuxType; | 30 typedef Aux AuxType; |
55 BPFTesterCompatibilityDelegate( | 31 BPFTesterCompatibilityDelegate( |
56 void (*test_function)(AuxType*), | 32 void (*test_function)(AuxType*), |
57 typename CompatibilityPolicy<AuxType>::SyscallEvaluator policy_function) | 33 typename CompatibilityPolicy<AuxType>::SyscallEvaluator policy_function) |
58 : aux_holder_(), | 34 : aux_(), |
59 test_function_(test_function), | 35 test_function_(test_function), |
60 policy_function_(policy_function) {} | 36 policy_function_(policy_function) {} |
61 | 37 |
62 virtual ~BPFTesterCompatibilityDelegate() {} | 38 virtual ~BPFTesterCompatibilityDelegate() {} |
63 | 39 |
64 virtual scoped_ptr<SandboxBPFPolicy> GetSandboxBPFPolicy() OVERRIDE { | 40 virtual scoped_ptr<SandboxBPFPolicy> GetSandboxBPFPolicy() OVERRIDE { |
65 // The current method is guaranteed to only run in the child process | 41 // The current method is guaranteed to only run in the child process |
66 // running the test. In this process, the current object is guaranteed | 42 // running the test. In this process, the current object is guaranteed |
67 // to live forever. So it's ok to pass aux_pointer_for_policy_ to | 43 // to live forever. So it's ok to pass aux_pointer_for_policy_ to |
68 // the policy, which could in turn pass it to the kernel via Trap(). | 44 // the policy, which could in turn pass it to the kernel via Trap(). |
69 return scoped_ptr<SandboxBPFPolicy>( | 45 return scoped_ptr<SandboxBPFPolicy>( |
70 new CompatibilityPolicy<AuxType>(policy_function_, aux_holder_.get())); | 46 new CompatibilityPolicy<AuxType>(policy_function_, &aux_)); |
71 } | 47 } |
72 | 48 |
73 virtual void RunTestFunction() OVERRIDE { | 49 virtual void RunTestFunction() OVERRIDE { |
74 // Run the actual test. | 50 // Run the actual test. |
75 // The current object is guaranteed to live forever in the child process | 51 // The current object is guaranteed to live forever in the child process |
76 // where this will run. | 52 // where this will run. |
77 test_function_(aux_holder_.get()); | 53 test_function_(&aux_); |
78 } | 54 } |
79 | 55 |
80 private: | 56 private: |
81 internal::AuxHolder<AuxType> aux_holder_; | 57 AuxType aux_; |
82 void (*test_function_)(AuxType*); | 58 void (*test_function_)(AuxType*); |
83 typename CompatibilityPolicy<AuxType>::SyscallEvaluator policy_function_; | 59 typename CompatibilityPolicy<AuxType>::SyscallEvaluator policy_function_; |
84 DISALLOW_COPY_AND_ASSIGN(BPFTesterCompatibilityDelegate); | 60 DISALLOW_COPY_AND_ASSIGN(BPFTesterCompatibilityDelegate); |
85 }; | 61 }; |
86 | 62 |
87 } // namespace sandbox | 63 } // namespace sandbox |
88 | 64 |
89 #endif // SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTER_COMPATIBILITY_DELEGATE_H_ | 65 #endif // SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTER_COMPATIBILITY_DELEGATE_H_ |
OLD | NEW |