| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- G E N E R A T E _ M I N I M A L _ R E P R O D U C E R -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2024-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 11 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 12 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 14 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 15 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 16 |
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- |
| 17 |
-- for more details. You should have received a copy of the GNU General -- |
| 18 |
-- Public License distributed with GNAT; see file COPYING3. If not, go to -- |
| 19 |
-- http://www.gnu.org/licenses for a complete copy of the license. -- |
| 20 |
-- -- |
| 21 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 22 |
-- Extensive contributions were provided by AdaCore. -- |
| 23 |
-- -- |
| 24 |
------------------------------------------------------------------------------ |
| 25 |
|
| 26 |
with Fmap; |
| 27 |
with Fname.UF; |
| 28 |
with Lib; |
| 29 |
with Namet; use Namet; |
| 30 |
with Osint; use Osint; |
| 31 |
with Output; use Output; |
| 32 |
with Sinfo.Nodes; |
| 33 |
with System.CRTL; |
| 34 |
with System.OS_Lib; use System.OS_Lib; |
| 35 |
with Types; use Types; |
| 36 |
|
| 37 |
procedure Generate_Minimal_Reproducer is |
| 38 |
Reproducer_Generation_Failed : exception; |
| 39 |
|
| 40 |
function Create_Reproducer_Directory return String; |
| 41 |
-- Create a directory that will be used to run adareducer, and will |
| 42 |
-- eventually contain the reduced set of sources to be collected by the |
| 43 |
-- user. The name of the directory makes its purpose clear, and it has a |
| 44 |
-- numeric suffix to avoid clashes with other compiler invocations that |
| 45 |
-- might have generated reproducers already. |
| 46 |
|
| 47 |
--------------------------------- |
| 48 |
-- Create_Reproducer_Directory -- |
| 49 |
--------------------------------- |
| 50 |
|
| 51 |
function Create_Reproducer_Directory return String is |
| 52 |
Max_Id : constant Positive := 1000; |
| 53 |
|
| 54 |
Prefix : constant String := "reduce-crash-reproducer"; |
| 55 |
|
| 56 |
Result : System.CRTL.int; |
| 57 |
begin |
| 58 |
for Id in 1 .. Max_Id loop |
| 59 |
declare |
| 60 |
Candidate_Path : String := Prefix & Positive'Image (Id); |
| 61 |
begin |
| 62 |
Candidate_Path (Prefix'Length + 1) := '-'; |
| 63 |
|
| 64 |
Result := System.CRTL.mkdir (Candidate_Path & ASCII.NUL); |
| 65 |
|
| 66 |
-- If mkdir fails, we assume that it's because the directory |
| 67 |
-- already exists. We should check for EEXIST instead??? |
| 68 |
if Result = 0 then |
| 69 |
return Candidate_Path; |
| 70 |
end if; |
| 71 |
end; |
| 72 |
end loop; |
| 73 |
|
| 74 |
Write_Line ("failed to create reproducer directory"); |
| 75 |
raise Reproducer_Generation_Failed; |
| 76 |
end Create_Reproducer_Directory; |
| 77 |
|
| 78 |
Dirname : constant String := Create_Reproducer_Directory; |
| 79 |
|
| 80 |
Gpr_File_Path : constant String := |
| 81 |
Dirname & Directory_Separator & "reduce_crash_reproducer.gpr"; |
| 82 |
|
| 83 |
Src_Dir_Path : constant String := Dirname & Directory_Separator & "src"; |
| 84 |
|
| 85 |
Oracle_Path : constant String := |
| 86 |
Dirname & Directory_Separator & Executable_Name ("oracle"); |
| 87 |
|
| 88 |
Result : Integer; |
| 89 |
begin |
| 90 |
Create_Semantic_Closure_Project : |
| 91 |
declare |
| 92 |
Gpr_File : File_Descriptor; |
| 93 |
|
| 94 |
B : constant Saved_Output_Buffer := Save_Output_Buffer; |
| 95 |
begin |
| 96 |
Gpr_File := Create_File (Gpr_File_Path, Text); |
| 97 |
if Gpr_File = Invalid_FD then |
| 98 |
Write_Line ("failed to create GPR file"); |
| 99 |
raise Reproducer_Generation_Failed; |
| 100 |
end if; |
| 101 |
|
| 102 |
Push_Output; |
| 103 |
Set_Output (Gpr_File); |
| 104 |
|
| 105 |
Write_Line ("project Reduce_Crash_Reproducer is"); |
| 106 |
Write_Line (" for Source_Dirs use (""src"");"); |
| 107 |
Write_Line ("end Reduce_Crash_Reproducer;"); |
| 108 |
|
| 109 |
Close (Gpr_File); |
| 110 |
Pop_Output; |
| 111 |
Restore_Output_Buffer (B); |
| 112 |
|
| 113 |
Result := System.CRTL.mkdir (Src_Dir_Path & ASCII.NUL); |
| 114 |
|
| 115 |
if Result /= 0 then |
| 116 |
Write_Line ("failed to create reproducer directory"); |
| 117 |
raise Reproducer_Generation_Failed; |
| 118 |
end if; |
| 119 |
|
| 120 |
for J in Main_Unit .. Lib.Last_Unit loop |
| 121 |
declare |
| 122 |
Path : File_Name_Type := |
| 123 |
Fmap.Mapped_Path_Name (Lib.Unit_File_Name (J)); |
| 124 |
|
| 125 |
Default_File_Name : constant String := |
| 126 |
Fname.UF.Get_Default_File_Name (Lib.Unit_Name (J)); |
| 127 |
|
| 128 |
File_Copy_Path : constant String := |
| 129 |
Src_Dir_Path & Directory_Separator & Default_File_Name; |
| 130 |
|
| 131 |
-- We may have synthesized units for child subprograms without |
| 132 |
-- spec files. We need to filter out those units because we would |
| 133 |
-- create bogus spec files that break compilation if we didn't. |
| 134 |
Is_Synthetic_Subprogram_Spec : constant Boolean := |
| 135 |
not Sinfo.Nodes.Comes_From_Source (Lib.Cunit (J)); |
| 136 |
begin |
| 137 |
if not Lib.Is_Internal_Unit (J) |
| 138 |
and then not Is_Synthetic_Subprogram_Spec |
| 139 |
then |
| 140 |
-- Mapped_Path_Name might have returned No_File. This has been |
| 141 |
-- observed for files with a Source_File_Name pragma. |
| 142 |
if Path = No_File then |
| 143 |
Path := Find_File (Lib.Unit_File_Name (J), Osint.Source); |
| 144 |
pragma Assert (Path /= No_File); |
| 145 |
end if; |
| 146 |
|
| 147 |
declare |
| 148 |
File_Path : constant String := Get_Name_String (Path); |
| 149 |
Success : Boolean; |
| 150 |
begin |
| 151 |
System.OS_Lib.Copy_File |
| 152 |
(File_Path, File_Copy_Path, Success, Overwrite); |
| 153 |
|
| 154 |
pragma Assert (Success); |
| 155 |
end; |
| 156 |
end if; |
| 157 |
end; |
| 158 |
end loop; |
| 159 |
end Create_Semantic_Closure_Project; |
| 160 |
|
| 161 |
Create_Oracle : |
| 162 |
declare |
| 163 |
Gnatmake_Path : String_Access := Locate_Exec_On_Path ("gnatmake"); |
| 164 |
|
| 165 |
Oracle_Dir_Path : constant String := |
| 166 |
Dirname & Directory_Separator & "oracle-src"; |
| 167 |
|
| 168 |
Source_File_Path : constant String := |
| 169 |
Oracle_Dir_Path & Directory_Separator & "oracle.adb"; |
| 170 |
|
| 171 |
Source_File : File_Descriptor; |
| 172 |
|
| 173 |
Result : System.CRTL.int; |
| 174 |
begin |
| 175 |
if Gnatmake_Path = null then |
| 176 |
Write_Line ("-gnatd_m was specified but gnatmake is not available"); |
| 177 |
raise Reproducer_Generation_Failed; |
| 178 |
end if; |
| 179 |
|
| 180 |
Result := System.CRTL.mkdir (Oracle_Dir_Path & ASCII.NUL); |
| 181 |
|
| 182 |
if Result /= 0 then |
| 183 |
Write_Line ("failed to create directory"); |
| 184 |
raise Reproducer_Generation_Failed; |
| 185 |
end if; |
| 186 |
|
| 187 |
Source_File := Create_File (Source_File_Path, Text); |
| 188 |
if Source_File = Invalid_FD then |
| 189 |
Write_Line ("failed to create oracle source file"); |
| 190 |
raise Reproducer_Generation_Failed; |
| 191 |
end if; |
| 192 |
|
| 193 |
Write_Oracle_Code : |
| 194 |
declare |
| 195 |
Old_Main_Path : constant String := |
| 196 |
Get_Name_String |
| 197 |
(Fmap.Mapped_Path_Name (Lib.Unit_File_Name (Main_Unit))); |
| 198 |
|
| 199 |
Default_Main_Name : constant String := |
| 200 |
Fname.UF.Get_Default_File_Name (Lib.Unit_Name (Main_Unit)); |
| 201 |
|
| 202 |
New_Main_Path : constant String := |
| 203 |
Src_Dir_Path & Directory_Separator & Default_Main_Name; |
| 204 |
|
| 205 |
Gnat1_Path : String (1 .. Len_Arg (0)); |
| 206 |
|
| 207 |
B : constant Saved_Output_Buffer := Save_Output_Buffer; |
| 208 |
begin |
| 209 |
Fill_Arg (Gnat1_Path'Address, 0); |
| 210 |
|
| 211 |
Push_Output; |
| 212 |
Set_Output (Source_File); |
| 213 |
|
| 214 |
Write_Line ("with Ada.Command_Line;"); |
| 215 |
Write_Line ("use Ada.Command_Line;"); |
| 216 |
Write_Line ("with GNAT.Expect;"); |
| 217 |
Write_Line ("with GNAT.OS_Lib;"); |
| 218 |
Write_Eol; |
| 219 |
Write_Line ("procedure Oracle is"); |
| 220 |
Write_Line (" Child_Code : aliased Integer;"); |
| 221 |
Write_Eol; |
| 222 |
Write_Line (" Gnat1_Path : constant String := "); |
| 223 |
|
| 224 |
Write_Str (" """); |
| 225 |
Write_Str (Gnat1_Path); |
| 226 |
Write_Line (""";"); |
| 227 |
|
| 228 |
Write_Eol; |
| 229 |
Write_Line (" Args : constant GNAT.OS_Lib.Argument_List :="); |
| 230 |
|
| 231 |
Write_Str (" (new String'(""-gnatd_M"")"); |
| 232 |
|
| 233 |
-- The following way of iterating through the command line arguments |
| 234 |
-- was copied from Set_Targ. TODO factorize??? |
| 235 |
declare |
| 236 |
type Arg_Array is array (Nat) of Big_String_Ptr; |
| 237 |
type Arg_Array_Ptr is access Arg_Array; |
| 238 |
-- Types to access compiler arguments |
| 239 |
|
| 240 |
save_argc : Nat; |
| 241 |
pragma Import (C, save_argc); |
| 242 |
-- Saved value of argc (number of arguments), imported from |
| 243 |
-- misc.cc |
| 244 |
|
| 245 |
save_argv : Arg_Array_Ptr; |
| 246 |
pragma Import (C, save_argv); |
| 247 |
-- Saved value of argv (argument pointers), imported from misc.cc |
| 248 |
|
| 249 |
gnat_argc : Nat; |
| 250 |
gnat_argv : Arg_Array_Ptr; |
| 251 |
pragma Import (C, gnat_argc); |
| 252 |
pragma Import (C, gnat_argv); |
| 253 |
-- If save_argv is not set, default to gnat_argc/argv |
| 254 |
|
| 255 |
argc : Nat; |
| 256 |
argv : Arg_Array_Ptr; |
| 257 |
|
| 258 |
function Len_Arg (Arg : Big_String_Ptr) return Nat; |
| 259 |
-- Determine length of argument Arg (a nul terminated C string). |
| 260 |
|
| 261 |
------------- |
| 262 |
-- Len_Arg -- |
| 263 |
------------- |
| 264 |
|
| 265 |
function Len_Arg (Arg : Big_String_Ptr) return Nat is |
| 266 |
begin |
| 267 |
for J in 1 .. Nat'Last loop |
| 268 |
if Arg (Natural (J)) = ASCII.NUL then |
| 269 |
return J - 1; |
| 270 |
end if; |
| 271 |
end loop; |
| 272 |
|
| 273 |
raise Program_Error; |
| 274 |
end Len_Arg; |
| 275 |
|
| 276 |
begin |
| 277 |
if save_argv /= null then |
| 278 |
argv := save_argv; |
| 279 |
argc := save_argc; |
| 280 |
else |
| 281 |
-- Case of a non-GCC compiler, e.g. gnat2why or gnat2scil |
| 282 |
argv := gnat_argv; |
| 283 |
argc := gnat_argc; |
| 284 |
end if; |
| 285 |
|
| 286 |
for Arg in 1 .. argc - 1 loop |
| 287 |
declare |
| 288 |
Argv_Ptr : constant Big_String_Ptr := argv (Arg); |
| 289 |
Argv_Len : constant Nat := Len_Arg (Argv_Ptr); |
| 290 |
|
| 291 |
Arg : constant String := Argv_Ptr (1 .. Natural (Argv_Len)); |
| 292 |
begin |
| 293 |
-- We filter out mapping file arguments because we want to |
| 294 |
-- use the copies of source files we made. |
| 295 |
if Argv_Len > 8 and then Arg (1 .. 8) = "-gnatem=" then |
| 296 |
null; |
| 297 |
|
| 298 |
-- We must not have the oracle run the compiler in |
| 299 |
-- reduce-on-crash mode, that would result in recursive |
| 300 |
-- invocations. |
| 301 |
elsif Arg = "-gnatd_m" then |
| 302 |
null; |
| 303 |
else |
| 304 |
Write_Line (","); |
| 305 |
Write_Str (" new String'("""); |
| 306 |
|
| 307 |
-- We replace references to the main source file with |
| 308 |
-- references to the copy we made. |
| 309 |
if Old_Main_Path = Arg then |
| 310 |
Write_Str (New_Main_Path); |
| 311 |
|
| 312 |
-- We copy the other command line arguments unmodified |
| 313 |
else |
| 314 |
Write_Str (Arg); |
| 315 |
end if; |
| 316 |
|
| 317 |
Write_Str (""")"); |
| 318 |
end if; |
| 319 |
end; |
| 320 |
end loop; |
| 321 |
end; |
| 322 |
|
| 323 |
Write_Line (");"); |
| 324 |
|
| 325 |
Write_Eol; |
| 326 |
|
| 327 |
Write_Line (" Output : constant String :="); |
| 328 |
Write_Line (" GNAT.Expect.Get_Command_Output"); |
| 329 |
Write_Str (" (Gnat1_Path, Args, """", Child_Code'Access, "); |
| 330 |
Write_Line ("Err_To_Out => True);"); |
| 331 |
|
| 332 |
Write_Eol; |
| 333 |
|
| 334 |
Write_Line (" Crash_Marker : constant String :="); |
| 335 |
Write_Line (" ""+===========================GNAT BUG DETECTE"";"); |
| 336 |
|
| 337 |
Write_Eol; |
| 338 |
|
| 339 |
Write_Line (" Crashed : constant Boolean :="); |
| 340 |
Write_Line (" Crash_Marker'Length <= Output'Length"); |
| 341 |
Write_Str (" and then Output (Output'First .. Output'First "); |
| 342 |
Write_Line ("+ Crash_Marker'Length - 1)"); |
| 343 |
Write_Line (" = Crash_Marker;"); |
| 344 |
|
| 345 |
Write_Eol; |
| 346 |
|
| 347 |
Write_Str (" Status_Code : Exit_Status := "); |
| 348 |
Write_Line ("(if Crashed then 0 else 1);"); |
| 349 |
Write_Line ("begin"); |
| 350 |
Write_Line (" Set_Exit_Status (Status_Code);"); |
| 351 |
Write_Line ("end Oracle;"); |
| 352 |
|
| 353 |
Pop_Output; |
| 354 |
Restore_Output_Buffer (B); |
| 355 |
end Write_Oracle_Code; |
| 356 |
|
| 357 |
Close (Source_File); |
| 358 |
|
| 359 |
declare |
| 360 |
Args : constant Argument_List := |
| 361 |
(new String'(Source_File_Path), |
| 362 |
new String'("-o"), |
| 363 |
new String'(Oracle_Path), |
| 364 |
new String'("-D"), |
| 365 |
new String'(Oracle_Dir_Path)); |
| 366 |
|
| 367 |
Success : Boolean; |
| 368 |
begin |
| 369 |
Spawn (Gnatmake_Path.all, Args, Success); |
| 370 |
|
| 371 |
pragma Assert (Success); |
| 372 |
end; |
| 373 |
|
| 374 |
Free (Gnatmake_Path); |
| 375 |
end Create_Oracle; |
| 376 |
|
| 377 |
Run_Adareducer : |
| 378 |
declare |
| 379 |
-- See section 12.8.3 of the GNAT Studio user's guide for documentation |
| 380 |
-- about how to invoke adareducer. |
| 381 |
Gnatstudio_Cli_Path : String_Access := |
| 382 |
Locate_Exec_On_Path ("gnatstudio_cli"); |
| 383 |
|
| 384 |
begin |
| 385 |
if Gnatstudio_Cli_Path = null then |
| 386 |
Write_Line ("-gnatd_m was specified but adareducer is not available"); |
| 387 |
return; |
| 388 |
end if; |
| 389 |
|
| 390 |
declare |
| 391 |
Args : constant Argument_List := |
| 392 |
(new String'("adareducer"), |
| 393 |
new String'("-P"), |
| 394 |
new String'(Gpr_File_Path), |
| 395 |
new String'("-s"), |
| 396 |
new String'(Oracle_Path)); |
| 397 |
|
| 398 |
Success : Boolean; |
| 399 |
begin |
| 400 |
Spawn (Gnatstudio_Cli_Path.all, Args, Success); |
| 401 |
pragma Assert (Success); |
| 402 |
end; |
| 403 |
|
| 404 |
Free (Gnatstudio_Cli_Path); |
| 405 |
end Run_Adareducer; |
| 406 |
|
| 407 |
Clean_Up_Reproducer_Source : |
| 408 |
declare |
| 409 |
|
| 410 |
use type System.Address; |
| 411 |
|
| 412 |
Directory_Stream : System.CRTL.DIRs; |
| 413 |
|
| 414 |
function opendir (file_name : String) return System.CRTL.DIRs with |
| 415 |
Import, Convention => C, External_Name => "__gnat_opendir"; |
| 416 |
|
| 417 |
Conservative_Name_Max : constant Positive := 4096; |
| 418 |
|
| 419 |
Buffer : String (1 .. Conservative_Name_Max); |
| 420 |
Length : aliased Integer; |
| 421 |
|
| 422 |
Addr : System.Address; |
| 423 |
|
| 424 |
Dummy : Integer; |
| 425 |
|
| 426 |
Dummy_Success : Boolean; |
| 427 |
|
| 428 |
function readdir |
| 429 |
(Directory : System.CRTL.DIRs; |
| 430 |
Buffer : System.Address; |
| 431 |
Length : access Integer) return System.Address |
| 432 |
with Import, Convention => C, External_Name => "__gnat_readdir"; |
| 433 |
|
| 434 |
function closedir (directory : System.CRTL.DIRs) return Integer with |
| 435 |
Import, Convention => C, External_Name => "__gnat_closedir"; |
| 436 |
|
| 437 |
begin |
| 438 |
Directory_Stream := opendir (Src_Dir_Path & ASCII.NUL); |
| 439 |
|
| 440 |
if Directory_Stream = System.Null_Address then |
| 441 |
return; |
| 442 |
end if; |
| 443 |
|
| 444 |
loop |
| 445 |
Addr := readdir (Directory_Stream, Buffer'Address, Length'Access); |
| 446 |
if Addr = System.Null_Address then |
| 447 |
exit; |
| 448 |
end if; |
| 449 |
|
| 450 |
declare |
| 451 |
S : constant String := Buffer (1 .. Length); |
| 452 |
begin |
| 453 |
if (5 <= S'Length and then S (S'Last - 4 .. S'Last) = ".orig") |
| 454 |
or else (2 <= S'Length and then S (S'Last - 1 .. S'Last) = ".s") |
| 455 |
then |
| 456 |
System.OS_Lib.Delete_File |
| 457 |
(Src_Dir_Path & Directory_Separator & S, Dummy_Success); |
| 458 |
end if; |
| 459 |
end; |
| 460 |
end loop; |
| 461 |
|
| 462 |
Dummy := closedir (Directory_Stream); |
| 463 |
end Clean_Up_Reproducer_Source; |
| 464 |
end Generate_Minimal_Reproducer; |