Skip to content

Commit 5c5964e

Browse files
committed
fix(deploy): bound Helm render memory, no wrong image from a skipped change
Review round 2 of DeusData#2677 (N1-N12). Every case ends in an explicit unknown, never in an OOM, a quadratic stall or a wrong image. Bounds (helm_template.c, pass_k8s.c): - N1: capture arrays grow by doubling, so n captures cost memory linear in n. Arena growth since the render began counts against a third budget (256 MiB) beside steps and output; a trip is logged with its reason. - N10: poisoned maps are a pointer hash set (merged values a path list), so a loop poisoning a new dict per iteration keeps reads O(1). Release images are deduplicated by hash. Wrong images (helm_template.c): - N2: a set / merge in a body an UNKNOWN condition skips is judged by its target whatever it is -- a call's result like `index .Values "image"` is evaluated in the frame: own, or a known map (poisoned), else it taints. - N3: putting a value another part holds into an own map revokes own; a part of a poisoned map is never own. - N4: a run-time set whose target is an UNKNOWN call result taints. - N5: an UNKNOWN break / continue spoils the rest of the loop body around it; a continue is a continue, not a break. - N6: the scan notes redefinitions and assignments, carries what it saw across the actions of one branch, and never judges a variable the part rebinds by its value in the frame. - N11: a define's "may not change" that leaned on a define still being scanned (mutual includes) is not kept. - N12: an include not run because its argument is poisoned or UNKNOWN spoils that argument (poisoned deep, or a taint). Releases and charts (pass_k8s.c, pass_terraform.c): - N8: an empty values.yaml is values {} (Helm's), not a file not read. - N9: a Terraform set_sensitive whose name overlaps a key an image reads, a Terraform postrender, or a kustomization with its own transformers (images, patches, replacements, transformers, components) leaves the release without an image of its own. Tests (N7): the symlink probes are POSIX-only (SKIP_PLATFORM on Windows). One test per item, RED on 4cea54f by its assertion; N10's is a correctness control, its cost measured (linear vs quadratic) outside the suite. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 4cea54f commit 5c5964e

7 files changed

Lines changed: 951 additions & 190 deletions

File tree

‎src/pipeline/helm_template.c‎

Lines changed: 423 additions & 156 deletions
Large diffs are not rendered by default.

‎src/pipeline/helm_template.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ void cbm_helm_defines_free(cbm_helm_defines_t *d);
6868
typedef struct {
6969
int line; /* 1-based line of the key in the template */
7070
int n; /* captures */
71+
int cap; /* room in image / keys (the render's own) */
7172
const char **image; /* the rendered scalar, YAML quoting removed; NULL: UNKNOWN */
7273
const char **keys; /* JSON array of the values paths it read; NULL: none */
7374
} cbm_helm_probe_t;

‎src/pipeline/pass_k8s.c‎

Lines changed: 132 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3904,6 +3904,7 @@ typedef struct {
39043904
k8s_himg_t *v;
39053905
int n;
39063906
int cap;
3907+
CBMHashTable *seen; /* "src:img" of each v (keys cbm_alloc'd): one look-up a push */
39073908
} k8s_himgs_t;
39083909

39093910
typedef struct {
@@ -3919,6 +3920,9 @@ typedef struct {
39193920
int tcap;
39203921
k8s_himgs_t dflt; /* the containers' images with the defaults */
39213922
int edges;
3923+
/* the release rendered: its set_sensitive names (JSON array; NULL: none) --
3924+
* an image reading one is not known, its value never read */
3925+
const char *sensitive;
39223926
} k8s_helm_t;
39233927

39243928
/* The templated images of the facts, kept past them. */
@@ -3948,16 +3952,29 @@ static char *k8s_repo_read(cbm_pipeline_ctx_t *ctx, const char *rel, int *len) {
39483952
}
39493953
char path[CBM_SZ_4K];
39503954
int n = snprintf(path, sizeof(path), "%s/%s", ctx->repo_path, rel);
3951-
return n > 0 && (size_t)n < sizeof(path) ? k8s_read_file(path, len) : NULL;
3955+
if (n <= 0 || (size_t)n >= sizeof(path)) {
3956+
return NULL;
3957+
}
3958+
char *buf = k8s_read_file(path, len);
3959+
cbm_path_info_t info;
3960+
if (!buf && cbm_path_info_utf8(path, &info) == CBM_PATH_INFO_OK && info.is_regular &&
3961+
!info.is_symlink && info.size == 0) {
3962+
/* an empty file is read: empty text (padded like every read) */
3963+
enum { CBM_TS_EMPTY_PAD = 16 };
3964+
buf = cbm_calloc(CBM_MEM_CLASS_OTHER, CBM_TS_EMPTY_PAD);
3965+
*len = 0;
3966+
}
3967+
return buf;
39523968
}
39533969

3954-
/* Is repository path `rel` there at all -- a file, a symlink, anything (a
3955-
* link is reported, never followed)? */
3970+
/* Is repository path `rel` there, or can that not be told (a link is
3971+
* reported, never followed; only a path proven absent is not there)? */
39563972
static bool k8s_repo_has(cbm_pipeline_ctx_t *ctx, const char *rel) {
39573973
char path[CBM_SZ_4K];
39583974
int n = snprintf(path, sizeof(path), "%s/%s", ctx->repo_path, rel);
39593975
cbm_path_info_t info;
3960-
return n > 0 && (size_t)n < sizeof(path) && cbm_path_info_utf8(path, &info) == 0;
3976+
return n > 0 && (size_t)n < sizeof(path) &&
3977+
cbm_path_info_utf8(path, &info) != CBM_PATH_INFO_ABSENT;
39613978
}
39623979

39633980
/* A file of `dir` (one of `names`), parsed as values; *found when one exists.
@@ -4232,14 +4249,38 @@ static const char *k8s_helm_overridden(k8s_helm_t *h, const char *keys, yyjson_m
42324249
}
42334250

42344251
static void k8s_himg_push(k8s_himgs_t *l, k8s_himg_t x) {
4235-
for (int i = 0; i < l->n; i++) {
4236-
if (l->v[i].src == x.src && l->v[i].img == x.img) {
4237-
return; /* the first (in file order) of the same Image stands for it */
4238-
}
4252+
char key[CBM_SZ_64];
4253+
snprintf(key, sizeof(key), "%lld:%lld", (long long)x.src, (long long)x.img);
4254+
if (!l->seen) {
4255+
l->seen = cbm_ht_create(0);
4256+
}
4257+
if (!l->seen || cbm_ht_get(l->seen, key)) {
4258+
return; /* the first (in file order) of the same Image stands for it */
4259+
}
4260+
size_t kl = strlen(key) + SKIP_ONE;
4261+
char *k = cbm_alloc(CBM_MEM_CLASS_OTHER, kl);
4262+
if (!k || !k8s_grow((void **)&l->v, &l->cap, l->n, sizeof(*l->v))) {
4263+
cbm_free(CBM_MEM_CLASS_OTHER, k);
4264+
return;
42394265
}
4240-
if (k8s_grow((void **)&l->v, &l->cap, l->n, sizeof(*l->v))) {
4241-
l->v[l->n++] = x;
4266+
memcpy(k, key, kl);
4267+
cbm_ht_set(l->seen, k, (void *)(uintptr_t)SKIP_ONE);
4268+
l->v[l->n++] = x;
4269+
}
4270+
4271+
static void k8s_himg_key_free(const char *key, void *value, void *ud) {
4272+
(void)value;
4273+
(void)ud;
4274+
cbm_free(CBM_MEM_CLASS_OTHER, (void *)key);
4275+
}
4276+
4277+
static void k8s_himgs_free(k8s_himgs_t *l) {
4278+
if (l->seen) {
4279+
cbm_ht_foreach(l->seen, k8s_himg_key_free, NULL);
4280+
cbm_ht_free(l->seen);
42424281
}
4282+
cbm_free(CBM_MEM_CLASS_OTHER, l->v);
4283+
*l = (k8s_himgs_t){0};
42434284
}
42444285

42454286
/* Does `src` USE Image `img` already by a literal reference? */
@@ -4313,8 +4354,68 @@ static void k8s_helm_emit_uses(cbm_pipeline_ctx_t *ctx, k8s_helm_t *h, const k8s
43134354
}
43144355
}
43154356

