Files
meshcore-analyzer/Makefile
T
0af70cf2c1 fix(build): pin musl thread stacks at 8 MiB and check the artifacts (#2410)
## Summary

Pins musl thread stacks at 8 MiB on every link, and fixes a crash in
**native Alpine builds** (`make build`, `make test`, or `go test` on an
Alpine host).

**The released images are not affected.** An earlier version of this
description said public endpoints could crash the shipped image. That
was wrong, and review caught it: see "Who is affected".

## Root cause

- musl sizes every non-main thread's stack from the ELF `PT_GNU_STACK`
size, with a floor of 128 KiB and a cap of 8 MiB (glibc defaults to 8
MiB).
- cgo calls run on the calling thread's stack, and SQLite's
`multiSelect` recurses about 850 bytes per `UNION ALL` term.
- `lookupPrefixes` (up to 400 terms) and `sharedEightBytePrefixes` (up
to 450 terms) overflow a 128 KiB stack and SIGSEGV the whole process.

## Who is affected

| Link | `PT_GNU_STACK` without the flag | Thread stack | Wide-UNION
tests |
|---|---|---|---|
| Alpine gcc + GNU ld (`make build` / `make test` / `go test` on Alpine)
| `0` | 128 KiB | **SIGSEGV** |
| zig 0.16.0, x86_64-linux-musl (Dockerfile, `crossbuild`, release job)
| `0x1000000` (16 MiB) | 8 MiB | pass 3/3 |
| zig 0.16.0, aarch64-linux-musl | `0x1000000` (16 MiB) | 8 MiB | pass
3/3, run on an arm64 node |

So for the released images the flag pins a value that zig's default
already provides. It stops being a property of a linker default, and a
build check now fails if it is lost. For native Alpine links it is the
fix: with the flag those tests pass.

Control: a zig arm64 link with `-Wl,-z,stack-size=131072` gets 128 KiB
threads and the same tests SIGSEGV in cgo on the arm64 node.

## Fix

- Every musl link passes `-Wl,-z,stack-size=8388608`: the `Dockerfile`,
the `Makefile` (`build` and `test` on Linux, and `crossbuild`) and the
release job in `deploy.yml`.
- This reserves address space only. Memory is committed as pages are
touched.
- **The artifacts are checked.** The Dockerfile's builder stage and the
release job's "Verify release binaries" step read `PT_GNU_STACK` with
`readelf` and fail unless it is exactly 8 MiB. Verified: the builder
stage passes for `linux/amd64` and `linux/arm64`, and fails with
`--build-arg STACK_LDFLAG=` (`/corescope-server: PT_GNU_STACK is not 8
MiB`, memsz `0x1000000`).

## Testing

- `TestMuslThreadStackWideUnionQueries`
(`cmd/server/musl_thread_stack_linux_test.go`, Linux only) runs both
queries off the main thread and asserts exact results, so a failed chunk
cannot pass as "nothing matched". Under `golang:1.27-alpine` with gcc:
SIGSEGV without the flag, pass with it. On CI's glibc runners it passes
either way, which is why the artifact checks above exist.
- `tests/unit/test-issue-1956-release-routing.js`: the release verify
step must check both binaries, and a `0x000000` or `0x1000000` header
must fail the release.
- `tests/unit/test-dockerfile-buildplatform.js`: all three `go build`
lines carry `STACK_LDFLAG`, and the `readelf` check covers all three
binaries.
- `sh test-all.sh` passes. `go vet` passes for `cmd/server` on Linux
with gofmt clean.
- Not verified: a Windows `go vet`. The fix there is the
`_linux_test.go` file name; no Windows machine was available to run it.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---

## Merge order

These four PRs were split out of #2396 and are **independent — they can
merge in any order.** Verified: every pair is conflict-free, and all
four merged together pass `go vet`, `go test ./...` (all 16 modules) and
`sh test-all.sh`. (That combined run predates the review fixes on #2409
and #2410.)

Suggested order, none of it urgent:

1. **#2408** — never lose rows during startup / a failed poll.
2. **#2407** — explicit `observers.idx` primary key.
3. **#2409** — content-hash migration moved into the ingestor.
4. **#2410** — pin musl thread stacks at 8 MiB. Released images are not
affected; it fixes native Alpine builds.

After these land, #2396 (Postgres) will be rebased onto master.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: efiten <erwin.fiten@gmail.com>
2026-11-18 18:35:41 +01:00

189 lines
8.1 KiB
Makefile

# corescope build entry point.
#
# This repo is 16 Go modules wired with `replace ../../internal/*` and no
# go.work, so every recipe changes into its module. The build flags live here
# because the SQLite driver is cgo now (github.com/mattn/go-sqlite3) and the
# flags stopped being something you can safely retype at each call site.
#
# Cross-compilation uses `zig cc` as the C toolchain, targeting musl so the
# binaries are fully static and run on the alpine runtime image (or scratch)
# with no libc dependency at all.
#
# Quick reference:
# make build # all four binaries for the host
# make build-server # just one
# make crossbuild # linux/amd64 + linux/arm64, static, into dist/
# make test / vet / fmt-check # across all modules
# make docker-build # multi-arch image via buildx
# make web API_BASE=… AUTH=… # the UI alone, for a static host, into dist/web
# make dev-split # UI on :8788 calling the API on :3000 cross-origin
GO ?= $(shell command -v go)
GIT_VERSION ?= $(shell git describe --tags --match "v*" 2>/dev/null || echo unknown)
GIT_COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
BUILD_TIME ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
GOENV_GOOS := $(shell $(GO) env GOOS)
GOENV_GOARCH := $(shell $(GO) env GOARCH)
GOOS ?= $(GOENV_GOOS)
GOARCH ?= $(GOENV_GOARCH)
# Exported: a bare make variable never reaches the go toolchain.
export CGO_ENABLED ?= 1
# netgo/osusergo keep the pure-Go resolver and user lookup the binaries had
# under CGO_ENABLED=0, so enabling cgo for SQLite does not quietly switch DNS
# resolution to the C resolver. sqlite_omit_load_extension drops the dlopen
# path, which is what lets -extldflags -static link cleanly. -Wl,-s on the
# cross builds strips the musl objects zig links in: Go's own -s -w does not
# reach them, and they account for well over half the binary.
GO_BUILD_TAGS ?= netgo,osusergo,sqlite_omit_load_extension
GO_BUILD_FLAGS ?= -trimpath
GO_LDFLAGS_OPTIMS ?= -s -w
# musl sizes every non-main thread's stack from the ELF PT_GNU_STACK size
# (floor 128 KiB, cap 8 MiB). cgo calls into SQLite run on the calling thread's
# stack, and a deep query (multiSelect recursing ~850 B per UNION ALL term)
# overflows 128 KiB and SIGSEGVs the process.
# - Native musl links (`make build` / `make test` on Alpine: gcc + GNU ld)
# write a zero PT_GNU_STACK and get the 128 KiB floor. The flag is the fix.
# - zig links (crossbuild, the Dockerfile, the release job) already write
# 16 MiB, so those binaries had 8 MiB threads without it. There the flag
# pins the size instead of leaving it to a linker default.
# Reserved address space, not resident memory. Go rejects this flag in
# #cgo LDFLAGS, so it must go in -extldflags. Harmless on glibc; GNU ld/lld
# only, so the host build and `make test` apply it on Linux only.
STACK_LDFLAG ?= -Wl,-z,stack-size=8388608
ifeq ($(GOOS),linux)
HOST_EXTLDFLAGS := -extldflags '$(STACK_LDFLAG)'
HOST_TEST_LDFLAGS := -ldflags=-extldflags=$(STACK_LDFLAG)
endif
CMDS := server ingestor decrypt migrate
DIST := dist
# Per-binary version stamping. Only these two read build metadata; ingestor and
# migrate take none. Kept as make variables so crossbuild and the host build
# cannot drift apart.
LDFLAGS_server := -X main.Version=$(GIT_VERSION) -X main.Commit=$(GIT_COMMIT) -X main.BuildTime=$(BUILD_TIME)
LDFLAGS_ingestor :=
LDFLAGS_decrypt := -X main.version=$(GIT_VERSION)
LDFLAGS_migrate :=
# zig target triples for the platforms we ship. musl, so the result is static.
ZIG_TARGET_linux_amd64 := x86_64-linux-musl
ZIG_TARGET_linux_arm64 := aarch64-linux-musl
CROSS_PLATFORMS := linux/amd64 linux/arm64
DOCKER_IMAGE ?= ghcr.io/kpa-clawbot/corescope
DOCKER_TAG ?= $(GIT_VERSION)
DOCKER_PLATFORMS ?= linux/amd64,linux/arm64
.PHONY: all build crossbuild test vet fmt-check tidy clean web dev-split docker-build docker-push help
all: build
help:
@echo "targets: build crossbuild test vet fmt-check tidy clean web dev-split docker-build docker-push"
@echo " build-{$(shell echo $(CMDS) | tr ' ' ',')}"
# -- host builds ---------------------------------------------------------------
build: $(addprefix build-,$(CMDS))
build-%:
@mkdir -p $(DIST)
cd cmd/$* && GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO) build \
-tags $(GO_BUILD_TAGS) $(GO_BUILD_FLAGS) \
-ldflags "$(GO_LDFLAGS_OPTIMS) $(HOST_EXTLDFLAGS) $(LDFLAGS_$*)" \
-o ../../$(DIST)/corescope-$* .
# -- cross builds --------------------------------------------------------------
#
# The host build above deliberately leaves CC alone: native builds, `go vet` and
# `-race` must use the host compiler. Only these recipes hand the build to zig.
crossbuild: $(foreach p,$(CROSS_PLATFORMS),$(foreach c,$(CMDS),crossbuild-$(c)-$(subst /,-,$(p))))
define CROSSBUILD_RULE
.PHONY: crossbuild-$(2)-$(3)-$(4)
crossbuild-$(2)-$(3)-$(4):
@mkdir -p $$(DIST)
@command -v zig >/dev/null || { echo "zig not found: needed to cross-compile cgo. See https://ziglang.org/download/"; exit 1; }
cd cmd/$(2) && CGO_ENABLED=1 GOOS=$(3) GOARCH=$(4) CC="zig cc -target $(1)" \
$$(GO) build -tags $$(GO_BUILD_TAGS) $$(GO_BUILD_FLAGS) \
-ldflags '$$(GO_LDFLAGS_OPTIMS) -extldflags "-static -Wl,-s $$(STACK_LDFLAG)" $$(LDFLAGS_$(2))' \
-o ../../$$(DIST)/corescope-$(2)-$(3)-$(4) .
endef
$(foreach c,$(CMDS),\
$(eval $(call CROSSBUILD_RULE,$(ZIG_TARGET_linux_amd64),$(c),linux,amd64))\
$(eval $(call CROSSBUILD_RULE,$(ZIG_TARGET_linux_arm64),$(c),linux,arm64)))
# -- checks --------------------------------------------------------------------
test:
bash scripts/allmod.sh test $(HOST_TEST_LDFLAGS) ./...
vet:
bash scripts/allmod.sh vet ./...
fmt-check:
@unformatted=$$(gofmt -l $$(git ls-files '*.go')); \
if [ -n "$$unformatted" ]; then echo "gofmt required on:"; echo "$$unformatted"; exit 1; fi; \
echo "gofmt: clean"
tidy:
bash scripts/allmod.sh mod tidy
clean:
rm -rf $(DIST)
# -- static frontend ------------------------------------------------------------
#
# web packages public/ into dist/web for a static host or a CDN. It reads
# API_BASE, AUTH, CHANNEL, CLASSIC_URL and COMMIT; see scripts/static/build.sh.
# The Go binaries share dist/, which is why the UI gets a folder of its own.
web:
bash scripts/static/build.sh
# dev-split runs the UI the way a static host serves it: the anonymous
# build on :8788 calling an API-only server on :3000 cross-origin, with no
# proxy. The server change is the one env var, exactly as in production.
# DEV_SPLIT_DB=path/to.db picks the data (default: the E2E fixture); it runs on a
# migrated copy in dist/, so the original is never touched.
DEV_SPLIT_DB ?= test-fixtures/e2e-fixture.db
DEV_SPLIT_API_PORT ?= 3000
DEV_SPLIT_STATIC_PORT ?= 8788
dev-split: build-server build-migrate
cp $(DEV_SPLIT_DB) $(DIST)/dev-split.db
$(DIST)/corescope-migrate -db $(DIST)/dev-split.db
CHANNEL=dev COMMIT=dev AUTH=false \
API_BASE=http://localhost:$(DEV_SPLIT_API_PORT) CLASSIC_URL=http://localhost:$(DEV_SPLIT_API_PORT) \
bash scripts/static/build.sh
@echo "UI: http://localhost:$(DEV_SPLIT_STATIC_PORT) API: http://localhost:$(DEV_SPLIT_API_PORT) (Ctrl-C stops both)"
@trap 'kill 0' INT TERM EXIT; \
CORS_ALLOWED_ORIGINS=http://localhost:$(DEV_SPLIT_STATIC_PORT) \
$(DIST)/corescope-server -port $(DEV_SPLIT_API_PORT) -db $(DIST)/dev-split.db -public /nonexistent & \
node scripts/dev-static.mjs --root $(DIST)/web --port $(DEV_SPLIT_STATIC_PORT)
# -- docker --------------------------------------------------------------------
docker-build:
docker buildx build . \
--platform=$(DOCKER_PLATFORMS) \
--build-arg=APP_VERSION=$(GIT_VERSION) \
--build-arg=GIT_COMMIT=$(GIT_COMMIT) \
--build-arg=BUILD_TIME=$(BUILD_TIME) \
-t $(DOCKER_IMAGE):$(DOCKER_TAG)
docker-push: DOCKER_PUSH_FLAG := --push
docker-push:
docker buildx build . \
--platform=$(DOCKER_PLATFORMS) \
--build-arg=APP_VERSION=$(GIT_VERSION) \
--build-arg=GIT_COMMIT=$(GIT_COMMIT) \
--build-arg=BUILD_TIME=$(BUILD_TIME) \
-t $(DOCKER_IMAGE):$(DOCKER_TAG) $(DOCKER_PUSH_FLAG)