Skip to content

Commit

Permalink
Avoid double-free in functions cloned for vulkan relaxed mode (Khrono…
Browse files Browse the repository at this point in the history
…sGroup#2987)

* Avoid double-free in functions cloned for vulkan relaxed mode

When rewriting function calls atomicCounterIncrement and
atoicCounterDecrement, clone the parameters so that the TParameter
'type' field is cloned.  This avoids double-free when both the original
and transformed functions are deleted by the parser.

Fixes a ubsan failure.
  • Loading branch information
dneto0 authored Aug 3, 2022
1 parent f0ce653 commit f28022c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
10 changes: 6 additions & 4 deletions glslang/MachineIndependent/ParseHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7035,12 +7035,14 @@ TIntermTyped* TParseContext::vkRelaxedRemapFunctionCall(const TSourceLoc& loc, T

TFunction realFunc(&name, function->getType());

// Use copyParam to avoid shared ownership of the 'type' field
// of the parameter.
for (int i = 0; i < function->getParamCount(); ++i) {
realFunc.addParameter((*function)[i]);
realFunc.addParameter(TParameter().copyParam((*function)[i]));
}

TParameter tmpP = { 0, &uintType };
realFunc.addParameter(tmpP);
realFunc.addParameter(TParameter().copyParam(tmpP));
arguments = intermediate.growAggregate(arguments, intermediate.addConstantUnion(1, loc, true));

result = handleFunctionCall(loc, &realFunc, arguments);
Expand All @@ -7053,11 +7055,11 @@ TIntermTyped* TParseContext::vkRelaxedRemapFunctionCall(const TSourceLoc& loc, T
TFunction realFunc(&name, function->getType());

for (int i = 0; i < function->getParamCount(); ++i) {
realFunc.addParameter((*function)[i]);
realFunc.addParameter(TParameter().copyParam((*function)[i]));
}

TParameter tmpP = { 0, &uintType };
realFunc.addParameter(tmpP);
realFunc.addParameter(TParameter().copyParam(tmpP));
arguments = intermediate.growAggregate(arguments, intermediate.addConstantUnion(-1, loc, true));

result = handleFunctionCall(loc, &realFunc, arguments);
Expand Down
2 changes: 1 addition & 1 deletion glslang/MachineIndependent/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ TFunction::TFunction(const TFunction& copyOf) : TSymbol(copyOf)
for (unsigned int i = 0; i < copyOf.parameters.size(); ++i) {
TParameter param;
parameters.push_back(param);
parameters.back().copyParam(copyOf.parameters[i]);
(void)parameters.back().copyParam(copyOf.parameters[i]);
}

extensions = nullptr;
Expand Down
3 changes: 2 additions & 1 deletion glslang/MachineIndependent/SymbolTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,15 @@ struct TParameter {
TString *name;
TType* type;
TIntermTyped* defaultValue;
void copyParam(const TParameter& param)
TParameter& copyParam(const TParameter& param)
{
if (param.name)
name = NewPoolTString(param.name->c_str());
else
name = 0;
type = param.type->clone();
defaultValue = param.defaultValue;
return *this;
}
TBuiltInVariable getDeclaredBuiltIn() const { return type->getQualifier().declaredBuiltIn; }
};
Expand Down

0 comments on commit f28022c

Please sign in to comment.