Skip to content

Add More Reliable OpenBSD Executable Path Solution#1243

Open
samuelvenable wants to merge 10 commits intoRedot-Engine:masterfrom
samuelvenable:patch-1
Open

Add More Reliable OpenBSD Executable Path Solution#1243
samuelvenable wants to merge 10 commits intoRedot-Engine:masterfrom
samuelvenable:patch-1

Conversation

@samuelvenable
Copy link
Copy Markdown
Contributor

@samuelvenable samuelvenable commented Apr 24, 2026

Summary by CodeRabbit

  • Bug Fixes
    • More reliable executable-path resolution on OpenBSD: inspects the running process to derive and validate candidate paths from argv[0], PATH entries, current directory and PWD, with an environment-variable fallback; logs a warning and falls back to the previous method if unresolved.
  • Chores
    • Improved OpenBSD build support by updating included system headers.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 24, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

OpenBSD's OS_Unix::get_executable_path() now uses kvm to read the process argv and file mappings, constructs and validates argv-, PATH-, PWD-, and _-derived candidates against filesystem metadata and kvm text entries, then falls back to the prior realpath-based resolution if needed.

Changes

Cohort / File(s) Summary
OpenBSD Executable Path Resolution
drivers/unix/os_unix.cpp
Replaces simple realpath resolution for OpenBSD with a kvm-backed resolver: opens kvm, reads process argv and file map, builds argv-derived candidates, validates executability via stat/access/realpath and confirms matches with kvm file/text entries. Adds OpenBSD-specific includes and fallback to _ env or previous realpath(OS::get_executable_path()) if unresolved.

Sequence Diagram(s)

sequenceDiagram
    participant Caller as OS_Unix::get_executable_path()
    participant KVM as kvm (kvm_open/getprocs/getargv/getfiles)
    participant FS as Filesystem (stat, access, realpath)
    participant Env as Environment (PATH, PWD, `_`)

    Caller->>KVM: open kvm, read process argv & file map
    KVM-->>Caller: argv[] and file entries
    loop for each argv candidate
        Caller->>FS: is_exe(candidate) -> stat/access/realpath
        FS-->>Caller: metadata & resolved path
        Caller->>KVM: verify candidate matches kvm text entry
        KVM-->>Caller: match? yes/no
        alt match & executable
            Caller->>Caller: return resolved path
        end
    end
    alt none matched
        Caller->>Env: iterate PATH (+ defaults, $HOME/bin)
        Env-->>Caller: PATH candidates
        Caller->>FS: is_exe checks
        FS-->>Caller: results
        alt found
            Caller->>Caller: return resolved path
        else
            Caller->>Env: try PWD/getcwd then `_`
            Env-->>Caller: candidates
            Caller->>FS: is_exe checks
            FS-->>Caller: results
            Caller->>Caller: return resolved path or fallback to realpath(OS::get_executable_path())
        end
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and clearly summarizes the main change: improving OpenBSD executable path resolution. It accurately reflects the primary objective of the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 6

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@drivers/unix/os_unix.cpp`:
- Around line 1199-1203: When kvm_openfiles or kvm_getargv fail (and whenever
all candidate realpath checks leave `path` empty) do not return an empty String;
instead WARN_PRINT a diagnostic and fall back to returning
OS::get_executable_path() (mirroring the FreeBSD/NetBSD branches). Update the
failure branch after kvm_openfiles (and the similar branch around the
kvm_getargv/candidate loop) to call WARN_PRINT(...) with context and then return
OS::get_executable_path() instead of returning an empty String, and ensure the
same fallback is applied at the other affected region (the branch covering the
candidate verification loop).
- Around line 1154-1277: This block uses STL types and goto-based flow; refactor
to Godot conventions by replacing std::string/ std::vector/ std::stringstream
with String/CharString/Vector<String> and splitting PATH with String::split;
extract the is_exe lambda into a static/file-local helper (e.g.
_openbsd_verify_exe) that encapsulates kvm_openfiles/kvm_getfiles/kvm_close and
returns a String, replace cppstr_getenv with a thin helper that returns String,
and eliminate goto retry/fallback by implementing a two-pass PATH search (env
PATH then built-in PATH) via a small helper (e.g. _openbsd_search_path) and an
explicit second-attempt for the "_" argv0 fallback instead of goto; update
usages in the current function to call these helpers and iterate Vector<String>
results.
- Around line 1159-1215: The OpenBSD-specific block around the
kvm_openfiles/kvm_getfiles usage (search for symbols kvm_openfiles,
kvm_getfiles, and the fallback: label / KERN_FILE_TEXT) is misformatted; run
your formatter (pre-commit run clang-format --files or clang-format -i) on the
changed file to fix clang-format violations and then stage/commit the
reformatted file so the CI pre-commit clang-format check passes.
- Around line 1274-1277: The return path conversion currently uses
String(path.c_str()) which calls the non-UTF-8 overload and will mangle
non-ASCII components; replace that with String::utf8(path) (matching the FreeBSD
branch's use of String::utf8(buf)) to preserve UTF-8 path data, and remove the
unnecessary errno = 0 assignment on the successful path so callers' errno isn't
altered; update the return site that constructs the String from the local
variable path accordingly.
- Around line 1169-1171: Replace the manual file-type test that uses (st.st_mode
& S_IFREG) with the POSIX macro S_ISREG(st.st_mode) in the conditional that
compares exe file attributes (the if that checks stat(exe.c_str(), &st),
executable bit, regular file, realpath and device/inode against
kif[i].va_fsid/va_fileid); update that expression to use S_ISREG(st.st_mode) so
the regular-file check is done portably and correctly.
- Around line 1163-1164: The loop currently assumes pseudo entries are all at
the start by using "for (int i = 0; i < cntp && kif[i].fd_fd < 0; i++)", which
is fragile; change the logic that scans the kvm_getfiles() result (kif array
from kvm_getfiles) to iterate over the entire range 0..cntp-1 and explicitly
check each entry's fd type (e.g. kif[i].fd_fd < 0 and compare kif[i].fd_type or
kif[i].fd_fd against known pseudo values such as KERN_FILE_TEXT, KERN_FILE_CDIR,
KERN_FILE_RDIR) and handle the text/pseudo entries when matched, breaking only
once the desired entry is found, instead of relying on contiguous ordering.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 86581f85-0b91-472d-90a8-cfa638e3033d

📥 Commits

Reviewing files that changed from the base of the PR and between 5f54c0d and b9b484f.

📒 Files selected for processing (1)
  • drivers/unix/os_unix.cpp

Comment thread drivers/unix/os_unix.cpp Outdated
Comment thread drivers/unix/os_unix.cpp Outdated
Comment thread drivers/unix/os_unix.cpp
Comment thread drivers/unix/os_unix.cpp Outdated
Comment thread drivers/unix/os_unix.cpp Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@drivers/unix/os_unix.cpp`:
- Around line 66-68: The build lacks linking of libkvm for OpenBSD which causes
unresolved symbols for kvm_openfiles, kvm_getprocs, kvm_getargv, kvm_getfiles,
and kvm_close; update the platform detection logic that currently only adds
"-lkvm" for FreeBSD (the condition around the variable that appends "-lkvm") to
also include OpenBSD so the linker flag is added when platform.system() is
"OpenBSD", ensuring libkvm is linked for the code paths in os_unix.cpp that
include <kvm.h>.
- Around line 1176-1184: The fallback that builds exe from kif[i].p_comm is
unsafe because kinfo_file::p_comm is truncated (KI_MAXCOMLEN); replace that
fallback in the block around exe/fallback with a robust resolution: first try to
obtain the full argv[0] via kvm_getargv(..., proc, 0) and use it if it’s an
absolute path and stat matches kif[i].va_fsid/kif[i].va_fileid, and if that
fails, scan the directory containing the original exe (use exe.substr(0,
last_slash_pos+1)) and stat each entry to find a file whose device/inode match
kif[i].va_fsid and kif[i].va_fileid; only use kif[i].p_comm as a last resort (or
drop it) so you don’t rely on the truncated p_comm string.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 7529962a-4d5e-4cb3-938b-a40cd9d2094a

