binaryninja-api icon indicating copy to clipboard operation
binaryninja-api copied to clipboard

parse_types_from_source_file doesn't fully support the C standard.

Open lucasduffey opened this issue 9 years ago • 8 comments

>>> Architecture.parse_types_from_source_file(bv.arch, "/home/user/research/android/wpa_supplicant_8/wpa_supplicant/wps_supplicant.h")
(None, '/home/user/research/android/wpa_supplicant_8/wpa_supplicant/wps_supplicant.h:92: error: syntax error\n')

/home/user/research/android/wpa_supplicant_8/wpa_supplicant/wps_supplicant.h

# "syntax error" on the following line 92
static inline int wpas_wps_init(struct wpa_supplicant *wpa_s) 
{
        return 0;
}

lucasduffey avatar Dec 04 '16 06:12 lucasduffey

I've also seen parse_types_from_source_file syntax error on:

#if 0

lucasduffey avatar Dec 04 '16 06:12 lucasduffey

I've also seen parse_types_from_source_file syntax error on:

/*

lucasduffey avatar Dec 09 '16 05:12 lucasduffey

I'm updating this ticket with a few more similar issues with these functions which are making it so I can't load headers, at least without doing a lot of pre-processing on them.

One thing is that any #define'd items from a pre-processor seem to not be recognized (can't debug further as is in core.BNParseTypesFromSourceFile). This I can work around by writing a script that find/replaces these definitions (ideally just by calling a preprocessor).

It also fails on typedef enums. This is what the header I'm trying to load has:

typedef enum
{
    OptionA = 0,
    OptionB,
} sec_info_type_t;

I tried a ton of options till I was able to get this to work:

enum sec_info_type_t
{
    OptionA = 0,
    OptionB //note can't have trailing comma
}; //note can't use typedef of enum

rmspeers avatar Jan 09 '19 19:01 rmspeers

Yeah, I've renamed this bug to focus specifically on the pre-processor support since that's a known missing feature that unfortunately we don't have any plans for now to support.

The concern is that if we just silently ignore them we'll obviously get wrong or unexpected behavior and we don't have any plans at this time to add support with the existing SCC based solution.

We may at some point use llvm instead which would obviously have much better preprocessor support but likely quite far off.

psifertex avatar Jan 09 '19 20:01 psifertex

I've been hacking on preprocessing in a plugin this afternoon, right now trying to tweak pcpp to work for what this expects. It doesn't handle the typedef enum stuff though, which appears to be a separate issue from the preprocessing.

Is there a specific C subset or something that the core function expects to be provided in the file?

rmspeers avatar Jan 09 '19 21:01 rmspeers

Function prototypes seem to be handled incorrectly as well.

Pointers and other typedefs work:

typedef void (*test)(int a, int b);

But this causes a syntax error.

typedef void test(int a, int b);

If you create it through the UI or though parse_type_string() + define_user_type() it works fine:

>>> a = bv.parse_type_string("void test(int a, int b);")
>>> a
(<type: void (int32_t a, int32_t b)>, 'test')
>>> bv.define_user_type(a[1], a[0])
>>> bv.get_type_by_name('test')
<type: void (int32_t a, int32_t b)>

in the Types UI:

typedef void  test(int32_t a, int32_t b);

EDIT: if you remove the typedef from the source file you can get it to parse it as a function, though:

>>> bv.platform.parse_types_from_source_file(file)
<types: {}, variables: {}, functions: {'test': <type: void (int32_t a, int32_t b)>}>
>>> bv.platform.parse_types_from_source_file(file).functions['test']
<type: void (int32_t a, int32_t b)>

natronkeltner avatar Jan 10 '19 20:01 natronkeltner

Function prototypes seem to be handled incorrectly as well.

Pointers and other typedefs work:

Not in all cases, see this example source

bv.platform.parse_types_from_source("double (*my_function(int index))(double);")

will fail with a SyntaxError. Same deal when trying to parse a prototype that takes a function pointer as an argument.

irwincong avatar Mar 28 '19 22:03 irwincong

Many of the issues here have been fixed, and some appear to be invalid. We're not attempting to be completely compliant with a specific standard as we require our own extensions. If there is a particular syntax that you believe should be supported, please create a separate issue.

plafosse avatar Oct 14 '20 19:10 plafosse