Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | |
michael@0 | 2 | #include <stdio.h> |
michael@0 | 3 | |
michael@0 | 4 | typedef unsigned nsresult; |
michael@0 | 5 | typedef unsigned uint32_t; |
michael@0 | 6 | typedef unsigned nsXPCVariant; |
michael@0 | 7 | |
michael@0 | 8 | |
michael@0 | 9 | #if defined(WIN32) |
michael@0 | 10 | #define NS_IMETHOD virtual nsresult __stdcall |
michael@0 | 11 | #define NS_IMETHODIMP nsresult __stdcall |
michael@0 | 12 | #else |
michael@0 | 13 | #define NS_IMETHOD virtual nsresult |
michael@0 | 14 | #define NS_IMETHODIMP nsresult |
michael@0 | 15 | #endif |
michael@0 | 16 | |
michael@0 | 17 | |
michael@0 | 18 | class base{ |
michael@0 | 19 | public: |
michael@0 | 20 | NS_IMETHOD ignored() = 0; |
michael@0 | 21 | }; |
michael@0 | 22 | |
michael@0 | 23 | class foo : public base { |
michael@0 | 24 | public: |
michael@0 | 25 | NS_IMETHOD callme1(int i, int j) = 0; |
michael@0 | 26 | NS_IMETHOD callme2(int i, int j) = 0; |
michael@0 | 27 | NS_IMETHOD callme3(int i, int j) = 0; |
michael@0 | 28 | }; |
michael@0 | 29 | |
michael@0 | 30 | class bar : public foo{ |
michael@0 | 31 | public: |
michael@0 | 32 | NS_IMETHOD ignored(); |
michael@0 | 33 | NS_IMETHOD callme1(int i, int j); |
michael@0 | 34 | NS_IMETHOD callme2(int i, int j); |
michael@0 | 35 | NS_IMETHOD callme3(int i, int j); |
michael@0 | 36 | }; |
michael@0 | 37 | |
michael@0 | 38 | class baz : public base { |
michael@0 | 39 | public: |
michael@0 | 40 | NS_IMETHOD ignored(); |
michael@0 | 41 | NS_IMETHOD callme1(); |
michael@0 | 42 | NS_IMETHOD callme2(); |
michael@0 | 43 | NS_IMETHOD callme3(); |
michael@0 | 44 | void setfoo(foo* f) {other = f;} |
michael@0 | 45 | |
michael@0 | 46 | foo* other; |
michael@0 | 47 | }; |
michael@0 | 48 | NS_IMETHODIMP baz::ignored(){return 0;} |
michael@0 | 49 | |
michael@0 | 50 | NS_IMETHODIMP bar::ignored(){return 0;} |
michael@0 | 51 | |
michael@0 | 52 | NS_IMETHODIMP bar::callme1(int i, int j) |
michael@0 | 53 | { |
michael@0 | 54 | printf("called bar::callme1 with: %d %d\n", i, j); |
michael@0 | 55 | return 15; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | NS_IMETHODIMP bar::callme2(int i, int j) |
michael@0 | 59 | { |
michael@0 | 60 | printf("called bar::callme2 with: %d %d\n", i, j); |
michael@0 | 61 | return 25; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | NS_IMETHODIMP bar::callme3(int i, int j) |
michael@0 | 65 | { |
michael@0 | 66 | printf("called bar::callme3 with: %d %d\n", i, j); |
michael@0 | 67 | return 35; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | void docall(foo* f, int i, int j){ |
michael@0 | 71 | f->callme1(i, j); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | /***************************************************************************/ |
michael@0 | 75 | #if defined(WIN32) |
michael@0 | 76 | |
michael@0 | 77 | static int __stdcall |
michael@0 | 78 | PrepareAndDispatch(baz* self, uint32_t methodIndex, |
michael@0 | 79 | uint32_t* args, uint32_t* stackBytesToPop) |
michael@0 | 80 | { |
michael@0 | 81 | fprintf(stdout, "PrepareAndDispatch (%p, %d, %p)\n", |
michael@0 | 82 | (void*)self, methodIndex, (void*)args); |
michael@0 | 83 | foo* a = self->other; |
michael@0 | 84 | int p1 = (int) *args; |
michael@0 | 85 | int p2 = (int) *(args+1); |
michael@0 | 86 | int out = 0; |
michael@0 | 87 | switch(methodIndex) |
michael@0 | 88 | { |
michael@0 | 89 | case 1: out = a->callme1(p1, p2); break; |
michael@0 | 90 | case 2: out = a->callme2(p1, p2); break; |
michael@0 | 91 | case 3: out = a->callme3(p1, p2); break; |
michael@0 | 92 | } |
michael@0 | 93 | *stackBytesToPop = 2*4; |
michael@0 | 94 | return out; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | #ifndef __GNUC__ |
michael@0 | 98 | static __declspec(naked) void SharedStub(void) |
michael@0 | 99 | { |
michael@0 | 100 | __asm { |
michael@0 | 101 | push ebp // set up simple stack frame |
michael@0 | 102 | mov ebp, esp // stack has: ebp/vtbl_index/retaddr/this/args |
michael@0 | 103 | push ecx // make room for a ptr |
michael@0 | 104 | lea eax, [ebp-4] // pointer to stackBytesToPop |
michael@0 | 105 | push eax |
michael@0 | 106 | lea ecx, [ebp+16] // pointer to args |
michael@0 | 107 | push ecx |
michael@0 | 108 | mov edx, [ebp+4] // vtbl_index |
michael@0 | 109 | push edx |
michael@0 | 110 | mov eax, [ebp+12] // this |
michael@0 | 111 | push eax |
michael@0 | 112 | call PrepareAndDispatch |
michael@0 | 113 | mov edx, [ebp+8] // return address |
michael@0 | 114 | mov ecx, [ebp-4] // stackBytesToPop |
michael@0 | 115 | add ecx, 12 // for this, the index, and ret address |
michael@0 | 116 | mov esp, ebp |
michael@0 | 117 | pop ebp |
michael@0 | 118 | add esp, ecx // fix up stack pointer |
michael@0 | 119 | jmp edx // simulate __stdcall return |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | // these macros get expanded (many times) in the file #included below |
michael@0 | 124 | #define STUB_ENTRY(n) \ |
michael@0 | 125 | __declspec(naked) nsresult __stdcall baz::callme##n() \ |
michael@0 | 126 | { __asm push n __asm jmp SharedStub } |
michael@0 | 127 | |
michael@0 | 128 | #else /* __GNUC__ */ |
michael@0 | 129 | |
michael@0 | 130 | #define STUB_ENTRY(n) \ |
michael@0 | 131 | nsresult __stdcall baz::callme##n() \ |
michael@0 | 132 | { \ |
michael@0 | 133 | uint32_t *args, stackBytesToPop; \ |
michael@0 | 134 | int result = 0; \ |
michael@0 | 135 | baz *obj; \ |
michael@0 | 136 | __asm__ __volatile__ ( \ |
michael@0 | 137 | "leal 0x0c(%%ebp), %0\n\t" /* args */ \ |
michael@0 | 138 | "movl 0x08(%%ebp), %1\n\t" /* this */ \ |
michael@0 | 139 | : "=r" (args), \ |
michael@0 | 140 | "=r" (obj)); \ |
michael@0 | 141 | result = PrepareAndDispatch(obj, n, args,&stackBytesToPop); \ |
michael@0 | 142 | fprintf(stdout, "stub returning: %d\n", result); \ |
michael@0 | 143 | fprintf(stdout, "bytes to pop: %d\n", stackBytesToPop); \ |
michael@0 | 144 | return result; \ |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | #endif /* ! __GNUC__ */ |
michael@0 | 148 | |
michael@0 | 149 | #else |
michael@0 | 150 | /***************************************************************************/ |
michael@0 | 151 | // just Linux_x86 now. Add other later... |
michael@0 | 152 | |
michael@0 | 153 | static int |
michael@0 | 154 | PrepareAndDispatch(baz* self, uint32_t methodIndex, uint32_t* args) |
michael@0 | 155 | { |
michael@0 | 156 | foo* a = self->other; |
michael@0 | 157 | int p1 = (int) *args; |
michael@0 | 158 | int p2 = (int) *(args+1); |
michael@0 | 159 | switch(methodIndex) |
michael@0 | 160 | { |
michael@0 | 161 | case 1: a->callme1(p1, p2); break; |
michael@0 | 162 | case 2: a->callme2(p1, p2); break; |
michael@0 | 163 | case 3: a->callme3(p1, p2); break; |
michael@0 | 164 | } |
michael@0 | 165 | return 1; |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | #define STUB_ENTRY(n) \ |
michael@0 | 169 | nsresult baz::callme##n() \ |
michael@0 | 170 | { \ |
michael@0 | 171 | void* method = PrepareAndDispatch; \ |
michael@0 | 172 | nsresult result; \ |
michael@0 | 173 | __asm__ __volatile__( \ |
michael@0 | 174 | "leal 0x0c(%%ebp), %%ecx\n\t" /* args */ \ |
michael@0 | 175 | "pushl %%ecx\n\t" \ |
michael@0 | 176 | "pushl $"#n"\n\t" /* method index */ \ |
michael@0 | 177 | "movl 0x08(%%ebp), %%ecx\n\t" /* this */ \ |
michael@0 | 178 | "pushl %%ecx\n\t" \ |
michael@0 | 179 | "call *%%edx" /* PrepareAndDispatch */ \ |
michael@0 | 180 | : "=a" (result) /* %0 */ \ |
michael@0 | 181 | : "d" (method) /* %1 */ \ |
michael@0 | 182 | : "memory" ); \ |
michael@0 | 183 | return result; \ |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | #endif |
michael@0 | 187 | /***************************************************************************/ |
michael@0 | 188 | |
michael@0 | 189 | STUB_ENTRY(1) |
michael@0 | 190 | STUB_ENTRY(2) |
michael@0 | 191 | STUB_ENTRY(3) |
michael@0 | 192 | |
michael@0 | 193 | int main() |
michael@0 | 194 | { |
michael@0 | 195 | foo* a = new bar(); |
michael@0 | 196 | baz* b = new baz(); |
michael@0 | 197 | |
michael@0 | 198 | /* here we make the global 'check for alloc failure' checker happy */ |
michael@0 | 199 | if(!a || !b) |
michael@0 | 200 | return 1; |
michael@0 | 201 | |
michael@0 | 202 | foo* c = (foo*)b; |
michael@0 | 203 | |
michael@0 | 204 | b->setfoo(a); |
michael@0 | 205 | c->callme1(1,2); |
michael@0 | 206 | c->callme2(2,4); |
michael@0 | 207 | c->callme3(3,6); |
michael@0 | 208 | |
michael@0 | 209 | return 0; |
michael@0 | 210 | } |