3

我正在通过 Undefined Behavior Sanitizer 运行一些更新。消毒剂正在产生一条我不太明白的消息:

kalyna.cpp:1326:61: runtime error: load of address 0x0000016262c0 with insufficient space for an object of type 'const uint32_t'
0x0000016262c0: note: pointer points here
 20 8b c1 1f  a9 f7 f9 5c 53 c4 cf d2  2f 3f 52 be 84 ed 96 1b  b8 7a b2 85 e0 96 7d 5d  70 ee 06 07
              ^

有问题的代码试图通过触摸缓存行范围内的地址来使缓存定时攻击更加困难。第 1326 行是reinterpret_cast

// In KalynaTab namespace
uint64_t S[4][256] = {
    ...
};
...

// In library's namespace
const int cacheLineSize = GetCacheLineSize();
volatile uint32_t _u = 0;
uint32_t u = _u;

for (unsigned int i=0; i<256; i+=cacheLineSize)
    u &= *reinterpret_cast<const uint32_t*>(KalynaTab::S+i);

为什么 santizier 声称 auint32_t u没有足够的空间来容纳 a uint32_t

或者,我是否正确解析了错误消息?这就是sanitzier抱怨的事情吗?如果我解析不正确,那么消毒剂在抱怨什么?


$ lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch

$ gcc --version
gcc (GCC) 6.3.1 20161221 (Red Hat 6.3.1-1)
4

1 回答 1

4

标识符S不会转换为您认为的类型的指针。结果,您的指针算法使您超出数据范围,最好通过示例说明:

#include <iostream>
#include <cstdint>

uint64_t S[4][256];

int main()
{
    std::cout << static_cast<void*>(S+0) << '\n';
    std::cout << static_cast<void*>(S+1) << '\n';
    std::cout << static_cast<void*>(S+2) << '\n';
    std::cout << static_cast<void*>(S+3) << '\n';
    std::cout << '\n';

    std::cout << static_cast<void*>(*S+0) << '\n';
    std::cout << static_cast<void*>(*S+1) << '\n';
    std::cout << static_cast<void*>(*S+2) << '\n';
    std::cout << static_cast<void*>(*S+3) << '\n';
}

输出(显然取决于平台)

0x1000020b0
0x1000028b0
0x1000030b0
0x1000038b0

0x1000020b0
0x1000020b8
0x1000020c0
0x1000020c8

请注意每个劣行的第一个数字序列 0x800 的步幅。这是有道理的,因为每一行都由 0x100 个条目组成,每个条目有 8 个字节(uint64_t 元素)。指针算术中使用的指针类型是uint64_t (*)[256].

现在注意第二个序列的步幅,它只与S[0]. 步长为 8 个字节,每个插槽一个。此计算中转换后的指针的类型是uint64_t *

简而言之,您的指针算术假设S转换为uint64_t*,但事实并非如此。像所有数组到指针的转换一样,它转换为指向第一个元素的指针,包括相同的类型。数组数组中的元素类型是uint64_t[256],所以转换后的指针类型是uint64_t (*)[256]

于 2017-05-08T19:43:05.680 回答