4357+
/* Do values paths a and b overlap -- one is the other or lies within it
4358+
* (a key boundary `.` / `[` after the shorter; "" is the whole values)? */
4359+
static bool k8s_path_overlap(const char *a, const char *b) {
4360+
size_t al = strlen(a);
4361+
size_t bl = strlen(b);
4362+
size_t m = al < bl ? al : bl;
4363+
if (strncmp(a, b, m) != 0) {
4364+
return false;
4365+
}
4366+
char c = (al < bl ? b : a)[m];
4367+
return m == 0 || c == '\0' || c == '.' || c == '[';
4368+
}
4369+
4370+
/* Does values path `path` (of the release's chart) overlap a set_sensitive
4371+
* name of the release rendered? */
4372+
static bool k8s_helm_sensitive_path(const k8s_helm_t *h, const char *path) {
4373+
if (!h->sensitive || !path) {
4374+
return false;
4375+
}
4376+
yyjson_doc *d = yyjson_read(h->sensitive, strlen(h->sensitive), 0);
4377+
bool hit = !d; /* names not read: say it touches */
4378+
size_t idx, max;
4379+
yyjson_val *k;
4380+
yyjson_arr_foreach(d ? yyjson_doc_get_root(d) : NULL, idx, max, k) {
4381+
const char *s = yyjson_get_str(k);
4382+
hit = hit || (s && k8s_path_overlap(path, s));
4383+
}
4384+
if (d) {
4385+
yyjson_doc_free(d);
4386+
}
4387+
return hit;
4388+
}
4389+
4390+
/* Does an image reading values keys `keys` (a JSON array, of its chart at
4391+
* key path `prefix` within the release's; global.* are the release's own)
4392+
* read a set_sensitive name of the release rendered? */
4393+
static bool k8s_helm_sensitive_keys(k8s_helm_t *h, const char *keys, const char *prefix) {
4394+
if (!h->sensitive || !keys) {
4395+
return false;
4396+
}
4397+
yyjson_doc *d = yyjson_read(keys, strlen(keys), 0);
4398+
bool hit = !d;
4399+
size_t idx, max;
4400+
yyjson_val *k;
4401+
yyjson_arr_foreach(d ? yyjson_doc_get_root(d) : NULL, idx, max, k) {
4402+
const char *key = yyjson_get_str(k);
4403+
bool global = key && strncmp(key, "global.", strlen("global.")) == 0;
4404+
const char *full = !key ? NULL
4405+
: global || !prefix || !prefix[0]
4406+
? key
4407+
: cbm_arena_sprintf(&h->a, "%s.%s", prefix, key);
4408+
hit = hit || !full || k8s_helm_sensitive_path(h, full);
4409+
}
4410+
if (d) {
4411+
yyjson_doc_free(d);
4412+
}
4413+
return hit;
4414+
}
4415+
43164416
/* Chart `from` -DEPLOYS {via, ref: image, image, override_of}-> the images an
4317-
* overlay changed: rendered there, absent from the defaults' images. */
4417+
* overlay changed: rendered there, absent from the defaults' images. An image
4418+
* reading a set_sensitive name (whose value is not read) is not known. */
43184419
static void k8s_helm_emit_overrides(cbm_pipeline_ctx_t *ctx, k8s_helm_t *h, int64_t from,
43194420
const char *via, const k8s_himgs_t *dflt,
43204421
const k8s_himgs_t *got, yyjson_mut_val *overlay,
@@ -4328,7 +4429,7 @@ static void k8s_helm_emit_overrides(cbm_pipeline_ctx_t *ctx, k8s_helm_t *h, int6
43284429
strcmp(dflt->v[j].ref, x->ref) == 0;
43294430
}
43304431
const char *ovr = k8s_helm_overridden(h, x->keys, overlay, globals, prefix);
4331-
if (same || !ovr || from <= 0) {
4432+
if (same || !ovr || from <= 0 || k8s_helm_sensitive_keys(h, x->keys, prefix)) {
43324433
continue;
43334434
}
43344435
k8s_himg_t e = {from, x->img, x->ref, NULL, ovr};
@@ -4593,7 +4694,7 @@ static void k8s_helm_release_local(cbm_pipeline_ctx_t *ctx, k8s_bundles_t *b, k8
45934694
yyjson_mut_val *globals = yyjson_mut_obj_get(rv, "global");
45944695
k8s_helm_emit_overrides(ctx, h, src, via, &h->dflt, &got, overlay, globals,
45954696
path && path[0] ? path : NULL, emitted);
4596-
cbm_free(CBM_MEM_CLASS_OTHER, got.v);
4697+
k8s_himgs_free(&got);
45974698
}
45984699
cbm_free(CBM_MEM_CLASS_OTHER, srcs);
45994700
from = to;
@@ -4647,7 +4748,9 @@ static void k8s_helm_convention(cbm_pipeline_ctx_t *ctx, k8s_helm_t *h, int64_t
46474748
: "",
46484749
dig[0] ? dig : tag)
46494750
: NULL;
4650-
int64_t img = ref && k8s_helm_has_host(ref) && cbm_image_ref_valid(ref)
4751+
/* a set_sensitive name within the mapping: its value is not read */
4752+
bool sensitive = k8s_helm_sensitive_path(h, path ? path : "");
4753+
int64_t img = ref && !sensitive && k8s_helm_has_host(ref) && cbm_image_ref_valid(ref)
46514754
? cbm_pipeline_image_node(ctx, ref)
46524755
: 0;
46534756
int before = emitted->n;
@@ -4699,7 +4802,7 @@ static void k8s_helm_release(cbm_pipeline_ctx_t *ctx, k8s_bundles_t *b, k8s_helm
46994802
cbm_ht_get(b->charts, dir)) {
47004803
k8s_helm_release_local(ctx, b, h, l, src, via, dir, rv, &emitted);
47014804
}
4702-
cbm_free(CBM_MEM_CLASS_OTHER, emitted.v);
4805+
k8s_himgs_free(&emitted);
47034806
}
47044807

47054808
/* A node's document (lines start_line..end_line of its file) as values. */
@@ -4792,6 +4895,16 @@ static yyjson_mut_val *k8s_helm_kustomize_values(cbm_pipeline_ctx_t *ctx, k8s_he
47924895
char *src = kust->file_path ? k8s_repo_read(ctx, kust->file_path, &len) : NULL;
47934896
yyjson_mut_val *doc = src ? cbm_helm_values_parse(h->doc, src, (size_t)len) : NULL;
47944897
cbm_free(CBM_MEM_CLASS_OTHER, src);
4898+
/* kustomize runs the kustomization's own transformers over the inflated
4899+
* chart: images / patches / replacements may change what it deploys */
4900+
static const char *const TRANSFORMS[] = {
4901+
"images", "patches", "patchesJson6902", "patchesStrategicMerge",
4902+
"replacements", "transformers", "components", NULL};
4903+
for (int i = 0; TRANSFORMS[i]; i++) {
4904+
if (k8s_helm_gives(doc, TRANSFORMS[i])) {
4905+
return NULL;
4906+
}
4907+
}
47954908
yyjson_mut_val *charts = yyjson_mut_is_obj(doc) ? yyjson_mut_obj_get(doc, "helmCharts") : NULL;
47964909
yyjson_mut_val *inline_vals = NULL;
47974910
size_t idx, max;
@@ -4874,9 +4987,12 @@ static void k8s_helm_link_releases(cbm_pipeline_ctx_t *ctx, k8s_bundles_t *b, k8
48744987
if (jd) {
48754988
yyjson_doc_free(jd);
48764989
}
4990+
h->sensitive = r->sensitive;
48774991
k8s_helm_release(ctx, b, h, &l, r->src, "terraform", r->chart, rv);
4992+
h->sensitive = NULL;
48784993
cbm_free(CBM_MEM_CLASS_OTHER, r->values);
48794994
}
4995+
cbm_free(CBM_MEM_CLASS_OTHER, r->sensitive);
48804996
}
48814997
cbm_free(CBM_MEM_CLASS_OTHER, ctx->tf_releases);
48824998
ctx->tf_releases = NULL;
@@ -4905,7 +5021,7 @@ static void k8s_helm_free(k8s_helm_t *h) {
49055021
yyjson_mut_doc_free(h->doc);
49065022
}
49075023
cbm_free(CBM_MEM_CLASS_OTHER, h->t);
4908-
cbm_free(CBM_MEM_CLASS_OTHER, h->dflt.v);
5024+
k8s_himgs_free(&h->dflt);
49095025
cbm_arena_destroy(&h->a);
49105026
if (h->edges > 0) {
49115027
cbm_log_info("pass.k8s.helm_images", "edges", itoa_k8s(h->edges));

0 commit comments

Comments
 (0)