4
0

reloc.bpf.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // clang-format off
  2. #include <vmlinux.h>
  3. #include <bpf/bpf_helpers.h>
  4. #include <bpf/bpf_core_read.h>
  5. // clang-format on
  6. char _license[] SEC("license") = "GPL";
  7. struct {
  8. __uint(type, BPF_MAP_TYPE_ARRAY);
  9. __type(key, __u32);
  10. __type(value, __u64);
  11. __uint(max_entries, 1);
  12. } output_map SEC(".maps");
  13. long set_output(__u64 value) {
  14. __u32 key = 0;
  15. return bpf_map_update_elem(&output_map, &key, &value, BPF_ANY);
  16. }
  17. struct relocated_struct_with_scalars {
  18. #ifndef TARGET
  19. __u8 a;
  20. #endif
  21. __u8 b;
  22. __u8 c;
  23. #ifdef TARGET
  24. __u8 d;
  25. #endif
  26. };
  27. __noinline int field_global() {
  28. struct relocated_struct_with_scalars s = {1, 2, 3};
  29. return set_output(__builtin_preserve_access_index(s.b));
  30. }
  31. SEC("uprobe") int field(void *ctx) { return field_global(); }
  32. struct relocated_struct_with_pointer {
  33. #ifndef TARGET
  34. struct relocated_struct_with_pointer *first;
  35. #endif
  36. struct relocated_struct_with_pointer *second;
  37. #ifdef TARGET
  38. struct relocated_struct_with_pointer *first;
  39. #endif
  40. };
  41. __noinline int pointer_global() {
  42. struct relocated_struct_with_pointer s = {
  43. (struct relocated_struct_with_pointer *)42,
  44. (struct relocated_struct_with_pointer *)21,
  45. };
  46. return set_output((__u64)__builtin_preserve_access_index(s.first));
  47. }
  48. SEC("uprobe") int pointer(void *ctx) { return pointer_global(); }
  49. __noinline int struct_flavors_global() {
  50. struct relocated_struct_with_scalars s = {1, 2, 3};
  51. #ifndef TARGET
  52. if (bpf_core_field_exists(s.a)) {
  53. return set_output(__builtin_preserve_access_index(s.a));
  54. #else
  55. if (bpf_core_field_exists(s.d)) {
  56. return set_output(__builtin_preserve_access_index(s.d));
  57. #endif
  58. } else {
  59. return set_output(__builtin_preserve_access_index(s.c));
  60. }
  61. }
  62. SEC("uprobe") int struct_flavors(void *ctx) { return struct_flavors_global(); }
  63. enum relocated_enum_unsigned_32 {
  64. U32_VAL =
  65. #ifndef TARGET
  66. 0xAAAAAAAA
  67. #else
  68. 0xBBBBBBBB
  69. #endif
  70. };
  71. __noinline int enum_unsigned_32_global() {
  72. return set_output(
  73. bpf_core_enum_value(enum relocated_enum_unsigned_32, U32_VAL));
  74. }
  75. SEC("uprobe") int enum_unsigned_32(void *ctx) {
  76. return enum_unsigned_32_global();
  77. }
  78. enum relocated_enum_unsigned_32_checked_variants {
  79. #ifndef TARGET
  80. U32_VAL_A = 0xAAAAAAAA,
  81. #endif
  82. U32_VAL_B = 0xBBBBBBBB,
  83. #ifdef TARGET
  84. U32_VAL_C = 0xCCCCCCCC
  85. #endif
  86. };
  87. __noinline int enum_unsigned_32_checked_variants_global() {
  88. #ifndef TARGET
  89. if (bpf_core_enum_value_exists(
  90. enum relocated_enum_unsigned_32_checked_variants, U32_VAL_A)) {
  91. return set_output(bpf_core_enum_value(
  92. enum relocated_enum_unsigned_32_checked_variants, U32_VAL_A));
  93. #else
  94. if (bpf_core_enum_value_exists(
  95. enum relocated_enum_unsigned_32_checked_variants, U32_VAL_C)) {
  96. return set_output(bpf_core_enum_value(
  97. enum relocated_enum_unsigned_32_checked_variants, U32_VAL_C));
  98. #endif
  99. } else {
  100. return set_output(bpf_core_enum_value(
  101. enum relocated_enum_unsigned_32_checked_variants, U32_VAL_B));
  102. }
  103. }
  104. SEC("uprobe") int enum_unsigned_32_checked_variants(void *ctx) {
  105. return enum_unsigned_32_checked_variants_global();
  106. }
  107. enum relocated_enum_signed_32 {
  108. S32_VAL =
  109. #ifndef TARGET
  110. -0x7AAAAAAA
  111. #else
  112. -0x7BBBBBBB
  113. #endif
  114. };
  115. __noinline int enum_signed_32_global() {
  116. return set_output(
  117. bpf_core_enum_value(enum relocated_enum_signed_32, S32_VAL));
  118. }
  119. SEC("uprobe") int enum_signed_32(void *ctx) { return enum_signed_32_global(); }
  120. enum relocated_enum_signed_32_checked_variants {
  121. #ifndef TARGET
  122. S32_VAL_A = -0x7AAAAAAA,
  123. #endif
  124. S32_VAL_B = -0x7BBBBBBB,
  125. #ifdef TARGET
  126. S32_VAL_C = -0x7CCCCCCC
  127. #endif
  128. };
  129. __noinline int enum_signed_32_checked_variants_global() {
  130. #ifndef TARGET
  131. if (bpf_core_enum_value_exists(enum relocated_enum_signed_32_checked_variants,
  132. S32_VAL_A)) {
  133. return set_output(bpf_core_enum_value(
  134. enum relocated_enum_signed_32_checked_variants, S32_VAL_A));
  135. #else
  136. if (bpf_core_enum_value_exists(enum relocated_enum_signed_32_checked_variants,
  137. S32_VAL_C)) {
  138. return set_output(bpf_core_enum_value(
  139. enum relocated_enum_signed_32_checked_variants, S32_VAL_C));
  140. #endif
  141. } else {
  142. return set_output(bpf_core_enum_value(
  143. enum relocated_enum_signed_32_checked_variants, S32_VAL_B));
  144. }
  145. }
  146. SEC("uprobe") int enum_signed_32_checked_variants(void *ctx) {
  147. return enum_signed_32_checked_variants_global();
  148. }
  149. enum relocated_enum_unsigned_64 {
  150. U64_VAL =
  151. #ifndef TARGET
  152. 0xAAAAAAAABBBBBBBB
  153. #else
  154. 0xCCCCCCCCDDDDDDDD
  155. #endif
  156. };
  157. __noinline int enum_unsigned_64_global() {
  158. return set_output(
  159. bpf_core_enum_value(enum relocated_enum_unsigned_64, U64_VAL));
  160. }
  161. SEC("uprobe") int enum_unsigned_64(void *ctx) {
  162. return enum_unsigned_64_global();
  163. }
  164. enum relocated_enum_unsigned_64_checked_variants {
  165. #ifndef TARGET
  166. U64_VAL_A = 0xAAAAAAAABBBBBBBB,
  167. #endif
  168. U64_VAL_B = 0xCCCCCCCCDDDDDDDD,
  169. #ifdef TARGET
  170. U64_VAL_C = 0xEEEEEEEEFFFFFFFF
  171. #endif
  172. };
  173. __noinline int enum_unsigned_64_checked_variants_global() {
  174. #ifndef TARGET
  175. if (bpf_core_enum_value_exists(
  176. enum relocated_enum_unsigned_64_checked_variants, U64_VAL_A)) {
  177. return set_output(bpf_core_enum_value(
  178. enum relocated_enum_unsigned_64_checked_variants, U64_VAL_A));
  179. #else
  180. if (bpf_core_enum_value_exists(
  181. enum relocated_enum_unsigned_64_checked_variants, U64_VAL_C)) {
  182. return set_output(bpf_core_enum_value(
  183. enum relocated_enum_unsigned_64_checked_variants, U64_VAL_C));
  184. #endif
  185. } else {
  186. return set_output(bpf_core_enum_value(
  187. enum relocated_enum_unsigned_64_checked_variants, U64_VAL_B));
  188. }
  189. }
  190. SEC("uprobe") int enum_unsigned_64_checked_variants(void *ctx) {
  191. return enum_unsigned_64_checked_variants_global();
  192. }
  193. enum relocated_enum_signed_64 {
  194. S64_VAL =
  195. #ifndef TARGET
  196. -0xAAAAAAABBBBBBBB
  197. #else
  198. -0xCCCCCCCDDDDDDDD
  199. #endif
  200. };
  201. __noinline int enum_signed_64_global() {
  202. return set_output(
  203. bpf_core_enum_value(enum relocated_enum_signed_64, S64_VAL));
  204. }
  205. SEC("uprobe") int enum_signed_64(void *ctx) { return enum_signed_64_global(); }
  206. enum relocated_enum_signed_64_checked_variants {
  207. #ifndef TARGET
  208. S64_VAL_A = -0xAAAAAAABBBBBBB,
  209. #endif
  210. S64_VAL_B = -0xCCCCCCCDDDDDDD,
  211. #ifdef TARGET
  212. S64_VAL_C = -0xEEEEEEEFFFFFFF
  213. #endif
  214. };
  215. __noinline int enum_signed_64_checked_variants_global() {
  216. #ifndef TARGET
  217. if (bpf_core_enum_value_exists(enum relocated_enum_signed_64_checked_variants,
  218. S64_VAL_A)) {
  219. return set_output(bpf_core_enum_value(
  220. enum relocated_enum_signed_64_checked_variants, S64_VAL_A));
  221. #else
  222. if (bpf_core_enum_value_exists(enum relocated_enum_signed_64_checked_variants,
  223. S64_VAL_C)) {
  224. return set_output(bpf_core_enum_value(
  225. enum relocated_enum_signed_64_checked_variants, S64_VAL_C));
  226. #endif
  227. } else {
  228. return set_output(bpf_core_enum_value(
  229. enum relocated_enum_signed_64_checked_variants, S64_VAL_B));
  230. }
  231. }
  232. SEC("uprobe") int enum_signed_64_checked_variants(void *ctx) {
  233. return enum_signed_64_checked_variants_global();
  234. }