Exemplo n.º 1
0
    def testTypedefDuplicateType(self):
        body = """
      typedef struct data {
        int a;
        int b;
      } data_s;

      struct s {
        struct data* f1;
      };

      extern "C" uint function_using_typedefs(struct s* a1, data_s* a2);
    """
        generator = code.Generator([analyze_string(body)])
        functions = generator._get_functions()
        self.assertLen(functions, 1)

        args = functions[0].arguments()
        self.assertLen(args, 2)

        types = generator._get_related_types()
        self.assertLen(generator.translation_units[0].types_to_skip, 1)

        names = [t._clang_type.spelling for t in types]
        self.assertSameElements(['data_s', 'struct s'], names)

        # Extra check for generation, in case rendering throws error for this test.
        generator.generate('Test', [], 'sapi::Tests', None, None)
Exemplo n.º 2
0
    def testFunctionPointerRelatedTypes(self):
        body = """
      typedef unsigned int uint;
      typedef unsigned char uchar;
      typedef uint (*funcp)(uchar, uchar);

      struct struct_1 {
        uint (*func)(uchar);
        int a;
      };

      extern "C" void function(struct struct_1* a1, funcp a2);
    """
        generator = code.Generator([analyze_string(body)])
        functions = generator._get_functions()
        self.assertLen(functions, 1)

        args = functions[0].arguments()
        self.assertLen(args, 2)

        types = args[0].get_related_types()
        names = [t._clang_type.spelling for t in types]
        self.assertLen(types, 3)
        self.assertSameElements(names, ['struct struct_1', 'uint', 'uchar'])

        types = args[1].get_related_types()
        names = [t._clang_type.spelling for t in types]
        self.assertLen(types, 3)
        self.assertSameElements(names, ['funcp', 'uint', 'uchar'])

        # Extra check for generation, in case rendering throws error for this test.
        generator.generate('Test', [], 'sapi::Tests', None, None)
Exemplo n.º 3
0
    def testCollectDefines(self):
        body = """
      #define SIZE 1024
      #define NOT_USED 7
      #define SIZE2 2*1024
      #define SIZE3 1337
      #define SIZE4 10
      struct test {
        int a[SIZE];
        char b[SIZE2];
        float c[777];
        int (*d)[SIZE3*SIZE4];
      };
      extern "C" int function_1(struct test* a1);
    """
        generator = code.Generator([analyze_string(body)])
        self.assertLen(generator.translation_units, 1)

        generator._get_related_types()
        tu = generator.translation_units[0]
        tu._process()

        self.assertLen(tu.required_defines, 4)
        defines = generator._get_defines()
        self.assertLen(defines, 4)
        self.assertIn('#define SIZE 1024', defines)
        self.assertIn('#define SIZE2 2 * 1024', defines)
        self.assertIn('#define SIZE3 1337', defines)
        self.assertIn('#define SIZE4 10', defines)

        # Extra check for generation, in case rendering throws error for this test.
        generator.generate('Test', [], 'sapi::Tests', None, None)
Exemplo n.º 4
0
    def testYaraCase(self):
        body = """
      #define YR_ALIGN(n) __attribute__((aligned(n)))
      #define DECLARE_REFERENCE(type, name) union {    \
        type name;            \
        int64_t name##_;      \
      } YR_ALIGN(8)
      struct YR_NAMESPACE {
        int32_t t_flags[1337];
        DECLARE_REFERENCE(char*, name);
      };

      extern "C" int function_1(struct YR_NAMESPACE* a1);
    """
        generator = code.Generator([analyze_string(body)])
        self.assertLen(generator.translation_units, 1)

        generator._get_related_types()
        tu = generator.translation_units[0]
        tu._process()

        self.assertLen(tu.required_defines, 2)
        defines = generator._get_defines()
        # _get_defines will add dependant defines to tu.required_defines
        self.assertLen(defines, 2)
        gold = '#define DECLARE_REFERENCE('
        # DECLARE_REFERENCE must be second to pass this test
        self.assertTrue(defines[1].startswith(gold))

        # Extra check for generation, in case rendering throws error for this test.
        generator.generate('Test', [], 'sapi::Tests', None, None)
Exemplo n.º 5
0
    def testEnumRelatedTypes(self):
        body = """
      enum Enumeration { ONE, TWO, THREE };
      typedef enum Numbers { UNKNOWN, FIVE = 5, SE7EN = 7 } Nums;
      typedef enum { SIX = 6, TEN = 10 } SixOrTen;
      enum class Color : long long { RED, GREEN = 20, BLUE };  // NOLINT
      enum struct Direction { LEFT = 'l', RIGHT = 'r' };
      enum __rlimit_resource {  RLIMIT_CPU = 0, RLIMIT_MEM = 1};

      extern "C" int function_using_enums(Enumeration a1, SixOrTen a2, Color a3,
                           Direction a4, Nums a5, enum __rlimit_resource a6);
     """
        generator = code.Generator([analyze_string(body)])
        functions = generator._get_functions()
        self.assertLen(functions, 1)

        args = functions[0].arguments()
        self.assertLen(args, 6)

        self.assertLen(args[0].get_related_types(), 1)
        self.assertLen(args[1].get_related_types(), 1)
        self.assertLen(args[2].get_related_types(), 1)
        self.assertLen(args[3].get_related_types(), 1)
        self.assertLen(args[4].get_related_types(), 1)
        self.assertLen(args[5].get_related_types(), 1)

        # Extra check for generation, in case rendering throws error for this test.
        generator.generate('Test', [], 'sapi::Tests', None, None)
Exemplo n.º 6
0
 def testElaboratedArgument2(self):
     body = """
   typedef struct { int a; char b; } x;
   extern "C" int function(x a) { return a.a; }
 """
     generator = code.Generator([analyze_string(body)])
     with self.assertRaisesRegexp(ValueError, r'Elaborate.*mapped.*'):
         generator.generate('Test', ['function'], 'sapi::Tests', None, None)
Exemplo n.º 7
0
    def testFilterFunctionsFromInputFilesOnly(self):
        file1_code = """
      #include "/f2.h"

      extern "C" int function1();
    """
        file2_code = """
      extern "C" int function2();
    """

        files = [('f1.h', file1_code), ('/f2.h', file2_code)]
        generator = code.Generator([analyze_strings('f1.h', files)])
        functions = generator._get_functions()
        self.assertLen(functions, 2)

        generator = code.Generator([analyze_strings('f1.h', files, True)])
        functions = generator._get_functions()
        self.assertLen(functions, 1)
Exemplo n.º 8
0
 def testArgumentNames(self, body, names):
     generator = code.Generator([analyze_string(body)])
     functions = generator._get_functions()
     self.assertLen(functions, 1)
     self.assertLen(functions[0].argument_types, len(names))
     # Extra check for generation, in case rendering throws error for this test.
     generator.generate('Test', [], 'sapi::Tests', None, None)
     for t in functions[0].argument_types:
         self.assertIn(t.name, names)
Exemplo n.º 9
0
 def testGetMappedType(self):
     body = """
   typedef unsigned int uint;
   typedef uint* uintp;
   extern "C" uint function(uintp a) { return *a; }
 """
     generator = code.Generator([analyze_string(body)])
     result = generator.generate('Test', [], 'sapi::Tests', None, None)
     self.assertMultiLineEqual(code_test_util.CODE_GOLD_MAPPED, result)
Exemplo n.º 10
0
    def testArrayAsParam(self):
        body = """
      extern "C" int function_using_enums(char a[10], char *const __argv[]);
     """
        generator = code.Generator([analyze_string(body)])
        functions = generator._get_functions()
        self.assertLen(functions, 1)

        args = functions[0].arguments()
        self.assertLen(args, 2)
Exemplo n.º 11
0
    def testStructureRelatedTypes(self):
        body = """
      typedef unsigned int uint;

      typedef struct {
        uint a;
        struct {
          int a;
          int b;
        } b;
      } struct_1;

      struct struct_2 {
        uint a;
        char b;
        struct_1* c;
      };

      typedef struct a {
        int b;
      } struct_a;

      extern "C" int function_using_structures(struct struct_2* a1, struct_1* a2,
      struct_a* a3);
    """
        generator = code.Generator([analyze_string(body)])
        functions = generator._get_functions()
        self.assertLen(functions, 1)

        args = functions[0].arguments()
        self.assertLen(args, 3)

        types = args[0].get_related_types()
        names = [t._clang_type.spelling for t in types]
        self.assertLen(types, 3)
        self.assertSameElements(names, ['struct struct_2', 'uint', 'struct_1'])

        types = args[1].get_related_types()
        names = [t._clang_type.spelling for t in types]
        self.assertLen(types, 2)
        self.assertSameElements(names, ['struct_1', 'uint'])

        names = [
            t._clang_type.spelling for t in generator._get_related_types()
        ]
        self.assertEqual(names,
                         ['uint', 'struct_1', 'struct struct_2', 'struct_a'])

        types = args[2].get_related_types()
        self.assertLen(types, 1)

        # Extra check for generation, in case rendering throws error for this test.
        generator.generate('Test', [], 'sapi::Tests', None, None)
