-
Notifications
You must be signed in to change notification settings - Fork 27
/
fuzz_skrifa_outline.rs
164 lines (145 loc) · 4.5 KB
/
fuzz_skrifa_outline.rs
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
#![no_main]
use std::{error::Error, fmt::Display};
use libfuzzer_sys::{
arbitrary::{self, Arbitrary, Unstructured},
fuzz_target,
};
use skrifa::{
instance::Size,
outline::{DrawError, DrawSettings, HintingInstance, HintingMode, LcdLayout, OutlinePen},
raw::tables::glyf::ToPathStyle,
FontRef, MetadataProvider,
};
/// The pen for when you don't really care what gets drawn
struct NopPen;
impl OutlinePen for NopPen {
fn move_to(&mut self, _x: f32, _y: f32) {
// nop
}
fn line_to(&mut self, _x: f32, _y: f32) {
// nop
}
fn quad_to(&mut self, _cx0: f32, _cy0: f32, _x: f32, _y: f32) {
// nop
}
fn curve_to(&mut self, _cx0: f32, _cy0: f32, _cx1: f32, _cy1: f32, _x: f32, _y: f32) {
// nop
}
fn close(&mut self) {
// nop
}
}
/// Each entry represents a set of options for [HintingMode]
///
/// Exists to fulfill [Arbitrary]
#[derive(Arbitrary, Debug)]
enum FuzzerHintingMode {
Strong,
Smooth {
vertical_lcd: Option<bool>,
preserve_linear_metrics: bool,
},
}
impl From<FuzzerHintingMode> for HintingMode {
fn from(value: FuzzerHintingMode) -> Self {
match value {
FuzzerHintingMode::Strong => HintingMode::Strong,
FuzzerHintingMode::Smooth {
vertical_lcd,
preserve_linear_metrics,
} => HintingMode::Smooth {
lcd_subpixel: match vertical_lcd {
None => None,
Some(true) => Some(LcdLayout::Vertical),
Some(false) => Some(LcdLayout::Horizontal),
},
preserve_linear_metrics,
},
}
}
}
/// Drawing glyph outlines is fun and flexible! Try to test lots of options.
///
/// See
/// * <https://rust-fuzz.github.io/book/cargo-fuzz/structure-aware-fuzzing.html>
/// * <https://docs.rs/skrifa/latest/skrifa/outline/index.html>
#[derive(Arbitrary, Debug)]
struct OutlineRequest {
/// None => unscaled
size: Option<f32>,
axis_positions: Vec<f32>,
hinted: bool,
hinted_pedantic: bool,
hinting_mode: FuzzerHintingMode,
with_memory: bool, // ~half tests should be with memory, half not
memory_size: u16, // if we do test with_memory, how much of it? u16 to avoid asking for huge chunks.
harfbuzz_pathstyle: bool,
}
#[derive(Debug)]
struct DrawErrorWrapper(DrawError);
impl Error for DrawErrorWrapper {}
impl Display for DrawErrorWrapper {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
fn do_glyf_things(outline_request: OutlineRequest, data: &[u8]) -> Result<(), Box<dyn Error>> {
let font = FontRef::new(data)?;
let outlines = font.outline_glyphs();
let size = outline_request
.size
.map(Size::new)
.unwrap_or_else(Size::unscaled);
let raw_location = font
.axes()
.iter()
.zip(&outline_request.axis_positions)
.map(|(axis, pos)| (axis.tag(), *pos))
.collect::<Vec<_>>();
let location = font.axes().location(raw_location);
let mut buf: Vec<u8> = Vec::with_capacity(
outline_request
.with_memory
.then_some(outline_request.memory_size)
.unwrap_or_default() as usize,
);
let hinting_instance = if outline_request.hinted {
Some(
HintingInstance::new(
&outlines,
size,
&location,
outline_request.hinting_mode.into(),
)
.map_err(DrawErrorWrapper)?,
)
} else {
None
};
for (_codepoint, gid) in font.charmap().mappings() {
let Some(glyph) = outlines.get(gid) else {
continue;
};
let mut settings = if let Some(instance) = &hinting_instance {
DrawSettings::hinted(instance, outline_request.hinted_pedantic)
} else {
DrawSettings::unhinted(size, &location)
};
if outline_request.with_memory {
settings = settings.with_memory(Some(&mut buf));
}
if outline_request.harfbuzz_pathstyle {
settings = settings.with_path_style(ToPathStyle::HarfBuzz);
}
let _ = glyph.draw(settings, &mut NopPen {});
}
Ok(())
}
fuzz_target!(|data: &[u8]| {
let mut unstructured = Unstructured::new(data);
let Ok(outline_request) = unstructured.arbitrary() else {
return;
};
let data = unstructured.take_rest();
let _ = do_glyf_things(outline_request, data);
});