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 3 // found in the LICENSE file. 4 5 #ifndef SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTER_COMPATIBILITY_DELEGATE_H_ 6 #define SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTER_COMPATIBILITY_DELEGATE_H_ 7 8 #include <memory> 9 10 #include "base/macros.h" 11 #include "sandbox/linux/seccomp-bpf/sandbox_bpf_test_runner.h" 12 13 namespace sandbox { 14 15 // This templated class allows building a BPFTesterDelegate from a 16 // deprecated-style BPF policy (that is a SyscallEvaluator function pointer, 17 // instead of a SandboxBPFPolicy class), specified in |policy_function| and a 18 // function pointer to a test in |test_function|. 19 // This allows both the policy and the test function to take a pointer to an 20 // object of type "Aux" as a parameter. This is used to implement the BPF_TEST 21 // macro and should generally not be used directly. 22 template <class Policy, class Aux> 23 class BPFTesterCompatibilityDelegate : public BPFTesterDelegate { 24 public: 25 typedef void (*TestFunction)(Aux*); 26 BPFTesterCompatibilityDelegate(TestFunction test_function)27 explicit BPFTesterCompatibilityDelegate(TestFunction test_function) 28 : aux_(), test_function_(test_function) {} 29 ~BPFTesterCompatibilityDelegate()30 ~BPFTesterCompatibilityDelegate() override {} 31 GetSandboxBPFPolicy()32 std::unique_ptr<bpf_dsl::Policy> GetSandboxBPFPolicy() override { 33 // The current method is guaranteed to only run in the child process 34 // running the test. In this process, the current object is guaranteed 35 // to live forever. So it's ok to pass aux_pointer_for_policy_ to 36 // the policy, which could in turn pass it to the kernel via Trap(). 37 return std::unique_ptr<bpf_dsl::Policy>(new Policy(&aux_)); 38 } 39 RunTestFunction()40 void RunTestFunction() override { 41 // Run the actual test. 42 // The current object is guaranteed to live forever in the child process 43 // where this will run. 44 test_function_(&aux_); 45 } 46 47 private: 48 Aux aux_; 49 TestFunction test_function_; 50 51 DISALLOW_COPY_AND_ASSIGN(BPFTesterCompatibilityDelegate); 52 }; 53 54 } // namespace sandbox 55 56 #endif // SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTER_COMPATIBILITY_DELEGATE_H_ 57