File: restrict_function_pointers.cpp

package info (click to toggle)
cbmc 6.6.0-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,852 kB
  • sloc: cpp: 386,459; ansic: 114,466; java: 28,405; python: 6,003; yacc: 4,552; makefile: 4,041; lex: 2,487; xml: 2,388; sh: 2,050; perl: 557; pascal: 184; javascript: 163; ada: 36
file content (300 lines) | stat: -rw-r--r-- 8,223 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*******************************************************************\

Module: Restrict function pointers unit tests

Author: Daniel Poetzl

\*******************************************************************/

#include <testing-utils/get_goto_model_from_c.h>
#include <testing-utils/message.h>
#include <testing-utils/use_catch.h>

#include <goto-programs/label_function_pointer_call_sites.h>
#include <goto-programs/restrict_function_pointers.h>

#include <json/json_parser.h>

class fp_restrictionst : public function_pointer_restrictionst
{
  friend void restriction_parsing_test();
  friend void merge_restrictions_test();
  friend void get_function_pointer_by_name_restrictions_test();
};

void restriction_parsing_test()
{
  goto_modelt goto_model;

  {
    const auto res = fp_restrictionst::parse_function_pointer_restriction(
      "func1/func2", "test", goto_model);
    REQUIRE(res.first == "func1");
    REQUIRE(res.second.size() == 1);
    REQUIRE(res.second.find("func2") != res.second.end());
  }

  {
    const auto res = fp_restrictionst::parse_function_pointer_restriction(
      "func1/func2,func3", "test", goto_model);
    REQUIRE(res.first == "func1");
    REQUIRE(res.second.size() == 2);
    REQUIRE(res.second.find("func2") != res.second.end());
    REQUIRE(res.second.find("func3") != res.second.end());
  }

  REQUIRE_THROWS_AS(
    fp_restrictionst::parse_function_pointer_restriction(
      "func", "test", goto_model),
    invalid_restriction_exceptiont);

  REQUIRE_THROWS_AS(
    fp_restrictionst::parse_function_pointer_restriction(
      "/func", "test", goto_model),
    invalid_restriction_exceptiont);

  REQUIRE_THROWS_AS(
    fp_restrictionst::parse_function_pointer_restriction(
      "func/", "test", goto_model),
    invalid_restriction_exceptiont);

  REQUIRE_THROWS_AS(
    fp_restrictionst::parse_function_pointer_restriction(
      "func/,", "test", goto_model),
    invalid_restriction_exceptiont);

  REQUIRE_THROWS_AS(
    fp_restrictionst::parse_function_pointer_restriction(
      "func1/func2,", "test", goto_model),
    invalid_restriction_exceptiont);

  REQUIRE_THROWS_AS(
    fp_restrictionst::parse_function_pointer_restriction(
      "func1/,func2", "test", goto_model),
    invalid_restriction_exceptiont);
}

void merge_restrictions_test()
{
  fp_restrictionst::restrictionst r1;
  r1.emplace("fp1", std::unordered_set<irep_idt>{"func1", "func2"});
  r1.emplace("fp2", std::unordered_set<irep_idt>{"func1"});

  fp_restrictionst::restrictionst r2;
  r2.emplace("fp1", std::unordered_set<irep_idt>{"func1", "func3"});

  fp_restrictionst::restrictionst result =
    fp_restrictionst::merge_function_pointer_restrictions(r1, r2);

  REQUIRE(result.size() == 2);

  const auto &fp1_restrictions = result.at("fp1");
  REQUIRE(fp1_restrictions.size() == 3);
  REQUIRE(fp1_restrictions.count("func1") == 1);
  REQUIRE(fp1_restrictions.count("func2") == 1);
  REQUIRE(fp1_restrictions.count("func3") == 1);

  const auto &fp2_restrictions = result.at("fp2");
  REQUIRE(fp2_restrictions.size() == 1);
  REQUIRE(fp2_restrictions.count("func1") == 1);
}

void get_function_pointer_by_name_restrictions_test()
{
  SECTION("Translate parameter restriction to indexed restriction")
  {
    const std::string code = R"(
      typedef void (*fp_t)(void);
      void f();

      void func(fp_t fp)
      {
        f(); // ignored

        fp();
      }

      void main() {}
    )";

    goto_modelt goto_model = get_goto_model_from_c(code);
    label_function_pointer_call_sites(goto_model);

    const auto restrictions =
      fp_restrictionst::get_function_pointer_by_name_restrictions(
        {"func::fp/g"}, goto_model);

    REQUIRE(restrictions.size() == 1);

    const auto set = restrictions.at("func.function_pointer_call.1");
    REQUIRE(set.size() == 1);
    REQUIRE(set.count("g") == 1);
  }

  SECTION("Translate local nested variable restriction to indexed restriction")
  {
    const std::string code = R"(
      typedef void (*fp_t)(void);
      void f();

      void main()
      {
        f(); // ignored

        {
          fp_t fp;
          fp();
        }
      }
    )";

    goto_modelt goto_model = get_goto_model_from_c(code);
    label_function_pointer_call_sites(goto_model);

    const auto restrictions =
      fp_restrictionst::get_function_pointer_by_name_restrictions(
        {"main::1::1::fp/g"}, goto_model);

    REQUIRE(restrictions.size() == 1);

    const auto set = restrictions.at("main.function_pointer_call.1");
    REQUIRE(set.size() == 1);
    REQUIRE(set.count("g") == 1);
  }

  SECTION("Translate global variable restriction to indexed restriction")
  {
    const std::string code = R"(
      typedef void (*fp_t)(void);
      void f();

      fp_t fp;

      void main()
      {
        f(); // ignored

        fp();
      }
    )";

    goto_modelt goto_model = get_goto_model_from_c(code);
    label_function_pointer_call_sites(goto_model);

    const auto restrictions =
      fp_restrictionst::get_function_pointer_by_name_restrictions(
        {"fp/g"}, goto_model);

    REQUIRE(restrictions.size() == 1);

    const auto set = restrictions.at("main.function_pointer_call.1");
    REQUIRE(set.size() == 1);
    REQUIRE(set.count("g") == 1);
  }

  SECTION(
    "Translate a variable restriction to indexed restrictions, "
    "for the case when a function pointer is called more than once")
  {
    const std::string code = R"(
      typedef void (*fp_t)(void);
      void f();

      fp_t fp;

      void main()
      {
        f(); // ignored

        fp();
        fp(); // second call to same function pointer
      }
    )";

    goto_modelt goto_model = get_goto_model_from_c(code);
    label_function_pointer_call_sites(goto_model);

    const auto restrictions =
      fp_restrictionst::get_function_pointer_by_name_restrictions(
        {"fp/g"}, goto_model);

    REQUIRE(restrictions.size() == 2);

    const auto set1 = restrictions.at("main.function_pointer_call.1");
    REQUIRE(set1.size() == 1);
    REQUIRE(set1.count("g") == 1);

    const auto set2 = restrictions.at("main.function_pointer_call.2");
    REQUIRE(set2.size() == 1);
    REQUIRE(set2.count("g") == 1);
  }
}

TEST_CASE("Restriction parsing", "[core]")
{
  restriction_parsing_test();
}

TEST_CASE("Merge function pointer restrictions", "[core]")
{
  merge_restrictions_test();
}

TEST_CASE("Json conversion", "[core]")
{
  // conversion json1 -> restrictions1 -> json2 -> restrictions2
  // then check that restrictions1 == restrictions2
  //
  // we use json as a starting point as it is easy to write, and we compare the
  // restrictions as it is a canonical representation (in contrast, the json
  // representation for the same restrictions can differ, due to the array
  // elements appearing in different orders)

  goto_modelt goto_model;
  std::istringstream ss(
    "{"
    "  \"use_f.function_pointer_call.1\": [\"f\", \"g\"],"
    "  \"use_f.function_pointer_call.2\": [\"h\"]"
    "}");

  jsont json1;

  parse_json(ss, "", null_message_handler, json1);

  // json1 -> restrictions1
  const auto function_pointer_restrictions1 =
    function_pointer_restrictionst::from_json(json1, goto_model);

  const auto &restrictions = function_pointer_restrictions1.restrictions;

  REQUIRE(restrictions.size() == 2);

  const auto &fp1_restrictions =
    restrictions.at("use_f.function_pointer_call.1");
  REQUIRE(fp1_restrictions.size() == 2);
  REQUIRE(fp1_restrictions.count("f") == 1);
  REQUIRE(fp1_restrictions.count("g") == 1);

  const auto &fp2_restrictions =
    restrictions.at("use_f.function_pointer_call.2");
  REQUIRE(fp2_restrictions.size() == 1);
  REQUIRE(fp2_restrictions.count("h") == 1);

  // restrictions1 -> json2
  const auto json2 = function_pointer_restrictions1.to_json();

  // json2 -> restrictions2
  const auto function_pointer_restrictions2 =
    function_pointer_restrictionst::from_json(json2, goto_model);

  REQUIRE(
    function_pointer_restrictions1.restrictions ==
    function_pointer_restrictions2.restrictions);
}

TEST_CASE(
  "Get function pointer by name restrictions",
  "[core][goto-programs][restrict-function-pointers]")
{
  get_function_pointer_by_name_restrictions_test();
}