fixed a potential issue by shared data access in mod-hawk.c
This commit is contained in:
		| @ -1,7 +1,7 @@ | ||||
| /* | ||||
|  * $Id$ | ||||
|  * | ||||
|     Copyright (c) 2014-2019 Chung, Hyung-Hwan. All rights reserved. | ||||
|     Copyright (c) 2006-2020 Chung, Hyung-Hwan. All rights reserved. | ||||
|  | ||||
|     Redistribution and use in source and binary forms, with or without | ||||
|     modification, are permitted provided that the following conditions | ||||
|  | ||||
| @ -1,7 +1,7 @@ | ||||
| /* | ||||
|  * $Id$ | ||||
|  * | ||||
|     Copyright (c) 2014-2019 Chung, Hyung-Hwan. All rights reserved. | ||||
|     Copyright (c) 2006-2020 Chung, Hyung-Hwan. All rights reserved. | ||||
|  | ||||
|     Redistribution and use in source and binary forms, with or without | ||||
|     modification, are permitted provided that the following conditions | ||||
|  | ||||
| @ -1,7 +1,7 @@ | ||||
| /* | ||||
|  * $Id$ | ||||
|  * | ||||
|     Copyright (c) 2014-2019 Chung, Hyung-Hwan. All rights reserved. | ||||
|     Copyright (c) 2006-2020 Chung, Hyung-Hwan. All rights reserved. | ||||
|  | ||||
|     Redistribution and use in source and binary forms, with or without | ||||
|     modification, are permitted provided that the following conditions | ||||
|  | ||||
| @ -1,7 +1,7 @@ | ||||
| /* | ||||
|  * $Id$ | ||||
|  * | ||||
|     Copyright (c) 2014-2019 Chung, Hyung-Hwan. All rights reserved. | ||||
|     Copyright (c) 2006-2020 Chung, Hyung-Hwan. All rights reserved. | ||||
|  | ||||
|     Redistribution and use in source and binary forms, with or without | ||||
|     modification, are permitted provided that the following conditions | ||||
|  | ||||
| @ -26,7 +26,14 @@ | ||||
|  | ||||
| #include "mod-hawk.h" | ||||
| #include "hawk-prv.h" | ||||
| #include <hawk-mtx.h> | ||||
|  | ||||
|  | ||||
| struct mod_data_t | ||||
| { | ||||
| 	hawk_mtx_t mq_mtx; | ||||
| }; | ||||
| typedef struct mod_data_t mod_data_t; | ||||
| /* ----------------------------------------------------------------- */ | ||||
|  | ||||
| /* | ||||
| @ -150,7 +157,14 @@ static int fnc_function_exists (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) | ||||
| 			if (!rx) | ||||
| 			{ | ||||
| 				hawk_mod_sym_t sym; | ||||
| 				mod_data_t* md; | ||||
|  | ||||
| 				md = (mod_data_t*)fi->mod->ctx; | ||||
| 				/* hawk_query_module_with_name() may update some shared data under | ||||
| 				 * the hawk object. use a mutex for shared data safety */ | ||||
| 				hawk_mtx_lock (&md->mq_mtx, HAWK_NULL); | ||||
| 				rx = (hawk_query_module_with_name(hawk_rtx_gethawk(rtx), &name, &sym) != HAWK_NULL); | ||||
| 				hawk_mtx_unlock (&md->mq_mtx); | ||||
| 			} | ||||
| 		} | ||||
| 		hawk_rtx_freevaloocstr (rtx, a0, name.ptr); | ||||
| @ -449,17 +463,27 @@ static void fini (hawk_mod_t* mod, hawk_rtx_t* rtx) | ||||
|  | ||||
| static void unload (hawk_mod_t* mod, hawk_t* hawk) | ||||
| { | ||||
| 	/* nothing to do */ | ||||
| 	mod_data_t* md = (mod_data_t*)mod->ctx; | ||||
|  | ||||
| 	hawk_mtx_fini (&md->mq_mtx); | ||||
| 	hawk_freemem (hawk, md); | ||||
| } | ||||
|  | ||||
| int hawk_mod_hawk (hawk_mod_t* mod, hawk_t* hawk) | ||||
| { | ||||
| 	mod_data_t* md; | ||||
|  | ||||
| 	md = hawk_allocmem(hawk, HAWK_SIZEOF(*md)); | ||||
| 	if (HAWK_UNLIKELY(!md)) return -1; | ||||
|  | ||||
| 	hawk_mtx_init (&md->mq_mtx, hawk_getgem(hawk)); | ||||
|  | ||||
| 	mod->query = query; | ||||
| 	mod->unload = unload; | ||||
|  | ||||
| 	mod->init = init; | ||||
| 	mod->fini = fini; | ||||
| 	mod->ctx = HAWK_NULL; | ||||
| 	mod->ctx = md; | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| @ -363,7 +363,7 @@ void* hawk_stdmodopen (hawk_t* hawk, const hawk_mod_spec_t* spec) | ||||
| 	else | ||||
| 	{ | ||||
| 		void* h; | ||||
| 		h = lt_dlopen(NULL); | ||||
| 		h = lt_dlopen(HAWK_NULL); | ||||
| 		if (HAWK_UNLIKELY(!h)) hawk_seterrfmt (hawk, HAWK_NULL, HAWK_ESYSERR, HAWK_T("%hs"), lt_dlerror()); | ||||
| 		return h; | ||||
| 	} | ||||
| @ -409,7 +409,7 @@ void* hawk_stdmodopen (hawk_t* hawk, const hawk_mod_spec_t* spec) | ||||
| 	else | ||||
| 	{ | ||||
| 		HMODULE h; | ||||
| 		h = GetModuleHandle(NULL); | ||||
| 		h = GetModuleHandle(HAWK_NULL); | ||||
| 		if (!h) hawk_seterrnum (hawk, HAWK_NULL, hawk_syserr_to_errnum(GetLastError()); | ||||
| 		return h; | ||||
| 	} | ||||
| @ -512,7 +512,7 @@ void* hawk_stdmodopen (hawk_t* hawk, const hawk_mod_spec_t* spec) | ||||
| 	else | ||||
| 	{ | ||||
| 		void* h; | ||||
| 		h = GetModuleHandle(NULL); | ||||
| 		h = GetModuleHandle(HAWK_NULL); | ||||
| 		if (!h) hawk_seterrnum (hawk, HAWK_NULL, HAWK_ESYSERR); | ||||
| 		return h; | ||||
| 	} | ||||
| @ -572,11 +572,11 @@ void hawk_stdmodclose (hawk_t* hawk, void* handle) | ||||
| #if defined(USE_LTDL) | ||||
| 	lt_dlclose (handle); | ||||
| #elif defined(_WIN32) | ||||
| 	if (handle != GetModuleHandle(NULL)) FreeLibrary ((HMODULE)handle); | ||||
| 	if (handle != GetModuleHandle(HAWK_NULL)) FreeLibrary ((HMODULE)handle); | ||||
| #elif defined(__OS2__) | ||||
| 	DosFreeModule ((HMODULE)handle); | ||||
| #elif defined(__DOS__) && defined(HAWK_ENABLE_DOS_DYNAMIC_MODULE) | ||||
| 	if (handle != GetModuleHandle(NULL))  FreeModule (handle); | ||||
| 	if (handle != GetModuleHandle(HAWK_NULL))  FreeModule (handle); | ||||
| #elif defined(USE_DLFCN) | ||||
| 	dlclose (handle); | ||||
| #else | ||||
|  | ||||
		Reference in New Issue
	
	Block a user