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

SIGSEGV when calling assembly.Write(path) on PCL from Android #266

Closed
mfkl opened this issue Apr 7, 2016 · 6 comments
Closed

SIGSEGV when calling assembly.Write(path) on PCL from Android #266

mfkl opened this issue Apr 7, 2016 · 6 comments

Comments

@mfkl
Copy link

mfkl commented Apr 7, 2016

My code is roughly this:
var assembly = AssemblyDefinition.ReadAssembly(path);
// modify types within assembly
assembly.Write(path);

Crashes on the call to Write(path)

04-07 19:31:21.984 E/mono-rt ( 4322):
04-07 19:31:21.984 E/mono-rt ( 4322): Attempting native Android stacktrace:
04-07 19:31:21.984 E/mono-rt ( 4322):
04-07 19:31:21.986 E/mono-rt ( 4322): at mono_metadata_type_hash+298 [0xa44d833a]
04-07 19:31:21.986 E/mono-rt ( 4322): at +298 [0xa44d83b4]
04-07 19:31:21.986 E/mono-rt ( 4322): at +298 [0xa44d8416]
04-07 19:31:21.986 E/mono-rt ( 4322): at +298 [0xa44d8463]
04-07 19:31:21.986 E/mono-rt ( 4322): at +298 [0xa459c096]
04-07 19:31:21.986 E/mono-rt ( 4322): at +298 [0xa459c177]
04-07 19:31:21.986 E/mono-rt ( 4322): at +298 [0xa44dd519]
04-07 19:31:21.986 E/mono-rt ( 4322): at +298 [0xa4486300]
04-07 19:31:21.986 E/mono-rt ( 4322): at +298 [0xa4486617]
04-07 19:31:21.986 E/mono-rt ( 4322): at +298 [0xa4419385]
04-07 19:31:21.986 E/mono-rt ( 4322): at +298 [0xa441af9b]
04-07 19:31:21.987 E/mono-rt ( 4322): at +298 [0xa441b69c]
04-07 19:31:21.987 E/mono-rt ( 4322): at +298 [0xa443b3dd]
04-07 19:31:21.987 E/mono-rt ( 4322): at +298 [0xa444c446]
04-07 19:31:21.987 E/mono-rt ( 4322): at +298 [0xa4596fd4]
04-07 19:31:21.987 E/mono-rt ( 4322): at _ZL15__pthread_startPv+56 [0xb73cc7e1]
04-07 19:31:21.987 E/mono-rt ( 4322): at __start_thread+25 [0xb736d586]
04-07 19:31:21.987 E/mono-rt ( 4322): at __bionic_clone+70 [0xb7363116]
04-07 19:31:21.987 E/mono-rt ( 4322):
04-07 19:31:21.987 E/mono-rt ( 4322): =================================================================
04-07 19:31:21.987 E/mono-rt ( 4322): Got a SIGSEGV while executing native code. This usually indicates
04-07 19:31:21.987 E/mono-rt ( 4322): a fatal error in the mono runtime or one of the native libraries
04-07 19:31:21.987 E/mono-rt ( 4322): used by your application.
04-07 19:31:21.987 E/mono-rt ( 4322): =================================================================
04-07 19:31:21.987 E/mono-rt ( 4322):
04-07 19:31:21.987 F/libc ( 4322): Fatal signal 7 (SIGBUS), code 2, fault addr 0xaf2c5ad0 in tid 4339 (Thread-336)
04-07 19:31:22.101 F/DEBUG ( 68): # 15 pc 000807e1 /system/l04-07 19:31:22.089 F/DEBUG ( 68): *** *** *** *** *** *** *** *** *** *** *** *** *** *** ** ***
04-07 19:31:22.101 F/DEBUG ( 68): # 04 pc 0023d096 /data/app/Mono.Android.DebugRuntime-1/lib/x86/libmonosgen-32bit-2.0.so
04-07 19:31:22.101 F/DEBUG ( 68): # 05 pc 0023d177 /data/app/Mono.Android.DebugRuntime-1/lib/x86/libmonosgen-32bit-2.0.so
04-07 19:31:22.101 F/DEBUG ( 68): # 06 pc 0017e519 /data/app/Mono.Android.DebugRuntime-1/lib/x86/libmonosgen-32bit-2.0.so
04-07 19:31:22.101 F/DEBUG ( 68): # 07 pc 00127300 /data/app/Mono.Android.DebugRuntime-1/lib/x86/libmonosgen-32bit-2.0.so
04-07 19:31:22.101 F/DEBUG ( 68): # 08 pc 00127617 /data/app/Mono.Android.DebugRuntime-1/lib/x86/libmonosgen-32bit-2.0.so
04-07 19:31:22.101 F/DEBUG ( 68): # 09 pc 000ba385 /data/app/Mono.Android.DebugRuntime-1/lib/x86/libmonosgen-32bit-2.0.so
04-07 19:31:22.101 F/DEBUG ( 68): # 10 pc 000bbf9b /data/app/Mono.Android.DebugRuntime-1/lib/x86/libmonosgen-32bit-2.0.so
04-07 19:31:22.101 F/DEBUG ( 68): # 11 pc 000bc69c /data/app/Mono.Android.DebugRuntime-1/lib/x86/libmonosgen-32bit-2.0.so
04-07 19:31:22.101 F/DEBUG ( 68): # 12 pc 000dc3dd /data/app/Mono.Android.DebugRuntime-1/lib/x86/libmonosgen-32bit-2.0.so
04-07 19:31:22.101 F/DEBUG ( 68): # 13 pc 000ed446 /data/app/Mono.Android.DebugRuntime-1/lib/x86/libmonosgen-32bit-2.0.so
04-07 19:31:22.101 F/DEBUG ( 68): # 14 pc 00237fd4 /data/app/Mono.Android.DebugRuntime-1/lib/x86/libmonosgen-32bit-2.0.so
04-07 19:31:22.101 F/DEBUG ( 68): # 15 pc 000807e1 /system/lib/libc.so (__pthread_start(void
)+56)
04-07 19:31:22.101 F/DEBUG ( 68): # 16 pc 00021586 /system/lib/libc.so (__start_thread+25)
04-07 19:31:22.101 F/DEBUG ( 68): # 17 pc 00017116 /system/lib/libc.so (__bionic_clone+70)
04-07 19:31:22.207 F/DEBUG ( 68):
04-07 19:31:22.207 F/DEBUG ( 68): Tombstone written to: /data/tombstones/tombstone_08
04-07 19:31:22.207 E/DEBUG ( 68): AM write failed: Broken pipe

Mono.Cecil.dll built from latest master branch (which I've read in #262 should support PCLs).
Tried using PCL profiles 111 and 259, to no avail.

Is there something I'm doing wrong or is it a legit bug?

@jbevain
Copy link
Owner

jbevain commented Apr 7, 2016

Hm! If anything, that's a bug in Mono for Android that is triggered by us. As far as Cecil is concerned, it's simply writing a binary file.

Can you file a bug with Xamarin and let us know the results?

@mfkl
Copy link
Author

mfkl commented Apr 7, 2016

When creating a simple repro project with the bug for Xamarin, I realised the (string filePath) API wasn't available anymore, just the (Stream stream) one. Turns out I wasn't using the latest version of Mono.Cecil after all.. My bad.

Using this crossplatform IO library, reading an assembly is fine but I don't see how I can get the modified assembly as a stream in a crossplatform way to write/save it back. Is there (another :-P) thing I've been missing regarding your PCL API?

Thanks!

@mfkl mfkl closed this as completed Apr 7, 2016
@jbevain
Copy link
Owner

jbevain commented Apr 7, 2016

@Martz94 can't you pass a stream you control to Write? Could be a MemoryStream if you want to stay in memory.

@mfkl
Copy link
Author

mfkl commented Apr 7, 2016

file = FileSystem.Current.GetFileFromPathAsync(path);
var s = file.Result.OpenAsync(FileAccess.Read).Result;
var assembly = AssemblyDefinition.ReadAssembly(s);
var loadedType = assembly.MainModule.Types.First(t => t.FullName == serviceFullName);
if(loadedType.Interfaces.Contains(_defService)) return;
loadedType.Interfaces.Add(_defService);
s.Position = 0;
assembly.Write(s);

This throws a System.ArgumentException: Value does not fall within the expected range. because using s is I assume, wrong.
I'd love to create a new MemoryStream and pass it to Write but I can't find how to get an AssemblyDefinition as a byte[]. Feels like I'm definitely missing on something here...

@jbevain
Copy link
Owner

jbevain commented Apr 7, 2016

In that case, s is a Read only stream (you pass FileAccess.Read). Cecil needs write and seek access. If you pass FileAccess.ReadWrite, your code should work.

If you want to write in memory you can simply write:

var stream = new MemoryStream();
assembly.Write(stream);

@mfkl
Copy link
Author

mfkl commented Apr 7, 2016

Great, it works indeed. Thanks!
I can now write the stream without an issue by using ReadWrite, however when I check the type afterwards to see if the interface was actually added using GetTypeInfo().ImplementedInterfaces, its not there, as if nothing happened...

I'll figure it out eventually though. Thanks so much for your help!

Cheers.

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