plotters icon indicating copy to clipboard operation
plotters copied to clipboard

[BUG] unsafe preconditions violated

Open HyperCodec opened this issue 1 year ago • 12 comments

Describe the bug I start to get this error on build_cartesian_2d:

thread 'main' panicked at library/core/src/panicking.rs:215:5:
unsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`
stack backtrace:
   0: rust_begin_unwind
             at /rustc/0d8b3346a3992ab11ea35ff0fb95a6864b91f797/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_nounwind_fmt::runtime
             at /rustc/0d8b3346a3992ab11ea35ff0fb95a6864b91f797/library/core/src/panicking.rs:110:18
   2: core::panicking::panic_nounwind_fmt
             at /rustc/0d8b3346a3992ab11ea35ff0fb95a6864b91f797/library/core/src/panicking.rs:120:5
   3: core::panicking::panic_nounwind
             at /rustc/0d8b3346a3992ab11ea35ff0fb95a6864b91f797/library/core/src/panicking.rs:215:5
   4: core::slice::raw::from_raw_parts::precondition_check
             at /rustc/0d8b3346a3992ab11ea35ff0fb95a6864b91f797/library/core/src/ub_checks.rs:66:21
   5: core::slice::raw::from_raw_parts
             at /rustc/0d8b3346a3992ab11ea35ff0fb95a6864b91f797/library/core/src/slice/raw.rs:96:9
   6: font_kit::loaders::freetype::Font::rasterize_glyph
             at /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/font-kit-0.11.0/src/loaders/freetype.rs:847:26
   7: <plotters::style::font::ttf::FontDataInternal as plotters::style::font::FontData>::draw
             at /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/plotters-0.3.5/src/style/font/ttf.rs:265:26
   8: plotters::style::font::font_desc::FontDesc::draw
             at /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/plotters-0.3.5/src/style/font/font_desc.rs:168:29
   9: <plotters::style::text::TextStyle as plotters_backend::text::BackendTextStyle>::draw
             at /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/plotters-0.3.5/src/style/text.rs:322:9
  10: plotters_backend::DrawingBackend::draw_text
             at /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/plotters-backend-0.3.5/src/lib.rs:250:15
  11: plotters::drawing::area::DrawingArea<DB,plotters::coord::Shift>::titled::{{closure}}
             at /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/plotters-0.3.5/src/drawing/area.rs:496:13
  12: plotters::drawing::area::DrawingArea<DB,CT>::backend_ops
             at /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/plotters-0.3.5/src/drawing/area.rs:282:13
  13: plotters::drawing::area::DrawingArea<DB,plotters::coord::Shift>::titled
             at /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/plotters-0.3.5/src/drawing/area.rs:495:9
  14: plotters::chart::builder::ChartBuilder<DB>::build_cartesian_2d
             at /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/plotters-0.3.5/src/chart/builder.rs:332:28
  15: plot::main
             at ./examples/plot.rs:93:21
  16: core::ops::function::FnOnce::call_once
             at /rustc/0d8b3346a3992ab11ea35ff0fb95a6864b91f797/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread caused non-unwinding panic. aborting.
Aborted (core dumped)

To Reproduce Here's everything I have that is related to this crate:

let root = BitMapBackend::new(OUTPUT_FILE_NAME, (640, 480)).into_drawing_area();
root.fill(&WHITE)?;

let mut chart = ChartBuilder::on(&root)
      .caption("agent fitness values per generation", ("sans-serif", 50).into_font())
      .margin(5)
      .x_label_area_size(30)
      .y_label_area_size(30)
      .build_cartesian_2d(0usize..100, 0f32..200.0)?;

chart.configure_mesh().draw()?;

let data: Vec<_> = Arc::into_inner(performance_stats).unwrap().into_inner().unwrap()
      .into_iter()
      .enumerate()
      .collect();
let highs = data
      .iter()
      .map(|(i, PerformanceStats { high, .. })| (*i, *high));

let medians = data
      .iter()
      .map(|(i, PerformanceStats { median, .. })| (*i, *median));

let lows = data
     .iter()
     .map(|(i, PerformanceStats { low, .. })| (*i, *low));

chart
    .draw_series(LineSeries::new(highs, &GREEN))?
    .label("high");

chart
    .draw_series(LineSeries::new(medians, &YELLOW))?
    .label("median");

chart
    .draw_series(LineSeries::new(lows, &RED))?
    .label("low");

root.present()?;

Version Information plotters = "0.3.5"

HyperCodec avatar Apr 16 '24 18:04 HyperCodec

Related:

https://github.com/rust-lang/rust/issues/123285 https://github.com/alacritty/alacritty/issues/7736

This is triggered by caption. My traceback for the default 3d example https://plotters-rs.github.io/book/basic/draw_3d_plots.html

unsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`
stack backtrace:
   0: rust_begin_unwind
             at /rustc/a07f3eb43acc5df851e15176c7081a900a30a4d7/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_nounwind_fmt::runtime
             at /rustc/a07f3eb43acc5df851e15176c7081a900a30a4d7/library/core/src/panicking.rs:110:18
   2: core::panicking::panic_nounwind_fmt
             at /rustc/a07f3eb43acc5df851e15176c7081a900a30a4d7/library/core/src/panicking.rs:120:5
   3: core::panicking::panic_nounwind
             at /rustc/a07f3eb43acc5df851e15176c7081a900a30a4d7/library/core/src/panicking.rs:215:5
   4: core::slice::raw::from_raw_parts::precondition_check
             at /rustc/a07f3eb43acc5df851e15176c7081a900a30a4d7/library/core/src/ub_checks.rs:66:21
   5: core::slice::raw::from_raw_parts
             at /rustc/a07f3eb43acc5df851e15176c7081a900a30a4d7/library/core/src/slice/raw.rs:96:9
   6: font_kit::loaders::freetype::Font::rasterize_glyph
             at /home/peter/.cargo/registry/src/index.crates.io-6f17d22bba15001f/font-kit-0.11.0/src/loaders/freetype.rs:847:26
   7: <plotters::style::font::ttf::FontDataInternal as plotters::style::font::FontData>::draw
             at /home/peter/.cargo/registry/src/index.crates.io-6f17d22bba15001f/plotters-0.3.5/src/style/font/ttf.rs:265:26
   8: plotters::style::font::font_desc::FontDesc::draw
             at /home/peter/.cargo/registry/src/index.crates.io-6f17d22bba15001f/plotters-0.3.5/src/style/font/font_desc.rs:168:29
   9: <plotters::style::text::TextStyle as plotters_backend::text::BackendTextStyle>::draw
             at /home/peter/.cargo/registry/src/index.crates.io-6f17d22bba15001f/plotters-0.3.5/src/style/text.rs:322:9
  10: plotters_backend::DrawingBackend::draw_text
             at /home/peter/.cargo/registry/src/index.crates.io-6f17d22bba15001f/plotters-backend-0.3.5/src/lib.rs:250:15
  11: plotters::drawing::area::DrawingArea<DB,plotters::coord::Shift>::titled::{{closure}}
             at /home/peter/.cargo/registry/src/index.crates.io-6f17d22bba15001f/plotters-0.3.5/src/drawing/area.rs:496:13
  12: plotters::drawing::area::DrawingArea<DB,CT>::backend_ops
             at /home/peter/.cargo/registry/src/index.crates.io-6f17d22bba15001f/plotters-0.3.5/src/drawing/area.rs:282:13
  13: plotters::drawing::area::DrawingArea<DB,plotters::coord::Shift>::titled
             at /home/peter/.cargo/registry/src/index.crates.io-6f17d22bba15001f/plotters-0.3.5/src/drawing/area.rs:495:9
  14: plotters::chart::builder::ChartBuilder<DB>::build_cartesian_3d
             at /home/peter/.cargo/registry/src/index.crates.io-6f17d22bba15001f/plotters-0.3.5/src/chart/builder.rs:475:28
  15: hexglobe::goldberg_polyhedron::plotting_test::can_make_plot
             at ./src/goldberg_polyhedron/plotting_test.rs:11:21
  16: hexglobe::goldberg_polyhedron::plotting_test::can_make_plot::{{closure}}
             at ./src/goldberg_polyhedron/plotting_test.rs:6:23
  17: core::ops::function::FnOnce::call_once
             at /rustc/a07f3eb43acc5df851e15176c7081a900a30a4d7/library/core/src/ops/function.rs:250:5
  18: core::ops::function::FnOnce::call_once
             at /rustc/a07f3eb43acc5df851e15176c7081a900a30a4d7/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread caused non-unwinding panic. aborting.
error: test failed, to rerun pass `--lib`

petertrotman avatar Apr 29 '24 07:04 petertrotman

It appears that the test suite doesnt cover this - running cargo test --all-features in this repo doesnt trigger it...?

jayvdb avatar May 03 '24 06:05 jayvdb

I think the bug is fixed on master

jayvdb avatar May 03 '24 06:05 jayvdb

Seems that even the fix has broken plotters. I get no spacing between the text now, when I used the latest git commit:

image

elpiel avatar May 03 '24 18:05 elpiel

Seems that even the fix has broken plotters. I get no spacing between the text now, when I used the latest git commit

I see the same thing. However there was no "fix" for this issue - it was already fixed on master, so some other change has caused this. So I've created https://github.com/plotters-rs/plotters/issues/576

jayvdb avatar May 04 '24 00:05 jayvdb

Seems that even the fix has broken plotters. I get no spacing between the text now, when I used the latest git commit

I see the same thing. However there was no "fix" for this issue - it was already fixed on master, so some other change has caused this. So I've created https://github.com/plotters-rs/plotters/issues/576

Thank you for your response. From the back traces so that it was related to fonts so my line of thought is that it could be related to some extent. Honesty, however, I haven't looked at plottes code in any way so it's a shot in the dark 😅

elpiel avatar May 04 '24 08:05 elpiel

