Skip to content

Commit c61b380

Browse files
authored
Merge pull request DeusData#2449 from DeusData/fix/build-seams-and-cx-tmp
fix(build): make prod objects follow TEST_SEAMS; stop cx tmp leak
2 parents 0bed0d3 + 4c466fc commit c61b380

2 files changed

Lines changed: 71 additions & 50 deletions

File tree

‎Makefile.cbm‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -832,8 +832,14 @@ BUILD_DIR = build/c
832832
# ── Object file compilation (grammars need relaxed warnings) ─────
833833

834834
# Grammar + tree-sitter runtime: compiled without -Werror (upstream code has warnings)
835+
# $(TEST_SEAM_DEFINE): these prod objects (grammars, ts_runtime, lsp_all) are
836+
# linked into the same binary as the CFLAGS_PROD sources, so both halves must
837+
# agree on the seam switch. lsp_all holds seam code of its own (ts_lsp.c's
838+
# budget probes, any seam counter a resolver defines): without the define a
839+
# TEST_SEAMS=1 binary silently lacks those seams, and a seam symbol that cbm.c
840+
# references fails to link. GRAMMAR_CFLAGS_TEST always carries the define.
835841
GRAMMAR_CFLAGS = -std=c11 -D_DEFAULT_SOURCE -O2 -w -I$(CBM_DIR) -I$(TS_INCLUDE) -I$(TS_SRC) \
836-
$(MEMWASTE_VENDOR_CFLAGS)
842+
$(TEST_SEAM_DEFINE) $(MEMWASTE_VENDOR_CFLAGS)
837843
GRAMMAR_CFLAGS_TEST = -std=c11 -D_DEFAULT_SOURCE -g -O1 -w -I$(CBM_DIR) -I$(TS_INCLUDE) -I$(TS_SRC) \
838844
-DCBM_ENABLE_TEST_SEAMS=1 $(SANITIZED_DEFINE) $(SANITIZE)
839845
GRAMMAR_CFLAGS_TSAN = -std=c11 -D_DEFAULT_SOURCE -g -O1 -w -I$(CBM_DIR) -I$(TS_INCLUDE) -I$(TS_SRC) \
@@ -941,7 +947,9 @@ $(BUILD_DIR)/sqlite3.o: $(SQLITE3_SRC) | $(BUILD_DIR)
941947
$(BUILD_DIR)/tsan_sqlite3.o: $(SQLITE3_SRC) | $(BUILD_DIR)
942948
$(CC) $(SQLITE3_CFLAGS_TEST) $(TSAN_SANITIZE) -c -o $@ $<
943949

944-
$(BUILD_DIR)/prod_sqlite3.o: $(SQLITE3_SRC) Makefile.cbm | $(BUILD_DIR)
950+
# .build-config: SQLITE3_CFLAGS carries MEMWASTE_VENDOR_CFLAGS (see the stamp
951+
# under "Production binary").
952+
$(BUILD_DIR)/prod_sqlite3.o: $(SQLITE3_SRC) Makefile.cbm $(BUILD_DIR)/.build-config | $(BUILD_DIR)
945953
$(CC) $(SQLITE3_CFLAGS) -c -o $@ $<
946954

947955
# TRE regex (only compiled on Windows — POSIX uses system <regex.h>)
@@ -971,7 +979,7 @@ $(BUILD_DIR)/tre.o: $(TRE_SRC) | $(BUILD_DIR)
971979
$(BUILD_DIR)/tsan_tre.o: $(TRE_SRC) | $(BUILD_DIR)
972980
$(CC) $(TRE_CFLAGS) $(TSAN_SANITIZE) -c -o $@ $<
973981

974-
$(BUILD_DIR)/prod_tre.o: $(TRE_SRC) | $(BUILD_DIR)
982+
$(BUILD_DIR)/prod_tre.o: $(TRE_SRC) $(BUILD_DIR)/.build-config | $(BUILD_DIR)
975983
$(CC) $(TRE_CFLAGS) -O2 $(MEMWASTE_VENDOR_CFLAGS) -c -o $@ $<
976984
endif
977985

@@ -1125,28 +1133,35 @@ TS_RUNTIME_OBJ_PROD = $(BUILD_DIR)/prod_ts_runtime.o
11251133
LSP_OBJ_PROD = $(BUILD_DIR)/prod_lsp_all.o
11261134
PP_OBJ_PROD = $(BUILD_DIR)/prod_preprocessor.o
11271135

1128-
$(BUILD_DIR)/prod_%.o: $(CBM_DIR)/%.c $(GRAMMAR_UNITY_DEPS) | $(BUILD_DIR)
1136+
# Every prod object whose flags carry a BUILD_CONFIG_SIG variable
1137+
# (TEST_SEAM_DEFINE via GRAMMAR_CFLAGS, MEMWASTE_VENDOR_CFLAGS) depends on the
1138+
# $(BUILD_DIR)/.build-config stamp. These objects are compiled once and linked
1139+
# into every later binary, so without the stamp a TEST_SEAMS or MEMWASTE flip
1140+
# relinks the PREVIOUS flavour: a release binary keeps seam code compiled into
1141+
# lsp_all, or a seams binary misses it. prod_mimalloc.o and the unixcoder blob
1142+
# take no config variable and stay off the stamp.
1143+
$(BUILD_DIR)/prod_%.o: $(CBM_DIR)/%.c $(GRAMMAR_UNITY_DEPS) $(BUILD_DIR)/.build-config | $(BUILD_DIR)
11291144
$(CC) $(GRAMMAR_CFLAGS) -c -o $@ $<
11301145

1131-
$(BUILD_DIR)/prod_ts_runtime.o: $(TS_RUNTIME_DEPS) | $(BUILD_DIR)
1146+
$(BUILD_DIR)/prod_ts_runtime.o: $(TS_RUNTIME_DEPS) $(BUILD_DIR)/.build-config | $(BUILD_DIR)
11321147
$(CC) $(GRAMMAR_CFLAGS) -c -o $@ $<
11331148

1134-
$(BUILD_DIR)/prod_lsp_all.o: $(LSP_UNITY_DEPS) | $(BUILD_DIR)
1149+
$(BUILD_DIR)/prod_lsp_all.o: $(LSP_UNITY_DEPS) $(BUILD_DIR)/.build-config | $(BUILD_DIR)
11351150
$(CC) $(GRAMMAR_CFLAGS) -c -o $@ $<
11361151

1137-
$(BUILD_DIR)/prod_preprocessor.o: $(CBM_DIR)/preprocessor.cpp | $(BUILD_DIR)
1152+
$(BUILD_DIR)/prod_preprocessor.o: $(CBM_DIR)/preprocessor.cpp $(BUILD_DIR)/.build-config | $(BUILD_DIR)
11381153
$(CXX) $(CXXFLAGS_PROD) -w -I$(CBM_DIR)/vendored -c -o $@ $<
11391154

11401155
# Vendored LZ4 (compiled separately, not unity-built via lz4_store.c)
11411156
LZ4_OBJ_PROD = $(BUILD_DIR)/prod_lz4.o $(BUILD_DIR)/prod_lz4hc.o
1142-
$(BUILD_DIR)/prod_lz4.o: $(CBM_DIR)/vendored/lz4/lz4.c | $(BUILD_DIR)
1157+
$(BUILD_DIR)/prod_lz4.o: $(CBM_DIR)/vendored/lz4/lz4.c $(BUILD_DIR)/.build-config | $(BUILD_DIR)
11431158
$(CC) -std=c11 -D_DEFAULT_SOURCE -O2 -w $(MEMWASTE_VENDOR_CFLAGS) -I$(CBM_DIR) -c -o $@ $<
1144-
$(BUILD_DIR)/prod_lz4hc.o: $(CBM_DIR)/vendored/lz4/lz4hc.c | $(BUILD_DIR)
1159+
$(BUILD_DIR)/prod_lz4hc.o: $(CBM_DIR)/vendored/lz4/lz4hc.c $(BUILD_DIR)/.build-config | $(BUILD_DIR)
11451160
$(CC) -std=c11 -D_DEFAULT_SOURCE -O2 -w $(MEMWASTE_VENDOR_CFLAGS) -I$(CBM_DIR)/vendored/lz4 -c -o $@ $<
11461161