📥 Commits

Reviewing files that changed from the base of the PR and between b9b484f and 028f019.

📒 Files selected for processing (1)
  • drivers/unix/os_unix.cpp

Comment thread drivers/unix/os_unix.cpp
Comment thread drivers/unix/os_unix.cpp
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@drivers/unix/os_unix.cpp`:
- Around line 1280-1287: Remove the errno reset, check realpath() return value
before using resolved_path, and construct a UTF-8 String on success;
specifically, in the fallback branch that calls
realpath(OS::get_executable_path().utf8().get_data(), resolved_path) verify the
return is non-null and only then return String::utf8(resolved_path), otherwise
handle the failure (e.g., return an empty String or the original path) and log
the error; apply the same fixes to the NetBSD branch that uses realpath and
resolved_path so both paths consistently use String::utf8 and do not clobber
errno.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: bdbd7bce-6b2d-440a-842a-3dd9b3626a5a

📥 Commits

Reviewing files that changed from the base of the PR and between 028f019 and af782e7.

📒 Files selected for processing (1)
  • drivers/unix/os_unix.cpp

Comment thread drivers/unix/os_unix.cpp Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@drivers/unix/os_unix.cpp`:
- Around line 1201-1205: The early return in OS_Unix::get_executable_path()
after failing kvm_openfiles() uses a std::string variable path and returns it
directly, causing a type mismatch with Godot's String return type and bypassing
the intended fallback; change the behavior to not return std::string there —
instead ensure you convert path.c_str() (or construct a Godot String) when
returning, or better, remove the early return and let the function fall through
to the existing tail fallback added in commit 6da2fe4 so callers like
open_dynamic_library() still receive the fallback path; specifically update the
kd/kvm_openfiles() failure branch (symbols: kd, kvm_openfiles, path,
OS_Unix::get_executable_path) to either construct and return a proper Godot
String or omit the return so the fallback code executes.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 1f0ba4db-2d34-4313-984c-eb0eaf1417db

📥 Commits

Reviewing files that changed from the base of the PR and between 06e5709 and 6da2fe4.

📒 Files selected for processing (1)
  • drivers/unix/os_unix.cpp

Comment thread drivers/unix/os_unix.cpp Outdated
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.

1 participant