Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

iasiothiscallresolver.cpp 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. IASIOThiscallResolver.cpp see the comments in iasiothiscallresolver.h for
  3. the top level description - this comment describes the technical details of
  4. the implementation.
  5. The latest version of this file is available from:
  6. http://www.audiomulch.com/~rossb/code/calliasio
  7. please email comments to Ross Bencina <rossb@audiomulch.com>
  8. BACKGROUND
  9. The IASIO interface declared in the Steinberg ASIO 2 SDK declares
  10. functions with no explicit calling convention. This causes MSVC++ to default
  11. to using the thiscall convention, which is a proprietary convention not
  12. implemented by some non-microsoft compilers - notably borland BCC,
  13. C++Builder, and gcc. MSVC++ is the defacto standard compiler used by
  14. Steinberg. As a result of this situation, the ASIO sdk will compile with
  15. any compiler, however attempting to execute the compiled code will cause a
  16. crash due to different default calling conventions on non-Microsoft
  17. compilers.
  18. IASIOThiscallResolver solves the problem by providing an adapter class that
  19. delegates to the IASIO interface using the correct calling convention
  20. (thiscall). Due to the lack of support for thiscall in the Borland and GCC
  21. compilers, the calls have been implemented in assembly language.
  22. A number of macros are defined for thiscall function calls with different
  23. numbers of parameters, with and without return values - it may be possible
  24. to modify the format of these macros to make them work with other inline
  25. assemblers.
  26. THISCALL DEFINITION
  27. A number of definitions of the thiscall calling convention are floating
  28. around the internet. The following definition has been validated against
  29. output from the MSVC++ compiler:
  30. For non-vararg functions, thiscall works as follows: the object (this)
  31. pointer is passed in ECX. All arguments are passed on the stack in
  32. right to left order. The return value is placed in EAX. The callee
  33. clears the passed arguments from the stack.
  34. FINDING FUNCTION POINTERS FROM AN IASIO POINTER
  35. The first field of a COM object is a pointer to its vtble. Thus a pointer
  36. to an object implementing the IASIO interface also points to a pointer to
  37. that object's vtbl. The vtble is a table of function pointers for all of
  38. the virtual functions exposed by the implemented interfaces.
  39. If we consider a variable declared as a pointer to IASO:
  40. IASIO *theAsioDriver
  41. theAsioDriver points to:
  42. object implementing IASIO
  43. {
  44. IASIOvtbl *vtbl
  45. other data
  46. }
  47. in other words, theAsioDriver points to a pointer to an IASIOvtbl
  48. vtbl points to a table of function pointers:
  49. IASIOvtbl ( interface IASIO : public IUnknown )
  50. {
  51. (IUnknown functions)
  52. 0 virtual HRESULT STDMETHODCALLTYPE (*QueryInterface)(REFIID riid, void **ppv) = 0;
  53. 4 virtual ULONG STDMETHODCALLTYPE (*AddRef)() = 0;
  54. 8 virtual ULONG STDMETHODCALLTYPE (*Release)() = 0;
  55. (IASIO functions)
  56. 12 virtual ASIOBool (*init)(void *sysHandle) = 0;
  57. 16 virtual void (*getDriverName)(char *name) = 0;
  58. 20 virtual long (*getDriverVersion)() = 0;
  59. 24 virtual void (*getErrorMessage)(char *string) = 0;
  60. 28 virtual ASIOError (*start)() = 0;
  61. 32 virtual ASIOError (*stop)() = 0;
  62. 36 virtual ASIOError (*getChannels)(long *numInputChannels, long *numOutputChannels) = 0;
  63. 40 virtual ASIOError (*getLatencies)(long *inputLatency, long *outputLatency) = 0;
  64. 44 virtual ASIOError (*getBufferSize)(long *minSize, long *maxSize,
  65. long *preferredSize, long *granularity) = 0;
  66. 48 virtual ASIOError (*canSampleRate)(ASIOSampleRate sampleRate) = 0;
  67. 52 virtual ASIOError (*getSampleRate)(ASIOSampleRate *sampleRate) = 0;
  68. 56 virtual ASIOError (*setSampleRate)(ASIOSampleRate sampleRate) = 0;
  69. 60 virtual ASIOError (*getClockSources)(ASIOClockSource *clocks, long *numSources) = 0;
  70. 64 virtual ASIOError (*setClockSource)(long reference) = 0;
  71. 68 virtual ASIOError (*getSamplePosition)(ASIOSamples *sPos, ASIOTimeStamp *tStamp) = 0;
  72. 72 virtual ASIOError (*getChannelInfo)(ASIOChannelInfo *info) = 0;
  73. 76 virtual ASIOError (*createBuffers)(ASIOBufferInfo *bufferInfos, long numChannels,
  74. long bufferSize, ASIOCallbacks *callbacks) = 0;
  75. 80 virtual ASIOError (*disposeBuffers)() = 0;
  76. 84 virtual ASIOError (*controlPanel)() = 0;
  77. 88 virtual ASIOError (*future)(long selector,void *opt) = 0;
  78. 92 virtual ASIOError (*outputReady)() = 0;
  79. };
  80. The numbers in the left column show the byte offset of each function ptr
  81. from the beginning of the vtbl. These numbers are used in the code below
  82. to select different functions.
  83. In order to find the address of a particular function, theAsioDriver
  84. must first be dereferenced to find the value of the vtbl pointer:
  85. mov eax, theAsioDriver
  86. mov edx, [theAsioDriver] // edx now points to vtbl[0]
  87. Then an offset must be added to the vtbl pointer to select a
  88. particular function, for example vtbl+44 points to the slot containing
  89. a pointer to the getBufferSize function.
  90. Finally vtbl+x must be dereferenced to obtain the value of the function
  91. pointer stored in that address:
  92. call [edx+44] // call the function pointed to by
  93. // the value in the getBufferSize field of the vtbl
  94. SEE ALSO
  95. Martin Fay's OpenASIO DLL at http://www.martinfay.com solves the same
  96. problem by providing a new COM interface which wraps IASIO with an
  97. interface that uses portable calling conventions. OpenASIO must be compiled
  98. with MSVC, and requires that you ship the OpenASIO DLL with your
  99. application.
  100. ACKNOWLEDGEMENTS
  101. Ross Bencina: worked out the thiscall details above, wrote the original
  102. Borland asm macros, and a patch for asio.cpp (which is no longer needed).
  103. Thanks to Martin Fay for introducing me to the issues discussed here,
  104. and to Rene G. Ceballos for assisting with asm dumps from MSVC++.
  105. Antti Silvast: converted the original calliasio to work with gcc and NASM
  106. by implementing the asm code in a separate file.
  107. Fraser Adams: modified the original calliasio containing the Borland inline
  108. asm to add inline asm for gcc i.e. Intel syntax for Borland and AT&T syntax
  109. for gcc. This seems a neater approach for gcc than to have a separate .asm
  110. file and it means that we only need one version of the thiscall patch.
  111. Fraser Adams: rewrote the original calliasio patch in the form of the
  112. IASIOThiscallResolver class in order to avoid modifications to files from
  113. the Steinberg SDK, which may have had potential licence issues.
  114. Andrew Baldwin: contributed fixes for compatibility problems with more
  115. recent versions of the gcc assembler.
  116. */
  117. // We only need IASIOThiscallResolver at all if we are on Win32. For other
  118. // platforms we simply bypass the IASIOThiscallResolver definition to allow us
  119. // to be safely #include'd whatever the platform to keep client code portable
  120. #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) && !defined(_WIN64)
  121. // If microsoft compiler we can call IASIO directly so IASIOThiscallResolver
  122. // is not used.
  123. #if !defined(_MSC_VER)
  124. #include <new>
  125. #include <assert.h>
  126. // We have a mechanism in iasiothiscallresolver.h to ensure that asio.h is
  127. // #include'd before it in client code, we do NOT want to do this test here.
  128. #define iasiothiscallresolver_sourcefile 1
  129. #include "iasiothiscallresolver.h"
  130. #undef iasiothiscallresolver_sourcefile
  131. // iasiothiscallresolver.h redefines ASIOInit for clients, but we don't want
  132. // this macro defined in this translation unit.
  133. #undef ASIOInit
  134. // theAsioDriver is a global pointer to the current IASIO instance which the
  135. // ASIO SDK uses to perform all actions on the IASIO interface. We substitute
  136. // our own forwarding interface into this pointer.
  137. extern IASIO* theAsioDriver;
  138. // The following macros define the inline assembler for BORLAND first then gcc
  139. #if defined(__BCPLUSPLUS__) || defined(__BORLANDC__)
  140. #define CALL_THISCALL_0( resultName, thisPtr, funcOffset )\
  141. void *this_ = (thisPtr); \
  142. __asm { \
  143. mov ecx, this_ ; \
  144. mov eax, [ecx] ; \
  145. call [eax+funcOffset] ; \
  146. mov resultName, eax ; \
  147. }
  148. #define CALL_VOID_THISCALL_1( thisPtr, funcOffset, param1 )\
  149. void *this_ = (thisPtr); \
  150. __asm { \
  151. mov eax, param1 ; \
  152. push eax ; \
  153. mov ecx, this_ ; \
  154. mov eax, [ecx] ; \
  155. call [eax+funcOffset] ; \
  156. }
  157. #define CALL_THISCALL_1( resultName, thisPtr, funcOffset, param1 )\
  158. void *this_ = (thisPtr); \
  159. __asm { \
  160. mov eax, param1 ; \
  161. push eax ; \
  162. mov ecx, this_ ; \
  163. mov eax, [ecx] ; \
  164. call [eax+funcOffset] ; \
  165. mov resultName, eax ; \
  166. }
  167. #define CALL_THISCALL_1_DOUBLE( resultName, thisPtr, funcOffset, param1 )\
  168. void *this_ = (thisPtr); \
  169. void *doubleParamPtr_ (&param1); \
  170. __asm { \
  171. mov eax, doubleParamPtr_ ; \
  172. push [eax+4] ; \
  173. push [eax] ; \
  174. mov ecx, this_ ; \
  175. mov eax, [ecx] ; \
  176. call [eax+funcOffset] ; \
  177. mov resultName, eax ; \
  178. }
  179. #define CALL_THISCALL_2( resultName, thisPtr, funcOffset, param1, param2 )\
  180. void *this_ = (thisPtr); \
  181. __asm { \
  182. mov eax, param2 ; \
  183. push eax ; \
  184. mov eax, param1 ; \
  185. push eax ; \
  186. mov ecx, this_ ; \
  187. mov eax, [ecx] ; \
  188. call [eax+funcOffset] ; \
  189. mov resultName, eax ; \
  190. }
  191. #define CALL_THISCALL_4( resultName, thisPtr, funcOffset, param1, param2, param3, param4 )\
  192. void *this_ = (thisPtr); \
  193. __asm { \
  194. mov eax, param4 ; \
  195. push eax ; \
  196. mov eax, param3 ; \
  197. push eax ; \
  198. mov eax, param2 ; \
  199. push eax ; \
  200. mov eax, param1 ; \
  201. push eax ; \
  202. mov ecx, this_ ; \
  203. mov eax, [ecx] ; \
  204. call [eax+funcOffset] ; \
  205. mov resultName, eax ; \
  206. }
  207. #elif defined(__GNUC__)
  208. #define CALL_THISCALL_0( resultName, thisPtr, funcOffset ) \
  209. __asm__ __volatile__ ("movl (%1), %%edx\n\t" \
  210. "call *"#funcOffset"(%%edx)\n\t" \
  211. :"=a"(resultName) /* Output Operands */ \
  212. :"c"(thisPtr) /* Input Operands */ \
  213. : "%edx" /* Clobbered Registers */ \
  214. ); \
  215. #define CALL_VOID_THISCALL_1( thisPtr, funcOffset, param1 ) \
  216. __asm__ __volatile__ ("pushl %0\n\t" \
  217. "movl (%1), %%edx\n\t" \
  218. "call *"#funcOffset"(%%edx)\n\t" \
  219. : /* Output Operands */ \
  220. :"r"(param1), /* Input Operands */ \
  221. "c"(thisPtr) \
  222. : "%edx" /* Clobbered Registers */ \
  223. ); \
  224. #define CALL_THISCALL_1( resultName, thisPtr, funcOffset, param1 ) \
  225. __asm__ __volatile__ ("pushl %1\n\t" \
  226. "movl (%2), %%edx\n\t" \
  227. "call *"#funcOffset"(%%edx)\n\t" \
  228. :"=a"(resultName) /* Output Operands */ \
  229. :"r"(param1), /* Input Operands */ \
  230. "c"(thisPtr) \
  231. : "%edx" /* Clobbered Registers */ \
  232. ); \
  233. #define CALL_THISCALL_1_DOUBLE( resultName, thisPtr, funcOffset, param1 ) \
  234. do { \
  235. double param1f64 = param1; /* Cast explicitly to double */ \
  236. double *param1f64Ptr = &param1f64; /* Make pointer to address */ \
  237. __asm__ __volatile__ ("pushl 4(%1)\n\t" \
  238. "pushl (%1)\n\t" \
  239. "movl (%2), %%edx\n\t" \
  240. "call *"#funcOffset"(%%edx);\n\t" \
  241. : "=a"(resultName) /* Output Operands */ \
  242. : "r"(param1f64Ptr), /* Input Operands */ \
  243. "c"(thisPtr), \
  244. "m"(*param1f64Ptr) /* Using address */ \
  245. : "%edx" /* Clobbered Registers */ \
  246. ); \
  247. } while (0); \
  248. #define CALL_THISCALL_2( resultName, thisPtr, funcOffset, param1, param2 ) \
  249. __asm__ __volatile__ ("pushl %1\n\t" \
  250. "pushl %2\n\t" \
  251. "movl (%3), %%edx\n\t" \
  252. "call *"#funcOffset"(%%edx)\n\t" \
  253. :"=a"(resultName) /* Output Operands */ \
  254. :"r"(param2), /* Input Operands */ \
  255. "r"(param1), \
  256. "c"(thisPtr) \
  257. : "%edx" /* Clobbered Registers */ \
  258. ); \
  259. #define CALL_THISCALL_4( resultName, thisPtr, funcOffset, param1, param2, param3, param4 )\
  260. __asm__ __volatile__ ("pushl %1\n\t" \
  261. "pushl %2\n\t" \
  262. "pushl %3\n\t" \
  263. "pushl %4\n\t" \
  264. "movl (%5), %%edx\n\t" \
  265. "call *"#funcOffset"(%%edx)\n\t" \
  266. :"=a"(resultName) /* Output Operands */ \
  267. :"r"(param4), /* Input Operands */ \
  268. "r"(param3), \
  269. "r"(param2), \
  270. "r"(param1), \
  271. "c"(thisPtr) \
  272. : "%edx" /* Clobbered Registers */ \
  273. ); \
  274. #endif
  275. // Our static singleton instance.
  276. IASIOThiscallResolver IASIOThiscallResolver::instance;
  277. // Constructor called to initialize static Singleton instance above. Note that
  278. // it is important not to clear that_ incase it has already been set by the call
  279. // to placement new in ASIOInit().
  280. IASIOThiscallResolver::IASIOThiscallResolver()
  281. {
  282. }
  283. // Constructor called from ASIOInit() below
  284. IASIOThiscallResolver::IASIOThiscallResolver(IASIO* that)
  285. : that_( that )
  286. {
  287. }
  288. // Implement IUnknown methods as assert(false). IASIOThiscallResolver is not
  289. // really a COM object, just a wrapper which will work with the ASIO SDK.
  290. // If you wanted to use ASIO without the SDK you might want to implement COM
  291. // aggregation in these methods.
  292. HRESULT STDMETHODCALLTYPE IASIOThiscallResolver::QueryInterface(REFIID riid, void **ppv)
  293. {
  294. (void)riid; // suppress unused variable warning
  295. assert( false ); // this function should never be called by the ASIO SDK.
  296. *ppv = NULL;
  297. return E_NOINTERFACE;
  298. }
  299. ULONG STDMETHODCALLTYPE IASIOThiscallResolver::AddRef()
  300. {
  301. assert( false ); // this function should never be called by the ASIO SDK.
  302. return 1;
  303. }
  304. ULONG STDMETHODCALLTYPE IASIOThiscallResolver::Release()
  305. {
  306. assert( false ); // this function should never be called by the ASIO SDK.
  307. return 1;
  308. }
  309. // Implement the IASIO interface methods by performing the vptr manipulation
  310. // described above then delegating to the real implementation.
  311. ASIOBool IASIOThiscallResolver::init(void *sysHandle)
  312. {
  313. ASIOBool result;
  314. CALL_THISCALL_1( result, that_, 12, sysHandle );
  315. return result;
  316. }
  317. void IASIOThiscallResolver::getDriverName(char *name)
  318. {
  319. CALL_VOID_THISCALL_1( that_, 16, name );
  320. }
  321. long IASIOThiscallResolver::getDriverVersion()
  322. {
  323. ASIOBool result;
  324. CALL_THISCALL_0( result, that_, 20 );
  325. return result;
  326. }
  327. void IASIOThiscallResolver::getErrorMessage(char *string)
  328. {
  329. CALL_VOID_THISCALL_1( that_, 24, string );
  330. }
  331. ASIOError IASIOThiscallResolver::start()
  332. {
  333. ASIOBool result;
  334. CALL_THISCALL_0( result, that_, 28 );
  335. return result;
  336. }
  337. ASIOError IASIOThiscallResolver::stop()
  338. {
  339. ASIOBool result;
  340. CALL_THISCALL_0( result, that_, 32 );
  341. return result;
  342. }
  343. ASIOError IASIOThiscallResolver::getChannels(long *numInputChannels, long *numOutputChannels)
  344. {
  345. ASIOBool result;
  346. CALL_THISCALL_2( result, that_, 36, numInputChannels, numOutputChannels );
  347. return result;
  348. }
  349. ASIOError IASIOThiscallResolver::getLatencies(long *inputLatency, long *outputLatency)
  350. {
  351. ASIOBool result;
  352. CALL_THISCALL_2( result, that_, 40, inputLatency, outputLatency );
  353. return result;
  354. }
  355. ASIOError IASIOThiscallResolver::getBufferSize(long *minSize, long *maxSize,
  356. long *preferredSize, long *granularity)
  357. {
  358. ASIOBool result;
  359. CALL_THISCALL_4( result, that_, 44, minSize, maxSize, preferredSize, granularity );
  360. return result;
  361. }
  362. ASIOError IASIOThiscallResolver::canSampleRate(ASIOSampleRate sampleRate)
  363. {
  364. ASIOBool result;
  365. CALL_THISCALL_1_DOUBLE( result, that_, 48, sampleRate );
  366. return result;
  367. }
  368. ASIOError IASIOThiscallResolver::getSampleRate(ASIOSampleRate *sampleRate)
  369. {
  370. ASIOBool result;
  371. CALL_THISCALL_1( result, that_, 52, sampleRate );
  372. return result;
  373. }
  374. ASIOError IASIOThiscallResolver::setSampleRate(ASIOSampleRate sampleRate)
  375. {
  376. ASIOBool result;
  377. CALL_THISCALL_1_DOUBLE( result, that_, 56, sampleRate );
  378. return result;
  379. }
  380. ASIOError IASIOThiscallResolver::getClockSources(ASIOClockSource *clocks, long *numSources)
  381. {
  382. ASIOBool result;
  383. CALL_THISCALL_2( result, that_, 60, clocks, numSources );
  384. return result;
  385. }
  386. ASIOError IASIOThiscallResolver::setClockSource(long reference)
  387. {
  388. ASIOBool result;
  389. CALL_THISCALL_1( result, that_, 64, reference );
  390. return result;
  391. }
  392. ASIOError IASIOThiscallResolver::getSamplePosition(ASIOSamples *sPos, ASIOTimeStamp *tStamp)
  393. {
  394. ASIOBool result;
  395. CALL_THISCALL_2( result, that_, 68, sPos, tStamp );
  396. return result;
  397. }
  398. ASIOError IASIOThiscallResolver::getChannelInfo(ASIOChannelInfo *info)
  399. {
  400. ASIOBool result;
  401. CALL_THISCALL_1( result, that_, 72, info );
  402. return result;
  403. }
  404. ASIOError IASIOThiscallResolver::createBuffers(ASIOBufferInfo *bufferInfos,
  405. long numChannels, long bufferSize, ASIOCallbacks *callbacks)
  406. {
  407. ASIOBool result;
  408. CALL_THISCALL_4( result, that_, 76, bufferInfos, numChannels, bufferSize, callbacks );
  409. return result;
  410. }
  411. ASIOError IASIOThiscallResolver::disposeBuffers()
  412. {
  413. ASIOBool result;
  414. CALL_THISCALL_0( result, that_, 80 );
  415. return result;
  416. }
  417. ASIOError IASIOThiscallResolver::controlPanel()
  418. {
  419. ASIOBool result;
  420. CALL_THISCALL_0( result, that_, 84 );
  421. return result;
  422. }
  423. ASIOError IASIOThiscallResolver::future(long selector,void *opt)
  424. {
  425. ASIOBool result;
  426. CALL_THISCALL_2( result, that_, 88, selector, opt );
  427. return result;
  428. }
  429. ASIOError IASIOThiscallResolver::outputReady()
  430. {
  431. ASIOBool result;
  432. CALL_THISCALL_0( result, that_, 92 );
  433. return result;
  434. }
  435. // Implement our substitute ASIOInit() method
  436. ASIOError IASIOThiscallResolver::ASIOInit(ASIODriverInfo *info)
  437. {
  438. // To ensure that our instance's vptr is correctly constructed, even if
  439. // ASIOInit is called prior to main(), we explicitly call its constructor
  440. // (potentially over the top of an existing instance). Note that this is
  441. // pretty ugly, and is only safe because IASIOThiscallResolver has no
  442. // destructor and contains no objects with destructors.
  443. new((void*)&instance) IASIOThiscallResolver( theAsioDriver );
  444. // Interpose between ASIO client code and the real driver.
  445. theAsioDriver = &instance;
  446. // Note that we never need to switch theAsioDriver back to point to the
  447. // real driver because theAsioDriver is reset to zero in ASIOExit().
  448. // Delegate to the real ASIOInit
  449. return ::ASIOInit(info);
  450. }
  451. #endif /* !defined(_MSC_VER) */
  452. #endif /* Win32 */