1 // Copyright 2019 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // The sandbox2::StackTrace class provides static methods useful when analyzing 16 // call-stack of the process. It uses libunwind-ptrace, so the process must be 17 // in a stopped state to call those methods. 18 19 #ifndef SANDBOXED_API_SANDBOX2_STACK_TRACE_H_ 20 #define SANDBOXED_API_SANDBOX2_STACK_TRACE_H_ 21 22 #include <cstddef> 23 #include <memory> 24 #include <string> 25 #include <utility> 26 #include <vector> 27 28 #include "absl/log/check.h" 29 #include "absl/status/statusor.h" 30 #include "sandboxed_api/sandbox2/comms.h" 31 #include "sandboxed_api/sandbox2/executor.h" 32 #include "sandboxed_api/sandbox2/namespace.h" 33 #include "sandboxed_api/sandbox2/policy.h" 34 #include "sandboxed_api/sandbox2/regs.h" 35 #include "sandboxed_api/sandbox2/result.h" 36 37 namespace sandbox2 { 38 39 class Sandbox2; 40 class StackTraceTestPeer; 41 42 namespace internal { 43 44 class SandboxPeer { 45 public: Spawn(std::unique_ptr<Executor> executor,std::unique_ptr<Policy> policy)46 static std::unique_ptr<SandboxPeer> Spawn(std::unique_ptr<Executor> executor, 47 std::unique_ptr<Policy> policy) { 48 CHECK_NE(spawn_fn_, nullptr); 49 return spawn_fn_(std::move(executor), std::move(policy)); 50 } 51 52 virtual ~SandboxPeer() = default; 53 54 virtual Comms* comms() = 0; 55 virtual void Kill() = 0; 56 virtual Result AwaitResult() = 0; 57 58 private: 59 friend class ::sandbox2::Sandbox2; 60 friend class ::sandbox2::StackTraceTestPeer; 61 using SpawnFn = std::unique_ptr<SandboxPeer> (*)(std::unique_ptr<Executor>, 62 std::unique_ptr<Policy>); 63 static SpawnFn spawn_fn_; 64 }; 65 66 } // namespace internal 67 68 // Maximum depth of analyzed call stack. 69 constexpr size_t kDefaultMaxFrames = 200; 70 71 // Returns the stack-trace of the PID=pid, one line per frame. 72 absl::StatusOr<std::vector<std::string>> GetStackTrace( 73 const Regs* regs, const Namespace* ns, bool uses_custom_forkserver, 74 int recursion_depth); 75 76 // Returns a stack trace that collapses duplicate stack frames and annotates 77 // them with a repetition count. 78 // Example: 79 // _start _start 80 // main main 81 // recursive_call recursive_call 82 // recursive_call (previous frame repeated 2 times) 83 // recursive_call tail_call 84 // tail_call 85 std::vector<std::string> CompactStackTrace( 86 const std::vector<std::string>& stack_trace); 87 88 } // namespace sandbox2 89 90 #endif // SANDBOXED_API_SANDBOX2_STACK_TRACE_H_ 91