Skip to content

Instantly share code, notes, and snippets.

@ihnorton
Last active December 16, 2015 00:59
Show Gist options
  • Select an option

  • Save ihnorton/5351485 to your computer and use it in GitHub Desktop.

Select an option

Save ihnorton/5351485 to your computer and use it in GitHub Desktop.
#include <stdio.h>
// g++ -g -shared test.cpp -o test.dll
class __declspec(dllexport) AAA {
public:
AAA();
virtual ~AAA();
virtual void foo() {};
};
class __declspec(dllexport) BBB: public AAA {
public:
BBB();
virtual ~BBB();
virtual void foo(float b) {
printf("hello %l\n", b);
return;
}
BBB* New() {
return new BBB;
}
};
AAA::AAA() {};
AAA::~AAA() {};
BBB::BBB() {};
BBB::~BBB() {};
# Segfault will happen at line 16, this=0x41200000 is very wrong.
julia> l = dlopen("test.dll")
Ptr{Void} @0x10049ef0
julia> bb = ccall( dlsym(l, "_ZN3BBB3NewEv"), Ptr{Void}, () )
Ptr{Void} @0x10634c80
julia> v_fptr(thisptr, vtidx) = unsafe_ref(unsafe_ref(pointer(Ptr{Ptr{Void}},thisptr)), vtidx+1)
# methods for generic function v_fptr
v_fptr(thisptr,vtidx) at none:1
julia> fptr = v_fptr(bb,3)
Ptr{Void} @0x6bec1a50
julia> ccall( fptr, thiscall, Void, (Ptr{Void}, Float32), bb, 10.0)
Breakpoint 1, BBB::foo (this=0x41200000, b=2.2587663434055604e-314) at test.cpp:15
15 printf("hello %l\n", b);
(gdb) n
hello
17 }
(gdb) n
0x00de7473 in ?? ()
(gdb) n
Cannot find bounds of current function
(gdb)
julia> function f2()
_f = pointer(Void, 0x6bec1a50)
_b = pointer(Void, 0x104da6d8)
ccall( _f, thiscall, Void, (Ptr{Void}, Float32), _b, 10.0)
end
# methods for generic function f2
f2() at none:2
julia> disassemble(f2, ())
define void @julia_f2() sspreq alignstack(16) align 16 {
pass:
call x86_thiscallcc void inttoptr (i32 1810635344 to void (i8*, float)*)(i8* inttoptr (i32 273524440 to i8*), float 1.000000e+01), !dbg !5319
ret void, !dbg !5319
}
bash-3.1$ cat test.ir
define void @julia_f2() sspreq alignstack(16) align 16 {
pass:
call x86_thiscallcc void inttoptr (i32 1810635344 to void (i8*, float)*)(i8* inttoptr (i32 273524440 to i8*), float 1.000000e+01)
ret void
}
bash-3.1$ llc test.ir
bash-3.1$ cat test.ir.s
.def _julia_f2;
.scl 2;
.type 32;
.endef
.text
.globl _julia_f2
.align 16, 0x90
_julia_f2: # @julia_f2
.cfi_startproc
# BB#0: # %pass
pushl %ebp
Ltmp2:
.cfi_def_cfa_offset 8
Ltmp3:
.cfi_offset %ebp, -8
movl %esp, %ebp
Ltmp4:
.cfi_def_cfa_register %ebp
andl $-16, %esp
subl $16, %esp
movl ___stack_chk_guard, %eax
movl %eax, 4(%esp)
movl $1092616192, (%esp) # imm = 0x41200000
movl $273524440, %ecx # imm = 0x104DA6D8
calll 1810635344
subl $4, %esp
movl ___stack_chk_guard, %eax
cmpl 4(%esp), %eax
jne LBB0_2
# BB#1: # %SP_return
movl %ebp, %esp
popl %ebp
ret
LBB0_2: # %CallStackCheckFailBlk
calll ___stack_chk_fail
.cfi_endproc
julia> disassemble(f, ())
define void @julia_f() sspreq alignstack(16) align 16 {
top:
%0 = alloca [4 x %jl_value_t*]
%.sub = getelementptr inbounds [4 x %jl_value_t*]* %0, i32 0, i32 0
%1 = getelementptr [4 x %jl_value_t*]* %0, i32 0, i32 2, !dbg !5229
%2 = bitcast [4 x %jl_value_t*]* %0 to i32*, !dbg !5229
store i32 4, i32* %2
%3 = getelementptr [4 x %jl_value_t*]* %0, i32 0, i32 1, !dbg !5229
%4 = bitcast %jl_value_t** %3 to %jl_value_t***, !dbg !5229
%5 = load %jl_value_t*** @jl_pgcstack, !dbg !5229
store %jl_value_t** %5, %jl_value_t*** %4, !dbg !5229
store %jl_value_t** %.sub, %jl_value_t*** @jl_pgcstack, !dbg !5229
store %jl_value_t* null, %jl_value_t** %1
%6 = getelementptr [4 x %jl_value_t*]* %0, i32 0, i32 3
store %jl_value_t* null, %jl_value_t** %6
%7 = load %jl_value_t** inttoptr (i32 276354808 to %jl_value_t**), !dbg !5236
%8 = getelementptr inbounds %jl_value_t* %7, i32 0, i32 0, !dbg !5236
%9 = load %jl_value_t** %8, !dbg !5236
%10 = getelementptr inbounds %jl_value_t* %9, i32 0, i32 0, !dbg !5236
%11 = load %jl_value_t** %10, !dbg !5236
%12 = icmp eq %jl_value_t* %11, inttoptr (i32 21241128 to %jl_value_t*), !dbg !5236
br i1 %12, label %pass, label %fail, !dbg !5236
fail: ; preds = %top
call void @jl_type_error_rt(i8* getelementptr inbounds ([2 x i8]* @_j_str463, i32 0, i32 0), i8* getelementptr inbounds ([69 x i8]* @_j_str464, i32 0, i32 0), %jl_value_t* inttoptr (i32 21241128 to %jl_value_t*), %jl_value_t* %9), !dbg !5236
unreachable
pass: ; preds = %top
%13 = getelementptr %jl_value_t** %10, i32 4, !dbg !5236
%14 = load %jl_value_t** %13, !dbg !5236
%15 = icmp eq %jl_value_t* %14, inttoptr (i32 21283168 to %jl_value_t*), !dbg !5236
br i1 %15, label %pass2, label %fail1, !dbg !5236
fail1: ; preds = %pass
call void @jl_type_error_rt(i8* getelementptr inbounds ([2 x i8]* @_j_str463, i32 0, i32 0), i8* getelementptr inbounds ([69 x i8]* @_j_str464, i32 0, i32 0), %jl_value_t* inttoptr (i32 21259064 to %jl_value_t*), %jl_value_t* %7), !dbg !5236
unreachable
pass2: ; preds = %pass
%16 = getelementptr %jl_value_t** %8, i32 1, !dbg !5236
%17 = bitcast %jl_value_t** %16 to i32*, !dbg !5236
%18 = load i32* %17, !dbg !5236
%19 = icmp eq i32 %18, 0, !dbg !5236
br i1 %19, label %fail3, label %pass4, !dbg !5236
fail3: ; preds = %pass2
%20 = load %jl_value_t** @jl_undefref_exception, !dbg !5236
call void @jl_throw_with_superfluous_argument(%jl_value_t* %20, i32 1), !dbg !5236
unreachable, !dbg !5236
pass4: ; preds = %pass2
%21 = inttoptr i32 %18 to void (i8*, float)*, !dbg !5236
store %jl_value_t* inttoptr (i32 21259192 to %jl_value_t*), %jl_value_t** %1, !dbg !5236
%22 = load %jl_value_t** inttoptr (i32 274674616 to %jl_value_t**), !dbg !5236
store %jl_value_t* %22, %jl_value_t** %6, !dbg !5236
%23 = call %jl_value_t* @jl_apply_generic(%jl_value_t* inttoptr (i32 63682208 to %jl_value_t*), %jl_value_t** %1, i32 2), !dbg !5236
%24 = call i8* @jl_value_to_pointer(%jl_value_t* inttoptr (i32 21274064 to %jl_value_t*), %jl_value_t* %23, i32 1, i32 0), !dbg !5236
call x86_thiscallcc void %21(i8* %24, float 1.000000e+01), !dbg !5236
%25 = load %jl_value_t** %3, !dbg !5236
%26 = getelementptr inbounds %jl_value_t* %25, i32 0, i32 0, !dbg !5236
store %jl_value_t** %26, %jl_value_t*** @jl_pgcstack, !dbg !5236
ret void, !dbg !5236
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment