Skip to content

Conversation

@Prabhuk
Copy link
Contributor

@Prabhuk Prabhuk commented Nov 10, 2025

When there are more than 255 sections, MachO object writer allows
creation of object files which are potentially malformed. Currently,
there are assertions in object writer code that prevents this behavior.
But for distributions where assertions are turned off this still results in
creation of malformed object files. Turning assertions into explicit
errors.

When there are more than 255 sections, MachO object writer allows
creation of object files which are potentially malformed. Currently,
there is an assertion in object writer code that prevents it. But for
distributions where assertions are turned on this still results in
creation of malformed objects. Turning the assertion into an explicit
fatal error.
@llvmbot llvmbot added the llvm:mc Machine (object) code label Nov 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 10, 2025

@llvm/pr-subscribers-llvm-mc

Author: Prabhu Rajasekaran (Prabhuk)

Changes

When there are more than 255 sections, MachO object writer allows
creation of object files which are potentially malformed. Currently,
there is an assertion in object writer code that prevents it. But for
distributions where assertions are turned on this still results in
creation of malformed objects. Turning the assertion into an explicit
fatal error.


Full diff: https://github.com/llvm/llvm-project/pull/167418.diff

1 Files Affected:

  • (modified) llvm/lib/MC/MachObjectWriter.cpp (+2-1)
diff --git a/llvm/lib/MC/MachObjectWriter.cpp b/llvm/lib/MC/MachObjectWriter.cpp
index 39542bfbdd8e3..a6d87827795c2 100644
--- a/llvm/lib/MC/MachObjectWriter.cpp
+++ b/llvm/lib/MC/MachObjectWriter.cpp
@@ -646,7 +646,8 @@ void MachObjectWriter::computeSymbolTable(
       LocalSymbolData.push_back(MSD);
     } else {
       MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
-      assert(MSD.SectionIndex && "Invalid section index!");
+      if (!MSD.SectionIndex)
+        report_fatal_error("Invalid section index!");
       LocalSymbolData.push_back(MSD);
     }
   }

@Prabhuk
Copy link
Contributor Author

Prabhuk commented Nov 11, 2025

Addresses issue: #167408

@EugeneZelenko EugeneZelenko removed their request for review November 11, 2025 00:32
@Prabhuk Prabhuk requested a review from MaskRay November 11, 2025 00:44
@github-actions
Copy link

github-actions bot commented Nov 11, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@Prabhuk Prabhuk changed the title [MachO] Fatal error on invalid section index [MachO] Report error when there are too many sections Nov 11, 2025
@Prabhuk Prabhuk requested a review from lhames November 11, 2025 21:12
Copy link
Contributor

@lhames lhames left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks @Prabhuk!

@Prabhuk Prabhuk merged commit dd1564e into llvm:main Nov 11, 2025
10 checks passed
@Prabhuk Prabhuk deleted the patch_macho_writer branch November 11, 2025 22:26
@llvm-ci
Copy link
Collaborator

llvm-ci commented Nov 11, 2025

LLVM Buildbot has detected a new failure on builder llvm-clang-x86_64-sie-ubuntu-fast running on sie-linux-worker while building llvm at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/144/builds/39919

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'LLVM :: MC/MachO/invalid-section-index.s' FAILED ********************
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 3
not /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/llvm-mc -filetype=obj -triple arm64-apple-macos /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/llvm/test/MC/MachO/invalid-section-index.s -o - 2>&1 | /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/FileCheck /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/llvm/test/MC/MachO/invalid-section-index.s --check-prefix=MACHOERROR
# executed command: not /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/llvm-mc -filetype=obj -triple arm64-apple-macos /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/llvm/test/MC/MachO/invalid-section-index.s -o -
# note: command had no output on stdout or stderr
# executed command: /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/FileCheck /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/llvm/test/MC/MachO/invalid-section-index.s --check-prefix=MACHOERROR
# .---command stderr------------
# | �[1m/home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/llvm/test/MC/MachO/invalid-section-index.s:5:16: �[0m�[0;1;31merror: �[0m�[1mMACHOERROR: expected string not found in input
�[0m# | �[1m�[0m// MACHOERROR: error: Too many sections!
# | �[0;1;32m               ^
�[0m# | �[0;1;32m�[0m�[1m<stdin>:1:1: �[0m�[0;1;30mnote: �[0m�[1mscanning from here
�[0m# | �[1m�[0m/home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/llvm-mc: error: unable to get target for 'arm64-apple-macos', see --version and --triple.
# | �[0;1;32m^
�[0m# | �[0;1;32m�[0m�[1m<stdin>:1:83: �[0m�[0;1;30mnote: �[0m�[1mpossible intended match here
�[0m# | �[1m�[0m/home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/llvm-mc: error: unable to get target for 'arm64-apple-macos', see --version and --triple.
# | �[0;1;32m                                                                                  ^
�[0m# | �[0;1;32m�[0m
# | Input file: <stdin>
# | Check file: /home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/llvm-project/llvm/test/MC/MachO/invalid-section-index.s
# | 
# | -dump-input=help explains the following input dump.
# | 
# | Input was:
# | <<<<<<
# | �[1m�[0m�[0;1;30m           1: �[0m�[1m�[0;1;46m/home/buildbot/buildbot-root/llvm-clang-x86_64-sie-ubuntu-fast/build/bin/llvm-mc: error: unable to get target for 'arm64-apple-macos', see --version and --triple.�[0m
# | �[0;1;31mcheck:5'0     X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
�[0m# | �[0;1;31m�[0m�[0;1;35mcheck:5'1                                                                                       ?                                                                                possible intended match
�[0m# | �[0;1;35m�[0m>>>>>>
# `-----------------------------
# error: command failed with exit status: 1

--

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


@Kewen12
Copy link
Contributor

Kewen12 commented Nov 11, 2025

Hello, we saw llvm test failure with this PR. Could you please take a look? Thanks!

bot: https://lab.llvm.org/buildbot/#/builders/10/builds/17126

******************** TEST 'LLVM :: MC/MachO/invalid-section-index.s' FAILED ********************
Exit Code: 1
Command Output (stdout):
--
# RUN: at line 3
not /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.build/bin/llvm-mc -filetype=obj -triple arm64-apple-macos /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/llvm/test/MC/MachO/invalid-section-index.s -o - 2>&1 | /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.build/bin/FileCheck /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/llvm/test/MC/MachO/invalid-section-index.s --check-prefix=MACHOERROR
# executed command: not /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.build/bin/llvm-mc -filetype=obj -triple arm64-apple-macos /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/llvm/test/MC/MachO/invalid-section-index.s -o -
# note: command had no output on stdout or stderr
# executed command: /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.build/bin/FileCheck /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/llvm/test/MC/MachO/invalid-section-index.s --check-prefix=MACHOERROR
# .---command stderr------------
# | /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/llvm/test/MC/MachO/invalid-section-index.s:5:16: error: MACHOERROR: expected string not found in input
# | // MACHOERROR: error: Too many sections!
# |                ^
# | <stdin>:1:1: note: scanning from here
# | /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.build/bin/llvm-mc: error: unable to get target for 'arm64-apple-macos', see --version and --triple.
# | ^
# | <stdin>:1:80: note: possible intended match here
# | /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.build/bin/llvm-mc: error: unable to get target for 'arm64-apple-macos', see --version and --triple.
# |                                                                                ^
# | 
# | Input file: <stdin>
# | Check file: /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.src/llvm/test/MC/MachO/invalid-section-index.s
# | 
# | -dump-input=help explains the following input dump.
# | 
# | Input was:
# | <<<<<<
# |            1: /home/botworker/builds/openmp-offload-amdgpu-runtime-2/llvm.build/bin/llvm-mc: error: unable to get target for 'arm64-apple-macos', see --version and --triple.
# | check:5'0     X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
# | check:5'1                                                                                    ?                                                                                possible intended match
# | >>>>>>
# `-----------------------------
# error: command failed with exit status: 1
--
********************

@Prabhuk
Copy link
Contributor Author

Prabhuk commented Nov 11, 2025

Apologies @Kewen12 -- sending out a fix right away. If that doesn't work I'll revert ASAP.

@Prabhuk
Copy link
Contributor Author

Prabhuk commented Nov 11, 2025

#167598 -- please let me know if this fixes the failure.

@llvm-ci
Copy link
Collaborator

llvm-ci commented Nov 12, 2025

LLVM Buildbot has detected a new failure on builder llvm-nvptx64-nvidia-ubuntu running on as-builder-7 while building llvm at step 6 "test-build-unified-tree-check-llvm".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/160/builds/28102

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-llvm) failure: test (failure)
******************** TEST 'LLVM :: MC/MachO/invalid-section-index.s' FAILED ********************
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 3
not /home/buildbot/worker/as-builder-7/llvm-nvptx64-nvidia-ubuntu/build/bin/llvm-mc -filetype=obj -triple arm64-apple-macos /home/buildbot/worker/as-builder-7/llvm-nvptx64-nvidia-ubuntu/llvm-project/llvm/test/MC/MachO/invalid-section-index.s -o - 2>&1 | /home/buildbot/worker/as-builder-7/llvm-nvptx64-nvidia-ubuntu/build/bin/FileCheck /home/buildbot/worker/as-builder-7/llvm-nvptx64-nvidia-ubuntu/llvm-project/llvm/test/MC/MachO/invalid-section-index.s --check-prefix=MACHOERROR
# executed command: not /home/buildbot/worker/as-builder-7/llvm-nvptx64-nvidia-ubuntu/build/bin/llvm-mc -filetype=obj -triple arm64-apple-macos /home/buildbot/worker/as-builder-7/llvm-nvptx64-nvidia-ubuntu/llvm-project/llvm/test/MC/MachO/invalid-section-index.s -o -
# executed command: /home/buildbot/worker/as-builder-7/llvm-nvptx64-nvidia-ubuntu/build/bin/FileCheck /home/buildbot/worker/as-builder-7/llvm-nvptx64-nvidia-ubuntu/llvm-project/llvm/test/MC/MachO/invalid-section-index.s --check-prefix=MACHOERROR
# .---command stderr------------
# | /home/buildbot/worker/as-builder-7/llvm-nvptx64-nvidia-ubuntu/llvm-project/llvm/test/MC/MachO/invalid-section-index.s:5:16: error: MACHOERROR: expected string not found in input
# | // MACHOERROR: error: Too many sections!
# |                ^
# | <stdin>:1:1: note: scanning from here
# | /home/buildbot/worker/as-builder-7/llvm-nvptx64-nvidia-ubuntu/build/bin/llvm-mc: error: unable to get target for 'arm64-apple-macos', see --version and --triple.
# | ^
# | <stdin>:1:82: note: possible intended match here
# | /home/buildbot/worker/as-builder-7/llvm-nvptx64-nvidia-ubuntu/build/bin/llvm-mc: error: unable to get target for 'arm64-apple-macos', see --version and --triple.
# |                                                                                  ^
# | 
# | Input file: <stdin>
# | Check file: /home/buildbot/worker/as-builder-7/llvm-nvptx64-nvidia-ubuntu/llvm-project/llvm/test/MC/MachO/invalid-section-index.s
# | 
# | -dump-input=help explains the following input dump.
# | 
# | Input was:
# | <<<<<<
# |            1: /home/buildbot/worker/as-builder-7/llvm-nvptx64-nvidia-ubuntu/build/bin/llvm-mc: error: unable to get target for 'arm64-apple-macos', see --version and --triple.
# | check:5'0     X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
# | check:5'1                                                                                      ?                                                                                possible intended match
# | >>>>>>
# `-----------------------------
# error: command failed with exit status: 1

--

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


@llvm-ci
Copy link
Collaborator

llvm-ci commented Nov 12, 2025

LLVM Buildbot has detected a new failure on builder llvm-nvptx-nvidia-ubuntu running on as-builder-7 while building llvm at step 6 "test-build-unified-tree-check-llvm".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/180/builds/28242

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-llvm) failure: test (failure)
******************** TEST 'LLVM :: MC/MachO/invalid-section-index.s' FAILED ********************
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 3
not /home/buildbot/worker/as-builder-7/llvm-nvptx-nvidia-ubuntu/build/bin/llvm-mc -filetype=obj -triple arm64-apple-macos /home/buildbot/worker/as-builder-7/llvm-nvptx-nvidia-ubuntu/llvm-project/llvm/test/MC/MachO/invalid-section-index.s -o - 2>&1 | /home/buildbot/worker/as-builder-7/llvm-nvptx-nvidia-ubuntu/build/bin/FileCheck /home/buildbot/worker/as-builder-7/llvm-nvptx-nvidia-ubuntu/llvm-project/llvm/test/MC/MachO/invalid-section-index.s --check-prefix=MACHOERROR
# executed command: not /home/buildbot/worker/as-builder-7/llvm-nvptx-nvidia-ubuntu/build/bin/llvm-mc -filetype=obj -triple arm64-apple-macos /home/buildbot/worker/as-builder-7/llvm-nvptx-nvidia-ubuntu/llvm-project/llvm/test/MC/MachO/invalid-section-index.s -o -
# executed command: /home/buildbot/worker/as-builder-7/llvm-nvptx-nvidia-ubuntu/build/bin/FileCheck /home/buildbot/worker/as-builder-7/llvm-nvptx-nvidia-ubuntu/llvm-project/llvm/test/MC/MachO/invalid-section-index.s --check-prefix=MACHOERROR
# .---command stderr------------
# | /home/buildbot/worker/as-builder-7/llvm-nvptx-nvidia-ubuntu/llvm-project/llvm/test/MC/MachO/invalid-section-index.s:5:16: error: MACHOERROR: expected string not found in input
# | // MACHOERROR: error: Too many sections!
# |                ^
# | <stdin>:1:1: note: scanning from here
# | /home/buildbot/worker/as-builder-7/llvm-nvptx-nvidia-ubuntu/build/bin/llvm-mc: error: unable to get target for 'arm64-apple-macos', see --version and --triple.
# | ^
# | <stdin>:1:80: note: possible intended match here
# | /home/buildbot/worker/as-builder-7/llvm-nvptx-nvidia-ubuntu/build/bin/llvm-mc: error: unable to get target for 'arm64-apple-macos', see --version and --triple.
# |                                                                                ^
# | 
# | Input file: <stdin>
# | Check file: /home/buildbot/worker/as-builder-7/llvm-nvptx-nvidia-ubuntu/llvm-project/llvm/test/MC/MachO/invalid-section-index.s
# | 
# | -dump-input=help explains the following input dump.
# | 
# | Input was:
# | <<<<<<
# |            1: /home/buildbot/worker/as-builder-7/llvm-nvptx-nvidia-ubuntu/build/bin/llvm-mc: error: unable to get target for 'arm64-apple-macos', see --version and --triple.
# | check:5'0     X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
# | check:5'1                                                                                    ?                                                                                possible intended match
# | >>>>>>
# `-----------------------------
# error: command failed with exit status: 1

--

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


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:mc Machine (object) code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants