[vlc-devel] [PATCH 2/2] rand: only load the random generator algorithm once

Steve Lhomme robux4 at ycbcr.xyz
Fri Mar 27 08:34:25 CET 2020


On 2020-03-26 17:20, Rémi Denis-Courmont wrote:
> Le torstaina 26. maaliskuuta 2020, 17.33.37 EET Steve Lhomme a écrit :
>> According to the documentation:
>> Because of the number and type of operations that are required to find,
>> load, and initialize an algorithm provider, the BCryptOpenAlgorithmProvider
>> function is a relatively time intensive function. Because of this, we
>> recommend that you cache any algorithm provider handles that you will use
>> more than once, rather than opening and closing the algorithm providers
>> over and over.
>>
>> We don't release it anymore but it should be released when the process ends.
> 
> So what happens if LibVLC is loaded/unloaded multiple times in the same
> process?

The same as the other vlc_once() calls in the core.

Anyway I don't think it's good to load/unload libvlc multiple times in 
the same process, since we don't unload the DLLs it loads.

>> ---
>>   src/win32/rand.c | 19 +++++++++++++------
>>   1 file changed, 13 insertions(+), 6 deletions(-)
>>
>> diff --git a/src/win32/rand.c b/src/win32/rand.c
>> index 1af02aa5984..949ce866dec 100644
>> --- a/src/win32/rand.c
>> +++ b/src/win32/rand.c
>> @@ -27,14 +27,21 @@
>>
>>   #include <bcrypt.h>
>>
>> -void vlc_rand_bytes (void *buf, size_t len)
>> +static BCRYPT_ALG_HANDLE algo_handle = NULL;
>> +
>> +static void InitAlgo(void)
>>   {
>> -    BCRYPT_ALG_HANDLE algo_handle;
>>       NTSTATUS ret = BCryptOpenAlgorithmProvider(&algo_handle,
>> BCRYPT_RNG_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0); -    if
>> (BCRYPT_SUCCESS(ret))
>> -    {
>> +    if (!BCRYPT_SUCCESS(ret))
>> +        algo_handle = NULL;
>> +}
>> +
>> +void vlc_rand_bytes (void *buf, size_t len)
>> +{
>> +    static vlc_once_t algo_init_once = VLC_STATIC_ONCE;
>> +    vlc_once( &algo_init_once, InitAlgo );
>> +
>> +    if (likely(algo_handle != NULL))
>>           BCryptGenRandom(algo_handle, buf, len, 0);
>> -        BCryptCloseAlgorithmProvider(algo_handle, 0);
>> -    }
>>   }
> 
> 
> -- 
> Rémi Denis-Courmont
> http://www.remlab.net/
> 
> 
> 
> _______________________________________________
> vlc-devel mailing list
> To unsubscribe or modify your subscription options:
> https://mailman.videolan.org/listinfo/vlc-devel
> 


More information about the vlc-devel mailing list