Skip to content

Commit 2d803cf

Browse files
authored
Merge pull request #5585 from tautschnig/messaget-java_class_loader
java_class_loader(_base)t isn't a messaget
2 parents 67afa28 + f932cbf commit 2d803cf

File tree

7 files changed

+119
-75
lines changed

7 files changed

+119
-75
lines changed

jbmc/src/java_bytecode/java_bytecode_language.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ bool java_bytecode_languaget::preprocess(
292292
void java_bytecode_languaget::set_message_handler(
293293
message_handlert &message_handler)
294294
{
295-
java_class_loader.set_message_handler(message_handler);
296295
languaget::set_message_handler(message_handler);
297296
}
298297

@@ -301,7 +300,7 @@ void java_bytecode_languaget::initialize_class_loader()
301300
java_class_loader.clear_classpath();
302301

303302
for(const auto &p : config.java.classpath)
304-
java_class_loader.add_classpath_entry(p);
303+
java_class_loader.add_classpath_entry(p, get_message_handler());
305304

306305
java_class_loader.set_java_cp_include_files(
307306
language_options->java_cp_include_files);
@@ -330,7 +329,7 @@ void java_bytecode_languaget::parse_from_main_class()
330329
{
331330
// Try to load class
332331
status() << "Trying to load Java main class: " << main_class << eom;
333-
if(!java_class_loader.can_load_class(main_class))
332+
if(!java_class_loader.can_load_class(main_class, get_message_handler()))
334333
{
335334
// Try to extract class name and load class
336335
const auto maybe_class_name =
@@ -342,7 +341,8 @@ void java_bytecode_languaget::parse_from_main_class()
342341
}
343342
status() << "Trying to load Java main class: " << maybe_class_name.value()
344343
<< eom;
345-
if(!java_class_loader.can_load_class(maybe_class_name.value()))
344+
if(!java_class_loader.can_load_class(
345+
maybe_class_name.value(), get_message_handler()))
346346
{
347347
throwMainClassLoadingError(id2string(main_class));
348348
return;
@@ -354,7 +354,8 @@ void java_bytecode_languaget::parse_from_main_class()
354354
}
355355
status() << "Found Java main class: " << main_class << eom;
356356
// Now really load it.
357-
const auto &parse_trees = java_class_loader(main_class);
357+
const auto &parse_trees =
358+
java_class_loader(main_class, get_message_handler());
358359
if(parse_trees.empty() || !parse_trees.front().loading_successful)
359360
{
360361
throwMainClassLoadingError(id2string(main_class));
@@ -429,12 +430,13 @@ bool java_bytecode_languaget::parse(
429430
if(main_class.empty())
430431
{
431432
status() << "JAR file without entry point: loading class files" << eom;
432-
const auto classes = java_class_loader.load_entire_jar(path);
433+
const auto classes =
434+
java_class_loader.load_entire_jar(path, get_message_handler());
433435
for(const auto &c : classes)
434436
main_jar_classes.push_back(c);
435437
}
436438
else
437-
java_class_loader.add_classpath_entry(path);
439+
java_class_loader.add_classpath_entry(path, get_message_handler());
438440
}
439441
else
440442
main_class = config.java.main_class;
@@ -1504,7 +1506,7 @@ bool java_bytecode_languaget::final(symbol_table_baset &)
15041506
void java_bytecode_languaget::show_parse(std::ostream &out)
15051507
{
15061508
java_class_loadert::parse_tree_with_overlayst &parse_trees =
1507-
java_class_loader(main_class);
1509+
java_class_loader(main_class, get_message_handler());
15081510
parse_trees.front().output(out);
15091511
if(parse_trees.size() > 1)
15101512
{

jbmc/src/java_bytecode/java_class_loader.cpp

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ Author: Daniel Kroening, [email protected]
1616

1717
#include "java_bytecode_parser.h"
1818

19-
java_class_loadert::parse_tree_with_overlayst &java_class_loadert::operator()(
20-
const irep_idt &class_name)
19+
java_class_loadert::parse_tree_with_overlayst &java_class_loadert::
20+
operator()(const irep_idt &class_name, message_handlert &message_handler)
2121
{
22-
debug() << "Classpath:";
22+
messaget log(message_handler);
23+
24+
log.debug() << "Classpath:";
2325
for(const auto &entry : classpath_entries)
2426
{
25-
debug() << "\n " << entry.path;
27+
log.debug() << "\n " << entry.path;
2628
}
27-
debug() << messaget::eom;
29+
log.debug() << messaget::eom;
2830

2931
std::stack<irep_idt> queue;
3032
// Always require java.lang.Object, as it is the base of
@@ -44,7 +46,7 @@ java_class_loadert::parse_tree_with_overlayst &java_class_loadert::operator()(
4446
queue.push(id);
4547

4648
java_class_loader_limitt class_loader_limit(
47-
get_message_handler(), java_cp_include_files);
49+
message_handler, java_cp_include_files);
4850

4951
while(!queue.empty())
5052
{
@@ -54,10 +56,10 @@ java_class_loadert::parse_tree_with_overlayst &java_class_loadert::operator()(
5456
if(class_map.count(c) != 0)
5557
continue;
5658

57-
debug() << "Reading class " << c << eom;
59+
log.debug() << "Reading class " << c << messaget::eom;
5860

5961
parse_tree_with_overlayst &parse_trees =
60-
get_parse_tree(class_loader_limit, c);
62+
get_parse_tree(class_loader_limit, c, message_handler);
6163

6264
// Add any dependencies to queue
6365
for(const java_bytecode_parse_treet &parse_tree : parse_trees)
@@ -103,11 +105,13 @@ static bool is_overlay_class(const java_bytecode_parse_treet::classt &c)
103105
.has_value();
104106
}
105107

106-
bool java_class_loadert::can_load_class(const irep_idt &class_name)
108+
bool java_class_loadert::can_load_class(
109+
const irep_idt &class_name,
110+
message_handlert &message_handler)
107111
{
108112
for(const auto &cp_entry : classpath_entries)
109113
{
110-
auto parse_tree = load_class(class_name, cp_entry);
114+
auto parse_tree = load_class(class_name, cp_entry, message_handler);
111115
if(parse_tree.has_value())
112116
return true;
113117
}
@@ -121,6 +125,7 @@ bool java_class_loadert::can_load_class(const irep_idt &class_name)
121125
/// to find the .class file.
122126
/// \param class_loader_limit: Filter to decide whether to load classes
123127
/// \param class_name: Name of class to load
128+
/// \param message_handler: message handler
124129
/// \return The list of valid implementations, including overlays
125130
/// \remarks
126131
/// Allows multiple definitions of the same class to appear on the
@@ -129,23 +134,27 @@ bool java_class_loadert::can_load_class(const irep_idt &class_name)
129134
java_class_loadert::parse_tree_with_overlayst &
130135
java_class_loadert::get_parse_tree(
131136
java_class_loader_limitt &class_loader_limit,
132-
const irep_idt &class_name)
137+
const irep_idt &class_name,
138+
message_handlert &message_handler)
133139
{
134140
parse_tree_with_overlayst &parse_trees = class_map[class_name];
135141
PRECONDITION(parse_trees.empty());
136142

143+
messaget log(message_handler);
144+
137145
// do we refuse to load?
138146
if(!class_loader_limit.load_class_file(class_name_to_jar_file(class_name)))
139147
{
140-
debug() << "not loading " << class_name << " because of limit" << eom;
148+
log.debug() << "not loading " << class_name << " because of limit"
149+
<< messaget::eom;
141150
parse_trees.emplace_back(class_name);
142151
return parse_trees;
143152
}
144153

145154
// Rummage through the class path
146155
for(const auto &cp_entry : classpath_entries)
147156
{
148-
auto parse_tree = load_class(class_name, cp_entry);
157+
auto parse_tree = load_class(class_name, cp_entry, message_handler);
149158
if(parse_tree.has_value())
150159
parse_trees.emplace_back(std::move(*parse_tree));
151160
}
@@ -164,10 +173,10 @@ java_class_loadert::get_parse_tree(
164173
++parse_tree_it;
165174
break;
166175
}
167-
debug() << "Skipping class " << class_name
168-
<< " marked with OverlayClassImplementation but found before"
169-
" original definition"
170-
<< eom;
176+
log.debug() << "Skipping class " << class_name
177+
<< " marked with OverlayClassImplementation but found before"
178+
" original definition"
179+
<< messaget::eom;
171180
}
172181
auto unloaded_or_overlay_out_of_order_it = parse_tree_it;
173182
++parse_tree_it;
@@ -179,8 +188,9 @@ java_class_loadert::get_parse_tree(
179188
// Remove non-initial classes that aren't overlays
180189
if(!is_overlay_class(parse_tree_it->parsed_class))
181190
{
182-
debug() << "Skipping duplicate definition of class " << class_name
183-
<< " not marked with OverlayClassImplementation" << eom;
191+
log.debug() << "Skipping duplicate definition of class " << class_name
192+
<< " not marked with OverlayClassImplementation"
193+
<< messaget::eom;
184194
auto duplicate_non_overlay_it = parse_tree_it;
185195
++parse_tree_it;
186196
parse_trees.erase(duplicate_non_overlay_it);
@@ -192,45 +202,50 @@ java_class_loadert::get_parse_tree(
192202
return parse_trees;
193203

194204
// Not found or failed to load
195-
debug() << "failed to load class " << class_name << eom;
205+
log.debug() << "failed to load class " << class_name << messaget::eom;
196206
parse_trees.emplace_back(class_name);
197207
return parse_trees;
198208
}
199209

200210
/// Load all class files from a .jar file
201211
/// \param jar_path: the path for the .jar to load
202212
std::vector<irep_idt> java_class_loadert::load_entire_jar(
203-
const std::string &jar_path)
213+
const std::string &jar_path,
214+
message_handlert &message_handler)
204215
{
205-
auto classes = read_jar_file(jar_path);
216+
auto classes = read_jar_file(jar_path, message_handler);
206217
if(!classes.has_value())
207218
return {};
208219

209220
classpath_entries.push_front(
210221
classpath_entryt(classpath_entryt::JAR, jar_path));
211222

212223
for(const auto &c : *classes)
213-
operator()(c);
224+
operator()(c, message_handler);
214225

215226
classpath_entries.pop_front();
216227

217228
return *classes;
218229
}
219230

220-
optionalt<std::vector<irep_idt>>
221-
java_class_loadert::read_jar_file(const std::string &jar_path)
231+
optionalt<std::vector<irep_idt>> java_class_loadert::read_jar_file(
232+
const std::string &jar_path,
233+
message_handlert &message_handler)
222234
{
235+
messaget log(message_handler);
236+
223237
std::vector<std::string> filenames;
224238
try
225239
{
226240
filenames = jar_pool(jar_path).filenames();
227241
}
228242
catch(const std::runtime_error &)
229243
{
230-
error() << "failed to open JAR file '" << jar_path << "'" << eom;
244+
log.error() << "failed to open JAR file '" << jar_path << "'"
245+
<< messaget::eom;
231246
return {};
232247
}
233-
debug() << "Adding JAR file '" << jar_path << "'" << eom;
248+
log.debug() << "Adding JAR file '" << jar_path << "'" << messaget::eom;
234249

235250
// Create a new entry in the map and initialize using the list of file names
236251
// that are in jar_filet
@@ -239,8 +254,8 @@ java_class_loadert::read_jar_file(const std::string &jar_path)
239254
{
240255
if(has_suffix(file_name, ".class"))
241256
{
242-
debug() << "Found class file " << file_name << " in JAR '" << jar_path
243-
<< "'" << eom;
257+
log.debug() << "Found class file " << file_name << " in JAR '" << jar_path
258+
<< "'" << messaget::eom;
244259
irep_idt class_name=file_to_class_name(file_name);
245260
classes.push_back(class_name);
246261
}

jbmc/src/java_bytecode/java_class_loader.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,17 @@ class java_class_loadert : public java_class_loader_baset
4141
{
4242
}
4343

44-
parse_tree_with_overlayst &operator()(const irep_idt &class_name);
44+
parse_tree_with_overlayst &
45+
operator()(const irep_idt &class_name, message_handlert &);
4546

4647
/// Checks whether \p class_name is parseable from the classpath,
4748
/// ignoring class loading limits.
48-
bool can_load_class(const irep_idt &class_name);
49+
bool can_load_class(const irep_idt &class_name, message_handlert &);
4950

5051
parse_tree_with_overlayst &get_parse_tree(
5152
java_class_loader_limitt &class_loader_limit,
52-
const irep_idt &class_name);
53+
const irep_idt &class_name,
54+
message_handlert &);
5355

5456
/// Set the argument of the class loader limit \ref java_class_loader_limitt
5557
/// \param cp_include_files: argument string for java_class_loader_limit
@@ -74,7 +76,8 @@ class java_class_loadert : public java_class_loader_baset
7476
java_load_classes.push_back(id);
7577
}
7678

77-
std::vector<irep_idt> load_entire_jar(const std::string &jar_path);
79+
std::vector<irep_idt>
80+
load_entire_jar(const std::string &jar_path, message_handlert &);
7881

7982
/// Map from class names to the bytecode parse trees
8083
fixed_keys_map_wrappert<parse_tree_with_overridest_mapt>
@@ -103,7 +106,8 @@ class java_class_loadert : public java_class_loader_baset
103106
/// Map from class names to the bytecode parse trees
104107
parse_tree_with_overridest_mapt class_map;
105108

106-
optionalt<std::vector<irep_idt>> read_jar_file(const std::string &jar_path);
109+
optionalt<std::vector<irep_idt>>
110+
read_jar_file(const std::string &jar_path, message_handlert &);
107111
};
108112

109113
#endif // CPROVER_JAVA_BYTECODE_JAVA_CLASS_LOADER_H

0 commit comments

Comments
 (0)