-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
946 lines (739 loc) · 33.8 KB
/
Copy pathrun.py
File metadata and controls
946 lines (739 loc) · 33.8 KB
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
import atexit
import logging
import os
import random
import shutil
import sys
import time
import traceback
import signal
import threading
from functools import partial
from utils_api import (
read_json,
write_json,
read_file,
write_file,
copy_file,
delete_file,
create_permissioned_file,
rename_directory,
create_directory,
delete_directory,
reset_directory,
get_last_directory,
parse_def_loc,
remove_base_path,
set_log,
record_log,
grant_permissions,
)
from llm_api import (
LLMInterface,
init_prompt_count,
save_coverage_report,
get_claude_model,
configure_llm,
AgentInterface,
)
from c_parser_api import (
analyze_call_relationship
)
from pilot_lib.utils import (
Paths,
run_script,
get_coverage,
merge_config,
)
from pilot_lib.tool import (
find_tool,
get_tool_string,
native_tool_configuration,
)
from pilot_lib.analyze import search_main
from pilot_lib.graph import (
analyze_call_dependencies,
get_related_main,
build_graph,
write_metrics,
get_estimated_cent,
)
from pilot_lib.generate import (
ask_basic_command,
explore_path,
explore_branch,
clear_gcda_files,
)
from pilot_lib.reformat import (
export_to_seed_dir,
generate_input_files,
generate_base_argv,
generate_carpet_argv,
generate_zigzag_argv,
generate_afl_argv,
)
periodic_running = True
periodic_start_time = None # Variable to record the start time
def run_periodic(
interval, target_dir, azure_endpoint, counter,
tmp_branch_path, tmp_line_path, tmp_function_path
):
"""Keep running the function against the directory at the specified interval"""
global periodic_running, periodic_start_time
periodic_start_time = time.time() # Record the start time
while periodic_running:
elapsed_time = time.time() - periodic_start_time
minutes = int(elapsed_time // 60) # Compute the minutes
if azure_endpoint is None:
print(f"Elapsed time: {elapsed_time:.2f} seconds ({minutes} min)")
else:
print(f"Elapsed time: {elapsed_time:.2f} seconds ({minutes} min), region: {azure_endpoint}")
print(f"\nTarget function count: {counter['value']}")
branch_path = tmp_branch_path
line_path = tmp_line_path
function_path = tmp_function_path
time.sleep(interval) # Wait for the specified number of seconds
def start_periodic_server(
process_type, strategy, interval, target_dir, azure_endpoint, counter,
tmp_branch_path, tmp_line_path, tmp_function_path
):
if process_type != "prepare" and process_type != "preset" and process_type != "gcno":
periodic_thread = threading.Thread(target=run_periodic, args=(interval, target_dir, azure_endpoint, counter, tmp_branch_path, tmp_line_path, tmp_function_path))
periodic_thread.daemon = True # Terminate together when the main thread ends
periodic_thread.start()
def get_basic_report(
paths, target, original_target_dir, exec_time, target_cmd,
process_type, llm_model, strategy, llm_choice,
max_explore_time, temperature, total_time, interval,
cent, WO_READ, WO_PATH, WO_VALIDATION
):
print("\n====== Getting output report =======")
log_json = read_json(paths.logging_path)
if target_cmd not in log_json:
log_json[target_cmd] = {}
if 'current_count' not in log_json[target_cmd]:
log_json[target_cmd]['current_count'] = 1
trial_id = str(log_json[target_cmd]['current_count'])
log_json[target_cmd]['current_count'] += 1
write_json(paths.logging_path, log_json)
result_json = read_json(paths.result_path)
if result_json is None:
result_json = {}
if target_cmd not in result_json:
result_json[target_cmd] = {}
if trial_id not in result_json[target_cmd]:
result_json[target_cmd][trial_id] = {}
llm = ""
if process_type in ["gen"]:
llm = f"_{llm_model}"
strategy_str = ""
if process_type == "gen" and strategy != "":
strategy_str = f"_{strategy}"
fixed_str = ""
if cent is not None:
fixed_str = f"_{cent}"
else:
fixed_str = ""
formatted_id = str(trial_id).zfill(3)
destination = paths.archive_dir + "/" f"{formatted_id}_" f"{process_type}" + f"{llm}" + f"{strategy_str}" + f"{fixed_str}" # + f"_{max_explore_time}"
# print(paths.chat_dir)
# print(paths.snap_dir)
# print(cov_report_path)
# print(paths.select_path)
if os.path.exists(paths.chat_dir):
copy_directory(f"{paths.chat_dir}", destination)
rename_directory(f"{destination}/{target_cmd}", f"{destination}/chats")
if os.path.exists(paths.snap_dir):
copy_directory(f"{paths.snap_dir}", destination)
rename_directory(f"{destination}/{target_cmd}", f"{destination}/snapdata")
if os.path.exists(paths.cov_report_path):
copy_file(paths.cov_report_path, destination)
if os.path.exists(paths.select_path):
copy_file(paths.select_path, destination)
if os.path.exists(f"{paths.database_dir}/cov_increased.json"):
copy_file(f"{paths.database_dir}/cov_increased.json", destination)
if os.path.exists(f"{paths.database_dir}/cov_time.csv"):
copy_file(f"{paths.database_dir}/cov_time.csv", destination)
if os.path.exists(paths.token_path):
copy_file(paths.token_path, destination)
if os.path.exists(paths.log_path):
copy_file(paths.log_path, destination)
setting_path = f"{destination}/setting.json"
setting = {
"process_type" : process_type,
"llm_model" : llm_model,
"total_time" : total_time,
"interval" : interval, # The measurement interval
"explore_time" : max_explore_time, # = 300 #180
"temperature" : temperature,
#"explore_fix" : explore_fix,
"WO_READ" : WO_READ,
"WO_PATH" : WO_PATH,
"WO_VALIDATION" : WO_VALIDATION,
}
write_json(setting_path, setting)
total_in = 0
total_out = 0
if os.path.exists(paths.token_path):
token_data = read_json(paths.token_path)
for item in token_data:
total_in += item['input_token']
total_out += item['output_token']
USD_TO_JPY = 150
if "claude" in llm_choice:
total_cost = total_in * 0.0000030 + total_out * 0.0000150
else:
total_cost = total_in * 0.0000050 + total_out * 0.0000150
total_cost_jpy = total_cost * USD_TO_JPY
# print(f"\nTotal cost: {total_cost:.6f} USD\n") # , {total_cost_jpy:.2f} JPY\n")
current_directory = os.getcwd()
result_json[target_cmd][trial_id]['cwd'] = current_directory
result_json[target_cmd][trial_id]['exec_time'] = exec_time
result_json[target_cmd][trial_id]['archive_dir'] = destination
result_json[target_cmd][trial_id]["process_type"] = process_type
"""
for key, value in config.items():
result_json[target_cmd][trial_id][key] = value
if 'repair_details' not in result_json[target_cmd]:
result_json[target_cmd][trial_id]['repair_details'] = {}
if 'input_token' not in result_json[target_cmd]:
result_json[target_cmd][trial_id]['input_token'] = {}
if 'output_token' not in result_json[target_cmd]:
result_json[target_cmd][trial_id]['output_token'] = {}
"""
write_json(paths.result_path, result_json)
will_show = None
def get_final_report(
paths, process_type, start_time, target_cmd, target, original_target_dir,
max_explore_time, temperature, total_time, interval,
cent, llm_choice, llm_model, strategy,
WO_READ, WO_PATH, WO_VALIDATION
):
global will_show
if will_show:
print("Program ended! Done.")
end_time = time.time()
exec_time = end_time - start_time
get_basic_report(
paths, target, original_target_dir, exec_time, target_cmd,
process_type, llm_model, strategy, llm_choice,
max_explore_time, temperature, total_time, interval,
cent, WO_READ, WO_PATH, WO_VALIDATION
)
def stop_periodic_server():
global periodic_running
periodic_running = False
time.sleep(1)
def set_timeout(
paths, seconds, start_time, process_type, target_cmd, target,
target_dir, original_target_dir, archive_dir, meta_dir,
result_path, dep_json_path, logging_path,
max_explore_time, temperature, total_time, interval,
cent, llm_choice, llm_model, strategy,
chat_dir, snap_dir, WO_READ, WO_PATH, WO_VALIDATION,
cov_report_path, select_path, database_dir, token_path
):
def timeout_handler(signum, frame):
sys.stderr.write(f"Timeout ({seconds} seconds) occurred\n")
stop_periodic_server() # Stop the periodic thread
get_final_report(
paths, process_type, start_time, target_cmd, target, original_target_dir,
max_explore_time, temperature, total_time, interval,
cent, llm_choice, llm_model, strategy,
WO_READ, WO_PATH, WO_VALIDATION
)
os._exit(1) # Terminate the entire process
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(seconds)
def copy_directory(source_directory, destination_root, target=None):
if target != "wireshark-4.0.1":
# Get only the directory name from source_directory
source_directory_name = os.path.basename(os.path.normpath(source_directory))
final_destination = os.path.join(destination_root, source_directory_name) # Build the path of the final destination directory
shutil.copytree(source_directory, final_destination, dirs_exist_ok=True) # Copy the source directory to the final destination # Set the third argument dirs_exist_ok of shutil.copytree to True to # avoid an error when the destination directory already exists (requires Python 3.8 or higher)
print(f"Copied {source_directory} to {final_destination}")
else:
try:
# Get only the directory name from source_directory
source_directory_name = os.path.basename(os.path.normpath(source_directory))
final_destination = os.path.join(destination_root, source_directory_name)
# Remove the destination if it is a file
if os.path.exists(final_destination) and not os.path.isdir(final_destination):
print(f"Warning: Removing file {final_destination} to create directory")
os.remove(final_destination)
# Create the directory
os.makedirs(final_destination, exist_ok=True)
# Copy recursively by hand
for root, dirs, files in os.walk(source_directory):
# Compute the relative path
rel_path = os.path.relpath(root, source_directory)
dest_dir = os.path.join(final_destination, rel_path)
# Create directories that do not exist
os.makedirs(dest_dir, exist_ok=True)
# Copy the files
for file in files:
try:
src_file = os.path.join(root, file)
dst_file = os.path.join(dest_dir, file)
# Remove the destination if it is a directory
if os.path.isdir(dst_file):
print(f"Warning: Removing directory {dst_file} to create file")
shutil.rmtree(dst_file)
# In the case of a symbolic link
if os.path.islink(src_file):
link_to = os.readlink(src_file)
if os.path.exists(dst_file):
os.remove(dst_file)
os.symlink(link_to, dst_file)
print(f"Created symlink from {dst_file} to {link_to}")
# In the case of a regular file
elif os.path.isfile(src_file):
shutil.copy2(src_file, dst_file)
except Exception as file_error:
print(f"Warning: Failed to copy {src_file}: {file_error}")
print(f"Copied {source_directory} to {final_destination}")
except Exception as e:
print(f"Error during copy from {source_directory} to {destination_root}: {e}")
import traceback
traceback.print_exc()
def get_cent_key(cent):
cent_key = None
# if cent == "random_t":
if cent == "close":
cent_key = "closeness_centrality"
elif cent == "page":
cent_key = "pagerank"
elif cent == "bet":
cent_key = "betweenness_centrality"
elif cent == "deg":
cent_key = "degree_centrality"
elif cent == "katz":
cent_key = "katz_centrality"
return cent_key
def get_random_id(process_type):
return ""
if process_type in ["prepare", "preset", "gcno", "gen_seed", "seed"]:
return ""
# Get the current UNIX timestamp
timestamp = int(time.time())
# Combine the last 3 digits of the timestamp with a 3-digit random number
timestamp_part = timestamp % 1000 # Last 3 digits
random_part = random.randint(0, 999) # A random number from 0 to 999
return f"_{timestamp_part:03d}{random_part:03d}" # 6 digits in total
def get_strategies(strategy, cent):
cent_key = None
WO_READ = False
WO_PATH = False
WO_VALIDATION = False
cent_key = get_cent_key(cent)
if strategy == "wo_read":
WO_READ = True
elif strategy == "wo_t":
max_target_func = None
elif strategy == "wo_v": # without validation
WO_VALIDATION = True
elif strategy == "wo_p": # without path guiding
WO_READ = True
WO_PATH = True
elif strategy == "wo_pv": # without path guiding + without validation
WO_VALIDATION = True
WO_READ = True
WO_PATH = True
# elif strategy == "wo_tool": # without tool
# elif strategy == "random_t":
# if strategy not in ["wo_read", "wo_t", "random_t", "topo", "page", "katz", "close", "hy", "wo_p", "wo_pv", "wo_v", "bet", "deg", "wo_k", "wo_tool"]:
# raise ValueError('Must within ["wo_read", "wo_t", "random_t", "topo", "page", "katz", "close", "hy", "wo_p", "wo_pv", "wo_v", "bet", "deg", "wo_k", "wo_tool"]')
return cent_key, WO_READ, WO_PATH, WO_VALIDATION
def initialize(process_type, paths, original_target_dir):
reset_directory(paths.meta_dir)
reset_directory(paths.work_dir)
reset_directory(paths.snap_dir)
reset_directory(paths.chat_dir)
reset_directory(paths.tmp_dir)
if process_type == "prepare":
reset_directory(paths.database_dir)
delete_file(paths.reformed_path)
delete_file(paths.isolated_path)
delete_file(paths.callee_path)
delete_file(f"{paths.database_dir}/flow_moment.txt")
delete_file(f"{paths.database_dir}/cov_increased.json")
delete_file(paths.branch_path)
delete_file(paths.line_path)
delete_file(paths.priority_path)
delete_file(paths.info_path)
delete_file(paths.select_path)
# delete_file(paths.is_program_path)
delete_file(paths.token_path)
delete_file(f"{paths.cov_report_path}")
delete_file(paths.time_path)
delete_file(paths.function_path)
delete_file(paths.branch_path)
delete_file(paths.line_path)
delete_file(paths.candidate_path)
write_file(paths.current_cov_path, "")
init_prompt_count(paths.logging_path)
if process_type != "prepare" and os.path.exists(paths.preset_dir):
copy_directory(f"{paths.preset_dir}/workspace_{target_cmd}", "./")
copy_directory(f"{paths.preset_dir}/metadata_{target_cmd}", "./")
copy_file(f"{paths.preset_dir}/dependencies.json", paths.database_dir)
copy_file(f"{paths.preset_dir}/reformed.json", paths.reformed_path)
copy_file(f"{paths.preset_dir}/isolated.json", paths.isolated_path)
copy_file(f"{paths.preset_dir}/callee.json", paths.callee_path)
copy_file(f"{paths.preset_dir}/callee_main.json", paths.callee_main_path)
else:
copy_directory(original_target_dir, paths.work_dir)
grant_permissions(paths.work_dir)
def explorer_main(target_cmd, process_type, directory_id, home_dir, config):
#######################################################
###### Setup
#######################################################
if 'openssl' in target_cmd:
config.interval = 120
base_dir = home_dir.replace("/PILOT", "")
database_json = read_json(config.database_path)
original_target_dir = database_json[target_cmd]["dir_name"]
original_target_dir = f"{home_dir}/{original_target_dir}"
target = get_last_directory(original_target_dir)
paths = Paths(
target_cmd=target_cmd,
process_type=process_type,
target=target,
directory_id=directory_id,
home_dir=home_dir,
#macro_parser_dir=f"{base_dir}/{config.macro_parser_dir}",
)
if config.cent is None:
config.cent = get_estimated_cent(
target_cmd, paths
)
cent_key, WO_READ, WO_PATH, WO_VALIDATION = get_strategies(
config.strategy, config.cent
)
#######################################################
###### Main pipeline
#######################################################
if config.llm_model is None:
if "claude" in config.llm_choice:
config.llm_model = get_claude_model(config.llm_choice)
if process_type in ["file", "carpet", "zigzag"]:
config.AGENT = False
if os.path.exists(paths.chat_dir):
delete_directory(paths.chat_dir)
create_directory(paths.chat_dir)
if config.AGENT is False:
llm_interface = LLMInterface(
AGENT=config.AGENT,
project_id=target,
occupy_path=paths.occupy_path,
llm_choice=config.llm_choice,
llm_model=config.llm_model,
api_key=config.api_key,
azure_endpoint=config.azure_endpoint,
temperature=config.temperature,
timeout=config.timeout,
output_max=config.output_max, #128000, #4000,
context_window=config.context_window, #1000000,
history_path=paths.history_path,
token_path=paths.token_path,
database_dir=paths.database_dir,
chat_dir=paths.chat_dir,
count_path=paths.count_path,
)
else:
work_dir_abs = os.path.abspath(paths.work_dir)
database_dir_abs = os.path.abspath(paths.database_dir)
if os.path.exists(paths.session_path):
delete_file(paths.session_path)
if os.path.exists(paths.cost_path):
delete_file(paths.cost_path)
os.environ["IS_SANDBOX"] = "1"
llm_interface = AgentInterface(
AGENT=config.AGENT,
project_id=target,
occupy_path=paths.occupy_path,
llm_choice=config.llm_choice,
llm_model=config.llm_model,
api_key=config.api_key,
azure_endpoint=config.azure_endpoint,
work_dir=work_dir_abs,
allowed_tools=[
"Read", "Grep", "Glob", "Bash", "Write", "Edit"
# f"Edit({work_dir_abs}/**)",
# f"Write({work_dir_abs}/**)",
],
add_dirs=[database_dir_abs],
max_turns=20, # 50,
permission_mode="bypassPermissions",
session_path=paths.session_path,
database_dir=paths.database_dir,
chat_dir=paths.chat_dir,
count_path=paths.count_path,
token_path=paths.token_path,
)
if process_type in ["prepare", "gen"]: #"preset", "gcno", "exp", "set", "file", "carpet", "zigzag", "afl_argv"]:
initialize(process_type, paths, original_target_dir)
# Start
print(f"\n=============== Start of {process_type} ===============\n\n")
will_show = True
targeted_set = set()
ommited_files = []
start_time = time.time()
if process_type == "gen":
atexit.register(partial(get_final_report, paths,
process_type, start_time, target_cmd, target, original_target_dir,
config.max_explore_time, config.temperature, config.total_time, config.interval,
config.cent, config.llm_choice, config.llm_model, config.strategy,
WO_READ, WO_PATH, WO_VALIDATION
))
try:
if process_type == "prepare":
print("Preparing ...")
executable = True
executable, main_list = search_main(
process_type, target, paths.run_test_path, paths.dep_json_path, paths.build_path,
paths.target_dir, paths.meta_dir, paths.database_dir, ommited_files,
paths.div_meta_dir, paths.taken_directive_path, paths.unordered_taken_directive_path,
paths.all_directive_path, paths.is_program_path, paths.all_macros_path, paths.taken_macros_path,
paths.guards_path, paths.guarded_macros_path, paths.independent_path, paths.flag_path, paths.const_path, paths.global_path
)
print("---------------- Result ----------------")
main_paths = []
for entry in main_list:
def_file_path, def_start_line, _ = parse_def_loc(entry['definition'])
base_path = remove_base_path(def_file_path, f"{home_dir}/{paths.target_dir}")
main_paths.append({
"path": f"{original_target_dir}/{base_path}",
"start_line": def_start_line,
"end_line": entry['end_line'],
})
if len(main_list) > 1:
for item in main_paths:
print(f"{item['path']}:{item['start_line']}-{item['end_line']}")
print("Should avoid using as the target because it has multiple main functions.")
elif len(main_list) == 0:
print("Should avoid using as the target because it has no main function.")
else:
for item in main_paths:
print(f"{item['path']}:{item['start_line']}-{item['end_line']}")
elif process_type == "preset":
# get dependencies
analyze_call_dependencies(
paths.target_dir, paths.meta_dir, paths.database_dir, paths.is_program_path, paths.reformed_path, paths.isolated_path
)
# get call relationship
analyze_call_relationship(
paths.meta_dir, paths.callee_path, paths.target_dir, paths.is_program_path
)
# get main-function surrounded
get_related_main(
paths.program_dir, database_json, target_cmd, home_dir,
paths.work_dir, paths.callee_main_path, paths.callee_path, paths.distance_path,
paths.meta_dir, paths.database_dir
)
graph_metrics, G = build_graph(paths.callee_path, paths.callee_main_path)
write_metrics(target_cmd, graph_metrics)
delete_directory(paths.preset_dir)
copy_directory(paths.meta_dir, paths.preset_dir)
copy_file(paths.is_program_path, paths.preset_dir)
copy_file(paths.dep_json_path, paths.preset_dir)
copy_file(paths.reformed_path, paths.preset_dir)
copy_file(paths.isolated_path, paths.preset_dir)
copy_file(paths.callee_path, paths.preset_dir)
copy_file(paths.callee_main_path, paths.preset_dir)
elif process_type == "gcno":
# paths.work_dir comes from afl's directory with gcno
delete_directory(paths.work_dir)
copy_directory(original_target_dir, paths.work_dir)
run_script(process_type, paths.database_dir, paths.build_path, 10000, True, None, "both")
copy_directory(paths.work_dir, paths.preset_dir)
elif process_type == "tool":
if os.path.exists(paths.chat_dir):
delete_directory(paths.chat_dir)
create_directory(paths.chat_dir)
if os.path.exists(paths.tool_dir):
delete_directory(paths.tool_dir)
create_directory(paths.tool_dir)
native_tool_configuration(target, target_cmd, paths.target_dir, paths.tool_dir, paths.tool_json_path, llm_interface, config.os_version)
elif process_type == "gen":
record_log(paths.log_path)
# func_sum = get_func_sum(paths.callee_main_path)
# Set at the start of execution
set_timeout(
paths, config.total_time, start_time, process_type, target_cmd, target,
paths.target_dir, original_target_dir, paths.archive_dir, paths.meta_dir,
paths.result_path, paths.dep_json_path, paths.logging_path,
config.max_explore_time, config.temperature, config.total_time, config.interval,
config.cent, config.llm_choice, config.llm_model, config.strategy,
paths.chat_dir, paths.snap_dir, WO_READ, WO_PATH, WO_VALIDATION,
paths.cov_report_path, paths.select_path, paths.database_dir, paths.token_path
)
print(f"Target_dir: {paths.target_dir}")
# create_permissioned_file(paths.run_test_path)
graph_metrics, G = build_graph(paths.callee_path, paths.callee_main_path)
error = None
std_out = None
cycle = 1
testfile_counter = 0
current_max_counter = 0
total_start_time = time.time()
clear_gcda_files(paths.target_dir)
tool_string, cmd_self = get_tool_string(paths.tool_json_path, target_cmd, target)
# Write the first testcase
testfile_counter = ask_basic_command(
paths, llm_interface, target_cmd,
config.cov_target, config.max_num_test, cent_key, config.max_version_count,
original_target_dir, database_json, config.max_iterations, tool_string, config.strategy, testfile_counter, WO_VALIDATION
)
counter = {"value": 0}
current_coverage = None
while(1):
print(f"Current_coverage: {current_coverage}")
if current_coverage == 100:
break
elapsed_time = time.time() - total_start_time
if counter["value"] >= config.max_target_func:
print(f"Reached {counter["value"]} functions, so stopping")
minutes = int(elapsed_time // 60)
print(f"{elapsed_time:.2f} seconds ({minutes} min) elapsed")
break
if cycle == 1:
if config.COUNT_PERIODIC is True:
start_periodic_server(
process_type, config.strategy, config.interval, paths.target_dir, config.azure_endpoint, counter,
paths.tmp_branch_path, paths.tmp_line_path, paths.tmp_function_path
)
# Explore pathes
try:
testfile_counter, target_entry = explore_path(
paths, llm_interface, config.strategy, config.cent, tool_string, config.max_num_test, config.cov_target, current_coverage, targeted_set, target_cmd,
original_target_dir, cent_key, config.max_version_count, config.max_explore_time, config.explore_fix,
database_json, error, std_out, graph_metrics, G,
WO_READ, WO_PATH, WO_VALIDATION, testfile_counter
)
if target_entry['is_covered'] is True and target_entry['is_full_branch_covered'] is False:
try:
testfile_counter = explore_branch(
paths, llm_interface, config.strategy, config.cov_target, target_cmd, target_entry, tool_string, cent_key,
original_target_dir, config.max_num_test,
config.max_explore_time, config.explore_fix, config.max_iterations, config.max_version_count, testfile_counter,
WO_READ, WO_PATH, WO_VALIDATION,
database_json, error, std_out, graph_metrics, G
)
# current_coverage = explore_branch(
except Exception as e:
msg = str(e)
if "flagged this message for a cybersecurity topic" in msg or "Claude Code returned an error result" in msg:
logging.warning(f"Cyber safeguard flagged target {target_cmd}, skipping")
counter["value"] += 1
cycle += 1
continue
raise
except Exception as e:
msg = str(e)
if "flagged this message for a cybersecurity topic" in msg or "Claude Code returned an error result" in msg:
logging.warning(f"Cyber safeguard flagged target {target_cmd}, skipping")
counter["value"] += 1
cycle += 1
continue
raise
branch_coverage, line_coverage, func_coverage = get_coverage(
config.cov_target, paths.target_dir, paths.database_dir, paths.branch_path, paths.line_path, paths.function_path,
paths.is_program_path, paths.current_cov_path
)
current_coverage = save_coverage_report(
config.cov_target, paths.cov_report_path, paths.token_path,
branch_coverage, line_coverage, func_coverage, None
)
if config.strategy == "wo_t":
print("Finished: WO_TARGET senario done!")
sys.exit(0)
error = None
std_out = None
cycle += 1
counter["value"] += 1
elif process_type == "exp":
empty_id = export_to_seed_dir(paths.snap_dir, paths.shell_dir, target_cmd)
print(f"\nGenerated seed_id: {empty_id}")
print(f"\nNext action:")
print(f"python3.12 run.py {target_cmd} set {empty_id}")
elif process_type == "set": # "car_prepare" # prepare the set directory
if not os.path.exists(f"{paths.transit_dir}"):
create_directory(f"{paths.transit_dir}")
generate_base_argv(
paths.target_shell_dir, paths.base_argv_path,
process_type, paths.target_dir, paths.database_dir, database_json, target_cmd, directory_id
)
print(f"Input argvs: saved at {paths.base_argv_path}")
elif process_type == "file":
input_file_dir = f"{paths.seed_dir}/pilot/input/{target_cmd}_{directory_id}"
generate_input_files(
paths.base_argv_path, input_file_dir,
target_cmd, directory_id, llm_interface,
paths.database_dir, paths.target_dir, paths.transit_dir, database_json, paths.seed_dir,
paths.target_shell_dir, process_type
)
print(f"\nInput files: saved at {input_file_dir}")
elif process_type == "carpet":
carpet_argv_path = f'{paths.seed_dir}/pilot/carpet_argvs/{target_cmd}_{directory_id}.txt'
generate_carpet_argv(
paths.base_argv_path, carpet_argv_path,
llm_interface, paths.chat_dir, paths.transit_dir, paths.database_dir, target_cmd, directory_id
)
print(f"\nSaved: {carpet_argv_path}")
elif process_type == "zigzag":
zigzag_argv_path = f"{paths.seed_dir}/pilot/keyword_dict/list_{target_cmd}_{directory_id}.txt"
generate_zigzag_argv(
paths.base_argv_path, zigzag_argv_path,
target_cmd
)
print(f"\nSaved: {zigzag_argv_path}")
elif process_type == "afl_argv":
#input_path = f"{paths.transit_dir}/afl_{target_cmd}_{directory_id}.txt"
afl_argv_dir = f"{paths.seed_dir}/pilot/afl_argvs/{target_cmd}_{directory_id}"
generate_afl_argv(
paths.base_argv_path, afl_argv_dir,
paths.chat_dir, paths.transit_dir, target_cmd, directory_id
)
pass
print(f"\n=============== End of {process_type} ===============\n\n")
except SystemExit as e:
if e.code == 0:
will_show = True
else:
will_show = False
traceback.print_exc()
except KeyboardInterrupt:
will_show = True
traceback.print_exc()
except:
will_show = False
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
target_cmd = str(sys.argv[1])
process_type = str(sys.argv[2])
directory_id = None
if process_type in [
"set",
"file",
"carpet",
"zigzag",
"afl_argv"
]:
directory_id = str(sys.argv[3])
home_dir = os.path.dirname(os.path.abspath(__file__))
config_path = f"{home_dir}/config.json"
sys_config_path = f"{home_dir}/config_sys.json"
user_config = read_json(f"{config_path}")
sys_config = read_json(f"{sys_config_path}")
config = merge_config(user_config, sys_config)
if config.strategy not in ["wo_read", "wo_t", "random_t", "base"]:
raise ValueError('Must within ["wo_read", "wo_t", "random_t", "base"]')
try:
explorer_main(
target_cmd, process_type, directory_id, home_dir, config
)
except:
will_show = False
traceback.print_exc()
sys.exit(1)