Skip to content

Commit 652f4bd

Browse files
committed
disallow unknown-length pointer to opaque
This also means that translate-c has to detect when a pointer to opaque is happening, and use `*` instead of `[*]`. See #1059
1 parent 7a09482 commit 652f4bd

14 files changed

+89
-50
lines changed

src/analyze.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ TypeTableEntry *get_pointer_to_type_extra(CodeGen *g, TypeTableEntry *child_type
384384
bool is_volatile, PtrLen ptr_len, uint32_t byte_alignment, uint32_t bit_offset, uint32_t unaligned_bit_count)
385385
{
386386
assert(!type_is_invalid(child_type));
387+
assert(ptr_len == PtrLenSingle || child_type->id != TypeTableEntryIdOpaque);
387388

388389
TypeId type_id = {};
389390
TypeTableEntry **parent_pointer = nullptr;

src/ir.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4620,11 +4620,8 @@ static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *
46204620

46214621
static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode *node) {
46224622
assert(node->type == NodeTypePointerType);
4623-
// The null check here is for C imports which don't set a token on the AST node. We could potentially
4624-
// update that code to create a fake token and then remove this check.
4625-
PtrLen ptr_len = (node->data.pointer_type.star_token != nullptr &&
4626-
(node->data.pointer_type.star_token->id == TokenIdStar ||
4627-
node->data.pointer_type.star_token->id == TokenIdStarStar)) ? PtrLenSingle : PtrLenUnknown;
4623+
PtrLen ptr_len = (node->data.pointer_type.star_token->id == TokenIdStar ||
4624+
node->data.pointer_type.star_token->id == TokenIdStarStar) ? PtrLenSingle : PtrLenUnknown;
46284625
bool is_const = node->data.pointer_type.is_const;
46294626
bool is_volatile = node->data.pointer_type.is_volatile;
46304627
AstNode *expr_node = node->data.pointer_type.op_expr;
@@ -18973,6 +18970,9 @@ static TypeTableEntry *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstruc
1897318970
if (child_type->id == TypeTableEntryIdUnreachable) {
1897418971
ir_add_error(ira, &instruction->base, buf_sprintf("pointer to noreturn not allowed"));
1897518972
return ira->codegen->builtin_types.entry_invalid;
18973+
} else if (child_type->id == TypeTableEntryIdOpaque && instruction->ptr_len == PtrLenUnknown) {
18974+
ir_add_error(ira, &instruction->base, buf_sprintf("unknown-length pointer to opaque"));
18975+
return ira->codegen->builtin_types.entry_invalid;
1897618976
}
1897718977

1897818978
uint32_t align_bytes;

src/tokenizer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ struct Token {
170170
TokenCharLit char_lit;
171171
} data;
172172
};
173+
// work around conflicting name Token which is also found in libclang
174+
typedef Token ZigToken;
173175

174176
struct Tokenization {
175177
ZigList<Token> *tokens;

src/translate_c.cpp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,11 @@ static AstNode *maybe_suppress_result(Context *c, ResultUsed result_used, AstNod
276276
node);
277277
}
278278

279-
static AstNode *trans_create_node_ptr_type(Context *c, bool is_const, bool is_volatile, AstNode *child_node) {
279+
static AstNode *trans_create_node_ptr_type(Context *c, bool is_const, bool is_volatile, AstNode *child_node, PtrLen ptr_len) {
280280
AstNode *node = trans_create_node(c, NodeTypePointerType);
281+
node->data.pointer_type.star_token = allocate<ZigToken>(1);
282+
node->data.pointer_type.star_token->id = (ptr_len == PtrLenSingle) ? TokenIdStar: TokenIdBracketStarBracket;
283+
node->data.pointer_type.is_const = is_const;
281284
node->data.pointer_type.is_const = is_const;
282285
node->data.pointer_type.is_volatile = is_volatile;
283286
node->data.pointer_type.op_expr = child_node;
@@ -731,6 +734,30 @@ static bool qual_type_has_wrapping_overflow(Context *c, QualType qt) {
731734
}
732735
}
733736

737+
static bool type_is_opaque(Context *c, const Type *ty, const SourceLocation &source_loc) {
738+
switch (ty->getTypeClass()) {
739+
case Type::Builtin: {
740+
const BuiltinType *builtin_ty = static_cast<const BuiltinType*>(ty);
741+
return builtin_ty->getKind() == BuiltinType::Void;
742+
}
743+
case Type::Record: {
744+
const RecordType *record_ty = static_cast<const RecordType*>(ty);
745+
return record_ty->getDecl()->getDefinition() == nullptr;
746+
}
747+
case Type::Elaborated: {
748+
const ElaboratedType *elaborated_ty = static_cast<const ElaboratedType*>(ty);
749+
return type_is_opaque(c, elaborated_ty->getNamedType().getTypePtr(), source_loc);
750+
}
751+
case Type::Typedef: {
752+
const TypedefType *typedef_ty = static_cast<const TypedefType*>(ty);
753+
const TypedefNameDecl *typedef_decl = typedef_ty->getDecl();
754+
return type_is_opaque(c, typedef_decl->getUnderlyingType().getTypePtr(), source_loc);
755+
}
756+
default:
757+
return false;
758+
}
759+
}
760+
734761
static AstNode *trans_type(Context *c, const Type *ty, const SourceLocation &source_loc) {
735762
switch (ty->getTypeClass()) {
736763
case Type::Builtin:
@@ -855,8 +882,10 @@ static AstNode *trans_type(Context *c, const Type *ty, const SourceLocation &sou
855882
return trans_create_node_prefix_op(c, PrefixOpMaybe, child_node);
856883
}
857884

885+
PtrLen ptr_len = type_is_opaque(c, child_qt.getTypePtr(), source_loc) ? PtrLenSingle : PtrLenUnknown;
886+
858887
AstNode *pointer_node = trans_create_node_ptr_type(c, child_qt.isConstQualified(),
859-
child_qt.isVolatileQualified(), child_node);
888+
child_qt.isVolatileQualified(), child_node, ptr_len);
860889
return trans_create_node_prefix_op(c, PrefixOpMaybe, pointer_node);
861890
}
862891
case Type::Typedef:
@@ -1041,7 +1070,7 @@ static AstNode *trans_type(Context *c, const Type *ty, const SourceLocation &sou
10411070
return nullptr;
10421071
}
10431072
AstNode *pointer_node = trans_create_node_ptr_type(c, child_qt.isConstQualified(),
1044-
child_qt.isVolatileQualified(), child_type_node);
1073+
child_qt.isVolatileQualified(), child_type_node, PtrLenUnknown);
10451074
return pointer_node;
10461075
}
10471076
case Type::BlockPointer:
@@ -4448,7 +4477,7 @@ static AstNode *parse_ctok_suffix_op_expr(Context *c, CTokenize *ctok, size_t *t
44484477
} else if (first_tok->id == CTokIdAsterisk) {
44494478
*tok_i += 1;
44504479

4451-
node = trans_create_node_ptr_type(c, false, false, node);
4480+
node = trans_create_node_ptr_type(c, false, false, node, PtrLenUnknown);
44524481
} else {
44534482
return node;
44544483
}

std/c/index.zig

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ pub extern "c" fn @"fstat$INODE64"(fd: c_int, buf: *Stat) c_int;
2020
pub extern "c" fn lseek(fd: c_int, offset: isize, whence: c_int) isize;
2121
pub extern "c" fn open(path: [*]const u8, oflag: c_int, ...) c_int;
2222
pub extern "c" fn raise(sig: c_int) c_int;
23-
pub extern "c" fn read(fd: c_int, buf: [*]c_void, nbyte: usize) isize;
23+
pub extern "c" fn read(fd: c_int, buf: *c_void, nbyte: usize) isize;
2424
pub extern "c" fn stat(noalias path: [*]const u8, noalias buf: *Stat) c_int;
25-
pub extern "c" fn write(fd: c_int, buf: [*]const c_void, nbyte: usize) isize;
26-
pub extern "c" fn mmap(addr: ?[*]c_void, len: usize, prot: c_int, flags: c_int, fd: c_int, offset: isize) ?[*]c_void;
27-
pub extern "c" fn munmap(addr: [*]c_void, len: usize) c_int;
25+
pub extern "c" fn write(fd: c_int, buf: *const c_void, nbyte: usize) isize;
26+
pub extern "c" fn mmap(addr: ?*c_void, len: usize, prot: c_int, flags: c_int, fd: c_int, offset: isize) ?*c_void;
27+
pub extern "c" fn munmap(addr: *c_void, len: usize) c_int;
2828
pub extern "c" fn unlink(path: [*]const u8) c_int;
2929
pub extern "c" fn getcwd(buf: [*]u8, size: usize) ?[*]u8;
3030
pub extern "c" fn waitpid(pid: c_int, stat_loc: *c_int, options: c_int) c_int;
@@ -48,15 +48,15 @@ pub extern "c" fn setreuid(ruid: c_uint, euid: c_uint) c_int;
4848
pub extern "c" fn setregid(rgid: c_uint, egid: c_uint) c_int;
4949
pub extern "c" fn rmdir(path: [*]const u8) c_int;
5050

