This is an archive of the discontinued LLVM Phabricator instance.

[clang][Interp] Fix dereferencing arrays with no offset applied
ClosedPublic

Authored by tbaeder on Oct 31 2022, 7:51 AM.

Details

Summary

There is a difference between a Pointer and a "Pointer to the first element of an array".

Diff Detail

Event Timeline

tbaeder created this revision.Oct 31 2022, 7:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 31 2022, 7:51 AM
tbaeder requested review of this revision.Oct 31 2022, 7:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 31 2022, 7:51 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

There is a difference between a Pointer and a "Pointer to the first element of an array".

I'm pretty confused because this statement is false per the language standard (http://eel.is/c++draft/expr.sub#2). Basically, array subscripting works through pointer arithmetic, so &array[0] and array (decayed to a pointer) have the same value. Why do we need to offset to get to the first element in the interpreter?

There is a difference between a Pointer and a "Pointer to the first element of an array".

I'm pretty confused because this statement is false per the language standard (http://eel.is/c++draft/expr.sub#2). Basically, array subscripting works through pointer arithmetic, so &array[0] and array (decayed to a pointer) have the same value. Why do we need to offset to get to the first element in the interpreter?

That's just an implementation detail in the Pointer class. For primitive arrays, we need the sizeof(InitMap*) applied, which happens via atIndex(). Otherwise, deref() will look at the first few bytes of the InitMap* pointer.

I've added some documentation about this in https://reviews.llvm.org/D135750 (and the MetadataSize added to Descriptor there could be used to clean this up I think).

aaron.ballman added inline comments.Nov 2 2022, 11:40 AM
clang/lib/AST/Interp/Interp.h
966–967 ↗(On Diff #472007)

So why don't we need to do this dance for Store/StorePop?

tbaeder added inline comments.Nov 3 2022, 12:43 AM
clang/lib/AST/Interp/Interp.h
966–967 ↗(On Diff #472007)

We do, I just realized.

It might make more sense to do this in Pointer::deref() directly, so 'deref()`'ing an array always returns the first element.

tbaeder updated this revision to Diff 472887.Nov 3 2022, 3:35 AM
aaron.ballman accepted this revision.Nov 3 2022, 10:43 AM

LGTM! I think this is a more clear approach than the previous one.

This revision is now accepted and ready to land.Nov 3 2022, 10:43 AM
This revision was landed with ongoing or failed builds.Jan 25 2023, 2:22 AM
This revision was automatically updated to reflect the committed changes.