[PATCH v3 10/11] sampling-asan: Use Glibc-internal functions to supress the addition of extra PLT entries in DSO
Sung-hun Kim
sfoon.kim@samsung.com
Tue Sep 30 06:46:59 GMT 2025
Signed-off-by: Sung-hun Kim <sfoon.kim@samsung.com>
---
sampling-asan/samasan_allocate.c | 16 ++++++++--------
sampling-asan/samasan_allocate.h | 1 +
sampling-asan/samasan_backtrace.c | 6 +++---
sampling-asan/samasan_common.h | 4 ++--
sampling-asan/samasan_error.c | 4 ++--
sampling-asan/samasan_fault_handler.c | 8 ++++----
sampling-asan/samasan_report.c | 15 ++++++++-------
7 files changed, 28 insertions(+), 26 deletions(-)
diff --git a/sampling-asan/samasan_allocate.c b/sampling-asan/samasan_allocate.c
index 77db975e40..c9786c976e 100644
--- a/sampling-asan/samasan_allocate.c
+++ b/sampling-asan/samasan_allocate.c
@@ -25,7 +25,6 @@
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
-#include <sys/mman.h> /* for mmap/munmap */
#include <dso_handle.h> /* for __dso_handle */
#include <register-atfork.h> /* for __register_atfork */
@@ -224,14 +223,14 @@ get_page_size (void *ptr, size_t size)
static bool
protect_range_in_block (void *start, size_t size)
{
- return mprotect (get_page_pointer (start), get_page_size (start, size),
+ return __mprotect (get_page_pointer (start), get_page_size (start, size),
PROT_NONE) == 0;
}
static bool
unprotect_range_in_block (void *start, size_t size)
{
- return mprotect (get_page_pointer (start), get_page_size (start, size),
+ return __mprotect (get_page_pointer (start), get_page_size (start, size),
PROT_READ | PROT_WRITE) == 0;
}
@@ -508,16 +507,17 @@ samasan_is_pointer_in_sampling_pool (void *ptr)
but the check-on-free will detect invalid updates on unallocated area in
block pages. */
+
static void *
memory_pool_allocate (size_t size, size_t block_size)
{
- void *pool = mmap (NULL, size, PROT_READ | PROT_WRITE,
+ void *pool = __mmap (NULL, size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
samasan_assert (pool != MAP_FAILED, "memory pool allocation is failed");
for (void *block = pool + memory_pool_partition_size; block < pool + size;
block += (block_size + memory_pool_partition_size))
fill_guard_pattern (block, block_size);
- samasan_assert (mprotect (pool, size, PROT_NONE) == 0, "mprotect failed");
+ samasan_assert (__mprotect (pool, size, PROT_NONE) == 0, "mprotect failed");
memory_pool_begin = (uintptr_t) pool;
memory_pool_end = (uintptr_t) pool + size;
memory_pool_offset = (uintptr_t) pool % memory_pool_block_size;
@@ -527,7 +527,7 @@ memory_pool_allocate (size_t size, size_t block_size)
static void
memory_pool_deallocate (void *pool)
{
- samasan_assert (munmap (pool, memory_pool_block_size * memory_pool_size
+ samasan_assert (__munmap (pool, memory_pool_block_size * memory_pool_size
+ memory_pool_partition_size * (memory_pool_size + 1)) == 0,
"memory pool deallocation is failed");
}
@@ -535,7 +535,7 @@ memory_pool_deallocate (void *pool)
static void *
memory_pool_metadata_allocate (size_t size)
{
- void *metadata = mmap (NULL, size, PROT_READ | PROT_WRITE,
+ void *metadata = __mmap (NULL, size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
samasan_assert (metadata != MAP_FAILED,
"memory pool metadata allocation is failed");
@@ -545,7 +545,7 @@ memory_pool_metadata_allocate (size_t size)
static void
memory_pool_metadata_deallocate (void *metadata)
{
- samasan_assert (munmap (metadata, sizeof (struct memory_pool_entry_info)
+ samasan_assert (__munmap (metadata, sizeof (struct memory_pool_entry_info)
* memory_pool_size) == 0,
"memory pool metadata deallocation is failed");
}
diff --git a/sampling-asan/samasan_allocate.h b/sampling-asan/samasan_allocate.h
index 4f5195a58f..7e6f03af8a 100644
--- a/sampling-asan/samasan_allocate.h
+++ b/sampling-asan/samasan_allocate.h
@@ -21,6 +21,7 @@
#define _SAMASAN_ALLOCATE_H
#include <stdbool.h>
+#include <sys/mman.h> /* for mmap/munmap */
/* For allocation/deallocation traces */
#define INVALID_TID 0UL
diff --git a/sampling-asan/samasan_backtrace.c b/sampling-asan/samasan_backtrace.c
index 9317025b14..f52a094b6e 100644
--- a/sampling-asan/samasan_backtrace.c
+++ b/sampling-asan/samasan_backtrace.c
@@ -29,8 +29,8 @@ void
get_backtrace (struct memory_pool_trace *trace)
{
trace->trace_size =
- backtrace ((void **) trace->stacktrace, ALLOCATION_TRACE_SIZE);
- trace->tid = gettid ();
+ __backtrace ((void **) trace->stacktrace, ALLOCATION_TRACE_SIZE);
+ trace->tid = __gettid ();
}
void
@@ -45,7 +45,7 @@ print_backtrace (struct memory_pool_trace *trace, int stream,
return;
}
- symbols = backtrace_symbols ((void **) trace->stacktrace, trace->trace_size);
+ symbols = __backtrace_symbols ((void **) trace->stacktrace, trace->trace_size);
for (size_t i = 0; i < trace->trace_size; i++)
{
if (!symbols)
diff --git a/sampling-asan/samasan_common.h b/sampling-asan/samasan_common.h
index ae6e919c06..be860eca18 100644
--- a/sampling-asan/samasan_common.h
+++ b/sampling-asan/samasan_common.h
@@ -22,7 +22,7 @@
#include <stdio.h> /* for fprintf */
#include <stddef.h> /* for size_t */
#include <stdbool.h> /* for bool type */
-#include <unistd.h> /* for sysconf */
+#include <unistd.h> /* for __sysconf */
#include <libc-lock.h> /* for mutex */
#include <atomic.h> /* for atomic operations */
@@ -47,7 +47,7 @@ extern SAMASAN_TLS_SPECIFIER bool is_in_samasan;
static inline size_t
get_system_page_size (void)
{
- return sysconf(_SC_PAGESIZE);
+ return __sysconf(_SC_PAGESIZE);
}
static inline void
diff --git a/sampling-asan/samasan_error.c b/sampling-asan/samasan_error.c
index f2c37f5b1f..2e6e6041f5 100644
--- a/sampling-asan/samasan_error.c
+++ b/sampling-asan/samasan_error.c
@@ -98,7 +98,7 @@ diagnose_error (void *ptr, struct memory_pool_entry_info *entry)
bool
samasan_error_init (void)
{
- fault_area = mmap (NULL, get_system_page_size (), PROT_NONE,
+ fault_area = __mmap (NULL, get_system_page_size (), PROT_NONE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
samasan_assert (fault_area != MAP_FAILED,
"fault area allocation is failed\n");
@@ -110,7 +110,7 @@ samasan_error_deinit (void)
{
if (fault_area)
{
- samasan_assert (munmap (fault_area, get_system_page_size ()) == 0,
+ samasan_assert (__munmap (fault_area, get_system_page_size ()) == 0,
"failed to unmap fault area");
fault_area = NULL;
}
diff --git a/sampling-asan/samasan_fault_handler.c b/sampling-asan/samasan_fault_handler.c
index 8154df4292..7dcb2f28f3 100644
--- a/sampling-asan/samasan_fault_handler.c
+++ b/sampling-asan/samasan_fault_handler.c
@@ -73,14 +73,14 @@ segfault_handler (int sig, siginfo_t *info, void *context)
!is_bypassable_error (error) ||
!bypass_block (entry))
{
- signal (SIGSEGV, SIG_DFL);
+ __sigaction (SIGSEGV, &default_handler, NULL);
raise (SIGSEGV);
}
}
else if (default_handler.sa_handler == SIG_IGN)
{
/* This error is not reported by sampling-asan */
- signal (SIGSEGV, SIG_IGN);
+ __sigaction (SIGSEGV, &default_handler, NULL);
raise (SIGSEGV);
}
else
@@ -105,7 +105,7 @@ install_signal_handler (void)
sigemptyset (&action.sa_mask);
action.sa_sigaction = segfault_handler;
action.sa_flags = SA_SIGINFO;
- sigaction (SIGSEGV, &action, &default_handler);
+ __sigaction (SIGSEGV, &action, &default_handler);
handler_installed = true;
}
@@ -114,7 +114,7 @@ uninstall_signal_handler (void)
{
if (SAMASAN_UNLIKELY (!handler_installed))
return;
- sigaction (SIGSEGV, &default_handler, NULL);
+ __sigaction (SIGSEGV, &default_handler, NULL);
handler_installed = false;
}
diff --git a/sampling-asan/samasan_report.c b/sampling-asan/samasan_report.c
index 0adb6df4a4..580504c395 100644
--- a/sampling-asan/samasan_report.c
+++ b/sampling-asan/samasan_report.c
@@ -22,6 +22,7 @@
#include <string.h>
#include <inttypes.h>
#include <fcntl.h>
+#include <libioP.h>
#include "samasan_common.h"
#include "samasan_error.h"
@@ -38,7 +39,7 @@ static void
get_command_line (char *str)
{
const char *path = "/proc/self/cmdline";
- int comm_fd = open (path, O_RDONLY);
+ int comm_fd = __open (path, O_RDONLY);
size_t pos = 0;
if (comm_fd < 0)
@@ -46,12 +47,12 @@ get_command_line (char *str)
while (1)
{
- size_t ret = read (comm_fd, &str[pos], NAMELEN - pos);
+ size_t ret = __read (comm_fd, &str[pos], NAMELEN - pos);
if (ret == 0)
break;
if (ret < 0)
{
- close (comm_fd);
+ __close (comm_fd);
memset (str, 0, pos);
goto comm_error;
}
@@ -62,7 +63,7 @@ get_command_line (char *str)
break;
}
}
- close (comm_fd);
+ __close (comm_fd);
str[pos] = '\0';
return;
@@ -96,7 +97,7 @@ static void __report_printf (int fd, const char *format, ...)
samasan_mutex_lock (&report_lock);
va_start (ap, format);
- vsnprintf (buffer, BUF_LEN, format, ap);
+ __vsnprintf_internal (buffer, BUF_LEN, format, ap, 0);
va_end (ap);
samasan_mutex_unlock (&report_lock);
@@ -118,7 +119,7 @@ samasan_report_write (samasan_error_t error, uintptr_t address,
const char *error_name = get_error_name (error);
if (strcmp (report_path, "stderr"))
- stream = open (report_path, O_CREAT | O_WRONLY, 0644);
+ stream = __open (report_path, O_CREAT | O_WRONLY, 0644);
if (SAMASAN_UNLIKELY (!stream))
return;
@@ -242,7 +243,7 @@ report_end:
report_printf (stream, "%s\n", report_foot);
if (stream != STDERR_FILENO)
- close (stream);
+ __close (stream);
}
bool
--
2.25.1
More information about the Libc-alpha
mailing list