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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
#!/usr/bin/perl
#
# Validate a set of wxi files to determine if they
# list all the DLL dependencies required by any EXE
# or DLL files that contain and that all listed files
# do exist on disk
#
# Copyright 2018-2019 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see
# <http://www.gnu.org/licenses/>.
#
use strict;
use warnings;
use XML::XPath;
use XML::XPath::XMLParser;
# WXI Filename -> group name
my %filegroups;
# Group name -> dict ( array of component groups, array of component hashes )
my %groups;
# Component hash -> file path
my %components;
# DLL file name -> component hash
my %dllcomponents;
# DLL file name -> group name
my %dllgroups;
# DLL files provided by Windows runtime which
# thus don't need to be listed in wxi files
# as dependencies
my $dllbuiltin = "^(" .
"advapi32|" .
"api-ms-.*|" .
"avrt|" .
"bcrypt|" .
"bcryptprimitives|" .
"comctl32|" .
"comdlg32|" .
"crypt32|" .
"dbghelp|" .
"dwrite|" .
"d3d10|" .
"d3d11|" .
"d3d9|" .
"dnsapi|" .
"dsound|" .
"dwmapi|" .
"dxgi|" .
"gdi32|" .
"gdiplus|" .
"hid|" .
"imm32|" .
"iphlpapi|" .
"kernel32|" .
"ksuser|" .
"msimg32|" .
"msvcrt|" .
"mswsock|" .
"ncrypt|" .
"ntdll|" .
"ole32|" .
"oleaut32|" .
"opengl32|" .
"psapi|" .
"setupapi|" .
"shell32|" .
"shlwapi|" .
"user32|" .
"userenv|" .
"usp10|" .
"version|" .
"winmm|" .
"wldap32|" .
"ws2_32|" .
").dll\$";
my $drvbuiltin = "^(" .
"winspool|" .
").drv\$";
my @checkgroups;
my $errors = 0;
# Load all the requested wxi files, doing
# a basic sanity check that all files in
# the manifest exist
foreach my $file (@ARGV) {
push @checkgroups, &load($file);
}
exit 1 if $errors;
# Check all DLL/EXE files to see if they
# reference DLLs which are not mentioned as
# dependencies
foreach my $group (@checkgroups) {
my %allowedcomps;
&expand($group, \%allowedcomps);
&check($group, \%allowedcomps);
}
exit $errors;
sub load {
my $file = shift;
# By convention the .wxi files must be named to match
# the mingw RPM name
my $rpmname = $file;
$rpmname =~ s,^.*/,,;
$rpmname =~ s/.wxi//;
my $rpmname32 = "mingw32-$rpmname";
my $rpmname64 = "mingw64-$rpmname";
# This testing is only going to work if the RPMs are
# actually installed
my $hasrpm32 = (system "rpm -q $rpmname32 >/dev/null 2>&1") == 0;
my $hasrpm64 = (system "rpm -q $rpmname64 >/dev/null 2>&1") == 0;
unless ($hasrpm32) {
$errors = 1;
print "Cannot check $file, $rpmname32 not installed\n";
}
unless ($hasrpm32) {
$errors = 1;
print "Cannot check $file, $rpmname32 not installed\n";
}
# Since we're recursively expanding <require> imports
# we might be called repeatedly for the same file, so
# short circuit if that happens
if (exists $filegroups{$file}) {
return $filegroups{$file};
}
# XML::XPath will automatically process the
# <?require> directives, but that is problematic
# as we want to track the exact source file that
# problems are present in.
#
# Thus we pre-emptively load any includes here
# so we can report errors directly, and setup
# the right file groups
open XML, "$file" or die "cannot read $file: $!";
foreach (<XML>) {
if (/<\?require (\S+)\?>/) {
my $incname = $1;
my $path = $file;
$path =~ s,/[^/]+$,/,;
my $incfile = $path . $incname;
&load($incfile);
}
}
close XML;
my $xp = XML::XPath->new(filename => $file);
my $nodeset;
# Extract all the listed dependencies
$nodeset = eval { $xp->find("//ComponentGroup"); };
if ($@) {
print STDERR "Parsing failed on $file: $@\n";
$errors = 1;
return;
}
my $cg = $nodeset->[0];
my $cgid = $cg->getAttribute("Id");
$groups{$cgid} = {
"file" => $file,
"groups" => [],
"components" => [],
};
foreach my $child ($cg->getChildNodes()) {
next unless defined $child->getName();
if ($child->getName() eq "ComponentRef") {
# These are file local dependencies
push @{$groups{$cgid}->{components}}, $child->getAttribute("Id");
} else {
# These are external file dependencies
push @{$groups{$cgid}->{groups}}, $child->getAttribute("Id");
}
}
# Extract all files listed in this wxi manifest
$nodeset = eval { $xp->find("//Component"); };
if ($@) {
print STDERR "Parsing failed on $file: $@\n";
return;
}
foreach my $node ($nodeset->get_nodelist) {
my $id = $node->getAttribute("Id");
foreach my $child ($node->getChildNodes()) {
next unless defined $child->getName() && $child->getName() eq "File";
my $fname = $child->getAttribute("Source");
my $msifile32 = $fname;
my $msifile64 = $fname;
$msifile32 =~ s,\$\(var.GLIB_ARCH\),win32,;
$msifile64 =~ s,\$\(var.GLIB_ARCH\),win64,;
$msifile32 =~ s,\$\(var.SourceDir\),/usr/i686-w64-mingw32/sys-root/mingw,;
$msifile64 =~ s,\$\(var.SourceDir\),/usr/x86_64-w64-mingw32/sys-root/mingw,;
# Hacks because we don't parse conditionals
# gcc
$msifile32 = undef if $msifile32 =~ /libgcc_s_seh-1\.dll/;
$msifile64 = undef if $msifile64 =~ /libgcc_s_dw2-1.dll/;
# openssl
$msifile32 = undef if $msifile32 && $msifile32 =~ /lib(ssl|crypto)-[^-]+-x64.dll/;
$msifile64 = undef if $msifile64 && $msifile64 =~ /lib(ssl|crypto)-[^-]+.dll/;
# gstreamer1-plugins-base
$msifile32 = undef if $msifile32 && $msifile32 =~ /x86_64/;
$msifile64 = undef if $msifile64 && $msifile64 =~ /i686/;
# Check that the listed files really do exist
if ($hasrpm32 && $msifile32 && ! -e $msifile32) {
print "\n\nErrors in $file\n\n" unless $errors;
$errors = 1;
print " - Missing $msifile32\n";
}
if ($hasrpm64 && $msifile64 && ! -e $msifile64) {
print "\n\nErrors in $file\n\n" unless $errors;
$errors = 1;
print " - Missing $msifile64\n";
}
# Setup mappings for the component and file
$components{$id} = [] unless exists $components{$id};
push @{$components{$id}}, $msifile32 if $msifile32;
push @{$components{$id}}, $msifile64 if $msifile64;
# Setup mappings for the DLLs
if ($fname =~ m,.*/([^/]+.dll)$,) {
my $dllname = lc $1;
$dllcomponents{$dllname} = $id;
$dllgroups{$dllname} = $cgid;
}
}
}
$filegroups{$file} = $cgid;
return $cgid;
}
# Recursively expand a file to find the full
# transitive set of dependencies / references
sub expand {
my $name = shift;
my $allowed = shift;
foreach (@{$groups{$name}->{components}}) {
$allowed->{$_} = 1;
}
foreach my $ref (@{$groups{$name}->{groups}}) {
&expand($ref, $allowed);
}
}
# Check that all DLL/EXE files have their
# dependencies satisfied
sub check {
my $name = shift;
my $allowed = shift;
foreach my $comp (@{$groups{$name}->{components}}) {
if (!exists $components{$comp}) {
$errors = 1;
print "Referenced component $comp does not exist in $name\n";
return;
}
my @files = @{$components{$comp}};
foreach my $file (@files) {
next unless $file =~ /\.(exe|dll)$/;
my $problems = 0;
unless (-f $file) {
print " > $file\n" if ++$problems == 1;
print " - Cannot analyse missing file $file\n";
next;
}
# Get the list of dependancies from objdump. The
# output contains list of stuff we don't care
# about so we're looking for lines like:
#
# DLL Name: libcurl-4.dll
# DLL Name: libdl.dll
# DLL Name: libgcc_s_sjlj-1.dll
# DLL Name: libgnutls-30.dll
# DLL Name: libintl-8.dll
open OBJ, "x86_64-w64-mingw32-objdump -p $file |";
foreach my $info (<OBJ>) {
next unless $info =~ /DLL Name: (\S+)/;
my $dllname = lc $1;
next if $dllname =~ $dllbuiltin;
next if $dllname =~ $drvbuiltin;
unless (exists $dllcomponents{$dllname}) {
print " > $file\n" if ++$problems == 1;
print " - Unknown component for $dllname\n";
next;
}
my $dllcomp = $dllcomponents{$dllname};
next if exists $allowed->{$dllcomp};
my $dllgroup = $dllgroups{$dllname};
print " > $file\n" if ++$problems == 1;
print " - Missing group ref $dllgroup for $dllname\n";
}
$errors = 1 if $problems;
}
}
}
|