【问题标题】:Explanation of the UB while changing data更改数据时的 UB 说明
【发布时间】:2013-05-21 11:15:42
【问题描述】:

我试图向工作伙伴证明,如果你真的想(并且知道如何)通过使用一些技巧来更改常量限定变量的值,在我的演示过程中,我发现存在两个常量值的“风味”:无论做什么都无法改变的那些,以及可以通过使用肮脏技巧来改变的那些。

当编译器使用文字值而不是存储在堆栈中的值时,常量值是不可更改的(阅读here),这是一个piece of code,它说明了我的意思:

// TEST 1
#define LOG(index, cv, ncv) std::cout \
    << std::dec << index << ".- Address = " \
    << std::hex << &cv << "\tValue = " << cv << '\n' \
    << std::dec << index << ".- Address = " \
    << std::hex << &ncv << "\tValue = " << ncv << '\n'

const unsigned int const_value = 0xcafe01e;

// Try with no-const reference
unsigned int &no_const_ref = const_cast<unsigned int &>(const_value);
no_const_ref = 0xfabada;
LOG(1, const_value, no_const_ref);

// Try with no-const pointer
unsigned int *no_const_ptr = const_cast<unsigned int *>(&const_value);
*no_const_ptr = 0xb0bada;
LOG(2, const_value, (*no_const_ptr));

// Try with c-style cast
no_const_ptr = (unsigned int *)&const_value;
*no_const_ptr = 0xdeda1;
LOG(3, const_value, (*no_const_ptr));

// Try with memcpy
unsigned int brute_force = 0xba51c;
std::memcpy(no_const_ptr, &brute_force, sizeof(const_value));
LOG(4, const_value, (*no_const_ptr));

// Try with union
union bad_idea
{
    const unsigned int *const_ptr;
    unsigned int *no_const_ptr;
} u;

u.const_ptr = &const_value;
*u.no_const_ptr = 0xbeb1da;
LOG(5, const_value, (*u.no_const_ptr));

这会产生以下输出:

1.- Address = 0xbfffbe2c    Value = cafe01e
1.- Address = 0xbfffbe2c    Value = fabada
2.- Address = 0xbfffbe2c    Value = cafe01e
2.- Address = 0xbfffbe2c    Value = b0bada
3.- Address = 0xbfffbe2c    Value = cafe01e
3.- Address = 0xbfffbe2c    Value = deda1
4.- Address = 0xbfffbe2c    Value = cafe01e
4.- Address = 0xbfffbe2c    Value = ba51c
5.- Address = 0xbfffbe2c    Value = cafe01e
5.- Address = 0xbfffbe2c    Value = beb1da

由于我依赖 UB(更改 const 数据的值),因此预计该程序的行为会很奇怪;但这种奇怪的程度超出了我的预期。

假设编译器使用的是字面量值,那么,当代码到达改变常量值的指令(通过引用、指针或memcpying)时,只要该值就简单地忽略顺序是文字(虽然是未定义的行为)。这解释了为什么值保持不变但是:

  • 为什么两个变量的内存地址相同,但包含的值不同?

AFAIK 相同的内存地址不能指向不同的值,因此,输出之一是谎言:

  • 到底发生了什么?哪个内存地址是假的(如果有的话)?

对上面的代码进行一些更改,我们可以尽量避免使用字面值,这样诡计就可以发挥作用(source here):

// TEST 2
// Try with no-const reference
void change_with_no_const_ref(const unsigned int &const_value)
{
    unsigned int &no_const_ref = const_cast<unsigned int &>(const_value);
    no_const_ref = 0xfabada;
    LOG(1, const_value, no_const_ref);    
}

// Try with no-const pointer
void change_with_no_const_ptr(const unsigned int &const_value)
{
    unsigned int *no_const_ptr = const_cast<unsigned int *>(&const_value);
    *no_const_ptr = 0xb0bada;
    LOG(2, const_value, (*no_const_ptr));
}

// Try with c-style cast
void change_with_cstyle_cast(const unsigned int &const_value)
{
    unsigned int *no_const_ptr = (unsigned int *)&const_value;
    *no_const_ptr = 0xdeda1;
    LOG(3, const_value, (*no_const_ptr));
}

// Try with memcpy
void change_with_memcpy(const unsigned int &const_value)
{
    unsigned int *no_const_ptr = const_cast<unsigned int *>(&const_value);
    unsigned int brute_force = 0xba51c;
    std::memcpy(no_const_ptr, &brute_force, sizeof(const_value));
    LOG(4, const_value, (*no_const_ptr));
}

void change_with_union(const unsigned int &const_value)
{
    // Try with union
    union bad_idea
    {
        const unsigned int *const_ptr;
        unsigned int *no_const_ptr;
    } u;

    u.const_ptr = &const_value;
    *u.no_const_ptr = 0xbeb1da;
    LOG(5, const_value, (*u.no_const_ptr));
}

int main(int argc, char **argv)
{
    unsigned int value = 0xcafe01e;
    change_with_no_const_ref(value);
    change_with_no_const_ptr(value);
    change_with_cstyle_cast(value);
    change_with_memcpy(value);
    change_with_union(value);

    return 0;
}

产生以下输出:

1.- Address = 0xbff0f5dc    Value = fabada
1.- Address = 0xbff0f5dc    Value = fabada
2.- Address = 0xbff0f5dc    Value = b0bada
2.- Address = 0xbff0f5dc    Value = b0bada
3.- Address = 0xbff0f5dc    Value = deda1
3.- Address = 0xbff0f5dc    Value = deda1
4.- Address = 0xbff0f5dc    Value = ba51c
4.- Address = 0xbff0f5dc    Value = ba51c
5.- Address = 0xbff0f5dc    Value = beb1da
5.- Address = 0xbff0f5dc    Value = beb1da

正如我们所看到的,const 限定变量在每次change_with_* 调用时都发生了变化,除了这个事实之外,行为与以前相同,所以我很想假设内存地址的奇怪行为表现出来当 const 数据用作文字而不是值时。

所以,为了确保这个假设,我做了最后一个测试,将main 中的unsigned int value 更改为const unsigned int value

// TEST 3
const unsigned int value = 0xcafe01e;
change_with_no_const_ref(value);
change_with_no_const_ptr(value);
change_with_cstyle_cast(value);
change_with_memcpy(value);
change_with_union(value);

令人惊讶的是,输出与TEST 2 (code here) 相同,所以我认为数据是作为变量而不是字面值传递的,因为它用作参数,所以这让我想知道:

  • 是什么让编译器决定将 const 值优化为文字值?

简而言之,我的问题是:

  • TEST 1
    • 为什么 const 值和 no-const 值共享相同的内存地址,但包含的值不同?
    • 程序产生此输出的步骤是什么?哪个内存地址是假的(如果有的话)?
  • TEST 3
    • 是什么让编译器决定将 const 值优化为文字值?

【问题讨论】:

  • “UB 的解释”——这不是矛盾吗?
  • 试图和 UB 讲道理是完全没用的。标准绝对不提供任何保证,所以所有的赌注都没有,有龙等等。
  • 因为独角兽总是隐藏在众目睽睽之下。不管你做什么,都不要吃橘子!
  • 编译器将用 TEST1 中 cout 中的常量值替换常量变量,因为该值在代码内部而不是在全局地址空间中,当更改静态变量的值时它不会改变.这是作为优化完成的。读取程序代码比读取全局空间更快。那种回答你的问题。
  • @H2CO3 (Carbonic acid lol) 解释 UB 可能被认为是矛盾的,我同意 :) 但自相矛盾的是,我认为编译器为这种情况定义了一种行为(也许每个编译器定义了不同的行为),我正在寻找比我更熟练的人,可以很好地解释真正发生的事情(也许看看 ASM 代码,我没有的技能)

标签: c++ constants undefined-behavior const-cast


【解决方案1】:

一般来说,分析未定义行为是没有意义的,因为无法保证您可以将分析结果转移到不同的程序。

在这种情况下,可以通过假设编译器已应用称为 constant propagation 的优化技术来解释该行为。在该技术中,如果您使用编译器知道其值的const 变量的值,则编译器将使用该变量的值替换const 变量的使用(因为它在编译时已知) .变量的其他用途,例如获取其地址,不会被替换。

这种优化是有效的,正是因为更改定义为const 的变量会导致未定义的行为,并且允许编译器假设程序没有调用未定义的行为。

因此,在TEST 1 中,地址是相同的,因为它都是相同的变量,但值不同,因为每对中的第一个反映了编译器(正确地)假定为变量的值,并且第二个反映了实际存储在那里的内容。 在TEST 2TEST 3 中,编译器无法进行优化,因为编译器不能100% 确定函数参数将引用一个常量值(而在TEST 2 中,它没有) .

【讨论】:

    猜你喜欢
    • 2016-03-20
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    相关资源
    最近更新 更多