Skip to content

Commit 3b9bf27

Browse files
author
joaosaffran
committed
address errors
1 parent d1b32f3 commit 3b9bf27

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

llvm/include/llvm/Object/DXContainer.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ struct RootConstantView : RootParameterView {
160160
}
161161
};
162162

163+
static Error parseFailed(const Twine &Msg) {
164+
return make_error<GenericBinaryError>(Msg.str(), object_error::parse_failed);
165+
}
166+
163167
class RootSignature {
164168
private:
165169
uint32_t Version = 2;
@@ -192,23 +196,25 @@ class RootSignature {
192196
}
193197
param_header_iterator param_header_end() { return ParametersHeaders.end(); }
194198
uint32_t getFlags() const { return Flags; }
195-
RootParameterView
199+
200+
llvm::Expected<RootParameterView>
196201
getParameter(const dxbc::RootParameterHeader &Header) const {
197202
size_t CorrectOffset = Header.ParameterOffset - ParameterSpaceOffset;
198203
StringRef Data;
199204

200-
size_t DataSize = 0;
205+
size_t DataSize;
201206

202207
switch (Header.ParameterType) {
203208
case dxbc::RootParameterType::Constants32Bit:
204209
DataSize = sizeof(dxbc::RootConstants);
205-
if (CorrectOffset + DataSize > ParameterSpace.size()) {
206-
// throw
207-
}
208-
Data = ParameterSpace.substr(CorrectOffset, DataSize);
209210
break;
210211
}
211212

213+
if (CorrectOffset + DataSize > ParameterSpace.size())
214+
return parseFailed("Reading structure out of file bounds");
215+
216+
Data = ParameterSpace.substr(CorrectOffset, DataSize);
217+
212218
RootParameterView View = RootParameterView(Header, Data);
213219
return View;
214220
}

llvm/lib/ObjectYAML/DXContainerYAML.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@ DXContainerYAML::RootSignatureYamlDesc::RootSignatureYamlDesc(
4545
NewP.Type = PH.ParameterType;
4646
NewP.Visibility = PH.ShaderVisibility;
4747

48-
auto ParamView = Data.getParameter(PH);
48+
llvm::Expected<object::DirectX::RootParameterView> ParamView =
49+
Data.getParameter(PH);
50+
if (!ParamView)
51+
llvm::errs() << "Error: " << ParamView.takeError() << "\n";
52+
auto PV = *ParamView;
4953

50-
if (auto *RCV = dyn_cast<object::DirectX::RootConstantView>(&ParamView)) {
54+
if (auto *RCV = dyn_cast<object::DirectX::RootConstantView>(&PV)) {
5155
auto Constants = RCV->read();
5256
if (!Constants)
5357
llvm::errs() << "Error: " << Constants.takeError() << "\n";

llvm/unittests/Object/DXContainerTest.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -926,9 +926,10 @@ TEST(RootSignature, ParseRootConstant) {
926926
ASSERT_EQ((unsigned)RootParam.ParameterType, 1u);
927927
ASSERT_EQ((unsigned)RootParam.ShaderVisibility, 2u);
928928
auto ParamView = RS.getParameter(RootParam);
929+
ASSERT_THAT_ERROR(ParamView.takeError(), Succeeded());
929930

930931
DirectX::RootConstantView *RootConstantsView =
931-
dyn_cast<DirectX::RootConstantView>(&ParamView);
932+
dyn_cast<DirectX::RootConstantView>(&*ParamView);
932933
ASSERT_TRUE(RootConstantsView != nullptr);
933934
auto Constants = RootConstantsView->read();
934935

@@ -999,11 +1000,8 @@ TEST(RootSignature, ParseRootConstant) {
9991000
const auto &RS = MaybeRS.value();
10001001
auto RootParam = *RS.param_header().begin();
10011002
auto ParamView = RS.getParameter(RootParam);
1002-
DirectX::RootConstantView *RootConstantsView =
1003-
dyn_cast<DirectX::RootConstantView>(&ParamView);
1004-
ASSERT_TRUE(RootConstantsView != nullptr);
1005-
EXPECT_THAT_EXPECTED(
1006-
RootConstantsView->read(),
1003+
ASSERT_THAT_ERROR(
1004+
ParamView.takeError(),
10071005
FailedWithMessage("Reading structure out of file bounds"));
10081006
}
10091007
}

0 commit comments

Comments
 (0)