Skip to content

Fix use of uninitialized memory for Vector3 constants - #74857

Merged
jkotas merged 5 commits into
dotnet:mainfrom
jkotas:vector3-uninitialized
Aug 31, 2022
Merged

Fix use of uninitialized memory for Vector3 constants#74857
jkotas merged 5 commits into
dotnet:mainfrom
jkotas:vector3-uninitialized

Conversation

@jkotas

@jkotasjkotas commented Aug 31, 2022

Copy link
Copy Markdown
Member

Found while investigating #72149.

In a good case, it made the size and content of readonly data non-deterministic.
In a bad case, it exposed downstream bugs like #72149 intermittently.

@ghostghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Aug 31, 2022
@ghostghost assigned jkotasAug 31, 2022
@ghost

Copy link
Copy Markdown

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Issue Details

null

Author:jkotas
Assignees:-
Labels:

area-CodeGen-coreclr

Milestone:-

@EgorBo

EgorBo commented Aug 31, 2022

Copy link
Copy Markdown
Member

I wonder if it should always be zero in the first place, it looks like that field is not zero-initialized when GenTreeVecCon is created

GenTreeVecCon(var_types type, CorInfoType simdBaseJitType)
: GenTree(GT_CNS_VEC, type), gtSimdBaseJitType((unsignedchar)simdBaseJitType)

and e.g. here it gets a garbage value in the 4th element:

GenTreeVecCon* vecCon = gtNewVconNode(tree->TypeGet(), CORINFO_TYPE_FLOAT);
vecCon->gtSimd12Val = value;
since we create a non-zero initialized veccon tree and set only 12 bytes

@jakobbotsch

jakobbotsch commented Aug 31, 2022

Copy link
Copy Markdown
Member

It is technically UB to access the inactive union member there, so I think the code should rather be changed to access gtSimd12Val and widen it explicitly. There is a similar occurrence in instr.cpp it seems.

Comment threadsrc/coreclr/jit/codegenarm64.cpp Outdated
@jkotas

Copy link
Copy Markdown
MemberAuthor

I wonder if it should always be zero in the first place, it looks like that field is not zero-initialized when GenTreeVecCon is created

I am not following. The line that you have highlighted is initializing gtSimdBaseJitType field. This PR is fixing uninitialized value of gtSimd*Val field.

jkotasand others added 2 commits August 31, 2022 07:21
Co-authored-by: Jakob Botsch Nielsen <Jakob.botsch.nielsen@gmail.com>
@jakobbotsch

Copy link
Copy Markdown
Member

This should get the same fix:

caseTYP_SIMD12:
caseTYP_SIMD16:
{
simd16_t constValue = op->AsVecCon()->gtSimd16Val;
returnOperandDesc(emit->emitSimd16Const(constValue));
}

@jkotas

Copy link
Copy Markdown
MemberAuthor

/backport to release/7.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/7.0: https://github.com/dotnet/runtime/actions/runs/2965504715

@jkotas
jkotas merged commit 02e144e into dotnet:mainAug 31, 2022
@jkotas
jkotas deleted the vector3-uninitialized branch August 31, 2022 18:27
@EgorBo

Copy link
Copy Markdown
Member

I am not following. The line that you have highlighted is initializing gtSimdBaseJitType field.

@jkotas That's the point - it doesn't initialize gtSimd32Val there leaving, potentially, garbage in it, does it?

@jkotas

Copy link
Copy Markdown
MemberAuthor

it doesn't initialize gtSimd32Val there leaving, potentially, garbage in it, does it?

Right. The type of the tree is TYP_SIMD12, so one can naturally expect that the value is stored in gtSimd12Val field.

I think it would be unnatural to require that the top 4 bytes of gtSimd16Val field have to be initialized for TYP_SIMD12 values.

@ghostghost locked as resolved and limited conversation to collaborators Sep 30, 2022
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@jkotas@EgorBo@jakobbotsch