Skip to content

Commit

Permalink
Guard against overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
ishitatsuyuki committed Jun 25, 2021
1 parent a8c39fa commit b967956
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
12 changes: 8 additions & 4 deletions piet-gpu/shader/kernel4.comp
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,11 @@ void main() {
break;
case Cmd_BeginClip:
for (uint k = 0; k < CHUNK; k++) {
blend_stack[clip_depth][k] = packsRGB(vec4(rgba[k]));
blend_alpha_stack[clip_depth][k] = clamp(abs(area[k]), 0.0, 1.0);
// We reject any inputs that might overflow in render_ctx.rs.
// The following is a sanity check so we don't corrupt memory should there be malformed inputs.
uint d = min(clip_depth, MAX_BLEND_STACK - 1);
blend_stack[d][k] = packsRGB(vec4(rgba[k]));
blend_alpha_stack[d][k] = clamp(abs(area[k]), 0.0, 1.0);
rgba[k] = vec4(0.0);
}
clip_depth++;
Expand All @@ -222,8 +225,9 @@ void main() {
case Cmd_EndClip:
clip_depth--;
for (uint k = 0; k < CHUNK; k++) {
mediump vec4 bg = unpacksRGB(blend_stack[clip_depth][k]);
mediump vec4 fg = rgba[k] * area[k] * blend_alpha_stack[clip_depth][k];
uint d = min(clip_depth, MAX_BLEND_STACK - 1);
mediump vec4 bg = unpacksRGB(blend_stack[d][k]);
mediump vec4 fg = rgba[k] * area[k] * blend_alpha_stack[d][k];
rgba[k] = bg * (1.0 - fg.a) + fg;
}
cmd_ref.offset += 4;
Expand Down
Binary file modified piet-gpu/shader/kernel4.spv
Binary file not shown.
Binary file modified piet-gpu/shader/kernel4_idx.spv
Binary file not shown.
2 changes: 2 additions & 0 deletions piet-gpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const WIDTH_IN_TILES: usize = 128;
const HEIGHT_IN_TILES: usize = 96;
const PTCL_INITIAL_ALLOC: usize = 1024;

const MAX_BLEND_STACK: usize = 128;

const N_CIRCLES: usize = 0;

pub fn render_svg(rc: &mut impl RenderContext, filename: &str, scale: f64) {
Expand Down
4 changes: 4 additions & 0 deletions piet-gpu/src/render_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use piet::{
Color, Error, FixedGradient, FontFamily, HitTestPoint, ImageFormat, InterpolationMode,
IntoBrush, LineMetric, RenderContext, StrokeStyle, Text, TextLayout, TextLayoutBuilder,
};
use crate::MAX_BLEND_STACK;

use piet_gpu_types::encoder::{Encode, Encoder};
use piet_gpu_types::scene::{
Expand Down Expand Up @@ -211,6 +212,9 @@ impl RenderContext for PietGpuRenderContext {
self.elements.push(Element::BeginClip(Clip {
bbox: Default::default(),
}));
if self.clip_stack.len() >= MAX_BLEND_STACK {
panic!("Maximum clip/blend stack size {} exceeded", MAX_BLEND_STACK);
}
self.clip_stack.push(ClipElement {
bbox: None,
begin_ix,
Expand Down

0 comments on commit b967956

Please sign in to comment.