Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions core/include/ten_utils/macro/check.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include <stdio.h> // IWYU pragma: keep
#include <stdlib.h> // IWYU pragma: keep

#if !defined(OS_WINDOWS)
#include <unistd.h>
#endif

#include "ten_utils/backtrace/backtrace.h" // IWYU pragma: keep
#include "ten_utils/lib/pid.h" // IWYU pragma: keep
#include "ten_utils/lib/time.h" // IWYU pragma: keep
Expand Down Expand Up @@ -87,6 +91,40 @@

// Enable minimal protection if the optimization is enabled.

// The dprintf function is not available on Windows, so we use fprintf instead.
#if !defined(OS_WINDOWS)
#define TEN_ASSERT(expr, fmt, ...) \
do { \
/* NOLINTNEXTLINE */ \
if (!(expr)) { \
/* NOLINTNEXTLINE */ \
char *____err_msg = \
(char *)calloc(ASSERT_ERR_MSG_MAX_LENGTH, sizeof(char)); \
if (!____err_msg) { \
abort(); \
} \
int64_t pid = 0; \
int64_t tid = 0; \
ten_get_pid_tid(&pid, &tid); \
int written = \
snprintf(____err_msg, ASSERT_ERR_MSG_MAX_LENGTH, \
"%" PRId64 "(%" PRId64 ") %s@%s:%d " fmt, pid, tid, \
__func__, __FILE__, __LINE__, ##__VA_ARGS__); \
if (written < 0) { \
free(____err_msg); \
abort(); \
} \
(void)dprintf(STDERR_FILENO, "%s\n", ____err_msg); \
ten_backtrace_dump_global(0); \
/* Wait for a short period to allow backtrace to be written. */ \
ten_sleep_ms(200); \
free(____err_msg); \
/* NOLINTNEXTLINE */ \
abort(); \
} \
} while (0)
#else

#define TEN_ASSERT(expr, fmt, ...) \
do { \
/* NOLINTNEXTLINE */ \
Expand Down Expand Up @@ -121,6 +159,7 @@
abort(); \
} \
} while (0)
#endif

#endif // NDEBUG

Expand Down
6 changes: 5 additions & 1 deletion core/src/ten_runtime/binding/go/native/internal/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ ten_go_error_t ten_go_copy_c_str_to_slice_and_free(const char *src,
ten_go_error_t cgo_error;
TEN_GO_ERROR_INIT(cgo_error);

strcpy(dest, src);
// Use memcpy instead of strcpy. The Go side allocates exactly strlen(src)
// bytes (without room for '\0'). strcpy would write the '\0' terminator one
// byte past the buffer, causing a heap overflow.
size_t len = strlen(src);
memcpy(dest, src, len);
TEN_FREE(src);

return cgo_error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ static void ten_env_proxy_notify_on_configure_done(ten_env_t *ten_env,
TEN_ERROR_INIT(err);

bool rc = ten_env_on_configure_done(ten_env, &err);
TEN_ASSERT(rc, "Should not happen.");
if (!rc) {
TEN_LOGE("TEN/GO failed to on_configure_done, error: %s",
ten_error_message(&err));
}

ten_error_deinit(&err);
}
Expand All @@ -40,7 +43,10 @@ void ten_go_ten_env_on_configure_done(uintptr_t bridge_addr) {
ten_env_proxy_notify_on_configure_done, NULL,
false, &err);

TEN_ASSERT(rc, "Should not happen.");
if (!rc) {
TEN_LOGE("TEN/GO failed to on_configure_done, error: %s",
ten_error_message(&err));
}

ten_error_deinit(&err);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ static void ten_env_proxy_notify_on_deinit_done(ten_env_t *ten_env,
TEN_ASSERT(ten_env_bridge, "Should not happen.");

bool rc = ten_env_on_deinit_done(ten_env, &err);
TEN_ASSERT(rc, "Should not happen.");
if (!rc) {
TEN_LOGE("TEN/GO failed to on_deinit_done, error: %s",
ten_error_message(&err));
}

ten_error_deinit(&err);
}
Expand Down Expand Up @@ -71,9 +74,10 @@ void ten_go_ten_env_on_deinit_done(uintptr_t bridge_addr) {
bool rc = ten_env_proxy_notify(self->c_ten_env_proxy,
ten_env_proxy_notify_on_deinit_done, self,
false, &err);
TEN_ASSERT(rc,
"ten_env_proxy_notify failed, "
"ten_env_proxy_notify_on_deinit_done failed");
if (!rc) {
TEN_LOGE("TEN/GO failed to on_deinit_done, error: %s",
ten_error_message(&err));
}
} else {
TEN_ASSERT(
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ static void ten_env_proxy_notify_on_init_done(ten_env_t *ten_env,
TEN_ERROR_INIT(err);

bool rc = ten_env_on_init_done(ten_env, &err);
TEN_ASSERT(rc, "Should not happen.");
if (!rc) {
TEN_LOGE("TEN/GO failed to on_init_done, error: %s",
ten_error_message(&err));
}

ten_error_deinit(&err);
}
Expand All @@ -40,13 +43,14 @@ void ten_go_ten_env_on_init_done(uintptr_t bridge_addr) {
bool rc = ten_env_proxy_notify(self->c_ten_env_proxy,
ten_env_proxy_notify_on_init_done, NULL,
false, &err);
TEN_ASSERT(rc,
"ten_env_proxy_notify failed, ten_env_proxy_notify_on_init_done "
"failed");
if (!rc) {
TEN_LOGE("TEN/GO failed to on_init_done, error: %s",
ten_error_message(&err));
}
} else {
TEN_ASSERT(
0,
"ten_env_proxy is not set, ten_env_proxy_notify_on_init_done failed");
TEN_ASSERT(0,
"ten_env_proxy is not set, ten_env_proxy_notify_on_init_done "
"failed");
}

ten_error_deinit(&err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ static void ten_env_proxy_notify_on_start_done(ten_env_t *ten_env,
TEN_ERROR_INIT(err);

bool rc = ten_env_on_start_done(ten_env, &err);
TEN_ASSERT(rc, "Should not happen.");
if (!rc) {
TEN_LOGE("TEN/GO failed to on_start_done, error: %s",
ten_error_message(&err));
}

ten_error_deinit(&err);
}
Expand All @@ -39,7 +42,10 @@ void ten_go_ten_env_on_start_done(uintptr_t bridge_addr) {
TEN_UNUSED bool rc = ten_env_proxy_notify(self->c_ten_env_proxy,
ten_env_proxy_notify_on_start_done,
NULL, false, &err);
TEN_ASSERT(rc, "Should not happen.");
if (!rc) {
TEN_LOGE("TEN/GO failed to on_start_done, error: %s",
ten_error_message(&err));
}

ten_error_deinit(&err);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ static void ten_env_proxy_notify_on_stop_done(ten_env_t *ten_env,
TEN_ERROR_INIT(err);

bool rc = ten_env_on_stop_done(ten_env, &err);
TEN_ASSERT(rc, "Should not happen.");
if (!rc) {
TEN_LOGE("TEN/GO failed to on_stop_done, error: %s",
ten_error_message(&err));
}

ten_error_deinit(&err);
}
Expand All @@ -38,7 +41,10 @@ void ten_go_ten_env_on_stop_done(uintptr_t bridge_addr) {
TEN_UNUSED bool rc = ten_env_proxy_notify(self->c_ten_env_proxy,
ten_env_proxy_notify_on_stop_done,
NULL, false, &err);
TEN_ASSERT(rc, "Should not happen.");
if (!rc) {
TEN_LOGE("TEN/GO failed to on_stop_done, error: %s",
ten_error_message(&err));
}

ten_error_deinit(&err);

Expand Down
Loading
Loading