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

Segfaults when builder is called without setting its position first #425

Closed
jdupak opened this issue Jun 19, 2023 · 5 comments
Closed

Segfaults when builder is called without setting its position first #425

jdupak opened this issue Jun 19, 2023 · 5 comments
Assignees
Labels
Milestone

Comments

@jdupak
Copy link

jdupak commented Jun 19, 2023

Describe the Bug
Program compiles successfully but segfaults, if builder position is not set to some BB.

To Reproduce

        let main = module.add_function("main", ctx.i64_type().fn_type(&[], false), None);
        let entry = ctx.append_basic_block(main, "entry");
        // builder.position_at_end(entry); without this is segfaults
        let exit_code = builder.build_alloca(ctx.i64_type(), "exit_code");
        let exit_bb = ctx.append_basic_block(main, "exit");

Expected Behavior
This should at least produce a "controled" runtime error.

LLVM Version (please complete the following information):

  • LLVM Version: 11, 14
  • Inkwell Branch Used: crates.io release 0.2.0

Desktop (please complete the following information):

  • OS: Manjaro
@TheDan64
Copy link
Owner

I wanna say this is a known issue but I can't find a ticket for it lol.

Not sure the LLVM api has a safe way for us to verify if a position has been set

@TheDan64 TheDan64 added this to the 0.3.0 milestone Aug 14, 2023
@TheDan64 TheDan64 added the bug label Aug 14, 2023
@marcospb19
Copy link

marcospb19 commented Aug 25, 2023

I was about to open an issue but found this one.

Minimal reprod:

fn main() {
    let context = inkwell::context::Context::create();
    let builder = context.create_builder();

    // [1] 70215 segmentation fault (core dumped)
    builder.build_alloca(context.i64_type(), &"n");
}
inkwell = { version = "0.2.0", features = ["llvm15-0"] }

@EricLBuehler
Copy link
Contributor

if builder position is not set to some BB.

Perhaps Inkwell can store a state of BB, so that it has a runtime error? Or better yet, a type-state of some sort?

@TheDan64
Copy link
Owner

Yeah, I've thought about using type state for this before but we'd have to figure out which methods are valid for each state and there are a lot of builder methods

@EricLBuehler
Copy link
Contributor

I agree, it would be complicated to implement type state.

To implement type state based safety, perhaps a builer pattern can be implemented for the Builder struct. With this idea, the only way a "buildable" Builder can be created is by calling one of the position_* methods on a "non-buildable" Builder? This would return a "buildable" Builder and would essentially be a build method for the builder pattern.

However, as someone who uses Inkwell in my own projects, I think this would be cumbersome from an API useability point of view. For example, a Builder<State=NotSet> and then an Option<Builder<State=Set>> would need to be kept in order to use type state safety. In addition, backwards compatibility would be an issue. However, the major improvement this would bring is that no segfaults are caused by a simple mistake.

For those reasons, I am think runtime assertions to ensure safety by adding state to the Builder struct would be better.

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

No branches or pull requests

4 participants