Exemplo n.º 12
0
    def testEnumGeneration(self):
        body = """
      enum ProcessStatus {
        OK = 0,
        ERROR = 1,
      };

      extern "C" ProcessStatus ProcessDatapoint(ProcessStatus status) {
        return status;
      }
    """
        generator = code.Generator([analyze_string(body)])
        result = generator.generate('Test', [], 'sapi::Tests', None, None)
        self.assertMultiLineEqual(code_test_util.CODE_ENUM_GOLD, result)
Exemplo n.º 13
0
def main(c_flags):
    # remove path to current binary
    c_flags.pop(0)
    logging.debug(FLAGS.sapi_functions)
    extract_includes(FLAGS.sapi_isystem, c_flags)
    tus = code.Analyzer.process_files(FLAGS.sapi_in, c_flags)
    generator = code.Generator(tus)
    result = generator.generate(FLAGS.sapi_name, FLAGS.sapi_functions,
                                FLAGS.sapi_ns, FLAGS.sapi_out,
                                FLAGS.sapi_embed_dir, FLAGS.sapi_embed_name)

    if FLAGS.sapi_out:
        with open(FLAGS.sapi_out, 'w') as out_file:
            out_file.write(result)
    else:
        sys.stdout.write(result)
Exemplo n.º 14
0
    def testTypeOrder(self, func, a1, a2):
        """Checks if comparison functions of Type class work properly.

    This is necessary for Generator._get_related_types to return types in
    proper order, ready to be emitted in the generated file. To be more
    specific: emitted types will be ordered in a way that would allow
    compilation ie. if structure field type is a typedef, typedef definition
    will end up before structure definition.

    Args:
      func: comparison assert to call
      a1: function argument number to take the type to compare
      a2: function argument number to take the type to compare
    """

        file1_code = """
    typedef unsigned int uint;
    #include "/f2.h"
    typedef uint u32;
    #include "/f3.h"

    struct args {
      u32 a;
      uchar b;
      ulong c;
      ushort d;
      chr e;
    };
    extern "C" int function(struct args* a0, uint a1, ushort a2, chr a3,
                 uchar a4, u32 a5, ulong a6, struct args* a7);
    """
        file2_code = """
    typedef unsigned short ushort;
    #include "/f4.h"
    typedef unsigned char uchar;"""
        file3_code = 'typedef unsigned long ulong;'
        file4_code = 'typedef char chr;'
        files = [('f1.h', file1_code), ('/f2.h', file2_code),
                 ('/f3.h', file3_code), ('/f4.h', file4_code)]
        generator = code.Generator([analyze_strings('f1.h', files)])
        functions = generator._get_functions()
        self.assertLen(functions, 1)

        args = functions[0].arguments()
        getattr(self, func)(args[a1], args[a2])
        # Extra check for generation, in case rendering throws error for this test.
        generator.generate('Test', [], 'sapi::Tests', None, None)
Exemplo n.º 15
0
    def testForwardDeclaration(self):
        body = """
      struct struct_6_def;
      typedef struct struct_6_def struct_6;
      typedef struct_6* struct_6p;
      typedef void (*function_p3)(struct_6p);
      struct struct_6_def {
        function_p3 fn;
      };

      extern "C" void function_using_type_loop(struct_6p a1);
    """
        generator = code.Generator([analyze_string(body)])
        functions = generator._get_functions()
        self.assertLen(functions, 1)

        args = functions[0].arguments()
        self.assertLen(args, 1)

        types = args[0].get_related_types()
        names = [t._clang_type.spelling for t in types]
        self.assertLen(types, 4)
        self.assertSameElements(
            names,
            ['struct_6p', 'struct_6', 'struct struct_6_def', 'function_p3'])

        self.assertLen(generator.translation_units, 1)
        self.assertLen(generator.translation_units[0].forward_decls, 1)

        t = next(x for x in types
                 if x._clang_type.spelling == 'struct struct_6_def')
        self.assertIn(t, generator.translation_units[0].forward_decls)

        names = [
            t._clang_type.spelling for t in generator._get_related_types()
        ]
        self.assertEqual(
            names,
            ['struct_6', 'struct_6p', 'function_p3', 'struct struct_6_def'])

        # Extra check for generation, in case rendering throws error for this test.
        forward_decls = generator._get_forward_decls(
            generator._get_related_types())
        self.assertLen(forward_decls, 1)
        self.assertEqual(forward_decls[0], 'struct struct_6_def;')
        generator.generate('Test', [], 'sapi::Tests', None, None)
Exemplo n.º 16
0
    def testDoubleFunction(self):
        body = """
      extern "C" int function_1(int a);
      extern "C" int function_1(int a) {
        return a + 1;
      };
    """
        generator = code.Generator([analyze_string(body)])
        self.assertLen(generator.translation_units, 1)

        tu = generator.translation_units[0]
        tu._process()

        self.assertLen(tu.functions, 1)

        # Extra check for generation, in case rendering throws error for this test.
        generator.generate('Test', [], 'sapi::Tests', None, None)
Exemplo n.º 17
0
    def testTypeToString(self):
        body = """
      #define SIZE 1024
      typedef unsigned int uint;

      typedef struct {
      #if SOME_DEFINE >= 12 \
      && SOME_OTHER == 13
        uint a;
      #else
        uint aa;
      #endif
        struct {
          uint a;
          int b;
          char c[SIZE];
        } b;
      } struct_1;

      extern "C" int function_using_structures(struct_1* a1);
    """

        # pylint: disable=trailing-whitespace
        expected = """typedef struct {
#if SOME_DEFINE >= 12 && SOME_OTHER == 13
\tuint a ;
#else
\tuint aa ;
#endif
\tstruct {
\t\tuint a ;
\t\tint b ;
\t\tchar c [ SIZE ] ;
\t} b ;
} struct_1"""
        generator = code.Generator([analyze_string(body)])
        functions = generator._get_functions()
        self.assertLen(functions, 1)

        types = generator._get_related_types()
        self.assertLen(types, 2)
        self.assertEqual('typedef unsigned int uint', types[0].stringify())
        self.assertMultiLineEqual(expected, types[1].stringify())

        # Extra check for generation, in case rendering throws error for this test.
        generator.generate('Test', [], 'sapi::Tests', None, None)
Exemplo n.º 18
0
  def testCodeGeneratorOutput(self):
    body = """
      extern "C" {
        int function_a(int x, int y) { return x + y; }

        int types_1(bool a0, unsigned char a1, char a2, unsigned short a3, short a4);
        int types_2(int a0, unsigned int a1, long a2, unsigned long a3);
        int types_3(long long a0, unsigned long long a1, float a2, double a3);
        int types_4(signed char a0, signed short a1, signed int a2, signed long a3);
        int types_5(signed long long a0, long double a1);
        void types_6(char* a0);
      }
    """
    functions = ['function_a', 'types_1', 'types_2', 'types_3', 'types_4',
                 'types_5', 'types_6']
    generator = code.Generator([analyze_string(body)])
    result = generator.generate('Test', functions, 'sapi::Tests', None, None)
    self.assertMultiLineEqual(code_test_util.CODE_GOLD, result)
Exemplo n.º 19
0
    def testJpegTurboCase(self):
        body = """
      typedef short JCOEF;
      #define DCTSIZE2 1024
      typedef JCOEF JBLOCK[DCTSIZE2];

      extern "C" void function(JBLOCK* a);
    """
        generator = code.Generator([analyze_string(body)])
        self.assertLen(generator.translation_units, 1)

        # initialize all internal data
        generator.generate('Test', [], 'sapi::Tests', None, None)

        tu = generator.translation_units[0]
        self.assertLen(tu.functions, 1)
        self.assertLen(generator._get_defines(), 1)
        self.assertLen(generator._get_related_types(), 2)
Exemplo n.º 20
0
    def testTypeEq(self):
        body = """
    typedef unsigned int uint;
    extern "C" void function(uint a1, uint a2, char a3);
    """
        generator = code.Generator([analyze_string(body)])
        functions = generator._get_functions()
        self.assertLen(functions, 1)

        args = functions[0].arguments()
        self.assertLen(args, 3)
        self.assertEqual(args[0], args[1])
        self.assertNotEqual(args[0], args[2])
        self.assertNotEqual(args[1], args[2])

        self.assertLen(set(args), 2)
        # Extra check for generation, in case rendering throws error for this test.
        generator.generate('Test', [], 'sapi::Tests', None, None)
Exemplo n.º 21
0
    def testMultipleTypesWhenConst(self):
        body = """
      struct Instance {
        void* instance = nullptr;
        void* state_memory = nullptr;
        void* scratch_memory = nullptr;
      };

      extern "C" void function1(Instance* a);
      extern "C" void function2(const Instance* a);
    """
        generator = code.Generator([analyze_string(body)])
        self.assertLen(generator.translation_units, 1)

        # Initialize all internal data
        generator.generate('Test', [], 'sapi::Tests', None, None)

        tu = generator.translation_units[0]
        self.assertLen(tu.functions, 2)
        self.assertLen(generator._get_related_types(), 1)
Exemplo n.º 22
0
    def testReference(self):
        body = """
      struct Instance {
        int a;
      };

      void Function1(Instance& a, Instance&& a);
    """
        generator = code.Generator([analyze_string(body)])
        self.assertLen(generator.translation_units, 1)

        # Initialize all internal data
        generator.generate('Test', [], 'sapi::Tests', None, None)

        tu = generator.translation_units[0]
        self.assertLen(tu.functions, 1)

        # this will return 0 related types because function will be mangled and
        # filtered out by generator
        self.assertEmpty(generator._get_related_types())
        self.assertLen(next(iter(tu.functions)).get_related_types(), 1)
Exemplo n.º 23
0
    def testDefineStructBody(self):
        body = """
      #define STRUCT_BODY \
      int a;  \
      char b; \
      long c
      struct test {
        STRUCT_BODY;
      };
      extern "C" void function(struct test* a1);
    """

        generator = code.Generator([analyze_string(body)])
        self.assertLen(generator.translation_units, 1)

        # initialize all internal data
        generator.generate('Test', [], 'sapi::Tests', None, None)
        tu = generator.translation_units[0]

        self.assertLen(tu.functions, 1)
        self.assertLen(tu.required_defines, 1)
Exemplo n.º 24
0
    def testCppHeader(self):
        path = 'tmp.h'
        content = """
      int sum(int a, float b);

      extern "C" int sum(int a, float b);
    """
        unsaved_files = [(path, content)]
        generator = code.Generator([analyze_strings(path, unsaved_files)])
        # Initialize all internal data
        generator.generate('Test', [], 'sapi::Tests', None, None)

        # generator should filter out mangled function
        functions = generator._get_functions()
        self.assertLen(functions, 1)

        tu = generator.translation_units[0]
        functions = tu.get_functions()
        self.assertLen(functions, 2)

        mangled_names = [f.mangled_name for f in functions]
        self.assertSameElements(mangled_names, ['sum', '_Z3sumif'])
Exemplo n.º 25
0
    def testUnionRelatedTypes(self):
        body = """
      typedef unsigned int uint;

      typedef union {
        uint a;
        union {
          int a;
          int b;
        } b;
      } union_1;

      union union_2 {
        uint a;
        char b;
        union_1* c;
      };

      extern "C" int function_using_unions(union union_2* a1, union_1* a2);
    """
        generator = code.Generator([analyze_string(body)])
        functions = generator._get_functions()
        self.assertLen(functions, 1)

        args = functions[0].arguments()
        self.assertLen(args, 2)

        types = args[0].get_related_types()
        names = [t._clang_type.spelling for t in types]
        self.assertLen(types, 3)
        self.assertSameElements(names, ['union union_2', 'uint', 'union_1'])

        types = args[1].get_related_types()
        names = [t._clang_type.spelling for t in types]
        self.assertLen(types, 2)
        self.assertSameElements(names, ['union_1', 'uint'])

        # Extra check for generation, in case rendering throws error for this test.
        generator.generate('Test', [], 'sapi::Tests', None, None)
Exemplo n.º 26
0
    def testTypedefRelatedTypes(self):
        body = """
      typedef unsigned int uint;
      typedef uint* uint_p;
      typedef uint_p* uint_pp;

      typedef struct data {
        int a;
        int b;
      } data_s;
      typedef data_s* data_p;

      extern "C" uint function_using_typedefs(uint_p a1, uint_pp a2, data_p a3);
    """
        generator = code.Generator([analyze_string(body)])
        functions = generator._get_functions()
        self.assertLen(functions, 1)

        args = functions[0].arguments()
        self.assertLen(args, 3)

        types = args[0].get_related_types()
        names = [t._clang_type.spelling for t in types]
        self.assertLen(types, 2)
        self.assertSameElements(names, ['uint_p', 'uint'])

        types = args[1].get_related_types()
        names = [t._clang_type.spelling for t in types]
        self.assertLen(types, 3)
        self.assertSameElements(names, ['uint_pp', 'uint_p', 'uint'])

        types = args[2].get_related_types()
        names = [t._clang_type.spelling for t in types]
        self.assertLen(types, 2)
        self.assertSameElements(names, ['data_s', 'data_p'])

        # Extra check for generation, in case rendering throws error for this test.
        generator.generate('Test', [], 'sapi::Tests', None, None)
Exemplo n.º 27
0
 def testStaticFunctions(self):
     body = 'static int function() { return 7; };'
     generator = code.Generator([analyze_string(body)])
     self.assertEmpty(generator._get_functions())