1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2021 Google LLC. */ 3 4 #include <test_progs.h> 5 #include "test_snprintf.skel.h" 6 #include "test_snprintf_single.skel.h" 7 8 #define EXP_NUM_OUT "-8 9 96 -424242 1337 DABBAD00" 9 #define EXP_NUM_RET sizeof(EXP_NUM_OUT) 10 11 #define EXP_IP_OUT "127.000.000.001 0000:0000:0000:0000:0000:0000:0000:0001" 12 #define EXP_IP_RET sizeof(EXP_IP_OUT) 13 14 /* The third specifier, %pB, depends on compiler inlining so don't check it */ 15 #define EXP_SYM_OUT "schedule schedule+0x0/" 16 #define MIN_SYM_RET sizeof(EXP_SYM_OUT) 17 18 /* The third specifier, %p, is a hashed pointer which changes on every reboot */ 19 #define EXP_ADDR_OUT "0000000000000000 ffff00000add4e55 " 20 #define EXP_ADDR_RET sizeof(EXP_ADDR_OUT "unknownhashedptr") 21 22 #define EXP_STR_OUT "str1 a b c d e longstr" 23 #define EXP_STR_RET sizeof(EXP_STR_OUT) 24 25 #define EXP_OVER_OUT "%over" 26 #define EXP_OVER_RET 10 27 28 #define EXP_PAD_OUT " 4 000" 29 #define EXP_PAD_RET 900007 30 31 #define EXP_NO_ARG_OUT "simple case" 32 #define EXP_NO_ARG_RET 12 33 34 #define EXP_NO_BUF_RET 29 35 36 static void test_snprintf_positive(void) 37 { 38 char exp_addr_out[] = EXP_ADDR_OUT; 39 char exp_sym_out[] = EXP_SYM_OUT; 40 struct test_snprintf *skel; 41 42 skel = test_snprintf__open_and_load(); 43 if (!ASSERT_OK_PTR(skel, "skel_open")) 44 return; 45 46 skel->bss->pid = getpid(); 47 48 if (!ASSERT_OK(test_snprintf__attach(skel), "skel_attach")) 49 goto cleanup; 50 51 /* trigger tracepoint */ 52 usleep(1); 53 54 ASSERT_STREQ(skel->bss->num_out, EXP_NUM_OUT, "num_out"); 55 ASSERT_EQ(skel->bss->num_ret, EXP_NUM_RET, "num_ret"); 56 57 ASSERT_STREQ(skel->bss->ip_out, EXP_IP_OUT, "ip_out"); 58 ASSERT_EQ(skel->bss->ip_ret, EXP_IP_RET, "ip_ret"); 59 60 ASSERT_OK(memcmp(skel->bss->sym_out, exp_sym_out, 61 sizeof(exp_sym_out) - 1), "sym_out"); 62 ASSERT_LT(MIN_SYM_RET, skel->bss->sym_ret, "sym_ret"); 63 64 ASSERT_OK(memcmp(skel->bss->addr_out, exp_addr_out, 65 sizeof(exp_addr_out) - 1), "addr_out"); 66 ASSERT_EQ(skel->bss->addr_ret, EXP_ADDR_RET, "addr_ret"); 67 68 ASSERT_STREQ(skel->bss->str_out, EXP_STR_OUT, "str_out"); 69 ASSERT_EQ(skel->bss->str_ret, EXP_STR_RET, "str_ret"); 70 71 ASSERT_STREQ(skel->bss->over_out, EXP_OVER_OUT, "over_out"); 72 ASSERT_EQ(skel->bss->over_ret, EXP_OVER_RET, "over_ret"); 73 74 ASSERT_STREQ(skel->bss->pad_out, EXP_PAD_OUT, "pad_out"); 75 ASSERT_EQ(skel->bss->pad_ret, EXP_PAD_RET, "pad_ret"); 76 77 ASSERT_STREQ(skel->bss->noarg_out, EXP_NO_ARG_OUT, "no_arg_out"); 78 ASSERT_EQ(skel->bss->noarg_ret, EXP_NO_ARG_RET, "no_arg_ret"); 79 80 ASSERT_EQ(skel->bss->nobuf_ret, EXP_NO_BUF_RET, "no_buf_ret"); 81 82 cleanup: 83 test_snprintf__destroy(skel); 84 } 85 86 /* Loads an eBPF object calling bpf_snprintf with up to 10 characters of fmt */ 87 static int load_single_snprintf(char *fmt) 88 { 89 struct test_snprintf_single *skel; 90 int ret; 91 92 skel = test_snprintf_single__open(); 93 if (!skel) 94 return -EINVAL; 95 96 memcpy(skel->rodata->fmt, fmt, MIN(strlen(fmt) + 1, 10)); 97 98 ret = test_snprintf_single__load(skel); 99 test_snprintf_single__destroy(skel); 100 101 return ret; 102 } 103 104 static void test_snprintf_negative(void) 105 { 106 ASSERT_OK(load_single_snprintf("valid %d"), "valid usage"); 107 108 ASSERT_ERR(load_single_snprintf("0123456789"), "no terminating zero"); 109 ASSERT_ERR(load_single_snprintf("%d %d"), "too many specifiers"); 110 ASSERT_ERR(load_single_snprintf("%pi5"), "invalid specifier 1"); 111 ASSERT_ERR(load_single_snprintf("%a"), "invalid specifier 2"); 112 ASSERT_ERR(load_single_snprintf("%"), "invalid specifier 3"); 113 ASSERT_ERR(load_single_snprintf("%12345678"), "invalid specifier 4"); 114 ASSERT_ERR(load_single_snprintf("%--------"), "invalid specifier 5"); 115 ASSERT_ERR(load_single_snprintf("%lc"), "invalid specifier 6"); 116 ASSERT_ERR(load_single_snprintf("%llc"), "invalid specifier 7"); 117 ASSERT_ERR(load_single_snprintf("\x80"), "non ascii character"); 118 ASSERT_ERR(load_single_snprintf("\x1"), "non printable character"); 119 } 120 121 void test_snprintf(void) 122 { 123 if (test__start_subtest("snprintf_positive")) 124 test_snprintf_positive(); 125 if (test__start_subtest("snprintf_negative")) 126 test_snprintf_negative(); 127 } 128