Skip to content

unsafe::reinterpret_cast can cause valgrind errors #1175

Closed
@jdm

Description

@jdm

Sorry for the vague summary, but I've reduced this testcase about as far as I can.

import std::{str, option, unsafe};
use std;

type metadata<T> = {node: int, data: @T};
type compile_unit_md = {path: str};
type metadata_cache = [@debug_metadata];

tag debug_metadata {
    compile_unit_metadata(@metadata<compile_unit_md>);
}

fn get_compile_unit_metadata(cache: metadata_cache, full_path: str)
    -> @metadata<compile_unit_md> unsafe {
    ret alt *cache[0] {
      compile_unit_metadata(md) { unsafe::reinterpret_cast(md) }
    };
}

fn main() {
    let cache = [@compile_unit_metadata(@{node: 1, data: @{path: "test/a"}})];
    get_compile_unit_metadata(cache, "test/a/b");
}

When running the above, I get output like:

godot:build jdm$ ~/src/rustbug
rustbug(24826,0xb0305000) malloc: *** error for object 0x11001a0: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug

and valgrind has lots of entries for glue_drop15:

==24813== Invalid read of size 4
==24813==    at 0x30A6: glue_drop15 (in /Users/jdm/src/rustbug)
==24813==    by 0x232A: main::_e820af82886e4814 (in /Users/jdm/src/rustbug)
==24813==    by 0x259A: _rust_main (in /Users/jdm/src/rustbug)
==24813==    by 0x34E717: task_start_wrapper (in /Users/jdm/src/rust/build/stage2/lib/rustc/i686-apple-darwin/lib/librustrt.dylib)
==24813==    by 0xDEADBEEE: ???
==24813==  Address 0x1c08d60 is 0 bytes inside a block of size 12 free'd
==24813==    at 0x1243A: free (vg_replace_malloc.c:430)
==24813==    by 0x351B79: upcall_free (in /Users/jdm/src/rust/build/stage2/lib/rustc/i686-apple-darwin/lib/librustrt.dylib)
==24813==    by 0x3165: glue_free16 (in /Users/jdm/src/rustbug)
==24813==    by 0x30E3: glue_drop15 (in /Users/jdm/src/rustbug)
==24813==    by 0x2201: main::_e820af82886e4814 (in /Users/jdm/src/rustbug)
==24813==    by 0x259A: _rust_main (in /Users/jdm/src/rustbug)
==24813==    by 0x34E717: task_start_wrapper (in /Users/jdm/src/rust/build/stage2/lib/rustc/i686-apple-darwin/lib/librustrt.dylib)
==24813==    by 0xDEADBEEE: ???

If I assign the result of get_compile_metadata to a local var valgrind still complains, but the malloc error doesn't seem to show up any more.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions