[llvm-dev] Proper method to initialize all LLVM Internal Data Structures?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[llvm-dev] Proper method to initialize all LLVM Internal Data Structures?

Tim Northover via llvm-dev
Hi:
I'm building a small tool on top of LLVM Core Library.

```
LLVMContext context;
SMDiagnostic diag;
Module *M = parseIRFile(InputIR, diag, context).get();
  if (M == nullptr) {
    diag.print("LLVM", errs());
    exit(-1);
  }
  assert(M->isMaterialized() && "Module not materialized!");
  PointerType *ArrayPtrTy =
      M->getTypeByName("struct._Array")->getPointerTo();
```

However this piece of code crashes at Module::getTypeByName because getContext().pImpl is NULL pointer. Other similar issues include Module::getTypeByName results in a NULL pointer dereference because TheTable in StringMap is not allocated/initialized.

It would be great if someone could show me what's the correct way to initialize all this kind of internal data structures so I could focus on my tool's logic


_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Proper method to initialize all LLVM Internal Data Structures?

Tim Northover via llvm-dev
I’m using LLVM6.0 Release version on macOS

Zhang

> 在 2018年6月12日,12:59,Zhang via llvm-dev <[hidden email]> 写道:
>
> Hi:
> I'm building a small tool on top of LLVM Core Library.
>
> ```
> LLVMContext context;
> SMDiagnostic diag;
> Module *M = parseIRFile(InputIR, diag, context).get();
>   if (M == nullptr) {
>     diag.print("LLVM", errs());
>     exit(-1);
>   }
>   assert(M->isMaterialized() && "Module not materialized!");
>   PointerType *ArrayPtrTy =
>       M->getTypeByName("struct._Array")->getPointerTo();
> ```
>
> However this piece of code crashes at Module::getTypeByName because getContext().pImpl is NULL pointer. Other similar issues include Module::getTypeByName results in a NULL pointer dereference because TheTable in StringMap is not allocated/initialized.
>
> It would be great if someone could show me what's the correct way to initialize all this kind of internal data structures so I could focus on my tool's logic
>
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev



_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Proper method to initialize all LLVM Internal Data Structures?

Tim Northover via llvm-dev
This line looks incorrect

Module *M = parseIRFile(InputIR, diag, context).get();

parseIRFile returns a unique_ptr, you called "get()" on it and captured the value of the ptr, but the temporary unique_ptr was not invalidated. So its destructor still ran and deleted the module out from under you. You should use std::unique_ptr<Module> M to maintain ownership.

~Craig


On Mon, Jun 11, 2018 at 10:04 PM mayuyu.io via llvm-dev <[hidden email]> wrote:
I’m using LLVM6.0 Release version on macOS

Zhang

> 在 2018年6月12日,12:59,Zhang via llvm-dev <[hidden email]> 写道:
>
> Hi:
> I'm building a small tool on top of LLVM Core Library.
>
> ```
> LLVMContext context;
> SMDiagnostic diag;
> Module *M = parseIRFile(InputIR, diag, context).get();
>   if (M == nullptr) {
>     diag.print("LLVM", errs());
>     exit(-1);
>   }
>   assert(M->isMaterialized() && "Module not materialized!");
>   PointerType *ArrayPtrTy =
>       M->getTypeByName("struct._Array")->getPointerTo();
> ```
>
> However this piece of code crashes at Module::getTypeByName because getContext().pImpl is NULL pointer. Other similar issues include Module::getTypeByName results in a NULL pointer dereference because TheTable in StringMap is not allocated/initialized.
>
> It would be great if someone could show me what's the correct way to initialize all this kind of internal data structures so I could focus on my tool's logic
>
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev



_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev