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

Crash on GetElementType #221

Open
0x410c opened this issue Feb 6, 2024 · 3 comments
Open

Crash on GetElementType #221

0x410c opened this issue Feb 6, 2024 · 3 comments

Comments

@0x410c
Copy link

0x410c commented Feb 6, 2024

LLVMContextRef context = LLVMContextRef.Create();
string ir = File.ReadAllText("helper_module.ir");
nint mem = Marshal.StringToHGlobalAnsi(ir);
var memBuf = LLVM.CreateMemoryBufferWithMemoryRangeCopy((sbyte*)mem, (nuint)ir.Length, (sbyte*)Marshal.StringToHGlobalAnsi("helper_module"));
context.TryParseIR(memBuf,out _helperModule,out string msg);
 _helperModule.TryVerify(LLVMVerifierFailureAction.LLVMPrintMessageAction, out string message);
 var opcodeNewarr = LLVM.GetNamedFunction(_helperModule, (sbyte*)Marshal.StringToHGlobalAnsi("a"));
 var to1 = LLVM.TypeOf(opcodeNewarr);
var to1e = LLVM.GetElementType(to1);

ir:

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define i32 @a(){
  ret i32 1
}

i am trying to get return type of the function, but getting on var to1e = LLVM.GetElementType(to1);

System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

i am using version 16 of llvmsharp

@davidelettieri
Copy link

Not an expert but I investigated a bit your issue and I think it may be related with opaque pointers (https://releases.llvm.org/16.0.0/docs/OpaquePointers.html). Even if it's not, the migration instructions contains some suggestions that can be applied to your case.

The type your are getting in the line

var to1 = LLVM.TypeOf(opcodeNewarr);

is a PointerType, you can check this using LLVM.GetTypeKind() function. From the linked docs we know that GetElementType is not supported anymore

In order to support opaque pointers, two types of changes tend to be necessary. The first is the removal of all calls to PointerType::getElementType() and Type::getPointerElementType().

Since the function defined in the ir is a global function and there is a getValueType() function for globals we can try to use that one. I think getValueType() on llvm can be translated to LLVM.GlobalGetValueType() in LLVMSharp

LLVMContextRef context = LLVMContextRef.Create();
LLVMModuleRef _helperModule;
string ir = File.ReadAllText("helper_module.ir");
nint mem = Marshal.StringToHGlobalAnsi(ir);
var memBuf = LLVM.CreateMemoryBufferWithMemoryRangeCopy((sbyte*)mem, (nuint)ir.Length, (sbyte*)Marshal.StringToHGlobalAnsi("helper_module"));
context.TryParseIR(memBuf, out _helperModule, out string msg);
_helperModule.TryVerify(LLVMVerifierFailureAction.LLVMPrintMessageAction, out string message);
var func = LLVM.GetNamedFunction(_helperModule, (sbyte*)Marshal.StringToHGlobalAnsi("a"));
var funcType = LLVM.GlobalGetValueType(func);
var returnType = LLVM.GetReturnType(funcType);

var funcTypeKind = LLVM.GetTypeKind(funcType);
var returnTypeKind = LLVM.GetTypeKind(returnType);

This code is not failing anymore.

@0x410c
Copy link
Author

0x410c commented Jun 24, 2024

wont be able to confirm, dropped llvm usage in the project 😅

@davidelettieri
Copy link

It's fine, it was an exercise for me to check the issue. Hopefully someone else might find it useful.

Should the issue be closed?

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

No branches or pull requests

2 participants