diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2025-06-09 12:46:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-09 12:46:22 +0100 |
| commit | 87247b8bb787e53f7ca26fba1a11739844bf442d (patch) | |
| tree | 2415eb49e48ab985237ca61211e42cf1a668f2a6 | |
| parent | b8ac7776494eb7c55828b11fa5a431ef2eb44197 (diff) | |
| parent | f8228a91d1823a2f586dcca5f5ff9f57d17dc92f (diff) | |
Merge pull request #5286 from Feoramund/no-san-mem
Add `@(no_sanitize_memory)` with additions to `base:sanitizer`
| -rw-r--r-- | base/sanitizer/memory.odin | 74 | ||||
| -rw-r--r-- | core/os/os2/file_linux.odin | 1 | ||||
| -rw-r--r-- | core/os/os_freebsd.odin | 6 | ||||
| -rw-r--r-- | core/os/os_haiku.odin | 6 | ||||
| -rw-r--r-- | core/os/os_linux.odin | 8 | ||||
| -rw-r--r-- | core/os/os_netbsd.odin | 6 | ||||
| -rw-r--r-- | core/os/os_openbsd.odin | 6 | ||||
| -rw-r--r-- | src/check_decl.cpp | 1 | ||||
| -rw-r--r-- | src/checker.cpp | 6 | ||||
| -rw-r--r-- | src/checker.hpp | 1 | ||||
| -rw-r--r-- | src/entity.cpp | 1 | ||||
| -rw-r--r-- | src/llvm_backend_proc.cpp | 2 |
12 files changed, 101 insertions, 17 deletions
diff --git a/base/sanitizer/memory.odin b/base/sanitizer/memory.odin new file mode 100644 index 000000000..b16309a49 --- /dev/null +++ b/base/sanitizer/memory.odin @@ -0,0 +1,74 @@ +#+no-instrumentation +package sanitizer + +@(private="file") +MSAN_ENABLED :: .Memory in ODIN_SANITIZER_FLAGS + +@(private="file") +@(default_calling_convention="system") +foreign { + __msan_unpoison :: proc(addr: rawptr, size: uint) --- +} + +/* +Marks a slice as fully initialized. + +Code instrumented with `-sanitize:memory` will be permitted to access any +address within the slice as if it had already been initialized. + +When msan is not enabled this procedure does nothing. +*/ +memory_unpoison_slice :: proc "contextless" (region: $T/[]$E) { + when MSAN_ENABLED { + __msan_unpoison(raw_data(region), size_of(E) * len(region)) + } +} + +/* +Marks a pointer as fully initialized. + +Code instrumented with `-sanitize:memory` will be permitted to access memory +within the region the pointer points to as if it had already been initialized. + +When msan is not enabled this procedure does nothing. +*/ +memory_unpoison_ptr :: proc "contextless" (ptr: ^$T) { + when MSAN_ENABLED { + __msan_unpoison(ptr, size_of(T)) + } +} + +/* +Marks the region covering `[ptr, ptr+len)` as fully initialized. + +Code instrumented with `-sanitize:memory` will be permitted to access memory +within this range as if it had already been initialized. + +When msan is not enabled this procedure does nothing. +*/ +memory_unpoison_rawptr :: proc "contextless" (ptr: rawptr, len: int) { + when MSAN_ENABLED { + __msan_unpoison(ptr, uint(len)) + } +} + +/* +Marks the region covering `[ptr, ptr+len)` as fully initialized. + +Code instrumented with `-sanitize:memory` will be permitted to access memory +within this range as if it had already been initialized. + +When msan is not enabled this procedure does nothing. +*/ +memory_unpoison_rawptr_uint :: proc "contextless" (ptr: rawptr, len: uint) { + when MSAN_ENABLED { + __msan_unpoison(ptr, len) + } +} + +memory_unpoison :: proc { + memory_unpoison_slice, + memory_unpoison_ptr, + memory_unpoison_rawptr, + memory_unpoison_rawptr_uint, +} diff --git a/core/os/os2/file_linux.odin b/core/os/os2/file_linux.odin index a1ead7f9f..b1d11b425 100644 --- a/core/os/os2/file_linux.odin +++ b/core/os/os2/file_linux.odin @@ -269,6 +269,7 @@ _write_at :: proc(f: ^File_Impl, p: []byte, offset: i64) -> (nt: i64, err: Error return } +@(no_sanitize_memory) _file_size :: proc(f: ^File_Impl) -> (n: i64, err: Error) { // TODO: Identify 0-sized "pseudo" files and return No_Size. This would // eliminate the need for the _read_entire_pseudo_file procs. diff --git a/core/os/os_freebsd.odin b/core/os/os_freebsd.odin index 87a56b057..f2ee6a496 100644 --- a/core/os/os_freebsd.odin +++ b/core/os/os_freebsd.odin @@ -662,7 +662,7 @@ last_write_time_by_name :: proc(name: string) -> (File_Time, Error) { return File_Time(modified), nil } -@(private, require_results) +@(private, require_results, no_sanitize_memory) _stat :: proc(path: string) -> (OS_Stat, Error) { runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() cstr := strings.clone_to_cstring(path, context.temp_allocator) @@ -674,7 +674,7 @@ _stat :: proc(path: string) -> (OS_Stat, Error) { return s, nil } -@(private, require_results) +@(private, require_results, no_sanitize_memory) _lstat :: proc(path: string) -> (OS_Stat, Error) { runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() cstr := strings.clone_to_cstring(path, context.temp_allocator) @@ -688,7 +688,7 @@ _lstat :: proc(path: string) -> (OS_Stat, Error) { return s, nil } -@(private, require_results) +@(private, require_results, no_sanitize_memory) _fstat :: proc(fd: Handle) -> (OS_Stat, Error) { s: OS_Stat = --- result := _unix_fstat(fd, &s) diff --git a/core/os/os_haiku.odin b/core/os/os_haiku.odin index 4a57afb87..4ce726965 100644 --- a/core/os/os_haiku.odin +++ b/core/os/os_haiku.odin @@ -325,7 +325,7 @@ _alloc_command_line_arguments :: proc() -> []string { return res } -@(private, require_results) +@(private, require_results, no_sanitize_memory) _stat :: proc(path: string) -> (OS_Stat, Error) { runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() cstr := strings.clone_to_cstring(path, context.temp_allocator) @@ -339,7 +339,7 @@ _stat :: proc(path: string) -> (OS_Stat, Error) { return s, nil } -@(private, require_results) +@(private, require_results, no_sanitize_memory) _lstat :: proc(path: string) -> (OS_Stat, Error) { runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() cstr := strings.clone_to_cstring(path, context.temp_allocator) @@ -353,7 +353,7 @@ _lstat :: proc(path: string) -> (OS_Stat, Error) { return s, nil } -@(private, require_results) +@(private, require_results, no_sanitize_memory) _fstat :: proc(fd: Handle) -> (OS_Stat, Error) { // deliberately uninitialized s: OS_Stat = --- diff --git a/core/os/os_linux.odin b/core/os/os_linux.odin index 2281e6a82..84a7f7b32 100644 --- a/core/os/os_linux.odin +++ b/core/os/os_linux.odin @@ -674,7 +674,7 @@ seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) { return i64(res), nil } -@(require_results) +@(require_results, no_sanitize_memory) file_size :: proc(fd: Handle) -> (i64, Error) { // deliberately uninitialized; the syscall fills this buffer for us s: OS_Stat = --- @@ -794,7 +794,7 @@ last_write_time_by_name :: proc(name: string) -> (time: File_Time, err: Error) { return File_Time(modified), nil } -@(private, require_results) +@(private, require_results, no_sanitize_memory) _stat :: proc(path: string) -> (OS_Stat, Error) { runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() cstr := strings.clone_to_cstring(path, context.temp_allocator) @@ -808,7 +808,7 @@ _stat :: proc(path: string) -> (OS_Stat, Error) { return s, nil } -@(private, require_results) +@(private, require_results, no_sanitize_memory) _lstat :: proc(path: string) -> (OS_Stat, Error) { runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() cstr := strings.clone_to_cstring(path, context.temp_allocator) @@ -822,7 +822,7 @@ _lstat :: proc(path: string) -> (OS_Stat, Error) { return s, nil } -@(private, require_results) +@(private, require_results, no_sanitize_memory) _fstat :: proc(fd: Handle) -> (OS_Stat, Error) { // deliberately uninitialized; the syscall fills this buffer for us s: OS_Stat = --- diff --git a/core/os/os_netbsd.odin b/core/os/os_netbsd.odin index e3ba760a4..40b41b133 100644 --- a/core/os/os_netbsd.odin +++ b/core/os/os_netbsd.odin @@ -724,7 +724,7 @@ last_write_time_by_name :: proc(name: string) -> (time: File_Time, err: Error) { return File_Time(modified), nil } -@(private, require_results) +@(private, require_results, no_sanitize_memory) _stat :: proc(path: string) -> (OS_Stat, Error) { runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() cstr := strings.clone_to_cstring(path, context.temp_allocator) @@ -736,7 +736,7 @@ _stat :: proc(path: string) -> (OS_Stat, Error) { return s, nil } -@(private, require_results) +@(private, require_results, no_sanitize_memory) _lstat :: proc(path: string) -> (OS_Stat, Error) { runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() cstr := strings.clone_to_cstring(path, context.temp_allocator) @@ -750,7 +750,7 @@ _lstat :: proc(path: string) -> (OS_Stat, Error) { return s, nil } -@(private, require_results) +@(private, require_results, no_sanitize_memory) _fstat :: proc(fd: Handle) -> (OS_Stat, Error) { s: OS_Stat = --- result := _unix_fstat(fd, &s) diff --git a/core/os/os_openbsd.odin b/core/os/os_openbsd.odin index 6548a57dc..d90e51e13 100644 --- a/core/os/os_openbsd.odin +++ b/core/os/os_openbsd.odin @@ -639,7 +639,7 @@ last_write_time_by_name :: proc(name: string) -> (time: File_Time, err: Error) { return File_Time(modified), nil } -@(private, require_results) +@(private, require_results, no_sanitize_memory) _stat :: proc(path: string) -> (OS_Stat, Error) { runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() cstr := strings.clone_to_cstring(path, context.temp_allocator) @@ -653,7 +653,7 @@ _stat :: proc(path: string) -> (OS_Stat, Error) { return s, nil } -@(private, require_results) +@(private, require_results, no_sanitize_memory) _lstat :: proc(path: string) -> (OS_Stat, Error) { runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() cstr := strings.clone_to_cstring(path, context.temp_allocator) @@ -667,7 +667,7 @@ _lstat :: proc(path: string) -> (OS_Stat, Error) { return s, nil } -@(private, require_results) +@(private, require_results, no_sanitize_memory) _fstat :: proc(fd: Handle) -> (OS_Stat, Error) { // deliberately uninitialized s: OS_Stat = --- diff --git a/src/check_decl.cpp b/src/check_decl.cpp index d53c3c6b7..c696fc4c1 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -1370,6 +1370,7 @@ gb_internal void check_proc_decl(CheckerContext *ctx, Entity *e, DeclInfo *d) { e->Procedure.has_instrumentation = has_instrumentation; e->Procedure.no_sanitize_address = ac.no_sanitize_address; + e->Procedure.no_sanitize_memory = ac.no_sanitize_memory; e->deprecated_message = ac.deprecated_message; e->warning_message = ac.warning_message; diff --git a/src/checker.cpp b/src/checker.cpp index 67dee9963..4ebabe004 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -3776,6 +3776,12 @@ gb_internal DECL_ATTRIBUTE_PROC(proc_decl_attribute) { } ac->no_sanitize_address = true; return true; + } else if (name == "no_sanitize_memory") { + if (value != nullptr) { + error(value, "'%.*s' expects no parameter", LIT(name)); + } + ac->no_sanitize_memory = true; + return true; } return false; } diff --git a/src/checker.hpp b/src/checker.hpp index 0cdfd69ab..dabb7330a 100644 --- a/src/checker.hpp +++ b/src/checker.hpp @@ -140,6 +140,7 @@ struct AttributeContext { bool instrumentation_enter : 1; bool instrumentation_exit : 1; bool no_sanitize_address : 1; + bool no_sanitize_memory : 1; bool rodata : 1; bool ignore_duplicates : 1; u32 optimization_mode; // ProcedureOptimizationMode diff --git a/src/entity.cpp b/src/entity.cpp index a16779419..6c0aa6ace 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -263,6 +263,7 @@ struct Entity { bool uses_branch_location : 1; bool is_anonymous : 1; bool no_sanitize_address : 1; + bool no_sanitize_memory : 1; } Procedure; struct { Array<Entity *> entities; diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index f51ed2b4d..0a51b5cb5 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -345,7 +345,7 @@ gb_internal lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool i if (build_context.sanitizer_flags & SanitizerFlag_Address && !entity->Procedure.no_sanitize_address) { lb_add_attribute_to_proc(m, p->value, "sanitize_address"); } - if (build_context.sanitizer_flags & SanitizerFlag_Memory) { + if (build_context.sanitizer_flags & SanitizerFlag_Memory && !entity->Procedure.no_sanitize_memory) { lb_add_attribute_to_proc(m, p->value, "sanitize_memory"); } if (build_context.sanitizer_flags & SanitizerFlag_Thread) { |