xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/sandbox2/syscall_defs.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 #ifndef SANDBOXED_API_SANDBOX2_SYSCALL_DEFS_H_
2 #define SANDBOXED_API_SANDBOX2_SYSCALL_DEFS_H_
3 
4 #include <sys/types.h>
5 
6 #include <array>
7 #include <cstdint>
8 #include <string>
9 #include <vector>
10 
11 #include "absl/status/statusor.h"
12 #include "absl/strings/string_view.h"
13 #include "absl/types/span.h"
14 #include "sandboxed_api/config.h"
15 #include "sandboxed_api/sandbox2/syscall.h"
16 
17 namespace sandbox2 {
18 namespace syscalls {
19 
20 constexpr int kMaxArgs = 6;
21 
22 // Type of a given syscall argument. Used with argument conversion routines.
23 enum ArgType {
24   kGen = 1,
25   kInt,
26   kPath,
27   kHex,
28   kOct,
29   kSocketCall,
30   kSocketCallPtr,
31   kSignal,
32   kString,
33   kAddressFamily,
34   kSockaddr,
35   kSockmsghdr,
36   kCloneFlag,
37 };
38 
39 }  // namespace syscalls
40 
41 class SyscallTable {
42  public:
43   // Single syscall definition
44   struct Entry {
45     // Returns the number of arguments which given syscall takes.
GetNumArgsEntry46     int GetNumArgs() const {
47       if (num_args < 0 || num_args > syscalls::kMaxArgs) {
48         return syscalls::kMaxArgs;
49       }
50       return num_args;
51     }
52 
53     static std::string GetArgumentDescription(uint64_t value,
54                                               syscalls::ArgType type,
55                                               pid_t pid);
56 
BySyscallNrEntry57     static constexpr bool BySyscallNr(const SyscallTable::Entry& a,
58                                       const SyscallTable::Entry& b) {
59       return a.nr < b.nr;
60     }
61 
62     int nr;
63     absl::string_view name;
64     int num_args;
65     std::array<syscalls::ArgType, syscalls::kMaxArgs> arg_types;
66   };
67 
68   // Returns the syscall table for the architecture.
69   static SyscallTable get(sapi::cpu::Architecture arch);
70 
size()71   int size() { return data_.size(); }
72 
73   absl::string_view GetName(int syscall) const;
74 
75   std::vector<std::string> GetArgumentsDescription(int syscall,
76                                                    const uint64_t values[],
77                                                    pid_t pid) const;
78 
79   absl::StatusOr<Entry> GetEntry(int syscall) const;
80   // Returns the first entry matching the provided name.
81   absl::StatusOr<Entry> GetEntry(absl::string_view name) const;
82 
GetEntries()83   absl::Span<const Entry> GetEntries() const { return data_; }
84 
85  private:
86   constexpr SyscallTable() = default;
SyscallTable(absl::Span<const Entry> data)87   explicit constexpr SyscallTable(absl::Span<const Entry> data) : data_(data) {}
88 
89   const absl::Span<const Entry> data_;
90 };
91 
92 }  // namespace sandbox2
93 
94 #endif  // SANDBOXED_API_SANDBOX2_SYSCALL_DEFS_H_
95