Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JVM: Add constructor informations in the prompt #429

Merged
merged 7 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions llm_toolkit/prompt_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,8 @@ def _format_argument(self, count: int, arg_type: str) -> str:
# java.lang.Object argument
if 'java.lang.Object' in arg_type:
base = self._get_template(self.object_arg_description_template_file)
prefix = 'Argument \#{count} requires an Object instance\n'
argument = '<argument>' + prefix + base + '</argument>'
prefix = f'Argument #{count} requires an Object instance\n'
argument = f'<argument>{prefix}{base}</argument>'
return argument

# Simple arguments
Expand Down Expand Up @@ -784,6 +784,49 @@ def _format_arguments(self) -> str:

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.benchmark.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>')

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
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
Expand All @@ -807,6 +850,7 @@ def _format_problem(self, signature: str) -> str:
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)
Expand Down
1 change: 1 addition & 0 deletions prompts/template_xml/jvm_problem.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Your goal is to write a fuzzing harness for the provided method signature using Jazzer framework from Code Intellengence. It is important that the provided solution compiles and actually calls the function specified by the method signature:
{TARGET}
{ARGUMENTS}
{CONSTRUCTORS}
{EXCEPTIONS}
Here is the source code of the target constructor for reference.
<code>
Expand Down
Loading