Skip to content

Commit cb74839

Browse files
kcwumikea
authored andcommitted
add fuzzer for libtsm (#46)
1 parent 4a10146 commit cb74839

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

libtsm/Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2016 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
################################################################################
16+
17+
FROM ossfuzz/base-libfuzzer
18+
MAINTAINER kcwu@csie.org
19+
RUN apt-get install -y make autoconf automake libtool pkg-config
20+
21+
RUN git clone git://people.freedesktop.org/~dvdhrm/libtsm
22+
COPY build.sh libtsm_fuzzer.c /src/

libtsm/Jenkinsfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2016 Google Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
////////////////////////////////////////////////////////////////////////////////
16+
17+
def libfuzzerBuild = fileLoader.fromGit('infra/libfuzzer-pipeline.groovy',
18+
'https://github.com/google/oss-fuzz.git')
19+
20+
libfuzzerBuild {
21+
git = "git://people.freedesktop.org/~dvdhrm/libtsm"
22+
}

libtsm/build.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash -eu
2+
# Copyright 2016 Google Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
################################################################################
17+
18+
cd /src/libtsm
19+
20+
# build the library.
21+
./autogen.sh
22+
make clean all
23+
24+
# build your fuzzer(s)
25+
$CC $CFLAGS -Isrc/tsm \
26+
-o /out/libtsm_fuzzer \
27+
/src/libtsm_fuzzer.c \
28+
.libs/libtsm.a \
29+
-lfuzzer $FUZZER_LDFLAGS

libtsm/libtsm_fuzzer.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2016 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "libtsm.h"
6+
7+
#define WIDTH 80
8+
#define HEIGHT 24
9+
10+
static void terminal_write_fn(struct tsm_vte *vte,
11+
const char *u8,
12+
size_t len,
13+
void *data)
14+
{
15+
// try to access the written data
16+
static char out[4096];
17+
while (len--)
18+
out[len % sizeof(out)] = u8[len];
19+
}
20+
21+
static int term_draw_cell(struct tsm_screen *screen, uint32_t id,
22+
const uint32_t *ch, size_t len,
23+
unsigned int cwidth, unsigned int posx,
24+
unsigned int posy,
25+
const struct tsm_screen_attr *attr,
26+
tsm_age_t age, void *data)
27+
{
28+
if (posx >= WIDTH || posy >= HEIGHT)
29+
abort();
30+
return 0;
31+
}
32+
33+
// Entry point for LibFuzzer.
34+
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
35+
struct tsm_screen *screen;
36+
struct tsm_vte *vte;
37+
const int scrollback_size = 200; // frecon use 200
38+
39+
tsm_screen_new(&screen, NULL, NULL);
40+
tsm_screen_set_max_sb(screen, scrollback_size);
41+
tsm_vte_new(&vte, screen, terminal_write_fn, NULL, NULL, NULL);
42+
tsm_screen_resize(screen, WIDTH, HEIGHT);
43+
44+
tsm_vte_input(vte, (const char*) data, size);
45+
tsm_screen_draw(screen, term_draw_cell, NULL);
46+
47+
tsm_vte_unref(vte);
48+
tsm_screen_unref(screen);
49+
return 0;
50+
}

0 commit comments

Comments
 (0)