btf: stop resolving cyclic type references
PATCH 5/20 btf: stop resolving cyclic type references
test: expand BTF cyclic type resolution coverage
--- libdtrace/dt_btf.c | 8 +- test/internals/btf/tst.cyclic-types.sh | 18 ++++ test/triggers/Build | 8 +- test/triggers/btf-cyclic-types.c | 113 +++++++++++++++++++++++++ 4 files changed, 145 insertions(+), 2 deletions(-) create mode 100755 test/internals/btf/tst.cyclic-types.sh create mode 100644 test/triggers/btf-cyclic-types.c
libdtrace/dt_btf.c +7 -1
@@ -100,8 +100,14 @@ static btf_type_t * dt_btf_real_type_by_id(const dt_btf_t *btf, int32_t id) {btf_type_t *type =dt_btf_type_by_id (btf, id); +uint64_t remaining =1 + (uint64_t )btf->type_cnt; -while (type !=NULL ) { +if (btf->parent !=NULL ) + remaining += btf->parent->type_cnt; + +/* A longer chain must revisit a type, so reject cyclic BTF references. */ +while (type !=NULL && remaining >0 ) { + remaining--;switch (BTF_INFO_KIND (type->info)) {case BTF_KIND_CONST :case BTF_KIND_FUNC :
test/internals/btf/tst.cyclic-types.sh new file +18 -0
@@ -0,0 +1,18 @@ +#!/bin/bash +# +# Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. +# Licensed under the Universal Permissive License v 1.0 as shown at +# http://oss.oracle.com/licenses/upl. +# +# ASSERTION: Cyclic BTF type references fail resolution, while a finite +# chain still resolves to its function prototype. +# + +test/triggers/btf-cyclic-types +rc=$? + +echo "Result: $rc" + +[[ $rc -eq 0 ]] || exit 1 + +exit 0
test/triggers/Build +7 -1
@@ -25,7 +25,7 @@ EXTERNAL_TRIGGERS = $(EXTERNAL_64BIT_TRIGGERS) $(if $(NATIVE_BITNESS_ONLY),,$(EX INTERNAL_64BIT_TRIGGERS = libproc-pldd libproc-consistency libproc-sleeper \ libproc-sleeper-pie libproc-dlmadopen libproc-lookup-by-name \ libproc-lookup-victim libproc-execing-bkpts libproc-execing-bkpts-victim \ - module-discovery shared-ctf + btf-cyclic-types shared-ctf module-discovery INTERNAL_32BIT_TRIGGERS := libproc-sleeper-32 libproc-sleeper-pie-32 INTERNAL_TRIGGERS = $(INTERNAL_64BIT_TRIGGERS) $(if $(NATIVE_BITNESS_ONLY),,$(INTERNAL_32BIT_TRIGGERS))
@@ -126,6 +126,12 @@ visible-constructor-static_LDFLAGS := -static -s visible-constructor_LDFLAGS := -s visible-constructor-32_LDFLAGS := -s +# Include the private BTF resolver and discard its unrelated functions, which +# otherwise require the rest of libdtrace at link time. +btf-cyclic-types_CFLAGS := -I. -Ilibdtrace -Ilibproc -Ilibcommon \ + -ffunction-sections -fdata-sections -Wno-pedantic +btf-cyclic-types_LDFLAGS := -Wl,--gc-sections + # We are using machinery from libproc directly, but libproc calls into # nonpublic parts of libdtrace for various shared machinery (dt_list # and so on). Thus, we must link directly to the build libraries, since
test/triggers/btf-cyclic-types.c new file +113 -0
113 lines not on this page · raw message
Signature · 1 line
2.52.0
On 30 Oct 2026, Kris Van Hees told this:
Quoted 8 lines
libdtrace/dt_btf.c +1
-0@@ -100,8 +100,14 @@ static btf_type_t * dt_btf_real_type_by_id(const dt_btf_t *btf, int32_t id) {btf_type_t *type =dt_btf_type_by_id (btf, id); +uint64_t remaining =1 + (uint64_t )btf->type_cnt;
libdtrace/dt_btf.c static btf_type_t * · line 113
Quoted 10 lines
libdtrace/dt_btf.c +6
-1-while (type !=NULL ) { +if (btf->parent !=NULL ) + remaining += btf->parent->type_cnt; + +/* A longer chain must revisit a type, so reject cyclic BTF references. */ +while (type !=NULL && remaining >0 ) { + remaining--;switch (BTF_INFO_KIND (type->info)) {case BTF_KIND_CONST :case BTF_KIND_FUNC :
That's a fun approach. Simple and entirely different from what I'm doing in a yet-to-be-pushed libctf commit :) (which involves a counter which starts at 1 and is reduced on every lookup, and whenever it hits 0 an identity-check is done and the counter is reset with an increased initial value: a tortoise-and-hare-in-O(1) space-and-time algorithm of Hellman's. Like yours, it has the advantage of being basically free unless things go wrong.)
Reviewed-by Nick Alcock <[email protected]>
Signature · 1 line
NULL && (void)