<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/101716>101716</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [PAC][clang] Fix address discrimination for type_info vtable pointers
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          kovdan01
      </td>
    </tr>
</table>

<pre>
    In #99726, `-fptrauth-type-info-vtable-pointer-discrimination` was introduced, which is intended to enable type and address discrimination for type_info vtable pointers.

However, some codegen logic for actually enabling address discrimination is missing. Particularly, in `ItaniumRTTIBuilder::BuildVTablePointer` (clang/lib/CodeGen/ItaniumCXXABI.cpp), there is the following piece of code:

```
  if (auto &Schema = CGM.getCodeGenOpts().PointerAuth.CXXTypeInfoVTablePointer)
    VTable = CGM.getConstantSignedPointer(VTable, Schema, nullptr, GlobalDecl(),
 QualType(Ty, 0));
```

Here, `nullptr` is used as `StorageAddress` unconditionally, so, address discrimination is not actually enabled even if requested. It caused test-suite failures in several EH-related tests.

I was able to fix that locally by just using a dummy `ptr inttoptr (i64 1 to ptr)` value as `StorageAddress` (just like I did with init/fini, see https://github.com/llvm/llvm-project/pull/96478#issuecomment-2196819332), and tests became passing. I'm not sure how to get a proper `StorageAddress` here, so I've used that dummy placeholder which actually seems to do the job.

An existing test clang/test/CodeGenCXX/ptrauth-type-info-vtable.cpp shows such incorrect behavior. Particularly, in line 55, we have

```
ptr ptrauth (ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv117__class_type_infoE, i64 2), i32 2, i64 [[STDTYPEINFO_DISC]])
```

This has constant discriminator, but does not have address discrimination, while it should be enabled with `-fptrauth-type-info-vtable-pointer-discrimination`. The correct output should be smth like (if we use a placeholder value `ptr inttoptr (i64 1 to ptr)` as storage address)

```
ptr ptrauth (ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv117__class_type_infoE, i64 2), i32 2, i64 [[STDTYPEINFO_DISC]], ptr inttoptr (i64 1 to ptr))
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUVk2T2rgW_TVicwvKFmDwggUNIWHxkn6vqVS_2VCydY2VyJLHkqD591NXNp3ume7UTHZTRYGN7HvuxzlHEs6pk0Fcsfkdm29HIvjadqvv9iyFSdJRYeV1tTfA-DTPFzxjfAMsS8ZV6zt6duyvLY6Vqez47EWhcdxaZTx2Y6lc2alGGeGVNSxL4CIcKOM7K0OJkiJdalXWoOLfaCRK8BbQUBygwCCMBCFlh87B64BQ2S4-cyRw6MFhAHcTlmxZsu6_P9kLnrEjQGcbhNJKPKEBbU-qjHFE6YPQ-tpjK3N6D1Q5aJRzypwmcC86r8qgRaevFFsZ6szeC6NC87_DYX8XlJbYsemaTdfx5uuBsrzvk6SWML4stTAnxndaFYzvNlbiRzSM74ZAm8fH9d1-UrYt4znB-Bo7pER8jVBZre2FEm4Vlgi2itUR5IsGsCwZPvEWQFWELIK3wHj2UNbYCGDTLWw-_mdyQj9k8aX1jvEl4_lkyHkdfD3ZPD4eri3uTWVfF8TzGwBAv_I6qHFeGP9AhJPP7yz7J6m0PhG6MkHr1seRfdS2EHqLpe5TYXwzoPw3CE2JML48xAkkcTln07s36x7YgB0OLL6hZAn1MziUIBwtPHjbiROuexLQejClNVIRCYgoPZfo-32iGOv_RCyUgGc01P4Ofw_oPMoJ7D2UIoJ7dH7sgvIIlVA6dEjKAEfsFRo-fBp3qIUfnnxN8n2UV68cC5V6Al8LD9qWEb-4wrfgPAQX2Q0yNM2VSm19R-rzli4YX6psBimFiO3Pqfaz0AHf7QzjyxhZq-8Ie5BKwkX5GpRRnvFdpYyK3UKE2vvWETf5jvHdSfk6FJPSNsR-fb79jNvOfsOSXm6D1ozv8my2WDI-Vc4FLG3ToPFjnubZMs2nUz4Ig7wi9gUKLEWD0IpBqXvGF02chwsdQm0vVOAJPQhoO9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