Refactor syscall definitions to rely less on macros

PiperOrigin-RevId: 288478535
Change-Id: I56bf8b8817f31d60db4726b2847f8400215b7b8c
This commit is contained in:
Christian Blichmann 2020-01-07 05:26:10 -08:00 committed by Copybara-Service
parent 3e442b252c
commit 18776b6f16
3 changed files with 1191 additions and 1194 deletions

View File

@ -68,38 +68,17 @@ uint32_t Syscall::GetHostAuditArch() {
#endif
}
namespace {
// Syscall entry in syscall table for the architecture
const SyscallTable GetSyscallTable(Syscall::CpuArch arch) {
switch (arch) {
#if defined(__x86_64__)
case Syscall::kX86_64:
return SyscallTable::kSyscallDataX8664;
case Syscall::kX86_32:
return SyscallTable::kSyscallDataX8632;
#elif defined(__powerpc64__)
case Syscall::kPPC_64:
return SyscallTable::kSyscallDataPPC64;
#endif
default:
return SyscallTable();
}
}
} // namespace
std::string Syscall::GetName() const {
const char* name = GetSyscallTable(arch_).GetEntry(nr_).name;
if (name == nullptr) {
absl::string_view name = SyscallTable::get(arch_).GetName(nr_);
if (name.empty()) {
return absl::StrFormat("UNKNOWN[%d/0x%x]", nr_, nr_);
}
return name;
return std::string(name);
}
std::vector<std::string> Syscall::GetArgumentsDescription() const {
return GetSyscallTable(arch_).GetEntry(nr_).GetArgumentsDescription(
args_.data(), pid_);
return SyscallTable::get(arch_).GetArgumentsDescription(nr_, args_.data(),
pid_);
}
std::string Syscall::GetDescription() const {

File diff suppressed because it is too large Load Diff

View File

@ -2,76 +2,40 @@
#define SANDBOXED_API_SANDBOX2_SYSCALL_DEFS_H_
#include <sys/types.h>
#include <cstdint>
#include <string>
#include <vector>
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "sandboxed_api/sandbox2/syscall.h"
namespace sandbox2 {
namespace syscalls {
constexpr int kMaxArgs = 6;
}
} // namespace syscalls
class SyscallTable {
public:
// Type of a given syscall argument. Used with argument conversion routines.
enum ArgType {
kGen = 1,
kInt,
kPath,
kHex,
kOct,
kSocketCall,
kSocketCallPtr,
kSignal,
kString,
kAddressFamily,
kSockaddr,
kSockmsghdr,
kCloneFlag,
};
struct Entry;
// Single syscall definition
struct Entry {
const char* const name;
const int num_args;
const ArgType arg_types[syscalls::kMaxArgs];
// Returns the number of arguments which given syscall takes.
int GetNumArgs() const {
if (num_args < 0 || num_args > syscalls::kMaxArgs) {
return syscalls::kMaxArgs;
}
return num_args;
}
std::vector<std::string> GetArgumentsDescription(
const uint64_t values[syscalls::kMaxArgs], pid_t pid) const;
};
#if defined(__x86_64__)
static const absl::Span<const Entry> kSyscallDataX8664;
static const absl::Span<const Entry> kSyscallDataX8632;
#elif defined(__powerpc64__)
static const absl::Span<const Entry> kSyscallDataPPC64;
#endif
constexpr SyscallTable() = default;
constexpr SyscallTable(absl::Span<const Entry> data) : data_(data) {}
// Returns the syscall table for the architecture.
static SyscallTable get(Syscall::CpuArch arch);
int size() { return data_.size(); }
const Entry& GetEntry(uint64_t syscall) const {
static Entry invalid_entry{
nullptr, syscalls::kMaxArgs, {kGen, kGen, kGen, kGen, kGen, kGen}};
if (syscall < data_.size()) {
return data_[syscall];
}
return invalid_entry;
}
absl::string_view GetName(int syscall) const;
std::vector<std::string> GetArgumentsDescription(
int syscall, const uint64_t values[syscalls::kMaxArgs], pid_t pid) const;
private:
constexpr SyscallTable() = default;
explicit constexpr SyscallTable(absl::Span<const Entry> data) : data_(data) {}
const absl::Span<const Entry> data_;
};