<div dir="auto"><div><br><div class="gmail_extra"><br><div class="gmail_quote">On Jul 30, 2017 10:08 AM, "HaiLin via llvm-dev" <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br type="attribution"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hi, I come across an LLVM byte code which crashes opt when running with command opt-4.0 -instcombine ptx_link.bc -o opt.bc. </div><div><br></div><div><br></div><div>I have the source code of LLVM-4.0, so I did simple debugging. The gdb stack trace is </div><div><div>(gdb) r  -instcombine ptx_link.bc -o opt.bc</div><div>Starting program: opt -instcombine ptx_link.bc -o opt.bc</div><div>[Thread debugging using libthread_db enabled]</div><div>Using host libthread_db library "/lib/x86_64-linux-gnu/<wbr>libthread_db.so.1".</div><div><br></div><div>Program received signal SIGSEGV, Segmentation fault.</div><div>0x000000000116bd48 in llvm::GlobalValue::getParent (this=0x0) at ~/llvm-4.0.0/include/llvm/IR/<wbr>GlobalValue.h:538</div><div>538       const Module *getParent() const { return Parent; }</div><div>(gdb) bt</div><div>#0  0x000000000116bd48 in llvm::GlobalValue::getParent (this=0x0) at ~/llvm-4.0.0/include/llvm/IR/<wbr>GlobalValue.h:538</div><div>#1  0x00000000016def39 in llvm::TargetLibraryInfoImpl::<wbr>getLibFunc (this=0x4740160, FDecl=..., F=@0x7fffffffc0f4: llvm::LibFunc::msvc_new_int) at ~/llvm-4.0.0/lib/Analysis/<wbr>TargetLibraryInfo.cpp:1033</div><div>#2  0x0000000001466a96 in llvm::TargetLibraryInfo::<wbr>getLibFunc (this=0x4740218, FDecl=..., F=@0x7fffffffc0f4: llvm::LibFunc::msvc_new_int) at ~/llvm-4.0.0/include/llvm/<wbr>Analysis/TargetLibraryInfo.h:<wbr>233</div><div>#3  0x00000000023c6c0e in llvm::inferLibFuncAttributes (F=..., TLI=...) at ~/llvm-4.0.0/lib/Transforms/<wbr>Utils/BuildLibCalls.cpp:111</div><div>#4  0x00000000023caa4a in llvm::emitFWrite (Ptr=0x4c6f558, Size=0x4fe2b40, File=0x4be2aa8, B=..., DL=..., TLI=0x4740218) at ~/llvm-4.0.0/lib/Transforms/<wbr>Utils/BuildLibCalls.cpp:1000</div><div>#5  0x00000000024cee7e in llvm::LibCallSimplifier::<wbr>optimizeFPrintFString (this=0x7fffffffc520, CI=0x527a628, B=...) at ~/llvm-4.0.0/lib/Transforms/<wbr>Utils/SimplifyLibCalls.cpp:<wbr>1818</div><div>#6  0x00000000024cf06e in llvm::LibCallSimplifier::<wbr>optimizeFPrintF (this=0x7fffffffc520, CI=0x527a628, B=...) at ~/llvm-4.0.0/lib/Transforms/<wbr>Utils/SimplifyLibCalls.cpp:<wbr>1847</div><div>#7  0x00000000024d016a in llvm::LibCallSimplifier::<wbr>optimizeCall (this=0x7fffffffc520, CI=0x527a628) at ~/llvm-4.0.0/lib/Transforms/<wbr>Utils/SimplifyLibCalls.cpp:<wbr>2118</div><div>#8  0x0000000001ec9501 in llvm::InstCombiner::<wbr>tryOptimizeCall (this=0x7fffffffd050, CI=0x527a628) at ~/llvm-4.0.0/lib/Transforms/<wbr>InstCombine/InstCombineCalls.<wbr>cpp:2853</div><div>#9  0x0000000001eca247 in llvm::InstCombiner::<wbr>visitCallSite (this=0x7fffffffd050, CS=...) at ~/llvm-4.0.0/lib/Transforms/<wbr>InstCombine/InstCombineCalls.<wbr>cpp:3063</div><div>#10 0x0000000001ec2d79 in llvm::InstCombiner::<wbr>visitCallInst (this=0x7fffffffd050, CI=...) at ~/llvm-4.0.0/lib/Transforms/<wbr>InstCombine/InstCombineCalls.<wbr>cpp:1406</div><div>#11 0x0000000001e954aa in llvm::InstVisitor<llvm::<wbr>InstCombiner, llvm::Instruction*>::<wbr>delegateCallInst (this=0x7fffffffd050, I=...) at ~/llvm-4.0.0/include/llvm/IR/<wbr>InstVisitor.h:281</div><div>#12 0x0000000001e9452d in llvm::InstVisitor<llvm::<wbr>InstCombiner, llvm::Instruction*>::visitCall (this=0x7fffffffd050, I=...) at ~/llvm-4.0.0/include/llvm/IR/<wbr>Instruction.def:186</div><div>#13 0x0000000001e929a9 in llvm::InstVisitor<llvm::<wbr>InstCombiner, llvm::Instruction*>::visit (this=0x7fffffffd050, I=...) at ~/llvm-4.0.0/include/llvm/IR/<wbr>Instruction.def:186</div><div>#14 0x0000000001e8c95e in llvm::InstCombiner::run (this=0x7fffffffd050) at ~/llvm-4.0.0/lib/Transforms/<wbr>InstCombine/<wbr>InstructionCombining.cpp:2921</div><div>#15 0x0000000001e8daf4 in combineInstructionsOverFunctio<wbr>n (F=..., Worklist=..., AA=0x49cbcf0, AC=..., TLI=..., DT=..., ExpensiveCombines=true, LI=0x0) at ~/llvm-4.0.0/lib/Transforms/<wbr>InstCombine/<wbr>InstructionCombining.cpp:3155</div><div>#16 0x0000000001e8de4a in llvm::<wbr>InstructionCombiningPass::<wbr>runOnFunction (this=0x480c990, F=...) at ~/llvm-4.0.0/lib/Transforms/<wbr>InstCombine/<wbr>InstructionCombining.cpp:3212</div><div>#17 0x0000000001ccf4fc in llvm::FPPassManager::<wbr>runOnFunction (this=0x480c350, F=...) at ~/llvm-4.0.0/lib/IR/<wbr>LegacyPassManager.cpp:1513</div><div>#18 0x0000000001ccf695 in llvm::FPPassManager::<wbr>runOnModule (this=0x480c350, M=...) at ~/llvm-4.0.0/lib/IR/<wbr>LegacyPassManager.cpp:1534</div><div>#19 0x0000000001ccfa10 in (anonymous namespace)::MPPassManager::<wbr>runOnModule (this=0x478ea20, M=...) at ~/llvm-4.0.0/lib/IR/<wbr>LegacyPassManager.cpp:1590</div><div>#20 0x0000000001cd0125 in llvm::legacy::PassManagerImpl:<wbr>:run (this=0x4741bd0, M=...) at ~/llvm-4.0.0/lib/IR/<wbr>LegacyPassManager.cpp:1693</div><div>#21 0x0000000001cd0331 in llvm::legacy::PassManager::run (this=0x7fffffffd550, M=...) at ~/llvm-4.0.0/lib/IR/<wbr>LegacyPassManager.cpp:1724</div><div>#22 0x0000000001120f58 in main (argc=136, argv=0x7fffffffda48) at ~/llvm-4.0.0/tools/opt/opt.<wbr>cpp:739</div><div>(gdb) f 4</div><div>#4  0x00000000023caa4a in llvm::emitFWrite (Ptr=0x4c6f558, Size=0x4fe2b40, File=0x4be2aa8, B=..., DL=..., TLI=0x4740218) at ~/llvm-4.0.0/lib/Transforms/<wbr>Utils/BuildLibCalls.cpp:1000</div><div>1000        inferLibFuncAttributes(*M-><wbr>getFunction(FWriteName), *TLI);</div><div>(gdb) p FWriteName</div><div>$1 = {</div><div>  static npos = 18446744073709551615,</div><div>  Data = 0x2fbf720 "fwrite",</div><div>  Length = 6</div><div>}</div><div>(gdb) p M->getFunction(FWriteName)</div><div>$2 = (llvm::Function *) 0x0</div></div><div><br></div><div>It looks like LLVM cannot find function fwrite. I disassembled the byte code and found it is an alias to fwrite_unlocked.</div><div><br></div><div><div>➜  src grep alias ptx_link.ll |grep fwrite</div><div>@fwrite = alias i64 (i8*, i64, i64, %struct.__STDIO_FILE_STRUCT*), i64 (i8*, i64, i64, %struct.__STDIO_FILE_STRUCT*)* @fwrite_unlocked</div><div>define hidden i64 @__stdio_fwrite(i8* noalias, i64, %struct.__STDIO_FILE_STRUCT* noalias) #0 !dbg !38214 {</div><div>define i64 @fwrite_unlocked(i8* noalias, i64, i64, %struct.__STDIO_FILE_STRUCT* noalias) #0 !dbg !41244 {</div><div>define hidden i64 @_wstdio_fwrite(i32* noalias, i64, %struct.__STDIO_FILE_STRUCT* noalias) #0 !dbg !53331 {</div></div><div><br></div><div><div>The byte code is obtained from the coreutils and klee-uclibc project.</div><div><br></div><div>1) build klee-uclibc</div><div>git clone <a href="https://github.com/klee/klee-uclibc.git" target="_blank">https://github.com/klee/klee-<wbr>uclibc.git</a><br></div><div>cd klee-uclibc</div><div>./configure -l --with-llvm-config=/usr/bin/<wbr>llvm-config-4.0 --with-cc=/usr/bin/clang-4.0<br></div><div>mkdir tmp</div><div>cd tmp</div><div>llvm-ar-4.0 x ../lib/libc.a<br></div><div>llvm-link-4.0 ./*.os -o libc.a<br></div><div><br></div><div>2) build CoreUtils</div><div><div>pip install --upgrade wllvm</div><div><br></div><div>sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-4.0 80</div><div>export LLVM_COMPILER=clang</div></div><div><br></div><div>wget <a href="https://ftp.gnu.org/gnu/coreutils/coreutils-6.10.tar.gz" target="_blank">https://ftp.gnu.org/gnu/<wbr>coreutils/coreutils-6.10.tar.<wbr>gz</a></div><div><div>tar -zxvf coreutils-6.10.tar.gz</div><div><br></div><div>mkdir -p coreutils-6.10/obj-llvm</div><div>cd coreutils-6.10/obj-llvm</div><div>CC=wllvm ../configure --disable-nls CFLAGS="-g"</div><div>CC=wllvm make</div><div><br></div><div>cd src</div><div>find . -executable -type f | xargs -I '{}' extract-bc '{}'</div></div><div><br></div><div>sudo update-alternatives --install /usr/bin/llvm-link llvm-link /usr/bin/llvm-link-4.0 80<br></div><div>llvm-link ptx.bc ~/Documents/Dev/klee-uclibc/<wbr>tmp/libc.a -o ptx_link.bc<br></div><div><br></div><div>3) run opt</div><div>opt-4.0 -instcombine ptx_link.bc -o opt.bc</div></div><div><br></div><div><br></div><div>The running environment is Ubuntu 16.04.2 LTS.</div></div>
<br>______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br></blockquote></div><br></div></div><div class="gmail_extra" dir="auto">Make sure this reproes on ToT, and in case it does, please file a bug (make sure to include the bc file to the report)</div><div class="gmail_extra" dir="auto"><br></div><div class="gmail_extra" dir="auto">--</div><div class="gmail_extra" dir="auto">Davide</div></div>