diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /xpcom/reflect/xptcall/md/test | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'xpcom/reflect/xptcall/md/test')
-rw-r--r-- | xpcom/reflect/xptcall/md/test/README | 6 | ||||
-rwxr-xr-x | xpcom/reflect/xptcall/md/test/clean.bat | 5 | ||||
-rw-r--r-- | xpcom/reflect/xptcall/md/test/invoke_test.cpp | 187 | ||||
-rwxr-xr-x | xpcom/reflect/xptcall/md/test/mk_invoke.bat | 9 | ||||
-rwxr-xr-x | xpcom/reflect/xptcall/md/test/mk_stub.bat | 9 | ||||
-rw-r--r-- | xpcom/reflect/xptcall/md/test/moz.build | 11 | ||||
-rw-r--r-- | xpcom/reflect/xptcall/md/test/stub_test.cpp | 209 |
7 files changed, 436 insertions, 0 deletions
diff --git a/xpcom/reflect/xptcall/md/test/README b/xpcom/reflect/xptcall/md/test/README new file mode 100644 index 0000000000..04850b2e01 --- /dev/null +++ b/xpcom/reflect/xptcall/md/test/README @@ -0,0 +1,6 @@ +These are just simple test programs in which stripped down versions of the +XPConnect invoke and stubs code can be built and tested as the code is brought +up on various platforms. These probrams do not test the param sizing and copying +functionality of the routines. However, they do supply a place where the lowest +level assembly language code can be developed and debugged in the simplest of +contexts before it is moved into the real routines.
\ No newline at end of file diff --git a/xpcom/reflect/xptcall/md/test/clean.bat b/xpcom/reflect/xptcall/md/test/clean.bat new file mode 100755 index 0000000000..f320e222c9 --- /dev/null +++ b/xpcom/reflect/xptcall/md/test/clean.bat @@ -0,0 +1,5 @@ +@echo off +echo deleting intermediate files +if exist *.obj del *.obj > NUL +if exist *.ilk del *.ilk > NUL +if exist *.pdb del *.pdb > NUL
\ No newline at end of file diff --git a/xpcom/reflect/xptcall/md/test/invoke_test.cpp b/xpcom/reflect/xptcall/md/test/invoke_test.cpp new file mode 100644 index 0000000000..f23a9ccf15 --- /dev/null +++ b/xpcom/reflect/xptcall/md/test/invoke_test.cpp @@ -0,0 +1,187 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include <stdio.h> + +typedef unsigned nsresult; +typedef unsigned uint32_t; +typedef unsigned nsXPCVariant; + +#if defined(WIN32) +# define NS_IMETHOD virtual nsresult __stdcall +# define NS_IMETHODIMP nsresult __stdcall +#else +# define NS_IMETHOD virtual nsresult +# define NS_IMETHODIMP nsresult +#endif + +class base { + public: + NS_IMETHOD ignored() = 0; +}; + +class foo : public base { + public: + NS_IMETHOD callme1(int i, int j) = 0; + NS_IMETHOD callme2(int i, int j) = 0; + NS_IMETHOD callme3(int i, int j) = 0; +}; + +class bar : public foo { + public: + NS_IMETHOD ignored() override; + NS_IMETHOD callme1(int i, int j) override; + NS_IMETHOD callme2(int i, int j) override; + NS_IMETHOD callme3(int i, int j) override; +}; + +/* +class baz : public base { +public: + NS_IMETHOD ignored() override; + NS_IMETHOD callme1() override; + NS_IMETHOD callme2() override; + NS_IMETHOD callme3() override; + void setfoo(foo* f) {other = f;} + + foo* other; +}; +NS_IMETHODIMP baz::ignored(){return 0;} +*/ + +NS_IMETHODIMP bar::ignored() { return 0; } + +NS_IMETHODIMP bar::callme1(int i, int j) { + printf("called bar::callme1 with: %d %d\n", i, j); + return 5; +} + +NS_IMETHODIMP bar::callme2(int i, int j) { + printf("called bar::callme2 with: %d %d\n", i, j); + return 5; +} + +NS_IMETHODIMP bar::callme3(int i, int j) { + printf("called bar::callme3 with: %d %d\n", i, j); + return 5; +} + +void docall(foo* f, int i, int j) { f->callme1(i, j); } + +/***************************************************************************/ +#if defined(WIN32) + +static uint32_t __stdcall invoke_count_words(uint32_t paramCount, + nsXPCVariant* s) { + return paramCount; +} + +static void __stdcall invoke_copy_to_stack(uint32_t* d, uint32_t paramCount, + nsXPCVariant* s) { + for (uint32_t i = 0; i < paramCount; i++, d++, s++) { + *((uint32_t*)d) = *((uint32_t*)s); + } +} + +static nsresult __stdcall DoInvoke(void* that, uint32_t index, + uint32_t paramCount, nsXPCVariant* params) { + __asm { + push params + push paramCount + call invoke_count_words // stdcall, result in eax + shl eax,2 // *= 4 + sub esp,eax // make space for params + mov edx,esp + push params + push paramCount + push edx + call invoke_copy_to_stack // stdcall + mov ecx,that // instance in ecx + push ecx // push this + mov edx,[ecx] // vtable in edx + mov eax,index + shl eax,2 // *= 4 + add edx,eax + call [edx] // stdcall, i.e. callee cleans up stack. + } +} + +#else +/***************************************************************************/ +// just Linux_x86 now. Add other later... + +static uint32_t invoke_count_words(uint32_t paramCount, nsXPCVariant* s) { + return paramCount; +} + +static void invoke_copy_to_stack(uint32_t* d, uint32_t paramCount, + nsXPCVariant* s) { + for (uint32_t i = 0; i < paramCount; i++, d++, s++) { + *((uint32_t*)d) = *((uint32_t*)s); + } +} + +static nsresult DoInvoke(void* that, uint32_t index, uint32_t paramCount, + nsXPCVariant* params) { + uint32_t result; + void* fn_count = invoke_count_words; + void* fn_copy = invoke_copy_to_stack; + + __asm__ __volatile__( + "pushl %4\n\t" + "pushl %3\n\t" + "movl %5, %%eax\n\t" + "call *%%eax\n\t" /* count words */ + "addl $0x8, %%esp\n\t" + "shl $2, %%eax\n\t" /* *= 4 */ + "subl %%eax, %%esp\n\t" /* make room for params */ + "movl %%esp, %%edx\n\t" + "pushl %4\n\t" + "pushl %3\n\t" + "pushl %%edx\n\t" + "movl %6, %%eax\n\t" + "call *%%eax\n\t" /* copy params */ + "addl $0xc, %%esp\n\t" + "movl %1, %%ecx\n\t" + "pushl %%ecx\n\t" + "movl (%%ecx), %%edx\n\t" + "movl %2, %%eax\n\t" /* function index */ + "shl $2, %%eax\n\t" /* *= 4 */ + "addl $8, %%eax\n\t" /* += 8 */ + "addl %%eax, %%edx\n\t" + "call *(%%edx)\n\t" /* safe to not cleanup esp */ + "movl %%eax, %0" + : "=g"(result) /* %0 */ + : "g"(that), /* %1 */ + "g"(index), /* %2 */ + "g"(paramCount), /* %3 */ + "g"(params), /* %4 */ + "g"(fn_count), /* %5 */ + "g"(fn_copy) /* %6 */ + : "ax", "cx", "dx", "memory"); + + return result; +} + +#endif +/***************************************************************************/ + +int main() { + nsXPCVariant params1[2] = {1, 2}; + nsXPCVariant params2[2] = {2, 4}; + nsXPCVariant params3[2] = {3, 6}; + + foo* a = new bar(); + + // printf("calling via C++...\n"); + // docall(a, 12, 24); + + printf("calling via ASM...\n"); + DoInvoke(a, 1, 2, params1); + DoInvoke(a, 2, 2, params2); + DoInvoke(a, 3, 2, params3); + + return 0; +} diff --git a/xpcom/reflect/xptcall/md/test/mk_invoke.bat b/xpcom/reflect/xptcall/md/test/mk_invoke.bat new file mode 100755 index 0000000000..10a9be51de --- /dev/null +++ b/xpcom/reflect/xptcall/md/test/mk_invoke.bat @@ -0,0 +1,9 @@ +@echo off +@echo deleing old output +if exist invoke_test.obj del invoke_test.obj > NUL +if exist invoke_test.ilk del invoke_test.ilk > NUL +if exist *.pdb del *.pdb > NUL +if exist invoke_test.exe del invoke_test.exe > NUL + +@echo building... +cl /nologo -Zi -DWIN32 invoke_test.cpp
\ No newline at end of file diff --git a/xpcom/reflect/xptcall/md/test/mk_stub.bat b/xpcom/reflect/xptcall/md/test/mk_stub.bat new file mode 100755 index 0000000000..f9af17affe --- /dev/null +++ b/xpcom/reflect/xptcall/md/test/mk_stub.bat @@ -0,0 +1,9 @@ +@echo off +@echo deleing old output +if exist stub_test.obj del stub_test.obj > NUL +if exist stub_test.ilk del stub_test.ilk > NUL +if exist *.pdb del *.pdb > NUL +if exist stub_test.exe del stub_test.exe > NUL + +@echo building... +cl /nologo -Zi -DWIN32 stub_test.cpp
\ No newline at end of file diff --git a/xpcom/reflect/xptcall/md/test/moz.build b/xpcom/reflect/xptcall/md/test/moz.build new file mode 100644 index 0000000000..f31bcf64a3 --- /dev/null +++ b/xpcom/reflect/xptcall/md/test/moz.build @@ -0,0 +1,11 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +SimplePrograms( + [ + "stub_test", + ] +) diff --git a/xpcom/reflect/xptcall/md/test/stub_test.cpp b/xpcom/reflect/xptcall/md/test/stub_test.cpp new file mode 100644 index 0000000000..c83eb93ccc --- /dev/null +++ b/xpcom/reflect/xptcall/md/test/stub_test.cpp @@ -0,0 +1,209 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include <stdio.h> + +typedef unsigned nsresult; +typedef unsigned uint32_t; +typedef unsigned nsXPCVariant; + +#if defined(WIN32) +# define NS_IMETHOD virtual nsresult __stdcall +# define NS_IMETHODIMP nsresult __stdcall +#else +# define NS_IMETHOD virtual nsresult +# define NS_IMETHODIMP nsresult +#endif + +class base { + public: + NS_IMETHOD ignored() = 0; +}; + +class foo : public base { + public: + NS_IMETHOD callme1(int i, int j) = 0; + NS_IMETHOD callme2(int i, int j) = 0; + NS_IMETHOD callme3(int i, int j) = 0; +}; + +class bar : public foo { + public: + NS_IMETHOD ignored() override; + NS_IMETHOD callme1(int i, int j) override; + NS_IMETHOD callme2(int i, int j) override; + NS_IMETHOD callme3(int i, int j) override; +}; + +class baz : public base { + public: + NS_IMETHOD ignored() override; + NS_IMETHOD callme1() override; + NS_IMETHOD callme2() override; + NS_IMETHOD callme3() override; + void setfoo(foo* f) { other = f; } + + foo* other; +}; +NS_IMETHODIMP baz::ignored() { return 0; } + +NS_IMETHODIMP bar::ignored() { return 0; } + +NS_IMETHODIMP bar::callme1(int i, int j) { + printf("called bar::callme1 with: %d %d\n", i, j); + return 15; +} + +NS_IMETHODIMP bar::callme2(int i, int j) { + printf("called bar::callme2 with: %d %d\n", i, j); + return 25; +} + +NS_IMETHODIMP bar::callme3(int i, int j) { + printf("called bar::callme3 with: %d %d\n", i, j); + return 35; +} + +void docall(foo* f, int i, int j) { f->callme1(i, j); } + +/***************************************************************************/ +#if defined(WIN32) + +static int __stdcall PrepareAndDispatch(baz* self, uint32_t methodIndex, + uint32_t* args, + uint32_t* stackBytesToPop) { + fprintf(stdout, "PrepareAndDispatch (%p, %d, %p)\n", (void*)self, methodIndex, + (void*)args); + foo* a = self->other; + int p1 = (int)*args; + int p2 = (int)*(args + 1); + int out = 0; + switch (methodIndex) { + case 1: + out = a->callme1(p1, p2); + break; + case 2: + out = a->callme2(p1, p2); + break; + case 3: + out = a->callme3(p1, p2); + break; + } + *stackBytesToPop = 2 * 4; + return out; +} + +# ifndef __GNUC__ +static __declspec(naked) void SharedStub(void) { + __asm { + push ebp // set up simple stack frame + mov ebp, esp // stack has: ebp/vtbl_index/retaddr/this/args + push ecx // make room for a ptr + lea eax, [ebp-4] // pointer to stackBytesToPop + push eax + lea ecx, [ebp+16] // pointer to args + push ecx + mov edx, [ebp+4] // vtbl_index + push edx + mov eax, [ebp+12] // this + push eax + call PrepareAndDispatch + mov edx, [ebp+8] // return address + mov ecx, [ebp-4] // stackBytesToPop + add ecx, 12 // for this, the index, and ret address + mov esp, ebp + pop ebp + add esp, ecx // fix up stack pointer + jmp edx // simulate __stdcall return + } +} + +// these macros get expanded (many times) in the file #included below +# define STUB_ENTRY(n) \ + __declspec(naked) nsresult __stdcall baz::callme##n() { \ + __asm push n __asm jmp SharedStub \ + } + +# else /* __GNUC__ */ + +# define STUB_ENTRY(n) \ + nsresult __stdcall baz::callme##n() { \ + uint32_t *args, stackBytesToPop; \ + int result = 0; \ + baz* obj; \ + __asm__ __volatile__( \ + "leal 0x0c(%%ebp), %0\n\t" /* args */ \ + "movl 0x08(%%ebp), %1\n\t" /* this */ \ + : "=r"(args), "=r"(obj)); \ + result = PrepareAndDispatch(obj, n, args, &stackBytesToPop); \ + fprintf(stdout, "stub returning: %d\n", result); \ + fprintf(stdout, "bytes to pop: %d\n", stackBytesToPop); \ + return result; \ + } + +# endif /* ! __GNUC__ */ + +#else +/***************************************************************************/ +// just Linux_x86 now. Add other later... + +static int PrepareAndDispatch(baz* self, uint32_t methodIndex, uint32_t* args) { + foo* a = self->other; + int p1 = (int)*args; + int p2 = (int)*(args + 1); + switch (methodIndex) { + case 1: + a->callme1(p1, p2); + break; + case 2: + a->callme2(p1, p2); + break; + case 3: + a->callme3(p1, p2); + break; + } + return 1; +} + +# define STUB_ENTRY(n) \ + nsresult baz::callme##n() { \ + void* method = PrepareAndDispatch; \ + nsresult result; \ + __asm__ __volatile__( \ + "leal 0x0c(%%ebp), %%ecx\n\t" /* args */ \ + "pushl %%ecx\n\t" \ + "pushl $" #n \ + "\n\t" /* method index */ \ + "movl 0x08(%%ebp), %%ecx\n\t" /* this */ \ + "pushl %%ecx\n\t" \ + "call *%%edx" /* PrepareAndDispatch */ \ + : "=a"(result) /* %0 */ \ + : "d"(method) /* %1 */ \ + : "memory"); \ + return result; \ + } + +#endif +/***************************************************************************/ + +STUB_ENTRY(1) +STUB_ENTRY(2) +STUB_ENTRY(3) + +int main() { + foo* a = new bar(); + baz* b = new baz(); + + /* here we make the global 'check for alloc failure' checker happy */ + if (!a || !b) return 1; + + foo* c = (foo*)b; + + b->setfoo(a); + c->callme1(1, 2); + c->callme2(2, 4); + c->callme3(3, 6); + + return 0; +} |