File size: 6,265 Bytes
f57ae1a
 
 
 
65df539
 
 
 
f57ae1a
 
 
 
 
65df539
 
 
 
f57ae1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65df539
 
 
 
 
08c3539
 
 
 
 
f57ae1a
 
 
 
 
 
 
 
755bba7
2ba158b
9199788
 
01df533
9199788
 
 
f57ae1a
 
de399e6
65df539
 
 
f57ae1a
 
 
 
 
 
65df539
f57ae1a
 
 
 
 
 
65df539
f57ae1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65df539
08c3539
65df539
 
48b6277
65df539
 
48b6277
 
 
65df539
 
48b6277
65df539
 
 
48b6277
65df539
 
48b6277
65df539
 
48b6277
65df539
 
08c3539
 
 
 
48b6277
08c3539
 
 
65df539
 
 
 
08c3539
65df539
 
 
 
 
 
 
 
 
 
f57ae1a
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/python

import datasets

import itertools

import os

import pyarrow as pa
import pyarrow.parquet as pq

BASE_DATASET = "ejschwartz/oo-method-test"

def setexe(r):
    r['Dirname'], r['Exename'] = os.path.split(r['Binary'])
    return r

class OOMethodTestDataset(datasets.ArrowBasedBuilder):

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="combined",
            version=datasets.Version("1.0.0"),
            description="All data files combined",
        ),
        datasets.BuilderConfig(
            name="byrow",
            version=datasets.Version("1.0.0"),
            description="Split by example (dumb)",
        ),
        datasets.BuilderConfig(
            name="byfuncname",
            version=datasets.Version("1.0.0"),
            description="Split by function name",
        ),
        datasets.BuilderConfig(
            name="bylibrary",
            version=datasets.Version("1.0.0"),
            description="Split so that library functions (those appearing in >1 exe) are used for training, and non-library functions are used for testing",
        ),
        datasets.BuilderConfig(
            name="bylibrarydedup",
            version=datasets.Version("1.0.0"),
            description="Split so that library functions (those appearing in >1 exe) are used for training, and non-library functions are used for testing. Only one example per function name is retained.",
        )

    ]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def _info(self):
        return datasets.DatasetInfo(
            features = datasets.Features({'Binary': datasets.Value(dtype='string', id=None),
                 'Addr': datasets.Value(dtype='string'),
                 'Name': datasets.Value(dtype='string'),
                 'Type': datasets.ClassLabel(num_classes=2, names=['func', 'method']),
                 'Disassembly': datasets.Value(dtype='string'),
                 'Dirname': datasets.Value(dtype='string'),
                 'Exename': datasets.Value(dtype='string')}))

    def _split_generators(self, dl_manager):
        ds = datasets.load_dataset(BASE_DATASET)['combined']

        ds = ds.map(setexe, batched=False)

        if self.config.name == "combined":

            return [
                datasets.SplitGenerator(
                    name="combined",
                    gen_kwargs={
                        "ds": ds,
                    },
                ),
            ]
        
        elif self.config.name == "byrow":

            ds = ds.train_test_split(test_size=0.1, seed=42)
            #print(ds)

            return [
                datasets.SplitGenerator(
                    name="train",
                    gen_kwargs={
                        "ds": ds['train'],
                    },
                ),
                datasets.SplitGenerator(
                    name="test",
                    gen_kwargs={
                        "ds": ds['test'],
                    },
                ),

            ]
        
        elif self.config.name == "byfuncname":

            unique_names = ds.unique('Name')
            nameds = datasets.Dataset.from_dict({'Name': unique_names})

            name_split = nameds.train_test_split(test_size=0.1, seed=42)
            #print(name_split)

            train_name = name_split['train']['Name']
            test_name = name_split['test']['Name']

            return [
                datasets.SplitGenerator(
                    name="train",
                    gen_kwargs={
                        "ds": ds.filter(lambda r: r['Name'] in train_name),
                    },
                ),
                datasets.SplitGenerator(
                    name="test",
                    gen_kwargs={
                        "ds": ds.filter(lambda r: r['Name'] in test_name),
                    },
                ),

            ]
        
        elif self.config.name in ["bylibrary", "bylibrarydedup"]:
            # A function (name) is a library function if it appears in more than one Exename

            # this is (('func', 'oo.exe'))
            testcount = set(zip(ds['Name'], ds['Exename']))

            # (('func', 'path/oo.exe'))
            testcountfull = set(zip(ds['Name'], ds['Binary']))

            # sorted pairs by function name
            testcount = sorted(testcount, key=lambda x: x[0])
            testcountfull = sorted(testcountfull, key=lambda x: x[0])

            # group by function name
            grouped = itertools.groupby(testcount, lambda t: t[0])
            groupedfull = itertools.groupby(testcountfull, lambda t: t[0])

            grouped = {k: [b for _,b in g] for k, g in grouped}
            groupedfull = {k: [b for _,b in g] for k, g in groupedfull}

            library_func_names = {f for f, exes in grouped.items() if len(exes) > 1}
            library_func_names_dedup = {(f, exes[0]) for f, exes in groupedfull.items() if len(exes) > 1}
            nonlibrary_func_names = {f for f, exes in grouped.items() if len(exes) == 1}

            train_filter_fun = None
            if self.config.name == "bylibrary":
                train_filter_fun = lambda r: r['Name'] in library_func_names
            elif self.config.name == "bylibrarydedup":
                train_filter_fun = lambda r: (r['Name'], r['Binary']) in library_func_names_dedup
            else:
                assert False, "Invalid configuration"

            return [
                datasets.SplitGenerator(
                    name="train",
                    gen_kwargs={
                        "ds": ds.filter(train_filter_fun),
                    },
                ),
                datasets.SplitGenerator(
                    name="test",
                    gen_kwargs={
                        "ds": ds.filter(lambda r: r['Name'] in nonlibrary_func_names),
                    },
                ),

            ]

        else:
            assert False
    
    def _generate_tables(self, ds):

        # Converting to pandas is silly, but the old version of datasets doesn't
        # seem to have a way to convert to Arrow?
        for i, batch in enumerate(ds.to_pandas(batched=True)):
            yield i, pa.Table.from_pandas(batch)