Skip to content

Commit

Permalink
Get the syscall lists for workers
Browse files Browse the repository at this point in the history
Adds --only-list-syscalls option to list-syscalls.rb to produce these lists
  • Loading branch information
mrcnski committed Sep 21, 2023
1 parent f635302 commit 9d6d172
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 19 deletions.
71 changes: 71 additions & 0 deletions polkadot/scripts/list-syscalls/execute-worker-syscalls
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
0 (read)
1 (write)
2 (open)
3 (close)
4 (stat)
5 (fstat)
7 (poll)
8 (lseek)
9 (mmap)
10 (mprotect)
11 (munmap)
12 (brk)
13 (rt_sigaction)
14 (rt_sigprocmask)
15 (rt_sigreturn)
16 (ioctl)
19 (readv)
20 (writev)
24 (sched_yield)
25 (mremap)
28 (madvise)
39 (getpid)
41 (socket)
42 (connect)
45 (recvfrom)
46 (sendmsg)
53 (socketpair)
55 (getsockopt)
56 (clone)
60 (exit)
61 (wait4)
62 (kill)
72 (fcntl)
79 (getcwd)
82 (rename)
83 (mkdir)
87 (unlink)
89 (readlink)
96 (gettimeofday)
97 (getrlimit)
99 (sysinfo)
102 (getuid)
110 (getppid)
131 (sigaltstack)
140 (getpriority)
141 (setpriority)
144 (sched_setscheduler)
157 (prctl)
158 (arch_prctl)
200 (tkill)
202 (futex)
204 (sched_getaffinity)
213 (epoll_create)
217 (getdents64)
218 (set_tid_address)
228 (clock_gettime)
230 (clock_nanosleep)
231 (exit_group)
232 (epoll_wait)
233 (epoll_ctl)
257 (openat)
262 (newfstatat)
263 (unlinkat)
273 (set_robust_list)
281 (epoll_pwait)
284 (eventfd)
290 (eventfd2)
291 (epoll_create1)
302 (prlimit64)
318 (getrandom)
319 (memfd_create)
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
#
# Syntax: list-syscalls.rb <binary> [--only-used-syscalls]
#
# From: https://gist.github.com/koute/166f82bfee5e27324077891008fca6eb
# Author: @koute
# Source: https://gist.github.com/koute/166f82bfee5e27324077891008fca6eb

require 'shellwords'
require 'set'

SYNTAX_STRING = 'Syntax: list-syscalls.rb <binary> [--only-used-syscalls]'.freeze

# Generated from `libc` using the following regex:
# 'pub const SYS_([a-z0-9_]+): ::c_long = (\d+);'
# ' \2 => "\1",'
Expand Down Expand Up @@ -455,18 +458,29 @@
REG_MAP = (REGS_R64.map { |r| [r, r] } + REGS_R32.zip(REGS_R64) + REGS_R16.zip(REGS_R64) + REGS_R8.zip(REGS_R64)).to_h
REGS_R = (REGS_R64 + REGS_R32 + REGS_R16 + REGS_R8).join('|')

if ARGV.length != 1
warn 'Syntax: list-syscalls.rb <binary> [--only-used-syscalls]'
if ARGV.empty?
warn SYNTAX_STRING
exit 1
end

raise "no such file: #{ARGV[0]}" unless File.exist? ARGV[0]
file_path = ARGV[0]
raise "no such file: #{file_path}" unless File.exist? file_path

puts 'Running objdump...'
dump = `objdump -wd -j .text -M intel #{ARGV[0].shellescape}`
only_used_syscalls = false
ARGV[1..].each do |arg|
if arg == '--only-used-syscalls'
only_used_syscalls = true
else
warn "invalid argument '#{arg}':\n#{SYNTAX_STRING}"
exit 1
end
end

puts 'Running objdump...' unless only_used_syscalls
dump = `objdump -wd -j .text -M intel #{file_path.shellescape}`
raise 'objdump failed' unless $?.exitstatus == 0

