Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making it work with llvm-project/clang/utils/creduce-clang-crash.py #2

Open
qbolec opened this issue Aug 19, 2021 · 0 comments
Open

Comments

@qbolec
Copy link

qbolec commented Aug 19, 2021

First of all, I'd like to say HUGE THANK YOU for writing this detailed manual!

Background: I needed to file a bug about crash in LLVM 10, 11, and 12, and would give up without your help.
(A crashed LLVM generates rec-something.sh and rec-something.cpp files, but these tend to be huge for large projects with lots of includes, and https://llvm.org/docs/HowToSubmitABug.html requests people filing bugs to minimize them using C-Reduce)

There were some modifications I had to apply to your steps, and I thought I'll share them here, while I still remember (most of) them.
I'm on Windows 10, with VS 2019 Enterprise and GitBash.
• I had both ActivePerl and Strawberry Perl installed, which made cpan commands from your guide fail in strange ways, and this was resolved by simply uninstalling the ActivePerl and redoing the cpan commands
• It turned out that at some point Python3 was needed (I don't remember at which stage, I believe it was one of the cpan packages unittests or something?), so I had to install it. It is also helpful for running the creduce-clang-crash.py later)
• The unifdef repository got inaccessible:

$ git clone git://dotat.at/unifdef.git
Cloning into 'unifdef'...
fatal: Could not read from remote repository.

so I had to use git clone git://dotat.at/unifdef.git. I've contacted Tony Flinch about this, as the unifdef's home page also refers to the same repo, and he explained that this seems to be some IPv6 issue and I can use whichever of the two repos work as the code should be in sync.
• I was unable to build creduce by pointing it to latest commit of llvm-project (which I believe is 14.x ATM). Also I couldn't do it with origin/release/11.x (when trying the "matching" origin/llvm-11 branch of creduce) - in both cases it failed when trying to compile, or perhaps link, the clang_delta with clang-cpp or something like this. What eventually worked for me was to:

/c/ade/llvm-project $ git checkout origin/release/10.x

and also:

/c/ade/creduce $ git checkout origin/llvm-10

• I wanted to manually compile LLVM as you suggest, because I was also getting these errors about cmake files missing. But I have VS 2019, and you wrote:

Important: As of this writing, LLVM miscompiles with VS 2019. Please make sure you are using 2015 or 2017.
so I was in despair, but then my coworker made me realize, that I already have LLVM 10.0.0 installed (after all I was trying to report a bug in it), and that supposedly LLVM should compile with LLVM! Indeed this worked fine, with the following:

c:\ade\llvm-build> cmake -G Ninja -DCMAKE_C_COMPILER="C:/Program Files/LLVM10.0.0/bin/clang-cl.exe" -DCMAKE_CXX_COMPILER="C:/Program Files/LLVM10.0.0/bin/clang-cl.exe" -DCMAKE_LINKER="C:/Program Files/LLVM10.0.0/bin/lld-link.exe" -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_ENABLE_PROJECTS=clang;lld ..\llvm-project\llvm
c:\ade\llvm-build> ninja

In other words, I was able to build LLVM from release/10.x source by using the already installed LLVM10.0.0 compiler binaries:)
• above commands do not work for me from vanilla cmd.exe:

    FAILED: cmTC_13eab.exe
    cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_13eab.dir --rc=C:\PROGRA~1\LLVM10~1.0\bin\llvm-rc.exe --mt=CMAKE_MT-NOTFOUND --manifests  -- C:\PROGRA~1\LLVM10~1.0\bin\lld-link.exe /nologo CMakeFiles\cmTC_13eab.dir\testCCompiler.c.obj  /out:cmTC_13eab.exe /implib:cmTC_13eab.lib /pdb:cmTC_13eab.pdb /version:0.0  /machine:x64  /debug /INCREMENTAL /subsystem:console  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
    LINK Pass 1: command "C:\PROGRA~1\LLVM10~1.0\bin\lld-link.exe /nologo CMakeFiles\cmTC_13eab.dir\testCCompiler.c.obj /out:cmTC_13eab.exe /implib:cmTC_13eab.lib /pdb:cmTC_13eab.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\cmTC_13eab.dir/intermediate.manifest CMakeFiles\cmTC_13eab.dir/manifest.res" failed (exit code 1) with the following output:
    lld-link: error: could not open 'kernel32.lib': no such file or directory
    lld-link: error: could not open 'user32.lib': no such file or directory
    lld-link: error: could not open 'gdi32.lib': no such file or directory
    lld-link: error: could not open 'winspool.lib': no such file or directory
    lld-link: error: could not open 'shell32.lib': no such file or directory
    lld-link: error: could not open 'ole32.lib': no such file or directory
    lld-link: error: could not open 'oleaut32.lib': no such file or directory
    lld-link: error: could not open 'uuid.lib': no such file or directory
    lld-link: error: could not open 'comdlg32.lib': no such file or directory
    lld-link: error: could not open 'advapi32.lib': no such file or directory
    lld-link: error: could not open 'msvcrtd.lib': no such file or directory
    lld-link: error: could not open 'oldnames.lib': no such file or directory
    ninja: build stopped: subcommand failed.

• they also do not work for me from the x86 Native Tools Command Prompt for VS 2019:

    FAILED: cmTC_d2322.exe
    cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_d2322.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100177~1.0\x86\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100177~1.0\x86\mt.exe --manifests  -- C:\PROGRA~1\LLVM10~1.0\bin\lld-link.exe /nologo CMakeFiles\cmTC_d2322.dir\testCCompiler.c.obj  /out:cmTC_d2322.exe /implib:cmTC_d2322.lib /pdb:cmTC_d2322.pdb /version:0.0  /machine:x64  /debug /INCREMENTAL /subsystem:console  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
    LINK Pass 1: command "C:\PROGRA~1\LLVM10~1.0\bin\lld-link.exe /nologo CMakeFiles\cmTC_d2322.dir\testCCompiler.c.obj /out:cmTC_d2322.exe /implib:cmTC_d2322.lib /pdb:cmTC_d2322.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\cmTC_d2322.dir/intermediate.manifest CMakeFiles\cmTC_d2322.dir/manifest.res" failed (exit code 1) with the following output:
    lld-link: error: <root>: undefined symbol: mainCRTStartup
    ninja: build stopped: subcommand failed.

• but they do work for me, if I run a vanilla cmd.exe and manually execute:

"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64

in it, first!
• also, they do work from x64 Native Tools Command Prompt for VS 2019 (so "x64" vs "x86" here makes a big deal)
• For creduce, I've used the following cmake command:

c:\ade\creduce-build>cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DLLVM_DIR=c:/ade/llvm-build -DCMAKE_C_COMPILER=c:/ade/llvm-build/bin/clang-cl.exe -DCMAKE_CXX_COMPILER=c:/ade/llvm-build/bin/clang-cl.exe -DCMAKE_PREFIX_PATH=c:/ade/llvm-build ../creduce

• To build creduce, I had to manually edit some .l files to avoid complains about #include <unistd.h> not working on Windows, due to this header missing:

diff --git a/clex/clex.l b/clex/clex.l
index f975e65..4a2935b 100644
--- a/clex/clex.l
+++ b/clex/clex.l
@@ -16,6 +16,7 @@ FS                    (f|F|l|L)
 IS                     (u|U|l|L)*

 %option noyywrap
+%option nounistd

 %{

diff --git a/clex/strlex.l b/clex/strlex.l
index 2096735..dc55f22 100644
--- a/clex/strlex.l
+++ b/clex/strlex.l
@@ -16,6 +16,7 @@ FS                    (f|F|l|L)
 IS                     (u|U|l|L)*

 %option noyywrap
+%option nounistd

 %{

diff --git a/delta/topformflat.l b/delta/topformflat.l
index 18d0b63..1ac6e74 100644
--- a/delta/topformflat.l
+++ b/delta/topformflat.l
@@ -30,6 +30,7 @@ int threshold = 0;
 %option noyywrap
 /* dsw: don't define yyunput() */
 %option nounput
+%option nounistd

 /* start condition for strings */
 %x STRING

• I don't think you need ninja install step for creduce, because we gonna use the creduce "binary" (it's actually a perl script) inside src/creduce-build/creduce, not in C:\Program Files (x86)\creduce. Actually, when I've tried to use the one from Program Files (x86)/creduce, it complained about pass_unifdef missing :

prereqs not found for pass pass_unifdef at C:\\Program Files (x86)\\creduce\\bin\\creduce.exe line 364.
 at C:\\Program Files (x86)\\creduce\\bin\\creduce.exe line 35.
        main::__ANON__("prereqs not found for pass pass_unifdef at C:\\\\Program Files "...) called at C:\\Program Files (x86)\\creduce\\bin\\creduce.exe line 364
        main::call_prereq_check("pass_unifdef") called at C:\\Program Files (x86)\\creduce\\bin\\creduce.exe line 1025

and this is perhaps because we've copied unifdef to src/creduce-build/unifdef in one of your steps
• OTOH the LLVM's /c/ade/llvm-project/clang/utils/creduce-clang-crash.py script, really wants the executable to have an ".exe" suffix, so I had to manually copy the /c/ade/creduce-build/creduce/creduce to /c/ade/creduce-build/creduce/creduce.exe
• The creduce binary is actually a Perl script, which is a problem on Windows, where the shebang line which tells bash to use Perl interpreter for it, obviously doesn't work. It will work, though, from Git Bash shell, for example:

$ 'C:\\ade\\creduce-build\\creduce\\creduce.exe' '--tidy' 'rec-cf789c.test.bat' 'rec-cf789c.reduced.cpp'

works fine, as GitBash parses the shebang in creduce.exe and thus uses Perl interpreter for it. The same command will not work from regular cmd.exe prompt, though:

This version of C:\ade\creduce-build\creduce\creduce.exe is not compatible with the version of Windows you're running. Check your computer's system information and then contact the software publisher.

• You might need to edit creduce.exe so that it doesn't try to read key presses from console:

$ 'C:\\ade\\creduce-build\\creduce\\creduce.exe' '--tidy' 'rec-cf789c.test.bat' 'rec-cf789c.reduced.cpp'
===< 2968 >===
running 1 interestingness test in parallel
===< pass_unifdef :: 0 >===
Undefined subroutine &Term::ReadKey::ReadMode called at C:\\ade\\creduce-build\\creduce\\creduce.exe line 620.
 at C:\\ade\\creduce-build\\creduce\\creduce.exe line 35.
        main::__ANON__("Undefined subroutine &Term::ReadKey::ReadMode called at C:\\\\a"...) called at C:\\ade\\creduce-build\\creduce\\creduce.exe line 620
        main::delta_pass(HASH(0x3395278)) called at C:\\ade\\creduce-build\\creduce\\creduce.exe line 1080

the fix is something like:

$ diff /c/ade/creduce-build/creduce/creduce{,.exe}
38c38
< my $SKIP_KEY_OFF = 0;
---
> my $SKIP_KEY_OFF = 1;

• The LLVM's /c/ade/llvm-project/clang/utils/creduce-clang-crash.py script assumes the environment to be rather "bash-like", which is almost fine, when you run it from GitBash, but unfortunately the creduce.exe tool itself expects the test script passed to it to be a "Windows executable", not a bash script, so one has to add some additional ".bat" wrapper around the generated ".sh" file in order to make creduce.exe recognize it as something it can execute. At the same time, because creduce.exe is a Perl script, trying to execute it directly with Popen fails, so I had to manually prepend the call to Perl interpreter:

$ git diff
diff --git a/clang/utils/creduce-clang-crash.py b/clang/utils/creduce-clang-crash.py
index b69d6efc2481..08a2b78ab296 100755
--- a/clang/utils/creduce-clang-crash.py
+++ b/clang/utils/creduce-clang-crash.py
@@ -69,7 +69,8 @@ class Reduce(object):
     crash_script_name, crash_script_ext = os.path.splitext(crash_script)
     file_reduce_name, file_reduce_ext = os.path.splitext(file_to_reduce)

-    self.testfile = file_reduce_name + '.test.sh'
+    self.bashfile = file_reduce_name + '.test.sh'
+    self.testfile = file_reduce_name + '.test.bat'
     self.crash_script = crash_script_name + '.reduced' + crash_script_ext
     self.file_to_reduce = file_reduce_name + '.reduced' + file_reduce_ext
     shutil.copy(file_to_reduce, self.file_to_reduce)
@@ -192,7 +193,8 @@ class Reduce(object):
     for msg in self.expected_output:
       output += 'grep -F %s t.log || exit 1\n' % pipes.quote(msg)

-    write_to_script(output, self.testfile)
+    write_to_script(output, self.bashfile)
+    write_to_script("bash -c '%s'\nexit /b %%errorlevel%%" % os.path.abspath(self.bashfile).replace("\\", "/"), self.testfile)
     self.check_interestingness()

   def check_interestingness(self):
@@ -360,7 +362,7 @@ class Reduce(object):
   def run_creduce(self):
     print("\nRunning C-Reduce...")
     try:
-      p = subprocess.Popen([creduce_cmd] + self.creduce_flags +
+      p = subprocess.Popen(['perl',creduce_cmd] + self.creduce_flags +
                            [self.testfile, self.file_to_reduce])
       p.communicate()
     except KeyboardInterrupt:

I invoke it like this:

/c/ade/crash $ /c/Users/jlopusza/AppData/Local/Programs/Python/Python39/python.exe /c/ade/llvm-project/clang/utils/creduce-clang-crash.py --creduce '/c/ade/creduce-build/creduce/creduce.exe' --llvm-not /c/ade/llvm-build/bin/not.exe --verbose rec-cf789c.sh rec-cf789c.cpp

where the rec-* files were copied to this directory from wherever the crashed LLVM has put them originally.
It still has some issues near the start, but continues and seems to be properly reducing the file:

===< 13524 >===
running 1 interestingness test in parallel
===< pass_unifdef :: 0 >===
The system cannot find the path specified.
The system cannot find the path specified.

***************************************************

pass_unifdef::0 has encountered a bug:
pass failed to modify the variant

Please consider tarring up C:\ade\crash\creduce_bug_000
and mailing it to creduce-bugs@flux.utah.edu and we will try to fix
the bug.

This bug is not fatal, C-Reduce will continue to execute.

***************************************************

===< pass_comments :: 0 >===
(-3.7 %, 4559098 bytes)
===< pass_ifs :: 0 >===
===< pass_includes :: 0 >===
===< pass_line_markers :: 0 >===
===< pass_blank :: 0 >===
(-1.6 %, 4465926 bytes)
(0.3 %, 4384925 bytes)
===< pass_clang_binsrch :: replace-function-def-with-decl >===
Use of uninitialized value $line in pattern match (m//) at C:/ade/creduce-build/creduce/pass_clang_binsrch.pm line 36.
===< pass_clang_binsrch :: remove-unused-function >===
Use of uninitialized value $line in pattern match (m//) at C:/ade/creduce-build/creduce/pass_clang_binsrch.pm line 36.
===< pass_lines :: 0 >===
(2.6 %, 4282061 bytes)
(3.5 %, 4244116 bytes)
(3.9 %, 4226659 bytes)
(4.1 %, 4215602 bytes)
(4.4 %, 4205283 bytes)
...

Once again, big thanks for writing this manual. Sharing my above addendum is the least I could do to pay back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant