为什么OPENSSL_cleanse看起来那么复杂和线程安全的? [英] Why does OPENSSL_cleanse look so complex and thread-unsafe?

查看:154
本文介绍了为什么OPENSSL_cleanse看起来那么复杂和线程安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是OPENSSL_cleanse中的OpenSSL 1.0.1i实施

This is the implementation of OPENSSL_cleanse in OpenSSL 1.0.1i

unsigned char cleanse_ctr = 0;

void OPENSSL_cleanse(void *ptr, size_t len)
{
    unsigned char *p = ptr;
    size_t loop = len, ctr = cleanse_ctr;
    while(loop--)
    {
        *(p++) = (unsigned char)ctr;
        ctr += (17 + ((size_t)p & 0xF));
    }
    p=memchr(ptr, (unsigned char)ctr, len);
    if(p)
        ctr += (63 + (size_t)p);
    cleanse_ctr = (unsigned char)ctr;
}

它看起来复杂,线程安全的(通过读取和写入全局变量 cleanse_ctr )。可有人请解释一下这个执行?难道一个用户需要对可能的数据争关切呢?

It looks complex and thread-unsafe (by reading and writing global variable cleanse_ctr). Can somebody please explain a bit about this implementation? Does a user need to concern about the possible data race in it?

推荐答案

有在code数据争,但它并不重要,因为变量的点仅仅是提供了不同与垃圾数据填补了这块内存。换句话说,它并没有真正过任何事给定的线程从该变量读取什么价值。用户不需要关心它。事实上,数据可能比赛甚至使功能更加有效。

There is a data race in the code, but it doesn't matter because the point of the variable is just to provide varying garbage data with which to fill a piece of memory. In other words, it doesn't ever really matter what value any given thread reads from that variable. Users do not need to be concerned about it. In fact, the data race may even make the function more effective.

这篇关于为什么OPENSSL_cleanse看起来那么复杂和线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