Software APIs
sw
device
lib
base
global_mock_unittest.cc
1
// Copyright lowRISC contributors (OpenTitan project).
2
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3
// SPDX-License-Identifier: Apache-2.0
4
5
#include "sw/device/lib/base/global_mock.h"
6
7
#include "gtest/gtest.h"
8
9
namespace
global_mock_test {
10
11
// This is a regression test for a casting bug in `GlobalMock<T>`. The
12
// `GlobalMock()` constructor erroneously cast `this` to `T*` before the `T`
13
// object was constructed.
14
//
15
// This is what UBSan has to say when `GlobalMock`'s constructor casts `this` to
16
// `MockFoo*`:
17
// runtime error: downcast of address 0x7ffdb2a52900 which does not point to
18
// an object of type 'MockFoo'
19
TEST(GlobalMockTest, DowncastHygieneRegressionTest) {
20
class
MockFoo :
public
global_mock::GlobalMock
<MockFoo> {
21
public
:
22
int
value() {
return
value_; }
23
24
private
:
25
int
value_ = 42;
26
};
27
28
MockFoo foo;
29
MockFoo &foo_ref =
global_mock::GlobalMock<MockFoo>::Instance
();
30
ASSERT_EQ(foo_ref.value(), 42);
31
}
32
33
}
// namespace global_mock_test
Return to
OpenTitan Documentation