• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

iovisor / ubpf / 21411922965

27 Jan 2026 07:53PM UTC coverage: 77.132% (-0.2%) from 77.302%
21411922965

Pull #719

github

web-flow
Merge 2ffae1b36 into 523d7bc07
Pull Request #719: Store bytecode in read-only memory pages

70 of 101 new or added lines in 2 files covered. (69.31%)

192 existing lines in 1 file now uncovered.

6982 of 9052 relevant lines covered (77.13%)

386589.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.32
/custom_tests/srcs/ubpf_test_readonly_bytecode.cc
1
// Copyright (c) uBPF contributors
2
// SPDX-License-Identifier: Apache-2.0
3

4
#include <cstdint>
5
#include <iostream>
6
#include <memory>
7
#include <string>
8

9
extern "C"
10
{
11
#include "ubpf.h"
12
}
13

14
#include "ubpf_custom_test_support.h"
15

16
/**
17
 * @brief Test that bytecode can be loaded and executed with read-only protection enabled.
18
 * 
19
 * This test verifies that:
20
 * 1. Bytecode can be loaded successfully when read-only mode is enabled (default)
21
 * 2. The VM can execute the bytecode correctly
22
 * 3. Toggling read-only mode works correctly
23
 */
24
int
25
main(int argc, char** argv)
10✔
26
{
27
    std::string program_string{};
10✔
28
    std::string error{};
10✔
29
    ubpf_jit_fn jit_fn;
30

31
    if (!get_program_string(argc, argv, program_string, error)) {
10✔
NEW
32
        std::cerr << error << std::endl;
×
NEW
33
        return 1;
×
34
    }
35

36
    // Test 1: Load and execute with read-only bytecode enabled (default)
37
    {
38
        std::unique_ptr<ubpf_vm, decltype(&ubpf_destroy)> vm(ubpf_create(), ubpf_destroy);
10✔
39
        if (!vm) {
10✔
NEW
40
            std::cerr << "Failed to create VM" << std::endl;
×
NEW
41
            return 1;
×
42
        }
43

44
        // Read-only bytecode should be enabled by default
45
        if (!ubpf_setup_custom_test(
12✔
46
                vm, program_string, [](ubpf_vm_up&, std::string&) { return true; }, jit_fn, error)) {
20✔
NEW
47
            std::cerr << "Failed to load program with read-only bytecode: " << error << std::endl;
×
NEW
48
            return 1;
×
49
        }
50

51
        // Execute the program to ensure read-only bytecode doesn't break execution
52
        uint64_t memory = 0x123456789;
10✔
53
        uint64_t bpf_return_value;
54
        if (ubpf_exec(vm.get(), &memory, sizeof(memory), &bpf_return_value)) {
10✔
NEW
55
            std::cerr << "Failed to execute program with read-only bytecode" << std::endl;
×
NEW
56
            return 1;
×
57
        }
58

59
        std::cout << "Test 1 PASSED: Bytecode loaded and executed with read-only protection" << std::endl;
10✔
60
    }
10✔
61

62
    // Test 2: Toggle read-only bytecode off and verify it can still load
63
    {
64
        std::unique_ptr<ubpf_vm, decltype(&ubpf_destroy)> vm(ubpf_create(), ubpf_destroy);
10✔
65
        if (!vm) {
10✔
NEW
66
            std::cerr << "Failed to create VM" << std::endl;
×
NEW
67
            return 1;
×
68
        }
69

70
        // Disable read-only bytecode
71
        bool was_enabled = ubpf_toggle_readonly_bytecode(vm.get(), false);
10✔
72
        if (!was_enabled) {
10✔
NEW
73
            std::cerr << "Read-only bytecode was not enabled by default" << std::endl;
×
NEW
74
            return 1;
×
75
        }
76

77
        if (!ubpf_setup_custom_test(
12✔
78
                vm, program_string, [](ubpf_vm_up&, std::string&) { return true; }, jit_fn, error)) {
20✔
NEW
79
            std::cerr << "Failed to load program without read-only bytecode: " << error << std::endl;
×
NEW
80
            return 1;
×
81
        }
82

83
        // Execute the program
84
        uint64_t memory = 0x123456789;
10✔
85
        uint64_t bpf_return_value;
86
        if (ubpf_exec(vm.get(), &memory, sizeof(memory), &bpf_return_value)) {
10✔
NEW
87
            std::cerr << "Failed to execute program without read-only bytecode" << std::endl;
×
NEW
88
            return 1;
×
89
        }
90

91
        std::cout << "Test 2 PASSED: Bytecode loaded and executed without read-only protection" << std::endl;
10✔
92
    }
10✔
93

94
    // Test 3: Toggle read-only bytecode back on
95
    {
96
        std::unique_ptr<ubpf_vm, decltype(&ubpf_destroy)> vm(ubpf_create(), ubpf_destroy);
10✔
97
        if (!vm) {
10✔
NEW
98
            std::cerr << "Failed to create VM" << std::endl;
×
NEW
99
            return 1;
×
100
        }
101

102
        // Verify it's enabled by default
103
        bool was_enabled = ubpf_toggle_readonly_bytecode(vm.get(), false);
10✔
104
        if (!was_enabled) {
10✔
NEW
105
            std::cerr << "Read-only bytecode was not enabled by default" << std::endl;
×
NEW
106
            return 1;
×
107
        }
108

109
        // Re-enable it
110
        was_enabled = ubpf_toggle_readonly_bytecode(vm.get(), true);
10✔
111
        if (was_enabled) {
10✔
NEW
112
            std::cerr << "Read-only bytecode should have been disabled" << std::endl;
×
NEW
113
            return 1;
×
114
        }
115

116
        if (!ubpf_setup_custom_test(
12✔
117
                vm, program_string, [](ubpf_vm_up&, std::string&) { return true; }, jit_fn, error)) {
20✔
NEW
118
            std::cerr << "Failed to load program after re-enabling read-only: " << error << std::endl;
×
NEW
119
            return 1;
×
120
        }
121

122
        std::cout << "Test 3 PASSED: Toggle functionality works correctly" << std::endl;
10✔
123
    }
10✔
124

125
    std::cout << "All tests PASSED!" << std::endl;
10✔
126
    return 0;
10✔
127
}
10✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc