Skip to content

Commit 497a64a

Browse files
authored
Merge pull request #7948 from apple/lldb/handle-empty-kern-ver-str-lc-note
[lldb] [Mach-O] don't strip the end of the "kern ver str" LC_NOTE (llvm#77538)
2 parents ea6d9dc + eaab012 commit 497a64a

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5499,8 +5499,6 @@ std::string ObjectFileMachO::GetIdentifierString() {
54995499
uint32_t strsize = payload_size - sizeof(uint32_t);
55005500
std::string result(strsize, '\0');
55015501
m_data.CopyData(payload_offset, strsize, result.data());
5502-
while (result.back() == '\0')
5503-
result.resize(result.size() - 1);
55045502
LLDB_LOGF(log, "LC_NOTE 'kern ver str' found with text '%s'",
55055503
result.c_str());
55065504
return result;
@@ -5520,8 +5518,6 @@ std::string ObjectFileMachO::GetIdentifierString() {
55205518
std::string result(ident_command.cmdsize, '\0');
55215519
if (m_data.CopyData(offset, ident_command.cmdsize, result.data()) ==
55225520
ident_command.cmdsize) {
5523-
while (result.back() == '\0')
5524-
result.resize(result.size() - 1);
55255521
LLDB_LOGF(log, "LC_IDENT found with text '%s'", result.c_str());
55265522
return result;
55275523
}

lldb/test/API/macosx/lc-note/firmware-corefile/TestFirmwareCorefiles.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ def test_lc_note_version_string(self):
2424
aout_exe_basename = "a.out"
2525
aout_exe = self.getBuildArtifact(aout_exe_basename)
2626
verstr_corefile = self.getBuildArtifact("verstr.core")
27+
verstr_corefile_invalid_ident = self.getBuildArtifact(
28+
"verstr-invalid-ident.core"
29+
)
2730
verstr_corefile_addr = self.getBuildArtifact("verstr-addr.core")
2831
create_corefile = self.getBuildArtifact("create-empty-corefile")
2932
slide = 0x70000000000
@@ -36,6 +39,14 @@ def test_lc_note_version_string(self):
3639
+ " 0xffffffffffffffff 0xffffffffffffffff",
3740
shell=True,
3841
)
42+
call(
43+
create_corefile
44+
+ " version-string "
45+
+ verstr_corefile_invalid_ident
46+
+ ' "" '
47+
+ "0xffffffffffffffff 0xffffffffffffffff",
48+
shell=True,
49+
)
3950
call(
4051
create_corefile
4152
+ " version-string "
@@ -71,7 +82,18 @@ def test_lc_note_version_string(self):
7182
self.assertEqual(fspec.GetFilename(), aout_exe_basename)
7283
self.dbg.DeleteTarget(target)
7384

74-
# Second, try the "kern ver str" corefile where it loads at an address
85+
# Second, try the "kern ver str" corefile which has an invalid ident,
86+
# make sure we don't crash.
87+
target = self.dbg.CreateTarget("")
88+
err = lldb.SBError()
89+
if self.TraceOn():
90+
self.runCmd(
91+
"script print('loading corefile %s')" % verstr_corefile_invalid_ident
92+
)
93+
process = target.LoadCore(verstr_corefile_invalid_ident)
94+
self.assertEqual(process.IsValid(), True)
95+
96+
# Third, try the "kern ver str" corefile where it loads at an address
7597
target = self.dbg.CreateTarget("")
7698
err = lldb.SBError()
7799
if self.TraceOn():

lldb/test/API/macosx/lc-note/firmware-corefile/create-empty-corefile.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,16 @@ std::vector<uint8_t> lc_thread_load_command(cpu_type_t cputype) {
8686
void add_lc_note_kern_ver_str_load_command(
8787
std::vector<std::vector<uint8_t>> &loadcmds, std::vector<uint8_t> &payload,
8888
int payload_file_offset, std::string uuid, uint64_t address) {
89-
std::string ident = "EFI UUID=";
90-
ident += uuid;
91-
92-
if (address != 0xffffffffffffffff) {
93-
ident += "; stext=";
94-
char buf[24];
95-
sprintf(buf, "0x%" PRIx64, address);
96-
ident += buf;
89+
std::string ident;
90+
if (!uuid.empty()) {
91+
ident = "EFI UUID=";
92+
ident += uuid;
93+
if (address != 0xffffffffffffffff) {
94+
ident += "; stext=";
95+
char buf[24];
96+
sprintf(buf, "0x%" PRIx64, address);
97+
ident += buf;
98+
}
9799
}
98100

99101
std::vector<uint8_t> loadcmd_data;
@@ -187,6 +189,9 @@ void add_lc_segment(std::vector<std::vector<uint8_t>> &loadcmds,
187189

188190
std::string get_uuid_from_binary(const char *fn, cpu_type_t &cputype,
189191
cpu_subtype_t &cpusubtype) {
192+
if (strlen(fn) == 0)
193+
return {};
194+
190195
FILE *f = fopen(fn, "r");
191196
if (f == nullptr) {
192197
fprintf(stderr, "Unable to open binary '%s' to get uuid\n", fn);
@@ -295,6 +300,10 @@ int main(int argc, char **argv) {
295300
fprintf(stderr, "an LC_NOTE 'main bin spec' load command without an "
296301
"address specified, depending on\n");
297302
fprintf(stderr, "whether the 1st arg is version-string or main-bin-spec\n");
303+
fprintf(stderr, "\nan LC_NOTE 'kern ver str' with no binary provided "
304+
"(empty string filename) to get a UUID\n");
305+
fprintf(stderr, "means an empty 'kern ver str' will be written, an invalid "
306+
"LC_NOTE that lldb should handle.\n");
298307
exit(1);
299308
}
300309
if (strcmp(argv[1], "version-string") != 0 &&

0 commit comments

Comments
 (0)