Merge pull request #200 from SamTebbs33/bugfix/symbol-name

Add newline parsing for symbol map name
This commit is contained in:
Edward Dean 2020-07-23 11:55:34 +01:00 committed by GitHub
commit 387d02f439
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 16 deletions

View file

@ -425,6 +425,8 @@ const FreeListAllocator = struct {
testing.expectEqual(header.next_free, null); testing.expectEqual(header.next_free, null);
testing.expectEqual(free_list.first_free, header); testing.expectEqual(free_list.first_free, header);
std.debug.warn("", .{});
// 64 bytes aligned to 4 bytes // 64 bytes aligned to 4 bytes
const alloc1 = try alloc(allocator, 64, 4, 0); const alloc1 = try alloc(allocator, 64, 4, 0);
const alloc1_addr = @ptrToInt(alloc1.ptr); const alloc1_addr = @ptrToInt(alloc1.ptr);

View file

@ -217,6 +217,28 @@ fn parseNonWhitespace(ptr: [*]const u8, end: *const u8) PanicError![*]const u8 {
return ptr + i; return ptr + i;
} }
///
/// Parse until a newline character. Must be terminated by a newline character before the end
/// address.
///
/// Arguments:
/// IN ptr: [*]const u8 - The address at which to start looking.
/// IN end: *const u8 - The end address at which to start looking. A newline character must
/// be found before this.
///
/// Return: [*]const u8
/// ptr plus the number of non-newline characters consumed.
///
/// Error: PanicError
/// PanicError.InvalidSymbolFile: A terminating newline character wasn't found before the end
/// address.
///
fn parseNonNewLine(ptr: [*]const u8, end: *const u8) PanicError![*]const u8 {
var i: u32 = 0;
while ((try parseChar(ptr + i, end)) != '\n') : (i += 1) {}
return ptr + i;
}
/// ///
/// Parse a name from the pointer up until the end pointer. Must be terminated by a whitespace /// Parse a name from the pointer up until the end pointer. Must be terminated by a whitespace
/// character. /// character.
@ -235,7 +257,7 @@ fn parseNonWhitespace(ptr: [*]const u8, end: *const u8) PanicError![*]const u8 {
/// ///
fn parseName(ptr: *[*]const u8, end: *const u8) PanicError![]const u8 { fn parseName(ptr: *[*]const u8, end: *const u8) PanicError![]const u8 {
const name_start = ptr.*; const name_start = ptr.*;
ptr.* = try parseNonWhitespace(ptr.*, end); ptr.* = try parseNonNewLine(ptr.*, end);
const len = @ptrToInt(ptr.*) - @ptrToInt(name_start); const len = @ptrToInt(ptr.*) - @ptrToInt(name_start);
return name_start[0..len]; return name_start[0..len];
} }
@ -321,78 +343,98 @@ pub fn init(mem_profile: *const mem.MemProfile, allocator: *std.mem.Allocator) !
test "parseChar" { test "parseChar" {
const str: []const u8 = "plutoisthebest"; const str: []const u8 = "plutoisthebest";
const end = @ptrCast(*const u8, str.ptr + 14); const end = @ptrCast(*const u8, str.ptr + str.len);
var char = try parseChar(str.ptr, end); var char = try parseChar(str.ptr, end);
testing.expectEqual(char, 'p'); testing.expectEqual(char, 'p');
char = try parseChar(str.ptr + 1, end); char = try parseChar(str.ptr + 1, end);
testing.expectEqual(char, 'l'); testing.expectEqual(char, 'l');
testing.expectError(PanicError.InvalidSymbolFile, parseChar(str.ptr + 14, end)); testing.expectError(PanicError.InvalidSymbolFile, parseChar(str.ptr + str.len, end));
} }
test "parseWhitespace" { test "parseWhitespace" {
const str: []const u8 = " a"; const str: []const u8 = " a";
const end = @ptrCast(*const u8, str.ptr + 5); const end = @ptrCast(*const u8, str.ptr + str.len);
var ptr = try parseWhitespace(str.ptr, end); var ptr = try parseWhitespace(str.ptr, end);
testing.expectEqual(@ptrToInt(str.ptr) + 4, @ptrToInt(ptr)); testing.expectEqual(@ptrToInt(str.ptr) + 4, @ptrToInt(ptr));
} }
test "parseWhitespace fails without a terminating whitespace" { test "parseWhitespace fails without a terminating whitespace" {
const str: []const u8 = " "; const str: []const u8 = " ";
const end = @ptrCast(*const u8, str.ptr + 3); const end = @ptrCast(*const u8, str.ptr + str.len);
testing.expectError(PanicError.InvalidSymbolFile, parseWhitespace(str.ptr, end)); testing.expectError(PanicError.InvalidSymbolFile, parseWhitespace(str.ptr, end));
} }
test "parseNonWhitespace" { test "parseNonWhitespace" {
const str: []const u8 = "ab "; const str: []const u8 = "ab ";
const end = @ptrCast(*const u8, str.ptr + 3); const end = @ptrCast(*const u8, str.ptr + str.len);
var ptr = try parseNonWhitespace(str.ptr, end); var ptr = try parseNonWhitespace(str.ptr, end);
testing.expectEqual(@ptrToInt(str.ptr) + 2, @ptrToInt(ptr)); testing.expectEqual(@ptrToInt(str.ptr) + 2, @ptrToInt(ptr));
} }
test "parseNonWhitespace fails without a terminating whitespace" { test "parseNonWhitespace fails without a terminating whitespace" {
const str: []const u8 = "abc"; const str: []const u8 = "abc";
const end = @ptrCast(*const u8, str.ptr + 3); const end = @ptrCast(*const u8, str.ptr + str.len);
testing.expectError(PanicError.InvalidSymbolFile, parseNonWhitespace(str.ptr, end)); testing.expectError(PanicError.InvalidSymbolFile, parseNonWhitespace(str.ptr, end));
} }
test "parseNonNewLine" {
const str: []const u8 = "ab\n";
const end = @ptrCast(*const u8, str.ptr + str.len);
var ptr = try parseNonNewLine(str.ptr, end);
testing.expectEqual(@ptrToInt(str.ptr) + 2, @ptrToInt(ptr));
}
test "parseNonNewLine fails without a terminating newline" {
const str: []const u8 = "abc";
const end = @ptrCast(*const u8, str.ptr + str.len);
testing.expectError(PanicError.InvalidSymbolFile, parseNonNewLine(str.ptr, end));
}
test "parseAddr" { test "parseAddr" {
const str: []const u8 = "1a2b3c4d "; const str: []const u8 = "1a2b3c4d ";
const end = @ptrCast(*const u8, str.ptr + 9); const end = @ptrCast(*const u8, str.ptr + str.len);
var ptr = str.ptr; var ptr = str.ptr;
testing.expectEqual(try parseAddr(&ptr, end), 0x1a2b3c4d); testing.expectEqual(try parseAddr(&ptr, end), 0x1a2b3c4d);
} }
test "parseAddr fails without a terminating whitespace" { test "parseAddr fails without a terminating whitespace" {
const str: []const u8 = "1a2b3c4d"; const str: []const u8 = "1a2b3c4d";
const end = @ptrCast(*const u8, str.ptr + 9); const end = @ptrCast(*const u8, str.ptr + str.len);
var ptr = str.ptr; var ptr = str.ptr;
testing.expectError(PanicError.InvalidSymbolFile, parseAddr(&ptr, end)); testing.expectError(PanicError.InvalidSymbolFile, parseAddr(&ptr, end));
} }
test "parseAddr fails with an invalid integer" { test "parseAddr fails with an invalid integer" {
const str: []const u8 = "1g2t "; const str: []const u8 = "1g2t ";
const end = @ptrCast(*const u8, str.ptr + 5); const end = @ptrCast(*const u8, str.ptr + str.len);
var ptr = str.ptr; var ptr = str.ptr;
testing.expectError(error.InvalidCharacter, parseAddr(&ptr, end)); testing.expectError(error.InvalidCharacter, parseAddr(&ptr, end));
} }
test "parseName" { test "parseName" {
const str: []const u8 = "func_name "; const str: []const u8 = "func_name\n";
const end = @ptrCast(*const u8, str.ptr + 10); const end = @ptrCast(*const u8, str.ptr + str.len);
var ptr = str.ptr; var ptr = str.ptr;
testing.expectEqualSlices(u8, try parseName(&ptr, end), "func_name"); testing.expectEqualSlices(u8, try parseName(&ptr, end), "func_name");
} }
test "parseName fails without a terminating whitespace" { test "parseName with spaces" {
const str: []const u8 = "func_name(*const type )\n";
const end = @ptrCast(*const u8, str.ptr + str.len);
var ptr = str.ptr;
testing.expectEqualSlices(u8, try parseName(&ptr, end), "func_name(*const type )");
}
test "parseName fails without a terminating newline" {
const str: []const u8 = "func_name"; const str: []const u8 = "func_name";
const end = @ptrCast(*const u8, str.ptr + 9); const end = @ptrCast(*const u8, str.ptr + str.len);
var ptr = str.ptr; var ptr = str.ptr;
testing.expectError(PanicError.InvalidSymbolFile, parseName(&ptr, end)); testing.expectError(PanicError.InvalidSymbolFile, parseName(&ptr, end));
} }
test "parseMapEntry" { test "parseMapEntry" {
const str: []const u8 = "1a2b3c4d func_name\n5e6f7a8b func_name2\n"; const str: []const u8 = "1a2b3c4d func_name\n5e6f7a8b func_name2\n";
const end = @ptrCast(*const u8, str.ptr + 39); const end = @ptrCast(*const u8, str.ptr + str.len);
var ptr = str.ptr; var ptr = str.ptr;
var actual = try parseMapEntry(&ptr, end); var actual = try parseMapEntry(&ptr, end);
@ -406,7 +448,7 @@ test "parseMapEntry" {
testing.expectEqualSlices(u8, actual.func_name, expected.func_name); testing.expectEqualSlices(u8, actual.func_name, expected.func_name);
} }
test "parseMapEntry fails without a terminating whitespace" { test "parseMapEntry fails without a terminating newline" {
const str: []const u8 = "1a2b3c4d func_name"; const str: []const u8 = "1a2b3c4d func_name";
var ptr = str.ptr; var ptr = str.ptr;
testing.expectError(PanicError.InvalidSymbolFile, parseMapEntry(&ptr, @ptrCast(*const u8, str.ptr + 18))); testing.expectError(PanicError.InvalidSymbolFile, parseMapEntry(&ptr, @ptrCast(*const u8, str.ptr + 18)));