Skip to content

Instantly share code, notes, and snippets.

@robyoung
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robyoung/3644f13a05c95cb1b947 to your computer and use it in GitHub Desktop.
Save robyoung/3644f13a05c95cb1b947 to your computer and use it in GitHub Desktop.
Return pointer to Rust struct
[package]
name = "python-to-rust"
version = "0.0.1"
authors = ["Rob Young"]
[lib]
name = "pytesttype"
crate-type = ["dylib"]
#![feature(libc)]
#![feature(alloc)]
extern crate libc;
use std::boxed;
pub struct Point {
x: i64,
y: i32,
}
#[no_mangle]
pub extern "C" fn start_state() -> *mut Point {
let point = Box::new(Point{x: 0, y: 10});
let raw = unsafe { boxed::into_raw(point) };
println!("{:?}", raw);
raw
}
#[no_mangle]
pub extern "C" fn continue_state(point: *mut Point) -> i32 {
println!("{:?}", point);
let p = unsafe { Box::from_raw(point) };
println!("{} {}", p.x, p.y);
0
}
import ctypes
lib = ctypes.cdll.LoadLibrary('target/libpytesttype.so')
lib.start_state.restype = ctypes.c_void_p
pointer = lib.start_state()
print("{:x}".format(pointer))
lib.continue_state(pointer)
0xdc24000
10dc24000
0xdc24000
[1] 64006 segmentation fault python src/main.py
Process: Python [62405]
Path: /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Identifier: Python
Version: 2.7.6 (2.7.6)
Code Type: X86-64 (Native)
Parent Process: zsh [36342]
Responsible: iTerm [1189]
User ID: 2016596
PlugIn Path: /Users/USER/*/libpytesttype-95f1ab493cc8fd06.dylib
PlugIn Identifier: libpytesttype-95f1ab493cc8fd06.dylib
PlugIn Version: ??? (0)
Date/Time: 2015-05-08 21:08:12.430 +0100
OS Version: Mac OS X 10.10.2 (14C1514)
Report Version: 11
Anonymous UUID: E5968667-05E2-0830-A381-F38ED2164959
Sleep/Wake UUID: 13E59820-ABF2-4557-BBC8-9F60A00B63E8
Time Awake Since Boot: 200000 seconds
Time Since Wake: 9200 seconds
Crashed Thread: 0 Dispatch queue: com.apple.main-thread
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000010824000
VM Regions Near 0x10824000:
-->
__TEXT 000000010fcc9000-000000010fcca000 [ 4K] r-x/rwx SM=COW /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libpytesttype-95f1ab493cc8fd06.dylib 0x000000011012d250 fmt::num::i32.fmt..Display::fmt::h4a85fc9ea099189fytB + 48
1 libpytesttype-95f1ab493cc8fd06.dylib 0x000000011012e54e fmt::write::hec30e83159a50eb8Z7B + 974
2 libpytesttype-95f1ab493cc8fd06.dylib 0x000000011012e179 fmt::Arguments$LT$$u27$a$GT$.Debug::fmt::hcebfc24865089036y5B + 121
3 libpytesttype-95f1ab493cc8fd06.dylib 0x000000011012e54e fmt::write::hec30e83159a50eb8Z7B + 974
4 libpytesttype-95f1ab493cc8fd06.dylib 0x0000000110043715 old_io::Writer::write_fmt::h6008283369114950499 + 197
5 libpytesttype-95f1ab493cc8fd06.dylib 0x0000000110045f26 old_io::stdio::println_args::hb86725cdadc4dc06hKg + 1190
6 libpytesttype-95f1ab493cc8fd06.dylib 0x000000010ffec0d9 continue_state::__rust_abi + 377
7 libpytesttype-95f1ab493cc8fd06.dylib 0x000000010ffebf5a continue_state + 58
8 libffi.dylib 0x00007fff9386df44 ffi_call_unix64 + 76
9 libffi.dylib 0x00007fff9386e781 ffi_call + 853
10 _ctypes.so 0x000000010ffd0762 _ctypes_callproc + 874
11 _ctypes.so 0x000000010ffcabad 0x10ffc8000 + 11181
12 org.python.python 0x000000010fcd82ac PyObject_Call + 99
13 org.python.python 0x000000010fd54c00 PyEval_EvalFrameEx + 11370
14 org.python.python 0x000000010fd51d62 PyEval_EvalCodeEx + 1413
15 org.python.python 0x000000010fd517d7 PyEval_EvalCode + 54
16 org.python.python 0x000000010fd717bd PyParser_ASTFromFile + 315
17 org.python.python 0x000000010fd71860 PyRun_FileExFlags + 133
18 org.python.python 0x000000010fd713fd PyRun_SimpleFileExFlags + 769
19 org.python.python 0x000000010fd82b23 Py_Main + 3051
20 libdyld.dylib 0x00007fff93e855c9 start + 1
Thread 0 crashed with X86 Thread State (64-bit):
rax: 0x0000000000000000 rbx: 0x00007fff4ff35810 rcx: 0x0000000000000000 rdx: 0x0000000000000000
rdi: 0x0000000010824000 rsi: 0x00007fff4ff35178 rbp: 0x00007fff4ff35140 rsp: 0x00007fff4ff350a0
r8: 0x0000000000000004 r9: 0x0000000000000003 r10: 0x0000000000000000 r11: 0x00000000006d3e28
r12: 0x0000000000000020 r13: 0x0000000000000020 r14: 0x00000001101be740 r15: 0x00007fff4ff35168
rip: 0x000000011012d250 rfl: 0x0000000000010206 cr2: 0x0000000010824000
Logical CPU: 2
Error Code: 0x00000004
Trap Number: 14
Binary Images:
0x10fcc9000 - 0x10fcc9fff org.python.python (2.7.6 - 2.7.6) <A7B51404-B996-3FDF-A40F-0CCE1837F1F4> /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
0x10fcce000 - 0x10fdbfff7 org.python.python (2.7.6 - 2.7.6) <A5C1B694-31A0-3966-B6BC-C40694DC707F> /System/Library/Frameworks/Python.framework/Versions/2.7/Python
0x10ffc8000 - 0x10ffd6fff _ctypes.so (89) <8BF6FB14-BF96-3CB3-BD44-39A5F402FF97> /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_ctypes.so
0x10ffe1000 - 0x10ffe4fff _struct.so (89) <015BD869-7103-3735-A10A-70E4C15C9AB8> /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_struct.so
0x10ffeb000 - 0x1101bdfcf +libpytesttype-95f1ab493cc8fd06.dylib (0) <6D50B67C-F9DE-3659-99CE-C998391B3D90> /Users/USER/*/libpytesttype-95f1ab493cc8fd06.dylib
0x7fff6e0be000 - 0x7fff6e0f4837 dyld (353.2.1) <65DCCB06-339C-3E25-9702-600A28291D0E> /usr/lib/dyld
0x7fff86486000 - 0x7fff86488ff7 libsystem_sandbox.dylib (358.1.1) <95312E09-DA28-324A-A084-F3E574D0210E> /usr/lib/system/libsystem_sandbox.dylib
0x7fff869cc000 - 0x7fff869d1ff7 libmacho.dylib (862) <126CA2ED-DE91-308F-8881-B9DAEC3C63B6> /usr/lib/system/libmacho.dylib
0x7fff86a16000 - 0x7fff86a18ff7 libquarantine.dylib (76) <DC041627-2D92-361C-BABF-A869A5C72293> /usr/lib/system/libquarantine.dylib
0x7fff86a20000 - 0x7fff86a28fff libsystem_dnssd.dylib (561.1.1) <62B70ECA-E40D-3C63-896E-7F00EC386DDB> /usr/lib/system/libsystem_dnssd.dylib
0x7fff86ad6000 - 0x7fff86b4cfe7 libcorecrypto.dylib (233.1.2) <E1789801-3985-3949-B736-6B3378873301> /usr/lib/system/libcorecrypto.dylib
0x7fff87120000 - 0x7fff87121fff libDiagnosticMessagesClient.dylib (100) <2EE8E436-5CDC-34C5-9959-5BA218D507FB> /usr/lib/libDiagnosticMessagesClient.dylib
0x7fff872ab000 - 0x7fff87641fff com.apple.CoreFoundation (6.9 - 1152) <CBD1591C-405E-376E-87E9-B264610EBF49> /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation
0x7fff877e7000 - 0x7fff877e8ff7 libsystem_blocks.dylib (65) <9615D10A-FCA7-3BE4-AA1A-1B195DACE1A1> /usr/lib/system/libsystem_blocks.dylib
0x7fff88171000 - 0x7fff88176ff7 libunwind.dylib (35.3) <BE7E51A0-B6EA-3A54-9CCA-9D88F683A6D6> /usr/lib/system/libunwind.dylib
0x7fff88177000 - 0x7fff88194fff libsystem_kernel.dylib (2782.10.72) <97CD7ACD-EA0C-3434-BEFC-FCD013D6BB73> /usr/lib/system/libsystem_kernel.dylib
0x7fff88c1e000 - 0x7fff88c1ffff libsystem_secinit.dylib (18) <581DAD0F-6B63-3A48-B63B-917AF799ABAA> /usr/lib/system/libsystem_secinit.dylib
0x7fff891e2000 - 0x7fff891e8ff7 libsystem_networkextension.dylib (167.1.10) <29AB225B-D7FB-30ED-9600-65D44B9A9442> /usr/lib/system/libsystem_networkextension.dylib
0x7fff89dbc000 - 0x7fff89dc1ff7 libsystem_stats.dylib (163.10.18) <9B8CCF24-DDDB-399A-9237-4BEC225D2E8C> /usr/lib/system/libsystem_stats.dylib
0x7fff89dc3000 - 0x7fff89e4fff7 libsystem_c.dylib (1044.10.1) <199ED5EB-77A1-3D43-AA51-81779CE0A742> /usr/lib/system/libsystem_c.dylib
0x7fff89f4d000 - 0x7fff89f5eff3 libsystem_coretls.dylib (35.10.1) <3EAED90A-7AA0-323C-A52B-E16477981D59> /usr/lib/system/libsystem_coretls.dylib
0x7fff8a28b000 - 0x7fff8a296fff libcommonCrypto.dylib (60061) <D381EBC6-69D8-31D3-8084-5A80A32CB748> /usr/lib/system/libcommonCrypto.dylib
0x7fff8a565000 - 0x7fff8a56efff libsystem_pthread.dylib (105.10.1) <3103AA7F-3BAE-3673-9649-47FFD7E15C97> /usr/lib/system/libsystem_pthread.dylib
0x7fff8a574000 - 0x7fff8a59eff7 libdispatch.dylib (442.1.4) <502CF32B-669B-3709-8862-08188225E4F0> /usr/lib/system/libdispatch.dylib
0x7fff8a71b000 - 0x7fff8a71dff7 libsystem_coreservices.dylib (9) <41B7C578-5A53-31C8-A96F-C73E030B0938> /usr/lib/system/libsystem_coreservices.dylib
0x7fff8a78a000 - 0x7fff8a7bafff libsystem_m.dylib (3086.1) <1E12AB45-6D96-36D0-A226-F24D9FB0D9D6> /usr/lib/system/libsystem_m.dylib
0x7fff8aef9000 - 0x7fff8af3fff7 libauto.dylib (186) <A260789B-D4D8-316A-9490-254767B8A5F1> /usr/lib/libauto.dylib
0x7fff8b533000 - 0x7fff8b53aff7 libcompiler_rt.dylib (35) <BF8FC133-EE10-3DA6-9B90-92039E28678F> /usr/lib/system/libcompiler_rt.dylib
0x7fff8b5fc000 - 0x7fff8b5fcff7 liblaunch.dylib (559.10.3) <DFCDEBDF-8247-3DC7-9879-E7E497DDA4B4> /usr/lib/system/liblaunch.dylib
0x7fff8b5fd000 - 0x7fff8b605fff libsystem_platform.dylib (63) <64E34079-D712-3D66-9CE2-418624A5C040> /usr/lib/system/libsystem_platform.dylib
0x7fff8e35e000 - 0x7fff8e374ff7 libsystem_asl.dylib (267) <F153AC5B-0542-356E-88C8-20A62CA704E2> /usr/lib/system/libsystem_asl.dylib
0x7fff8e375000 - 0x7fff8e56f46f libobjc.A.dylib (647) <759E155D-BC42-3D4E-869B-6F57D477177C> /usr/lib/libobjc.A.dylib
0x7fff8ea02000 - 0x7fff8ea03ffb libremovefile.dylib (35) <3485B5F4-6CE8-3C62-8DFD-8736ED6E8531> /usr/lib/system/libremovefile.dylib
0x7fff8eb55000 - 0x7fff8eb80fff libc++abi.dylib (125) <88A22A0F-87C6-3002-BFBA-AC0F2808B8B9> /usr/lib/libc++abi.dylib
0x7fff900d9000 - 0x7fff902beff3 libicucore.A.dylib (531.31) <B08E00D5-13C6-3391-AB3A-8DE693D3B42E> /usr/lib/libicucore.A.dylib
0x7fff902bf000 - 0x7fff90313fff libc++.1.dylib (120) <1B9530FD-989B-3174-BB1C-BDC159501710> /usr/lib/libc++.1.dylib
0x7fff90995000 - 0x7fff90999fff libcache.dylib (69) <45E9A2E7-99C4-36B2-BEE3-0C4E11614AD1> /usr/lib/system/libcache.dylib
0x7fff90ce2000 - 0x7fff90ceaffb libcopyfile.dylib (118.1.2) <0C68D3A6-ACDD-3EF3-991A-CC82C32AB836> /usr/lib/system/libcopyfile.dylib
0x7fff90f8c000 - 0x7fff90f92fff libsystem_trace.dylib (72.1.3) <A9E6B7D8-C327-3742-AC54-86C94218B1DF> /usr/lib/system/libsystem_trace.dylib
0x7fff90f93000 - 0x7fff90f9cff7 libsystem_notify.dylib (133.1.1) <61147800-F320-3DAA-850C-BADF33855F29> /usr/lib/system/libsystem_notify.dylib
0x7fff90f9d000 - 0x7fff90f9dff7 libkeymgr.dylib (28) <77845842-DE70-3CC5-BD01-C3D14227CED5> /usr/lib/system/libkeymgr.dylib
0x7fff915dc000 - 0x7fff91604fff libxpc.dylib (559.10.3) <876216DC-D5D3-381E-8AF9-49AE464E5107> /usr/lib/system/libxpc.dylib
0x7fff9164a000 - 0x7fff91672fff libsystem_info.dylib (459) <B85A85D5-8530-3A93-B0C3-4DEC41F79478> /usr/lib/system/libsystem_info.dylib
0x7fff91b22000 - 0x7fff91b23fff libSystem.B.dylib (1213) <90B107BC-FF74-32CC-B1CF-4E02F544D957> /usr/lib/libSystem.B.dylib
0x7fff92afa000 - 0x7fff92b0bff7 libz.1.dylib (55) <88C7C7DE-04B8-316F-8B74-ACD9F3DE1AA1> /usr/lib/libz.1.dylib
0x7fff934db000 - 0x7fff93513ffb libsystem_network.dylib (411.1) <2EC3A005-473F-3C36-A665-F88B5BACC7F0> /usr/lib/system/libsystem_network.dylib
0x7fff93779000 - 0x7fff9377bfff libsystem_configuration.dylib (699.1.5) <5E14864E-089A-3D84-85A4-980B776427A8> /usr/lib/system/libsystem_configuration.dylib
0x7fff9386d000 - 0x7fff9386efff libffi.dylib (18.1) <0F6C6A4D-1210-3585-8DA1-B8A94B9924A5> /usr/lib/libffi.dylib
0x7fff9386f000 - 0x7fff9388bff7 libsystem_malloc.dylib (53.1.1) <19BCC257-5717-3502-A71F-95D65AFA861B> /usr/lib/system/libsystem_malloc.dylib
0x7fff93969000 - 0x7fff93969ff7 libunc.dylib (29) <5676F7EA-C1DF-329F-B006-D2C3022B7D70> /usr/lib/system/libunc.dylib
0x7fff93e82000 - 0x7fff93e85ff7 libdyld.dylib (353.2.1) <4E33E416-F1D8-3598-B8CC-6863E2ECD0E6> /usr/lib/system/libdyld.dylib
External Modification Summary:
Calls made by other processes targeting this process:
task_for_pid: 0
thread_create: 0
thread_set_state: 0
Calls made by this process:
task_for_pid: 0
thread_create: 0
thread_set_state: 0
Calls made by all processes on this machine:
task_for_pid: 150162
thread_create: 0
thread_set_state: 0
VM Region Summary:
ReadOnly portion of Libraries: Total=85.7M resident=51.5M(60%) swapped_out_or_unallocated=34.1M(40%)
Writable regions: Total=29.2M written=2524K(8%) resident=4448K(15%) swapped_out=0K(0%) unallocated=24.8M(85%)
REGION TYPE VIRTUAL
=========== =======
Kernel Alloc Once 4K
MALLOC 12.6M
MALLOC (admin) 16K
STACK GUARD 56.0M
Stack 8192K
VM_ALLOCATE 8208K
__DATA 1672K
__LINKEDIT 71.5M
__TEXT 14.2M
__UNICODE 544K
shared memory 4K
=========== =======
TOTAL 172.5M
Model: MacBookAir6,2, BootROM MBA61.0099.B18, 2 processors, Intel Core i5, 1.3 GHz, 4 GB, SMC 2.13f15
Graphics: Intel HD Graphics 5000, Intel HD Graphics 5000, Built-In
Memory Module: BANK 0/DIMM0, 2 GB, DDR3, 1600 MHz, 0x80AD, 0x483943434E4E4E384A544D4C41522D4E544D
Memory Module: BANK 1/DIMM0, 2 GB, DDR3, 1600 MHz, 0x80AD, 0x483943434E4E4E384A544D4C41522D4E544D
AirPort: spairport_wireless_card_type_airport_extreme (0x14E4, 0x117), Broadcom BCM43xx 1.0 (7.15.159.13.12)
Bluetooth: Version 4.3.2f6 15235, 3 services, 19 devices, 1 incoming serial ports
Network Service: Wi-Fi, AirPort, en0
Serial ATA Device: APPLE SSD SD0128F, 121.33 GB
USB Device: Internal Memory Card Reader
USB Device: Mass Storage Device
USB Device: BRCM20702 Hub
USB Device: Bluetooth USB Host Controller
Thunderbolt Bus: MacBook Air, Apple Inc., 23.6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment