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

Fix string marshaling crash. #91

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Tests/Bindings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace LLVMSharp.Tests
{
using NUnit.Framework;
using System;

public class Bindings
{
[Test]
public void StringMarshaling()
{
var mod = LLVM.ModuleCreateWithName("string_marshaling_test");

var param_types = new LLVMTypeRef[0];
var func_type = LLVM.FunctionType(LLVM.Int32Type(), param_types, false);
var main = LLVM.AddFunction(mod, "main", func_type);

const string BasicBlockName = "entry";
var entry = LLVM.AppendBasicBlock(main, BasicBlockName);

var builder = LLVM.CreateBuilder();
LLVM.PositionBuilderAtEnd(builder, entry);
var ret = LLVM.BuildRet(builder, LLVM.ConstInt(LLVM.Int32Type(), 0ul, new LLVMBool(false)));

if (LLVM.VerifyModule(mod, LLVMVerifierFailureAction.LLVMPrintMessageAction, out var error) != new LLVMBool(true))
{
Console.WriteLine($"Error: {error}");
}

var basicBlockName = LLVM.GetBasicBlockName(entry);
Assert.AreEqual(BasicBlockName, basicBlockName);

var valueName = LLVM.GetValueName(ret);
Assert.IsEmpty(valueName);

var layout = LLVM.GetDataLayout(mod);
Assert.IsEmpty(layout);

var moduleTarget = LLVM.GetTarget(mod);
Assert.IsEmpty(moduleTarget);

LLVM.InitializeX86TargetInfo();

var targetName = LLVM.GetTargetName(LLVM.GetFirstTarget());
Assert.IsTrue(targetName.Contains("x86"));

var targetDescription = LLVM.GetTargetDescription(LLVM.GetFirstTarget());
Assert.IsNotEmpty(targetDescription);

LLVM.DisposeBuilder(builder);
}
}
}
18 changes: 0 additions & 18 deletions src/Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,15 +1150,9 @@ public static partial class LLVM
[DllImport(libraryPath, EntryPoint = "LLVMGetDataLayoutStr", CallingConvention = CallingConvention.Cdecl)]
public static extern string GetDataLayoutStr(LLVMModuleRef @M);

[DllImport(libraryPath, EntryPoint = "LLVMGetDataLayout", CallingConvention = CallingConvention.Cdecl)]
public static extern string GetDataLayout(LLVMModuleRef @M);

[DllImport(libraryPath, EntryPoint = "LLVMSetDataLayout", CallingConvention = CallingConvention.Cdecl)]
public static extern void SetDataLayout(LLVMModuleRef @M, [MarshalAs(UnmanagedType.LPStr)] string @DataLayoutStr);

[DllImport(libraryPath, EntryPoint = "LLVMGetTarget", CallingConvention = CallingConvention.Cdecl)]
public static extern string GetTarget(LLVMModuleRef @M);

[DllImport(libraryPath, EntryPoint = "LLVMSetTarget", CallingConvention = CallingConvention.Cdecl)]
public static extern void SetTarget(LLVMModuleRef @M, [MarshalAs(UnmanagedType.LPStr)] string @Triple);

Expand Down Expand Up @@ -1405,9 +1399,6 @@ public static partial class LLVM
[DllImport(libraryPath, EntryPoint = "LLVMGetValueKind", CallingConvention = CallingConvention.Cdecl)]
public static extern LLVMValueKind GetValueKind(LLVMValueRef @Val);

[DllImport(libraryPath, EntryPoint = "LLVMGetValueName", CallingConvention = CallingConvention.Cdecl)]
public static extern string GetValueName(LLVMValueRef @Val);

[DllImport(libraryPath, EntryPoint = "LLVMSetValueName", CallingConvention = CallingConvention.Cdecl)]
public static extern void SetValueName(LLVMValueRef @Val, [MarshalAs(UnmanagedType.LPStr)] string @Name);

