libbpf: btf__raw_data() returns stale data after btf__relocate()
Hi,
I found a case where btf__raw_data() returns stale data after a successful btf__relocate(). The type references in memory are updated, but the serialized data still contains the old references.
I reproduced this with libbpf commit 85c0d575a447651bbeb8e2376eacd64c30dce9a6 on Ubuntu 22.04, x86_64, running kernel 6.8.0-138-generic.
The reproducer creates a split BTF with a pointer to an int in the base BTF, then calls btf__raw_data() to populate the cache. It relocates the split BTF to a new base where the int has a different type ID. After relocation, the live pointer has the correct target, but serializing and parsing it again gives the old target.
Save the following as repro_cache.c in the libbpf source root:
#include <assert.h>
#include <stdio.h>
#include "btf.h"
int main(void)
{
struct btf *old = btf__new_empty();
struct btf *target = btf__new_empty();
struct btf *split, *roundtrip;
const void *raw;
unsigned int size;
unsigned int live_id, serialized_id;
assert(old && target);
assert(btf__add_int(old, "int", 4, BTF_INT_SIGNED) == 1);
split = btf__new_empty_split(old);
assert(split && btf__add_ptr(split, 1) == 2);
/* Populate the serialization cache before relocation. */
assert(btf__raw_data(split, &size));
assert(btf__add_int(target, "padding", 4, BTF_INT_SIGNED) == 1);
assert(btf__add_int(target, "int", 4, BTF_INT_SIGNED) == 2);
assert(btf__relocate(split, target) == 0);
/* The pointer has ID 3 with the new base. */
live_id = btf__type_by_id(split, 3)->type;
raw = btf__raw_data(split, &size);
assert(raw);
roundtrip = btf__new_split(raw, size, target);
assert(roundtrip);
serialized_id = btf__type_by_id(roundtrip, 3)->type;
printf("live_target=%u serialized_target=%u expected=2\n",
live_id, serialized_id);
btf__free(roundtrip);
btf__free(split);
btf__free(old);
btf__free(target);
return live_id == 2 && serialized_id == 2 ? 0 : 1;
}Build and run with GCC and the libelf/zlib development packages installed:
make -C src -j2
gcc -g -O0 -UNDEBUG -Isrc -Iinclude -Iinclude/uapi \
repro_cache.c src/libbpf.a -lelf -lz -o repro_cache
./repro_cacheNo root privileges are needed. Keep assertions enabled, since some API calls in this reproducer are inside assert().
Actual output, with exit status 1:
live_target=2 serialized_target=1 expected=2
Expected output, with exit status 0:
live_target=2 serialized_target=2 expected=2
In the new base, ID 1 is padding and ID 2 is int, so the serialized pointer ends up referring to the wrong type.
It looks like btf_relocate() updates the types and string offsets without invalidating the raw-data caches. A later btf__raw_data() call then returns the cached data through btf_get_raw_data().
I tried calling btf_ensure_modifiable(btf), checking its return value, and then calling btf_relocate() in btf__relocate(). With that change, the reproducer produces the expected output. Checks for both endianness caches, file round-trips, and repeated relocation also pass with the change.
I also added a regression test to the Linux 6.8 BPF selftests. It fails before the change and passes after it in both test_progs and test_progs-no_alu32. The broader selftests run needed compatibility adjustments for libbpf 1.8 and had other failures and skips, so I don't have a clean full-suite pass.
Would btf_ensure_modifiable() in btf__relocate() be the right way to handle this? I couldn't find an existing report; please point me to it if this is already being discussed.
Thanks.
Quoted 99 lines · On 11/1/26 1:53 PM, 洛 wrote:
Hi,
I found a case where
btf__raw_data()returns stale data after a successfulbtf__relocate(). The type references in memory are updated, but the serialized data still contains the old references.I reproduced this with libbpf commit
85c0d575a447651bbeb8e2376eacd64c30dce9a6on Ubuntu 22.04,x86_64, running kernel 6.8.0-138-generic.The reproducer creates a split BTF with a pointer to an int in the base BTF, then calls
btf__raw_data()to populate the cache. It relocates the split BTF to a new base where the int has a different type ID. After relocation, the live pointer has the correct target, but serializing and parsing it again gives the old target.Save the following as
repro_cache.cin the libbpf source root:#include <
assert.h>
#include <stdio.h>
#include "btf.h"int main(void)
{ struct btf *old = btf__new_empty(); struct btf *target = btf__new_empty(); struct btf *split, *roundtrip; const void *raw; unsigned int size; unsigned int live_id, serialized_id; assert(old && target); assert(btf__add_int(old, "int", 4, BTF_INT_SIGNED) == 1); split = btf__new_empty_split(old); assert(split && btf__add_ptr(split, 1) == 2); /* Populate the serialization cache before relocation. */ assert(btf__raw_data(split, &size)); assert(btf__add_int(target, "padding", 4, BTF_INT_SIGNED) == 1); assert(btf__add_int(target, "int", 4, BTF_INT_SIGNED) == 2); assert(btf__relocate(split, target) == 0); /* The pointer has ID 3 with the new base. */ live_id = btf__type_by_id(split, 3)->type; raw = btf__raw_data(split, &size); assert(raw); roundtrip = btf__new_split(raw, size, target); assert(roundtrip); serialized_id = btf__type_by_id(roundtrip, 3)->type; printf("live_target=%u serialized_target=%u expected=2\n", live_id, serialized_id); btf__free(roundtrip); btf__free(split); btf__free(old); btf__free(target); return live_id == 2 && serialized_id == 2 ? 0 : 1; }Build and run with GCC and the libelf/zlib development packages installed:
make -C src -j2 gcc -g -O0 -UNDEBUG -Isrc -Iinclude -Iinclude/uapi \ repro_cache.c src/libbpf.a -lelf -lz -o repro_cache ./repro_cacheNo root privileges are needed. Keep assertions enabled, since some API calls in this reproducer are inside
assert().Actual output, with exit status 1:
live_target=2 serialized_target=1 expected=2
Expected output, with exit status 0:
live_target=2 serialized_target=2 expected=2
In the new base, ID 1 is padding and ID 2 is int, so the serialized pointer ends up referring to the wrong type.
It looks like
btf_relocate()updates the types and string offsets without invalidating the raw-data caches. A laterbtf__raw_data()call then returns the cached data throughbtf_get_raw_data().I tried calling
btf_ensure_modifiable(btf), checking its return value, and then callingbtf_relocate()inbtf__relocate(). With that change, the reproducer produces the expected output. Checks for both endianness caches, file round-trips, and repeated relocation also pass with the change.I also added a regression test to the Linux 6.8 BPF selftests. It fails before the change and passes after it in both
test_progsand test_progs-no_alu32. The broader selftests run needed compatibility adjustments for libbpf 1.8 and had other failures and skips, so I don't have a clean full-suite pass.
Quoted 5 lines
Would
btf_ensure_modifiable()inbtf__relocate()be the right way to handle this? I couldn't find an existing report; please point me to it if this is already being discussed.Thanks.
Thanks for the report. Yes, btf__relocate() misses invalidating the cached raw_data, and btf_ensure_modifiable() is the right fix, same as btf__permute() does. I can fix it.
BTW, how did you run into this? Is there a real use case that calls btf__raw_data() before and after btf__relocate().
Hi Jiayuan,
Thanks for confirming and offering to fix it.
I found this while reviewing and testing the BTF APIs. I don't have a production case to point to.
Thinking about how this might come up in a build workflow, a tool could use btf__distill_base() and save the resulting base and split BTF as reusable metadata. It could then keep the same split BTF object in memory, relocate it against a compatible target kernel's BTF, and write out a version for that target.
In that case, the first btf__raw_data() call would be for saving the reusable copy, and the second would be for writing the relocated output. That's one possible use case I can imagine, though I haven't used this workflow in practice.
Thanks.
Quoted 116 lines · Jiayuan Chen <[email protected]> 于2026年11月1日周日 16:59写道:
Quoted 99 lines · On 11/1/26 1:53 PM, 洛 wrote:
Hi,
I found a case where
btf__raw_data()returns stale data after a successfulbtf__relocate(). The type references in memory are updated, but the serialized data still contains the old references.I reproduced this with libbpf commit
85c0d575a447651bbeb8e2376eacd64c30dce9a6on Ubuntu 22.04,x86_64, running kernel 6.8.0-138-generic.The reproducer creates a split BTF with a pointer to an int in the base BTF, then calls
btf__raw_data()to populate the cache. It relocates the split BTF to a new base where the int has a different type ID. After relocation, the live pointer has the correct target, but serializing and parsing it again gives the old target.Save the following as
repro_cache.cin the libbpf source root:#include <
assert.h>
#include <stdio.h>
#include "btf.h"int main(void)
{ struct btf *old = btf__new_empty(); struct btf *target = btf__new_empty(); struct btf *split, *roundtrip; const void *raw; unsigned int size; unsigned int live_id, serialized_id; assert(old && target); assert(btf__add_int(old, "int", 4, BTF_INT_SIGNED) == 1); split = btf__new_empty_split(old); assert(split && btf__add_ptr(split, 1) == 2); /* Populate the serialization cache before relocation. */ assert(btf__raw_data(split, &size)); assert(btf__add_int(target, "padding", 4, BTF_INT_SIGNED) == 1); assert(btf__add_int(target, "int", 4, BTF_INT_SIGNED) == 2); assert(btf__relocate(split, target) == 0); /* The pointer has ID 3 with the new base. */ live_id = btf__type_by_id(split, 3)->type; raw = btf__raw_data(split, &size); assert(raw); roundtrip = btf__new_split(raw, size, target); assert(roundtrip); serialized_id = btf__type_by_id(roundtrip, 3)->type; printf("live_target=%u serialized_target=%u expected=2\n", live_id, serialized_id); btf__free(roundtrip); btf__free(split); btf__free(old); btf__free(target); return live_id == 2 && serialized_id == 2 ? 0 : 1; }Build and run with GCC and the libelf/zlib development packages installed:
make -C src -j2 gcc -g -O0 -UNDEBUG -Isrc -Iinclude -Iinclude/uapi \ repro_cache.c src/libbpf.a -lelf -lz -o repro_cache ./repro_cacheNo root privileges are needed. Keep assertions enabled, since some API calls in this reproducer are inside
assert().Actual output, with exit status 1:
live_target=2 serialized_target=1 expected=2
Expected output, with exit status 0:
live_target=2 serialized_target=2 expected=2
In the new base, ID 1 is padding and ID 2 is int, so the serialized pointer ends up referring to the wrong type.
It looks like
btf_relocate()updates the types and string offsets without invalidating the raw-data caches. A laterbtf__raw_data()call then returns the cached data throughbtf_get_raw_data().I tried calling
btf_ensure_modifiable(btf), checking its return value, and then callingbtf_relocate()inbtf__relocate(). With that change, the reproducer produces the expected output. Checks for both endianness caches, file round-trips, and repeated relocation also pass with the change.I also added a regression test to the Linux 6.8 BPF selftests. It fails before the change and passes after it in both
test_progsand test_progs-no_alu32. The broader selftests run needed compatibility adjustments for libbpf 1.8 and had other failures and skips, so I don't have a clean full-suite pass.Quoted 5 lines
Would
btf_ensure_modifiable()inbtf__relocate()be the right way to handle this? I couldn't find an existing report; please point me to it if this is already being discussed.Thanks.
Thanks for the report. Yes,
btf__relocate()misses invalidating the cachedraw_data, andbtf_ensure_modifiable()is the right fix, same asbtf__permute()does. I can fix it.BTW, how did you run into this? Is there a real use case that calls
btf__raw_data()before and afterbtf__relocate().