summaryrefslogtreecommitdiff
path: root/.github/scripts/audit-staged-dll-imports-windows.sh
blob: 464faf7e91cb8c784dd6db9b7a095a823ec601b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env bash
set -u

stage_bin="${1:?usage: audit-staged-dll-imports-windows.sh <stage-bin> <exe> [exe...]}"
shift

if [ ! -d "$stage_bin" ]; then
  echo "::error::staged bin directory not found: $stage_bin"
  exit 1
fi

if [ "$#" -eq 0 ]; then
  echo "::error::no executables supplied for DLL import audit"
  exit 1
fi

fail=0

staged_lower=$(
  for dll in "$stage_bin"/*.dll "$stage_bin"/*.DLL; do
    [ -e "$dll" ] || continue
    basename "$dll" | tr '[:upper:]' '[:lower:]'
  done | sort -u
)

echo "staged DLLs in $stage_bin:"
if [ -n "$staged_lower" ]; then
  echo "$staged_lower"
else
  echo "(none)"
fi

for exe in "$@"; do
  if [ ! -f "$exe" ]; then
    echo "::error::executable not found for DLL import audit: $exe"
    fail=1
    continue
  fi

  echo "::group::DLL imports for $exe"
  if ! import_metadata=$(objdump -p "$exe"); then
    echo "::error::Could not inspect DLL imports for $exe"
    echo "::endgroup::"
    fail=1
    continue
  fi
  imports=$(
    printf '%s\n' "$import_metadata" |
      awk '/DLL Name:/ {print $3}' |
      sort -u
  )

  if [ -n "$imports" ]; then
    echo "$imports"
  else
    echo "(none)"
  fi

  if [ "$(basename "$exe" | tr '[:upper:]' '[:lower:]')" = jt9.exe ]; then
    qt_imports=$(printf '%s\n' "$imports" | awk 'tolower($0) ~ /^qt.*\.dll$/')
    if [ -n "$qt_imports" ]; then
      echo "::error::$exe must not depend on Qt: $qt_imports"
      fail=1
    fi
  fi

  win7_blocked_imports=$(
    printf '%s\n' "$import_metadata" |
      awk '
        /DLL Name:/ { dll=tolower($3) }
        /api-ms-win-core-synch-l1-2-0\.dll/ { print "api-ms-win-core-synch-l1-2-0.dll" }
        /WaitOnAddress/ { print dll "::WaitOnAddress" }
        /WakeByAddressSingle/ { print dll "::WakeByAddressSingle" }
      ' |
      sort -u
  )

  if [ -n "$win7_blocked_imports" ]; then
    echo "::error::$exe imports Windows 8+ synchronization APIs that block Windows 7 compatibility:"
    echo "$win7_blocked_imports"
    fail=1
  fi

  high_risk=$(
    echo "$imports" |
      awk '{ lower=tolower($0); if (lower ~ /^(libgomp|libgfortran|libgcc_s|libquadmath|libportaudio|libhamlib|libfftw|qt5.*\.dll)/) print lower }' |
      sort -u
  )

  for dll in $high_risk; do
    if ! printf '%s\n' "$staged_lower" | awk -v wanted="$dll" '$0 == wanted { found=1 } END { exit found ? 0 : 1 }'; then
      echo "::error::$exe imports $dll, but $dll is missing from $stage_bin"
      fail=1
    fi
  done
  echo "::endgroup::"
done

if [ "$fail" -ne 0 ]; then
  echo "::group::staged bin contents"
  ls -la "$stage_bin"
  echo "::endgroup::"
fi

exit "$fail"