diff options
author | Lifeng Pan <lifeng.pan@amd.com> | 2017-10-24 18:55:44 +0800 |
---|---|---|
committer | Yaxun (Sam) Liu <yaxun.liu@amd.com> | 2017-10-24 06:55:44 -0400 |
commit | 95f9b420b7e6f821c9c066f0b6bb614a7af998ab (patch) | |
tree | f9f4b832b97e5b0e248be27c7d10be6244693081 | |
parent | dccb034abd065ad290efe77b937756241a4d21ea (diff) |
Decoder.getEntry() may returns NULL (#224)
* Decoder.getEntry() may returns NULL
This happens when SPIR-V binary contains useless word which does not belong to
any instruction. Its high 16-bit is 0, and the decoder discards it.
* Decoder.getEntry() may returns NULL
Add a test which contains 2 useless words.
* Use nullptr instead of NULL
test/SPIRV/useless_word.spt -> test/SPIRV/redundant_word.spt
-rw-r--r-- | lib/SPIRV/libSPIRV/SPIRVModule.cpp | 7 | ||||
-rw-r--r-- | lib/SPIRV/libSPIRV/SPIRVStream.cpp | 2 | ||||
-rw-r--r-- | test/redundant_word.spt | 36 |
3 files changed, 42 insertions, 3 deletions
diff --git a/lib/SPIRV/libSPIRV/SPIRVModule.cpp b/lib/SPIRV/libSPIRV/SPIRVModule.cpp index e381428..1cec3f7 100644 --- a/lib/SPIRV/libSPIRV/SPIRVModule.cpp +++ b/lib/SPIRV/libSPIRV/SPIRVModule.cpp @@ -1504,8 +1504,11 @@ operator>> (std::istream &I, SPIRVModule &M) { Decoder >> MI.InstSchema;
assert(MI.InstSchema == SPIRVISCH_Default && "Unsupported instruction schema");
- while(Decoder.getWordCountAndOpCode())
- M.add(Decoder.getEntry());
+ while (Decoder.getWordCountAndOpCode()) {
+ SPIRVEntry *Entry = Decoder.getEntry();
+ if (Entry != nullptr)
+ M.add(Entry);
+ }
MI.optimizeDecorates();
MI.resolveUnknownStructFields();
diff --git a/lib/SPIRV/libSPIRV/SPIRVStream.cpp b/lib/SPIRV/libSPIRV/SPIRVStream.cpp index c78c2c1..096d49b 100644 --- a/lib/SPIRV/libSPIRV/SPIRVStream.cpp +++ b/lib/SPIRV/libSPIRV/SPIRVStream.cpp @@ -233,7 +233,7 @@ SPIRVDecoder::getWordCountAndOpCode() { SPIRVEntry *
SPIRVDecoder::getEntry() {
if (WordCount == 0 || OpCode == OpNop)
- return NULL;
+ return nullptr;
SPIRVEntry *Entry = SPIRVEntry::create(OpCode);
assert(Entry);
Entry->setModule(&M);
diff --git a/test/redundant_word.spt b/test/redundant_word.spt new file mode 100644 index 0000000..ec099a3 --- /dev/null +++ b/test/redundant_word.spt @@ -0,0 +1,36 @@ +119734787 65536 393230 11 0 +2 Capability Addresses +0 Capability +2 Capability Kernel +5 ExtInstImport 1 "OpenCL.std" +3 MemoryModel 2 2 +6 EntryPoint 6 5 "fmod_kernel" +3 Source 3 200000 +3 Name 6 "out" +3 Name 7 "in1" +3 Name 8 "in2" +4 Name 9 "entry" +4 Name 10 "call" +2 TypeVoid 2 +3 TypeFloat 3 32 +6 TypeFunction 4 2 3 3 3 +0 Nop + +5 Function 2 5 0 4 +3 FunctionParameter 3 6 +3 FunctionParameter 3 7 +3 FunctionParameter 3 8 + +2 Label 9 +7 ExtInst 3 10 1 fmod 7 8 +1 Return + +1 FunctionEnd + + +; FIXME: LIT comments/commands are moved at the end because llvm-spirv stops +; reading the file after first ';' symbol + +; RUN: llvm-spirv %s -to-binary -o %t.spv +; RUN: llvm-spirv -r %t.spv -o %t.bc + |