Bug 36895 - calling cs from C
Summary: calling cs from C
Status: RESOLVED ANSWERED
Alias: None
Product: Runtime
Classification: Mono
Component: JIT ()
Version: 4.2.0 (C6)
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-12-11 16:10 UTC by Girish Joglekar
Modified: 2015-12-14 15:55 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
C code for embedding CS (1006 bytes, text/x-csrc)
2015-12-11 16:10 UTC, Girish Joglekar
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Girish Joglekar 2015-12-11 16:10:07 UTC
Created attachment 14250 [details]
C code for embedding CS

This is not a bug report but a cry for help. 
I am trying to use a C program to call a CS function which uses a vb dll (created on Windows). The CS code with the dll works fine when compiled with mcs. 
I think I have followed the steps to embed the code into C. When I compile the C code I get a few innocuous warnings

test1dtl.c:14:21: warning: initialization makes pointer from integer without a cast [enabled by default]
test1dtl.c:22:2: warning: passing argument 3 of ‘mono_runtime_invoke’ from incompatible pointer type [enabled by default]
/usr/lib/pkgconfig/../../include/mono-2.0/mono/metadata/object.h:222:1: note: expected ‘void **’ but argument is of type ‘struct MonoArray *’
test1dtl.c:24:17: warning: initialization makes pointer from integer without a cast [enabled by default]

When I run the executable created I get segmentation fault
Stacktrace:


Native stacktrace:

	/usr/lib/libmonoboehm-2.0.so.1(+0xda09f) [0x7ffb198c909f]
	/usr/lib/libmonoboehm-2.0.so.1(+0x1329b2) [0x7ffb199219b2]
	/usr/lib/libmonoboehm-2.0.so.1(+0x5295d) [0x7ffb1984195d]
	/lib/x86_64-linux-gnu/libc.so.6(+0x36150) [0x7ffb19467150]
	/usr/lib/libmonoboehm-2.0.so.1(mono_method_desc_search_in_image+0xe) [0x7ffb1994654e]
	test1dtl.x() [0x4009c3]
	/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed) [0x7ffb1945276d]
	test1dtl.x() [0x400899]

Debug info from gdb:

Could not attach to process.  If your uid matches the uid of the target
process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try
again as the root user.  For more details, see /etc/sysctl.d/10-ptrace.conf
ptrace: Operation not permitted.
No threads.

=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

Aborted (core dumped)

My C code is attached. The make command I am using is gcc -o a.x test1dtl.c `pkg-config --cflags --libs mono-2` -lm
Please let me know what I am doing wrong.
Thank you. 
Girish
Comment 1 João Matos 2015-12-14 15:55:43 UTC
Instead of embedding the Mono C runtime which is error prone you might like to take a look at CppSharp (https://github.com/mono/CppSharp), which might provide a slightly easier way to interop between your native and managed code.

If you'd still like to try embedding Mono, then we need a slightly better stack trace to figure out what's going wrong. You'll need to compile the Mono runtime with debug info, which you can do with:

CFLAGS="-O0 -g" ./autogen.sh --prefix=`pwd`/build

Then link your program with the compiled Mono library and try running it under GDB again.