Skip to content

Fix: ensure that linux binaries have 8mb stack size (musl)#563

Merged
dimitris-m merged 2 commits intomainfrom
dm/better-print-thread-safety
Feb 4, 2026
Merged

Fix: ensure that linux binaries have 8mb stack size (musl)#563
dimitris-m merged 2 commits intomainfrom
dm/better-print-thread-safety

Conversation

@dimitris-m
Copy link
Collaborator

@dimitris-m dimitris-m commented Feb 2, 2026

Opengrep Core Segfault (-11) - Root Cause and Fix

Opengrep Version: 1.15.1

Summary

The opengrep-core exited with -11 (SIGSEGV) crash on Linux with multiple domains (-j > 1) was caused by musl libc's 128KB default pthread stack size, which is insufficient for PCRE's recursive pattern matching used by aliengrep rules.

Root Cause

musl libc defaults to 128KB pthread stack size, compared to glibc's 8MB. When opengrep runs complex aliengrep patterns with PCRE's recursive matching across multiple domains, the 128KB stack overflows.

From musl libc documentation:

"Thread stack size is not determined by RLIMIT_STACK on musl; the default is approximately 128KB."

The crash manifests as:

SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_ACCERR, si_addr=0x7fffeb378ee8}

SEGV_ACCERR with a stack-region address indicates hitting the stack guard page (stack overflow).

Fix

Added -Wl,-z,stack-size=8388608 linker flag to set 8MB default pthread stack size in src/main/flags.sh:

# In the Alpine Linux case (lines 62-76):
linux)
    if [[ -e /etc/alpine-release ]]; then
        # -Wl,-z,stack-size=8388608: Set default pthread stack size to 8MB.
        # musl libc defaults to only 128KB which causes stack overflow in
        # PCRE's recursive pattern matching (used by aliengrep).
        # See: https://wiki.musl-libc.org/functional-differences-from-glibc.html
        FLAGS=()
        CCLIB=("-lidn2" "-lunistring" "-lpsl" "-lssl" "-lcrypto" "-lz")
        CCOPT=("-static" "-no-pie" "-Wl,-z,stack-size=8388608")

This linker flag sets the PT_GNU_STACK program header in the ELF binary, which musl reads to determine the default pthread stack size.

Verification

Proprietary rules were used.

Raspberry Pi 4 (aarch64, native Linux)

Binary Stack Size Result
OLD 128KB (default) 100% crash rate
FIXED 8MB 50/50 success (0% crash)

Docker on Mac (x86_64, AWS Lambda-like)

Binary Stack Size Aliengrep Rules Full Rules
OLD 128KB (default) 1/10 success (90% crash) -
FIXED 8MB 10/10 success (0% crash) 10/10 success (0% crash)

Binary Verification

# OLD binary - no stack size set (uses 128KB default)
$ readelf -l opengrep-cli | grep -A1 GNU_STACK
  GNU_STACK      0x0 0x0 0x0
                 0x0 0x0  RW     0x10

# FIXED binary - 8MB stack size
$ readelf -l opengrep-cli-fixed | grep -A1 GNU_STACK
  GNU_STACK      0x0 0x0 0x0
                 0x0 0x800000  RW     0x10
                     ^^^^^^^^ = 8,388,608 bytes = 8MB

Affected Environments

All our Linux binaries are affected and these are commonly used in:

  • AWS Lambda
  • Docker containers
  • Kubernetes pods

This may explain issues like #204.

Not affected:

  • macOS
  • Windows
  • Single-threaded execution (-j 1)

Why Aliengrep Specifically?

Aliengrep uses complex PCRE patterns with:

  • (?(DEFINE)...) blocks for named subroutines
  • Recursive pattern references (?&name)
  • Deep backtracking

These features cause PCRE to use significant stack space during matching. The 128KB musl default is insufficient, while 8MB provides adequate headroom.

References

@dimitris-m dimitris-m force-pushed the dm/better-print-thread-safety branch from 81633f0 to 7a691f4 Compare February 3, 2026 21:32
@dimitris-m dimitris-m changed the title WIP: Ensure that console printing is thread safe Fix: ensure that linux binaries have 8mb stack size (musl) Feb 3, 2026
@dimitris-m dimitris-m force-pushed the dm/better-print-thread-safety branch from 7a691f4 to 55ac586 Compare February 4, 2026 00:16
@dimitris-m dimitris-m merged commit bb32e0b into main Feb 4, 2026
1 check passed
@dimitris-m dimitris-m deleted the dm/better-print-thread-safety branch February 4, 2026 00:17
@maciejpirog maciejpirog mentioned this pull request Feb 4, 2026
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Feb 15, 2026
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [opengrep/opengrep](https://github.com/opengrep/opengrep) | minor | `v1.15.1` → `v1.16.0` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>opengrep/opengrep (opengrep/opengrep)</summary>

### [`v1.16.0`](https://github.com/opengrep/opengrep/releases/tag/v1.16.0): Opengrep 1.16.0

[Compare Source](opengrep/opengrep@v1.15.1...v1.16.0)

#### Improvements

- Dart: Add typed metavariabless by [@&#8203;maciejpirog](https://github.com/maciejpirog) in [#&#8203;551](opengrep/opengrep#551)
- Dart: Use case of identifier to guess call vs new by [@&#8203;maciejpirog](https://github.com/maciejpirog) in [#&#8203;555](opengrep/opengrep#555)
- Go: Enable goroutines in taint tracking by [@&#8203;maciejpirog](https://github.com/maciejpirog) in [#&#8203;559](opengrep/opengrep#559)
- Add taint propagation via "for" comprehensions by [@&#8203;maciejpirog](https://github.com/maciejpirog) in [#&#8203;564](opengrep/opengrep#564)

#### Bug Fixes

- Rust: Missing Rust type alias translation by [@&#8203;smith-xyz](https://github.com/smith-xyz) in [#&#8203;549](opengrep/opengrep#549)
- Fix: Ensure that linux binaries have 8mb stack size (musl) by [@&#8203;dimitris-m](https://github.com/dimitris-m) in [#&#8203;563](opengrep/opengrep#563)
- Fixed a perf regression by removing system calls and improving the reachability graph and the callee lookup by [@&#8203;corneliuhoffman](https://github.com/corneliuhoffman) in [#&#8203;556](opengrep/opengrep#556)
- Fixed intrafile bug introduced by a superfluous fallback by [@&#8203;corneliuhoffman](https://github.com/corneliuhoffman) in [#&#8203;567](opengrep/opengrep#567)
- Ruby: Always translate `or` and `and` to expression by [@&#8203;maciejpirog](https://github.com/maciejpirog) in [#&#8203;562](opengrep/opengrep#562)
- Bash: Allow redirects before command arguments by [@&#8203;maciejpirog](https://github.com/maciejpirog) in [#&#8203;548](opengrep/opengrep#548)

#### Internal Improvements

- Add `show dump-intrafile-graph` and `show dump-taint-signatures` commands by [@&#8203;corneliuhoffman](https://github.com/corneliuhoffman) in [#&#8203;552](opengrep/opengrep#552)
- Improve tainting code by [@&#8203;maciejpirog](https://github.com/maciejpirog) in [#&#8203;546](opengrep/opengrep#546)
- Graph refactoring by [@&#8203;corneliuhoffman](https://github.com/corneliuhoffman) in [#&#8203;553](opengrep/opengrep#553)

#### New Contributors

- [@&#8203;smith-xyz](https://github.com/smith-xyz) made their first contribution in [#&#8203;549](opengrep/opengrep#549)

**Full Changelog**: <opengrep/opengrep@v1.15.1...v1.16.0>

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0Mi45Ni4yIiwidXBkYXRlZEluVmVyIjoiNDIuOTYuMiIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90IiwiYXV0b21hdGlvbjpib3QtYXV0aG9yZWQiLCJkZXBlbmRlbmN5LXR5cGU6Om1pbm9yIl19-->
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

Successfully merging this pull request may close these issues.

2 participants