aboutsummaryrefslogtreecommitdiffhomepage
path: root/src-cslul/fuzz/aflmain.c
blob: b51fcfc26d19e6cdd7d3c11185cfe66ae89bbd4b (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235

/*

  aflmain.c -- Special entry point for fuzzing C-SLUL

  Copyright © 2021-2024 Samuel Lidén Borell <samuel@kodafritt.se>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

#include "cslul.h"
#include "defaults.h"
#include <assert.h>
#include <setjmp.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

/* Looks like AFL needs these */
#include <stdio.h>
#include <unistd.h>
#include <signal.h>

__AFL_FUZZ_INIT()

/* TODO fuzzing with other files than just main.slul */
#define MAIN_SLUL ((CSlulFile)-1)
#define OUTFILE ((CSlulFile)-2)

static const unsigned char *fuzzbuff;
static const unsigned char *in_buffptr;
static size_t in_left;

/* 640K should be enough for anyone ;) */
#define MAX_MEM (640*1024)
static unsigned char cfg_memory[MAX_MEM]; /**< Not cleared between runs */
static unsigned char memory[MAX_MEM];     /**< Cleared after each run */
static unsigned char *nextalloc;
static int num_alloced;

static void *mocked_malloc(size_t size)
{
    void *p;
    size_t align;
    if (!size) return NULL;
    align = (sizeof(void*)-1) - ((((uintptr_t)nextalloc)-1) & (sizeof(void*)-1));
    if (nextalloc > &memory[MAX_MEM-size]) return NULL;
    num_alloced++;
    nextalloc += align;
    p = nextalloc;
    nextalloc += size;
    return p;
}

static void mocked_free(void *p)
{
    /* does (almost) nothing */
    if (p) num_alloced--;
}

static void *mocked_realloc(void *p, size_t size)
{
    void *newp;
    (void)p;
    /* stupid implementation that just allocates a new block */
    newp = mocked_malloc(size);
    if (!newp) return NULL;
    if (p) {
        num_alloced--; /* or it grows on each call */
        memmove(newp, p, size);
    }
    return newp;
}

static CSlulFile mocked_fopen(const char *filename, const char *mode)
{
    if (!strcmp(filename, "moduledir/main.slul")) {
        return MAIN_SLUL;
    } else if (!strcmp(mode, "rb")) {
        return (CSlulFile)NULL;
    } else {
        return OUTFILE;
    }
}

static CSlulFile mocked_createexec(const char *filename)
{
    (void)filename;
    return OUTFILE;
}

static int mocked_fclose(CSlulFile file)
{
    (void)file;
    return 0;
}

static int mocked_ferror(CSlulFile file)
{
    (void)file;
    return 0;
}

static int mocked_remove(const char *filename)
{
    (void)filename;
    return 0;
}

static size_t mocked_fwrite(const void *buffer, size_t size, size_t nmemb, CSlulFile file)
{
    (void)buffer;
    (void)size;
    (void)file;
    return nmemb;
}

static size_t mocked_fread(void *buffer, size_t size, size_t nmemb, CSlulFile file)
{
    size_t amount;
    assert(size == 1);
    assert(file == MAIN_SLUL);

    amount = nmemb <= in_left ? nmemb : in_left;
    memcpy(buffer, in_buffptr, amount);
    in_buffptr += amount;
    in_left -= amount;
    return amount;
}

static int mocked_mkdir(const char *filename)
{
    (void)filename;
    return 1;
}

static void messagetrap(const struct CSlulState *st)
{
    /* Abort on Internal Compiler Errors etc. */
    if (CSLUL_IS_INTERNAL_ERR(st->errorcode)) {
        abort();
    }
}

/*static void dry_run(struct CSlulConfig *cfg)
{
    struct CSlul *ctx;
    static const unsigned char code[] =
        "\\slul 0.0.0\n"
        "\\name test\n"
        "\n"
        "func test() -> int\n"
        "{\n"
        "    return 1+2\n"
        "}\n";
    in_buffptr = code;
    in_left = sizeof(code);

    ctx = cslul_create(cfg);
    cslul_build(ctx, "moduledir");
    cslul_free(ctx);
}*/

extern jmp_buf antioptimize;
jmp_buf antioptimize;

int main(int argc, char **argv)
{
    struct CSlulInitParams prm;
    struct CSlulConfig *cfg;

    (void)argc;
    (void)argv;
    __AFL_INIT();

    nextalloc = cfg_memory;
    prm.size = sizeof(prm);
    prm.dirsep = '/';
    prm.fptr_malloc = mocked_malloc;
    prm.fptr_free = mocked_free;
    prm.fptr_realloc = mocked_realloc;
    prm.fptr_fopen = mocked_fopen;
    prm.fptr_createexec = mocked_createexec;
    prm.fptr_fclose = mocked_fclose;
    prm.fptr_ferror = mocked_ferror;
    prm.fptr_remove = mocked_remove;
    prm.fptr_fwrite = mocked_fwrite;
    prm.fptr_fread = mocked_fread;
    prm.fptr_mkdir = mocked_mkdir;
    prm.fptr_dropprivs = NULL;

    cfg = cslul_config_create(&prm);
    assert(cfg);
    cslul_config_set_message_level(cfg, CSLUL_L_FATAL);
    cslul_config_set_message_handler(cfg, messagetrap);
    /* TODO this should probably add either all supported architectures or none */
    if (!cslul_config_add_host_arch(cfg)) assert(0);

    fuzzbuff = __AFL_FUZZ_TESTCASE_BUF;
    memset(memory, 0x7E, sizeof(memory));
    while (!setjmp(antioptimize) && __AFL_LOOP(2147483647) && !setjmp(antioptimize)) {
        struct CSlul *ctx;

        in_buffptr = fuzzbuff;
        in_left = __AFL_FUZZ_TESTCASE_LEN;
        nextalloc = memory;
        num_alloced = 0;

        ctx = cslul_create(cfg);
        cslul_build(ctx, "moduledir");
        cslul_free(ctx);

        assert(num_alloced == 0);
        memset(memory, 0x7E, nextalloc-memory);

    }
    cslul_config_free(cfg);
    return EXIT_SUCCESS;
}