Hi David,
first of all, thanks for your review!
On Tue, Nov 3, 2026 at 3:09 AM David Faust <[email protected]> wrote:
>
>
>
> On 10/31/26 7:54 PM, York Jasper Niebuhr wrote:
> > Use deferred constants to preserve semantic information required for BPF
> > CO-RE relocations.
> >
> > Mark enum types for deferred enum values and record types carrying
> > "preserve_access_index" for deferred field offsets. Deferred constants
> > encountered during BPF lowering are either turned into CO-RE relocations
> > or resolved to their ordinary compile-time values when no relocation is
> > requested.
> >
> > Add a "preserve_enum_values" attribute for enum types. Enumerators of a
> > type carrying this attribute automatically generate
> > BPF_RELO_ENUMVAL_VALUE relocations.
> >
> > Remove the bpf_enum_mappings workaround in favor of direct enum identity
> > recovery from deferred cst arguments to __builtin_preserve_enum_value.
> >
> > For record types carrying "preserve_access_index", deferred field
> > offsets originating from offsetof expressions are converted into
> > BPF_RELO_FIELD_BYTE_OFFSET relocations.
> >
> > BPF CO-RE relocations can not currently be emitted for static data. Add
> > an IPA pass which resolves deferred constants in static initializers
> > before the generic deferred-constant barrier. Initializers which would
> > require CO-RE relocations are diagnosed. Support for such initializers
> > could be added in the future by generating relocated runtime
> > initialization code, for cases such as:
> >
> > struct task_struct {
> > ...
> > int x;
> > ...
> > } __attribute__((preserve_access_index));
> >
> > static size_t OFF_X = offsetof (struct task_struct, x);
> >
> So I gave this a try to see if the above static initializer thing
> is an issue for the kernel selftests.
>
> Instead I encountered another problem when using an enumerator to index
> into an array, e.g.:
>
> enum bpf_map_type {
> BPF_MAP_TYPE_START,
> BPF_MAP_TYPE_RINGBUF,
> __BPF_MAP_TYPE_MAX
> };
> int a[BPF_MAP_TYPE_RINGBUF];
>
> $ ./xgcc -B. ~/playpen/bpf/defer.c -c
> /home/dfaust/playpen/bpf/defer.c:8:5: error: variably modified ‘a’ at file
> scope
> 8 | int a[BPF_MAP_TYPE_RINGBUF];
> | ^
>
> It's coming from this spot in c-decl.cc grokdeclarator:7444
>
> else if ((decl_context == NORMAL || decl_context == FIELD)
> && current_scope == file_scope)
> {
> error_at (loc, "variably modified %qE at file scope",
> name);
> size = integer_one_node;
> }
>
> Not sure whether this indicates a bug in the C/middle-end portion,
> or due to how it is being used from the BPF backend, e.g. if the
> plugin_finish_type hook needs to take more care rather than just
> marking any enumeral type.
>
The deferred constants work specifically in contexts that do not determine
the "shape" of the program. That means the usual runtime expressions
are fine, but e.g. type formation can not be deferred, because that would
fundamentally change what code has to be generated, instead of what value
goes into the code.
The issue you ran into is precisely this. Having
int a[BPF_MAP_TYPE_RINGBUF];
fail to compile is expected IF BPF_MAP_TYPE_RINGBUF can change later,
because how much space would you allocate for this array? This is only
an actual problem though if BPF_MAP_TYPE_RINGBUF should not actually be
deferred.
So, as you guessed, the real problem here is that the plugin_finish_type
callback unconditionally marks all enums. For the preserve_enum_value
attribute it would suffice to mark just the ones that actually have the
attribute. At the moment, it marks the others too such that the enum values
handed to __builtin_preserve_enum_value are also deferred, regardless of
whether they come from enums that actually have the attribute.
Since my 4 a.m. self forgot to allow deferred constants in "CASE X" labels
(which is slightly inconvenient for enums), I am currently putting together
v2 anyways. To solve the "mark all, even if they aren't actually deferred"
problem, I intend to add a hook that allows a consumer to resolve deferred
constants in illegal contexts (such as the array bounds) back to compile-
time values if they are not actually intended to be deferred. This is
basically what the BPF lowering pass currently does at a later point, so
the hook can mostly just reuse that code. With that, this issue should
hopefully be fixed :)
Regarding the static initializers...
Normally, stuff like
static size_t OFF_X = bpf_core_field_offset (...);
simply wouldn't compile. With deferred constants,
static size_t OFF_X = __builtin_offsetof (...);
is however allowed by the frontend, but should refer to the relocated
offset if the type is marked with preserve_access_index.
So a BPF backend transformation could inspect such static initializers
and automatically generate equivalent runtime initializers. This is also
what I did for Selfpatch-SLR, to allow static data that depends on
randomized field offsets. Conceptually, the code is turned into:
static size_t OFF_X = some initial value (e.g. the original offset);
void refresh_OFF_X () { OFF_X = relocated offset; }
and the refresh is executed before any other code runs. In the case of
BPF though, it would probably make more sense to just relocate OFF_X
directly somehow.
Anyways, I wouldn't expect this to break any existing code, because
that code would need to already be using non-relocated expressions for
values that are actually supposed to be chosen at load-time. I only
suggested this as a possible future quality of life improvement, which
would just take considerably more effort to implement.