Skip to content

Commit

Permalink
On OS4, use mutexes instead of semaphores and let Exec handle the mem…
Browse files Browse the repository at this point in the history
…ory pool protection
  • Loading branch information
Futaura committed Dec 9, 2023
1 parent 2186d2c commit 033de4d
Show file tree
Hide file tree
Showing 18 changed files with 193 additions and 106 deletions.
16 changes: 16 additions & 0 deletions include/internal/amissl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
6 changes: 3 additions & 3 deletions libcmt/fflush.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <internal/amissl.h>

extern struct MinList __filelist;
extern struct SignalSemaphore __filelist_cs;
extern LOCK_DECLARE(__filelist_cs);

int __fflush(FILE *stream) /* fflush exactly one file */
{
Expand Down Expand Up @@ -47,14 +47,14 @@ 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)
{
if(__fflush((FILE *)&((struct filenode *)node)->FILE))
retval=EOF;
node=nextnode;
}
ReleaseSemaphore(&__filelist_cs);
LOCK_RELEASE(__filelist_cs);
return retval;
}
11 changes: 6 additions & 5 deletions libcmt/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down
8 changes: 7 additions & 1 deletion libcmt/free.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down
25 changes: 18 additions & 7 deletions libcmt/libcmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -72,33 +74,38 @@ 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,
ASOPOOL_LockMem, FALSE,
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

// initialize file i/o stuff
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);
Expand Down Expand Up @@ -136,6 +143,8 @@ void __init_libcmt(void)
E(DBF_STARTUP, "ERROR on OpenLibrary()");

LEAVE();

return 1;
}

void __free_libcmt(void)
Expand All @@ -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__
Expand Down
4 changes: 2 additions & 2 deletions libcmt/libcmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);

/****************************************************************************/
Expand Down
10 changes: 8 additions & 2 deletions libcmt/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/amissl_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand All @@ -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;
};
Expand Down
1 change: 1 addition & 0 deletions src/amissl_glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <internal/amissl.h>
#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__)
Expand Down
15 changes: 8 additions & 7 deletions src/amissl_glue.patch
Original file line number Diff line number Diff line change
@@ -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 @@

/***************************************************************************/
Expand All @@ -14,14 +14,15 @@

/***************************************************************************/

@@ -14,11 +20,24 @@
@@ -14,11 +20,25 @@
#undef SAVEDS
#define SAVEDS __attribute__((baserel_restore)) __attribute__ ((noinline))
#elif defined(__amigaos3__)
-#error insert the correct offset to let the __restore_a4 function restore the correct base relative data segment below and remove this warning
+//#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 <internal/amissl.h>
+#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__
Expand All @@ -41,7 +42,7 @@
#endif // BASEREL

/***************************************************************************/
@@ -30,24 +49,30 @@
@@ -30,24 +50,30 @@

// ---

Expand Down Expand Up @@ -72,7 +73,7 @@

// ---

@@ -15369,6 +15394,8 @@
@@ -15369,6 +15395,8 @@

// ---

Expand All @@ -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 @@

// ---

Expand All @@ -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))
{
Expand Down
17 changes: 12 additions & 5 deletions src/amissl_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand All @@ -88,6 +92,8 @@ ULONG freeBase(UNUSED struct LibraryHeader *lib)

ULONG initBase(UNUSED struct LibraryHeader *lib)
{
ULONG success;

ENTER();

SHOWPOINTER(DBF_STARTUP, SysBase);
Expand All @@ -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);
Expand All @@ -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;
}

/***********************************************************************/
Expand Down
Loading

0 comments on commit 033de4d

Please sign in to comment.