Skip to content

Commit 9495ab2

Browse files
committed
assembler_parsert: construct with message handler
This both avoids an object of static lifetime as well as it fixes the (transitive) use of the deprecated messaget() constructor. The scanner is now fully reentrant.
1 parent 69bb2b6 commit 9495ab2

12 files changed

+68
-56
lines changed

doc/architectural/goto-program-transformations.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ This pass goes through the goto model and removes or lowers instances of
1717
assembly intructions. Assembly instructions are stored in instances of the
1818
`other` instruction.
1919

20-
The implementation of this pass is called via the \ref remove_asm(goto_modelt &)
20+
The implementation of this pass is called via the
21+
\ref remove_asm(goto_modelt &, message_handlert &)
2122
function. For full documentation of this pass see \ref remove_asm.h
2223

2324
<em>This pass has no predecessor.</em>

src/assembler/assembler_parser.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,26 @@ Author: Daniel Kroening, [email protected]
88

99
#include "assembler_parser.h"
1010

11-
assembler_parsert assembler_parser;
11+
char *yyassemblerget_text(void *);
1212

13-
extern char *yyassemblertext;
14-
15-
int yyassemblererror(const std::string &error)
13+
int yyassemblererror(
14+
assembler_parsert &assembler_parser,
15+
void *scanner,
16+
const std::string &error)
1617
{
17-
assembler_parser.parse_error(error, yyassemblertext);
18+
assembler_parser.parse_error(error, yyassemblerget_text(scanner));
1819
return 0;
1920
}
21+
22+
int yyassemblerlex_init_extra(assembler_parsert *, void **);
23+
int yyassemblerlex(void *);
24+
int yyassemblerlex_destroy(void *);
25+
26+
bool assembler_parsert::parse()
27+
{
28+
void *scanner;
29+
yyassemblerlex_init_extra(this, &scanner);
30+
yyassemblerlex(scanner);
31+
yyassemblerlex_destroy(scanner);
32+
return false;
33+
}

src/assembler/assembler_parser.h

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ Author: Daniel Kroening, [email protected]
1414

1515
#include <list>
1616

17-
int yyassemblerlex();
18-
int yyassemblererror(const std::string &error);
19-
void assembler_scanner_init();
17+
class assembler_parsert;
18+
int yyassemblererror(assembler_parsert &, void *, const std::string &error);
2019

2120
class assembler_parsert:public parsert
2221
{
@@ -37,24 +36,18 @@ class assembler_parsert:public parsert
3736
instructions.push_back(instructiont());
3837
}
3938

40-
assembler_parsert()
39+
explicit assembler_parsert(message_handlert &message_handler)
40+
: parsert(message_handler)
4141
{
4242
}
4343

44-
virtual bool parse()
45-
{
46-
yyassemblerlex();
47-
return false;
48-
}
44+
bool parse() override;
4945

50-
virtual void clear()
46+
void clear() override
5147
{
5248
parsert::clear();
5349
instructions.clear();
54-
// assembler_scanner_init();
5550
}
5651
};
5752

58-
extern assembler_parsert assembler_parser;
59-
6053
#endif // CPROVER_ASSEMBLER_ASSEMBLER_PARSER_H

src/assembler/remove_asm.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ Date: December 2014
3030
class remove_asmt
3131
{
3232
public:
33-
remove_asmt(symbol_tablet &_symbol_table, goto_functionst &_goto_functions)
34-
: symbol_table(_symbol_table), goto_functions(_goto_functions)
33+
remove_asmt(
34+
symbol_tablet &_symbol_table,
35+
goto_functionst &_goto_functions,
36+
message_handlert &message_handler)
37+
: symbol_table(_symbol_table),
38+
goto_functions(_goto_functions),
39+
message_handler(message_handler)
3540
{
3641
}
3742

@@ -44,6 +49,7 @@ class remove_asmt
4449
protected:
4550
symbol_tablet &symbol_table;
4651
goto_functionst &goto_functions;
52+
message_handlert &message_handler;
4753

4854
void process_function(const irep_idt &, goto_functionst::goto_functiont &);
4955

@@ -228,7 +234,7 @@ void remove_asmt::process_instruction_gcc(
228234
const irep_idt &i_str = to_string_constant(code.asm_text()).value();
229235

230236
std::istringstream str(id2string(i_str));
231-
assembler_parser.clear();
237+
assembler_parsert assembler_parser{message_handler};
232238
assembler_parser.in = &str;
233239
assembler_parser.parse();
234240

@@ -399,7 +405,7 @@ void remove_asmt::process_instruction_msc(
399405
const irep_idt &i_str = to_string_constant(code.op0()).value();
400406

401407
std::istringstream str(id2string(i_str));
402-
assembler_parser.clear();
408+
assembler_parsert assembler_parser{message_handler};
403409
assembler_parser.in = &str;
404410
assembler_parser.parse();
405411

@@ -544,13 +550,17 @@ void remove_asmt::process_function(
544550
remove_skip(goto_function.body);
545551
}
546552

547-
/// \copybrief remove_asm(goto_modelt &)
553+
/// \copybrief remove_asm(goto_modelt &, message_handlert &)
548554
///
549555
/// \param goto_functions: The goto functions
550556
/// \param symbol_table: The symbol table
551-
void remove_asm(goto_functionst &goto_functions, symbol_tablet &symbol_table)
557+
/// \param message_handler: Message handler
558+
void remove_asm(
559+
goto_functionst &goto_functions,
560+
symbol_tablet &symbol_table,
561+
message_handlert &message_handler)
552562
{
553-
remove_asmt rem(symbol_table, goto_functions);
563+
remove_asmt rem(symbol_table, goto_functions, message_handler);
554564
rem();
555565
}
556566

@@ -561,9 +571,11 @@ void remove_asm(goto_functionst &goto_functions, symbol_tablet &symbol_table)
561571
/// Unrecognised assembly instructions are ignored.
562572
///
563573
/// \param goto_model: The goto model
564-
void remove_asm(goto_modelt &goto_model)
574+
/// \param message_handler: Message handler
575+
void remove_asm(goto_modelt &goto_model, message_handlert &message_handler)
565576
{
566-
remove_asm(goto_model.goto_functions, goto_model.symbol_table);
577+
remove_asm(
578+
goto_model.goto_functions, goto_model.symbol_table, message_handler);
567579
}
568580

569581
bool has_asm(const goto_functionst &goto_functions)

src/assembler/remove_asm.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ Date: December 2014
5454

5555
class goto_functionst;
5656
class goto_modelt;
57+
class message_handlert;
5758
class symbol_tablet;
5859

59-
void remove_asm(goto_functionst &, symbol_tablet &);
60+
void remove_asm(goto_functionst &, symbol_tablet &, message_handlert &);
6061

61-
void remove_asm(goto_modelt &);
62+
void remove_asm(goto_modelt &, message_handlert &);
6263

6364
/// returns true iff the given goto functions use asm instructions
6465
bool has_asm(const goto_functionst &);

src/assembler/scanner.l

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
%option noinput
33
%option nounistd
44
%option never-interactive
5+
%option noyywrap
6+
%option reentrant
7+
%option extra-type="assembler_parsert *"
58

69
%{
710

@@ -13,7 +16,7 @@
1316
#pragma warning(disable:4005)
1417
#endif
1518

16-
#define PARSER assembler_parser
19+
#define PARSER (*yyextra)
1720
#define YYSTYPE unsigned
1821
#undef ECHO
1922
#define ECHO
@@ -60,14 +63,6 @@ string_lit ("L"|"u"|"U"|"u8")?["]{s_char}*["]
6063
%x GRAMMAR
6164
%x LINE_COMMENT
6265

63-
%{
64-
void assemlber_scanner_init()
65-
{
66-
YY_FLUSH_BUFFER;
67-
BEGIN(0);
68-
}
69-
%}
70-
7166
%%
7267

7368
<INITIAL>.|\n { BEGIN(GRAMMAR);
@@ -109,7 +104,3 @@ void assemlber_scanner_init()
109104
}
110105

111106
<<EOF>> { yyterminate(); /* done! */ }
112-
113-
%%
114-
115-
int yywrap() { return 1; }

src/cbmc/cbmc_parse_options.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ bool cbmc_parse_optionst::process_goto_program(
883883
{
884884
// Remove inline assembler; this needs to happen before
885885
// adding the library.
886-
remove_asm(goto_model);
886+
remove_asm(goto_model, log.get_message_handler());
887887

888888
// add the library
889889
log.status() << "Adding CPROVER library (" << config.ansi_c.arch << ")"
@@ -895,7 +895,7 @@ bool cbmc_parse_optionst::process_goto_program(
895895
// library functions may introduce inline assembler
896896
while(has_asm(goto_model))
897897
{
898-
remove_asm(goto_model);
898+
remove_asm(goto_model, log.get_message_handler());
899899
link_to_library(
900900
goto_model, log.get_message_handler(), cprover_cpp_library_factory);
901901
link_to_library(

src/goto-analyzer/goto_analyzer_parse_options.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ bool goto_analyzer_parse_optionst::process_goto_program(
698698
{
699699
// Remove inline assembler; this needs to happen before
700700
// adding the library.
701-
remove_asm(goto_model);
701+
remove_asm(goto_model, ui_message_handler);
702702

703703
// add the library
704704
log.status() << "Adding CPROVER library (" << config.ansi_c.arch << ")"
@@ -708,7 +708,7 @@ bool goto_analyzer_parse_optionst::process_goto_program(
708708
// library functions may introduce inline assembler
709709
while(has_asm(goto_model))
710710
{
711-
remove_asm(goto_model);
711+
remove_asm(goto_model, ui_message_handler);
712712
link_to_library(
713713
goto_model, ui_message_handler, cprover_cpp_library_factory);
714714
link_to_library(goto_model, ui_message_handler, cprover_c_library_factory);

src/goto-diff/goto_diff_parse_options.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ bool goto_diff_parse_optionst::process_goto_program(
176176
{
177177
// Remove inline assembler; this needs to happen before
178178
// adding the library.
179-
remove_asm(goto_model);
179+
remove_asm(goto_model, ui_message_handler);
180180

181181
// add the library
182182
log.status() << "Adding CPROVER library (" << config.ansi_c.arch << ")"
@@ -186,7 +186,7 @@ bool goto_diff_parse_optionst::process_goto_program(
186186
// library functions may introduce inline assembler
187187
while(has_asm(goto_model))
188188
{
189-
remove_asm(goto_model);
189+
remove_asm(goto_model, ui_message_handler);
190190
link_to_library(
191191
goto_model, ui_message_handler, cprover_cpp_library_factory);
192192
link_to_library(goto_model, ui_message_handler, cprover_c_library_factory);

src/goto-instrument/goto_instrument_parse_options.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ void goto_instrument_parse_optionst::do_indirect_call_and_rtti_removal(
949949
log.status() << "Virtual function removal" << messaget::eom;
950950
remove_virtual_functions(goto_model);
951951
log.status() << "Cleaning inline assembler statements" << messaget::eom;
952-
remove_asm(goto_model);
952+
remove_asm(goto_model, log.get_message_handler());
953953
}
954954

955955
/// Remove function pointers that can be resolved by analysing const variables
@@ -1065,7 +1065,7 @@ void goto_instrument_parse_optionst::instrument_goto_program()
10651065

10661066
// remove inline assembler as that may yield further library function calls
10671067
// that need to be resolved
1068-
remove_asm(goto_model);
1068+
remove_asm(goto_model, ui_message_handler);
10691069

10701070
// add the library
10711071
log.status() << "Adding CPROVER library (" << config.ansi_c.arch << ")"
@@ -1076,7 +1076,7 @@ void goto_instrument_parse_optionst::instrument_goto_program()
10761076
// library functions may introduce inline assembler
10771077
while(has_asm(goto_model))
10781078
{
1079-
remove_asm(goto_model);
1079+
remove_asm(goto_model, ui_message_handler);
10801080
link_to_library(
10811081
goto_model, ui_message_handler, cprover_cpp_library_factory);
10821082
link_to_library(

src/goto-synthesizer/cegis_verifier.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ get_checked_pointer_from_null_pointer_check(const exprt &violation)
8484
void cegis_verifiert::preprocess_goto_model()
8585
{
8686
// Preprocess `goto_model`. Copied from `cbmc_parse_options.cpp`.
87-
remove_asm(goto_model);
87+
remove_asm(goto_model, log.get_message_handler());
8888
link_to_library(
8989
goto_model, log.get_message_handler(), cprover_cpp_library_factory);
9090
link_to_library(
9191
goto_model, log.get_message_handler(), cprover_c_library_factory);
9292
// library functions may introduce inline assembler
9393
while(has_asm(goto_model))
9494
{
95-
remove_asm(goto_model);
95+
remove_asm(goto_model, log.get_message_handler());
9696
link_to_library(
9797
goto_model, log.get_message_handler(), cprover_cpp_library_factory);
9898
link_to_library(

src/libcprover-cpp/api.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ bool api_sessiont::is_goto_binary(std::string &file) const
178178
bool api_sessiont::preprocess_model() const
179179
{
180180
// Remove inline assembler; this needs to happen before adding the library.
181-
remove_asm(*implementation->model);
181+
remove_asm(*implementation->model, *implementation->message_handler);
182182

183183
// add the library
184184
messaget log{*implementation->message_handler};
@@ -191,7 +191,7 @@ bool api_sessiont::preprocess_model() const
191191
// library functions may introduce inline assembler
192192
while(has_asm(*implementation->model))
193193
{
194-
remove_asm(*implementation->model);
194+
remove_asm(*implementation->model, *implementation->message_handler);
195195
link_to_library(
196196
*implementation->model,
197197
*implementation->message_handler,

0 commit comments

Comments
 (0)