Merge bitcoin/bitcoin#31848: test, tracing: don't use problematic bpf_usdt_readarg_p()

a0b66b4bff Revert "test: Disable known broken USDT test for now" (0xb10c)
ec47ba349d contrib: don't use bpf_usdt_readarg_p (0xb10c)
35ae6ff60f test: don't use bpf_usdt_readarg_p (0xb10c)

Pull request description:

  Instead of using the undocumented bcc helper `bpf_usdt_readarg_p()`, use [`bpf_usdt_readarg()`][1] and [`bpf_probe_read_user()`][2]/[`bpf_probe_read_user_str()`][3] as documented in the [bcc USDT reference guide][1].

  Note that the `bpf_probe_read_user()` documentation says the following:
  > For safety, all user address space memory reads must pass through bpf_probe_read_user().

  It's [assumed](https://github.com/bitcoin/bitcoin/issues/27380#issuecomment-2286505348) that using `bpf_usdt_readarg_p()` caused a lifetime issue. With `bpf_usdt_readarg()` and `bpf_probe_read_user()`, this doesn't [seem](https://github.com/bitcoin/bitcoin/issues/27380#issuecomment-2528671656) to be a problem anymore.

  This allows to revert faed533743 and closes #27380.

    [1]: https://github.com/iovisor/bcc/blob/master/docs/reference_guide.md#6-usdt-probes
    [2]: https://github.com/iovisor/bcc/blob/master/docs/reference_guide.md#10-bpf_probe_read_user
    [3]: https://github.com/iovisor/bcc/blob/master/docs/reference_guide.md#11-bpf_probe_read_user_str

ACKs for top commit:
  i-am-yuvi:
    Tested ACK a0b66b4bff
  willcl-ark:
    tACK a0b66b4bff

Tree-SHA512: 002a692ad81ef284d4a610bbc6da477d623bf4dae5134c3692431c56b71692fa597e280fddd411eadd08eae77f01f47e2dcd0f37c0081326abe11a75bb10d6a6
This commit is contained in:
glozow
2025-03-05 10:18:40 -05:00
8 changed files with 115 additions and 59 deletions

View File

@@ -78,6 +78,7 @@ BPF_PERF_OUTPUT(outbound_messages);
int trace_inbound_message(struct pt_regs *ctx) {
int idx = 0;
struct p2p_message *msg = msg_arr.lookup(&idx);
void *paddr = NULL, *pconn_type = NULL, *pmsg_type = NULL, *pmsg = NULL;
// lookup() does not return a NULL pointer. However, the BPF verifier
// requires an explicit check that that the `msg` pointer isn't a NULL
@@ -85,11 +86,15 @@ int trace_inbound_message(struct pt_regs *ctx) {
if (msg == NULL) return 1;
bpf_usdt_readarg(1, ctx, &msg->peer_id);
bpf_usdt_readarg_p(2, ctx, &msg->peer_addr, MAX_PEER_ADDR_LENGTH);
bpf_usdt_readarg_p(3, ctx, &msg->peer_conn_type, MAX_PEER_CONN_TYPE_LENGTH);
bpf_usdt_readarg_p(4, ctx, &msg->msg_type, MAX_MSG_TYPE_LENGTH);
bpf_usdt_readarg(2, ctx, &paddr);
bpf_probe_read_user_str(&msg->peer_addr, sizeof(msg->peer_addr), paddr);
bpf_usdt_readarg(3, ctx, &pconn_type);
bpf_probe_read_user_str(&msg->peer_conn_type, sizeof(msg->peer_conn_type), pconn_type);
bpf_usdt_readarg(4, ctx, &pmsg_type);
bpf_probe_read_user_str(&msg->msg_type, sizeof(msg->msg_type), pmsg_type);
bpf_usdt_readarg(5, ctx, &msg->msg_size);
bpf_usdt_readarg_p(6, ctx, &msg->msg, _TRACEPOINT_TEST_MIN(msg->msg_size, MAX_MSG_DATA_LENGTH));
bpf_usdt_readarg(6, ctx, &pmsg);
bpf_probe_read_user(&msg->msg, _TRACEPOINT_TEST_MIN(msg->msg_size, MAX_MSG_DATA_LENGTH), pmsg);
inbound_messages.perf_submit(ctx, msg, sizeof(*msg));
return 0;
@@ -99,17 +104,23 @@ int trace_outbound_message(struct pt_regs *ctx) {
int idx = 0;
struct p2p_message *msg = msg_arr.lookup(&idx);
void *paddr = NULL, *pconn_type = NULL, *pmsg_type = NULL, *pmsg = NULL;
// lookup() does not return a NULL pointer. However, the BPF verifier
// requires an explicit check that that the `msg` pointer isn't a NULL
// pointer. See https://github.com/iovisor/bcc/issues/2595
if (msg == NULL) return 1;
bpf_usdt_readarg(1, ctx, &msg->peer_id);
bpf_usdt_readarg_p(2, ctx, &msg->peer_addr, MAX_PEER_ADDR_LENGTH);
bpf_usdt_readarg_p(3, ctx, &msg->peer_conn_type, MAX_PEER_CONN_TYPE_LENGTH);
bpf_usdt_readarg_p(4, ctx, &msg->msg_type, MAX_MSG_TYPE_LENGTH);
bpf_usdt_readarg(2, ctx, &paddr);
bpf_probe_read_user_str(&msg->peer_addr, sizeof(msg->peer_addr), paddr);
bpf_usdt_readarg(3, ctx, &pconn_type);
bpf_probe_read_user_str(&msg->peer_conn_type, sizeof(msg->peer_conn_type), pconn_type);
bpf_usdt_readarg(4, ctx, &pmsg_type);
bpf_probe_read_user_str(&msg->msg_type, sizeof(msg->msg_type), pmsg_type);
bpf_usdt_readarg(5, ctx, &msg->msg_size);
bpf_usdt_readarg_p(6, ctx, &msg->msg, _TRACEPOINT_TEST_MIN(msg->msg_size, MAX_MSG_DATA_LENGTH));
bpf_usdt_readarg(6, ctx, &pmsg);
bpf_probe_read_user(&msg->msg, _TRACEPOINT_TEST_MIN(msg->msg_size, MAX_MSG_DATA_LENGTH), pmsg);
outbound_messages.perf_submit(ctx, msg, sizeof(*msg));
return 0;

View File

@@ -65,8 +65,9 @@ BPF_PERF_OUTPUT(replaced_events);
int trace_added(struct pt_regs *ctx) {
struct added_event added = {};
bpf_usdt_readarg_p(1, ctx, &added.hash, HASH_LENGTH);
void *phash = NULL;
bpf_usdt_readarg(1, ctx, phash);
bpf_probe_read_user(&added.hash, sizeof(added.hash), phash);
bpf_usdt_readarg(2, ctx, &added.vsize);
bpf_usdt_readarg(3, ctx, &added.fee);
@@ -76,9 +77,11 @@ int trace_added(struct pt_regs *ctx) {
int trace_removed(struct pt_regs *ctx) {
struct removed_event removed = {};
bpf_usdt_readarg_p(1, ctx, &removed.hash, HASH_LENGTH);
bpf_usdt_readarg_p(2, ctx, &removed.reason, MAX_REMOVAL_REASON_LENGTH);
void *phash = NULL, *preason = NULL;
bpf_usdt_readarg(1, ctx, phash);
bpf_probe_read_user(&removed.hash, sizeof(removed.hash), phash);
bpf_usdt_readarg(1, ctx, preason);
bpf_probe_read_user_str(&removed.reason, sizeof(removed.reason), preason);
bpf_usdt_readarg(3, ctx, &removed.vsize);
bpf_usdt_readarg(4, ctx, &removed.fee);
bpf_usdt_readarg(5, ctx, &removed.entry_time);
@@ -89,22 +92,25 @@ int trace_removed(struct pt_regs *ctx) {
int trace_rejected(struct pt_regs *ctx) {
struct rejected_event rejected = {};
bpf_usdt_readarg_p(1, ctx, &rejected.hash, HASH_LENGTH);
bpf_usdt_readarg_p(2, ctx, &rejected.reason, MAX_REJECT_REASON_LENGTH);
void *phash = NULL, *preason = NULL;
bpf_usdt_readarg(1, ctx, phash);
bpf_probe_read_user(&rejected.hash, sizeof(rejected.hash), phash);
bpf_usdt_readarg(1, ctx, preason);
bpf_probe_read_user_str(&rejected.reason, sizeof(rejected.reason), preason);
rejected_events.perf_submit(ctx, &rejected, sizeof(rejected));
return 0;
}
int trace_replaced(struct pt_regs *ctx) {
struct replaced_event replaced = {};
bpf_usdt_readarg_p(1, ctx, &replaced.replaced_hash, HASH_LENGTH);
void *phash_replaced = NULL, *phash_replacement = NULL;
bpf_usdt_readarg(1, ctx, phash_replaced);
bpf_probe_read_user(&replaced.replaced_hash, sizeof(replaced.replaced_hash), phash_replaced);
bpf_usdt_readarg(2, ctx, &replaced.replaced_vsize);
bpf_usdt_readarg(3, ctx, &replaced.replaced_fee);
bpf_usdt_readarg(4, ctx, &replaced.replaced_entry_time);
bpf_usdt_readarg_p(5, ctx, &replaced.replacement_hash, HASH_LENGTH);
bpf_usdt_readarg(5, ctx, phash_replacement);
bpf_probe_read_user(&replaced.replacement_hash, sizeof(replaced.replacement_hash), phash_replacement);
bpf_usdt_readarg(6, ctx, &replaced.replacement_vsize);
bpf_usdt_readarg(7, ctx, &replaced.replacement_fee);

View File

@@ -47,11 +47,15 @@ BPF_PERF_OUTPUT(outbound_messages);
int trace_inbound_message(struct pt_regs *ctx) {
struct p2p_message msg = {};
void *paddr = NULL, *pconn_type = NULL, *pmsg_type = NULL;
bpf_usdt_readarg(1, ctx, &msg.peer_id);
bpf_usdt_readarg_p(2, ctx, &msg.peer_addr, MAX_PEER_ADDR_LENGTH);
bpf_usdt_readarg_p(3, ctx, &msg.peer_conn_type, MAX_PEER_CONN_TYPE_LENGTH);
bpf_usdt_readarg_p(4, ctx, &msg.msg_type, MAX_MSG_TYPE_LENGTH);
bpf_usdt_readarg(2, ctx, &paddr);
bpf_probe_read_user_str(&msg.peer_addr, sizeof(msg.peer_addr), paddr);
bpf_usdt_readarg(3, ctx, &pconn_type);
bpf_probe_read_user_str(&msg.peer_conn_type, sizeof(msg.peer_conn_type), pconn_type);
bpf_usdt_readarg(4, ctx, &pconn_type);
bpf_probe_read_user_str(&msg.msg_type, sizeof(msg.msg_type), pmsg_type);
bpf_usdt_readarg(5, ctx, &msg.msg_size);
inbound_messages.perf_submit(ctx, &msg, sizeof(msg));
@@ -60,11 +64,15 @@ int trace_inbound_message(struct pt_regs *ctx) {
int trace_outbound_message(struct pt_regs *ctx) {
struct p2p_message msg = {};
void *paddr = NULL, *pconn_type = NULL, *pmsg_type = NULL;
bpf_usdt_readarg(1, ctx, &msg.peer_id);
bpf_usdt_readarg_p(2, ctx, &msg.peer_addr, MAX_PEER_ADDR_LENGTH);
bpf_usdt_readarg_p(3, ctx, &msg.peer_conn_type, MAX_PEER_CONN_TYPE_LENGTH);
bpf_usdt_readarg_p(4, ctx, &msg.msg_type, MAX_MSG_TYPE_LENGTH);
bpf_usdt_readarg(2, ctx, &paddr);
bpf_probe_read_user_str(&msg.peer_addr, sizeof(msg.peer_addr), paddr);
bpf_usdt_readarg(3, ctx, &pconn_type);
bpf_probe_read_user_str(&msg.peer_conn_type, sizeof(msg.peer_conn_type), pconn_type);
bpf_usdt_readarg(4, ctx, &pconn_type);
bpf_probe_read_user_str(&msg.msg_type, sizeof(msg.msg_type), pmsg_type);
bpf_usdt_readarg(5, ctx, &msg.msg_size);
outbound_messages.perf_submit(ctx, &msg, sizeof(msg));