如何在多线程中使用 memory_buffer_alloc_init()?

问题描述

我有一个使用 mbedtls 对加密消息执行 RSA decryption 操作的简单代码。我正在尝试使用多线程并发执行相同的操作。我有兴趣使用 stack memory 而不是 heapmbedtls 为基于堆栈的内存分配器提供 memory_buffer_alloc.hmemory_buffer_alloc_init() 的文档说,

初始化使用基于堆栈的内存分配器。 基于堆栈的分配器在内部进行内存管理 呈现缓冲区并且不调用 malloc() 和 free()。 它设置全局 polarssl_malloc() 和 polarssl_free() 指针 到它自己的功能。 (提供的 polarssl_malloc() 和 polarssl_free() 是线程安全的,如果 POlarsSL_THREADING_C 已定义)

因此,我将以下配置添加到我的 config.h 文件中,

#define POlarsSL_THREADING_PTHREAD
#define POlarsSL_THREADING_C
#define POlarsSL_MEMORY_C
#define POlarsSL_MEMORY_BUFFER_ALLOC_C
#define POlarsSL_PLATFORM_MEMORY
#define POlarsSL_PLATFORM_C

我的代码适用于单个线程。但是,当我增加线程数时,我的代码显示错误。以下是我的源代码

#include "rsa/config.h"
#include "rsa/aes.h"
#include "rsa/bignum.h"
#include "rsa/rsa.h"
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h> 
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <pthread.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "rsa/key.h"

#include "rsa/memory_buffer_alloc.h"
#include "rsa/memory.h"
#include "rsa/platform.h"
#include "rsa/threading.h"


#define NUM_OF_THREAD 2  

//threading_mutex_t lock;


void decryption(){

    // initialize stack memory
    unsigned char alloc_buf[10000];
    memory_buffer_alloc_init( &alloc_buf,sizeof(alloc_buf) );

    unsigned char private_encrypt[KEY_BUFFER_SIZE];
    int total_dec=5;
    unsigned char * buffer = 0;
    long length;
    unsigned char msg_decrypted[KEY_LEN];


    // reading encrypted msg
    FILE * fp2 = fopen ("msg.enc","rb");
    int size1=KEY_BUFFER_SIZE;
    if(fp2){
        while(size1>0){
            fread(private_encrypt,1,sizeof (private_encrypt),fp2);
            size1=size1-1;
        }
    }
    fclose(fp2);

    // reading rsa-private key
    FILE * fp = fopen ("rsa_priv.txt","rb");
    if (fp){
        fseek (fp,SEEK_END);
        length = ftell (fp);
        fseek (fp,SEEK_SET);
        buffer = calloc (1,length+1);
        if (buffer){
            fread (buffer,length,fp);
        }
    fclose (fp);
    }

    // initialize rsaContext
    rsa_context rsaContext;
    rsa_init(&rsaContext,RSA_PKCS_V15,0);
    rsaContext.len=KEY_LEN;

    // spliting keys and load into rsa context
    const char s[3] = "= ";
    char *token;
    int k=0,size;
    char *rest=buffer;

    // get the first token
    token = strtok_r(rest,s,&rest);

    // walk through other tokens
    while( token != NULL ) {
        size = strlen(token);
        switch (k) {
            case 1:
                token[size-1]='\0';
                mpi_read_string(&rsaContext.N,16,token);
                break;

            case 3:
                token[size-1]='\0';
                mpi_read_string(&rsaContext.E,token);
                break;

            case 5:
                token[size-1]='\0';
                mpi_read_string(&rsaContext.D,token);
                break;

            case 7:
                token[size-1]='\0';
                mpi_read_string(&rsaContext.P,token);
                break;

            case 9:
                token[size-1]='\0';
                mpi_read_string(&rsaContext.Q,token);
                break;

            case 11:
                token[size-1]='\0';
                mpi_read_string(&rsaContext.DP,token);
                break;

            case 13:
                token[size-1]='\0';
                mpi_read_string(&rsaContext.DQ,token);
                break;

            case 15:
                token[size-1]='\0';
                mpi_read_string(&rsaContext.QP,token);
                break;
        }
        k=k+1;
        token = strtok_r(rest,"= \n",&rest);
    }


    if( rsa_private(&rsaContext,private_encrypt,msg_decrypted) != 0 ) {
        printf( "Decryption Failed! %d\n",rsa_private(&rsaContext,msg_decrypted));
    }else{
        printf("Decrypted plaintext-----> %s\n",msg_decrypted );
    }

   // free memory 
   memory_buffer_alloc_free();

}


void thread_function(void * input){

    printf("Test thread\n");
    int total_loop=5;
    while(total_loop>0){
        //pthread_mutex_lock(&lock); <-- multi-thread works with lock
        decryption();
        //pthread_mutex_unlock(&lock); 
        total_loop--;           
    }
}


int main(){ 
    int i;
    
    // total number of thread
    pthread_t ths[NUM_OF_THREAD];

    for (i = 0; i < NUM_OF_THREAD; i++) {
        pthread_create(&ths[i],NULL,thread_function,NULL);
    }

    for (i = 0; i < NUM_OF_THREAD; i++) {
        void* res;
        pthread_join(ths[i],&res);
    }
    return 0;
}

如果我使用 mutex,则上述代码有效。我不想使用锁。花了很长时间。 谁能告诉我我做错了什么?我该如何解决

以前,我使用堆内存并询问 Why multiple pthread_create() calling same function end up in segmentation fault?。感谢@Ingo Leonhardt @AlexM 提供的快速解决方案。我能够使用堆内存使用多线程。
代码可在此处获得:https://github.com/AlamShariful/stackMemory_multithread

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)