| File: | bin/extract-schemas |
| Coverage: | 73.6% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | #!/usr/bin/env perl | |||||
| 2 | ||||||
| 3 | 65 65 65 | 110127 47 831 | use strict; | |||
| 4 | 65 65 65 | 70 17 1138 | use warnings; | |||
| 5 | ||||||
| 6 | 65 | 4115068 | $| = 1; # unbuffered stdout so messages appear in execution order | |||
| 7 | ||||||
| 8 | 65 65 65 | 12438 161069 1501 | use Data::Dumper; | |||
| 9 | 65 65 65 | 108 51 1353 | use File::Path qw(make_path); | |||
| 10 | 65 65 65 | 74 31 309 | use File::Spec; | |||
| 11 | 65 65 65 | 17007 220635 96 | use Getopt::Long; | |||
| 12 | 65 65 65 | 12283 23584 1256 | use FindBin; | |||
| 13 | 65 65 65 | 9241 13877 131 | use lib "$FindBin::Bin/../lib"; | |||
| 14 | 65 65 65 | 13572 1246975 2581 | use Pod::Usage; | |||
| 15 | ||||||
| 16 | 65 65 65 | 48367 117 96650 | use App::Test::Generator::SchemaExtractor; | |||
| 17 | ||||||
| 18 - 189 | =head1 NAME extract-schemas - Extract test schemas from Perl modules =head1 SYNOPSIS extract-schemas [options] <module.pm> Options: --output-dir DIR Output directory for schema files (default: schemas/) --strict-pod=off|warn|fatal --function NAME Only extract and test this one routine (useful for debugging a single failure in t/self-fuzz.t) --verbose Show detailed analysis --version Show the version of App::Test::Generator::SchemaExtractor --fuzz Run coverage-guided fuzzing on extracted schemas --fuzz-iters N Iterations per method when fuzzing (default: 100) (no short form, to avoid conflict with --fuzz/-f) --fuzz-all Fuzz all methods, including those with no input schema --corpus-dir DIR Directory to persist fuzz corpora (default: schemas/corpus/) --minimize-corpus After fuzzing, trim each corpus to the minimum subset that still covers all discovered branches (greedy set-cover). Keeps the corpus file small across many CI runs. --help Show this help message --man Show full documentation Examples: extract-schemas lib/MyModule.pm extract-schemas --output-dir my_schemas --verbose lib/MyModule.pm extract-schemas --function greet lib/MyModule.pm extract-schemas --fuzz lib/MyModule.pm extract-schemas --fuzz --function greet lib/MyModule.pm extract-schemas --fuzz --fuzz-iters 300 --corpus-dir t/corpus lib/MyModule.pm extract-schemas --fuzz --fuzz-all lib/MyModule.pm extract-schemas --fuzz --minimize-corpus lib/MyModule.pm =head1 QUICK START Run C<extract-schemas --strict-pod=warn -v --fuzz lib/MyModule.pm> to analyse your module and automatically probe each method with hundreds of fuzzed inputs, looking for crashes caused by inputs that should be valid. Anything suspicious is saved to C<schemas/corpus/>. If genuine bugs are found, run C<fuzz-harness-generator --replay-corpus schemas/corpus/ -o t/fuzz_replay.t> to turn them into regression tests that will fail until you fix the underlying code and pass forever after. Run C<extract-schemas --fuzz> regularly - each run builds on the last, probing deeper into your code each time. Otherwise, for each of the functions in MyModule.pm, C<fuzz-harness-generator -r schemas/function.yml> =head1 OPTIONS =over 4 =item B<--output-dir|-o> I<DIR> Directory to write generated YAML schema files. Defaults to C<schemas/>. =item B<--strict-pod|-s> I<MODE> How to handle disagreements between POD and code analysis. C<0> (off), C<1> (warn), or C<fatal> (croak on disagreement). =item B<--function|-F> I<NAME> Extract schema for only the named function; delete all other generated YAMLs and restrict the fuzz run to that one routine. =item B<--verbose|-v> Print extra diagnostic information during extraction and fuzzing. =item B<--version|-V> Print the C<App::Test::Generator::SchemaExtractor> version and exit. =item B<--fuzz|-f> After extraction, run C<App::Test::Generator::CoverageGuidedFuzzer> against each method using the generated schema. =item B<--fuzz-all> Like C<--fuzz>, but also fuzzes methods that have no extractable schema. =item B<--fuzz-iters> I<N> Number of fuzzing iterations per method (default: 100). =item B<--corpus-dir|-c> I<DIR> Directory for loading and saving the fuzzing corpus between runs. =item B<--minimize-corpus> After fuzzing, run C<minimize_corpus()> to reduce the saved corpus to the smallest subset that still covers every branch discovered in this run. =item B<--help|-h> Print a short usage summary and exit. =item B<--man|-m> Print the full manual page and exit. =back =head1 DESCRIPTION This tool analyzes a Perl module and generates YAML schema files for each method, suitable for use with L<App::Test::Generator> using the C<fuzz-harness-generator> program which will create the C<.t> file to run through C<prove>. The extractor uses three sources of information: =over 4 =item 1. POD Documentation Parses parameter descriptions from POD to extract types and constraints. =item 2. Code Analysis Analyzes validation patterns in the code (ref checks, length checks, etc.) =item 3. Method Signatures Extracts parameter names from method signatures. =back The tool assigns a confidence level (high/medium/low) to each schema based on how much information it could infer. =head1 FUZZING When C<--fuzz> is specified, the tool will additionally run C<App::Test::Generator::CoverageGuidedFuzzer> against each method after schema extraction. By default all methods with at least one known input parameter are fuzzed, regardless of confidence level. Use C<--fuzz-all> to also attempt fuzzing methods with no input schema (these will use purely random generation). The fuzzer will: =over 4 =item * Load and C<require> the target module at runtime =item * Run coverage-guided fuzzing using the extracted schema as input spec =item * Report any crashes or unexpected errors found =item * Persist a corpus to C<--corpus-dir> for incremental improvement across runs =item * Optionally minimise the corpus with C<--minimize-corpus> to keep file sizes bounded =back Corpus files are named C<< <corpus-dir>/<method>.json >> and are automatically loaded on subsequent runs, so each run builds on the last. Without C<--minimize-corpus> the corpus grows by several entries per run; with it, only the entries that provide unique branch coverage are retained (greedy set-cover), plus a deduplicated set of entries from prior runs that cannot be re-evaluated. Bug-triggering inputs are always kept regardless of coverage contribution. =cut | |||||
| 190 | ||||||
| 191 | # --------------------------------------------------------------------------- | |||||
| 192 | # Option parsing | |||||
| 193 | # --------------------------------------------------------------------------- | |||||
| 194 | ||||||
| 195 | 65 | 144 | my %cli_opts = ( | |||
| 196 | help => 0, | |||||
| 197 | man => 0, | |||||
| 198 | ); | |||||
| 199 | ||||||
| 200 | 65 | 167 | my %extractor_opts = ( | |||
| 201 | output_dir => 'schemas', | |||||
| 202 | strict_pod => 'warn', | |||||
| 203 | verbose => 0, | |||||
| 204 | version => 0, | |||||
| 205 | ); | |||||
| 206 | ||||||
| 207 | 65 | 58 | my $fuzz = 0; | |||
| 208 | 65 | 62 | my $fuzz_all = 0; | |||
| 209 | 65 | 50 | my $fuzz_iters = 100; | |||
| 210 | 65 | 53 | my $minimize_corpus = 0; | |||
| 211 | 65 | 74 | my $corpus_dir; # default set after output_dir is known | |||
| 212 | my $function_filter; # when set, restrict to this one routine | |||||
| 213 | ||||||
| 214 | 65 | 174 | Getopt::Long::Configure('bundling'); | |||
| 215 | ||||||
| 216 | GetOptions( | |||||
| 217 | 'output-dir|o=s' => \$extractor_opts{output_dir}, | |||||
| 218 | 'strict-pod|s=s' => \$extractor_opts{strict_pod}, | |||||
| 219 | 'function|F=s' => \$function_filter, | |||||
| 220 | 'verbose|v' => \$extractor_opts{verbose}, | |||||
| 221 | 'version|V' => \$extractor_opts{version}, | |||||
| 222 | 'fuzz|f' => \$fuzz, | |||||
| 223 | 'fuzz-all' => \$fuzz_all, | |||||
| 224 | 'fuzz-iters=i' => \$fuzz_iters, | |||||
| 225 | 'corpus-dir|c=s' => \$corpus_dir, | |||||
| 226 | 'minimize-corpus' => \$minimize_corpus, | |||||
| 227 | 'help|h' => \$cli_opts{help}, | |||||
| 228 | 'man|m' => \$cli_opts{man}, | |||||
| 229 | 65 | 1378 | ) or pod2usage(2); | |||
| 230 | ||||||
| 231 | 65 | 30930 | pod2usage(-exitval => 0, -verbose => 1) if $cli_opts{help}; | |||
| 232 | 64 | 100 | pod2usage(-exitval => 0, -verbose => 2) if $cli_opts{man}; | |||
| 233 | ||||||
| 234 | 64 | 95 | if($extractor_opts{version}) { | |||
| 235 | 0 | 0 | print $App::Test::Generator::SchemaExtractor::VERSION, "\n"; | |||
| 236 | 0 | 0 | exit 0; | |||
| 237 | } | |||||
| 238 | ||||||
| 239 | 64 | 214 | if ($extractor_opts{strict_pod} !~ /^(off|warn|fatal)$/) { | |||
| 240 | 1 | 0 | die "Invalid --strict-pod value '$extractor_opts{strict_pod}'. Expected off, warn, or fatal"; | |||
| 241 | } | |||||
| 242 | ||||||
| 243 | 63 | 105 | my $input_file = shift @ARGV or pod2usage('Error: No input file specified'); | |||
| 244 | 62 | 534 | die "Error: File not found: $input_file" unless -f $input_file; | |||
| 245 | ||||||
| 246 | # Default corpus dir sits under the output dir | |||||
| 247 | 62 | 576 | $corpus_dir //= File::Spec->catdir($extractor_opts{output_dir}, 'corpus'); | |||
| 248 | ||||||
| 249 | # --------------------------------------------------------------------------- | |||||
| 250 | # Schema extraction | |||||
| 251 | # --------------------------------------------------------------------------- | |||||
| 252 | ||||||
| 253 | 62 | 2165 | print "Extracting schemas from: $input_file\n"; | |||
| 254 | 62 | 216 | print "Output directory: $extractor_opts{output_dir}\n\n"; | |||
| 255 | ||||||
| 256 | 62 | 906 | make_path($extractor_opts{output_dir}) unless -d $extractor_opts{output_dir}; | |||
| 257 | ||||||
| 258 | 62 | 301 | my $extractor = App::Test::Generator::SchemaExtractor->new( | |||
| 259 | input_file => $input_file, | |||||
| 260 | %extractor_opts, | |||||
| 261 | ); | |||||
| 262 | ||||||
| 263 | 62 | 124 | my $schemas = $extractor->extract_all(); | |||
| 264 | ||||||
| 265 | # Downstream all-fuzz runs otherwise just see a module stop being fuzzed. | |||||
| 266 | 62 67 | 109 292 | if(grep { ref($_) eq 'HASH' && ($_->{skip} // '') =~ /^multi-package file/ } values %$schemas) { | |||
| 267 | 0 | 0 | print STDERR "Note: $input_file declares more than one package; its schemas are marked skip and will not be fuzzed\n"; | |||
| 268 | } | |||||
| 269 | ||||||
| 270 | # --------------------------------------------------------------------------- | |||||
| 271 | # Optional: restrict to a single routine | |||||
| 272 | # --------------------------------------------------------------------------- | |||||
| 273 | ||||||
| 274 | 62 | 75 | if (defined $function_filter) { | |||
| 275 | 0 | 0 | unless (exists $schemas->{$function_filter}) { | |||
| 276 | 0 | 0 | print STDERR "Error: routine '$function_filter' not found in $input_file\n"; | |||
| 277 | 0 | 0 | print STDERR "Available routines: ", join(', ', sort keys %$schemas), "\n"; | |||
| 278 | 0 | 0 | exit 1; | |||
| 279 | } | |||||
| 280 | ||||||
| 281 | # Remove every other schema and its on-disk YAML file so that callers | |||||
| 282 | # (e.g. t/self-fuzz.t) see exactly one .yml in the output directory. | |||||
| 283 | 0 | 0 | for my $name (keys %$schemas) { | |||
| 284 | 0 | 0 | next if $name eq $function_filter; | |||
| 285 | 0 | 0 | delete $schemas->{$name}; | |||
| 286 | 0 | 0 | my $yml = File::Spec->catfile($extractor_opts{output_dir}, "$name.yml"); | |||
| 287 | 0 | 0 | unlink $yml if -f $yml; | |||
| 288 | } | |||||
| 289 | ||||||
| 290 | 0 | 0 | print "Restricted to routine: $function_filter\n\n"; | |||
| 291 | } | |||||
| 292 | ||||||
| 293 | # --------------------------------------------------------------------------- | |||||
| 294 | # Optional: coverage-guided fuzzing | |||||
| 295 | # --------------------------------------------------------------------------- | |||||
| 296 | ||||||
| 297 | 62 | 51 | my %fuzz_results; # method_name => report hashref | |||
| 298 | ||||||
| 299 | 62 | 69 | if ($fuzz) { | |||
| 300 | 3 | 754 | require App::Test::Generator::CoverageGuidedFuzzer; | |||
| 301 | 3 | 175 | make_path($corpus_dir) unless -d $corpus_dir; | |||
| 302 | ||||||
| 303 | # Load the target module once so all methods are callable | |||||
| 304 | 3 | 4 | my $package = _load_target_module($input_file, $schemas); | |||
| 305 | ||||||
| 306 | # Try to build a default instance for object method calls. | |||||
| 307 | # Most OO modules need a $self as the first argument. | |||||
| 308 | # We try new() with no args, then new({}), then give up and fuzz as functions. | |||||
| 309 | 3 | 5 | my $instance = _try_construct($package); | |||
| 310 | 3 | 5 | if ($instance) { | |||
| 311 | 0 | 0 | print "Constructed $package instance for method calls.\n"; | |||
| 312 | } else { | |||||
| 313 | 3 | 23 | print "Could not construct $package instance; fuzzing as functions.\n"; | |||
| 314 | } | |||||
| 315 | ||||||
| 316 | 3 | 9 | print "Fuzzing with $fuzz_iters iterations per method", | |||
| 317 | ($fuzz_all ? ' (all methods)' : ' (methods with known inputs)'), | |||||
| 318 | "...\n\n"; | |||||
| 319 | ||||||
| 320 | 3 | 5 | foreach my $method (sort keys %$schemas) { | |||
| 321 | 3 | 3 | my $schema = $schemas->{$method}; | |||
| 322 | 3 | 4 | my $iconf = $schema->{_confidence}{input}{level} // 'low'; | |||
| 323 | ||||||
| 324 | 3 | 4 | unless ($fuzz_all) { | |||
| 325 | # Skip methods with no input schema at all â there is nothing to fuzz | |||||
| 326 | 3 0 | 6 0 | next if $iconf eq 'none' && !%{ $schema->{input} // {} }; | |||
| 327 | } | |||||
| 328 | ||||||
| 329 | 3 | 6 | my $sub_ref = $package->can($method); | |||
| 330 | 3 | 4 | unless ($sub_ref) { | |||
| 331 | 0 | 0 | warn " Skipping $method: not callable in $package\n"; | |||
| 332 | 0 | 0 | next; | |||
| 333 | } | |||||
| 334 | ||||||
| 335 | # Skip constructors and AUTOLOAD â not suitable for direct fuzzing | |||||
| 336 | 3 | 7 | if ($method =~ /^(new|AUTOLOAD|DESTROY|import)$/) { | |||
| 337 | print " Skipping $method (constructor/special method)\n" | |||||
| 338 | 0 | 0 | if $extractor_opts{verbose}; | |||
| 339 | 0 | 0 | next; | |||
| 340 | } | |||||
| 341 | ||||||
| 342 | 3 | 18 | my $corpus_file = File::Spec->catfile($corpus_dir, "$method.json"); | |||
| 343 | ||||||
| 344 | 3 | 9 | print " Fuzzing $method ($iconf confidence)... "; | |||
| 345 | ||||||
| 346 | 3 | 8 | my $fuzzer = App::Test::Generator::CoverageGuidedFuzzer->new( | |||
| 347 | schema => $schema, | |||||
| 348 | target_sub => $sub_ref, | |||||
| 349 | instance => $instance, | |||||
| 350 | iterations => $fuzz_iters, | |||||
| 351 | ); | |||||
| 352 | ||||||
| 353 | 3 | 17 | $fuzzer->load_corpus($corpus_file) if -f $corpus_file; | |||
| 354 | ||||||
| 355 | 3 | 4 | my $report = $fuzzer->run(); | |||
| 356 | ||||||
| 357 | 3 | 3 | if ($minimize_corpus) { | |||
| 358 | 1 | 2 | my $stats = $fuzzer->minimize_corpus(); | |||
| 359 | printf "%d bugs, %d branches covered, corpus %d->%d entries\n", | |||||
| 360 | $report->{bugs_found}, | |||||
| 361 | $report->{branches_covered}, | |||||
| 362 | $stats->{before}, | |||||
| 363 | 1 | 15 | $stats->{after}; | |||
| 364 | } | |||||
| 365 | ||||||
| 366 | 3 | 8 | $fuzzer->save_corpus($corpus_file); | |||
| 367 | ||||||
| 368 | 3 | 4 | $fuzz_results{$method} = $report; | |||
| 369 | ||||||
| 370 | printf "%d bugs, %d branches covered\n", | |||||
| 371 | $report->{bugs_found}, | |||||
| 372 | $report->{branches_covered} | |||||
| 373 | 3 | 393 | unless $minimize_corpus; | |||
| 374 | } | |||||
| 375 | ||||||
| 376 | 3 | 13 | print "\n"; | |||
| 377 | } | |||||
| 378 | ||||||
| 379 | # --------------------------------------------------------------------------- | |||||
| 380 | # Summary report | |||||
| 381 | # --------------------------------------------------------------------------- | |||||
| 382 | ||||||
| 383 | 62 | 329 | print '=' x 70, "\n", | |||
| 384 | "EXTRACTION SUMMARY\n", | |||||
| 385 | '=' x 70, "\n\n"; | |||||
| 386 | ||||||
| 387 | 62 | 133 | my %input_confidence_counts = (high => 0, medium => 0, low => 0, none => 0); | |||
| 388 | 62 | 113 | my %output_confidence_counts = (high => 0, medium => 0, low => 0, none => 0); | |||
| 389 | ||||||
| 390 | 62 | 96 | foreach my $method (sort keys %$schemas) { | |||
| 391 | 67 | 62 | my $schema = $schemas->{$method}; | |||
| 392 | 67 | 114 | my $iconf = $schema->{_confidence}{input}{level} // 'low'; | |||
| 393 | 67 | 81 | my $oconf = $schema->{_confidence}{output}{level} // 'low'; | |||
| 394 | 67 | 74 | $input_confidence_counts{$iconf}++; | |||
| 395 | 67 | 51 | $output_confidence_counts{$oconf}++; | |||
| 396 | ||||||
| 397 | 67 83 67 | 63 95 98 | my $param_count = scalar grep { $_ !~ /^_/ } keys %{ $schema->{input} }; | |||
| 398 | ||||||
| 399 | 67 | 58 | my $fuzz_col = ''; | |||
| 400 | 67 | 87 | if (exists $fuzz_results{$method}) { | |||
| 401 | 3 | 1 | my $r = $fuzz_results{$method}; | |||
| 402 | $fuzz_col = $r->{bugs_found} | |||||
| 403 | ? sprintf(' BUGS: %d', $r->{bugs_found}) | |||||
| 404 | 3 | 6 | : ' fuzz: ok'; | |||
| 405 | } | |||||
| 406 | ||||||
| 407 | 67 | 324 | printf "%-30s %d params [%s input confidence] [%s output confidence]%s\n", | |||
| 408 | $method, $param_count, uc($iconf), uc($oconf), $fuzz_col; | |||||
| 409 | } | |||||
| 410 | ||||||
| 411 | 62 | 128 | print "\n"; | |||
| 412 | 62 | 334 | print 'Total methods: ', (scalar keys %$schemas), "\n"; | |||
| 413 | 62 | 123 | print " Input:\n"; | |||
| 414 | 62 | 151 | print " High confidence: $input_confidence_counts{high}\n"; | |||
| 415 | 62 | 141 | print " Medium confidence: $input_confidence_counts{medium}\n"; | |||
| 416 | 62 | 141 | print " Low confidence: $input_confidence_counts{low}\n"; | |||
| 417 | 62 | 124 | print " Output:\n"; | |||
| 418 | 62 | 140 | print " High confidence: $output_confidence_counts{high}\n"; | |||
| 419 | 62 | 139 | print " Medium confidence: $output_confidence_counts{medium}\n"; | |||
| 420 | 62 | 144 | print " Low confidence: $output_confidence_counts{low}\n"; | |||
| 421 | 62 | 121 | print "\n"; | |||
| 422 | ||||||
| 423 | 62 | 152 | if ($input_confidence_counts{low} > 0 || $input_confidence_counts{medium} > 0) { | |||
| 424 | 10 | 27 | print "RECOMMENDATION:\n", | |||
| 425 | "Review the generated schemas in $extractor_opts{output_dir}/\n", | |||||
| 426 | "Focus on methods with medium/low confidence ratings.\n\n"; | |||||
| 427 | } | |||||
| 428 | ||||||
| 429 | # Fuzz bug detail | |||||
| 430 | 62 | 69 | if (%fuzz_results) { | |||
| 431 | 3 | 3 | my $total_bugs = 0; | |||
| 432 | 3 | 6 | $total_bugs += $_->{bugs_found} for values %fuzz_results; | |||
| 433 | ||||||
| 434 | 3 | 4 | if ($total_bugs) { | |||
| 435 | 0 | 0 | print '=' x 70, "\n", | |||
| 436 | "FUZZING BUGS FOUND ($total_bugs total)\n", | |||||
| 437 | '=' x 70, "\n\n"; | |||||
| 438 | ||||||
| 439 | 0 | 0 | foreach my $method (sort keys %fuzz_results) { | |||
| 440 | 0 | 0 | my $r = $fuzz_results{$method}; | |||
| 441 | 0 | 0 | next unless $r->{bugs_found}; | |||
| 442 | 0 | 0 | print " $method:\n"; | |||
| 443 | 0 0 | 0 0 | for my $i (0 .. $#{ $r->{bugs} }) { | |||
| 444 | 0 | 0 | my $bug = $r->{bugs}[$i]; | |||
| 445 | 0 | 0 | my $inp = defined($bug->{input}) ? qq("$bug->{input}") : 'undef'; | |||
| 446 | printf " Bug %d: input=%-30s error=%s\n", | |||||
| 447 | 0 | 0 | $i + 1, $inp, $bug->{error}; | |||
| 448 | } | |||||
| 449 | 0 | 0 | print "\n"; | |||
| 450 | } | |||||
| 451 | 0 | 0 | print "Corpora saved to: $corpus_dir/\n\n"; | |||
| 452 | } else { | |||||
| 453 | 3 | 9 | print "Fuzzing complete: no bugs found across ", | |||
| 454 | scalar(keys %fuzz_results), " methods.\n\n"; | |||||
| 455 | } | |||||
| 456 | } | |||||
| 457 | ||||||
| 458 | 62 | 75 | if ($extractor_opts{verbose}) { | |||
| 459 | 1 | 2 | print "Schemas:\n\t", Dumper($schemas); | |||
| 460 | } | |||||
| 461 | ||||||
| 462 | 62 | 986 | print "Schema files written to: $extractor_opts{output_dir}/\n"; | |||
| 463 | ||||||
| 464 | # --------------------------------------------------------------------------- | |||||
| 465 | # Helper: load the target module so methods become callable | |||||
| 466 | # --------------------------------------------------------------------------- | |||||
| 467 | ||||||
| 468 | sub _load_target_module { | |||||
| 469 | 3 | 4 | my ($input_file, $schemas) = @_; | |||
| 470 | ||||||
| 471 | # Derive the package name from the first schema entry that has 'module' set | |||||
| 472 | 3 | 3 | my ($package) = map { $schemas->{$_}{module} } | |||
| 473 | 3 3 | 4 6 | grep { $schemas->{$_}{module} } | |||
| 474 | keys %$schemas; | |||||
| 475 | ||||||
| 476 | 3 | 6 | die 'Could not determine package name from extracted schemas' unless $package; | |||
| 477 | ||||||
| 478 | # Reject anything that is not a syntactically valid Perl package name | |||||
| 479 | # before it is used to build a require path â guards against this | |||||
| 480 | # becoming a code-injection vector if $package is ever sourced from | |||||
| 481 | # something less constrained than a PPI-parsed 'package' statement. | |||||
| 482 | 3 | 6 | die "Invalid package name: $package" | |||
| 483 | unless $package =~ /^[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/; | |||||
| 484 | ||||||
| 485 | # Add the module's containing lib dir to @INC | |||||
| 486 | # Walks up from the file looking for a 'lib' directory | |||||
| 487 | 3 | 32 | my $abs = File::Spec->rel2abs($input_file); | |||
| 488 | 3 | 36 | my ($volume, $directory) = File::Spec->splitpath($abs); | |||
| 489 | 3 | 12 | my @dirs = File::Spec->splitdir($directory); | |||
| 490 | ||||||
| 491 | 3 | 3 | while (@dirs) { | |||
| 492 | # catpath() (not catdir()) keeps $volume attached, so this still | |||||
| 493 | # resolves on Windows when the temp dir and the checkout live on | |||||
| 494 | # different drive letters (a bare catdir() result is interpreted | |||||
| 495 | # as relative to the *current* drive, not $volume's). | |||||
| 496 | 9 | 32 | my $candidate = File::Spec->catpath($volume, File::Spec->catdir(@dirs, 'lib'), ''); | |||
| 497 | 9 | 27 | if (-d $candidate) { | |||
| 498 | 3 | 9 | lib->import($candidate); | |||
| 499 | 3 | 109 | last; | |||
| 500 | } | |||||
| 501 | 6 | 6 | pop @dirs; | |||
| 502 | } | |||||
| 503 | ||||||
| 504 | # require() the module by file path rather than via string eval, so | |||||
| 505 | # $package is never compiled as Perl source. | |||||
| 506 | 3 | 4 | (my $module_file = $package) =~ s{::}{/}g; | |||
| 507 | 3 3 | 2 521 | eval { require "$module_file.pm" } | |||
| 508 | or die "Could not load $package for fuzzing: $@"; | |||||
| 509 | ||||||
| 510 | 3 | 3 | return $package; | |||
| 511 | } | |||||
| 512 | ||||||
| 513 | # Try to construct a default instance of the target package for method calls. | |||||
| 514 | # Attempts new() with progressively more forgiving argument lists. | |||||
| 515 | # Returns the instance on success, undef if nothing works. | |||||
| 516 | sub _try_construct { | |||||
| 517 | 3 | 3 | my ($package) = @_; | |||
| 518 | ||||||
| 519 | 3 | 5 | for my $args ([], [{}], [undef]) { | |||
| 520 | 9 9 | 6 52 | my $obj = eval { $package->new(@$args) }; | |||
| 521 | 9 | 9 | next if $@; | |||
| 522 | 0 | 0 | next unless defined $obj && ref $obj; | |||
| 523 | 0 | 0 | return $obj; | |||
| 524 | } | |||||
| 525 | ||||||
| 526 | 3 | 3 | return undef; | |||
| 527 | } | |||||
| 528 | ||||||