Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spirv-fuzz: add tests for full coverage of TransformationAccessChain #4304

Merged
merged 20 commits into from
Jun 2, 2021

Conversation

sliu-UIUC
Copy link
Contributor

@sliu-UIUC sliu-UIUC commented May 26, 2021

This PR will fix #4286.

Update:
Fixed redundant code. Improved comments and styles. Achieved 100% coverage.

Copy link
Contributor

@afd afd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - this looks good! A few comments about your comments :)

I agree with your analysis that the if condition can never be true.

I suggest we do a few changes to make the code clearer.

First, since GetIndexValue is only called on structs, let's change its name to GetStructIndexValue

Second, let's change its comment (in the header file) to reflect this, e.g. remove:

  // - the object being indexed is not a composite type

and start the whole comment with:

  // Requires that |object_type_id| is a struct type

Add an assertion at the top of the function to check that this is true:

assert(ir_context->get_def_use_mgr()->GetDef(object_type_id) == SpvOpTypeStruct && "Precondition: the type must be a struct type.");

And finally, change the redundant if condition into an assertion:

assert(spvOpcodeIsConstant(index_instruction->opcode()) && "A non-constant index should already have been rejected.");

In the process of investigating this I wrote the test below. Even though it doesn't increase coverage I think we may as well include it. So can you please add it to your PR?

Thanks!

TEST(TransformationAccessChainTest, StructIndexMustBeConstant) {
  std::string shader = R"(
               OpCapability Shader
          %1 = OpExtInstImport "GLSL.std.450"
               OpMemoryModel Logical GLSL450
               OpEntryPoint Fragment %4 "main"
               OpExecutionMode %4 OriginUpperLeft
               OpSource ESSL 320
          %2 = OpTypeVoid
          %3 = OpTypeFunction %2
          %6 = OpTypeInt 32 1
         %20 = OpUndef %6
          %7 = OpTypeStruct %6 %6
          %8 = OpTypePointer Function %7
         %10 = OpConstant %6 0
         %11 = OpConstant %6 2
         %12 = OpTypePointer Function %6
          %4 = OpFunction %2 None %3
          %5 = OpLabel
          %9 = OpVariable %8 Function
               OpReturn
               OpFunctionEnd
  )";

  const auto env = SPV_ENV_UNIVERSAL_1_4;
  const auto consumer = nullptr;
  const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  spvtools::ValidatorOptions validator_options;
  ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
                                               kConsoleMessageConsumer));
  TransformationContext transformation_context(
      MakeUnique<FactManager>(context.get()), validator_options);
  // Bad: %9 is a pointer to a struct, but %20 is not a constant.
  ASSERT_FALSE(TransformationAccessChain(
                   100, 9, {20}, MakeInstructionDescriptor(9, SpvOpReturn, 0))
                   .IsApplicable(context.get(), transformation_context));
}

test/fuzz/transformation_access_chain_test.cpp Outdated Show resolved Hide resolved
test/fuzz/transformation_access_chain_test.cpp Outdated Show resolved Hide resolved
test/fuzz/transformation_access_chain_test.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@afd afd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved with one nit about an unnecessary whitespace change. Please revert that, and then we can run the CI bots and merge.

test/fuzz/transformation_access_chain_test.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@afd afd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The debug builds fail due to a comparison between a pointer and an enum value.

source/fuzz/transformation_access_chain.cpp Outdated Show resolved Hide resolved
@sliu-UIUC
Copy link
Contributor Author

test/fuzz/fuzzerutil_test.cpp can be ignored for this PR as it is implemented in another MaybeTest PR. Sorry for including it.

afd and others added 7 commits June 1, 2021 14:05
The def-use manager was being incorrectly updated in
TransformationPermutePhiOperands, and this was causing future
transformations to go wrong during fuzzing. This change updates the
def-use manager in a correct manner, and adds a test exposing the
previous bug.

Fixes KhronosGroup#4300.
…ronosGroup#4308)

This change prevents TransformationOutlineFunction from outlining a
region of blocks if some block in the region has an unreachable
predecessor. This avoids a bug whereby the region would be outlined,
and the unreachable predecessors would be left behind, referring to
blocks that are no longer in the function.
Copy link
Contributor

@afd afd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! The only thing is that you don't have a new line at the end of the file.

source/fuzz/transformation_access_chain.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@afd afd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@afd afd added the kokoro:run label Jun 2, 2021
@afd afd merged commit 26cdce9 into KhronosGroup:master Jun 2, 2021
jkwak-work added a commit to shader-slang/SPIRV-Tools that referenced this pull request Jul 2, 2024
Merging '6a2bdee' from http://github.com/KhronosGroup/SPIRV-Tools.git
This merge is required to address an issue KhronosGroup#4304.

> commit 6a2bdee
> Author: Nathan Gauër <brioche@google.com>
> Date:   Tue Jun 4 16:18:06 2024 +0200
>     spirv-val, core: add support for OpExtInstWithForwardRefs (KhronosGroup#5698)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Achieve full test coverage of TransformationAccessChain
4 participants