Skip to content

Commit

Permalink
Fixed bug where seeded params were not being placed in function calls…
Browse files Browse the repository at this point in the history
… correctly and string concatenation was incorrectly interpreted as MBA expressions
  • Loading branch information
mad-cat-lon committed Aug 9, 2024
1 parent 245c249 commit bb087e5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
24 changes: 20 additions & 4 deletions jargonaut/preprocessing/seed_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,29 @@ def leave_Call(self, original_node: cst.Call, updated_node: cst.Call):
if qualified_names:
qualified_name = next(iter(qualified_names)).name
if qualified_name in self.funcs:
new_args = original_node.args + tuple(
updated_args = list(original_node.args)
new_args = [
cst.Arg(
value=cst.Integer(
value=str(random.randint(0, 100))
value=str(random.randint(0, 1000))
)
) for _ in range(self.num_params)
]
# Find the location of the first default and insert before it
insert_idx = -1
for i, a in enumerate(original_node.args):
if isinstance(a.equal, cst.AssignEqual):
insert_idx = i
break
if insert_idx != -1:
updated_args = (
updated_args[:insert_idx] +
new_args +
updated_args[insert_idx:]
)
else:
updated_args = updated_args + new_args
return updated_node.with_changes(
args=tuple(updated_args)
)
return updated_node.with_changes(args=new_args)

return updated_node
6 changes: 2 additions & 4 deletions jargonaut/transformations/data/expr_mba.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,13 @@ def leave_BinaryOperation(
TypeInferenceProvider,
original_node.left
)
except KeyError:
pass
try:
right_inferred_type = self.get_metadata(
TypeInferenceProvider,
original_node.right
)
except KeyError:
pass
# If we can't infer the type, just do nothing to the node to be safe
return updated_node
# Account for the case where we have the following:
# a = "abcd"
# b = "efgh"
Expand Down
17 changes: 13 additions & 4 deletions jargonaut/transformations/data/int_mba.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import random
from libcst.metadata import ParentNodeProvider, TypeInferenceProvider, PositionProvider
from libcst.metadata import ScopeProvider
from libcst.metadata import GlobalScope
from libcst.metadata import (
GlobalScope,
ClassScope,
FunctionScope
)
from .mba_utils import constant_to_linear_mba
from yaspin.spinners import Spinners
from yaspin import kbi_safe_yaspin
Expand Down Expand Up @@ -125,10 +129,15 @@ def leave_Integer(

if self.inference:
scope = self.get_metadata(ScopeProvider, original_node)
# print("="*50)
available = self.func_ints
# print(available)
try:
available = []
# If we're replacing an int in a default value for a parameter, we can't use
# other int parameters
# i.e def func(x, seed_int_param_1: int, y=4)
# -> func(x, seed_int_param_1: int, y=((seed_int_param_1 ^ 4)%2*12345) would be invalid
if isinstance(scope, FunctionScope):
available = self.func_ints

node_pos = self.get_metadata(PositionProvider, original_node)
available = available + [
i.value for i in self.get_global_ints_for_node_pos(node_pos.start.line)
Expand Down

0 comments on commit bb087e5

Please sign in to comment.