/src/mpv/fuzzers/common.h
Line | Count | Source |
1 | | /* |
2 | | * This file is part of mpv. |
3 | | * |
4 | | * mpv is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Lesser General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2.1 of the License, or (at your option) any later version. |
8 | | * |
9 | | * mpv is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | * GNU Lesser General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Lesser General Public |
15 | | * License along with mpv. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <limits.h> |
21 | | #include <stdbool.h> |
22 | | #include <stdint.h> |
23 | | #include <stdio.h> |
24 | | #include <stdlib.h> |
25 | | #include <string.h> |
26 | | #include <unistd.h> |
27 | | |
28 | | #include <mpv/client.h> |
29 | | |
30 | 1.84k | #define MAX_FUZZ_SIZE (100 << 10) // 100 KiB |
31 | | |
32 | | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); |
33 | | |
34 | | #define MPV_STRINGIFY_(X) #X |
35 | | #define MPV_STRINGIFY(X) MPV_STRINGIFY_(X) |
36 | | |
37 | | static inline void check_error(int status) |
38 | 0 | { |
39 | 0 | if (status < 0) { |
40 | 0 | fprintf(stderr, "mpv API error: %s\n", mpv_error_string(status)); |
41 | 0 | exit(1); |
42 | 0 | } |
43 | 0 | } |
44 | | |
45 | | static inline bool str_startswith(const char *str, size_t str_len, |
46 | | const char *prefix, size_t prefix_len) |
47 | 0 | { |
48 | 0 | if (str_len < prefix_len) |
49 | 0 | return false; |
50 | 0 | return !memcmp(str, prefix, prefix_len); |
51 | 0 | } |
52 | | |
53 | | static inline void set_fontconfig_sysroot(void) |
54 | 0 | { |
55 | 0 | #ifdef MPV_FONTCONFIG_SYSROOT |
56 | 0 | const char *sysroot = MPV_STRINGIFY(MPV_FONTCONFIG_SYSROOT); |
57 | 0 | char path[PATH_MAX]; |
58 | 0 | if (sysroot[0] != '/') { |
59 | 0 | ssize_t len = readlink("/proc/self/exe", path, sizeof(path) - 1); |
60 | 0 | if (len < 0) |
61 | 0 | exit(1); |
62 | 0 | path[len] = '\0'; |
63 | 0 | char *name = strrchr(path, '/') + 1; |
64 | 0 | snprintf(name, path + sizeof(path) - name, "%s", sysroot); |
65 | 0 | sysroot = path; |
66 | 0 | } |
67 | 0 | setenv("FONTCONFIG_SYSROOT", sysroot, 1); |
68 | 0 | #endif |
69 | 0 | } |
70 | | |
71 | | static inline bool wait_event(mpv_handle *ctx, mpv_event_id id) |
72 | 0 | { |
73 | 0 | while (1) { |
74 | 0 | mpv_event *event = mpv_wait_event(ctx, -1); |
75 | 0 | if (event->event_id == id) |
76 | 0 | return true; |
77 | 0 | if (event->event_id == MPV_EVENT_SHUTDOWN) |
78 | 0 | return false; |
79 | 0 | } |
80 | 0 | } |
81 | | |
82 | | // Waits for the core thread to process the current iteration and any pending requests. |
83 | | static inline bool sync_core(mpv_handle *ctx) |
84 | 0 | { |
85 | 0 | uint64_t sync_id = 1; |
86 | 0 | mpv_observe_property(ctx, sync_id, "user-data/fuzzer-sync", MPV_FORMAT_NONE); |
87 | 0 | bool ok; |
88 | 0 | while (1) { |
89 | 0 | mpv_event *event = mpv_wait_event(ctx, -1); |
90 | 0 | ok = event->event_id != MPV_EVENT_SHUTDOWN; |
91 | 0 | if (!ok || (event->event_id == MPV_EVENT_PROPERTY_CHANGE && |
92 | 0 | event->reply_userdata == sync_id)) |
93 | 0 | break; |
94 | 0 | } |
95 | 0 | mpv_unobserve_property(ctx, sync_id); |
96 | 0 | return ok; |
97 | 0 | } |
98 | | |
99 | | static inline void initialize(mpv_handle *ctx) |
100 | 0 | { |
101 | 0 | check_error(mpv_initialize(ctx)); |
102 | 0 | if (!wait_event(ctx, MPV_EVENT_IDLE) || !sync_core(ctx)) |
103 | 0 | exit(1); |
104 | 0 | } |
105 | | |
106 | | static inline void destroy(mpv_handle *ctx) |
107 | 0 | { |
108 | 0 | if (mpv_command(ctx, (const char *[]){"quit", NULL}) != MPV_ERROR_UNINITIALIZED) |
109 | 0 | sync_core(ctx); |
110 | 0 | mpv_terminate_destroy(ctx); |
111 | 0 | } |
112 | | |
113 | | #ifndef PLAYBACK_TIME_LIMIT |
114 | | #define PLAYBACK_TIME_LIMIT 1 |
115 | | #endif |
116 | | |
117 | | #ifndef EVENTS_LIMIT |
118 | | #define EVENTS_LIMIT 10 |
119 | | #endif |
120 | | |
121 | | static inline void player_loop(mpv_handle *ctx) |
122 | 0 | { |
123 | 0 | bool playing = false; |
124 | 0 | bool loaded = false; |
125 | 0 | int timeout = -1; |
126 | 0 | int events_limit_after_restart = EVENTS_LIMIT; |
127 | 0 | while (1) { |
128 | 0 | mpv_event *event = mpv_wait_event(ctx, timeout); |
129 | 0 | // If some events are spamming, like audio-reconfig preventing our |
130 | 0 | // timeout to trigger stop after N events. |
131 | 0 | if (timeout == PLAYBACK_TIME_LIMIT && events_limit_after_restart-- < 0) |
132 | 0 | break; |
133 | 0 | if (timeout == PLAYBACK_TIME_LIMIT && event->event_id == MPV_EVENT_NONE) |
134 | 0 | break; |
135 | 0 | if (event->event_id == MPV_EVENT_START_FILE) |
136 | 0 | loaded = playing = true; |
137 | 0 | if (event->event_id == MPV_EVENT_END_FILE) { |
138 | 0 | playing = false; |
139 | 0 | events_limit_after_restart = EVENTS_LIMIT; |
140 | 0 | timeout = -1; |
141 | 0 | } |
142 | 0 | if (playing && event->event_id == MPV_EVENT_PLAYBACK_RESTART) |
143 | 0 | timeout = PLAYBACK_TIME_LIMIT; |
144 | 0 | if (loaded && event->event_id == MPV_EVENT_IDLE) |
145 | 0 | break; |
146 | 0 | } |
147 | 0 | } |