Loading Makefile.am +2 −0 Original line number Diff line number Diff line Loading @@ -27,6 +27,8 @@ endif if USE_RUST rust_ldadd=$(top_builddir)/src/rust/target/release/@TOR_RUST_UTIL_STATIC_NAME@ rust_ldadd+=$(top_builddir)/src/rust/target/release/@TOR_RUST_PROTOVER_STATIC_NAME@ rust_ldadd+=$(top_builddir)/src/rust/target/release/@TOR_RUST_C_STRING_STATIC_NAME@ else rust_ldadd= endif Loading configure.ac +6 −0 Original line number Diff line number Diff line Loading @@ -440,11 +440,17 @@ if test "x$enable_rust" = "xyes"; then dnl the MSVC naming convention. if test "$bwin32" = "true"; then TOR_RUST_UTIL_STATIC_NAME=tor_util.lib TOR_RUST_PROTOVER_STATIC_NAME=libprotover.lib TOR_RUST_C_STRING_STATIC_NAME=libc_string.lib else TOR_RUST_UTIL_STATIC_NAME=libtor_util.a TOR_RUST_PROTOVER_STATIC_NAME=libprotover.a TOR_RUST_C_STRING_STATIC_NAME=libc_string.a fi AC_SUBST(TOR_RUST_UTIL_STATIC_NAME) AC_SUBST(TOR_RUST_PROTOVER_STATIC_NAME) AC_SUBST(TOR_RUST_C_STRING_STATIC_NAME) AC_SUBST(CARGO_ONLINE) AC_SUBST(RUST_DL) Loading src/common/include.am +2 −0 Original line number Diff line number Diff line Loading @@ -94,6 +94,7 @@ LIBOR_A_SRC = \ src/common/util_bug.c \ src/common/util_format.c \ src/common/util_process.c \ src/common/rust_types.c \ src/common/sandbox.c \ src/common/storagedir.c \ src/common/workqueue.c \ Loading Loading @@ -179,6 +180,7 @@ COMMONHEADERS = \ src/common/procmon.h \ src/common/pubsub.h \ src/common/sandbox.h \ src/common/rust_types.h \ src/common/storagedir.h \ src/common/testsupport.h \ src/common/timers.h \ Loading src/common/rust_types.c 0 → 100644 +55 −0 Original line number Diff line number Diff line /* Copyright (c) 2017, The Tor Project, Inc. */ /* See LICENSE for licensing information */ /** * \file rust_types.c * \brief This file is used for handling types returned from Rust to C. **/ #include "or.h" #include "rust_types.h" #ifdef HAVE_RUST void free_rust_str(char *ret); /* Because Rust strings can only be freed from Rust, we first copy the string's * contents to a c pointer, and then free the Rust string. * This function can be extended to return a success/error value if needed. */ void move_rust_str_to_c_and_free(rust_str_ref_t src, char **dest) { if (!src) { log_warn(LD_BUG, "Received a null pointer from protover rust."); return; } if (!dest) { log_warn(LD_BUG, "Received a null pointer from caller to protover rust. " "This results in a memory leak due to not freeing the rust " "string that was meant to be copied.."); return; } *dest = tor_strdup(src); free_rust_str(src); return; } #else /* When Rust is not enabled, this function should never be used. Log a warning * in the case that it is ever called when Rust is not enabled. */ void move_rust_str_to_c_and_free(rust_str_ref_t src, char **dest) { (void) src; (void) dest; log_warn(LD_BUG, "Received a call to free a Rust string when we are " " not running with Rust enabled."); return; } #endif /* defined(HAVE_RUST) */ src/common/rust_types.h 0 → 100644 +22 −0 Original line number Diff line number Diff line /* Copyright (c) 2017, The Tor Project, Inc. */ /* See LICENSE for licensing information */ /** * \file rust_types.h * \brief Headers for rust_types.c **/ #include "or.h" #ifndef TOR_RUST_TYPES_H #define TOR_RUST_TYPES_H /* This type is used to clearly mark strings that have been allocated in Rust, * and therefore strictly need to use the free_rust_str method to free. */ typedef char *rust_str_ref_t; void move_rust_str_to_c_and_free(rust_str_ref_t src, char **dest); #endif Loading
Makefile.am +2 −0 Original line number Diff line number Diff line Loading @@ -27,6 +27,8 @@ endif if USE_RUST rust_ldadd=$(top_builddir)/src/rust/target/release/@TOR_RUST_UTIL_STATIC_NAME@ rust_ldadd+=$(top_builddir)/src/rust/target/release/@TOR_RUST_PROTOVER_STATIC_NAME@ rust_ldadd+=$(top_builddir)/src/rust/target/release/@TOR_RUST_C_STRING_STATIC_NAME@ else rust_ldadd= endif Loading
configure.ac +6 −0 Original line number Diff line number Diff line Loading @@ -440,11 +440,17 @@ if test "x$enable_rust" = "xyes"; then dnl the MSVC naming convention. if test "$bwin32" = "true"; then TOR_RUST_UTIL_STATIC_NAME=tor_util.lib TOR_RUST_PROTOVER_STATIC_NAME=libprotover.lib TOR_RUST_C_STRING_STATIC_NAME=libc_string.lib else TOR_RUST_UTIL_STATIC_NAME=libtor_util.a TOR_RUST_PROTOVER_STATIC_NAME=libprotover.a TOR_RUST_C_STRING_STATIC_NAME=libc_string.a fi AC_SUBST(TOR_RUST_UTIL_STATIC_NAME) AC_SUBST(TOR_RUST_PROTOVER_STATIC_NAME) AC_SUBST(TOR_RUST_C_STRING_STATIC_NAME) AC_SUBST(CARGO_ONLINE) AC_SUBST(RUST_DL) Loading
src/common/include.am +2 −0 Original line number Diff line number Diff line Loading @@ -94,6 +94,7 @@ LIBOR_A_SRC = \ src/common/util_bug.c \ src/common/util_format.c \ src/common/util_process.c \ src/common/rust_types.c \ src/common/sandbox.c \ src/common/storagedir.c \ src/common/workqueue.c \ Loading Loading @@ -179,6 +180,7 @@ COMMONHEADERS = \ src/common/procmon.h \ src/common/pubsub.h \ src/common/sandbox.h \ src/common/rust_types.h \ src/common/storagedir.h \ src/common/testsupport.h \ src/common/timers.h \ Loading
src/common/rust_types.c 0 → 100644 +55 −0 Original line number Diff line number Diff line /* Copyright (c) 2017, The Tor Project, Inc. */ /* See LICENSE for licensing information */ /** * \file rust_types.c * \brief This file is used for handling types returned from Rust to C. **/ #include "or.h" #include "rust_types.h" #ifdef HAVE_RUST void free_rust_str(char *ret); /* Because Rust strings can only be freed from Rust, we first copy the string's * contents to a c pointer, and then free the Rust string. * This function can be extended to return a success/error value if needed. */ void move_rust_str_to_c_and_free(rust_str_ref_t src, char **dest) { if (!src) { log_warn(LD_BUG, "Received a null pointer from protover rust."); return; } if (!dest) { log_warn(LD_BUG, "Received a null pointer from caller to protover rust. " "This results in a memory leak due to not freeing the rust " "string that was meant to be copied.."); return; } *dest = tor_strdup(src); free_rust_str(src); return; } #else /* When Rust is not enabled, this function should never be used. Log a warning * in the case that it is ever called when Rust is not enabled. */ void move_rust_str_to_c_and_free(rust_str_ref_t src, char **dest) { (void) src; (void) dest; log_warn(LD_BUG, "Received a call to free a Rust string when we are " " not running with Rust enabled."); return; } #endif /* defined(HAVE_RUST) */
src/common/rust_types.h 0 → 100644 +22 −0 Original line number Diff line number Diff line /* Copyright (c) 2017, The Tor Project, Inc. */ /* See LICENSE for licensing information */ /** * \file rust_types.h * \brief Headers for rust_types.c **/ #include "or.h" #ifndef TOR_RUST_TYPES_H #define TOR_RUST_TYPES_H /* This type is used to clearly mark strings that have been allocated in Rust, * and therefore strictly need to use the free_rust_str method to free. */ typedef char *rust_str_ref_t; void move_rust_str_to_c_and_free(rust_str_ref_t src, char **dest); #endif