mirror of
https://github.com/KhronosGroup/SPIRV-Tools
synced 2025-01-11 17:10:06 +00:00
Fix leaks in tests (#2295)
Generally, a test fixture in a method that can generate a binary should release any previously cached binary. Similarly for diagnostics.
This commit is contained in:
parent
49b5b0abc6
commit
70404a96ab
@ -197,6 +197,8 @@ ParsedInstruction MakeParsedInt32TypeInstruction(uint32_t result_id) {
|
|||||||
|
|
||||||
class BinaryParseTest : public spvtest::TextToBinaryTestBase<::testing::Test> {
|
class BinaryParseTest : public spvtest::TextToBinaryTestBase<::testing::Test> {
|
||||||
protected:
|
protected:
|
||||||
|
~BinaryParseTest() { spvDiagnosticDestroy(diagnostic_); }
|
||||||
|
|
||||||
void Parse(const SpirvVector& words, spv_result_t expected_result,
|
void Parse(const SpirvVector& words, spv_result_t expected_result,
|
||||||
bool flip_words = false) {
|
bool flip_words = false) {
|
||||||
SpirvVector flipped_words(words);
|
SpirvVector flipped_words(words);
|
||||||
|
@ -34,8 +34,12 @@ using ::testing::HasSubstr;
|
|||||||
|
|
||||||
class BinaryToText : public ::testing::Test {
|
class BinaryToText : public ::testing::Test {
|
||||||
public:
|
public:
|
||||||
BinaryToText() : context(spvContextCreate(SPV_ENV_UNIVERSAL_1_0)) {}
|
BinaryToText()
|
||||||
~BinaryToText() { spvContextDestroy(context); }
|
: context(spvContextCreate(SPV_ENV_UNIVERSAL_1_0)), binary(nullptr) {}
|
||||||
|
~BinaryToText() {
|
||||||
|
spvBinaryDestroy(binary);
|
||||||
|
spvContextDestroy(context);
|
||||||
|
}
|
||||||
|
|
||||||
virtual void SetUp() {
|
virtual void SetUp() {
|
||||||
const char* textStr = R"(
|
const char* textStr = R"(
|
||||||
@ -63,17 +67,20 @@ class BinaryToText : public ::testing::Test {
|
|||||||
spv_diagnostic diagnostic = nullptr;
|
spv_diagnostic diagnostic = nullptr;
|
||||||
spv_result_t error =
|
spv_result_t error =
|
||||||
spvTextToBinary(context, text.str, text.length, &binary, &diagnostic);
|
spvTextToBinary(context, text.str, text.length, &binary, &diagnostic);
|
||||||
if (error) {
|
spvDiagnosticPrint(diagnostic);
|
||||||
spvDiagnosticPrint(diagnostic);
|
spvDiagnosticDestroy(diagnostic);
|
||||||
spvDiagnosticDestroy(diagnostic);
|
ASSERT_EQ(SPV_SUCCESS, error);
|
||||||
ASSERT_EQ(SPV_SUCCESS, error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void TearDown() { spvBinaryDestroy(binary); }
|
virtual void TearDown() {
|
||||||
|
spvBinaryDestroy(binary);
|
||||||
|
binary = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
// Compiles the given assembly text, and saves it into 'binary'.
|
// Compiles the given assembly text, and saves it into 'binary'.
|
||||||
void CompileSuccessfully(std::string text) {
|
void CompileSuccessfully(std::string text) {
|
||||||
|
spvBinaryDestroy(binary);
|
||||||
|
binary = nullptr;
|
||||||
spv_diagnostic diagnostic = nullptr;
|
spv_diagnostic diagnostic = nullptr;
|
||||||
EXPECT_EQ(SPV_SUCCESS, spvTextToBinary(context, text.c_str(), text.size(),
|
EXPECT_EQ(SPV_SUCCESS, spvTextToBinary(context, text.c_str(), text.size(),
|
||||||
&binary, &diagnostic));
|
&binary, &diagnostic));
|
||||||
|
@ -61,7 +61,7 @@ OpFunctionEnd
|
|||||||
; Generator: Khronos SPIR-V Tools Assembler; 0
|
; Generator: Khronos SPIR-V Tools Assembler; 0
|
||||||
; Bound: 9
|
; Bound: 9
|
||||||
; Schema: 0)";
|
; Schema: 0)";
|
||||||
spv_binary binary;
|
spv_binary binary = nullptr;
|
||||||
spv_diagnostic diagnostic;
|
spv_diagnostic diagnostic;
|
||||||
spv_result_t error = spvTextToBinary(context, spirv.c_str(), spirv.size(),
|
spv_result_t error = spvTextToBinary(context, spirv.c_str(), spirv.size(),
|
||||||
&binary, &diagnostic);
|
&binary, &diagnostic);
|
||||||
@ -102,6 +102,7 @@ OpFunctionEnd
|
|||||||
}
|
}
|
||||||
EXPECT_EQ(spirv_header + spirv, output_text->str);
|
EXPECT_EQ(spirv_header + spirv, output_text->str);
|
||||||
spvTextDestroy(output_text);
|
spvTextDestroy(output_text);
|
||||||
|
spvBinaryDestroy(binary);
|
||||||
spvContextDestroy(context);
|
spvContextDestroy(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,6 +61,8 @@ class TextToBinaryTestBase : public T {
|
|||||||
// compilation success. Returns the compiled code.
|
// compilation success. Returns the compiled code.
|
||||||
SpirvVector CompileSuccessfully(const std::string& txt,
|
SpirvVector CompileSuccessfully(const std::string& txt,
|
||||||
spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
|
spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
|
||||||
|
DestroyBinary();
|
||||||
|
DestroyDiagnostic();
|
||||||
spv_result_t status =
|
spv_result_t status =
|
||||||
spvTextToBinary(ScopedContext(env).context, txt.c_str(), txt.size(),
|
spvTextToBinary(ScopedContext(env).context, txt.c_str(), txt.size(),
|
||||||
&binary, &diagnostic);
|
&binary, &diagnostic);
|
||||||
@ -79,6 +81,8 @@ class TextToBinaryTestBase : public T {
|
|||||||
// Returns the error message(s).
|
// Returns the error message(s).
|
||||||
std::string CompileFailure(const std::string& txt,
|
std::string CompileFailure(const std::string& txt,
|
||||||
spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
|
spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
|
||||||
|
DestroyBinary();
|
||||||
|
DestroyDiagnostic();
|
||||||
EXPECT_NE(SPV_SUCCESS,
|
EXPECT_NE(SPV_SUCCESS,
|
||||||
spvTextToBinary(ScopedContext(env).context, txt.c_str(),
|
spvTextToBinary(ScopedContext(env).context, txt.c_str(),
|
||||||
txt.size(), &binary, &diagnostic))
|
txt.size(), &binary, &diagnostic))
|
||||||
@ -94,6 +98,7 @@ class TextToBinaryTestBase : public T {
|
|||||||
uint32_t disassemble_options = SPV_BINARY_TO_TEXT_OPTION_NONE,
|
uint32_t disassemble_options = SPV_BINARY_TO_TEXT_OPTION_NONE,
|
||||||
spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
|
spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
|
||||||
DestroyBinary();
|
DestroyBinary();
|
||||||
|
DestroyDiagnostic();
|
||||||
ScopedContext context(env);
|
ScopedContext context(env);
|
||||||
disassemble_options |= SPV_BINARY_TO_TEXT_OPTION_NO_HEADER;
|
disassemble_options |= SPV_BINARY_TO_TEXT_OPTION_NO_HEADER;
|
||||||
spv_result_t error = spvTextToBinary(context.context, txt.c_str(),
|
spv_result_t error = spvTextToBinary(context.context, txt.c_str(),
|
||||||
@ -126,6 +131,8 @@ class TextToBinaryTestBase : public T {
|
|||||||
// Returns the error message.
|
// Returns the error message.
|
||||||
std::string EncodeSuccessfullyDecodeFailed(
|
std::string EncodeSuccessfullyDecodeFailed(
|
||||||
const std::string& txt, const SpirvVector& words_to_append) {
|
const std::string& txt, const SpirvVector& words_to_append) {
|
||||||
|
DestroyBinary();
|
||||||
|
DestroyDiagnostic();
|
||||||
SpirvVector code =
|
SpirvVector code =
|
||||||
spvtest::Concatenate({CompileSuccessfully(txt), words_to_append});
|
spvtest::Concatenate({CompileSuccessfully(txt), words_to_append});
|
||||||
|
|
||||||
@ -169,6 +176,12 @@ class TextToBinaryTestBase : public T {
|
|||||||
binary = nullptr;
|
binary = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Destroys the diagnostic, if it exists.
|
||||||
|
void DestroyDiagnostic() {
|
||||||
|
spvDiagnosticDestroy(diagnostic);
|
||||||
|
diagnostic = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
spv_diagnostic diagnostic;
|
spv_diagnostic diagnostic;
|
||||||
|
|
||||||
std::string textString;
|
std::string textString;
|
||||||
|
@ -56,6 +56,18 @@ class ValidateBase : public ::testing::Test,
|
|||||||
spv_result_t ValidateAndRetrieveValidationState(
|
spv_result_t ValidateAndRetrieveValidationState(
|
||||||
spv_target_env env = SPV_ENV_UNIVERSAL_1_0);
|
spv_target_env env = SPV_ENV_UNIVERSAL_1_0);
|
||||||
|
|
||||||
|
// Destroys the stored binary.
|
||||||
|
void DestroyBinary() {
|
||||||
|
spvBinaryDestroy(binary_);
|
||||||
|
binary_ = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destroys the stored diagnostic.
|
||||||
|
void DestroyDiagnostic() {
|
||||||
|
spvDiagnosticDestroy(diagnostic_);
|
||||||
|
diagnostic_ = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
std::string getDiagnosticString();
|
std::string getDiagnosticString();
|
||||||
spv_position_t getErrorPosition();
|
spv_position_t getErrorPosition();
|
||||||
spv_validator_options getValidatorOptions();
|
spv_validator_options getValidatorOptions();
|
||||||
@ -67,7 +79,7 @@ class ValidateBase : public ::testing::Test,
|
|||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
ValidateBase<T>::ValidateBase() : binary_(), diagnostic_() {
|
ValidateBase<T>::ValidateBase() : binary_(nullptr), diagnostic_(nullptr) {
|
||||||
// Initialize to default command line options. Different tests can then
|
// Initialize to default command line options. Different tests can then
|
||||||
// specialize specific options as necessary.
|
// specialize specific options as necessary.
|
||||||
options_ = spvValidatorOptionsCreate();
|
options_ = spvValidatorOptionsCreate();
|
||||||
@ -83,14 +95,15 @@ void ValidateBase<T>::TearDown() {
|
|||||||
if (diagnostic_) {
|
if (diagnostic_) {
|
||||||
spvDiagnosticPrint(diagnostic_);
|
spvDiagnosticPrint(diagnostic_);
|
||||||
}
|
}
|
||||||
spvDiagnosticDestroy(diagnostic_);
|
DestroyBinary();
|
||||||
spvBinaryDestroy(binary_);
|
DestroyDiagnostic();
|
||||||
spvValidatorOptionsDestroy(options_);
|
spvValidatorOptionsDestroy(options_);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void ValidateBase<T>::CompileSuccessfully(std::string code,
|
void ValidateBase<T>::CompileSuccessfully(std::string code,
|
||||||
spv_target_env env) {
|
spv_target_env env) {
|
||||||
|
DestroyBinary();
|
||||||
spv_diagnostic diagnostic = nullptr;
|
spv_diagnostic diagnostic = nullptr;
|
||||||
ASSERT_EQ(SPV_SUCCESS,
|
ASSERT_EQ(SPV_SUCCESS,
|
||||||
spvTextToBinary(ScopedContext(env).context, code.c_str(),
|
spvTextToBinary(ScopedContext(env).context, code.c_str(),
|
||||||
@ -98,6 +111,7 @@ void ValidateBase<T>::CompileSuccessfully(std::string code,
|
|||||||
<< "ERROR: " << diagnostic->error
|
<< "ERROR: " << diagnostic->error
|
||||||
<< "\nSPIR-V could not be compiled into binary:\n"
|
<< "\nSPIR-V could not be compiled into binary:\n"
|
||||||
<< code;
|
<< code;
|
||||||
|
spvDiagnosticDestroy(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -110,6 +124,7 @@ void ValidateBase<T>::OverwriteAssembledBinary(uint32_t index, uint32_t word) {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
spv_result_t ValidateBase<T>::ValidateInstructions(spv_target_env env) {
|
spv_result_t ValidateBase<T>::ValidateInstructions(spv_target_env env) {
|
||||||
|
DestroyDiagnostic();
|
||||||
return spvValidateWithOptions(ScopedContext(env).context, options_,
|
return spvValidateWithOptions(ScopedContext(env).context, options_,
|
||||||
get_const_binary(), &diagnostic_);
|
get_const_binary(), &diagnostic_);
|
||||||
}
|
}
|
||||||
@ -117,6 +132,7 @@ spv_result_t ValidateBase<T>::ValidateInstructions(spv_target_env env) {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
spv_result_t ValidateBase<T>::ValidateAndRetrieveValidationState(
|
spv_result_t ValidateBase<T>::ValidateAndRetrieveValidationState(
|
||||||
spv_target_env env) {
|
spv_target_env env) {
|
||||||
|
DestroyDiagnostic();
|
||||||
return spvtools::val::ValidateBinaryAndKeepValidationState(
|
return spvtools::val::ValidateBinaryAndKeepValidationState(
|
||||||
ScopedContext(env).context, options_, get_const_binary()->code,
|
ScopedContext(env).context, options_, get_const_binary()->code,
|
||||||
get_const_binary()->wordCount, &diagnostic_, &vstate_);
|
get_const_binary()->wordCount, &diagnostic_, &vstate_);
|
||||||
|
@ -780,6 +780,8 @@ class OpTypeArrayLengthTest
|
|||||||
// Runs spvValidate() on v, printing any errors via spvDiagnosticPrint().
|
// Runs spvValidate() on v, printing any errors via spvDiagnosticPrint().
|
||||||
spv_result_t Val(const SpirvVector& v, const std::string& expected_err = "") {
|
spv_result_t Val(const SpirvVector& v, const std::string& expected_err = "") {
|
||||||
spv_const_binary_t cbinary{v.data(), v.size()};
|
spv_const_binary_t cbinary{v.data(), v.size()};
|
||||||
|
spvDiagnosticDestroy(diagnostic_);
|
||||||
|
diagnostic_ = nullptr;
|
||||||
const auto status =
|
const auto status =
|
||||||
spvValidate(ScopedContext().context, &cbinary, &diagnostic_);
|
spvValidate(ScopedContext().context, &cbinary, &diagnostic_);
|
||||||
if (status != SPV_SUCCESS) {
|
if (status != SPV_SUCCESS) {
|
||||||
|
Loading…
Reference in New Issue
Block a user