alexcrichton / xz2-rs

Bindings to liblzma in Rust (xz streams in Rust)
Apache License 2.0
81 stars 52 forks source link

Invalid memory reference with filter on some archs #114

Open wcampbell0x2a opened 1 year ago

wcampbell0x2a commented 1 year ago

The following code:

use xz2::stream::{Filters, LzmaOptions, MtStreamBuilder};

fn main() {
    let dict_size = 0x40000;

    let mut opts = LzmaOptions::new_preset(6).unwrap();
    opts.dict_size(dict_size);

    let mut filters = Filters::new();

    filters.ia64();
    filters.arm();
    filters.arm_thumb();
    filters.lzma2(&opts);

    let stream = MtStreamBuilder::new()
        //.block_size(0x1000)
        .filters(filters)
        .check(xz2::stream::Check::Crc32)
        .encoder()
        .unwrap();
}

causes an invalid memory reference on some arches, including arm-unknown-linux-musleabi.

$ gdb ./target/x86_64-unknown-linux-musl/debug/xz-issue
GNU gdb (GDB) 13.1
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./target/x86_64-unknown-linux-musl/debug/xz-issue...
warning: Missing auto-load script at offset 0 in section .debug_gdb_scripts
of file /home/wcampbell/projects/wcampbell/xz-issue/target/x86_64-unknown-linux-musl/debug/xz-issue.
Use `info auto-load python-scripts [REGEXP]' to list them.
(gdb) r
Starting program: /home/wcampbell/projects/wcampbell/xz-issue/target/x86_64-unknown-linux-musl/debug/xz-issue

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7f8a2f3 in lzma_mt_block_size (filters=0x7ffff7f638e0) at xz-5.2/src/liblzma/common/filter_encoder.c:237
237            if (fe->block_size != NULL) {

I pushed some code here that shows the issue: https://github.com/wcampbell0x2a/xz2-issue

wcampbell0x2a commented 1 year ago

Interestingly,a master commit: https://github.com/xz-mirror/xz/commit/8f236574986e7c414c0ea059f441982d1387e6a4#diff-746760992da1ff24d47722ba47d8d7cecf76735f04c809fa39b2350bcec6ec85R263 provides a check if (fe == NULL) and says that this is invalid?

wcampbell0x2a commented 1 year ago

Fixed by adding -D HAVE_DECODER_IA64=1 -D HAVE_ENCODER_IA64=1. Yet another problem caused by this.

It would be nice if they were normal rust features so that isn't hidden.