126236Swpaul// SPDX-License-Identifier: GPL-2.0
226236Swpaul// Copyright (c) 2019 Facebook
326236Swpaul
426236Swpaul#include <linux/bpf.h>
526236Swpaul#include <stdint.h>
626236Swpaul#include <bpf/bpf_helpers.h>
726236Swpaul#include <bpf/bpf_core_read.h>
826236Swpaul
926236Swpaulchar _license[] SEC("license") = "GPL";
1026236Swpaul
1126236Swpaulstruct {
1226236Swpaul	char in[256];
1326236Swpaul	char out[256];
1426236Swpaul} data = {};
1526236Swpaul
1626236Swpaulstruct core_reloc_misc_output {
1726236Swpaul	int a, b, c;
1826236Swpaul};
1926236Swpaul
2026236Swpaulstruct core_reloc_misc___a {
2126236Swpaul	int a1;
2226236Swpaul	int a2;
2326236Swpaul};
2426236Swpaul
2526236Swpaulstruct core_reloc_misc___b {
2626236Swpaul	int b1;
2726236Swpaul	int b2;
2826236Swpaul};
2926236Swpaul
3026236Swpaul/* fixed two first members, can be extended with new fields */
3126236Swpaulstruct core_reloc_misc_extensible {
3230378Scharnier	int a;
33114601Sobrien	int b;
34114601Sobrien};
3530378Scharnier
3626236Swpaul#define CORE_READ(dst, src) bpf_core_read(dst, sizeof(*(dst)), src)
3726236Swpaul
3826236SwpaulSEC("raw_tracepoint/sys_enter")
3926236Swpaulint test_core_misc(void *ctx)
4026236Swpaul{
4126236Swpaul	struct core_reloc_misc___a *in_a = (void *)&data.in;
4226236Swpaul	struct core_reloc_misc___b *in_b = (void *)&data.in;
4326236Swpaul	struct core_reloc_misc_extensible *in_ext = (void *)&data.in;
4426236Swpaul	struct core_reloc_misc_output *out = (void *)&data.out;
4526236Swpaul
4626236Swpaul	/* record two different relocations with the same accessor string */
4726236Swpaul	if (CORE_READ(&out->a, &in_a->a1) ||		/* accessor: 0:0 */
4826236Swpaul	    CORE_READ(&out->b, &in_b->b1))		/* accessor: 0:0 */
4990298Sdes		return 1;
5090298Sdes
5126236Swpaul	/* Validate relocations capture array-only accesses for structs with
5226236Swpaul	 * fixed header, but with potentially extendable tail. This will read
5326236Swpaul	 * first 4 bytes of 2nd element of in_ext array of potentially
5426236Swpaul	 * variably sized struct core_reloc_misc_extensible. */
5590297Sdes	if (CORE_READ(&out->c, &in_ext[2]))		/* accessor: 2 */
5626236Swpaul		return 1;
5726236Swpaul
5826236Swpaul	return 0;
5926236Swpaul}
6026236Swpaul
6126236Swpaul