11471162
# Vendored zstd (compiled separately, not unity-built via zstd_store.c)
11481163
ZSTD_OBJ_PROD = $(BUILD_DIR)/prod_zstd.o
1149-
$(BUILD_DIR)/prod_zstd.o: $(CBM_DIR)/vendored/zstd/zstd.c | $(BUILD_DIR)
1164+
$(BUILD_DIR)/prod_zstd.o: $(CBM_DIR)/vendored/zstd/zstd.c $(BUILD_DIR)/.build-config | $(BUILD_DIR)
11501165
$(CC) -std=c11 -D_DEFAULT_SOURCE -O2 -w $(MEMWASTE_VENDOR_CFLAGS) -I$(CBM_DIR)/vendored/zstd -c -o $@ $<
11511166

11521167
OBJS_VENDORED_PROD = $(MIMALLOC_OBJ_PROD) $(SQLITE3_OBJ_PROD) $(TRE_OBJ_PROD) $(GRAMMAR_OBJS_PROD) $(TS_RUNTIME_OBJ_PROD) $(LSP_OBJ_PROD) $(PP_OBJ_PROD) $(LZ4_OBJ_PROD) $(ZSTD_OBJ_PROD) $(UNIXCODER_OBJ)
@@ -1162,7 +1177,8 @@ MAIN_SRC = src/main.c
11621177
# configuration and it is rewritten only when that content changes, so ordinary
11631178
# incremental builds are unaffected. (Single-quoted: the signature may contain
11641179
# the escaped quotes of -DCBM_VERSION, which are literal inside '', but must not
1165-
# contain a literal single quote.)
1180+
# contain a literal single quote.) The config-flavoured prod objects above
1181+
# depend on the stamp for the same reason as the binary.
11661182
# SANITIZE belongs here: it changes the instrumentation compiled into every
11671183
# object, so a build that only changes it MUST rebuild. Its absence made
11681184
# `scripts/test.sh SANITIZE= ...` -- the documented way to get a plain build

‎tests/test_complexity.c‎

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -435,36 +435,58 @@ static double cx_ratio(double num, double den) {
435435
return den > 0.0 ? num / den : 0.0;
436436
}
437437

438+
typedef int (*CxCorpusBuilder)(const char *root, int k);
439+
440+
static void cx_remove_corpus(const char *root) {
441+
if (th_rmtree(root) != 0) {
442+
fprintf(stderr, " [complexity] WARN: could not remove temp corpus %s\n", root);
443+
}
444+
}
445+
446+
/* Build one corpus shape at k and 2k in two fresh temp dirs, run the pipeline
447+
* on both, and remove both dirs before returning — on success and on every
448+
* failure path. Only the metrics outlive the call; the corpora used to stay
449+
* behind in the temp root, four per suite run. Only dirs this call created
450+
* are removed (the temp root is shared with concurrent runs). */
451+
static int cx_measure(CxCorpusBuilder build, const char *prefix_a, const char *prefix_b,
452+
CxMetrics *out_a, CxMetrics *out_b) {
453+
const char *tmp = cbm_tmpdir();
454+
char root_a[512];
455+
char root_b[512];
456+
snprintf(root_a, sizeof(root_a), "%s/%s_XXXXXX", tmp, prefix_a);
457+
snprintf(root_b, sizeof(root_b), "%s/%s_XXXXXX", tmp, prefix_b);
458+
bool made_a = cbm_mkdtemp(root_a) != NULL;
459+
bool made_b = made_a && cbm_mkdtemp(root_b) != NULL;
460+
int rc = -1;
461+
if (made_b && build(root_a, CX_K_BASE) == 0 && build(root_b, CX_K_BASE * 2) == 0) {
462+
char db_a[600];
463+
char db_b[600];
464+
snprintf(db_a, sizeof(db_a), "%s/cx.db", root_a);
465+
snprintf(db_b, sizeof(db_b), "%s/cx.db", root_b);
466+
if (cx_run(root_a, db_a, out_a) == 0 && cx_run(root_b, db_b, out_b) == 0) {
467+
rc = 0;
468+
}
469+
}
470+
if (made_b) {
471+
cx_remove_corpus(root_b);
472+
}
473+
if (made_a) {
474+
cx_remove_corpus(root_a);
475+
}
476+
return rc;
477+
}
478+
438479
/* Shared across the suite so the report test reuses the measured pair instead
439480
* of paying two more pipeline runs. */
440481
static CxMetrics g_cx_base;
441482
static CxMetrics g_cx_doubled;
442483
static bool g_cx_measured = false;
443-
static char g_cx_root_base[512];
444-
static char g_cx_root_doubled[512];
445484

446485
static int cx_measure_pair(void) {
447486
if (g_cx_measured) {
448487
return 0;
449488
}
450-
const char *tmp = cbm_tmpdir();
451-
snprintf(g_cx_root_base, sizeof(g_cx_root_base), "%s/cbm_cx_base_XXXXXX", tmp);
452-
snprintf(g_cx_root_doubled, sizeof(g_cx_root_doubled), "%s/cbm_cx_dbl_XXXXXX", tmp);
453-
if (!cbm_mkdtemp(g_cx_root_base) || !cbm_mkdtemp(g_cx_root_doubled)) {
454-
return -1;
455-
}
456-
if (cx_build_corpus(g_cx_root_base, CX_K_BASE) != 0 ||
457-
cx_build_corpus(g_cx_root_doubled, CX_K_BASE * 2) != 0) {
458-
return -1;
459-
}
460-
char db1[600];
461-
char db2[600];
462-
snprintf(db1, sizeof(db1), "%s/cx.db", g_cx_root_base);
463-
snprintf(db2, sizeof(db2), "%s/cx.db", g_cx_root_doubled);
464-
if (cx_run(g_cx_root_base, db1, &g_cx_base) != 0) {
465-
return -1;
466-
}
467-
if (cx_run(g_cx_root_doubled, db2, &g_cx_doubled) != 0) {
489+
if (cx_measure(cx_build_corpus, "cbm_cx_base", "cbm_cx_dbl", &g_cx_base, &g_cx_doubled) != 0) {
468490
return -1;
469491
}
470492
g_cx_measured = true;
@@ -555,25 +577,8 @@ static int cx_measure_bigpkg_pair(void) {
555577
if (g_cx_big_measured) {
556578
return 0;
557579
}
558-
const char *tmp = cbm_tmpdir();
559-
char ra[512];
560-
char rb[512];
561-
snprintf(ra, sizeof(ra), "%s/cbm_cxbig_a_XXXXXX", tmp);
562-
snprintf(rb, sizeof(rb), "%s/cbm_cxbig_b_XXXXXX", tmp);
563-
if (!cbm_mkdtemp(ra) || !cbm_mkdtemp(rb)) {
564-
return -1;
565-
}
566-
if (cx_build_bigpkg(ra, CX_K_BASE) != 0 || cx_build_bigpkg(rb, CX_K_BASE * 2) != 0) {
567-
return -1;
568-
}
569-
char db1[600];
570-
char db2[600];
571-
snprintf(db1, sizeof(db1), "%s/cx.db", ra);
572-
snprintf(db2, sizeof(db2), "%s/cx.db", rb);
573-
if (cx_run(ra, db1, &g_cx_big_base) != 0) {
574-
return -1;
575-
}
576-
if (cx_run(rb, db2, &g_cx_big_doubled) != 0) {
580+
if (cx_measure(cx_build_bigpkg, "cbm_cxbig_a", "cbm_cxbig_b", &g_cx_big_base,
581+
&g_cx_big_doubled) != 0) {
577582
return -1;
578583
}
579584
g_cx_big_measured = true;

0 commit comments

Comments
 (0)