wasm-micro-runtime/core/shared/utils/bh_common.h
liang.he@intel.com d3a2cdd8f7 Use execve() to replace system() and rename
- Direct Execution: execve() directly executes a program, bypassing the shell.
  This avoids vulnerabilities like shell injection, which can occur with
  system() if user input is not properly sanitized.
- Controlled Environment: With execve(), you can explicitly specify the
  environment variables for the new process, providing better control over
  the execution context.
- No Shell Overhead: execve() does not invoke a shell, reducing the risk
  of unintended behavior caused by shell features or configurations.
- Predictable Behavior: execve() only executes the specified program, whereas
  system() relies on the shell, which may interpret commands differently
  based on the shell's configuration or environment.
2025-04-29 05:31:44 +00:00

94 lines
2.8 KiB
C

/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _BH_COMMON_H
#define _BH_COMMON_H
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
#define bh_memcpy_s(dest, dlen, src, slen) \
do { \
int _ret = b_memcpy_s(dest, dlen, src, slen); \
(void)_ret; \
bh_assert(_ret == 0); \
} while (0)
#define bh_memcpy_wa(dest, dlen, src, slen) \
do { \
int _ret = b_memcpy_wa(dest, dlen, src, slen); \
(void)_ret; \
bh_assert(_ret == 0); \
} while (0)
#define bh_memmove_s(dest, dlen, src, slen) \
do { \
int _ret = b_memmove_s(dest, dlen, src, slen); \
(void)_ret; \
bh_assert(_ret == 0); \
} while (0)
#define bh_strcat_s(dest, dlen, src) \
do { \
int _ret = b_strcat_s(dest, dlen, src); \
(void)_ret; \
bh_assert(_ret == 0); \
} while (0)
#define bh_strcpy_s(dest, dlen, src) \
do { \
int _ret = b_strcpy_s(dest, dlen, src); \
(void)_ret; \
bh_assert(_ret == 0); \
} while (0)
int
b_memcpy_s(void *s1, unsigned int s1max, const void *s2, unsigned int n);
int
b_memcpy_wa(void *s1, unsigned int s1max, const void *s2, unsigned int n);
int
b_memmove_s(void *s1, unsigned int s1max, const void *s2, unsigned int n);
int
b_strcat_s(char *s1, unsigned int s1max, const char *s2);
int
b_strcpy_s(char *s1, unsigned int s1max, const char *s2);
/* strdup with string allocated by BH_MALLOC */
char *
bh_strdup(const char *s);
/* strdup with string allocated by WA_MALLOC */
char *
wa_strdup(const char *s);
#if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
/*
* Executes a program referred to by cmd in bash/cmd.exe
* Always be sure that argv[argc-1] == NULL
*
* @param pathname The program to execute. need to be absolute path.
* @param argv The command line arguments.
* @param argc The number of command line arguments.
*
* like to execute "ls -l /tmp":
* bh_execve("/bin/ls", (char *const []){ "-l", "/tmp", NULL }, 3);
*/
int
bh_execve(const char *pathname, char *const argv[], int argc);
/* Tests whether can create a temporary file with the given name */
bool
bh_mkstemp(char *filename, size_t name_len);
#endif
#ifdef __cplusplus
}
#endif
#endif