puts 'Parsing objdump output...'
puts 'Parsing objdump output...' unless only_used_syscalls
current_fn = nil
code_for_fn = {}
fns_with_syscall = Set.new
Expand All @@ -490,8 +504,10 @@
fns_with_indirect_syscall.add(current_fn) if code =~ /<(syscall|__syscall_cp)>/
end

puts "Found #{fns_with_syscall.length} functions doing direct syscalls"
puts "Found #{fns_with_indirect_syscall.length} functions doing indirect syscalls"
unless only_used_syscalls
puts "Found #{fns_with_syscall.length} functions doing direct syscalls"
puts "Found #{fns_with_indirect_syscall.length} functions doing indirect syscalls"
end

syscalls_for_fn = {}
not_found_count = 0
Expand Down Expand Up @@ -570,16 +586,20 @@
end
end

puts 'Functions per syscall:'
fns_for_syscall.sort_by { |sc, _| sc }.each do |syscall, fn_names|
fn_names = fn_names.sort.uniq
if only_used_syscalls
puts syscalls_for_fn.values.flatten.sort.uniq.map { |sc| SYSCALLS[sc] || sc }.join("\n")
else
puts 'Functions per syscall:'
fns_for_syscall.sort_by { |sc, _| sc }.each do |syscall, fn_names|
fn_names = fn_names.sort.uniq

puts " #{SYSCALLS[syscall] || syscall} [#{fn_names.length} functions]"
fn_names.each do |fn_name|
puts " #{fn_name}"
puts " #{SYSCALLS[syscall] || syscall} [#{fn_names.length} functions]"
fn_names.each do |fn_name|
puts " #{fn_name}"
end
end
end

puts
puts 'Used syscalls:'
puts ' ' + syscalls_for_fn.values.flatten.sort.uniq.map { |sc| SYSCALLS[sc] || sc }.join("\n ")
puts
puts 'Used syscalls:'
puts ' ' + syscalls_for_fn.values.flatten.sort.uniq.map { |sc| SYSCALLS[sc] || sc }.join("\n ")
end
72 changes: 72 additions & 0 deletions polkadot/scripts/list-syscalls/prepare-worker-syscalls
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
1 (write)
2 (open)
3 (close)
4 (stat)
5 (fstat)
7 (poll)
8 (lseek)
9 (mmap)
10 (mprotect)
11 (munmap)
12 (brk)
13 (rt_sigaction)
14 (rt_sigprocmask)
15 (rt_sigreturn)
16 (ioctl)
19 (readv)
20 (writev)
24 (sched_yield)
25 (mremap)
28 (madvise)
39 (getpid)
41 (socket)
42 (connect)
45 (recvfrom)
46 (sendmsg)
53 (socketpair)
55 (getsockopt)
56 (clone)
60 (exit)
61 (wait4)
62 (kill)
72 (fcntl)
79 (getcwd)
82 (rename)
83 (mkdir)
87 (unlink)
89 (readlink)
96 (gettimeofday)
97 (getrlimit)
98 (getrusage)
99 (sysinfo)
102 (getuid)
110 (getppid)
131 (sigaltstack)
140 (getpriority)
141 (setpriority)
144 (sched_setscheduler)
157 (prctl)
158 (arch_prctl)
200 (tkill)
202 (futex)
203 (sched_setaffinity)
204 (sched_getaffinity)
213 (epoll_create)
217 (getdents64)
218 (set_tid_address)
228 (clock_gettime)
230 (clock_nanosleep)
231 (exit_group)
232 (epoll_wait)
233 (epoll_ctl)
257 (openat)
262 (newfstatat)
263 (unlinkat)
273 (set_robust_list)
281 (epoll_pwait)
284 (eventfd)
290 (eventfd2)
291 (epoll_create1)
302 (prlimit64)
309 (getcpu)
318 (getrandom)

0 comments on commit 9d6d172

Please sign in to comment.