Skip to content

Commit

Permalink
[python] Fix {{ ldd }} and add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Wojtek Porczyk <woju@invisiblethingslab.com>
  • Loading branch information
woju authored and dimakuv committed Oct 31, 2023
1 parent 65a2a5e commit f066fab
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions .ci/lib/stage-clean-check.jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ stage('clean-check') {
# python/ subdir does not have makefiles, so no clean
rm -rf /tmp/*.clean-check.clean/python/graminelibos/__pycache__
rm -rf python/graminelibos/__pycache__
rm -rf tests/__pycache__

./scripts/clean-check
'''
Expand Down
11 changes: 11 additions & 0 deletions .ci/lib/stage-test.jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,15 @@ stage('test') {
}
}
}

timeout(time: 15, unit: 'MINUTES') {
try {
sh '''
python3 -m pytest -v --junit-xml tests.xml tests/
'''
} finally {
junit 'tests.xml'
sh 'rm -rf tests.xml'
}
}
}
19 changes: 13 additions & 6 deletions python/graminelibos/gen_jinja_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

from . import _CONFIG_PKGLIBDIR

def ldd(*args):
'''
Args:
binaries for which to generate manifest trusted files list.
'''
def parse_ldd(output):
# Be careful: We have to skip vdso, which doesn't have a corresponding file on the disk (we
# assume that such files have paths starting with '/', seems ldd always prints absolute paths).
# Also, old ldd (from Ubuntu 16.04) prints vdso differently than newer ones:
Expand All @@ -21,14 +17,25 @@ def ldd(*args):
# new:
# linux-vdso.so.1 (0x00007ffd31fee000)
ret = set()
for line in subprocess.check_output(['ldd', *(os.fspath(i) for i in args)]).decode('ascii'):
for line in output.rstrip('\n').split('\n'):
line = line.strip().split()
if len(line) == 1 and line[0].endswith(':'):
# header for when there are multiple binaries given
continue
if line[1] == '=>' and line[2].startswith('/'):
ret.add(line[2])
elif line[0].startswith('/') and line[1].startswith('/'):
ret.add(line[0])
return sorted(ret)

def ldd(*args):
'''
Args:
binaries for which to generate manifest trusted files list.
'''
return parse_ldd(
subprocess.check_output(['ldd', *(os.fspath(i) for i in args)]).decode('ascii'))

def python_get_sys_path(interpreter, include_nonexisting=False):
for path in subprocess.check_output([interpreter, '-c',
'''import sys; print('\\0'.join(path for path in sys.path if path), end='')'''
Expand Down
26 changes: 26 additions & 0 deletions tests/test_ldd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from graminelibos.gen_jinja_env import parse_ldd

def test_parse_ldd():
output = parse_ldd(
# collected with glibc 2.31-0ubuntu9.9 on a helloworld app
'''\
\tlinux-vdso.so.1 (0x00007ffd56342000)
\tlibc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3a87190000)
\t/lib64/ld-linux-x86-64.so.2 (0x00007f3a8738c000)
''')
assert output == ['/lib/x86_64-linux-gnu/libc.so.6']

def test_parse_ldd_multiple_binaries():
output = parse_ldd(
# collected with glibc 2.36-9+deb12u3
'''\
/usr/bin/cat:
\tlinux-vdso.so.1 (0x00007ffe71bfe000)
\tlibc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa5165ff000)
\t/lib64/ld-linux-x86-64.so.2 (0x00007fa5167f8000)
/usr/bin/echo:
\tlinux-vdso.so.1 (0x00007fff8fde5000)
\tlibc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7533ee1000)
\t/lib64/ld-linux-x86-64.so.2 (0x00007f75340da000)
''')
assert output == ['/lib/x86_64-linux-gnu/libc.so.6']

0 comments on commit f066fab

Please sign in to comment.