diff --git a/include/internal/amissl.h b/include/internal/amissl.h index 1e6d29f63..5df90f056 100644 --- a/include/internal/amissl.h +++ b/include/internal/amissl.h @@ -45,8 +45,10 @@ typedef struct { APTR stack; struct tm localtime_var; struct Library *SocketBase; +#if !defined(__amigaos4__) && !defined(__MORPHOS__) LONG TCPIPStackType; struct MLinkLock *MLinkLock; // This is really ancient, but ib still supports it so... +#endif #ifdef __amigaos4__ struct SocketIFace *ISocket; struct SocketIFace **ISocketPtr; @@ -64,4 +66,18 @@ STDARGS int GetAmiSSLerrno(void); #define SETUPSTATE() AMISSL_STATE *state = GetAmiSSLState() +#if defined(__amigaos4__) +# define LOCK_INIT(mutex) ((mutex = AllocSysObject(ASOT_MUTEX, NULL)) != NULL) +# define LOCK_FREE(mutex) FreeSysObject(ASOT_MUTEX, mutex) +# define LOCK_OBTAIN(mutex) MutexObtain(mutex) +# define LOCK_RELEASE(mutex) MutexRelease(mutex) +# define LOCK_DECLARE(mutex) APTR mutex +#else +# define LOCK_INIT(sem) (InitSemaphore(&(sem)), TRUE) +# define LOCK_FREE(sem) while(0) +# define LOCK_OBTAIN(sem) ObtainSemaphore(&(sem)) +# define LOCK_RELEASE(sem) ReleaseSemaphore(&(sem)) +# define LOCK_DECLARE(sem) struct SignalSemaphore sem +#endif + #endif /* !INTERNAL_AMISSL_H */ diff --git a/libcmt/fflush.c b/libcmt/fflush.c index 616872779..9f0dee665 100644 --- a/libcmt/fflush.c +++ b/libcmt/fflush.c @@ -8,7 +8,7 @@ #include extern struct MinList __filelist; -extern struct SignalSemaphore __filelist_cs; +extern LOCK_DECLARE(__filelist_cs); int __fflush(FILE *stream) /* fflush exactly one file */ { @@ -47,7 +47,7 @@ int fflush(FILE *stream) /* fflush one or all files */ if(TOFILE(stream)!=NULL) return __fflush(stream); retval=0; - ObtainSemaphore(&__filelist_cs); + LOCK_OBTAIN(__filelist_cs); node=__filelist.mlh_Head; while((nextnode=node->mln_Succ)!=NULL) { @@ -55,6 +55,6 @@ int fflush(FILE *stream) /* fflush one or all files */ retval=EOF; node=nextnode; } - ReleaseSemaphore(&__filelist_cs); + LOCK_RELEASE(__filelist_cs); return retval; } diff --git a/libcmt/file.c b/libcmt/file.c index 847aa95bb..f8a752268 100644 --- a/libcmt/file.c +++ b/libcmt/file.c @@ -10,7 +10,8 @@ #include "libcmt.h" extern struct MinList __filelist; /* list of open files (fflush() needs also access) */ -extern struct SignalSemaphore __filelist_cs; + +extern LOCK_DECLARE(__filelist_cs); FILE *freopen(const char *filename,const char *mode,FILE *stream) { @@ -119,9 +120,9 @@ FILE *fopen(const char *name, const char *mode) node->FILE._flag |= _IOALLOCBUF; if(freopen(name,mode,(FILE *)&node->FILE)!=NULL) { - ObtainSemaphore(&__filelist_cs); + LOCK_OBTAIN(__filelist_cs); AddHead((struct List *)&__filelist,(struct Node *)&node->node); - ReleaseSemaphore(&__filelist_cs); + LOCK_RELEASE(__filelist_cs); return (FILE *)&node->FILE; } free(node->FILE._base); @@ -146,9 +147,9 @@ int fclose(FILE *file) node = (struct filenode *)(((BYTE *)file)-offsetof(struct filenode,FILE)); - ObtainSemaphore(&__filelist_cs); + LOCK_OBTAIN(__filelist_cs); Remove((struct Node *)node); - ReleaseSemaphore(&__filelist_cs); + LOCK_RELEASE(__filelist_cs); Close(TOFILE(file)->_file); diff --git a/libcmt/free.c b/libcmt/free.c index da36f766a..ff7859519 100644 --- a/libcmt/free.c +++ b/libcmt/free.c @@ -2,7 +2,7 @@ AmiSSL - OpenSSL wrapper for AmigaOS-based systems Copyright (c) 1999-2006 Andrija Antonijevic, Stefan Burstroem. - Copyright (c) 2006-2022 AmiSSL Open Source Team. + Copyright (c) 2006-2023 AmiSSL Open Source Team. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); @@ -44,10 +44,16 @@ void free(void *p) SHOWPOINTER(DBF_STARTUP, &__mem_pool); #endif + #if !defined(__amigaos4__) ObtainSemaphore(&__mem_cs); + #endif + p = &((ULONG *)p)[-1]; FreePooled(__mem_pool, p, *(ULONG *)p); + + #if !defined(__amigaos4__) ReleaseSemaphore(&__mem_cs); + #endif } #if 0 diff --git a/libcmt/libcmt.c b/libcmt/libcmt.c index 23b4d017d..c8273f1ca 100644 --- a/libcmt/libcmt.c +++ b/libcmt/libcmt.c @@ -2,7 +2,7 @@ AmiSSL - OpenSSL wrapper for AmigaOS-based systems Copyright (c) 1999-2006 Andrija Antonijevic, Stefan Burstroem. - Copyright (c) 2006-2022 AmiSSL Open Source Team. + Copyright (c) 2006-2023 AmiSSL Open Source Team. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); @@ -43,8 +43,10 @@ struct Library *LocaleBase = NULL; // global variables throughout libcmt struct MinList __filelist; /* list of open files (fflush() needs also access) */ -struct SignalSemaphore __filelist_cs; +LOCK_DECLARE(__filelist_cs); +#if !defined(__amigaos4__) struct SignalSemaphore __mem_cs; +#endif void *__mem_pool = NULL; ULONG __clock_base = 0; LONG __gmt_offset = 0; @@ -72,16 +74,13 @@ static int timezone_get_offset(void) #define timezone_get_offset() 0 #endif -void __init_libcmt(void) +int __init_libcmt(void) { ENTER(); // initialize memory stuff - SHOWPOINTER(DBF_STARTUP, &__mem_cs); SHOWPOINTER(DBF_STARTUP, &__mem_pool); - InitSemaphore(&__mem_cs); - #if defined(__amigaos4__) __mem_pool = AllocSysObjectTags(ASOT_MEMPOOL, ASOPOOL_MFlags, MEMF_SHARED, @@ -89,8 +88,12 @@ void __init_libcmt(void) ASOPOOL_Puddle, 8192, ASOPOOL_Threshold, 4096, ASOPOOL_Name, "AmiSSL", + ASOPOOL_Protected, TRUE, TAG_DONE); #else + SHOWPOINTER(DBF_STARTUP, &__mem_cs); + InitSemaphore(&__mem_cs); + __mem_pool = CreatePool(MEMF_ANY, 8192, 4096); #endif @@ -98,7 +101,11 @@ void __init_libcmt(void) SHOWPOINTER(DBF_STARTUP, &__filelist); SHOWPOINTER(DBF_STARTUP, &__filelist_cs); NewList((struct List *)&__filelist); - InitSemaphore(&__filelist_cs); + if(__mem_pool == NULL || !LOCK_INIT(__filelist_cs)) + { + __free_libcmt(); + return 0; + } // initialize clock/locale stuff SHOWPOINTER(DBF_STARTUP, &__clock_base); @@ -136,6 +143,8 @@ void __init_libcmt(void) E(DBF_STARTUP, "ERROR on OpenLibrary()"); LEAVE(); + + return 1; } void __free_libcmt(void) @@ -153,6 +162,8 @@ void __free_libcmt(void) CloseLibrary((struct Library *)LocaleBase); LocaleBase = NULL; + LOCK_FREE(__filelist_cs); + SHOWPOINTER(DBF_STARTUP, &__mem_pool); // free memory related stuff #ifdef __amigaos4__ diff --git a/libcmt/libcmt.h b/libcmt/libcmt.h index dc6da1456..57492f0db 100644 --- a/libcmt/libcmt.h +++ b/libcmt/libcmt.h @@ -5,7 +5,7 @@ AmiSSL - OpenSSL wrapper for AmigaOS-based systems Copyright (c) 1999-2006 Andrija Antonijevic, Stefan Burstroem. - Copyright (c) 2006-2022 AmiSSL Open Source Team. + Copyright (c) 2006-2023 AmiSSL Open Source Team. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); @@ -33,7 +33,7 @@ extern int __io2errno(int); extern void initialize_socket_errno(AMISSL_STATE *state); -extern void __init_libcmt(void); +extern int __init_libcmt(void); extern void __free_libcmt(void); /****************************************************************************/ diff --git a/libcmt/malloc.c b/libcmt/malloc.c index e256bbca8..293320282 100644 --- a/libcmt/malloc.c +++ b/libcmt/malloc.c @@ -2,7 +2,7 @@ AmiSSL - OpenSSL wrapper for AmigaOS-based systems Copyright (c) 1999-2006 Andrija Antonijevic, Stefan Burstroem. - Copyright (c) 2006-2022 AmiSSL Open Source Team. + Copyright (c) 2006-2023 AmiSSL Open Source Team. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); @@ -43,13 +43,19 @@ malloc( SHOWPOINTER(DBF_STARTUP, &__mem_cs); SHOWPOINTER(DBF_STARTUP, &__mem_pool); #endif - + + #if !defined(__amigaos4__) ObtainSemaphore(&__mem_cs); + #endif + n += sizeof(*p); p = AllocPooled(__mem_pool, n); if (p) *p++ = n; + + #if !defined(__amigaos4__) ReleaseSemaphore(&__mem_cs); + #endif #if 0 RETURN(p); diff --git a/src/amissl_base.h b/src/amissl_base.h index b5c60b8a9..92f8d7b3b 100644 --- a/src/amissl_base.h +++ b/src/amissl_base.h @@ -2,7 +2,7 @@ AmiSSL - OpenSSL wrapper for AmigaOS-based systems Copyright (c) 1999-2006 Andrija Antonijevic, Stefan Burstroem. - Copyright (c) 2006-2022 AmiSSL Open Source Team. + Copyright (c) 2006-2023 AmiSSL Open Source Team. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); @@ -62,7 +62,7 @@ struct LibraryHeader #if defined(__MORPHOS__) struct Library *sysBase; #endif - struct SignalSemaphore libSem; + LOCK_DECLARE( libLock); #if !defined(__amigaos4__) || !defined(NO_VECTABLE68K) struct LibraryHeader *extBase; @@ -78,7 +78,7 @@ struct LibraryHeader // for the parent libbase and which // are thus global throughout all libbases // created by AmiSSL. - struct SignalSemaphore openssl_cs; + LOCK_DECLARE(openssl_cs); struct HashTable *thread_hash; ULONG LastThreadGroupID; }; diff --git a/src/amissl_glue.c b/src/amissl_glue.c index f2242c48a..5681fcf61 100644 --- a/src/amissl_glue.c +++ b/src/amissl_glue.c @@ -23,6 +23,7 @@ //#error insert the correct offset to let the __restore_a4 function restore the correct base relative data segment below and remove this warning // restore the base relative data segment from the library base address in register a6 // this function will be called from all non-static functions in this module if it is built with -mrestore-a4 +#include #include "amissl_base.h" static const USED_VAR unsigned short __restore_a4[] = { 0x286e, OFFSET(LibraryHeader, dataSeg), 0x4e75 }; // "move.l a6@(dataSeg:w),a4;rts" #elif defined(__MORPHOS__) diff --git a/src/amissl_glue.patch b/src/amissl_glue.patch index 8da792161..b65fe96aa 100644 --- a/src/amissl_glue.patch +++ b/src/amissl_glue.patch @@ -1,5 +1,5 @@ ---- amissl_glue.c 2022-03-26 13:15:21.414382815 +0000 -+++ amissl_glue.c_patched 2022-03-26 13:15:15.750381865 +0000 +--- amissl_glue.c 2023-12-08 21:21:37.771112599 +0000 ++++ amissl_glue.c_patched 2023-12-08 21:22:36.683115409 +0000 @@ -4,7 +4,13 @@ /***************************************************************************/ @@ -14,7 +14,7 @@ /***************************************************************************/ -@@ -14,11 +20,24 @@ +@@ -14,11 +20,25 @@ #undef SAVEDS #define SAVEDS __attribute__((baserel_restore)) __attribute__ ((noinline)) #elif defined(__amigaos3__) @@ -22,6 +22,7 @@ +//#error insert the correct offset to let the __restore_a4 function restore the correct base relative data segment below and remove this warning // restore the base relative data segment from the library base address in register a6 // this function will be called from all non-static functions in this module if it is built with -mrestore-a4 ++#include +#include "amissl_base.h" static const USED_VAR unsigned short __restore_a4[] = { 0x286e, OFFSET(LibraryHeader, dataSeg), 0x4e75 }; // "move.l a6@(dataSeg:w),a4;rts" -#endif // __amigaos3__ @@ -41,7 +42,7 @@ #endif // BASEREL /***************************************************************************/ -@@ -30,24 +49,30 @@ +@@ -30,24 +50,30 @@ // --- @@ -72,7 +73,7 @@ // --- -@@ -15369,6 +15394,8 @@ +@@ -15369,6 +15395,8 @@ // --- @@ -81,7 +82,7 @@ int SAVEDS ASM LIB_UI_read_string_lib(REG(a6, UNUSED __IFACE_OR_BASE), REG(a0, UI * ui), REG(a1, UI_STRING * uis)) { return UI_read_string_lib(ui, uis); -@@ -15376,6 +15403,8 @@ +@@ -15376,6 +15404,8 @@ // --- @@ -90,7 +91,7 @@ int SAVEDS ASM LIB_UI_write_string_lib(REG(a6, UNUSED __IFACE_OR_BASE), REG(a0, UI * ui), REG(a1, UI_STRING * uis)) { return UI_write_string_lib(ui, uis); -@@ -26466,7 +26495,13 @@ +@@ -26466,7 +26496,13 @@ int SAVEDS ASM LIB_OBSOLETE_RSA_X931_derive_ex(REG(a6, UNUSED __IFACE_OR_BASE), REG(a0, RSA * rsa), REG(a1, BIGNUM * p1), REG(a2, BIGNUM * p2), REG(a3, BIGNUM * q1), REG(d0, BIGNUM * q2), REG(d1, const BIGNUM * Xp1), REG(d2, const BIGNUM * Xp2), REG(d3, const BIGNUM * Xp), REG(d4, const BIGNUM * Xq1), REG(d5, const BIGNUM * Xq2), REG(d6, const BIGNUM * Xq), REG(d7, const BIGNUM * e), REG(a4, BN_GENCB * cb)) { diff --git a/src/amissl_init.c b/src/amissl_init.c index 46af595ca..2b3fa9f66 100644 --- a/src/amissl_init.c +++ b/src/amissl_init.c @@ -2,7 +2,7 @@ AmiSSL - OpenSSL wrapper for AmigaOS-based systems Copyright (c) 1999-2006 Andrija Antonijevic, Stefan Burstroem. - Copyright (c) 2006-2022 AmiSSL Open Source Team. + Copyright (c) 2006-2023 AmiSSL Open Source Team. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); @@ -78,7 +78,11 @@ ULONG freeBase(UNUSED struct LibraryHeader *lib) SHOWPOINTER(DBF_STARTUP, &lib->thread_hash); SHOWPOINTER(DBF_STARTUP, lib->thread_hash); - h_free(lib->thread_hash); + + if(lib->thread_hash) + h_free(lib->thread_hash); + + LOCK_FREE(lib->openssl_cs); RETURN(TRUE); return TRUE; @@ -88,6 +92,8 @@ ULONG freeBase(UNUSED struct LibraryHeader *lib) ULONG initBase(UNUSED struct LibraryHeader *lib) { + ULONG success; + ENTER(); SHOWPOINTER(DBF_STARTUP, SysBase); @@ -97,7 +103,6 @@ ULONG initBase(UNUSED struct LibraryHeader *lib) // initialize the global variables of the parent libbase SHOWPOINTER(DBF_STARTUP, &lib->openssl_cs); - InitSemaphore(&lib->openssl_cs); lib->thread_hash = h_new(7, h_allocfunc, h_freefunc); SHOWPOINTER(DBF_STARTUP, &lib->thread_hash); @@ -107,10 +112,12 @@ ULONG initBase(UNUSED struct LibraryHeader *lib) SHOWPOINTER(DBF_STARTUP, &lib->LastThreadGroupID); SHOWVALUE(DBF_STARTUP, lib->LastThreadGroupID); + success = (lib->thread_hash != NULL) && LOCK_INIT(lib->openssl_cs); + D(DBF_STARTUP, "-------------------------------"); - RETURN(TRUE); - return TRUE; + RETURN(success); + return success; } /***********************************************************************/ diff --git a/src/amissl_libinit.c b/src/amissl_libinit.c index a7ef88aa3..45a0dcb21 100644 --- a/src/amissl_libinit.c +++ b/src/amissl_libinit.c @@ -2,7 +2,7 @@ AmiSSL - OpenSSL wrapper for AmigaOS-based systems Copyright (c) 1999-2006 Andrija Antonijevic, Stefan Burstroem. - Copyright (c) 2006-2022 AmiSSL Open Source Team. + Copyright (c) 2006-2023 AmiSSL Open Source Team. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); @@ -37,6 +37,9 @@ #include #endif +#include +#include + // #include "amissl_lib_protos.h" @@ -53,9 +56,6 @@ // -#include -#include - #include extern struct LibraryHeader *CreateExtLibrary(struct LibraryHeader *main); @@ -769,11 +769,11 @@ struct LibraryHeader * LIBFUNC LibInit(REG(d0, struct LibraryHeader *base), REG( D(DBF_STARTUP, "LibInit()"); #endif - InitSemaphore(&base->libSem); - + if(LOCK_INIT(base->libLock) #if defined(MULTIBASE) - if((base->parent = InitMultiBase(base))) + && (base->parent = InitMultiBase(base)) #endif /* MULTIBASE */ + ) { #if defined(__amigaos3__) && defined(__CLIB2__) __MathIeeeDoubBasBase = MathIeeeDoubBasBase = OpenLibrary("mathieeedoubbas.library", 37); @@ -820,6 +820,8 @@ struct LibraryHeader * LIBFUNC LibInit(REG(d0, struct LibraryHeader *base), REG( } } + LOCK_FREE(base->libLock); + #if defined(__amigaos4__) && defined(__NEWLIB__) if(NewlibBase) { @@ -876,6 +878,8 @@ STATIC BPTR LibDelete(struct LibraryHeader *base) SHOWPOINTER(DBF_STARTUP, SysBase); SHOWPOINTER(DBF_STARTUP, base); + LOCK_FREE(base->libLock); + // remove the library base from exec's lib list in advance Remove((struct Node *)base); @@ -1013,7 +1017,7 @@ struct LibraryHeader * LIBFUNC LibOpen(REG(d0, UNUSED ULONG version), REG(a6, st base->libBase.lib_Flags &= ~LIBF_DELEXP; // protect - ObtainSemaphore(&base->libSem); + LOCK_OBTAIN(base->libLock); #if defined(MULTIBASE) #if defined(__amigaos4__) @@ -1038,12 +1042,6 @@ struct LibraryHeader * LIBFUNC LibOpen(REG(d0, UNUSED ULONG version), REG(a6, st if(child != NULL) { - #if defined(__amigaos3__) - unsigned char *dataSeg; - LONG *relocs; - LONG numRelocs; - #endif - #if defined(__amigaos3__) // CreateLibrary/LibInit has already done this for OS4 // lets clone the child library header child->libBase.lib_Node.ln_Type = NT_LIBRARY; @@ -1055,7 +1053,21 @@ struct LibraryHeader * LIBFUNC LibOpen(REG(d0, UNUSED ULONG version), REG(a6, st child->libBase.lib_IdString = base->libBase.lib_IdString; #endif - InitSemaphore(&child->libSem); + if(!LOCK_INIT(child->libLock)) + { + DeleteLibrary(&child->libBase); + child = NULL; + } + } + + if(child != NULL) + { + #if defined(__amigaos3__) + unsigned char *dataSeg; + LONG *relocs; + LONG numRelocs; + #endif + child->parent = base; // initialize the user variables to their default values @@ -1210,7 +1222,7 @@ struct LibraryHeader * LIBFUNC LibOpen(REG(d0, UNUSED ULONG version), REG(a6, st #endif // MULTIBASE // unprotect - ReleaseSemaphore(&base->libSem); + LOCK_RELEASE(base->libLock); #if defined(DEBUG) SHOWPOINTER(DBF_STARTUP, SysBase); @@ -1253,13 +1265,13 @@ BPTR LIBFUNC LibClose(REG(a6, struct LibraryHeader *base)) #endif // free all our private data and stuff. - ObtainSemaphore(&base->libSem); + LOCK_OBTAIN(base->libLock); // make sure we have enough stack here callLibFunction(closeBase, base); // unprotect - ReleaseSemaphore(&base->libSem); + LOCK_RELEASE(base->libLock); // decrease the open counter base->libBase.lib_OpenCnt--; @@ -1274,7 +1286,11 @@ BPTR LIBFUNC LibClose(REG(a6, struct LibraryHeader *base)) #if defined(__amigaos4__) struct ExtendedLibrary *extlib = (struct ExtendedLibrary *)((ULONG)base + base->libBase.lib_PosSize); #endif + #endif + LOCK_FREE(base->libLock); + + #ifdef MULTIBASE DeleteLibrary(&base->extBase->libBase); /* release child base */ diff --git a/src/amissl_library.c b/src/amissl_library.c index 6b868ea2a..5416f5a3b 100644 --- a/src/amissl_library.c +++ b/src/amissl_library.c @@ -2,7 +2,7 @@ AmiSSL - OpenSSL wrapper for AmigaOS-based systems Copyright (c) 1999-2006 Andrija Antonijevic, Stefan Burstroem. - Copyright (c) 2006-2022 AmiSSL Open Source Team. + Copyright (c) 2006-2023 AmiSSL Open Source Team. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); @@ -142,7 +142,7 @@ static AMISSL_STATE *CreateAmiSSLState(void) ENTER(); - ObtainSemaphore(&parentBase->openssl_cs); + LOCK_OBTAIN(parentBase->openssl_cs); SHOWPOINTER(DBF_STARTUP, &ownBase); SHOWPOINTER(DBF_STARTUP, ownBase); @@ -179,7 +179,7 @@ static AMISSL_STATE *CreateAmiSSLState(void) } } - ReleaseSemaphore(&parentBase->openssl_cs); + LOCK_RELEASE(parentBase->openssl_cs); SHOWPOINTER(DBF_STARTUP, ret); SHOWPOINTER(DBF_STARTUP, SysBase); @@ -384,10 +384,10 @@ LIBPROTO(CleanupAmiSSLA, LONG, REG(a6, UNUSED __BASE_OR_IFACE), REG(a0, UNUSED s } #endif - ObtainSemaphore(&parentBase->openssl_cs); + LOCK_OBTAIN(parentBase->openssl_cs); D(DBF_STARTUP, "h_delete(parentBase->thread_hash)"); h_delete(parentBase->thread_hash, state->pid); - ReleaseSemaphore(&parentBase->openssl_cs); + LOCK_RELEASE(parentBase->openssl_cs); if(state->getenv_var) free(state->getenv_var); @@ -437,10 +437,10 @@ LIBPROTO(__UserLibCleanup, void, REG(a6, UNUSED __BASE_OR_IFACE), REG(a0, struct if(libBase->parent->thread_hash) { D(DBF_STARTUP, "Performing unfreed states cleanup for %08lx (group %lu)", FindTask(NULL), libBase->ThreadGroupID); - ObtainSemaphore(&libBase->parent->openssl_cs); + LOCK_OBTAIN(libBase->parent->openssl_cs); D(DBF_STARTUP, "h_doall(thread_hash)"); h_doall(libBase->parent->thread_hash, (void (*)(long, void *))ThreadGroupStateCleanup); - ReleaseSemaphore(&libBase->parent->openssl_cs); + LOCK_RELEASE(libBase->parent->openssl_cs); } else W(DBF_STARTUP, "No thread_hash"); @@ -496,10 +496,11 @@ LIBPROTO(__UserLibInit, int, REG(a6, __BASE_OR_IFACE), REG(a0, struct LibraryHea // lets set the parent of libBase as our parentBase parentBase = libBase->parent; SHOWPOINTER(DBF_STARTUP, &parentBase); - SHOWPOINTER(DBF_STARTUP, parentBase), + SHOWPOINTER(DBF_STARTUP, parentBase); // we have to initialize the libcmt stuff - __init_libcmt(); + if (!__init_libcmt()) + return err; D(DBF_STARTUP, "Global parentBase variables:"); D(DBF_STARTUP, "---------------------------"); @@ -510,13 +511,13 @@ LIBPROTO(__UserLibInit, int, REG(a6, __BASE_OR_IFACE), REG(a0, struct LibraryHea SHOWVALUE(DBF_STARTUP, parentBase->LastThreadGroupID); D(DBF_STARTUP, "---------------------------"); - ObtainSemaphore(&parentBase->openssl_cs); + LOCK_OBTAIN(parentBase->openssl_cs); ownBase->ThreadGroupID = ++(parentBase->LastThreadGroupID); SHOWPOINTER(DBF_STARTUP, &ownBase->ThreadGroupID); SHOWVALUE(DBF_STARTUP, ownBase->ThreadGroupID); - ReleaseSemaphore(&parentBase->openssl_cs); + LOCK_RELEASE(parentBase->openssl_cs); if (CRYPTO_THREAD_setup() #if defined(__amigaos4__) diff --git a/src/amissl_norestore.c b/src/amissl_norestore.c index 1350725e7..77bb27cda 100644 --- a/src/amissl_norestore.c +++ b/src/amissl_norestore.c @@ -2,7 +2,7 @@ AmiSSL - OpenSSL wrapper for AmigaOS-based systems Copyright (c) 1999-2006 Andrija Antonijevic, Stefan Burstroem. - Copyright (c) 2006-2022 AmiSSL Open Source Team. + Copyright (c) 2006-2023 AmiSSL Open Source Team. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); @@ -62,10 +62,10 @@ STDARGS AMISSL_STATE *GetAmiSSLState(void) SHOWPOINTER(DBF_BASEREL, SysBase); SHOWPOINTER(DBF_BASEREL, &parentBase->openssl_cs); - ObtainSemaphore(&parentBase->openssl_cs); + LOCK_OBTAIN(parentBase->openssl_cs); D(DBF_BASEREL, "h_find(parentBase->thread_hash=%08lx)", parentBase->thread_hash); ret = (AMISSL_STATE *)h_find(parentBase->thread_hash, (long)FindTask(NULL)); - ReleaseSemaphore(&parentBase->openssl_cs); + LOCK_RELEASE(parentBase->openssl_cs); RETURN(ret); return ret; diff --git a/src/amisslext_libinit.c b/src/amisslext_libinit.c index 593272996..5a129c155 100644 --- a/src/amisslext_libinit.c +++ b/src/amisslext_libinit.c @@ -2,7 +2,7 @@ AmiSSL - OpenSSL wrapper for AmigaOS-based systems Copyright (c) 1999-2006 Andrija Antonijevic, Stefan Burstroem. - Copyright (c) 2006-2022 AmiSSL Open Source Team. + Copyright (c) 2006-2023 AmiSSL Open Source Team. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); @@ -27,6 +27,8 @@ #include #include +#include + // #include "amissl_lib_protos.h" @@ -42,8 +44,6 @@ // -#include - #if defined(__amigaos3__) #if defined(__GNUC__) && __GNUC__ < 3 #define FORCED_CONST const __attribute__ ((section (".text"))) diff --git a/src/amisslmaster_base.h b/src/amisslmaster_base.h index 3c841dd8f..86435594e 100644 --- a/src/amisslmaster_base.h +++ b/src/amisslmaster_base.h @@ -2,7 +2,7 @@ AmiSSL - OpenSSL wrapper for AmigaOS-based systems Copyright (c) 1999-2006 Andrija Antonijevic, Stefan Burstroem. - Copyright (c) 2006-2022 AmiSSL Open Source Team. + Copyright (c) 2006-2023 AmiSSL Open Source Team. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); @@ -60,7 +60,7 @@ struct LibraryHeader BPTR segList; struct Library *sysBase; - struct SignalSemaphore libSem; + LOCK_DECLARE( libLock); }; #endif /* BASE_H */ diff --git a/src/amisslmaster_libinit.c b/src/amisslmaster_libinit.c index b2b4ade00..24c50b442 100644 --- a/src/amisslmaster_libinit.c +++ b/src/amisslmaster_libinit.c @@ -2,7 +2,7 @@ AmiSSL - OpenSSL wrapper for AmigaOS-based systems Copyright (c) 1999-2006 Andrija Antonijevic, Stefan Burstroem. - Copyright (c) 2006-2022 AmiSSL Open Source Team. + Copyright (c) 2006-2023 AmiSSL Open Source Team. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); @@ -32,6 +32,9 @@ #include #endif +#include +#include + #include "amisslmaster_lib_protos.h" #include "amisslmaster_base.h" #include "amisslmaster_glue.h" @@ -40,9 +43,6 @@ #include "amisslmaster_stubs_mos.h" #endif -#include -#include - #include "amissl_rev.h" #if defined(__amigaos4__) @@ -739,11 +739,11 @@ struct LibraryHeader * LIBFUNC LibInit(REG(d0, struct LibraryHeader *base), REG( D(DBF_STARTUP, "LibInit()"); #endif - InitSemaphore(&base->libSem); - + if(LOCK_INIT(base->libLock) #if defined(MULTIBASE) - if((base->parent = InitMultiBase(base))) + && (base->parent = InitMultiBase(base)) #endif /* MULTIBASE */ + ) { // If we are not running on AmigaOS4 (no stackswap required) we go and // do an explicit StackSwap() in case the user wants to make sure we @@ -774,6 +774,8 @@ struct LibraryHeader * LIBFUNC LibInit(REG(d0, struct LibraryHeader *base), REG( } } + LOCK_FREE(base->libLock); + #if defined(__amigaos4__) && defined(__NEWLIB__) if(NewlibBase) { @@ -831,6 +833,8 @@ STATIC BPTR LibDelete(struct LibraryHeader *base) SHOWPOINTER(DBF_STARTUP, SysBase); SHOWPOINTER(DBF_STARTUP, base); + LOCK_FREE(base->libLock); + // remove the library base from exec's lib list in advance Remove((struct Node *)base); @@ -968,7 +972,7 @@ struct LibraryHeader * LIBFUNC LibOpen(REG(d0, UNUSED ULONG version), REG(a6, st base->libBase.lib_Flags &= ~LIBF_DELEXP; // protect - ObtainSemaphore(&base->libSem); + LOCK_OBTAIN(base->libLock); #if defined(MULTIBASE) #if defined(__amigaos4__) @@ -993,12 +997,6 @@ struct LibraryHeader * LIBFUNC LibOpen(REG(d0, UNUSED ULONG version), REG(a6, st if(child != NULL) { - #if defined(__amigaos3__) - unsigned char *dataSeg; - ULONG *relocs; - ULONG numRelocs; - #endif - #if defined(__amigaos3__) // CreateLibrary/LibInit has already done this // lets clone the child library header child->libBase.lib_Node.ln_Type = NT_LIBRARY; @@ -1010,9 +1008,23 @@ struct LibraryHeader * LIBFUNC LibOpen(REG(d0, UNUSED ULONG version), REG(a6, st child->libBase.lib_IdString = base->libBase.lib_IdString; #endif - InitSemaphore(&child->libSem); child->parent = base; + if (!LOCK_INIT(child->libLock)) + { + DeleteLibrary(&child->libBase); + child = NULL; + } + } + + if(child != NULL) + { + #if defined(__amigaos3__) + unsigned char *dataSeg; + ULONG *relocs; + ULONG numRelocs; + #endif + #if defined(__amigaos4__) { uint32 offset; @@ -1145,7 +1157,7 @@ struct LibraryHeader * LIBFUNC LibOpen(REG(d0, UNUSED ULONG version), REG(a6, st #endif // MULTIBASE // unprotect - ReleaseSemaphore(&base->libSem); + LOCK_RELEASE(base->libLock); #if defined(DEBUG) SHOWPOINTER(DBF_STARTUP, SysBase); @@ -1187,13 +1199,13 @@ BPTR LIBFUNC LibClose(REG(a6, struct LibraryHeader *base)) #endif // free all our private data and stuff. - ObtainSemaphore(&base->libSem); + LOCK_OBTAIN(base->libLock); // make sure we have enough stack here callLibFunction(closeBase, base); // unprotect - ReleaseSemaphore(&base->libSem); + LOCK_RELEASE(base->libLock); // decrease the open counter base->libBase.lib_OpenCnt--; @@ -1205,10 +1217,16 @@ BPTR LIBFUNC LibClose(REG(a6, struct LibraryHeader *base)) #ifdef MULTIBASE struct LibraryHeader *parent = base->parent; BOOL exp_parent = (parent->libBase.lib_Flags & LIBF_DELEXP) != 0 ? 1 : 0; + #if defined(__amigaos4__) + struct ExtendedLibrary *extlib = (struct ExtendedLibrary *)((ULONG)base + base->libBase.lib_PosSize); + #endif + #endif + + LOCK_FREE(base->libLock); + #ifdef MULTIBASE /* release child base */ #if defined(__amigaos4__) - struct ExtendedLibrary *extlib = (struct ExtendedLibrary *)((ULONG)base + base->libBase.lib_PosSize); LIB___UserLibCleanup((struct AmiSSLMasterIFace *)extlib->MainIFace); (parent->IElf->FreeDataSegmentCopy)(parent->elfHandle, base->baserelData); base->baserelData = NULL; diff --git a/src/amisslmaster_library.c b/src/amisslmaster_library.c index 05343a704..cf2c3bcd4 100644 --- a/src/amisslmaster_library.c +++ b/src/amisslmaster_library.c @@ -40,6 +40,7 @@ #include #include +#include #ifdef __amigaos4__ #include @@ -74,7 +75,7 @@ LONG LibAPIVersion = AMISSL_CURRENT_VERSION; LONG LibUsesOpenSSLStructs = 0; LONG AmiSSLInitialised = FALSE; -struct SignalSemaphore AmiSSLMasterLock; +LOCK_DECLARE(AmiSSLMasterLock); struct AmiSSLInitStruct amisslinit; /* Keep them here so we know which ciphers we were able to open this time */ @@ -231,7 +232,7 @@ LIBPROTO(OpenAmiSSL, struct Library *, REG(a6, UNUSED __BASE_OR_IFACE)) SHOWPOINTER(DBF_STARTUP, SysBase); SHOWPOINTER(DBF_STARTUP, &AmiSSLMasterLock); - ObtainSemaphore(&AmiSSLMasterLock); + LOCK_OBTAIN(AmiSSLMasterLock); if(LibAPIVersion >= AMISSL_V302) { @@ -382,7 +383,7 @@ LIBPROTO(OpenAmiSSL, struct Library *, REG(a6, UNUSED __BASE_OR_IFACE)) E(DBF_STARTUP, "ERROR: couldn't open amissl library: %08lx", AmiSSLBase); #endif - ReleaseSemaphore(&AmiSSLMasterLock); + LOCK_RELEASE(AmiSSLMasterLock); RETURN(AmiSSLBase); return AmiSSLBase; @@ -390,7 +391,7 @@ LIBPROTO(OpenAmiSSL, struct Library *, REG(a6, UNUSED __BASE_OR_IFACE)) LIBPROTO(CloseAmiSSL, void, REG(a6, UNUSED __BASE_OR_IFACE)) { - ObtainSemaphore(&AmiSSLMasterLock); + LOCK_OBTAIN(AmiSSLMasterLock); if(AmiSSLBase) { @@ -420,7 +421,7 @@ LIBPROTO(CloseAmiSSL, void, REG(a6, UNUSED __BASE_OR_IFACE)) CloseLib(amisslinit.SHABase); CloseLib(amisslinit.RSABase); - ReleaseSemaphore(&AmiSSLMasterLock); + LOCK_RELEASE(AmiSSLMasterLock); } LIBPROTO(OpenAmiSSLCipher, struct Library *, REG(a6, UNUSED __BASE_OR_IFACE), REG(d0, LONG Cipher)) @@ -432,7 +433,7 @@ LIBPROTO(OpenAmiSSLCipher, struct Library *, REG(a6, UNUSED __BASE_OR_IFACE), RE struct Library *AmiSSLMasterBase = __BASE_OR_IFACE_VAR; #endif - ObtainSemaphore(&AmiSSLMasterLock); + LOCK_OBTAIN(AmiSSLMasterLock); // only open sub libraries for our old-style AmiSSL v2 versions. if(LibAPIVersion == AMISSL_V2) @@ -499,7 +500,7 @@ LIBPROTO(OpenAmiSSLCipher, struct Library *, REG(a6, UNUSED __BASE_OR_IFACE), RE } } - ReleaseSemaphore(&AmiSSLMasterLock); + LOCK_RELEASE(AmiSSLMasterLock); return result; } @@ -574,9 +575,9 @@ LIBPROTOVA(OpenAmiSSLTags, LONG, REG(a6, __BASE_OR_IFACE), REG(d0, LONG APIVersi LIBPROTO(CloseAmiSSLCipher, void, REG(a6, UNUSED __BASE_OR_IFACE), REG(a0, struct Library *LibBase)) { - ObtainSemaphore(&AmiSSLMasterLock); + LOCK_OBTAIN(AmiSSLMasterLock); CloseLib(LibBase); - ReleaseSemaphore(&AmiSSLMasterLock); + LOCK_RELEASE(AmiSSLMasterLock); } LIBPROTO(__UserLibCleanup, void, REG(a6, UNUSED __BASE_OR_IFACE)) @@ -627,6 +628,8 @@ LIBPROTO(__UserLibCleanup, void, REG(a6, UNUSED __BASE_OR_IFACE)) CloseLibrary((struct Library *)IntuitionBase); IntuitionBase = NULL; } + + LOCK_FREE(AmiSSLMasterLock); } LIBPROTO(__UserLibExpunge, void, REG(a6, UNUSED __BASE_OR_IFACE)) @@ -640,17 +643,17 @@ LIBPROTO(__UserLibInit, int, REG(a6, UNUSED __BASE_OR_IFACE)) ENTER(); - InitSemaphore(&AmiSSLMasterLock); - + if(LOCK_INIT(AmiSSLMasterLock) #if defined(__amigaos4__) - if((UtilityBase = OpenLibrary("utility.library", 50)) != NULL + && (UtilityBase = OpenLibrary("utility.library", 50)) != NULL && (IntuitionBase = OpenLibrary("intuition.library", 50)) != NULL && (IUtility = (struct UtilityIFace *)GetInterface(UtilityBase,"main",1,NULL)) != NULL - && (IIntuition = (struct IntuitionIFace *)GetInterface(IntuitionBase, "main", 1, NULL)) != NULL) + && (IIntuition = (struct IntuitionIFace *)GetInterface(IntuitionBase, "main", 1, NULL)) != NULL #else - if((UtilityBase = (struct UtilityBase *)OpenLibrary("utility.library", 37)) != NULL - && (IntuitionBase = (struct IntuitionBase*)OpenLibrary("intuition.library", 36)) != NULL) + && (UtilityBase = (struct UtilityBase *)OpenLibrary("utility.library", 37)) != NULL + && (IntuitionBase = (struct IntuitionBase*)OpenLibrary("intuition.library", 36)) != NULL #endif + ) { err = 0; }