I got the same error, seems like using spaces in captions or labels triggers that. Currently I'm using plotters 0.3.5 in Linux Mint.

AJMC2002 avatar May 06 '24 13:05 AJMC2002

I have the same problem when I use the lib under docker bookworm with rust 1.78 but the problem doesn't appear on MacOS.

While waiting for the patch, I've found an alternative which consists in using the SVGBackend and not the BitmapBackend. Apparently the bug is linked specifically to BitMapBackend.

After all, SVG renderings are of better quality.

tdameros avatar May 11 '24 09:05 tdameros

I have the same problem when I use the lib under docker bookworm with rust 1.78 but the problem doesn't appear on MacOS.

While waiting for the patch, I've found an alternative which consists in using the SVGBackend and not the BitmapBackend. Apparently the bug is linked specifically to BitMapBackend.

After all, SVG renderings are of better quality.

This workaround worked for me, will still keep the issue open though as a reference point for the larger BitmapBackend problem.

HyperCodec avatar May 15 '24 14:05 HyperCodec

Looks like this bug was fixed in font-kit a year ago, https://github.com/servo/font-kit/commit/787b68f69662e5acca6913db072bcc294c0a1e09 Not sure why plotters still depends on outdated font-kit version

dkrot avatar May 17 '24 14:05 dkrot

plotters master doesnt have this problem, and has an updated font-kit. We need a new release. See https://github.com/orgs/plotters-rs/discussions/577

jayvdb avatar May 17 '24 21:05 jayvdb

I'm getting the same error on next-release-devel (trying to use that because I'm running into #551 )

prescientmoon avatar Jun 27 '24 18:06 prescientmoon

This is fixed as of 0.3.7

cormacrelf avatar Sep 11 '24 05:09 cormacrelf

Yaay, thank you!

elpiel avatar Sep 11 '24 08:09 elpiel