Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] unsafe preconditions violated #573

Open
HyperCodec opened this issue Apr 16, 2024 · 11 comments
Open

[BUG] unsafe preconditions violated #573

HyperCodec opened this issue Apr 16, 2024 · 11 comments
Labels
bug Something isn't working

Comments

@HyperCodec
Copy link

HyperCodec commented Apr 16, 2024

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 HyperCodec added the bug Something isn't working label Apr 16, 2024
@petertrotman
Copy link

Related:

rust-lang/rust#123285
alacritty/alacritty#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`

@jayvdb
Copy link
Contributor

jayvdb commented May 3, 2024

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

@jayvdb
Copy link
Contributor

jayvdb commented May 3, 2024

I think the bug is fixed on master

@elpiel
Copy link
Contributor

elpiel commented May 3, 2024

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

image

@jayvdb
Copy link
Contributor

jayvdb commented May 4, 2024

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 #576

@elpiel
Copy link
Contributor

elpiel commented May 4, 2024

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 #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 😅

@AJMC2002
Copy link

AJMC2002 commented May 6, 2024

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.

@tdameros
Copy link

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.

@HyperCodec
Copy link
Author

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.

@dkrot
Copy link

dkrot commented May 17, 2024

Looks like this bug was fixed in font-kit a year ago, servo/font-kit@787b68f
Not sure why plotters still depends on outdated font-kit version

@jayvdb
Copy link
Contributor

jayvdb commented May 17, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants