xref: /aosp_15_r20/external/harfbuzz_ng/src/check-static-inits.py (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
1#!/usr/bin/env python3
2
3import sys, os, shutil, subprocess, glob, re
4
5builddir = os.getenv ('builddir', os.path.dirname (__file__))
6libs = os.getenv ('libs', '.libs')
7
8objdump = os.getenv ('OBJDUMP', shutil.which ('objdump'))
9if not objdump:
10	print ('check-static-inits.py: \'ldd\' not found; skipping test')
11	sys.exit (77)
12
13if sys.version_info < (3, 5):
14	print ('check-static-inits.py: needs python 3.5 for recursive support in glob')
15	sys.exit (77)
16
17OBJS = glob.glob (os.path.join (builddir, libs, '**', '*hb*.o'), recursive=True)
18if not OBJS:
19	print ('check-static-inits.py: object files not found; skipping test')
20	sys.exit (77)
21
22stat = 0
23tested = 0
24
25for obj in OBJS:
26	result = subprocess.run(objdump.split () + ['-t', obj], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
27
28	if result.returncode:
29		if result.stderr.find (b'not recognized') != -1:
30			# https://github.com/harfbuzz/harfbuzz/issues/3019
31			print ('objdump %s returned "not recognized", skipping' % obj)
32			continue
33		print ('objdump %s returned error:\n%s' % (obj, result.stderr.decode ('utf-8')))
34		stat = 2
35
36	result = result.stdout.decode ('utf-8')
37
38	# Checking that no object file has static initializers
39	for l in re.findall (r'^.*\.[cd]tors.*$', result, re.MULTILINE):
40		if not re.match (r'.*\b0+\b', l):
41			print ('Ouch, %s has static initializers/finalizers' % obj)
42			stat = 1
43
44	# Checking that no object file has lazy static C++ constructors/destructors or other such stuff
45	if ('__cxa_' in result) and ('__ubsan_handle' not in result):
46		print ('Ouch, %s has lazy static C++ constructors/destructors or other such stuff' % obj)
47		stat = 1
48
49	tested += 1
50
51sys.exit (stat if tested else 77)
52