Skip to content

Commit

Permalink
analysis: improve cpp func sig (#1433)
Browse files Browse the repository at this point in the history
* analysis: improve cpp func sig

Signed-off-by: David Korczynski <david@adalogics.com>

* fix styling

Signed-off-by: David Korczynski <david@adalogics.com>

---------

Signed-off-by: David Korczynski <david@adalogics.com>
  • Loading branch information
DavidKorczynski authored Feb 17, 2024
1 parent 90c7061 commit 98ea3f5
Showing 1 changed file with 57 additions and 5 deletions.
62 changes: 57 additions & 5 deletions src/fuzz_introspector/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,30 @@ def detect_branch_level_blockers(
return fuzz_blockers


def extract_namespace(mangled_function_name):
logger.info("Demangling: %s" % (mangled_function_name))
demangled_func_name = utils.demangle_cpp_func(mangled_function_name)
logger.info("Demangled name: %s" % (demangled_func_name))
if "::" not in demangled_func_name:
return []

split_namespace = demangled_func_name.split("::")
name_spaces = []
for elem in split_namespace:
if len(elem) > 0:
# Check: (anonymous namespace)
if elem[0] == '(':
name_spaces.append(elem)
elif '(' in elem:
name_spaces.append(elem.split("(")[0])
break
else:
name_spaces.append(elem)

logger.info("split namespace: %s" % (str(name_spaces)))
return name_spaces


def convert_debug_info_to_signature(function, introspector_func):
try:
func_signature = function['return_type'] + ' '
Expand All @@ -692,13 +716,41 @@ def convert_debug_info_to_signature(function, introspector_func):
# should be, e.g. if this is a method on an object. We need to identify
# this because we want the function signature to be equal to what developers
# see.

# First step: Identify namespae
# 1) demangle raw name
# 2) identify namespace
# 3) identify if namespace last part matches first argument
# 4) assemble
namespace = extract_namespace(introspector_func['raw-function-name'])

func_name = ''
param_idx = 0
if len(function['args']) > 0 and len(
introspector_func['ArgNames']
) > 0 and introspector_func['ArgNames'][0] == 'this':
func_name += function['args'][0].replace("*", '').strip() + "::"
param_idx += 1

logger.info("Namespace: %s" % (str(namespace)))
# Is this a class function?
if len(function['args']) > 0:
if len(namespace) > 1:
# Constructor handling
if namespace[-1] == function['args'][0].replace(" *", ""):
logger.info("Option 1")
func_name = "::".join(namespace[0:-1]) + "::"
param_idx += 1
# Destructor handling
elif "~" in namespace[-1] and namespace[-1].replace(
"~", "") == function['args'][0].replace(" *", ""):
logger.info("Option 2")
func_name = "::".join(namespace[0:-1]) + "::"

if not function['name'][0] == '~':
function['name'] = '~' + function['name']
param_idx += 1
# Class object handling
elif namespace[-2] == function['args'][0].replace(
" *", "").replace("const ", ""):
logger.info("Option 3")
func_name = "::".join(namespace[0:-1]) + "::"
param_idx += 1

func_name += function['name']

Expand Down

0 comments on commit 98ea3f5

Please sign in to comment.