Created
May 27, 2020 19:38
-
-
Save drakenclimber/5ce9cf25d38f1742823af9060db14869 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import subprocess | |
LIBSECCOMP_DIR="upstream-libseccomp" | |
DUMP_TOOL=os.path.join(LIBSECCOMP_DIR, "src/arch-syscall-dump") | |
ARCH_LIST=[ | |
"x86", | |
"x86_64", | |
"x32", | |
"arm", | |
"aarch64", | |
"mips", | |
"mips64", | |
"mips64n32", | |
"parisc", | |
"parisc64", | |
"ppc", | |
"ppc64", | |
"riscv64", | |
"s390", | |
"s390x", | |
] | |
def run(command, run_in_shell=False): | |
if run_in_shell: | |
if isinstance(command, str): | |
# nothing to do. command is already formatted as a string | |
pass | |
elif isinstance(command, list): | |
command = ' '.join(command) | |
else: | |
raise ValueError('Unsupported command type') | |
subproc = subprocess.Popen(command, shell=run_in_shell, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
out, err = subproc.communicate() | |
ret = subproc.returncode | |
out = out.strip() | |
err = err.strip() | |
if ret != 0: | |
raise Exception("Command '{}' failed".format(''.join(command)), | |
command, ret, out, err) | |
return out.decode("utf-8") | |
def build_arch_dict(arch): | |
print("Building syscall dictionary for {}".format(arch)) | |
try: | |
syscall_array = run([DUMP_TOOL, '-a', arch]) | |
except: | |
print("Warning: architecture {} not supported".format(arch)) | |
return None | |
arch_dict = dict() | |
for line in syscall_array.splitlines(): | |
[syscall, syscall_num] = line.split() | |
if arch == 'x32' and int(syscall_num) >= 0x40000000: | |
syscall_num = int(syscall_num) - 0x40000000 | |
if arch == 'mips' and int(syscall_num) >= 4000: | |
syscall_num = int(syscall_num) - 4000 | |
if arch == 'mips64' and int(syscall_num) >= 5000: | |
syscall_num = int(syscall_num) - 5000 | |
if arch == 'mips64n32' and int(syscall_num) >= 6000: | |
syscall_num = int(syscall_num) - 6000 | |
if int(syscall_num) < 0: | |
syscall_num = 'PNR' | |
arch_dict[syscall] = syscall_num | |
return arch_dict | |
def output_csv(arches_dict): | |
git_branch = run(["pushd {} > /dev/null;git branch --show-current;popd > /dev/null".format(LIBSECCOMP_DIR)], | |
run_in_shell=True) | |
f = open('{}.csv'.format(git_branch), 'w') | |
arch_keys = arches_dict.keys() | |
f.write('#syscall') | |
for arch in arch_keys: | |
f.write(',{}'.format(arch)) | |
f.write('\n') | |
syscall_keys = sorted(arches_dict[ARCH_LIST[0]].keys()) | |
#syscall_keys = list(arches_dict[ARCH_LIST[0]].keys()) | |
for syscall in syscall_keys: | |
f.write('{}'.format(syscall)) | |
for arch in arch_keys: | |
f.write(',{}'.format(arches_dict[arch][syscall])) | |
f.write('\n') | |
f.close() | |
if __name__ == "__main__": | |
all_arches_dict = dict() | |
for arch in ARCH_LIST: | |
arch_dict = build_arch_dict(arch) | |
if arch_dict is not None: | |
all_arches_dict[arch] = arch_dict | |
output_csv(all_arches_dict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment