-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathprompt_builder.py
1816 lines (1545 loc) · 74.8 KB
/
prompt_builder.py
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
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Prompt building tools.
"""
import logging
import os
import re
from abc import abstractmethod
from typing import Any, Optional, Tuple
import jinja2
from data_prep import introspector, project_targets
from experiment import oss_fuzz_checkout
from experiment.benchmark import Benchmark, FileType
from experiment.fuzz_target_error import SemanticCheckResult
from llm_toolkit import models, prompts
from results import BuildResult
logger = logging.getLogger(__name__)
DEFAULT_TEMPLATE_DIR: str = os.path.join(os.path.dirname(__file__),
'../prompts/template_xml/')
AGENT_TEMPLATE_DIR: str = os.path.join(os.path.dirname(__file__),
'../prompts/agent/')
# TODO(Dongge): Refactor this tot avoid hard-coding.
# Example files.
EXAMPLE_PATH = os.path.join(os.path.dirname(__file__), '..', 'prompts',
'example')
# Example with FuzzeDataProvider.
FDP_EXAMPLE_1_PROBLEM = os.path.join(EXAMPLE_PATH, 'gdImageString-problem.txt')
FDP_EXAMPLE_1_SOLUTION = os.path.join(EXAMPLE_PATH, 'gdImageString-solution.cc')
FDP_EXAMPLE_2_PROBLEM = os.path.join(EXAMPLE_PATH, 'mpg123_decode-problem.txt')
FDP_EXAMPLE_2_SOLUTION = os.path.join(EXAMPLE_PATH, 'mpg123_decode-solution.cc')
C_EXAMPLE_1_PROBLEM = os.path.join(EXAMPLE_PATH, 'fuzzerPolygonToCells.txt')
C_EXAMPLE_1_SOLUTION = os.path.join(EXAMPLE_PATH, 'fuzzerPolygonToCells.c')
C_EXAMPLE_2_PROBLEM = os.path.join(EXAMPLE_PATH, 'dns_message_parse.txt')
C_EXAMPLE_2_SOLUTION = os.path.join(EXAMPLE_PATH, 'dns_message_parse.c')
FDP_JVM_EXAMPLE_1_PROBLEM = os.path.join(EXAMPLE_PATH, 'joni_regex-problem.txt')
FDP_JVM_EXAMPLE_1_SOLUTION = os.path.join(EXAMPLE_PATH,
'joni_regex-solution.java')
FDP_JVM_EXAMPLE_2_PROBLEM = os.path.join(EXAMPLE_PATH,
'jansi_colors-problem.txt')
FDP_JVM_EXAMPLE_2_SOLUTION = os.path.join(EXAMPLE_PATH,
'jansi_colors-solution.java')
EXAMPLES = {
'c++': [
[FDP_EXAMPLE_1_PROBLEM, FDP_EXAMPLE_1_SOLUTION],
[FDP_EXAMPLE_2_PROBLEM, FDP_EXAMPLE_2_SOLUTION],
],
'c': [
[C_EXAMPLE_1_PROBLEM, C_EXAMPLE_1_SOLUTION],
[C_EXAMPLE_2_PROBLEM, C_EXAMPLE_2_SOLUTION],
],
'jvm': [
[FDP_JVM_EXAMPLE_1_PROBLEM, FDP_JVM_EXAMPLE_1_SOLUTION],
[FDP_JVM_EXAMPLE_2_PROBLEM, FDP_JVM_EXAMPLE_2_SOLUTION],
],
}
BUILD_ERROR_SUMMARY = 'The code has the following build issues:'
FUZZ_ERROR_SUMMARY = 'The code can build successfully but has a runtime issue: '
C_PROMPT_HEADERS_TO_ALWAYS_INCLUDES = ['stdio.h', 'stdlib.h', 'stdint.h']
class PromptBuilder:
"""Prompt builder."""
def __init__(self, model: models.LLM, initial=None):
self._model = model
self._prompt = model.prompt_type()(initial)
@abstractmethod
def build(self,
example_pair: list[list[str]],
project_example_content: Optional[list[list[str]]] = None,
project_context_content: Optional[dict] = None) -> prompts.Prompt:
"""Builds a prompt."""
@abstractmethod
def build_fixer_prompt(self, benchmark: Benchmark, raw_code: str,
error_desc: Optional[str],
errors: list[str]) -> prompts.Prompt:
"""Builds a fixer prompt."""
@abstractmethod
def build_triager_prompt(self, benchmark: Benchmark, driver_code: str,
crash_info: str, crash_func: dict) -> prompts.Prompt:
"""Builds a triager prompt."""
def post_process_generated_code(self, generated_code: str) -> str:
"""Allows prompt builder to adjust the generated code."""
# return the same by default
return generated_code
class DefaultTemplateBuilder(PromptBuilder):
"""Default builder for C/C++."""
def __init__(self,
model: models.LLM,
benchmark: Optional[Benchmark] = None,
template_dir: str = DEFAULT_TEMPLATE_DIR,
initial: Any = None):
super().__init__(model, initial)
self._template_dir = template_dir
self.benchmark = benchmark
# Load templates.
self.priming_template_file = self._find_template(template_dir,
'priming.txt')
self.cpp_priming_filler_file = self._find_template(
template_dir, 'cpp-specific-priming-filler.txt')
self.problem_template_file = self._find_template(template_dir,
'problem.txt')
self.solution_template_file = self._find_template(template_dir,
'solution.txt')
self.context_template_file = self._find_template(template_dir,
'context.txt')
self.fixer_priming_template_file = self._find_template(
template_dir, 'fixer_priming.txt')
self.fixer_problem_template_file = self._find_template(
template_dir, 'fixer_problem.txt')
self.fixer_context_template_file = self._find_template(
template_dir, 'fixer_context.txt')
self.fixer_instruction_template_file = self._find_template(
template_dir, 'fixer_instruction.txt')
self.triager_priming_template_file = self._find_template(
template_dir, 'triager_priming.txt')
self.triager_problem_template_file = self._find_template(
template_dir, 'triager_problem.txt')
def _format_priming(self, benchmark: Benchmark) -> str:
"""Formats a priming based on the prompt template."""
priming = self._get_template(self.priming_template_file)
priming = priming.replace('{LANGUAGE}', benchmark.file_type.value)
priming = priming.replace('{FUZZ_TARGET_PATH}', benchmark.target_path)
# TODO(Dongge): Add project name and fuzz target file path.
if benchmark.needs_extern:
priming += (
'IMPORTANT: The fuzz target is written in C++, whereas the '
'project-under-test is written in C. All headers, functions, and code'
'from the project must be consistently wrapped in '
'<code>extern "C"</code> to ensure error-free compilation and linkage'
'between C and C++:\n<code>\nextern "C" {\n //Include necessary C '
'headers, source files, functions, and code here.\n}\n</code>\n')
if benchmark.file_type == FileType.CPP:
type_specific_priming = self._get_template(self.cpp_priming_filler_file)
else:
type_specific_priming = ''
priming = priming.replace('{TYPE_SPECIFIC_PRIMING}', type_specific_priming)
return priming
def _find_template(self, template_dir: str, template_name: str) -> str:
"""Finds template file based on |template_dir|."""
preferred_template = os.path.join(template_dir, template_name)
# Use the preferred template if it exists.
if os.path.isfile(preferred_template):
return preferred_template
# Fall back to the default template.
default_template = os.path.join(DEFAULT_TEMPLATE_DIR, template_name)
return default_template
def _get_template(self, template_file: str) -> str:
"""Reads the template for prompts."""
with open(template_file) as file:
return file.read()
def format_problem(self, problem_content: str) -> str:
"""Formats a problem based on the prompt template."""
problem = self._get_template(self.problem_template_file)
problem = problem.replace('{PROBLEM_CONTENT}', problem_content)
return problem
def format_solution(self, solution_content: str) -> str:
"""Formats a solution based on the prompt template."""
solution = self._get_template(self.solution_template_file)
solution = solution.replace('{SOLUTION_CONTENT}', solution_content)
return solution
def format_context(self, context_info: dict) -> str:
context = jinja2.Template(self._get_template(self.context_template_file),
trim_blocks=True,
lstrip_blocks=True)
return context.render(
headers='\n'.join(context_info['files']),
must_insert=context_info['decl'],
func_source=context_info['func_source'],
xrefs='\n'.join(context_info['xrefs']),
include_statement=context_info['header'],
)
def _select_examples(self, examples: list[list],
prompt_size: int) -> list[list[str]]:
"""Selects |examples| based on |prompt_size|."""
# First remove repeated examples to avoid over fitting.
targets = set()
unique_examples = []
for example in examples:
if example[2] in targets:
continue
targets.add(example[2])
unique_examples.append(example)
if (sum(example[0] for example in unique_examples) + prompt_size
< self._model.context_window):
return [[example[1], example[2]] for example in examples]
# Then prioritize complex (i.e., long) examples.
unique_examples.sort(key=lambda x: x[0], reverse=True)
selected_examples = []
for example in unique_examples:
if example[0] + prompt_size >= self._model.context_window:
# The estimation is inaccurate, if an example's size equals to
# the limit, it's safer to not include the example.
continue
selected_examples.append([example[1], example[2]])
prompt_size += example[0]
# Write the most complex examples at the end so that LLM gives them
# a higher weight.
selected_examples.sort(key=len, reverse=True)
return selected_examples
def _add_examples(self,
example_files: list[list[str]],
final_problem: str,
example_content: Optional[list[list[str]]] = None):
"""Constructs the |example_files| to be used in the prompt."""
# Estimate prompt size so far.
prompt_size = self._model.estimate_token_num(self._prompt.get())
# Estimate space needed for the final problem.
final_problem_prompt = self._prompt.create_prompt_piece(
final_problem, 'user')
query_size = prompt_size + self._model.estimate_token_num(
final_problem_prompt)
# Collect all examples in a single list
examples = []
for problem, solution in example_files:
with open(problem) as problem_file:
problem = problem_file.read()[:-1]
with open(solution) as solution_file:
solution = solution_file.read()[:-1]
solution = project_targets.filter_target_lines(solution)
examples.append((problem, solution))
# TODO(mihaimaruseac): Should we start from these first?
if example_content:
for problem, solution in example_content:
solution = project_targets.filter_target_lines(solution)
examples.append((problem, solution))
# Next, we need to expand all templates and determine how much the size
# of the prompt would increase when adding each one of them:
weights = []
for problem, solution in examples:
problem = self.format_problem(problem)
solution = self.format_solution(solution)
problem_prompt = self._prompt.create_prompt_piece(problem, 'user')
solution_prompt = self._prompt.create_prompt_piece(solution, 'assistant')
problem_weight = self._model.estimate_token_num(problem_prompt)
solution_weight = self._model.estimate_token_num(solution_prompt)
total_weight = problem_weight + solution_weight + 1 # one \n
weights.append((total_weight, problem, solution))
# Select examples up to context window and add them to prompt.
selected_examples = self._select_examples(weights, query_size)
for problem, solution in selected_examples:
self._prompt.add_problem(problem)
self._prompt.add_solution(solution)
def build(self,
example_pair: list[list[str]],
project_example_content: Optional[list[list[str]]] = None,
project_context_content: Optional[dict] = None) -> prompts.Prompt:
"""Constructs a prompt using the templates in |self| and saves it."""
if not self.benchmark:
return self._prompt
priming = self._format_priming(self.benchmark)
final_problem = self.format_problem(self.benchmark.function_signature)
final_problem += (f'You MUST call <code>\n'
f'{self.benchmark.function_signature}\n'
f'</code> in your solution!\n')
if project_context_content:
final_problem += self.format_context(project_context_content)
final_problem += '\n<solution>'
self._prepare_prompt(priming, final_problem, example_pair,
project_example_content)
return self._prompt
def build_fixer_prompt(self,
benchmark: Benchmark,
raw_code: str,
error_desc: Optional[str],
errors: list[str],
context: str = '',
instruction: str = '') -> prompts.Prompt:
"""Prepares the code-fixing prompt."""
priming, priming_weight = self._format_fixer_priming(benchmark)
problem = self._format_fixer_problem(raw_code, error_desc, errors,
priming_weight, context, instruction)
self._prepare_prompt(priming, problem)
return self._prompt
def _format_fixer_priming(self, benchmark: Benchmark) -> Tuple[str, int]:
"""Formats a priming for code fixer based on the template."""
with open(self.fixer_priming_template_file) as f:
priming = f.read().strip() + '\n'
priming = priming.replace('{LANGUAGE}', benchmark.file_type.value)
if benchmark.needs_extern:
priming += ('\nNote that some code may need to be wrapped with '
'<code>extern "C"</code> because the project under test is '
'written in C but the fuzz target is in C++.\n')
priming_prompt = self._prompt.create_prompt_piece(priming, 'system')
priming_weight = self._model.estimate_token_num(priming_prompt)
# NOTE: We need to return the priming _as text_ and the weight. Otherwise,
# in the case of structured prompts, we will create nested structures.
return priming, priming_weight
def _format_fixer_problem(self, raw_code: str, error_desc: Optional[str],
errors: list[str], priming_weight: int,
context: str, instruction: str) -> str:
"""Formats a problem for code fixer based on the template."""
with open(self.fixer_problem_template_file) as f:
problem = f.read().strip()
problem = problem.replace('{CODE_TO_BE_FIXED}', raw_code)
if error_desc:
error_summary = FUZZ_ERROR_SUMMARY + error_desc
else:
# Build error does not pass error desc.
error_summary = BUILD_ERROR_SUMMARY
problem = problem.replace('{ERROR_SUMMARY}', error_summary)
if context:
with open(self.fixer_context_template_file) as f:
context_template = f.read().strip()
context = context_template.replace('{CONTEXT_SOURCE_CODE}', context)
problem = problem.replace('{CONTEXT}', context)
if instruction:
with open(self.fixer_instruction_template_file) as f:
instruction_template = f.read().strip()
instruction = instruction_template.replace('{INSTRUCTION}', instruction)
problem = problem.replace('{INSTRUCTION}', instruction)
problem_prompt = self._prompt.create_prompt_piece(problem, 'user')
template_piece = self._prompt.create_prompt_piece('{ERROR_MESSAGES}',
'user')
problem_weight = self._model.estimate_token_num(problem_prompt)
template_weight = self._model.estimate_token_num(template_piece)
# the template will be replaced later and should not be counted
prompt_size = priming_weight + problem_weight - template_weight
# Add extra 20-tokens redundancy
# TODO(mihaimaruseac): Is this needed?
prompt_size += 20
# We are adding errors one by one until we reach the maximum prompt size
selected_errors = []
for error in errors:
error_prompt = self._prompt.create_prompt_piece(error, 'user')
error_token_num = self._model.estimate_token_num(error_prompt)
if prompt_size + error_token_num >= self._model.context_window:
# The estimation is inaccurate, if an example's size equals to
# the limit, it's safer to not include the example.
break
prompt_size += error_token_num
selected_errors.append(error)
# Now, compose the problem part of the prompt
error_message = '\n'.join(selected_errors)
if error_message.strip():
return problem.replace('{ERROR_MESSAGES}', error_message)
# Expecting empty error message for NO_COV_INCREASE.
if SemanticCheckResult.is_no_cov_increase_err(error_desc):
return problem.replace('<error>\n', '')\
.replace('{ERROR_MESSAGES}\n', '')\
.replace('</error>\n', '')
# Log warning for an unexpected empty error message.
logger.warning(
'Unexpected empty error message in fix prompt for error_desc: %s',
str(error_desc))
return problem.replace('{ERROR_MESSAGES}', error_message)
def build_triager_prompt(self, benchmark: Benchmark, driver_code: str,
crash_info: str, crash_func: dict) -> prompts.Prompt:
"""Prepares the crash-triaging prompt."""
priming, priming_weight = self._format_triager_priming()
problem = self._format_triager_problem(benchmark, driver_code, crash_info,
crash_func, priming_weight)
self._prepare_prompt(priming, problem)
return self._prompt
def _format_triager_priming(self) -> Tuple[str, int]:
"""Formats a priming for crash triage based on the template."""
with open(self.triager_priming_template_file) as f:
priming = f.read().strip() + '\n'
priming_prompt = self._prompt.create_prompt_piece(priming, 'system')
priming_weight = self._model.estimate_token_num(priming_prompt)
# NOTE: We need to return the priming _as text_ and the weight. Otherwise,
# in the case of structured prompts, we will create nested structures.
return priming, priming_weight
def _format_triager_problem(self, benchmark: Benchmark, driver_code: str,
crash_info: str, crash_func: dict,
priming_weight: int) -> str:
"""Formats a problem for crash triage based on the template."""
all_func_code = []
for func_name, line_number in crash_func.items():
if func_name == 'LLVMFuzzerTestOneInput':
driver_code = self._slice_driver_code(benchmark.project, driver_code,
line_number)
else:
func_code = self._slice_func_code(benchmark.project, func_name,
line_number)
all_func_code.append(func_code)
with open(self.triager_problem_template_file) as f:
problem = f.read().strip()
problem = problem.replace('{CRASH_REPORT}', crash_info.strip())\
.replace('{DRIVER_CODE}', driver_code.strip())
problem_prompt = self._prompt.create_prompt_piece(problem, 'user')
template_piece = self._prompt.create_prompt_piece('{PROJECT_FUNCTION_CODE}',
'user')
problem_weight = self._model.estimate_token_num(problem_prompt)
template_weight = self._model.estimate_token_num(template_piece)
prompt_size = priming_weight + problem_weight - template_weight
# Add extra 20-tokens redundancy
prompt_size += 20
# Add function code one by one until we reach the maximum prompt size
selected_func_code = []
for func_code in all_func_code:
func_code_prompt = self._prompt.create_prompt_piece(func_code, 'user')
func_code_token_num = self._model.estimate_token_num(func_code_prompt)
if prompt_size + func_code_token_num >= self._model.context_window:
# The estimation is inaccurate, if an example's size equals to
# the limit, it's safer to not include the example.
logger.warning('Breaking because adding this function code \
would exceed context window')
break
prompt_size += func_code_token_num
selected_func_code.append(func_code)
# Compose the problem part of the prompt
project_function_code = '\n'.join(selected_func_code)
if project_function_code.strip():
return problem.replace('{PROJECT_FUNCTION_CODE}',
project_function_code.strip())
logger.warning(
'Empty project function code in triage prompt for project: %s, \
function name: %s', benchmark.project, benchmark.function_name)
return problem.replace('{PROJECT_FUNCTION_CODE}', \
'No relevant project function code')
def _prepare_prompt(
self,
priming: str,
final_problem: str,
example_pair: Optional[list[list[str]]] = None,
project_example_content: Optional[list[list[str]]] = None):
"""Constructs a prompt using the parameters and saves it."""
self._prompt.add_priming(priming)
if example_pair is None:
example_pair = []
self._add_examples(example_pair, final_problem, project_example_content)
self._prompt.add_problem(final_problem)
def _slice_driver_code(self, project: str, driver_code: str,
target_lines: set) -> str:
"""Slice the driver code up to the target line."""
target_line = max(target_lines)
lines = driver_code.split('\n')
if target_line > len(lines):
logger.warning(
'Driver target line exceed maxium limit in Project: %s, \
try to use whole driver code in trigae prompt', project)
return driver_code
code_snippet = '\n'.join(lines[:target_line])
result = f'\nLine 1 - {target_line}:\n{code_snippet}'
return result
def _slice_func_code(self, project: str, func_name: str,
target_lines: set) -> str:
"""Slice target line and four preceding lines from function code."""
func_sig = introspector.query_introspector_function_signature(
project, func_name)
func_code = introspector.query_introspector_function_source(
project, func_sig)
begin_line, end_line = introspector.query_introspector_function_line(
project, func_sig)
if begin_line != 0 and end_line != 0 and all(
begin_line <= line <= end_line for line in target_lines):
lines = func_code.split('\n')
output_lines = set()
result = []
for line in sorted(target_lines):
start = max(line - 4, begin_line)
end = line
if not any(l in output_lines for l in range(start, end + 1)):
code_snippet = '\n'.join(lines[(start -
begin_line):(end - begin_line) + 1])
result.append(f'\nFunction Name:\n{func_name}\n\
Line {start} - {end}:\n{code_snippet}')
output_lines.update(range(start, end + 1))
return '\n'.join(result)
logger.warning('Failed to slice Project: %s Function: %s at Lines: %s',
project, func_name, target_lines)
return ''
class PrototyperTemplateBuilder(DefaultTemplateBuilder):
"""Builder specifically targeted C (and excluding C++)."""
def __init__(self,
model: models.LLM,
benchmark: Benchmark,
template_dir: str = DEFAULT_TEMPLATE_DIR,
initial: Any = None):
super().__init__(model, benchmark, template_dir, initial)
self.agent_templare_dir = AGENT_TEMPLATE_DIR
# Load templates.
if benchmark.is_c_target:
self.priming_template_file = self._find_template(
self.agent_templare_dir, 'prototyper-priming.c.txt')
elif benchmark.is_cpp_target:
self.priming_template_file = self._find_template(
self.agent_templare_dir, 'prototyper-priming.cpp.txt')
else:
self.problem_template_file = self._find_template(
self.agent_templare_dir, 'prototyper-priming.txt')
self.cpp_priming_filler_file = self._find_template(
template_dir, 'cpp-specific-priming-filler.txt')
self.problem_template_file = self._find_template(template_dir,
'problem.txt')
self.solution_template_file = self._find_template(template_dir,
'solution.txt')
self.context_template_file = self._find_template(template_dir,
'context.txt')
def build(self,
example_pair: list[list[str]],
project_example_content: Optional[list[list[str]]] = None,
project_context_content: Optional[dict] = None,
tool_guides: str = '',
project_dir: str = '') -> prompts.Prompt:
"""Constructs a prompt using the templates in |self| and saves it."""
if not self.benchmark:
return self._prompt
priming = self._format_priming(self.benchmark)
priming = priming.replace('{PROJECT_DIR}', project_dir)
final_problem = self.format_problem(self.benchmark.function_signature)
final_problem += (f'You MUST call <code>\n'
f'{self.benchmark.function_signature}\n'
f'</code> in your solution!\n')
if project_context_content:
final_problem += self.format_context(project_context_content)
self._prepare_prompt(priming, final_problem, example_pair,
project_example_content)
self._prompt.append(tool_guides, True)
return self._prompt
class PrototyperFixerTemplateBuilder(PrototyperTemplateBuilder):
"""Builder specifically targeted C (and excluding C++)."""
def __init__(self,
model: models.LLM,
benchmark: Benchmark,
build_result: BuildResult,
compile_log: str,
template_dir: str = DEFAULT_TEMPLATE_DIR,
initial: Any = None):
super().__init__(model, benchmark, template_dir, initial)
# Load templates.
self.priming_template_file = self._find_template(self.agent_templare_dir,
'prototyper-fixing.txt')
self.build_result = build_result
self.compile_log = compile_log
def build(self,
example_pair: list[list[str]],
project_example_content: Optional[list[list[str]]] = None,
project_context_content: Optional[dict] = None,
tool_guides: str = '',
project_dir: str = '') -> prompts.Prompt:
"""Constructs a prompt using the templates in |self| and saves it."""
del (example_pair, project_example_content, project_context_content,
tool_guides)
if not self.benchmark:
return self._prompt
if self.build_result.build_script_source:
build_text = (f'<build script>\n{self.build_result.build_script_source}\n'
'</build script>')
else:
build_text = 'Build script reuses `/src/build.bk.sh`.'
prompt = self._get_template(self.priming_template_file)
prompt = prompt.replace('{FUZZ_TARGET_SOURCE}',
self.build_result.fuzz_target_source)
prompt = prompt.replace('{BUILD_TEXT}', build_text)
prompt = prompt.replace('{COMPILE_LOG}', self.compile_log)
prompt = prompt.replace('{FUNCTION_SIGNATURE}',
self.benchmark.function_signature)
prompt = prompt.replace('{PROJECT_DIR}', project_dir)
self._prompt.append(prompt)
return self._prompt
class EnhancerTemplateBuilder(PrototyperTemplateBuilder):
"""Builder specifically targeted C (and excluding C++)."""
def __init__(self,
model: models.LLM,
benchmark: Benchmark,
build_result: BuildResult,
error_desc: str,
errors: list[str],
template_dir: str = DEFAULT_TEMPLATE_DIR,
initial: Any = None):
super().__init__(model, benchmark, template_dir, initial)
# Load templates.
self.priming_template_file = self._find_template(self.agent_templare_dir,
'enhancer-priming.txt')
self.build_result = build_result
self.error_desc = error_desc
self.errors = errors
def build(self,
example_pair: list[list[str]],
project_example_content: Optional[list[list[str]]] = None,
project_context_content: Optional[dict] = None,
tool_guides: str = '',
project_dir: str = '') -> prompts.Prompt:
"""Constructs a prompt using the templates in |self| and saves it."""
del (example_pair, project_example_content, project_context_content)
if not self.benchmark:
return self._prompt
priming = self._get_template(self.priming_template_file)
priming = priming.replace('{LANGUAGE}', self.benchmark.file_type.value)
priming = priming.replace('{FUNCTION_SIGNATURE}',
self.benchmark.function_signature)
# TODO(dongge): Add build script to .
priming = priming.replace('{PROJECT_DIR}', project_dir)
if self.build_result.build_script_source:
build_text = (f'<build script>\n{self.build_result.build_script_source}\n'
'</build script>')
else:
build_text = 'Build script reuses `/src/build.bk.sh`.'
priming = priming.replace('{BUILD_TEXT}', build_text)
priming = priming.replace('{TOOL_GUIDES}', tool_guides)
priming_weight = self._model.estimate_token_num(priming)
problem = self._format_fixer_problem(self.build_result.fuzz_target_source,
self.error_desc, self.errors,
priming_weight, '', '')
self._prepare_prompt(priming, problem)
return self._prompt
class DefaultJvmTemplateBuilder(PromptBuilder):
"""Default builder for JVM projects."""
def __init__(self,
model: models.LLM,
benchmark: Benchmark,
template_dir: str = DEFAULT_TEMPLATE_DIR):
super().__init__(model)
self._template_dir = template_dir
self.benchmark = benchmark
self.project_url = oss_fuzz_checkout.get_project_repository(
self.benchmark.project)
# Retrieve additional properties for the target method
temp_properties = introspector.query_introspector_function_props(
self.benchmark.project, self.benchmark.function_signature)
self.exceptions = temp_properties.get('exceptions', [])
self.is_jvm_static = temp_properties.get('is-jvm-static', False)
self.need_close = temp_properties.get('need_close', False)
# Load templates.
self.priming_template_file = self._find_template(template_dir,
'jvm_priming.txt')
self.data_filler_template_file = self._find_template(
template_dir, 'jvm_specific_data_filler.txt')
self.requirement_template_file = self._find_template(
template_dir, 'jvm_requirement.txt')
self.problem_template_file = self._find_template(template_dir,
'jvm_problem.txt')
self.target_template_file = self._find_template(template_dir,
'jvm_target.txt')
self.arg_description_template_file = self._find_template(
template_dir, 'jvm_arg_description.txt')
self.import_template_file = self._find_template(template_dir,
'jvm_import_mapping.txt')
def _find_template(self, template_dir: str, template_name: str) -> str:
"""Finds template file based on |template_dir|."""
preferred_template = os.path.join(template_dir, template_name)
# Use the preferred template if it exists.
if os.path.isfile(preferred_template):
return preferred_template
# Fall back to the default template.
default_template = os.path.join(DEFAULT_TEMPLATE_DIR, template_name)
return default_template
def _get_template(self, template_file: str) -> str:
"""Reads the template for prompts."""
with open(template_file) as file:
return file.read()
def _format_exceptions(self) -> str:
"""Formats the exception thrown from this method or constructor."""
if self.exceptions:
exception_str_list = [
f'<exception>{exp}</exception>' for exp in self.exceptions
]
return '<exceptions>\n' + '\n'.join(
exception_str_list) + '\n</exceptions>'
return ''
def _format_import_mapping(self, full_class_name: str) -> str:
"""Formats the import mapping row on the prompt template."""
# full_class_name format: <package>.<class_name>$<inner_class_name>
# For example, the inner class Inner in class Test of package
# a.b.c will have a full_class_name of a.b.c.Test$Inner
class_name = full_class_name.rsplit('.')[-1]
full_class_name = full_class_name.split('$')[0]
mapping = self._get_template(self.import_template_file)
mapping = mapping.replace('{CLASS_NAME}', class_name)
mapping = mapping.replace('{FULL_CLASS_NAME}', full_class_name)
return mapping
def _format_generic_argument(self, arg_type: str) -> Tuple[str, str]:
"""Formats generic argument description."""
generic_types = arg_type.split('<', 1)[1][:-1].split(',')
new_types = []
generic_desc = []
for generic_type in generic_types:
if generic_type.endswith(('T', 'K', 'V')):
generic_type = 'java.lang.Object'
new_types.append(generic_type)
desc = (f'For generic type of {generic_type}, you MUST use '
'{RANDOM_METHODS} to generate the needed variable.')
method_str = self._get_methods_for_simple_type(generic_type)
if method_str:
desc = desc.replace('{RANDOM_METHODS}', method_str)
else:
desc = desc.replace('{RANDOM_METHODS}',
'correct constructors or static methods')
generic_desc.append(desc)
if not generic_desc:
return '', ''
generic_types = ','.join(new_types)
return f' with generic types of {generic_types}', '\n'.join(generic_desc)
def _format_argument(self, count: int, arg_type: str) -> str:
"""Formats general argument description."""
method_str = self._get_methods_for_simple_type(arg_type)
# Simple arguments
argument = self._get_template(self.arg_description_template_file)
argument = argument.replace('{ARG_COUNT}', str(count))
if method_str:
type_str = '{SIMPLE_TYPE} variable.'
desc_str = f'You must use {method_str} to generate {{ARRAY_OR_NOT}}.'
else:
type_str = '{SIMPLE_TYPE} instance {GENERIC_TYPE}.'
desc_str = ('Please generate {ARRAY_OR_NOT}. You should use constructors '
'or static methods for the generation.\nPlease also insert '
'random data into the created instance.')
argument = argument.replace('{TYPE}', type_str)
argument = argument.replace('{GENERAL_DESC}', desc_str)
# Array handling
if '[]' in arg_type:
arg_type_no_array = arg_type.replace('[]', '').split('<')[0]
argument = argument.replace('{SIMPLE_TYPE}',
f'an array of {arg_type_no_array} ')
argument = argument.replace(
'{ARRAY_OR_NOT}',
(f'multiple {arg_type_no_array} objects and initialise an array '
'of {arg_type_no_array} with the generated objects.'))
else:
argument = argument.replace('{SIMPLE_TYPE}', f'a {arg_type}')
argument = argument.replace('{ARRAY_OR_NOT}', 'the needed parameter.')
# Generic type handling
generic_type = ''
generic_desc = ''
if self._has_generic(arg_type):
generic_type, generic_desc = self._format_generic_argument(arg_type)
argument = argument.replace('{GENERIC_TYPE}', generic_type)
argument = argument.replace('{GENERIC_DESC}', generic_desc)
return argument
def _format_requirement(self, signature: str) -> str:
"""Formats a requirement based on the prompt template."""
classes = []
class_name = signature[1:].split(']')[0]
if self._need_import(class_name):
classes.append(class_name)
for arg_dict in self.benchmark.params:
arg_type = arg_dict['type'].split('<')[0]
if self._need_import(arg_type):
classes.append(arg_type)
classes = list(set(classes))
mappings = [self._format_import_mapping(type) for type in classes]
requirement = self._get_template(self.requirement_template_file)
requirement = requirement.replace('{IMPORT_MAPPINGS}', '\n'.join(mappings))
harness_name = os.path.basename(self.benchmark.target_path).replace(
'.java', '')
if harness_name:
requirement = requirement.replace('{HARNESS_NAME}', harness_name)
else:
requirement = requirement.replace('{HARNESS_NAME}', 'Fuzz')
class_name = self.benchmark.function_name[1:].split(']')[0]
if '<init>' in self.benchmark.function_name:
creation = (f'The target method is a constructor of {class_name} '
'invoke it directly with new keyword.')
elif self.is_jvm_static:
creation = ('The target method is a static method, invoke it directly '
'without creating an object.')
else:
creation = (f'You must create the {class_name} object before calling '
'the target method.')
requirement = requirement.replace('{STATIC_OR_INSTANCE}', creation)
close_statement = ''
if self.need_close:
close_statement = (
'<item>You MUST invoke the close method of the '
f'{class_name} objects in the finally block after the target method '
'is invoked.</item>')
requirement = requirement.replace('{NEED_CLOSE}', close_statement)
return requirement
def _format_data_filler(self) -> str:
"""Formats a data_filler based on the prompt template."""
data_filler = self._get_template(self.data_filler_template_file)
return data_filler
def _format_arguments(self) -> str:
"""Formats a list of argument descriptions."""
argument_descriptions = []
for count, function_arg in enumerate(self.benchmark.params):
arg_type = function_arg['type']
argument = self._format_argument(count, arg_type)
argument_descriptions.append(argument)
return '<arguments>' + '\n'.join(argument_descriptions) + '</arguments>'
def _format_constructors(self) -> str:
"""Formats a list of functions / constructors to create the object for
invoking the target method."""
if self.is_jvm_static:
return ''
constructors = []
ctrs = introspector.query_introspector_matching_function_constructor_type(
self.benchmark.project, self.benchmark.return_type, False)
for ctr in ctrs:
constructor_sig = ctr.get('function_signature', '')
if constructor_sig:
constructors.append(f'<signature>{constructor_sig}</signature>')
exceptions = introspector.query_introspector_function_props(
ctr.get('project', ''), constructor_sig).get('exceptions', [])
self.exceptions.extend(exceptions)
if constructors:
ctr_str = '\n'.join(constructors)
return f'<constructors>{ctr_str}</constructors>'
functions = []
funcs = introspector.query_introspector_matching_function_constructor_type(
self.benchmark.project, self.benchmark.return_type, True)
for func in funcs:
is_static = func.get('is_static', False)
function_sig = func.get('function_signature', '')
if not function_sig:
continue
exceptions = introspector.query_introspector_function_props(
func.get('project', ''), function_sig).get('exceptions', [])
self.exceptions.extend(exceptions)
if is_static:
functions.append(f'<item><signature>{function_sig}</signature></item>')
else:
function_class = function_sig[1:].split(']')[0]
function_str = f'<signature>{function_sig}</signature>'
function_str = function_str + (
'<prerequisite>You MUST create an '
f'{function_class} object before calling this constructing method.'
'</prerequisite>')
function_str = f'<item>{function_str}</item>'
functions.append(function_str)
if functions:
func_str = '\n'.join(functions)
return f'<constructors>{func_str}</constructors>'
return ''
def _format_source_reference(self, signature: str) -> Tuple[str, str]:
"""Formats the source code reference for this target."""
# Query for source code of the target method
source_code = introspector.query_introspector_function_source(
self.benchmark.project, signature)
# Query for source code of target method callsites
xref_source_list = []
for xref_source in introspector.query_introspector_cross_references(
self.benchmark.project, signature):
if xref_source:
xref_source_list.append(xref_source)
return source_code, '\n'.join(xref_source_list)
def _format_problem(self, signature: str) -> str:
"""Formats a problem based on the prompt template."""
is_constructor = bool('<init>' in signature)
problem = self._get_template(self.problem_template_file)
problem = problem.replace('{TARGET}',
self._get_template(self.target_template_file))
problem = problem.replace('{SIGNATURE}', signature)
problem = problem.replace('{CLASS}', signature.split('].')[0][1:])
problem = problem.replace('{REQUIREMENTS}',
self._format_requirement(signature))
problem = problem.replace('{ARGUMENTS}', self._format_arguments())
problem = problem.replace('{CONSTRUCTORS}', self._format_constructors())
problem = problem.replace('{EXCEPTIONS}', self._format_exceptions())
self_source, cross_source = self._format_source_reference(signature)
problem = problem.replace('{SELF_SOURCE}', self_source)
problem = problem.replace('{CROSS_SOURCE}', cross_source)
problem = problem.replace("{PROJECT_NAME}", self.benchmark.project)
problem = problem.replace("{PROJECT_URL}", self.project_url)
problem = problem.replace('{DATA_MAPPING}', self._format_data_filler())
if is_constructor: