Something went wrong. Try again.
MPRIS for Zig: a media player's side of the D-Bus remote control interfaces, on mdbus and zig-dbus-service.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Make the seeds the fuzz target in `tests/fuzz.zig` starts from, and write//! them out as a Zig source file.//!//! fuzz-corpus <fuzz_corpus.zig>//!//! A seed is the player's state before and after, and a method call as the//! target takes it -- its header's names, picked from lists this writes out//! too, and the values of its arguments. `calls` are calls to every method//! and property of the four MPRIS interfaces, with arguments of the right//! types, and to the standard interfaces, each of which the server answers//! with a return value; `refusals` are calls it must answer with an error --//! a value the specification does not allow, a property that cannot be//! written, a name or signature that is not there -- so the fuzzers start//! from both sides of every check. A mutation of a call that is answered gets//! into the argument decoding and what is passed on to the player; random//! bytes almost never make a call at all.
const std = @import("std");
const path = "/org/mpris/MediaPlayer2";const root = "org.mpris.MediaPlayer2";const player = "org.mpris.MediaPlayer2.Player";const tracklist = "org.mpris.MediaPlayer2.TrackList";const playlists_iface = "org.mpris.MediaPlayer2.Playlists";const properties = "org.freedesktop.DBus.Properties";
// The target's tracks and playlists; see `tests/fuzz.zig`.const track1 = "/org/example/track/1";const track2 = "/org/example/track/2";const playlist1 = "/org/example/playlist/1";
/// The names a call's parts are picked from, written into the corpus for/// the target to pick from too: everything an MPRIS player answers to,/// the tracks and playlists the target's player has, and a name of each/// kind it does not know.const paths = [_][]const u8{ path, "/", "/org", "/org/mpris", track1, track2, "/org/example/track/3", playlist1, "/org/example/playlist/2", "/org/mpris/MediaPlayer2/TrackList/NoTrack", "/elsewhere" };const interfaces = [_][]const u8{ "", root, player, tracklist, playlists_iface, properties, "org.freedesktop.DBus.Introspectable", "org.freedesktop.DBus.Peer", "org.example.Nope" };const members = [_][]const u8{ "Raise", "Quit", "Next", "Previous", "Pause", "PlayPause", "Stop", "Play", "Seek", "SetPosition", "OpenUri", "GetTracksMetadata", "AddTrack", "RemoveTrack", "GoTo", "ActivatePlaylist", "GetPlaylists", "Get", "GetAll", "Set", "Introspect", "Ping", "GetMachineId", "Nope" };const signatures = [_][]const u8{ "", "s", "ss", "ssv", "x", "ox", "o", "ao", "sob", "uusb", "d", "b", "as", "a{sv}", "u", "i", "v", "(oss)", "t", "y" };
pub fn main(init: std.process.Init) !void { const io = init.io; var arena_state: std.heap.ArenaAllocator = .init(init.gpa); defer arena_state.deinit(); const a = arena_state.allocator(); var args: std.process.Args.Iterator = .init(init.minimal.args); _ = args.skip(); const out_path = args.next() orelse return error.MissingArgument;
var calls: std.ArrayList([]const u8) = .empty; var refusals: std.ArrayList([]const u8) = .empty; const c: Calls = .{ .a = a, .list = &calls }; const r: Calls = .{ .a = a, .list = &refusals };
for ([_][]const u8{ "Raise", "Quit" }) |m| try c.add(path, root, m, "", &.{}); for ([_][]const u8{ "Next", "Previous", "Pause", "PlayPause", "Stop", "Play" }) |m| try c.add(path, player, m, "", &.{}); try c.add(path, player, "Seek", "x", &.{c.int(i64, 10_000_000)}); try c.add(path, player, "Seek", "x", &.{c.int(i64, -5_000_000)}); // Past the end, which is the next track. try c.add(path, player, "Seek", "x", &.{c.int(i64, 1_000_000_000)}); try c.add(path, player, "SetPosition", "ox", &.{ &.{try indexOf(&paths, track1)}, c.int(i64, 30_000_000) }); // A track that is not the one playing, and a position before the start: // both ignored. try c.add(path, player, "SetPosition", "ox", &.{ &.{try indexOf(&paths, track2)}, c.int(i64, 30_000_000) }); try c.add(path, player, "SetPosition", "ox", &.{ &.{try indexOf(&paths, track1)}, c.int(i64, -1) }); try c.add(path, player, "OpenUri", "s", &.{c.str("file:///music/song.flac")}); try c.add(path, tracklist, "GetTracksMetadata", "ao", &.{ &.{2}, &.{try indexOf(&paths, track1)}, &.{try indexOf(&paths, track2)} }); try c.add(path, tracklist, "GoTo", "o", &.{&.{try indexOf(&paths, track2)}}); try c.add(path, playlists_iface, "ActivatePlaylist", "o", &.{&.{try indexOf(&paths, playlist1)}}); try c.add(path, playlists_iface, "GetPlaylists", "uusb", &.{ c.int(u32, 0), c.int(u32, 10), c.str("UserDefined"), &.{1} }); try c.add(path, playlists_iface, "GetPlaylists", "uusb", &.{ c.int(u32, 1), c.int(u32, 1), c.str("Alphabetical"), &.{0} }); try c.add(path, playlists_iface, "GetPlaylists", "uusb", &.{ c.int(u32, 0xffff_ffff), c.int(u32, 0xffff_ffff), c.str(""), &.{0} });
const props = .{ .{ root, &[_][]const u8{ "CanQuit", "CanRaise", "HasTrackList", "Identity", "DesktopEntry", "SupportedUriSchemes", "SupportedMimeTypes" } }, .{ player, &[_][]const u8{ "PlaybackStatus", "LoopStatus", "Rate", "Shuffle", "Metadata", "Volume", "Position", "MinimumRate", "MaximumRate", "CanGoNext", "CanGoPrevious", "CanPlay", "CanPause", "CanSeek", "CanControl" } }, .{ tracklist, &[_][]const u8{ "Tracks", "CanEditTracks" } }, .{ playlists_iface, &[_][]const u8{ "PlaylistCount", "Orderings", "ActivePlaylist" } }, }; inline for (props) |iface| { for (iface[1]) |name| try c.add(path, properties, "Get", "ss", &.{ c.str(iface[0]), c.str(name) }); try c.add(path, properties, "GetAll", "s", &.{c.str(iface[0])}); } try c.add(path, properties, "Set", "ssv", &.{ c.str(player), c.str("LoopStatus"), &.{try indexOf(&signatures, "s")}, c.str("Track") }); try c.add(path, properties, "Set", "ssv", &.{ c.str(player), c.str("Shuffle"), &.{try indexOf(&signatures, "b")}, &.{1} }); try c.add(path, properties, "Set", "ssv", &.{ c.str(player), c.str("Volume"), &.{try indexOf(&signatures, "d")}, c.int(u64, @bitCast(@as(f64, 0.3))) }); try c.add(path, properties, "Set", "ssv", &.{ c.str(player), c.str("Rate"), &.{try indexOf(&signatures, "d")}, c.int(u64, @bitCast(@as(f64, 1.0))) }); // Below zero, which is zero. try c.add(path, properties, "Set", "ssv", &.{ c.str(player), c.str("Volume"), &.{try indexOf(&signatures, "d")}, c.int(u64, @bitCast(@as(f64, -0.5))) }); // Every property of the object at once. try c.add(path, properties, "GetAll", "s", &.{c.str("")});
for ([_][]const u8{ "/", "/org/mpris", path }) |p| try c.add(p, "org.freedesktop.DBus.Introspectable", "Introspect", "", &.{}); try c.add(path, "org.freedesktop.DBus.Peer", "Ping", "", &.{}); // With no interface named, which finds the method in any. try c.add(path, null, "Play", "", &.{}); try c.add(path, null, "Introspect", "", &.{});
// The state changing after the call, which is what `update` makes // signals of: another track, a jump in position, a different list. const playing = 0b1110_0100; try c.addAt(.{ playing, 85, playing + 1, 85 }, path, "org.freedesktop.DBus.Peer", "Ping", "", &.{}); try c.addAt(.{ playing, 85, playing, 200 }, path, "org.freedesktop.DBus.Peer", "Ping", "", &.{}); try c.addAt(.{ playing, 85, 0b0001_1010, 0 }, path, "org.freedesktop.DBus.Peer", "Ping", "", &.{}); try c.addAt(.{ 0b0001_1010, 0, playing, 85 }, path, "org.freedesktop.DBus.Peer", "Ping", "", &.{});
// What the server must refuse. A value the specification does not // allow: try r.add(path, properties, "Set", "ssv", &.{ c.str(player), c.str("LoopStatus"), &.{try indexOf(&signatures, "s")}, c.str("Sometimes") }); try r.add(path, properties, "Set", "ssv", &.{ c.str(player), c.str("Rate"), &.{try indexOf(&signatures, "d")}, c.int(u64, @bitCast(@as(f64, 2.0))) }); try r.add(path, properties, "Set", "ssv", &.{ c.str(player), c.str("Volume"), &.{try indexOf(&signatures, "d")}, c.int(u64, @bitCast(std.math.nan(f64))) }); // A property that cannot be written, or not with that type: try r.add(path, properties, "Set", "ssv", &.{ c.str(player), c.str("PlaybackStatus"), &.{try indexOf(&signatures, "s")}, c.str("Playing") }); try r.add(path, properties, "Set", "ssv", &.{ c.str(player), c.str("Shuffle"), &.{try indexOf(&signatures, "s")}, c.str("yes") }); // Names that are not there: try r.add(path, properties, "Get", "ss", &.{ c.str(player), c.str("Nope") }); try r.add(path, properties, "Get", "ss", &.{ c.str("org.example.Nope"), c.str("Volume") }); try r.add(path, properties, "GetAll", "s", &.{c.str("org.example.Nope")}); try r.add(path, properties, "Nope", "", &.{}); try r.add(path, player, "Nope", "", &.{}); try r.add(path, null, "Nope", "", &.{}); try r.add(path, "org.example.Nope", "Play", "", &.{}); try r.add("/elsewhere", player, "Play", "", &.{}); try r.add(path, "org.freedesktop.DBus.Peer", "Nope", "", &.{}); // A method called with the wrong arguments: try r.add(path, player, "Play", "s", &.{c.str("now")}); try r.add(path, player, "Seek", "s", &.{c.str("10")}); try r.add(path, properties, "Get", "s", &.{c.str(player)}); // And the track list's edits, which this player does not do. try r.add(path, tracklist, "AddTrack", "sob", &.{ c.str("file:///x.flac"), &.{try indexOf(&paths, track1)}, &.{0} }); try r.add(path, tracklist, "RemoveTrack", "o", &.{&.{try indexOf(&paths, track1)}});
var out: std.Io.Writer.Allocating = .init(a); const w = &out.writer; try w.writeAll("// Generated by tools/fuzz_corpus.zig; see there.\n\n"); inline for (.{ .{ "calls", &calls }, .{ "refusals", &refusals } }) |list| { try w.print("pub const {s} = [_][]const u8{{\n", .{list[0]}); for (list[1].items) |item| { try w.writeAll(" \""); for (item) |byte| try w.print("\\x{x:0>2}", .{byte}); try w.writeAll("\",\n"); } try w.writeAll("};\n\n"); } inline for (.{ .{ "paths", &paths }, .{ "interfaces", &interfaces }, .{ "members", &members }, .{ "signatures", &signatures } }) |list| { try w.print("pub const {s} = [_][]const u8{{\n", .{list[0]}); for (list[1]) |name| try w.print(" \"{s}\",\n", .{name}); try w.writeAll("};\n\n"); } try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = out_path, .data = out.written() });}
const Calls = struct { a: std.mem.Allocator, list: *std.ArrayList([]const u8),
/// A string value: a byte of length and its bytes. fn str(c: Calls, value: []const u8) []const u8 { const out = c.a.alloc(u8, 1 + value.len) catch @panic("out of memory"); out[0] = @intCast(value.len); @memcpy(out[1..], value); return out; }
/// A number's bytes, little-endian. fn int(c: Calls, comptime T: type, value: T) []const u8 { const out = c.a.alloc(u8, @sizeOf(T)) catch @panic("out of memory"); std.mem.writeInt(T, out[0..@sizeOf(T)], value, .little); return out; }
/// A method call from a peer, made to a player in the state `add` /// picks, which does not change after it. fn add(c: Calls, p: []const u8, interface: ?[]const u8, member: []const u8, signature: []const u8, values: []const []const u8) !void { // Playing the first track, a third of the way in, seekable, with a // track list and playlists -- and every so often something else. const n = c.list.items.len; const flags: u8 = if (n % 5 == 4) 0b0000_1001 else 0b1110_0100; return c.addAt(.{ flags, 85, flags, 85 }, p, interface, member, signature, values); }
/// A method call from a peer, as the target takes it: the two bytes of /// the player's state before it and the two after, then path, /// interface, member and signature, each by its index in the lists /// above, then the values of its arguments, in the target's order. fn addAt(c: Calls, states: [4]u8, p: []const u8, interface: ?[]const u8, member: []const u8, signature: []const u8, values: []const []const u8) !void { var out: std.ArrayList(u8) = .empty; try out.appendSlice(c.a, &states); try out.append(c.a, try indexOf(&paths, p)); try out.append(c.a, try indexOf(&interfaces, interface orelse "")); try out.append(c.a, try indexOf(&members, member)); try out.append(c.a, try indexOf(&signatures, signature)); for (values) |v| try out.appendSlice(c.a, v); try c.list.append(c.a, out.items); }};
fn indexOf(list: []const []const u8, name: []const u8) !u8 { for (list, 0..) |n, i| if (std.mem.eql(u8, n, name)) return @intCast(i); return error.NotInTheList;}