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

Inkwell causing a crash when metadata is encountered as the type of a function argument #546

Open
iamrecursion opened this issue Oct 11, 2024 · 1 comment

Comments

@iamrecursion
Copy link

Describe the Bug
As part of a project that ingests LLVM IR I am probing function definitions (and calls) for the types of their arguments. As my ingested IR comes from Rust (for now), it is not uncommon for me to see calls to @llvm.dbg.declare and the accompanying declaration.

declare void @llvm.dbg.declare(metadata, metadata, metadata)

Given the func: FunctionValue corresponding to this declaration, calling func.get_type().get_param_types() will cause a crash in BasicTypeEnum::new with the message "Unsupported basic type: Metadata".

To Reproduce

In an environment with the below things available, run test2::run.

lib.rs:

#[cfg(test)]
mod test2 {
    use std::path::Path;

    use inkwell::{context::Context, memory_buffer::MemoryBuffer};

    #[test]
    fn run() {
        let test_input = r"add_min.ll";
        let source_buf = MemoryBuffer::create_from_file(Path::new(test_input)).unwrap();

        let mut ctx = Context::create();
        let module = ctx.create_module_from_ir(source_buf).unwrap();

        for function in module.get_functions() {
            let fun_ty = function.get_type();

            // This crashes
            fun_ty.get_param_types();
        }
    }
}

add_min.ll:

declare void @llvm.dbg.declare(metadata, metadata, metadata) #1

Expected Behavior

I would have initially expected for metadata to be a supported type, but the fact that it isn't indicates that there is likely quite some complexity to making it so. For my use case, it would be sufficient to know that the type was metadata and avoid the crash.

Ideally I would like to avoid just banning known-problematic functions from my analysis, but that is a stopgap I am considering for the moment.

LLVM Version (please complete the following information):

  • LLVM Version: 18.0
  • Inkwell Branch Used: 0.5.0 with the llvm18-0 feature

Desktop (please complete the following information):

  • OS: macOS Sequoia 15.0.1

Additional Context

If there is a better way to detect usages of metadata in type position that doesn't eventually result in a crashing call to BasicTypeEnum::new, I would be happy to hear about it.

@TheDan64
Copy link
Owner

TheDan64 commented Nov 11, 2024

We already have a BasicMetadataValueEnum type, we probably want get_param_types to return that instead. Would be happy to review a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants