Skip to content

Commit b255dc7

Browse files
authored
Merge pull request #632 from scratchcpp/address_sanitizer
Use address sanitizer in unit tests
2 parents 33fb270 + 8869d4b commit b255dc7

File tree

8 files changed

+36
-58
lines changed

8 files changed

+36
-58
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ option(LIBSCRATCHCPP_BUILD_UNIT_TESTS "Build unit tests" ON)
1010
option(LIBSCRATCHCPP_NETWORK_SUPPORT "Support for downloading projects" ON)
1111
option(LIBSCRATCHCPP_PRINT_LLVM_IR "Print LLVM IR of compiled Scratch scripts (for debugging)" OFF)
1212

13+
if (CMAKE_BUILD_TYPE MATCHES "Debug")
14+
set(SANITIZER_FLAGS "-fsanitize=undefined -fsanitize=address -fno-omit-frame-pointer")
15+
set(CMAKE_CFLAGS ${CMAKE_CFLAGS} ${SANITIZER_FLAGS})
16+
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${SANITIZER_FLAGS})
17+
endif()
18+
1319
add_library(scratchcpp SHARED)
1420
add_subdirectory(src)
1521
include_directories(src) # TODO: Remove this line

include/scratchcpp/compilerconstant.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace libscratchcpp
1010
class CompilerConstantPrivate;
1111

1212
/*! \brief The CompilerConstant class represents a constant value in compiled code. */
13-
class LIBSCRATCHCPP_EXPORT CompilerConstant : public CompilerValue
13+
class LIBSCRATCHCPP_EXPORT CompilerConstant : public virtual CompilerValue
1414
{
1515
public:
1616
CompilerConstant(Compiler::StaticType type, const Value &value);

src/engine/internal/llvm/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ target_sources(scratchcpp
22
PRIVATE
33
llvmcodebuilder.cpp
44
llvmcodebuilder.h
5-
llvmregisterbase.h
65
llvmregister.h
76
llvmconstantregister.h
87
llvminstruction.h

src/engine/internal/llvm/llvmcodebuilder.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
980980
llvm::Value *inRange = m_builder.CreateAnd(m_builder.CreateFCmpOGE(index, min), m_builder.CreateFCmpOLT(index, size));
981981

982982
LLVMConstantRegister nullReg(listPtr.type == Compiler::StaticType::Unknown ? Compiler::StaticType::Number : listPtr.type, Value());
983-
llvm::Value *null = createValue(static_cast<LLVMRegister *>(static_cast<CompilerValue *>(&nullReg)));
983+
llvm::Value *null = createValue(static_cast<LLVMRegister *>(&nullReg));
984984

985985
index = m_builder.CreateFPToUI(index, m_builder.getInt64Ty());
986986
step.functionReturnReg->value = m_builder.CreateSelect(inRange, getListItem(listPtr, index), null);
@@ -1378,7 +1378,7 @@ CompilerValue *LLVMCodeBuilder::addFunctionCall(const std::string &functionName,
13781378
ins->functionName = functionName;
13791379

13801380
for (size_t i = 0; i < args.size(); i++)
1381-
ins->args.push_back({ argTypes[i], static_cast<LLVMRegister *>(args[i]) });
1381+
ins->args.push_back({ argTypes[i], dynamic_cast<LLVMRegister *>(args[i]) });
13821382

13831383
if (returnType != Compiler::StaticType::Void) {
13841384
auto reg = std::make_shared<LLVMRegister>(returnType);
@@ -1409,9 +1409,9 @@ CompilerValue *LLVMCodeBuilder::addFunctionCallWithCtx(const std::string &functi
14091409
CompilerConstant *LLVMCodeBuilder::addConstValue(const Value &value)
14101410
{
14111411
auto constReg = std::make_shared<LLVMConstantRegister>(TYPE_MAP[value.type()], value);
1412-
auto reg = std::reinterpret_pointer_cast<LLVMRegister>(constReg);
1412+
auto reg = std::static_pointer_cast<LLVMRegister>(constReg);
14131413
m_lastConstValue = reg.get();
1414-
return static_cast<CompilerConstant *>(static_cast<CompilerValue *>(addReg(reg, nullptr)));
1414+
return static_cast<CompilerConstant *>(static_cast<LLVMConstantRegister *>(addReg(reg, nullptr)));
14151415
}
14161416

14171417
CompilerValue *LLVMCodeBuilder::addStringChar(CompilerValue *string, CompilerValue *index)
@@ -1470,7 +1470,7 @@ CompilerValue *LLVMCodeBuilder::addListItem(List *list, CompilerValue *index)
14701470
if (m_listPtrs.find(list) == m_listPtrs.cend())
14711471
m_listPtrs[list] = LLVMListPtr();
14721472

1473-
ins->args.push_back({ Compiler::StaticType::Number, static_cast<LLVMRegister *>(index) });
1473+
ins->args.push_back({ Compiler::StaticType::Number, dynamic_cast<LLVMRegister *>(index) });
14741474

14751475
auto ret = std::make_shared<LLVMRegister>(Compiler::StaticType::Unknown);
14761476
ret->isRawValue = false;
@@ -1807,7 +1807,7 @@ void LLVMCodeBuilder::createListReplace(List *list, CompilerValue *index, Compil
18071807
void LLVMCodeBuilder::beginIfStatement(CompilerValue *cond)
18081808
{
18091809
auto ins = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::BeginIf, currentLoopScope(), m_loopCondition);
1810-
ins->args.push_back({ Compiler::StaticType::Bool, static_cast<LLVMRegister *>(cond) });
1810+
ins->args.push_back({ Compiler::StaticType::Bool, dynamic_cast<LLVMRegister *>(cond) });
18111811
m_instructions.push_back(ins);
18121812
}
18131813

@@ -1826,7 +1826,7 @@ void LLVMCodeBuilder::beginRepeatLoop(CompilerValue *count)
18261826
assert(!m_loopCondition);
18271827

18281828
auto ins = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::BeginRepeatLoop, currentLoopScope(), m_loopCondition);
1829-
ins->args.push_back({ Compiler::StaticType::Number, static_cast<LLVMRegister *>(count) });
1829+
ins->args.push_back({ Compiler::StaticType::Number, dynamic_cast<LLVMRegister *>(count) });
18301830
m_instructions.push_back(ins);
18311831
pushLoopScope(false);
18321832
}
@@ -1837,7 +1837,7 @@ void LLVMCodeBuilder::beginWhileLoop(CompilerValue *cond)
18371837
m_loopCondition = false;
18381838

18391839
auto ins = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::BeginWhileLoop, currentLoopScope(), m_loopCondition);
1840-
ins->args.push_back({ Compiler::StaticType::Bool, static_cast<LLVMRegister *>(cond) });
1840+
ins->args.push_back({ Compiler::StaticType::Bool, dynamic_cast<LLVMRegister *>(cond) });
18411841
m_instructions.push_back(ins);
18421842
pushLoopScope(false);
18431843
}
@@ -1848,7 +1848,7 @@ void LLVMCodeBuilder::beginRepeatUntilLoop(CompilerValue *cond)
18481848
m_loopCondition = false;
18491849

18501850
auto ins = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::BeginRepeatUntilLoop, currentLoopScope(), m_loopCondition);
1851-
ins->args.push_back({ Compiler::StaticType::Bool, static_cast<LLVMRegister *>(cond) });
1851+
ins->args.push_back({ Compiler::StaticType::Bool, dynamic_cast<LLVMRegister *>(cond) });
18521852
m_instructions.push_back(ins);
18531853
pushLoopScope(false);
18541854
}
@@ -2687,7 +2687,7 @@ LLVMRegister *LLVMCodeBuilder::createOp(const LLVMInstruction &ins, Compiler::St
26872687
m_instructions.push_back(createdIns);
26882688

26892689
for (size_t i = 0; i < args.size(); i++)
2690-
createdIns->args.push_back({ argTypes[i], static_cast<LLVMRegister *>(args[i]) });
2690+
createdIns->args.push_back({ argTypes[i], dynamic_cast<LLVMRegister *>(args[i]) });
26912691

26922692
if (retType != Compiler::StaticType::Void) {
26932693
auto ret = std::make_shared<LLVMRegister>(retType);

src/engine/internal/llvm/llvmconstantregister.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ namespace libscratchcpp
1010
{
1111

1212
struct LLVMConstantRegister
13-
: public LLVMRegisterBase
14-
, public CompilerConstant
13+
: public CompilerConstant
14+
, public LLVMRegister
1515
{
1616
LLVMConstantRegister(Compiler::StaticType type, const Value &value) :
17-
LLVMRegisterBase(),
18-
CompilerConstant(type, value)
17+
CompilerValue(type),
18+
CompilerConstant(type, value),
19+
LLVMRegister(type)
1920
{
2021
}
2122

22-
const Value &constValue() const override { return CompilerConstant::value(); }
23+
const Value &constValue() const override final { return CompilerConstant::value(); }
2324
};
2425

2526
} // namespace libscratchcpp

src/engine/internal/llvm/llvmregister.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
#pragma once
44

5-
#include "llvmregisterbase.h"
5+
#include <scratchcpp/compilervalue.h>
6+
#include <scratchcpp/value.h>
67

78
namespace llvm
89
{
@@ -14,25 +15,24 @@ class Value;
1415
namespace libscratchcpp
1516
{
1617

17-
struct LLVMRegister
18-
: public LLVMRegisterBase
19-
, public CompilerValue
18+
class LLVMInstruction;
19+
20+
struct LLVMRegister : public virtual CompilerValue
2021
{
2122
LLVMRegister(Compiler::StaticType type) :
22-
LLVMRegisterBase(),
2323
CompilerValue(type)
2424
{
2525
}
2626

27-
const Value &constValue() const override
27+
virtual const Value &constValue() const
2828
{
29-
if (isConst())
30-
return static_cast<const CompilerConstant *>(static_cast<const CompilerValue *>(this))->value();
31-
else {
32-
static const Value null = Value();
33-
return null;
34-
}
29+
static const Value null = Value();
30+
return null;
3531
}
32+
33+
llvm::Value *value = nullptr;
34+
bool isRawValue = false;
35+
std::shared_ptr<LLVMInstruction> instruction;
3636
};
3737

3838
} // namespace libscratchcpp

src/engine/internal/llvm/llvmregisterbase.h

Lines changed: 0 additions & 29 deletions
This file was deleted.

test/llvm/llvmcodebuilder_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <scratchcpp/variable.h>
99
#include <scratchcpp/list.h>
1010
#include <scratchcpp/blockprototype.h>
11+
#include <scratchcpp/compilerconstant.h>
1112
#include <engine/internal/llvm/llvmcodebuilder.h>
1213
#include <engine/internal/llvm/llvmcompilercontext.h>
1314
#include <gmock/gmock.h>

0 commit comments

Comments
 (0)