<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/151632>151632</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            BPF Clang crashes when trying to align inline asm bytes to non 8-byte boundary
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Ezlanding1
      </td>
    </tr>
</table>

<pre>
    In the program below, Clang attempts to align the 7-bytes by adding a 1-byte NOP sequence in BPF [here](https://github.com/llvm/llvm-project/blob/c7bb105e97208295babd5e5efdc34f4fb72602b5/llvm/lib/MC/MCAssembler.cpp#L528). Since this instruction doesn't exist in BPF, (it can only do an 8-byte NOP sequence (what I believe is a `if r0 == 0 goto +0` instruction)), the BPF writeNopData fails [here](https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp#L57) and causes the fatal error. 

## Program
```C
void foo() {
 asm(".byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00");
}

int main() {
        return 0;
}
```
## Stack Dump
```
fatal error: error in backend: unable to write nop sequence of 1 bytes
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: clang -target bpf -c bug.bpf.c -o bug.bpf.o
1.      <eof> parser at end of file
2.      Code generation
 #0 0x00007dc57f23fd01 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xe3fd01)
 #1 0x00007dc57f23da3e llvm::sys::RunSignalHandlers() (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xe3da3e)
 #2 0x00007dc57f23f0ab llvm::sys::CleanupOnSignal(unsigned long) (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xe3f0ab)
 #3 0x00007dc57f16bbea (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xd6bbea)
 #4 0x00007dc57f16bb8b (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xd6bb8b)
 #5 0x00007dc57f23a007 llvm::sys::Process::Exit(int, bool) (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xe3a007)
 #6 0x00000000004136d2 (/usr/lib/llvm-14/bin/clang+0x4136d2)
 #7 0x00007dc57f17a673 llvm::report_fatal_error(llvm::Twine const&, bool) (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xd7a673)
 #8 0x00007dc580796b27 llvm::MCAssembler::writeSectionData(llvm::raw_ostream&, llvm::MCSection const*, llvm::MCAsmLayout const&) const (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0x2396b27)
 #9 0x00007dc580781b48 (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0x2381b48)
#10 0x00007dc5807808e0 (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0x23808e0)
#11 0x00007dc5807979a7 llvm::MCAssembler::Finish() (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0x23979a7)
#12 0x00007dc5807b4f6b llvm::MCELFStreamer::finishImpl() (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0x23b4f6b)
#13 0x00007dc5807d91f7 llvm::MCStreamer::Finish(llvm::SMLoc) (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0x23d91f7)
#14 0x00007dc57fac0194 llvm::AsmPrinter::doFinalization(llvm::Module&) (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0x16c0194)
#15 0x00007dc57f381a41 llvm::FPPassManager::doFinalization(llvm::Module&) (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xf81a41)
#16 0x00007dc57f37ba81 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xf7ba81)
#17 0x00007dc586657201 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>) (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x1857201)
#18 0x00007dc58697c855 (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x1b7c855)
#19 0x00007dc585804824 clang::ParseAST(clang::Sema&, bool, bool) (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0xa04824)
#20 0x00007dc586978b71 clang::CodeGenAction::ExecuteAction() (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x1b78b71)
#21 0x00007dc587314b57 clang::FrontendAction::Execute() (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x2514b57)
#22 0x00007dc58726c3a6 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x246c3a6)
#23 0x00007dc58738e45b clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x258e45b)
#24 0x000000000041328b cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/lib/llvm-14/bin/clang+0x41328b)
#25 0x00000000004114bc (/usr/lib/llvm-14/bin/clang+0x4114bc)
#26 0x00007dc586eeaed2 (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x20eaed2)
#27 0x00007dc57f16bb6d llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xd6bb6d)
#28 0x00007dc586eea9c0 clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x20ea9c0)
#29 0x00007dc586eb5183 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&) const (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x20b5183)
#30 0x00007dc586eb540a clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) const (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x20b540a)
#31 0x00007dc586ecf507 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x20cf507)
#32 0x0000000000410f26 main (/usr/lib/llvm-14/bin/clang+0x410f26)
#33 0x00007dc57dc29d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#34 0x00007dc57dc29e40 call_init ./csu/../csu/libc-start.c:128:20
#35 0x00007dc57dc29e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#36 0x000000000040e3b5 _start (/usr/lib/llvm-14/bin/clang+0x40e3b5)
clang: error: clang frontend command failed with exit code 70 (use -v to see invocation)
Ubuntu clang version 14.0.0-1ubuntu1.1
Target: bpf
Thread model: posix
InstalledDir: /usr/bin
```


[bug-b9abe2.sh.txt](https://github.com/user-attachments/files/21541180/bug-b9abe2.sh.txt)
[bug-b9abe2.c.txt](https://github.com/user-attachments/files/21541181/bug-b9abe2.c.txt)
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy8WVtzoziw_jXKi8ouIcDAQx6IE5-drcxOajNnX12SaGydxRIriVz215-SwAE8zs7EO7VT1AQLqfvrT33RhVkrdwrgGqU3KL29Yp3ba3N993fDVCXVLrriunq9_qSw2wNujd4ZdsAcGv2M6BqvG6Z2mDkHh9ZZ7DRmjdz1nbMFf3VgMX_FrPKyMMNRaMO_fXnAFv7qQAnAUuGbhw1G6c0eDKD0FtF871xrUVwiukF0s5Nu3_Gl0AdEN03zdPyzaI3-PxAO0Q1vNEd0IzLOI5JCkVGS0yLljFcppFBXIk7qpOYZXRHK04kc6cd9Xof_SmvhwBswS9G2iMb3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