Linux: Wrong MachineOperand accessor

Created on 27 Sep 2019  路  14Comments  路  Source: ClangBuiltLinux/linux

Trying to compile kernel 5.3.1 with LLVM/clang-9.0.0 on i686

Full logs can be found here https://abf.openmandriva.org/build_lists/611355 also links to GitHub sources

I've applied that patch https://github.com/ClangBuiltLinux/continuous-integration/blob/i386/patches/llvm-all/linux/i386/i386-percpu.patch

 CC      kernel/jump_label.o
BUILDSTDERR: clang-9: /builddir/build/BUILD/llvm-9.0.0.src/llvm/include/llvm/CodeGen/MachineOperand.h:527: int64_t llvm::MachineOperand::getImm() const: Assertion `isImm() && "Wrong MachineOperand accessor"' failed.
BUILDSTDERR: Stack dump:
BUILDSTDERR: 0. Program arguments: /usr/bin/clang-9 -cc1 -triple i686-pc-linux-gnu -S -disable-free -main-file-name jump_label.c -mrelocation-model static -mthread-model posix -fno-delete-null-pointer-checks -mllvm -warn-stack-size=1024 -mregparm 3 -freg-struct-return -mdisable-fp-elim -relaxed-aliasing -mdisable-tail-calls -fmath-errno -masm-verbose -no-integrated-as -mconstructor-aliases -ffreestanding -fuse-init-array -target-cpu i686 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -target-feature -sse -target-feature -mmx -target-feature -sse2 -target-feature -3dnow -target-feature -avx -target-feature +retpoline-external-thunk -no-implicit-float -dwarf-column-info -debugger-tuning=gdb -coverage-notes-file /builddir/build/BUILD/linux-5.3/kernel/jump_label.gcno -nostdsysteminc -nobuiltininc -resource-dir /usr/lib/clang/9.0.0 -dependency-file kernel/.jump_label.o.d -MT kernel/jump_label.o -sys-header-deps -isystem /usr/lib/clang/9.0.0/include -include ./include/linux/kconfig.h -include ./include/linux/compiler_types.h -I ./arch/x86/include -I ./arch/x86/include/generated -I ./include -I ./arch/x86/include/uapi -I ./arch/x86/include/generated/uapi -I ./include/uapi -I ./include/generated/uapi -D __KERNEL__ -D CONFIG_AS_CFI=1 -D CONFIG_AS_CFI_SIGNAL_FRAME=1 -D CONFIG_AS_CFI_SECTIONS=1 -D CONFIG_AS_SSSE3=1 -D CONFIG_AS_AVX=1 -D CONFIG_AS_AVX2=1 -D CONFIG_AS_AVX512=1 -D CONFIG_AS_SHA1_NI=1 -D CONFIG_AS_SHA256_NI=1 -D CC_USING_FENTRY -D KBUILD_BASENAME="jump_label" -D KBUILD_MODNAME="jump_label" -O3 -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs -Werror=implicit-function-declaration -Werror=implicit-int -Wno-format-security -Werror=unknown-warning-option -Wno-sign-compare -Wno-address-of-packed-member -Wno-format-invalid-specifier -Wno-gnu -Wno-tautological-compare -Wno-unused-const-variable -Wdeclaration-after-statement -Wimplicit-fallthrough -Wvla -Wno-pointer-sign -Werror=date-time -Werror=incompatible-pointer-types -Wno-initializer-overrides -Wno-format -Wno-sign-compare -Wno-format-zero-length -std=gnu89 -fno-dwarf-directory-asm -fdebug-compilation-dir /builddir/build/BUILD/linux-5.3 -ferror-limit 19 -fmessage-length 0 -pg -mfentry -fwrapv -stack-protector 2 -mstack-alignment=4 -fcf-protection=none -fwchar-type=short -fno-signed-wchar -fobjc-runtime=gcc -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -o /tmp/jump_label-ae0337.s -x c kernel/jump_label.c 
BUILDSTDERR: 1. <eof> parser at end of file
BUILDSTDERR: 2. Code generation
BUILDSTDERR: 3. Running pass 'Function Pass Manager' on module 'kernel/jump_label.c'.
BUILDSTDERR: 4. Running pass 'Two-Address instruction pass' on function '@jump_label_init'
BUILDSTDERR:  #0 0xf2565d2f (/usr/lib/libLLVMSupport.so.9.0+0x210d2f)
BUILDSTDERR:  #1 0xf2562ede llvm::sys::RunSignalHandlers() (/usr/lib/libLLVMSupport.so.9.0+0x20dede)
BUILDSTDERR:  #2 0xf2565f45 (/usr/lib/libLLVMSupport.so.9.0+0x210f45)
BUILDSTDERR:  #3 0xf7f809b0 (linux-gate.so.1+0x9b0)
BUILDSTDERR:  #4 0xf7f80999 (linux-gate.so.1+0x999)
BUILDSTDERR:  #5 0xf1c05d42 raise (/lib/libc.so.6+0x3dd42)
BUILDSTDERR:  #6 0xf1be530b abort (/lib/libc.so.6+0x1d30b)
BUILDSTDERR:  #7 0xf1be51d3 plural_eval.cold (/lib/libc.so.6+0x1d1d3)
BUILDSTDERR:  #8 0xf1bf65fb (/lib/libc.so.6+0x2e5fb)
BUILDSTDERR:  #9 0xf5b441fa llvm::X86InstrInfo::convertToThreeAddress(llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::MachineBasicBlock, false, false, void>, false, false>&, llvm::MachineInstr&, llvm::LiveVariables*) const (/usr/lib/libLLVMX86CodeGen.so.9.0+0x47d1fa)
BUILDSTDERR: #10 0xf533451e (/usr/lib/libLLVMCodeGen.so.9.0+0x62d51e)
BUILDSTDERR: #11 0xf5331730 (/usr/lib/libLLVMCodeGen.so.9.0+0x62a730)
BUILDSTDERR: #12 0xf5101015 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (/usr/lib/libLLVMCodeGen.so.9.0+0x3fa015)
BUILDSTDERR: #13 0xf31de536 llvm::FPPassManager::runOnFunction(llvm::Function&) (/usr/lib/libLLVMCore.so.9.0+0x37b536)
BUILDSTDERR: #14 0xf31dea66 llvm::FPPassManager::runOnModule(llvm::Module&) (/usr/lib/libLLVMCore.so.9.0+0x37ba66)
BUILDSTDERR: #15 0xf31df23a llvm::legacy::PassManagerImpl::run(llvm::Module&) (/usr/lib/libLLVMCore.so.9.0+0x37c23a)
BUILDSTDERR: #16 0xf31dfbe7 llvm::legacy::PassManager::run(llvm::Module&) (/usr/lib/libLLVMCore.so.9.0+0x37cbe7)
BUILDSTDERR: #17 0xf44719c9 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::DataLayout const&, llvm::Module*, clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream> >) (/usr/lib/libclangCodeGen.so.9.0+0x2129c9)
BUILDSTDERR: #18 0xf47d81ac (/usr/lib/libclangCodeGen.so.9.0+0x5791ac)
BUILDSTDERR: #19 0xf03f358c clang::ParseAST(clang::Sema&, bool, bool) (/usr/lib/libclangParse.so.9.0+0x6358c)
BUILDSTDERR: #20 0xf3fbd915 clang::ASTFrontendAction::ExecuteAction() (/usr/lib/libclangFrontend.so.9.0+0x141915)
BUILDSTDERR: #21 0xf47d6e4a clang::CodeGenAction::ExecuteAction() (/usr/lib/libclangCodeGen.so.9.0+0x577e4a)
BUILDSTDERR: #22 0xf3fbd097 clang::FrontendAction::Execute() (/usr/lib/libclangFrontend.so.9.0+0x141097)
BUILDSTDERR: #23 0xf3f56fe7 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/lib/libclangFrontend.so.9.0+0xdafe7)
BUILDSTDERR: #24 0xf3e790a7 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/lib/libclangFrontendTool.so.9.0+0x50a7)
BUILDSTDERR: #25 0x004144a3 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/bin/clang-9+0x4144a3)
BUILDSTDERR: #26 0x00411951 main (/usr/bin/clang-9+0x411951)
BUILDSTDERR: #27 0xf1be6f51 __libc_start_main (/lib/libc.so.6+0x1ef51)
BUILDSTDERR: #28 0x0040e021 _start (/usr/bin/clang-9+0x40e021)
BUILDSTDERR: clang-9: error: unable to execute command: Aborted (core dumped)
BUILDSTDERR: clang-9: error: clang frontend command failed due to signal (use -v to see invocation)
BUILDSTDERR: clang version 9.0.0 (tags/RELEASE_900/final)
BUILDSTDERR: Target: i686-pc-linux-gnu
BUILDSTDERR: Thread model: posix
BUILDSTDERR: InstalledDir: /usr/bin
BUILDSTDERR: clang-9: note: diagnostic msg: PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script.
BUILDSTDERR: clang-9: note: diagnostic msg: 
BUILDSTDERR: ********************
BUILDSTDERR: PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
BUILDSTDERR: Preprocessed source(s) and associated run script(s) are located at:
BUILDSTDERR: clang-9: note: diagnostic msg: /tmp/jump_label-4b19f0.c
BUILDSTDERR: clang-9: note: diagnostic msg: /tmp/jump_label-4b19f0.sh
BUILDSTDERR: clang-9: note: diagnostic msg: 
BUILDSTDERR: ********************
BUILDSTDERR: make[1]: *** [scripts/Makefile.build:281: kernel/jump_label.o] Error 254
BUILDSTDERR: make[1]: *** Waiting for unfinished jobs....
Compiler crash [ARCH] x86 [BUG] llvm [FIXED][LLVM] 10

Most helpful comment

Merged into 9.x branch: https://github.com/llvm/llvm-project/commit/933cfc6

It will be available in clang 9.0.1

All 14 comments

This is reproducible with tip of tree LLVM and Linux without that patch (just have to build kernel/jump_label.o).

creduce spits out:

a() {
  int b, c = &b;
  d(c - (long)a);
  for (; a < &b;)
    ;
}

Original file and interestingness test available here.

The original was due to using -O0, which you can't do while compiling Linux. However, it should most likely not ICE.

All of my reproduction was at -O2.

@nathanchance without that patch I didn't reach that far in compilation process on i686

Yes, the idea is to make sure that the patch isn't causing issue. Building the one problematic translation unit (kernel/jump_label.o) does not require the patch, thus mainline can be tested without any issues.

For the record, here is the full command on to reproduce on my machine (with common.config and common-desktop.config added to kernel/configs):

% make -j$(nproc) -s CC=clang O=out distclean i386_defconfig common.config common-desktop.config kernel/jump_label.o

I wasn't able to reproduce it, i386 kernel compiles for me just fine. At some point it used to boot, but at the moment it panics with "Attempted to kill the idle task!".

Also Nathan's creduced test succeeds for me with just warnings:

cbl-718# clang-10 -O2 -no-integrated-as -m32 -mregparm=3 -march=i686 -Wno-pointer-sign -Wno-unused-value -c -o /dev/null jump_label.i
jump_label.i:1:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
a() {
^
jump_label.i:2:10: warning: incompatible pointer to integer conversion initializing 'int' with an expression of type 'int *'; remove & [-Wint-conversion]
  int b, c = &b;
         ^   ~~
jump_label.i:3:3: warning: implicit declaration of function 'd' is invalid in C99 [-Wimplicit-function-declaration]
  d(c - (long)a);
  ^
jump_label.i:4:12: warning: comparison of distinct pointer types ('int (*)()' and 'int *') [-Wcompare-distinct-pointer-types]
  for (; a < &b;)
         ~ ^ ~~
jump_label.i:6:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
5 warnings generated.

You will need to have assertions enabled to see this. Add --assertions if using tc-build or -DLLVM_ENABLE_ASSERTIONS=ON to your cmake arguments.

I was using the binaries from LLVM's apt repo. Seems that it was built without this option. I will enable assertions when building my own toolchain.

I am 99% certain that this crash is directly related to the boot issue we are having in our CI repo as I do not see this assertion on r366150 but I do see it with the next commit r366151, which I bisected as being the commit that causes our boot issues.

I have reported this bug upstream: https://llvm.org/pr43529

I tried building older versions of the kernel which used to build previously, and they now have similar panic, so I totally agree with you that this crash is directly related to this assertion.

The patch suggested by @topperc in LLVM bugzilla:

diff --git a/llvm/lib/Target/X86/X86InstrInfo.cpp b/llvm/lib/Target/X86/X86InstrInfo.cpp
index 7fba03c..bc13180 100644
--- a/llvm/lib/Target/X86/X86InstrInfo.cpp
+++ b/llvm/lib/Target/X86/X86InstrInfo.cpp
@@ -1122,6 +1122,8 @@ X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
     return nullptr;
   case X86::SUB32ri8:
   case X86::SUB32ri: {
+    if (!MI.getOperand(2).isImm())
+      return nullptr;
     int64_t Imm = MI.getOperand(2).getImm();
     if (!isInt<32>(-Imm))
       return nullptr;

Merged into master: https://github.com/llvm/llvm-project/commit/912870573c5f55b3f6dc4494a608e3ee9c2f730d

It's been nominated for clang 9.0.1.

I can confirm that patch does fix this issue. Finally i get kernel successfull build with LLVM/clang-9.0.0 (patched) on i686 arch https://abf.openmandriva.org/build_lists/612950

Merged into 9.x branch: https://github.com/llvm/llvm-project/commit/933cfc6

It will be available in clang 9.0.1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nickdesaulniers picture nickdesaulniers  路  4Comments

nathanchance picture nathanchance  路  3Comments

nickdesaulniers picture nickdesaulniers  路  3Comments

tpimh picture tpimh  路  5Comments

nathanchance picture nathanchance  路  4Comments