-
Notifications
You must be signed in to change notification settings - Fork 99
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.