/src/ogre/Tests/fuzz/stream_fuzz.cpp
Line | Count | Source |
1 | | /* Copyright 2023 Google LLC |
2 | | Licensed under the Apache License, Version 2.0 (the "License"); |
3 | | you may not use this file except in compliance with the License. |
4 | | You may obtain a copy of the License at |
5 | | http://www.apache.org/licenses/LICENSE-2.0 |
6 | | Unless required by applicable law or agreed to in writing, software |
7 | | distributed under the License is distributed on an "AS IS" BASIS, |
8 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
9 | | See the License for the specific language governing permissions and |
10 | | limitations under the License. |
11 | | */ |
12 | | |
13 | | /* |
14 | | * The main idea behind this fuzzer is the generate arbitrary stack traces |
15 | | * by way of recursive funcitons, and then using various calls to libunwind |
16 | | * apis arbitrarily. |
17 | | */ |
18 | | #include <stdio.h> |
19 | | #include <stdlib.h> |
20 | | #include <unistd.h> |
21 | | |
22 | | #include <iostream> |
23 | | #include <string> |
24 | | |
25 | | #include "OgreException.h" |
26 | | #include "OgreFileSystem.h" |
27 | | #include "OgreLogManager.h" |
28 | | #include "OgreStreamSerialiser.h" |
29 | | #include "OgreVector.h" |
30 | | |
31 | 310 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
32 | 310 | Ogre::LogManager logManager; |
33 | 310 | logManager.createLog("Temporary log", true, true, true); |
34 | | |
35 | 310 | char filename[256]; |
36 | 310 | sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
37 | | |
38 | 310 | FILE *fp = fopen(filename, "wb"); |
39 | 310 | if (!fp) { |
40 | 0 | return 0; |
41 | 0 | } |
42 | 310 | fwrite(data, size, 1, fp); |
43 | 310 | fclose(fp); |
44 | | |
45 | 310 | Ogre::String fileName = filename; |
46 | | |
47 | 310 | Ogre::FileSystemArchiveFactory factory; |
48 | 310 | Ogre::Archive *arch = factory.createInstance("/tmp/", false); |
49 | 310 | arch->load(); |
50 | | |
51 | 310 | Ogre::DataStreamPtr stream = arch->open(fileName); |
52 | 310 | Ogre::StreamSerialiser serialiser(stream); |
53 | 310 | try { |
54 | 310 | serialiser.readChunkBegin(); |
55 | | |
56 | 310 | Ogre::Vector3 dest; |
57 | 310 | serialiser.read(&dest, 1); |
58 | 310 | } catch (Ogre::Exception&) { |
59 | 307 | } |
60 | 310 | factory.destroyInstance(arch); |
61 | 310 | unlink(filename); |
62 | | |
63 | 310 | return 0; |
64 | 310 | } |