Skip to content

Commit

Permalink
Store 'raw' md5sum value in note.xrt.uid (#69)
Browse files Browse the repository at this point in the history
* Save raw md5sum value in note.xrt.UID and not its ascii hex representation

Signed-off-by: Sonal Santan <sonal.santan@amd.com>

* Updated md5sum gold files

Signed-off-by: Sonal Santan <sonal.santan@amd.com>

* Use early exit oin add_text_data_section()

Signed-off-by: Sonal Santan <sonal.santan@amd.com>

* Fix the md5sum calculation logic to resolve failures on Windows/Linux

Signed-off-by: Sonal Santan <sonal.santan@amd.com>

* Cleanup the md5sum byte swapping code

Signed-off-by: Sonal Santan <sonal.santan@amd.com>

---------

Signed-off-by: Sonal Santan <sonal.santan@amd.com>
  • Loading branch information
sonals authored Feb 28, 2025
1 parent a6af414 commit 7cdb18a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 49 deletions.
46 changes: 34 additions & 12 deletions src/cpp/aiebu/src/common/uid_md5.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,65 @@
#ifndef _AIEBU_COMMON_UID_MD5_H_
#define _AIEBU_COMMON_UID_MD5_H_

#include <vector>
#include <boost/uuid/detail/md5.hpp>
#include <boost/algorithm/hex.hpp>

namespace aiebu {

unsigned constexpr md5_size = 16;

class uid_md5 {
boost::uuids::detail::md5 hasher;
boost::uuids::detail::md5 hasher;
std::vector<char> sig = std::vector<char>(md5_size, 0);

public:
uid_md5()
{
}
uid_md5() = default;

void update(const std::vector<uint8_t>& data)
{
hasher.process_bytes(data.data(), data.size());
hasher.process_bytes(data.data(), data.size());
}

std::string calculate()
const std::vector<char>& calculate()
{
// Creating local copy of context, so calculate() return same md5sum on every call.
boost::uuids::detail::md5 hasher_copy = hasher;
boost::uuids::detail::md5::digest_type digest;

hasher_copy.get_digest(digest);
std::memcpy(sig.data(), digest, md5_size);

constexpr size_t element_size = sizeof(digest[0]);

if (element_size == 1)
return sig;

std::stringstream md5;
// Different boost versions model digest_type differently:
// 1. typedef unsigned int(digest_type)[4];
// 2. typedef unsigned char digest_type[16];
// The code sets the print width to number of chars needed to print the element type
// used by digest_type. This results in the same string representation for both cases
// which matches with that reported by command line md5sum utility
// For case 1. the following code swaps the bytes of each integer in
// the signature. This solves the little endian issue of integer bytes
// stored in the reverse order than in which they are printed.

auto tcurr = sig.begin();
auto done = sig.end();
while (tcurr < done) {
auto tend = tcurr + element_size;
std::reverse(tcurr, tend);
tcurr = tend;
}

return sig;
}

[[nodiscard]] std::string str() const {
std::stringstream md5;

md5 << std::hex << std::setfill('0');
for (auto ele : digest) {
md5 << std::setw(sizeof(ele) * 2) << (unsigned int)ele;
for (auto ele : sig) {
auto c = (unsigned char)ele;
md5 << std::setw(sizeof(ele) * 2) << (unsigned int)c;
}
return md5.str();
}
Expand Down
68 changes: 34 additions & 34 deletions src/cpp/aiebu/src/elf/elfwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,22 @@ add_dynamic_section_segment()

void
elf_writer::
add_note(ELFIO::Elf_Word type, const std::string& name, const std::string& dec)
add_note(ELFIO::Elf_Word type, const std::string& name, const std::vector<char>& dec)
{
ELFIO::section* note_sec = m_elfio.sections.add( name.c_str() );
note_sec->set_type( ELFIO::SHT_NOTE );
note_sec->set_addr_align( 1 );

ELFIO::note_section_accessor note_writer( m_elfio, note_sec );
note_writer.add_note( type, "XRT", dec.c_str(), dec.size() );
note_writer.add_note( type, "XRT", dec.data(), dec.size() );
}

std::vector<char>
elf_writer::
finalize()
{
std::cout << "UID:" << m_uid.calculate() << "\n";
add_note(NT_XRT_UID, ".note.xrt.UID", m_uid.calculate());
std::cout << "UID:" << m_uid.str() << "\n";
std::stringstream stream;
stream << std::noskipws;
//m_elfio.save( "hello_32" );
Expand All @@ -183,38 +183,38 @@ add_text_data_section(std::vector<writer>& mwriter, std::vector<symbol>& syms)
{
for(auto buffer : mwriter)
{
if(buffer.get_data().size())
if (buffer.get_data().size() == 0)
continue;

m_uid.update(buffer.get_data());
elf_section sec_data;
sec_data.set_name(buffer.get_name());
sec_data.set_type(ELFIO::SHT_PROGBITS);
if (buffer.get_type() == code_section::text)
sec_data.set_flags(ELFIO::SHF_ALLOC | ELFIO::SHF_EXECINSTR);
else
sec_data.set_flags(ELFIO::SHF_ALLOC | ELFIO::SHF_WRITE);
sec_data.set_align(align);
sec_data.set_buffer(buffer.get_data());
sec_data.set_link("");

elf_segment seg_data;
seg_data.set_type(ELFIO::PT_LOAD);
if (buffer.get_type() == code_section::text)
seg_data.set_flags(ELFIO::PF_X | ELFIO::PF_R);
else
seg_data.set_flags(ELFIO::PF_W | ELFIO::PF_R);
seg_data.set_vaddr(0x0);
seg_data.set_paddr(0x0);
seg_data.set_link(buffer.get_name());
seg_data.set_align(text_align);

add_section(sec_data);
add_segment(seg_data);
if (buffer.hassymbols())
{
m_uid.update(buffer.get_data());
elf_section sec_data;
sec_data.set_name(buffer.get_name());
sec_data.set_type(ELFIO::SHT_PROGBITS);
if (buffer.get_type() == code_section::text)
sec_data.set_flags(ELFIO::SHF_ALLOC | ELFIO::SHF_EXECINSTR);
else
sec_data.set_flags(ELFIO::SHF_ALLOC | ELFIO::SHF_WRITE);
sec_data.set_align(align);
sec_data.set_buffer(buffer.get_data());
sec_data.set_link("");

elf_segment seg_data;
seg_data.set_type(ELFIO::PT_LOAD);
if (buffer.get_type() == code_section::text)
seg_data.set_flags(ELFIO::PF_X | ELFIO::PF_R);
else
seg_data.set_flags(ELFIO::PF_W | ELFIO::PF_R);
seg_data.set_vaddr(0x0);
seg_data.set_paddr(0x0);
seg_data.set_link(buffer.get_name());
seg_data.set_align(text_align);

add_section(sec_data);
add_segment(seg_data);
if (buffer.hassymbols())
{
auto lsyms = buffer.get_symbols();
syms.insert(syms.end(), lsyms.begin(), lsyms.end());
}
auto lsyms = buffer.get_symbols();
syms.insert(syms.end(), lsyms.begin(), lsyms.end());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/aiebu/src/elf/elfwriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class elf_writer
void add_dynamic_section_segment();
std::vector<char> finalize();
void add_text_data_section(std::vector<writer>& mwriter, std::vector<symbol>& syms);
void add_note(ELFIO::Elf_Word type, const std::string& name, const std::string& dec);
void add_note(ELFIO::Elf_Word type, const std::string& name, const std::vector<char>& dec);

public:

Expand Down
2 changes: 1 addition & 1 deletion test/aie2-ctrlcode/basic/gold.md5
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8bcab5baf77209dba2a124383324ac3c
2845df67bdf17ba52829164da1aab533
2 changes: 1 addition & 1 deletion test/cpp_test/aie2ps/eff_net_coal/gold.md5
Original file line number Diff line number Diff line change
@@ -1 +1 @@
d43f2a43f5175300eeee04e84366b559
97845ba07a6739ddac7a3767250e5072

0 comments on commit 7cdb18a

Please sign in to comment.