Skip to content

Commit

Permalink
Remove the implicit conversion from span<T> to T* and instead introdu…
Browse files Browse the repository at this point in the history
…ce a .data() member, matching the C++20 design for span<T>
  • Loading branch information
jkoritzinsky committed Nov 1, 2024
1 parent b37fa88 commit b698b77
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 40 deletions.
18 changes: 9 additions & 9 deletions src/native/dnmd/src/inc/internal/dnmd_tools_platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
inline bool create_mdhandle(malloc_span<uint8_t> const& buffer, mdhandle_ptr& handle)
{
mdhandle_t h;
if (!md_create_handle(buffer, buffer.size(), &h))
if (!md_create_handle(buffer.data(), buffer.size(), &h))
return false;
handle.reset(h);
return true;
Expand Down Expand Up @@ -71,7 +71,7 @@ inline bool read_in_file(char const* file, malloc_span<uint8_t>& b)
return false;

b = { (uint8_t*)std::malloc(size), size };
fd.read((char*)(uint8_t*)b, b.size());
fd.read((char*)b.data(), b.size());
return true;
}

Expand All @@ -82,7 +82,7 @@ inline bool write_out_file(char const* file, malloc_span<uint8_t> b)
if (!fd)
return false;

fd.write((char*)(uint8_t*)b, b.size());
fd.write((char*)b.data(), b.size());
return true;
}

Expand All @@ -95,7 +95,7 @@ inline bool find_pe_image_bitness(uint16_t machine, uint8_t& bitness)
case ((x) ^ IMAGE_FILE_MACHINE_OS_MASK_NETBSD): \
case ((x) ^ IMAGE_FILE_MACHINE_OS_MASK_SUN): \
case (x)

switch (machine)
{
MAKE_MACHINE_CASE(IMAGE_FILE_MACHINE_I386):
Expand All @@ -120,7 +120,7 @@ inline bool get_metadata_from_pe(malloc_span<uint8_t>& b)

// [TODO] Handle endian issues with .NET generated PE images
// All integers should be read as little-endian.
auto dos_header = (PIMAGE_DOS_HEADER)(void*)b;
auto dos_header = (PIMAGE_DOS_HEADER)(void*)b.data();
bool is_pe = dos_header->e_magic == IMAGE_DOS_SIGNATURE;
if (!is_pe)
return false;
Expand All @@ -138,7 +138,7 @@ inline bool get_metadata_from_pe(malloc_span<uint8_t>& b)
size_t remaining_pe_size = b.size() - dos_header->e_lfanew;
uint16_t section_header_count;
uint8_t* section_header_begin;
auto nt_header_any = (PIMAGE_NT_HEADERS)(b + dos_header->e_lfanew);
auto nt_header_any = (PIMAGE_NT_HEADERS)(b.data() + dos_header->e_lfanew);
uint16_t machine = nt_header_any->FileHeader.Machine;

uint8_t bitness;
Expand Down Expand Up @@ -196,7 +196,7 @@ inline bool get_metadata_from_pe(malloc_span<uint8_t>& b)
if (cor_header_offset > b.size() - sizeof(IMAGE_COR20_HEADER))
return false;

auto cor_header = (PIMAGE_COR20_HEADER)(b + cor_header_offset);
auto cor_header = (PIMAGE_COR20_HEADER)(b.data() + cor_header_offset);
tgt_header = find_section_header(section_headers, cor_header->MetaData.VirtualAddress);
if (tgt_header == nullptr)
return false;
Expand All @@ -209,15 +209,15 @@ inline bool get_metadata_from_pe(malloc_span<uint8_t>& b)
if (metadata_offset > b.size())
return false;

void* ptr = (void*)(b + metadata_offset);
void* ptr = (void*)(b.data() + metadata_offset);

size_t metadata_length = cor_header->MetaData.Size;
if (metadata_length > b.size() - metadata_offset)
return false;

// Capture the metadata portion of the image.
malloc_span<uint8_t> metadata = { (uint8_t*)std::malloc(metadata_length), metadata_length };
std::memcpy(metadata, ptr, metadata.size());
std::memcpy(metadata.data(), ptr, metadata.size());
b = std::move(metadata);
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions src/native/dnmd/src/inc/internal/span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ class span
return _size;
}

operator T* () noexcept
T* data() noexcept
{
return _ptr;
}

operator T const* () const noexcept
T const* data() const noexcept
{
return _ptr;
}
Expand Down Expand Up @@ -137,7 +137,7 @@ span<T> slice(span<T> b, size_t offset)
{
if (offset > b.size())
throw std::out_of_range{ "Out of bounds access" };
return { b + offset, b.size() - offset };
return { b.data() + offset, b.size() - offset };
}

#endif // _SRC_INC_INTERNAL_SPAN_HPP_
#endif // _SRC_INC_INTERNAL_SPAN_HPP_
16 changes: 5 additions & 11 deletions src/native/dnmd/src/interfaces/importhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,13 @@ namespace
if (publicKeyBlob.size() < sizeof(PublicKeyBlob))
return CORSEC_E_INVALID_PUBLICKEY;

PublicKeyBlob const* publicKey = reinterpret_cast<PublicKeyBlob const*>((uint8_t const*)publicKeyBlob);
PublicKeyBlob const* publicKey = reinterpret_cast<PublicKeyBlob const*>(publicKeyBlob.data());

if (publicKey->PublicKeyLength != publicKeyBlob.size() - sizeof(PublicKeyBlob))
return CORSEC_E_INVALID_PUBLICKEY;

if (publicKeyBlob.size() == sizeof(StrongNameKeys::EcmaPublicKey)
&& std::memcmp(publicKeyBlob, StrongNameKeys::EcmaPublicKey, sizeof(StrongNameKeys::EcmaPublicKey)) == 0)
&& std::memcmp(publicKeyBlob.data(), StrongNameKeys::EcmaPublicKey, sizeof(StrongNameKeys::EcmaPublicKey)) == 0)
{
return S_OK;
}
Expand Down Expand Up @@ -685,14 +685,10 @@ namespace
if (!md_set_column_value_as_utf8(assemblyRef, mdtAssemblyRef_Culture, assemblyCulture))
return E_FAIL;

uint8_t const* hash = sourceAssemblyHash;
uint32_t hashLength = (uint32_t)sourceAssemblyHash.size();
if (!md_set_column_value_as_blob(assemblyRef, mdtAssemblyRef_HashValue, hash, hashLength))
if (!md_set_column_value_as_blob(assemblyRef, mdtAssemblyRef_HashValue, sourceAssemblyHash.data(), (uint32_t)sourceAssemblyHash.size()))
return E_FAIL;

uint8_t const* publicKeyTokenBlob = publicKeyToken.data();
uint32_t publicKeyTokenLength = (uint32_t)publicKeyToken.size();
if (!md_set_column_value_as_blob(assemblyRef, mdtAssemblyRef_PublicKeyOrToken, publicKeyTokenBlob, publicKeyTokenLength))
if (!md_set_column_value_as_blob(assemblyRef, mdtAssemblyRef_PublicKeyOrToken, publicKeyToken.data(), (uint32_t)publicKeyToken.size()))
return E_FAIL;

*targetAssembly = assemblyRef;
Expand Down Expand Up @@ -1704,9 +1700,7 @@ HRESULT ImportReferenceToTypeDefOrRefOrSpec(
if (!md_append_row(targetModule, mdtid_TypeSpec, &typeSpec))
return E_FAIL;

uint8_t const* importedSignatureData = importedSignature;
uint32_t importedSignatureLength = (uint32_t)importedSignature.size();
if (!md_set_column_value_as_blob(typeSpec, mdtTypeSpec_Signature, importedSignatureData, importedSignatureLength))
if (!md_set_column_value_as_blob(typeSpec, mdtTypeSpec_Signature, importedSignature.data(), (uint32_t)importedSignature.size()))
return E_FAIL;

if (!md_cursor_to_token(typeSpec, importedToken))
Expand Down
2 changes: 1 addition & 1 deletion src/native/dnmd/src/interfaces/metadataimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ HRESULT STDMETHODCALLTYPE MetadataImportRO::FindMethod(
if (!md_get_column_value_as_blob(target, mdtMethodDef_Signature, &sig, &sigLen))
return CLDB_E_FILE_CORRUPT;
if (sigLen != methodDefSig.size()
|| ::memcmp(methodDefSig, sig, sigLen) != 0)
|| ::memcmp(methodDefSig.data(), sig, sigLen) != 0)
{
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/native/dnmd/src/interfaces/pal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ int strcat_s(char* dest, rsize_t destsz, char const* src)

bool pal::ComputeSha1Hash(span<uint8_t const> data, std::array<uint8_t, SHA1_HASH_SIZE>& hashDestination)
{
minipal_sha1(data, data.size(), hashDestination.data(), SHA1_HASH_SIZE);
minipal_sha1(data.data(), data.size(), hashDestination.data(), SHA1_HASH_SIZE);
return true;
}

Expand Down
12 changes: 6 additions & 6 deletions src/native/dnmd/src/interfaces/signatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ namespace
std::tuple<uint32_t, span<T>> read_compressed_uint(span<T> signature)
{
ULONG value = 0;
signature = slice(signature, CorSigUncompressData(signature, &value));
signature = slice(signature, CorSigUncompressData(signature.data(), &value));
return std::make_tuple(value, signature);
}

template<typename T, typename = typename std::enable_if<std::is_same<typename std::remove_const<T>::type, uint8_t>::value>::type>
std::tuple<int32_t, span<T>> read_compressed_int(span<T> signature)
{
int value = 0;
signature = slice(signature, CorSigUncompressSignedInt(signature, &value));
signature = slice(signature, CorSigUncompressSignedInt(signature.data(), &value));
return std::make_tuple(value, signature);
}

template<typename T, typename = typename std::enable_if<std::is_same<typename std::remove_const<T>::type, uint8_t>::value>::type>
std::tuple<mdToken, span<T>> read_compressed_token(span<T> signature)
{
mdToken value = mdTokenNil;
signature = slice(signature, CorSigUncompressToken(signature, &value));
signature = slice(signature, CorSigUncompressToken(signature.data(), &value));
return std::make_tuple(value, signature);
}

Expand Down Expand Up @@ -272,14 +272,14 @@ void GetMethodDefSigFromMethodRefSig(span<uint8_t> methodRefSig, inline_span<uin
methodDefSig[offset++] = callingConvention;
if ((callingConvention & IMAGE_CEE_CS_CALLCONV_GENERIC) == IMAGE_CEE_CS_CALLCONV_GENERIC)
{
offset += CorSigCompressData(genericParameterCount, methodDefSig + offset);
offset += CorSigCompressData(genericParameterCount, methodDefSig.data() + offset);
}
std::memcpy(methodDefSig + offset, compressedNewParamCount, newParamCountCompressedSize);
std::memcpy(methodDefSig.data() + offset, compressedNewParamCount.data(), newParamCountCompressedSize);
offset += newParamCountCompressedSize;

// Now that we've re-written the parameter count, we can copy the rest of the signature directly from the MethodRefSig
assert(returnTypeAndParameters.size() >= methodDefSigBufferLength - offset);
std::memcpy(methodDefSig + offset, returnTypeAndParameters, methodDefSigBufferLength - offset);
std::memcpy(methodDefSig.data() + offset, returnTypeAndParameters.data(), methodDefSigBufferLength - offset);

return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/native/dnmd/src/mdmerge/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void merge(merge_config_t cfg)
size_t save_size;
md_write_to_buffer(handle.get(), nullptr, &save_size);
malloc_span<uint8_t> out_buffer { (uint8_t*)malloc(save_size), save_size };
if (!md_write_to_buffer(handle.get(), out_buffer, &save_size))
if (!md_write_to_buffer(handle.get(), out_buffer.data(), &save_size))
{
std::fprintf(stderr, "Failed to save image.\n");
}
Expand Down
2 changes: 1 addition & 1 deletion src/native/dnmd/test/regpal/pal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ bool pal::ReadFile(pal::path path, malloc_span<uint8_t>& b)
b = { (uint8_t*)std::malloc(size), size };

DWORD bytesRead;
if (!ReadFile(file.get(), b, (DWORD)b.size(), &bytesRead, nullptr))
if (!::ReadFile(file.get(), b.data(), (DWORD)b.size(), &bytesRead, nullptr))
return false;

return bytesRead == b.size();
Expand Down
2 changes: 1 addition & 1 deletion src/native/dnmd/test/regperf/perf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ int main(int argc, char** argv)
}

RETURN_IF_FAILED(PerfInitialize(
dataImage,
dataImage.data(),
(uint32_t)dataImage.size()));

benchmark::Initialize(&argc, argv);
Expand Down
2 changes: 1 addition & 1 deletion src/native/dnmd/test/regtest/discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ namespace
THROW_IF_FAILED(image->GetSaveSize(cssAccurate, &size));

malloc_span<uint8_t> imageWithIndirectionTables{ (uint8_t*)malloc(size), size };
THROW_IF_FAILED(image->SaveToMemory(imageWithIndirectionTables, size));
THROW_IF_FAILED(image->SaveToMemory(imageWithIndirectionTables.data(), size));

return imageWithIndirectionTables;
}
Expand Down
8 changes: 4 additions & 4 deletions src/native/dnmd/test/regtest/metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1624,14 +1624,14 @@ TEST(FindTest, FindAPIs)
malloc_span<uint8_t> metadata = GetRegressionAssemblyMetadata();

minipal::com_ptr<IMetaDataImport2> baselineImport;
ASSERT_HRESULT_SUCCEEDED(CreateImport(TestBaseline::Metadata, metadata, (uint32_t)metadata.size(), &baselineImport));
ASSERT_HRESULT_SUCCEEDED(CreateImport(TestBaseline::Metadata, metadata.data(), (uint32_t)metadata.size(), &baselineImport));
// Load metadata
minipal::com_ptr<IMetaDataImport2> currentImport;

minipal::com_ptr<IMetaDataDispenser> dispenser;
ASSERT_HRESULT_SUCCEEDED(GetDispenser(IID_IMetaDataDispenser, (void**)&dispenser));

ASSERT_HRESULT_SUCCEEDED(CreateImport(dispenser, metadata, (uint32_t)metadata.size(), &currentImport));
ASSERT_HRESULT_SUCCEEDED(CreateImport(dispenser, metadata.data(), (uint32_t)metadata.size(), &currentImport));

static auto FindTokenByName = [](IMetaDataImport2* import, LPCWSTR name, mdToken enclosing = mdTokenNil) -> mdToken
{
Expand Down Expand Up @@ -1767,7 +1767,7 @@ TEST_P(MetadataImportTest, ImportAPIs)
{
auto param = GetParam();
span<uint8_t> blob = GetMetadataForFile(param);
void const* data = blob;
void const* data = blob.data();
uint32_t dataLen = (uint32_t)blob.size();

// Load metadata
Expand Down Expand Up @@ -2004,7 +2004,7 @@ TEST_P(MetaDataLongRunningTest, ImportAPIs)
{
auto param = GetParam();
span<uint8_t> blob = GetMetadataForFile(param);
void const* data = blob;
void const* data = blob.data();
uint32_t dataLen = (uint32_t)blob.size();

// Load metadata
Expand Down

0 comments on commit b698b77

Please sign in to comment.