Expand Down Expand Up @@ -2173,9 +2164,6 @@ public static partial class LLVM
[DllImport(libraryPath, EntryPoint = "LLVMValueAsBasicBlock", CallingConvention = CallingConvention.Cdecl)]
public static extern LLVMBasicBlockRef ValueAsBasicBlock(LLVMValueRef @Val);

[DllImport(libraryPath, EntryPoint = "LLVMGetBasicBlockName", CallingConvention = CallingConvention.Cdecl)]
public static extern string GetBasicBlockName(LLVMBasicBlockRef @BB);

[DllImport(libraryPath, EntryPoint = "LLVMGetBasicBlockParent", CallingConvention = CallingConvention.Cdecl)]
public static extern LLVMValueRef GetBasicBlockParent(LLVMBasicBlockRef @BB);

Expand Down Expand Up @@ -3163,12 +3151,6 @@ public static partial class LLVM
[DllImport(libraryPath, EntryPoint = "LLVMGetTargetFromTriple", CallingConvention = CallingConvention.Cdecl)]
internal static extern LLVMBool GetTargetFromTriple([MarshalAs(UnmanagedType.LPStr)] string @Triple, out LLVMTargetRef @T, out IntPtr @ErrorMessage);

[DllImport(libraryPath, EntryPoint = "LLVMGetTargetName", CallingConvention = CallingConvention.Cdecl)]
public static extern string GetTargetName(LLVMTargetRef @T);

[DllImport(libraryPath, EntryPoint = "LLVMGetTargetDescription", CallingConvention = CallingConvention.Cdecl)]
public static extern string GetTargetDescription(LLVMTargetRef @T);

[DllImport(libraryPath, EntryPoint = "LLVMTargetHasJIT", CallingConvention = CallingConvention.Cdecl)]
public static extern LLVMBool TargetHasJIT(LLVMTargetRef @T);

Expand Down
15 changes: 12 additions & 3 deletions src/Overloads.Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,26 @@ partial class LLVM
{
[DllImport(libraryPath, EntryPoint = "LLVMGetValueName", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetValueNameAsPtr(LLVMValueRef @Val);

[DllImport(libraryPath, EntryPoint = "LLVMGetTarget", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetTargetAsPtr(LLVMModuleRef M);
public static string GetValueName(LLVMValueRef @Val) => Marshal.PtrToStringAnsi(GetValueNameAsPtr(@Val));

[DllImport(libraryPath, EntryPoint = "LLVMGetDataLayout", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetDataLayoutAsPtr(LLVMModuleRef M);
public static string GetDataLayout(LLVMModuleRef M) => Marshal.PtrToStringAnsi(GetDataLayoutAsPtr(M));

[DllImport(libraryPath, EntryPoint = "LLVMGetTarget", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetTargetAsPtr(LLVMModuleRef M);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a new addition not previously there? I would keep this private then.

public static string GetTarget(LLVMModuleRef M) => Marshal.PtrToStringAnsi(GetTargetAsPtr(M));

[DllImport(libraryPath, EntryPoint = "LLVMGetTargetName", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetTargetNameAsPtr(LLVMTargetRef @T);
public static string GetTargetName(LLVMTargetRef @T) => Marshal.PtrToStringAnsi(GetTargetNameAsPtr(@T));

[DllImport(libraryPath, EntryPoint = "LLVMGetTargetDescription", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetTargetDescriptionAsPtr(LLVMTargetRef @T);
public static string GetTargetDescription(LLVMTargetRef @T) => Marshal.PtrToStringAnsi(GetTargetDescriptionAsPtr(@T));

[DllImport(libraryPath, EntryPoint = "LLVMGetBasicBlockName", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetBasicBlockNameAsPtr(LLVMBasicBlockRef @BB);
public static string GetBasicBlockName(LLVMBasicBlockRef @BB) => Marshal.PtrToStringAnsi(GetBasicBlockNameAsPtr(@BB));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I keep having to refresh my memory but is there no deallocation requirement?

}
}