forked from diffblue/cbmc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremove_asm.cpp
615 lines (520 loc) · 17.4 KB
/
remove_asm.cpp
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*******************************************************************\
Module: Remove 'asm' statements by compiling them into suitable
standard goto program instructions
Author: Daniel Kroening
Date: December 2014
\*******************************************************************/
/// \file
/// Remove 'asm' statements by compiling them into suitable standard goto
/// program instructions
#include "remove_asm.h"
#include <util/c_types.h>
#include <util/pointer_expr.h>
#include <util/prefix.h>
#include <util/range.h>
#include <util/std_code.h>
#include <util/string_constant.h>
#include <goto-programs/goto_model.h>
#include <goto-programs/remove_skip.h>
#include "assembler_parser.h"
class remove_asmt
{
public:
remove_asmt(
symbol_tablet &_symbol_table,
goto_functionst &_goto_functions,
message_handlert &message_handler)
: symbol_table(_symbol_table),
goto_functions(_goto_functions),
message_handler(message_handler)
{
}
void operator()()
{
for(auto &f : goto_functions.function_map)
process_function(f.first, f.second);
}
protected:
symbol_tablet &symbol_table;
goto_functionst &goto_functions;
message_handlert &message_handler;
void process_function(const irep_idt &, goto_functionst::goto_functiont &);
void process_instruction(
const irep_idt &function_id,
goto_programt::instructiont &instruction,
goto_programt &dest);
void process_instruction_gcc(const code_asm_gcct &, goto_programt &dest);
void process_instruction_msc(
const irep_idt &,
const code_asmt &,
goto_programt &dest);
void gcc_asm_function_call(
const irep_idt &function_base_name,
const code_asm_gcct &code,
std::size_t n_args,
goto_programt &dest);
void msc_asm_function_call(
const irep_idt &function_base_name,
const exprt::operandst &operands,
const code_asmt &code,
goto_programt &dest);
};
/// Adds a call to a library function that implements the given gcc-style inline
/// assembly statement
///
/// \param function_base_name: Name of the function to call
/// \param code: gcc-style inline assembly statement to translate to function
/// call
/// \param n_args: Number of arguments required by \p function_base_name
/// \param dest: Goto program to append the function call to
void remove_asmt::gcc_asm_function_call(
const irep_idt &function_base_name,
const code_asm_gcct &code,
std::size_t n_args,
goto_programt &dest)
{
irep_idt function_identifier = function_base_name;
code_function_callt::argumentst arguments;
const typet void_pointer = pointer_type(empty_typet());
// outputs
forall_operands(it, code.outputs())
{
if(it->operands().size() == 2)
{
arguments.push_back(typecast_exprt(
address_of_exprt(to_binary_expr(*it).op1()), void_pointer));
}
}
// inputs
forall_operands(it, code.inputs())
{
if(it->operands().size() == 2)
{
arguments.push_back(typecast_exprt(
address_of_exprt(to_binary_expr(*it).op1()), void_pointer));
}
}
// An inline asm statement may consist of multiple commands, not all of which
// use all of the inputs/outputs of the inline asm statement.
DATA_INVARIANT_WITH_DIAGNOSTICS(
arguments.size() >= n_args,
"insufficient number of arguments for calling " +
id2string(function_identifier),
"required arguments: " + std::to_string(n_args),
code.pretty());
arguments.resize(n_args);
code_typet fkt_type{
code_typet::parameterst{
arguments.size(), code_typet::parametert{void_pointer}},
empty_typet()};
auto fkt = symbol_exprt{function_identifier, fkt_type}.with_source_location(
code.source_location());
code_function_callt function_call(std::move(fkt), std::move(arguments));
dest.add(
goto_programt::make_function_call(function_call, code.source_location()));
// do we have it?
if(!symbol_table.has_symbol(function_identifier))
{
symbolt symbol{function_identifier, fkt_type, ID_C};
symbol.base_name = function_base_name;
symbol_table.add(symbol);
goto_functions.function_map.emplace(function_identifier, goto_functiont());
}
else
{
DATA_INVARIANT_WITH_DIAGNOSTICS(
symbol_table.lookup_ref(function_identifier).type == fkt_type,
"types of function " + id2string(function_identifier) + " should match",
code.pretty(),
symbol_table.lookup_ref(function_identifier).type.pretty(),
fkt_type.pretty());
}
}
/// Adds a call to a library function that implements the given msc-style inline
/// assembly statement
///
/// \param function_base_name: Name of the function to call
/// \param operands: Arguments to be passed to function
/// \param code: msc-style inline assembly statement to translate to function
/// call
/// \param dest: Goto program to append the function call to
void remove_asmt::msc_asm_function_call(
const irep_idt &function_base_name,
const exprt::operandst &operands,
const code_asmt &code,
goto_programt &dest)
{
irep_idt function_identifier = function_base_name;
code_function_callt::argumentst arguments;
const typet void_pointer = pointer_type(empty_typet());
for(const auto &op : operands)
arguments.push_back(typecast_exprt::conditional_cast(op, void_pointer));
code_typet fkt_type{
code_typet::parameterst{
arguments.size(), code_typet::parametert{void_pointer}},
empty_typet()};
auto fkt = symbol_exprt{function_identifier, fkt_type}.with_source_location(
code.source_location());
code_function_callt function_call(std::move(fkt), std::move(arguments));
dest.add(
goto_programt::make_function_call(function_call, code.source_location()));
// do we have it?
if(!symbol_table.has_symbol(function_identifier))
{
symbolt symbol{function_identifier, fkt_type, ID_C};
symbol.base_name = function_base_name;
symbol_table.add(symbol);
goto_functions.function_map.emplace(function_identifier, goto_functiont());
}
else
{
DATA_INVARIANT(
symbol_table.lookup_ref(function_identifier).type == fkt_type,
"function types should match");
}
}
/// Translates the given inline assembly code (which must be in either gcc or
/// msc style) to non-assembly goto program instructions
///
/// \param function_id: Name of function being processed
/// \param instruction: The goto program instruction containing the inline
/// assembly statements
/// \param dest: The goto program to append the new instructions to
void remove_asmt::process_instruction(
const irep_idt &function_id,
goto_programt::instructiont &instruction,
goto_programt &dest)
{
const code_asmt &code = to_code_asm(instruction.get_other());
const irep_idt &flavor = code.get_flavor();
if(flavor == ID_gcc)
process_instruction_gcc(to_code_asm_gcc(code), dest);
else if(flavor == ID_msc)
process_instruction_msc(function_id, code, dest);
else
DATA_INVARIANT(false, "unexpected assembler flavor");
}
/// Translates the given inline assembly code (in gcc style) to non-assembly
/// goto program instructions
///
/// \param code: The inline assembly code statement to translate
/// \param dest: The goto program to append the new instructions to
void remove_asmt::process_instruction_gcc(
const code_asm_gcct &code,
goto_programt &dest)
{
const irep_idt &i_str = to_string_constant(code.asm_text()).value();
std::istringstream str(id2string(i_str));
assembler_parsert assembler_parser{message_handler};
assembler_parser.in = &str;
assembler_parser.parse();
goto_programt tmp_dest;
bool unknown = false;
bool x86_32_locked_atomic = false;
for(const auto &instruction : assembler_parser.instructions)
{
if(instruction.empty())
continue;
#if 0
std::cout << "A ********************\n";
for(const auto &ins : instruction)
{
std::cout << "XX: " << ins.pretty() << '\n';
}
std::cout << "B ********************\n";
#endif
// deal with prefixes
irep_idt command;
unsigned pos = 0;
if(
instruction.front().id() == ID_symbol &&
instruction.front().get(ID_identifier) == "lock")
{
x86_32_locked_atomic = true;
pos++;
}
// done?
if(pos == instruction.size())
continue;
if(instruction[pos].id() == ID_symbol)
{
command = instruction[pos].get(ID_identifier);
pos++;
}
if(command == "xchg" || command == "xchgl")
x86_32_locked_atomic = true;
if(x86_32_locked_atomic)
{
tmp_dest.add(goto_programt::make_atomic_begin(code.source_location()));
codet code_fence(ID_fence);
code_fence.add_source_location() = code.source_location();
code_fence.set(ID_WWfence, true);
code_fence.set(ID_RRfence, true);
code_fence.set(ID_RWfence, true);
code_fence.set(ID_WRfence, true);
tmp_dest.add(
goto_programt::make_other(code_fence, code.source_location()));
}
if(command == "fstcw" || command == "fnstcw" || command == "fldcw") // x86
{
gcc_asm_function_call("__asm_" + id2string(command), code, 1, tmp_dest);
}
else if(
command == "mfence" || command == "lfence" || command == "sfence") // x86
{
gcc_asm_function_call("__asm_" + id2string(command), code, 0, tmp_dest);
}
else if(command == ID_sync) // Power
{
codet code_fence(ID_fence);
code_fence.add_source_location() = code.source_location();
code_fence.set(ID_WWfence, true);
code_fence.set(ID_RRfence, true);
code_fence.set(ID_RWfence, true);
code_fence.set(ID_WRfence, true);
code_fence.set(ID_WWcumul, true);
code_fence.set(ID_RWcumul, true);
code_fence.set(ID_RRcumul, true);
code_fence.set(ID_WRcumul, true);
tmp_dest.add(
goto_programt::make_other(code_fence, code.source_location()));
}
else if(command == ID_lwsync) // Power
{
codet code_fence(ID_fence);
code_fence.add_source_location() = code.source_location();
code_fence.set(ID_WWfence, true);
code_fence.set(ID_RRfence, true);
code_fence.set(ID_RWfence, true);
code_fence.set(ID_WWcumul, true);
code_fence.set(ID_RWcumul, true);
code_fence.set(ID_RRcumul, true);
tmp_dest.add(
goto_programt::make_other(code_fence, code.source_location()));
}
else if(command == ID_isync) // Power
{
codet code_fence(ID_fence);
code_fence.add_source_location() = code.source_location();
tmp_dest.add(
goto_programt::make_other(code_fence, code.source_location()));
// doesn't do anything by itself,
// needs to be combined with branch
}
else if(command == "dmb" || command == "dsb") // ARM
{
codet code_fence(ID_fence);
code_fence.add_source_location() = code.source_location();
code_fence.set(ID_WWfence, true);
code_fence.set(ID_RRfence, true);
code_fence.set(ID_RWfence, true);
code_fence.set(ID_WRfence, true);
code_fence.set(ID_WWcumul, true);
code_fence.set(ID_RWcumul, true);
code_fence.set(ID_RRcumul, true);
code_fence.set(ID_WRcumul, true);
tmp_dest.add(
goto_programt::make_other(code_fence, code.source_location()));
}
else if(command == "isb") // ARM
{
codet code_fence(ID_fence);
code_fence.add_source_location() = code.source_location();
tmp_dest.add(
goto_programt::make_other(code_fence, code.source_location()));
// doesn't do anything by itself,
// needs to be combined with branch
}
else
unknown = true; // give up
if(x86_32_locked_atomic)
{
tmp_dest.add(goto_programt::make_atomic_end(code.source_location()));
x86_32_locked_atomic = false;
}
}
if(unknown)
{
// we give up; we should perhaps print a warning
}
else
dest.destructive_append(tmp_dest);
}
/// Translates the given inline assembly code (in msc style) to non-assembly
/// goto program instructions
///
/// \param function_id: Name of function being processed
/// \param code: The inline assembly code statement to translate
/// \param dest: The goto program to append the new instructions to
void remove_asmt::process_instruction_msc(
const irep_idt &function_id,
const code_asmt &code,
goto_programt &dest)
{
const irep_idt &i_str = to_string_constant(code.op0()).value();
std::istringstream str(id2string(i_str));
assembler_parsert assembler_parser{message_handler};
assembler_parser.in = &str;
assembler_parser.parse();
goto_programt tmp_dest;
bool unknown = false;
bool x86_32_locked_atomic = false;
for(const auto &instruction : assembler_parser.instructions)
{
if(instruction.empty())
continue;
#if 0
std::cout << "A ********************\n";
for(const auto &ins : instruction)
{
std::cout << "XX: " << ins.pretty() << '\n';
}
std::cout << "B ********************\n";
#endif
// deal with prefixes
irep_idt command;
unsigned pos = 0;
if(
instruction.front().id() == ID_symbol &&
instruction.front().get(ID_identifier) == "lock")
{
x86_32_locked_atomic = true;
pos++;
}
// done?
if(pos == instruction.size())
continue;
if(instruction[pos].id() == ID_symbol)
{
command = instruction[pos].get(ID_identifier);
pos++;
}
if(command == "xchg" || command == "xchgl")
x86_32_locked_atomic = true;
if(x86_32_locked_atomic)
{
tmp_dest.add(goto_programt::make_atomic_begin(code.source_location()));
codet code_fence(ID_fence);
code_fence.add_source_location() = code.source_location();
code_fence.set(ID_WWfence, true);
code_fence.set(ID_RRfence, true);
code_fence.set(ID_RWfence, true);
code_fence.set(ID_WRfence, true);
tmp_dest.add(
goto_programt::make_other(code_fence, code.source_location()));
}
if(command == "fstcw" || command == "fnstcw" || command == "fldcw") // x86
{
exprt::operandst args{null_pointer_exprt{pointer_type(empty_typet{})}};
// try to typecheck the argument
if(pos != instruction.size() && instruction[pos].id() == ID_symbol)
{
const irep_idt &name = instruction[pos].get(ID_identifier);
for(const auto &entry : equal_range(symbol_table.symbol_base_map, name))
{
// global scope symbol, don't replace a local one
if(entry.second == name && args[0].id() != ID_address_of)
{
args[0] =
address_of_exprt{symbol_table.lookup_ref(name).symbol_expr()};
}
// parameter or symbol in local scope
else if(has_prefix(
id2string(entry.second), id2string(function_id) + "::"))
{
args[0] = address_of_exprt{
symbol_table.lookup_ref(entry.second).symbol_expr()};
}
}
}
msc_asm_function_call(
"__asm_" + id2string(command), args, code, tmp_dest);
}
else if(
command == "mfence" || command == "lfence" || command == "sfence") // x86
{
msc_asm_function_call("__asm_" + id2string(command), {}, code, tmp_dest);
}
else
unknown = true; // give up
if(x86_32_locked_atomic)
{
tmp_dest.add(goto_programt::make_atomic_end(code.source_location()));
x86_32_locked_atomic = false;
}
}
if(unknown)
{
// we give up; we should perhaps print a warning
}
else
dest.destructive_append(tmp_dest);
}
/// Replaces inline assembly instructions in the goto function by non-assembly
/// goto program instructions
///
/// \param function_id: Name of function being processed
/// \param goto_function: The goto function
void remove_asmt::process_function(
const irep_idt &function_id,
goto_functionst::goto_functiont &goto_function)
{
bool did_something = false;
Forall_goto_program_instructions(it, goto_function.body)
{
if(it->is_other() && it->get_other().get_statement() == ID_asm)
{
goto_programt tmp_dest;
process_instruction(function_id, *it, tmp_dest);
it->turn_into_skip();
did_something = true;
goto_programt::targett next = it;
next++;
goto_function.body.destructive_insert(next, tmp_dest);
}
}
if(did_something)
remove_skip(goto_function.body);
}
/// \copybrief remove_asm(goto_modelt &, message_handlert &)
///
/// \param goto_functions: The goto functions
/// \param symbol_table: The symbol table
/// \param message_handler: Message handler
void remove_asm(
goto_functionst &goto_functions,
symbol_tablet &symbol_table,
message_handlert &message_handler)
{
remove_asmt rem(symbol_table, goto_functions, message_handler);
rem();
}
/// Replaces inline assembly instructions in the goto program (i.e.,
/// instructions of kind `OTHER` with a `code` member of type `code_asmt`) with
/// an appropriate (sequence of) non-assembly goto program instruction(s). At
/// present only a small number of x86 and Power instructions are supported.
/// Unrecognised assembly instructions are ignored.
///
/// \param goto_model: The goto model
/// \param message_handler: Message handler
void remove_asm(goto_modelt &goto_model, message_handlert &message_handler)
{
remove_asm(
goto_model.goto_functions, goto_model.symbol_table, message_handler);
}
bool has_asm(const goto_functionst &goto_functions)
{
for(auto &function_it : goto_functions.function_map)
for(auto &instruction : function_it.second.body.instructions)
if(
instruction.is_other() &&
instruction.get_other().get_statement() == ID_asm)
{
return true;
}
return false;
}
bool has_asm(const goto_modelt &goto_model)
{
return has_asm(goto_model.goto_functions);
}