Skip to content

fix(foundation): use HW_PHYSMEM for physical memory detection on FreeBSD - #1093

Merged
DeusData merged 3 commits into
DeusData:mainfrom
PR9000:freebsd-hw-physmem
Jul 16, 2026
Merged

DeusData merged 3 commits into
DeusData:mainfrom
PR9000:freebsd-hw-physmem

Conversation

@PR9000

@PR9000 PR9000 commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

This PR fixes physical memory detection on FreeBSD by using the standard HW_PHYSMEM sysctl in detect_system_bsd().

On FreeBSD 15.1-STABLE, the current implementation using HW_PHYSMEM64 does not return the expected physical memory size during system initialization. This change routes FreeBSD through the existing HW_PHYSMEM code path already used by OpenBSD.

Changes

  • Updated src/foundation/system_info.c.
  • Extended the BSD-specific condition:
#if defined(__OpenBSD__)

to:

#if defined(__OpenBSD__) || defined(__FreeBSD__)

so FreeBSD uses HW_PHYSMEM for physical memory detection.

Testing

Tested on FreeBSD 15.1-STABLE (amd64).

Validation performed:

  • Successfully built codebase-memory-mcp using:
gmake -f Makefile.cbm cbm
  • Successfully ran the CI linters:
gmake -f Makefile.cbm lint-ci CLANG_FORMAT=clang-format21

Result:

=== cppcheck ===
=== clang-format ===
=== NOLINT check ===
=== CI linters passed ===
  • Verified that the detected physical memory size is reported correctly after this change.

The test suite was also executed:

gmake -f Makefile.cbm test

However, the test build currently fails during linking with an unrelated AddressSanitizer duplicate symbol error:

duplicate symbol: bsearch
>>> defined at asan_interceptors.cpp
>>> defined at internal/cbm/vendored/grammars/perl/bsearch.h

This failure is independent from this change and occurs in the test infrastructure/linking stage before the tests are executed.

Why this is safe

This is a small, localized, platform-specific change affecting only the FreeBSD BSD implementation path.

Linux, macOS, Windows, and other supported platforms are unaffected. The change reuses the existing HW_PHYSMEM implementation already used for OpenBSD and does not modify the behavior of other operating systems.

Checklist

  • Every commit is signed off (git commit -s) — required, CI rejects unsigned commits ([DCO](../DCO), see [CONTRIBUTING.md](../CONTRIBUTING.md))
  • Tests pass locally (make -f Makefile.cbm test)
  • Lint passes (make -f Makefile.cbm lint-ci)
  • New behavior is covered by a test (reproduce-first for bug fixes)

@PR9000
PR9000 requested a review from DeusData as a code owner July 15, 2026 00:48
@DeusData DeusData added bug Something isn't working stability/performance Server crashes, OOM, hangs, high CPU/memory priority/high Needs near-term maintainer attention; high-impact bug, regression, safety issue, or release blocker. labels Jul 15, 2026
@DeusData DeusData added this to the 0.9.1-rc milestone Jul 15, 2026
@DeusData

Copy link
Copy Markdown
Owner

Thanks for the focused FreeBSD fix and the on-platform validation. FreeBSD's current sys/sysctl.h defines HW_PHYSMEM for this MIB and not HW_PHYSMEM64, so the direction is correct. I have triaged this as a high-priority 0.9.1-rc stability bug. The one-line diff adds no dependency, network, secret, or broader runtime surface.

The remaining blocker is the repository's reproduce-first requirement: the PR currently has no automated guard that fails on main. Because the normal matrix has no FreeBSD runner, please add the smallest testable regression or propose a narrow test seam for the MIB-selection contract before changing CI. Do not add a new workflow or dependency without design approval. The unrelated Perl/ASan duplicate-symbol problem should stay outside this PR.

@PR9000

PR9000 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

I've updated the PR with a regression test for cbm_system_info() (platform_system_info).

The original issue is a compile-time regression on FreeBSD (HW_PHYSMEM64 undeclared), so the unpatched tree cannot execute the test suite. With the fix applied, the new test verifies that cbm_system_info() returns valid CPU and RAM information and helps guard against future regressions in this code path.

Here is the test suite output running and passing successfully on FreeBSD:

  codebase-memory-mcp  C test suite

=== platform ===
  platform_now_ns                                        PASS
  platform_now_ms                                        PASS
  platform_nprocs                                        PASS
  platform_file_exists                                   PASS
  platform_is_dir                                        PASS
  platform_file_size                                     PASS
  platform_mmap                                          PASS
  platform_mmap_nonexistent                              PASS
  platform_default_workers_env_override                  PASS
  platform_default_workers_env_invalid                   PASS
  platform_default_workers_env_unset                     PASS
  platform_system_info                                   PASS

────────────────────────────────────────────
  12 passed
────────────────────────────────────────────

@PR9000
PR9000 force-pushed the freebsd-hw-physmem branch from de6c1af to 62808da Compare July 16, 2026 03:10
@PR9000

PR9000 commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor Author

I've updated the PR with an optional, non-intrusive production hardening option in Makefile.cbm (FREEBSD_HARDENING ?= 0) to support hardened downstream packaging (for example, FreeBSD Ports).

When enabled via FREEBSD_HARDENING=1, production builds (cbm and cbm-with-ui) receive:

  • Stack protection: -fstack-protector-strong
  • Fortify source definition: -D_FORTIFY_SOURCE=2
  • Full RELRO and BIND_NOW: -Wl,-z,relro -Wl,-z,now

The implementation keeps upstream impact minimal:

  • It is strictly opt-in (default is 0), so existing builds, CI pipelines, and developer workflows are unchanged.
  • It only affects production targets through CFLAGS_PROD, CXXFLAGS_PROD, and LDFLAGS.
  • Test builds remain unchanged, avoiding interaction with ASan/UBSan/TSan configurations.
  • The hardening flags are separated into generic HARDEN_CFLAGS and HARDEN_LDFLAGS variables to keep the build logic portable.

I verified this locally on FreeBSD 15.1:

gmake -f Makefile.cbm clean-c
gmake -f Makefile.cbm cbm FREEBSD_HARDENING=1

The resulting ELF binary was verified with:

readelf -d build/c/codebase-memory-mcp | grep BIND_NOW

Output:

0x000000000000001e (FLAGS)        BIND_NOW

and:

readelf -l build/c/codebase-memory-mcp | grep GNU_RELRO

Output:

GNU_RELRO      0x117164e0 0x00000000119184e0 0x00000000119184e0 0x000b40 0x001b20 R   0x1

The same option was also tested successfully with:

gmake -f Makefile.cbm cbm-with-ui FREEBSD_HARDENING=1

@DeusData
DeusData merged commit a688d85 into DeusData:main Jul 16, 2026
20 checks passed
DeusData added a commit that referenced this pull request Nov 4, 2026
Add .github/workflows/freebsd.yml, a standalone workflow triggered only
by workflow_dispatch. It builds the UI-embedded binary inside FreeBSD
15.1 and 14.4 x86_64 VMs (vmactions/freebsd-vm, full-SHA pin to v1.5.9)
with the canonical scripts/build.sh and runs the binary with --version.
It declares read-only permissions, uploads and publishes nothing, is not
part of ci-ok and is not called by release.yml or dry-run.yml, so a
FreeBSD red never gates a pull request or a release.

scripts/env.sh now detects FreeBSD and selects gmake there (BSD make
cannot read the GNU make syntax of Makefile.cbm) and make everywhere
else; the environment summary line reports the choice. scripts/build.sh
runs $MAKE at all three build call sites. macOS, Linux and Windows still
run make, so their builds are unchanged.

Distilled from #1422 by PR9000: the release matrix, the x86_64-host
choice, the aarch64 exclusion, the vmactions VM approach with a SHA pin,
fail-fast: false and the gmake detection all come from that pull
request, which built on the FreeBSD groundwork of #1093, #1423 and #1467.

Co-authored-by: Pedro Ramos <[email protected]>
Signed-off-by: Martin Vogel <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working priority/high Needs near-term maintainer attention; high-impact bug, regression, safety issue, or release blocker. stability/performance Server crashes, OOM, hangs, high CPU/memory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants