Skip to content

Commit 4183c6f

Browse files
committed
move std/debug.zig to a subdirectory
self hosted compiler parser tests do some fuzz testing
1 parent 9dae796 commit 4183c6f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+297
-223
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,8 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/c/index.zig" DESTINATION "${ZIG_STD_DEST}
516516
install(FILES "${CMAKE_SOURCE_DIR}/std/c/linux.zig" DESTINATION "${ZIG_STD_DEST}/c")
517517
install(FILES "${CMAKE_SOURCE_DIR}/std/c/windows.zig" DESTINATION "${ZIG_STD_DEST}/c")
518518
install(FILES "${CMAKE_SOURCE_DIR}/std/cstr.zig" DESTINATION "${ZIG_STD_DEST}")
519-
install(FILES "${CMAKE_SOURCE_DIR}/std/debug.zig" DESTINATION "${ZIG_STD_DEST}")
519+
install(FILES "${CMAKE_SOURCE_DIR}/std/debug/index.zig" DESTINATION "${ZIG_STD_DEST}/debug")
520+
install(FILES "${CMAKE_SOURCE_DIR}/std/debug/failing_allocator.zig" DESTINATION "${ZIG_STD_DEST}/debug")
520521
install(FILES "${CMAKE_SOURCE_DIR}/std/dwarf.zig" DESTINATION "${ZIG_STD_DEST}")
521522
install(FILES "${CMAKE_SOURCE_DIR}/std/elf.zig" DESTINATION "${ZIG_STD_DEST}")
522523
install(FILES "${CMAKE_SOURCE_DIR}/std/empty.zig" DESTINATION "${ZIG_STD_DEST}")

build.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ pub fn installStdLib(b: &Builder) {
172172
"c/linux.zig",
173173
"c/windows.zig",
174174
"cstr.zig",
175-
"debug.zig",
175+
"debug/failing_allocator.zig",
176+
"debug/index.zig",
176177
"dwarf.zig",
177178
"elf.zig",
178179
"empty.zig",

src-self-hosted/parser.zig

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,18 +1119,24 @@ fn testCanonical(source: []const u8) {
11191119
break :x failing_allocator.index;
11201120
};
11211121

1122-
// TODO make this pass
1123-
//var fail_index = needed_alloc_count;
1124-
//while (fail_index != 0) {
1125-
// fail_index -= 1;
1126-
// var fixed_allocator = mem.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
1127-
// var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, fail_index);
1128-
// if (testParse(source, &failing_allocator.allocator)) |_| {
1129-
// @panic("non-deterministic memory usage");
1130-
// } else |err| {
1131-
// assert(err == error.OutOfMemory);
1132-
// }
1133-
//}
1122+
var fail_index: usize = 0;
1123+
while (fail_index < needed_alloc_count) : (fail_index += 1) {
1124+
var fixed_allocator = mem.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
1125+
var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, fail_index);
1126+
if (testParse(source, &failing_allocator.allocator)) |_| {
1127+
@panic("non-deterministic memory usage");
1128+
} else |err| {
1129+
assert(err == error.OutOfMemory);
1130+
// TODO make this pass
1131+
//if (failing_allocator.allocated_bytes != failing_allocator.freed_bytes) {
1132+
// warn("\nfail_index: {}/{}\nallocated bytes: {}\nfreed bytes: {}\nallocations: {}\ndeallocations: {}\n",
1133+
// fail_index, needed_alloc_count,
1134+
// failing_allocator.allocated_bytes, failing_allocator.freed_bytes,
1135+
// failing_allocator.index, failing_allocator.deallocations);
1136+
// @panic("memory leak detected");
1137+
//}
1138+
}
1139+
}
11341140
}
11351141

11361142
test "zig fmt" {

std/array_list.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const debug = @import("debug.zig");
1+
const std = @import("index.zig");
2+
const debug = std.debug;
23
const assert = debug.assert;
3-
const mem = @import("mem.zig");
4+
const mem = std.mem;
45
const Allocator = mem.Allocator;
56

67
pub fn ArrayList(comptime T: type) -> type {

std/base64.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const assert = @import("debug.zig").assert;
2-
const mem = @import("mem.zig");
1+
const std = @import("index.zig");
2+
const assert = std.debug.assert;
3+
const mem = std.mem;
34

45
pub const standard_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
56
pub const standard_pad_char = '=';

std/buffer.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
const debug = @import("debug.zig");
2-
const mem = @import("mem.zig");
1+
const std = @import("index.zig");
2+
const debug = std.debug;
3+
const mem = std.mem;
34
const Allocator = mem.Allocator;
45
const assert = debug.assert;
5-
const ArrayList = @import("array_list.zig").ArrayList;
6+
const ArrayList = std.ArrayList;
67

7-
const fmt = @import("fmt/index.zig");
8+
const fmt = std.fmt;
89

910
/// A buffer that allocates memory and maintains a null byte at the end.
1011
pub const Buffer = struct {

std/cstr.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const debug = @import("debug.zig");
2-
const mem = @import("mem.zig");
1+
const std = @import("index.zig");
2+
const debug = std.debug;
3+
const mem = std.mem;
34
const assert = debug.assert;
45

56
pub fn len(ptr: &const u8) -> usize {

std/debug/failing_allocator.zig

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const std = @import("../index.zig");
2+
const mem = std.mem;
3+
4+
/// Allocator that fails after N allocations, useful for making sure out of
5+
/// memory conditions are handled correctly.
6+
pub const FailingAllocator = struct {
7+
allocator: mem.Allocator,
8+
index: usize,
9+
fail_index: usize,
10+
internal_allocator: &mem.Allocator,
11+
allocated_bytes: usize,
12+
freed_bytes: usize,
13+
deallocations: usize,
14+
15+
pub fn init(allocator: &mem.Allocator, fail_index: usize) -> FailingAllocator {
16+
return FailingAllocator {
17+
.internal_allocator = allocator,
18+
.fail_index = fail_index,
19+
.index = 0,
20+
.allocated_bytes = 0,
21+
.freed_bytes = 0,
22+
.deallocations = 0,
23+
.allocator = mem.Allocator {
24+
.allocFn = alloc,
25+
.reallocFn = realloc,
26+
.freeFn = free,
27+
},
28+
};
29+
}
30+
31+
fn alloc(allocator: &mem.Allocator, n: usize, alignment: u29) -> %[]u8 {
32+
const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
33+
if (self.index == self.fail_index) {
34+
return error.OutOfMemory;
35+
}
36+
const result = %return self.internal_allocator.allocFn(self.internal_allocator, n, alignment);
37+
self.allocated_bytes += result.len;
38+
self.index += 1;
39+
return result;
40+
}
41+
42+
fn realloc(allocator: &mem.Allocator, old_mem: []u8, new_size: usize, alignment: u29) -> %[]u8 {
43+
const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
44+
if (new_size <= old_mem.len) {
45+
self.freed_bytes += old_mem.len - new_size;
46+
return self.internal_allocator.reallocFn(self.internal_allocator, old_mem, new_size, alignment);
47+
}
48+
if (self.index == self.fail_index) {
49+
return error.OutOfMemory;
50+
}
51+
const result = %return self.internal_allocator.reallocFn(self.internal_allocator, old_mem, new_size, alignment);
52+
self.allocated_bytes += new_size - old_mem.len;
53+
self.deallocations += 1;
54+
self.index += 1;
55+
return result;
56+
}
57+
58+
fn free(allocator: &mem.Allocator, bytes: []u8) {
59+
const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
60+
self.freed_bytes += bytes.len;
61+
self.deallocations += 1;
62+
return self.internal_allocator.freeFn(self.internal_allocator, bytes);
63+
}
64+
};

std/debug.zig renamed to std/debug/index.zig

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const std = @import("index.zig");
1+
const std = @import("../index.zig");
22
const math = std.math;
33
const mem = std.mem;
44
const io = std.io;
55
const os = std.os;
6-
const elf = @import("elf.zig");
7-
const DW = @import("dwarf.zig");
6+
const elf = std.elf;
7+
const DW = std.dwarf;
88
const ArrayList = std.ArrayList;
99
const builtin = @import("builtin");
1010

@@ -992,59 +992,3 @@ fn readILeb128(in_stream: &io.InStream) -> %i64 {
992992
pub const global_allocator = &global_fixed_allocator.allocator;
993993
var global_fixed_allocator = mem.FixedBufferAllocator.init(global_allocator_mem[0..]);
994994
var global_allocator_mem: [100 * 1024]u8 = undefined;
995-
996-
/// Allocator that fails after N allocations, useful for making sure out of
997-
/// memory conditions are handled correctly.
998-
pub const FailingAllocator = struct {
999-
allocator: mem.Allocator,
1000-
index: usize,
1001-
fail_index: usize,
1002-
internal_allocator: &mem.Allocator,
1003-
allocated_bytes: usize,
1004-
1005-
pub fn init(allocator: &mem.Allocator, fail_index: usize) -> FailingAllocator {
1006-
return FailingAllocator {
1007-
.internal_allocator = allocator,
1008-
.fail_index = fail_index,
1009-
.index = 0,
1010-
.allocated_bytes = 0,
1011-
.allocator = mem.Allocator {
1012-
.allocFn = alloc,
1013-
.reallocFn = realloc,
1014-
.freeFn = free,
1015-
},
1016-
};
1017-
}
1018-
1019-
fn alloc(allocator: &mem.Allocator, n: usize, alignment: u29) -> %[]u8 {
1020-
const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
1021-
if (self.index == self.fail_index) {
1022-
return error.OutOfMemory;
1023-
}
1024-
self.index += 1;
1025-
const result = %return self.internal_allocator.allocFn(self.internal_allocator, n, alignment);
1026-
self.allocated_bytes += result.len;
1027-
return result;
1028-
}
1029-
1030-
fn realloc(allocator: &mem.Allocator, old_mem: []u8, new_size: usize, alignment: u29) -> %[]u8 {
1031-
const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
1032-
if (new_size <= old_mem.len) {
1033-
self.allocated_bytes -= old_mem.len - new_size;
1034-
return self.internal_allocator.reallocFn(self.internal_allocator, old_mem, new_size, alignment);
1035-
}
1036-
if (self.index == self.fail_index) {
1037-
return error.OutOfMemory;
1038-
}
1039-
self.index += 1;
1040-
const result = %return self.internal_allocator.reallocFn(self.internal_allocator, old_mem, new_size, alignment);
1041-
self.allocated_bytes += new_size - old_mem.len;
1042-
return result;
1043-
}
1044-
1045-
fn free(allocator: &mem.Allocator, bytes: []u8) {
1046-
const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
1047-
self.allocated_bytes -= bytes.len;
1048-
return self.internal_allocator.freeFn(self.internal_allocator, bytes);
1049-
}
1050-
};

std/fmt/errol/index.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
const std = @import("../../index.zig");
12
const enum3 = @import("enum3.zig").enum3;
23
const enum3_data = @import("enum3.zig").enum3_data;
34
const lookup_table = @import("lookup.zig").lookup_table;
45
const HP = @import("lookup.zig").HP;
5-
const math = @import("../../math/index.zig");
6-
const mem = @import("../../mem.zig");
7-
const assert = @import("../../debug.zig").assert;
6+
const math = std.math;
7+
const mem = std.mem;
8+
const assert = std.debug.assert;
89

910
pub const FloatDecimal = struct {
1011
digits: []u8,

std/fmt/index.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const math = @import("../math/index.zig");
2-
const debug = @import("../debug.zig");
1+
const std = @import("../index.zig");
2+
const math = std.math;
3+
const debug = std.debug;
34
const assert = debug.assert;
4-
const mem = @import("../mem.zig");
5+
const mem = std.mem;
56
const builtin = @import("builtin");
67
const errol3 = @import("errol/index.zig").errol3;
78

std/hash_map.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const debug = @import("debug.zig");
1+
const std = @import("index.zig");
2+
const debug = std.debug;
23
const assert = debug.assert;
3-
const math = @import("math/index.zig");
4-
const mem = @import("mem.zig");
4+
const math = std.math;
5+
const mem = std.mem;
56
const Allocator = mem.Allocator;
67
const builtin = @import("builtin");
78

std/heap.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
const debug = @import("debug.zig");
1+
const std = @import("index.zig");
2+
const debug = std.debug;
23
const assert = debug.assert;
3-
const mem = @import("mem.zig");
4-
const os = @import("os/index.zig");
4+
const mem = std.mem;
5+
const os = std.os;
56
const builtin = @import("builtin");
67
const Os = builtin.Os;
7-
const c = @import("c/index.zig");
8+
const c = std.c;
89

910
const Allocator = mem.Allocator;
1011

std/index.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub const base64 = @import("base64.zig");
1111
pub const build = @import("build.zig");
1212
pub const c = @import("c/index.zig");
1313
pub const cstr = @import("cstr.zig");
14-
pub const debug = @import("debug.zig");
14+
pub const debug = @import("debug/index.zig");
1515
pub const dwarf = @import("dwarf.zig");
1616
pub const elf = @import("elf.zig");
1717
pub const empty_import = @import("empty.zig");
@@ -39,7 +39,7 @@ test "std" {
3939
_ = @import("build.zig");
4040
_ = @import("c/index.zig");
4141
_ = @import("cstr.zig");
42-
_ = @import("debug.zig");
42+
_ = @import("debug/index.zig");
4343
_ = @import("dwarf.zig");
4444
_ = @import("elf.zig");
4545
_ = @import("empty.zig");

std/linked_list.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const debug = @import("debug.zig");
1+
const std = @import("index.zig");
2+
const debug = std.debug;
23
const assert = debug.assert;
3-
const mem = @import("mem.zig");
4+
const mem = std.mem;
45
const Allocator = mem.Allocator;
56

67
/// Generic doubly linked list.

std/math/acos.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
//
33
// - acos(x) = nan if x < -1 or x > 1
44

5-
const math = @import("index.zig");
6-
const assert = @import("../debug.zig").assert;
5+
const std = @import("../index.zig");
6+
const math = std.math;
7+
const assert = std.debug.assert;
78

89
pub fn acos(x: var) -> @typeOf(x) {
910
const T = @typeOf(x);

std/math/acosh.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
// - acosh(nan) = nan
55

66
const builtin = @import("builtin");
7-
const math = @import("index.zig");
8-
const assert = @import("../debug.zig").assert;
7+
const std = @import("../index.zig");
8+
const math = std.math;
9+
const assert = std.debug.assert;
910

1011
pub fn acosh(x: var) -> @typeOf(x) {
1112
const T = @typeOf(x);

std/math/asin.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
// - asin(+-0) = +-0
44
// - asin(x) = nan if x < -1 or x > 1
55

6-
const math = @import("index.zig");
7-
const assert = @import("../debug.zig").assert;
6+
const std = @import("../index.zig");
7+
const math = std.math;
8+
const assert = std.debug.assert;
89

910
pub fn asin(x: var) -> @typeOf(x) {
1011
const T = @typeOf(x);

std/math/asinh.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
// - asinh(+-inf) = +-inf
55
// - asinh(nan) = nan
66

7-
const math = @import("index.zig");
8-
const assert = @import("../debug.zig").assert;
7+
const std = @import("../index.zig");
8+
const math = std.math;
9+
const assert = std.debug.assert;
910

1011
pub fn asinh(x: var) -> @typeOf(x) {
1112
const T = @typeOf(x);

std/math/atan.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
// - atan(+-0) = +-0
44
// - atan(+-inf) = +-pi/2
55

6-
const math = @import("index.zig");
7-
const assert = @import("../debug.zig").assert;
6+
const std = @import("../index.zig");
7+
const math = std.math;
8+
const assert = std.debug.assert;
89

910
pub fn atan(x: var) -> @typeOf(x) {
1011
const T = @typeOf(x);

std/math/atan2.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
// atan2(+inf, x) = +pi/2
1919
// atan2(-inf, x) = -pi/2
2020

21-
const math = @import("index.zig");
22-
const assert = @import("../debug.zig").assert;
21+
const std = @import("../index.zig");
22+
const math = std.math;
23+
const assert = std.debug.assert;
2324

2425
fn atan2(comptime T: type, x: T, y: T) -> T {
2526
return switch (T) {

0 commit comments

Comments
 (0)