51-
pub extern "c" fn aligned_alloc(alignment: usize, size: usize) ?[*]c_void;
52-
pub extern "c" fn malloc(usize) ?[*]c_void;
53-
pub extern "c" fn realloc([*]c_void, usize) ?[*]c_void;
54-
pub extern "c" fn free([*]c_void) void;
55-
pub extern "c" fn posix_memalign(memptr: *[*]c_void, alignment: usize, size: usize) c_int;
51+
pub extern "c" fn aligned_alloc(alignment: usize, size: usize) ?*c_void;
52+
pub extern "c" fn malloc(usize) ?*c_void;
53+
pub extern "c" fn realloc(*c_void, usize) ?*c_void;
54+
pub extern "c" fn free(*c_void) void;
55+
pub extern "c" fn posix_memalign(memptr: **c_void, alignment: usize, size: usize) c_int;
5656

5757
pub extern "pthread" fn pthread_create(noalias newthread: *pthread_t, noalias attr: ?*const pthread_attr_t, start_routine: extern fn (?*c_void) ?*c_void, noalias arg: ?*c_void) c_int;
5858
pub extern "pthread" fn pthread_attr_init(attr: *pthread_attr_t) c_int;
59-
pub extern "pthread" fn pthread_attr_setstack(attr: *pthread_attr_t, stackaddr: [*]c_void, stacksize: usize) c_int;
59+
pub extern "pthread" fn pthread_attr_setstack(attr: *pthread_attr_t, stackaddr: *c_void, stacksize: usize) c_int;
6060
pub extern "pthread" fn pthread_attr_destroy(attr: *pthread_attr_t) c_int;
6161
pub extern "pthread" fn pthread_join(thread: pthread_t, arg_return: ?*?*c_void) c_int;
6262

std/heap.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn cAlloc(self: *Allocator, n: usize, alignment: u29) ![]u8 {
2222
}
2323

2424
fn cRealloc(self: *Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![]u8 {
25-
const old_ptr = @ptrCast([*]c_void, old_mem.ptr);
25+
const old_ptr = @ptrCast(*c_void, old_mem.ptr);
2626
if (c.realloc(old_ptr, new_size)) |buf| {
2727
return @ptrCast([*]u8, buf)[0..new_size];
2828
} else if (new_size <= old_mem.len) {
@@ -33,7 +33,7 @@ fn cRealloc(self: *Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![
3333
}
3434

3535
fn cFree(self: *Allocator, old_mem: []u8) void {
36-
const old_ptr = @ptrCast([*]c_void, old_mem.ptr);
36+
const old_ptr = @ptrCast(*c_void, old_mem.ptr);
3737
c.free(old_ptr);
3838
}
3939

@@ -140,7 +140,7 @@ pub const DirectAllocator = struct {
140140
const old_adjusted_addr = @ptrToInt(old_mem.ptr);
141141
const old_record_addr = old_adjusted_addr + old_mem.len;
142142
const root_addr = @intToPtr(*align(1) usize, old_record_addr).*;
143-
const old_ptr = @intToPtr([*]c_void, root_addr);
143+
const old_ptr = @intToPtr(*c_void, root_addr);
144144
const amt = new_size + alignment + @sizeOf(usize);
145145
const new_ptr = os.windows.HeapReAlloc(??self.heap_handle, 0, old_ptr, amt) ?? blk: {
146146
if (new_size > old_mem.len) return error.OutOfMemory;
@@ -170,7 +170,7 @@ pub const DirectAllocator = struct {
170170
Os.windows => {
171171
const record_addr = @ptrToInt(bytes.ptr) + bytes.len;
172172
const root_addr = @intToPtr(*align(1) usize, record_addr).*;
173-
const ptr = @intToPtr([*]c_void, root_addr);
173+
const ptr = @intToPtr(*c_void, root_addr);
174174
_ = os.windows.HeapFree(??self.heap_handle, 0, ptr);
175175
},
176176
else => @compileError("Unsupported OS"),

std/os/darwin.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,25 +327,25 @@ pub fn raise(sig: i32) usize {
327327
}
328328

329329
pub fn read(fd: i32, buf: [*]u8, nbyte: usize) usize {
330-
return errnoWrap(c.read(fd, @ptrCast([*]c_void, buf), nbyte));
330+
return errnoWrap(c.read(fd, @ptrCast(*c_void, buf), nbyte));
331331
}
332332

333333
pub fn stat(noalias path: [*]const u8, noalias buf: *stat) usize {
334334
return errnoWrap(c.stat(path, buf));
335335
}
336336

337337
pub fn write(fd: i32, buf: [*]const u8, nbyte: usize) usize {
338-
return errnoWrap(c.write(fd, @ptrCast([*]const c_void, buf), nbyte));
338+
return errnoWrap(c.write(fd, @ptrCast(*const c_void, buf), nbyte));
339339
}
340340

341341
pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
342-
const ptr_result = c.mmap(@ptrCast([*]c_void, address), length, @bitCast(c_int, c_uint(prot)), @bitCast(c_int, c_uint(flags)), fd, offset);
342+
const ptr_result = c.mmap(@ptrCast(*c_void, address), length, @bitCast(c_int, c_uint(prot)), @bitCast(c_int, c_uint(flags)), fd, offset);
343343
const isize_result = @bitCast(isize, @ptrToInt(ptr_result));
344344
return errnoWrap(isize_result);
345345
}
346346

347347
pub fn munmap(address: usize, length: usize) usize {
348-
return errnoWrap(c.munmap(@intToPtr([*]c_void, address), length));
348+
return errnoWrap(c.munmap(@intToPtr(*c_void, address), length));
349349
}
350350

351351
pub fn unlink(path: [*]const u8) usize {

std/os/file.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ pub const File = struct {
334334
while (index < buffer.len) {
335335
const want_read_count = windows.DWORD(math.min(windows.DWORD(@maxValue(windows.DWORD)), buffer.len - index));
336336
var amt_read: windows.DWORD = undefined;
337-
if (windows.ReadFile(self.handle, @ptrCast([*]c_void, buffer.ptr + index), want_read_count, &amt_read, null) == 0) {
337+
if (windows.ReadFile(self.handle, @ptrCast(*c_void, buffer.ptr + index), want_read_count, &amt_read, null) == 0) {
338338
const err = windows.GetLastError();
339339
return switch (err) {
340340
windows.ERROR.OPERATION_ABORTED => continue,

std/os/index.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,7 +2362,7 @@ pub const Thread = struct {
23622362
},
23632363
builtin.Os.windows => struct {
23642364
handle: windows.HANDLE,
2365-
alloc_start: [*]c_void,
2365+
alloc_start: *c_void,
23662366
heap_handle: windows.HANDLE,
23672367
},
23682368
else => @compileError("Unsupported OS"),
@@ -2533,7 +2533,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread
25332533

25342534
// align to page
25352535
stack_end -= stack_end % os.page_size;
2536-
assert(c.pthread_attr_setstack(&attr, @intToPtr([*]c_void, stack_addr), stack_end - stack_addr) == 0);
2536+
assert(c.pthread_attr_setstack(&attr, @intToPtr(*c_void, stack_addr), stack_end - stack_addr) == 0);
25372537

25382538
const err = c.pthread_create(&thread_ptr.data.handle, &attr, MainFuncs.posixThreadMain, @intToPtr(*c_void, arg));
25392539
switch (err) {

std/os/windows/index.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,17 @@ pub extern "kernel32" stdcallcc fn GetSystemTimeAsFileTime(?*FILETIME) void;
101101

102102
pub extern "kernel32" stdcallcc fn HeapCreate(flOptions: DWORD, dwInitialSize: SIZE_T, dwMaximumSize: SIZE_T) ?HANDLE;
103103
pub extern "kernel32" stdcallcc fn HeapDestroy(hHeap: HANDLE) BOOL;
104-
pub extern "kernel32" stdcallcc fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: [*]c_void, dwBytes: SIZE_T) ?[*]c_void;
105-
pub extern "kernel32" stdcallcc fn HeapSize(hHeap: HANDLE, dwFlags: DWORD, lpMem: [*]const c_void) SIZE_T;
106-
pub extern "kernel32" stdcallcc fn HeapValidate(hHeap: HANDLE, dwFlags: DWORD, lpMem: [*]const c_void) BOOL;
104+
pub extern "kernel32" stdcallcc fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: *c_void, dwBytes: SIZE_T) ?*c_void;
105+
pub extern "kernel32" stdcallcc fn HeapSize(hHeap: HANDLE, dwFlags: DWORD, lpMem: *const c_void) SIZE_T;
106+
pub extern "kernel32" stdcallcc fn HeapValidate(hHeap: HANDLE, dwFlags: DWORD, lpMem: *const c_void) BOOL;
107107
pub extern "kernel32" stdcallcc fn HeapCompact(hHeap: HANDLE, dwFlags: DWORD) SIZE_T;
108108
pub extern "kernel32" stdcallcc fn HeapSummary(hHeap: HANDLE, dwFlags: DWORD, lpSummary: LPHEAP_SUMMARY) BOOL;
109109

110110
pub extern "kernel32" stdcallcc fn GetStdHandle(in_nStdHandle: DWORD) ?HANDLE;
111111

112-
pub extern "kernel32" stdcallcc fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) ?[*]c_void;
112+
pub extern "kernel32" stdcallcc fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) ?*c_void;
113113

114-
pub extern "kernel32" stdcallcc fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: [*]c_void) BOOL;
114+
pub extern "kernel32" stdcallcc fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: *c_void) BOOL;
115115

116116
pub extern "kernel32" stdcallcc fn MoveFileExA(
117117
lpExistingFileName: LPCSTR,
@@ -127,7 +127,7 @@ pub extern "kernel32" stdcallcc fn PathFileExists(pszPath: ?LPCTSTR) BOOL;
127127

128128
pub extern "kernel32" stdcallcc fn ReadFile(
129129
in_hFile: HANDLE,
130-
out_lpBuffer: [*]c_void,
130+
out_lpBuffer: *c_void,
131131
in_nNumberOfBytesToRead: DWORD,
132132
out_lpNumberOfBytesRead: *DWORD,
133133
in_out_lpOverlapped: ?*OVERLAPPED,
@@ -150,7 +150,7 @@ pub extern "kernel32" stdcallcc fn WaitForSingleObject(hHandle: HANDLE, dwMillis
150150

151151
pub extern "kernel32" stdcallcc fn WriteFile(
152152
in_hFile: HANDLE,
153-
in_lpBuffer: [*]const c_void,
153+
in_lpBuffer: *const c_void,
154154
in_nNumberOfBytesToWrite: DWORD,
155155
out_lpNumberOfBytesWritten: ?*DWORD,
156156
in_out_lpOverlapped: ?*OVERLAPPED,

std/os/windows/util.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub const WriteError = error{
4242
};
4343

4444
pub fn windowsWrite(handle: windows.HANDLE, bytes: []const u8) WriteError!void {
45-
if (windows.WriteFile(handle, @ptrCast([*]const c_void, bytes.ptr), u32(bytes.len), null, null) == 0) {
45+
if (windows.WriteFile(handle, @ptrCast(*const c_void, bytes.ptr), u32(bytes.len), null, null) == 0) {
4646
const err = windows.GetLastError();
4747
return switch (err) {
4848
windows.ERROR.INVALID_USER_BUFFER => WriteError.SystemResources,

test/compare_output.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
284284
cases.addC("expose function pointer to C land",
285285
\\const c = @cImport(@cInclude("stdlib.h"));
286286
\\
287-
\\export fn compare_fn(a: ?[*]const c_void, b: ?[*]const c_void) c_int {
287+
\\export fn compare_fn(a: ?*const c_void, b: ?*const c_void) c_int {
288288
\\ const a_int = @ptrCast(*const i32, @alignCast(@alignOf(i32), a));
289289
\\ const b_int = @ptrCast(*const i32, @alignCast(@alignOf(i32), b));
290290
\\ if (a_int.* < b_int.*) {
@@ -299,7 +299,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
299299
\\export fn main() c_int {
300300
\\ var array = []u32{ 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 };
301301
\\
302-
\\ c.qsort(@ptrCast(?[*]c_void, array[0..].ptr), c_ulong(array.len), @sizeOf(i32), compare_fn);
302+
\\ c.qsort(@ptrCast(?*c_void, array[0..].ptr), c_ulong(array.len), @sizeOf(i32), compare_fn);
303303
\\
304304
\\ for (array) |item, i| {
305305
\\ if (item != i) {

test/compile_errors.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
const tests = @import("tests.zig");
22

33
pub fn addCases(cases: *tests.CompileErrorContext) void {
4+
cases.add(
5+
"unknown length pointer to opaque",
6+
\\export const T = [*]@OpaqueType();
7+
,
8+
".tmp_source.zig:1:18: error: unknown-length pointer to opaque",
9+
);
10+
411
cases.add(
512
"error when evaluating return type",
613
\\const Foo = struct {

0 commit comments

Comments
 (0)