Skip to content

Commit f3d2a01

Browse files
committed
try using pefile py module to inspect dll
1 parent 0ad3812 commit f3d2a01

File tree

5 files changed

+68
-8
lines changed

5 files changed

+68
-8
lines changed

docs/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ dependencies = [
1212
"readthedocs-sphinx-ext",
1313
"absl-py",
1414
"typing-extensions",
15-
"sphinx-reredirects"
15+
"sphinx-reredirects",
16+
"pefile"
1617
]

docs/requirements.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ colorama==0.4.6 ; sys_platform == 'win32' \
111111
--hash=sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44 \
112112
--hash=sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6
113113
# via sphinx
114-
docutils==0.22 \
115-
--hash=sha256:4ed966a0e96a0477d852f7af31bdcb3adc049fbb35ccba358c2ea8a03287615e \
116-
--hash=sha256:ba9d57750e92331ebe7c08a1bbf7a7f8143b86c476acd51528b042216a6aad0f
114+
docutils==0.21.2 \
115+
--hash=sha256:3a6b18732edf182daa3cd12775bbb338cf5691468f91eeeb109deff6ebfa986f \
116+
--hash=sha256:dafca5b9e384f0e419294eb4d2ff9fa826435bf15f15b7bd45723e8ad76811b2
117117
# via
118118
# myst-parser
119119
# sphinx
@@ -232,6 +232,10 @@ packaging==25.0 \
232232
# via
233233
# readthedocs-sphinx-ext
234234
# sphinx
235+
pefile==2024.8.26 \
236+
--hash=sha256:3ff6c5d8b43e8c37bb6e6dd5085658d658a7a0bdcd20b6a07b1fcfc1c4e9d632 \
237+
--hash=sha256:76f8b485dcd3b1bb8166f1128d395fa3d87af26360c2358fb75b80019b957c6f
238+
# via rules-python-docs (docs/pyproject.toml)
235239
pygments==2.19.2 \
236240
--hash=sha256:636cb2477cec7f8952536970bc533bc43743542f70392ae026374600add5b887 \
237241
--hash=sha256:86540386c03d588bb81d44bc3928634ff26449851e99741617ecb9037ee5ec0b

tests/cc/current_py_cc_headers/BUILD.bazel

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
1616
load("@rules_shell//shell:sh_test.bzl", "sh_test")
17+
load("//python:py_test.bzl", "py_test")
1718
load(":current_py_cc_headers_tests.bzl", "current_py_cc_headers_test_suite")
1819

1920
current_py_cc_headers_test_suite(name = "current_py_cc_headers_tests")
@@ -28,9 +29,20 @@ cc_binary(
2829
],
2930
)
3031

31-
sh_test(
32-
name = "abi3_headers_linkage_test",
33-
srcs = ["abi3_headers_linkage_test.sh"],
32+
#sh_test(
33+
# name = "abi3_headers_linkage_test",
34+
# srcs = ["abi3_headers_linkage_test.sh"],
35+
# data = [":bin_abi3"],
36+
# deps = ["@bazel_tools//tools/bash/runfiles"],
37+
#)
38+
39+
py_test(
40+
name = "abi3_headers_linkage_test_py",
41+
srcs = ["abi3_headers_linkage_test.py"],
3442
data = [":bin_abi3"],
35-
deps = ["@bazel_tools//tools/bash/runfiles"],
43+
main = "abi3_headers_linkage_test.py",
44+
deps = [
45+
"//python/runfiles",
46+
"@dev_pip//pefile",
47+
],
3648
)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
import os.path
3+
import sys
4+
import pefile
5+
import unittest
6+
7+
class CheckLinkageTest(unittest.TestCase):
8+
def test_linkage(self):
9+
10+
file_path = rf.Rlocation("_main/tests/cc/current_py_cc_headers/libbin_abi3.dll")
11+
if not file_path:
12+
self.fail("dll not found")
13+
14+
print(f"[*] Analyzing dependencies for: {os.path.basename(file_path)}\n")
15+
16+
try:
17+
# Parse the PE file
18+
pe = pefile.PE(file_path)
19+
20+
if not hasattr(pe, 'DIRECTORY_ENTRY_IMPORT'):
21+
print("[!] No import directory found. The file may not have dependencies or is packed.")
22+
raise Exception("no deps?")
23+
24+
print("Imported DLLs:")
25+
26+
# Iterate over the import directory entries
27+
# Each 'entry' corresponds to one imported DLL
28+
for entry in pe.DIRECTORY_ENTRY_IMPORT:
29+
# entry.dll is a bytes string, so we decode it to utf-8
30+
dll_name = entry.dll.decode('utf-8')
31+
print(f" - {dll_name}")
32+
33+
except pefile.PEFormatError as e:
34+
print(f"Error: Not a valid PE file (DLL/EXE). \nDetails: {e}")
35+
except Exception as e:
36+
print(f"An unexpected error occurred: {e}")
37+
38+
raise Exception("done")
39+
40+
if __name__ == "__main__":
41+
unittest.main()

tests/cc/current_py_cc_headers/abi3_headers_linkage_test.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ lib=$(rlocation _main/tests/cc/current_py_cc_headers/libbin_abi3.so)
2020
dumpbin /nologo /DEPENDENTS "$lib"
2121
dumpbin.exe /nologo /DEPENDENTS "$lib"
2222

23+
ls
24+
2325
exit 1

0 commit comments

Comments
 (0)