| 1 | |
| 2 | #include <test_progs.h>
|
| 3 |
|
| 4 | |
| 5 | * Test that replacing an inner percpu array map with one that has different
|
| 6 | * max_entries is rejected. percpu_array_map_gen_lookup() inlines the
|
| 7 | * template's index_mask, so allowing a smaller replacement would cause OOB.
|
| 8 |
|
| 9 | void test_percpu_array_inner_map(void)
|
| 10 | {
|
| 11 | LIBBPF_OPTS(bpf_map_create_opts, opts);
|
| 12 | int outer_fd, tmpl_fd, good_fd, bad_fd, err;
|
| 13 | int zero = 0;
|
| 14 |
|
| 15 |
|
| 16 | tmpl_fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, "tmpl",
|
| 17 | sizeof(int), sizeof(long), 8, NULL);
|
| 18 | if (!ASSERT_OK_FD(tmpl_fd, "create_tmpl"))
|
| 19 | return;
|
| 20 |
|
| 21 |
|
| 22 | opts.inner_map_fd = tmpl_fd;
|
| 23 | outer_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY_OF_MAPS, "outer",
|
| 24 | sizeof(int), sizeof(int), 1, &opts);
|
| 25 | if (!ASSERT_OK_FD(outer_fd, "create_outer"))
|
| 26 | goto close_tmpl;
|
| 27 |
|
| 28 |
|
| 29 | err = bpf_map_update_elem(outer_fd, &zero, &tmpl_fd, 0);
|
| 30 | if (!ASSERT_OK(err, "insert_tmpl"))
|
| 31 | goto close_outer;
|
| 32 |
|
| 33 |
|
| 34 | good_fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, "good",
|
| 35 | sizeof(int), sizeof(long), 8, NULL);
|
| 36 | if (!ASSERT_OK_FD(good_fd, "create_good"))
|
| 37 | goto close_outer;
|
| 38 |
|
| 39 | err = bpf_map_update_elem(outer_fd, &zero, &good_fd, 0);
|
| 40 | ASSERT_OK(err, "replace_same_max_entries");
|
| 41 | close(good_fd);
|
| 42 |
|
| 43 |
|
| 44 | bad_fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, "bad",
|
| 45 | sizeof(int), sizeof(long), 2, NULL);
|
| 46 | if (!ASSERT_OK_FD(bad_fd, "create_bad"))
|
| 47 | goto close_outer;
|
| 48 |
|
| 49 | err = bpf_map_update_elem(outer_fd, &zero, &bad_fd, 0);
|
| 50 | ASSERT_ERR(err, "replace_smaller_max_entries");
|
| 51 | close(bad_fd);
|
| 52 |
|
| 53 | close_outer:
|
| 54 | close(outer_fd);
|
| 55 | close_tmpl:
|
| 56 | close(tmpl_fd);
|
| 57 | }
|