Skip to content

Commit

Permalink
rust implementation of protover
Browse files Browse the repository at this point in the history
  • Loading branch information
chelseakomlo authored and nmathewson committed Oct 27, 2017
1 parent 5418aa8 commit d1820c1
Show file tree
Hide file tree
Showing 29 changed files with 1,913 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions src/common/include.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down
55 changes: 55 additions & 0 deletions src/common/rust_types.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* 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) */

22 changes: 22 additions & 0 deletions src/common/rust_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* 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

1 change: 1 addition & 0 deletions src/or/include.am
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ LIBTOR_A_SOURCES = \
src/or/parsecommon.c \
src/or/periodic.c \
src/or/protover.c \
src/or/protover_rust.c \
src/or/proto_cell.c \
src/or/proto_control0.c \
src/or/proto_ext_or.c \
Expand Down
4 changes: 4 additions & 0 deletions src/or/protover.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "protover.h"
#include "routerparse.h"

#ifndef HAVE_RUST

static const smartlist_t *get_supported_protocol_list(void);
static int protocol_list_contains(const smartlist_t *protos,
protocol_type_t pr, uint32_t ver);
Expand Down Expand Up @@ -735,3 +737,5 @@ protover_free_all(void)
}
}

#endif

6 changes: 5 additions & 1 deletion src/or/protover.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ typedef struct proto_entry_t {
smartlist_t *ranges;
} proto_entry_t;

#if !defined(HAVE_RUST) && defined(TOR_UNIT_TESTS)
STATIC smartlist_t *parse_protocol_list(const char *s);
STATIC void proto_entry_free(proto_entry_t *entry);
STATIC char *encode_protocol_list(const smartlist_t *sl);
STATIC const char *protocol_type_to_str(protocol_type_t pr);
STATIC int str_to_protocol_type(const char *s, protocol_type_t *pr_out);
STATIC void proto_entry_free(proto_entry_t *entry);

#endif

#endif /* defined(PROTOVER_PRIVATE) */

#endif /* !defined(TOR_PROTOVER_H) */
Expand Down
111 changes: 111 additions & 0 deletions src/or/protover_rust.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* Copyright (c) 2016-2017, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/*
* \file protover_rust.c
* \brief Provide a C wrapper for functions exposed in /src/rust/protover,
* and safe translation/handling between the Rust/C boundary.
*/

#include "or.h"
#include "protover.h"
#include "rust_types.h"

#ifdef HAVE_RUST

int rust_protover_all_supported(const char *s, char **missing);
rust_str_ref_t rust_protover_compute_for_old_tor(const char *version);
rust_str_ref_t rust_protover_compute_vote(const smartlist_t *proto_votes,
int threshold);
rust_str_ref_t rust_protover_get_supported_protocols(void);
int rust_protocol_list_supports_protocol(const char *list, protocol_type_t tp,
uint32_t version);
int rust_protover_is_supported_here(protocol_type_t pr, uint32_t ver);

/* Define for compatibility, used in main.c */
void protover_free_all(void) {};

/*
* Wrap rust_protover_is_supported_here, located in /src/rust/protover
*/
int
protover_is_supported_here(protocol_type_t pr, uint32_t ver)
{
return rust_protover_is_supported_here(pr, ver);
}

/*
* Wrap rust_protover_list_supports_protocol, located in /src/rust/protover
*/
int
protocol_list_supports_protocol(const char *list, protocol_type_t tp,
uint32_t version)
{
return rust_protocol_list_supports_protocol(list, tp, version);
}

/*
* Wrap rust_protover_get_supported_protocols, located in /src/rust/protover
*/
const char *
protover_get_supported_protocols(void)
{
rust_str_ref_t rust_protocols = rust_protover_get_supported_protocols();

char *protocols = NULL;
if (rust_protocols != NULL) {
move_rust_str_to_c_and_free(rust_protocols, &protocols);
}
return protocols;
}

/*
* Wrap rust_protover_compute_vote, located in /src/rust/protover
*/
char *
protover_compute_vote(const smartlist_t *proto_strings,
int threshold)
{
rust_str_ref_t rust_protocols = rust_protover_compute_vote(proto_strings,
threshold);

char *protocols = NULL;
if (rust_protocols != NULL) {
move_rust_str_to_c_and_free(rust_protocols, &protocols);
}
return protocols;
}

/*
* Wrap rust_protover_all_supported, located in /src/rust/protover
*/
int
protover_all_supported(const char *s, char **missing_out)
{
rust_str_ref_t missing_out_copy = NULL;
int is_supported = rust_protover_all_supported(s, &missing_out_copy);

if (!is_supported) {
move_rust_str_to_c_and_free(missing_out_copy, missing_out);
}

return is_supported;
}

/*
* Wrap rust_compute_for_old_tor, located in /src/rust/protover
*/
const char *
protover_compute_for_old_tor(const char *version)
{
rust_str_ref_t rust_protocols = rust_protover_compute_for_old_tor(version);

char *protocols = NULL;
if (rust_protocols != NULL) {
move_rust_str_to_c_and_free(rust_protocols, &protocols);
}
return protocols;
}

#endif

31 changes: 31 additions & 0 deletions src/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["tor_util"]
members = ["tor_util", "protover", "smartlist", "external", "c_string"]

[profile.release]
debug = true
Expand Down
13 changes: 13 additions & 0 deletions src/rust/c_string/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
authors = ["The Tor Project"]
version = "0.0.1"
name = "c_string"

[dependencies]
libc = "0.2.22"

[lib]
name = "c_string"
path = "ffi.rs"
crate_type = ["rlib", "staticlib"]

19 changes: 19 additions & 0 deletions src/rust/c_string/ffi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! FFI functions, only to be called from C.
//!
//! This module provides the ability for C to free strings that have been
//! allocated in Rust.

extern crate libc;

use libc::c_char;
use std::ffi::CString;

/// This allows strings allocated in Rust to be freed in Rust. Every string
/// sent across the Rust/C FFI boundary should utilize this function for
/// freeing strings allocated in Rust.
#[no_mangle]
pub extern "C" fn free_rust_str(ptr: *mut c_char) {
if !ptr.is_null() {
unsafe { CString::from_raw(ptr) };
}
}
12 changes: 12 additions & 0 deletions src/rust/c_string/include.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
EXTRA_DIST +=\
src/rust/c_string/Cargo.toml \
src/rust/c_string/ffi.rs

src/rust/target/release/@TOR_RUST_C_STRING_STATIC_NAME@: FORCE
( cd "$(abs_top_srcdir)/src/rust/c_string" ; \
CARGO_TARGET_DIR="$(abs_top_builddir)/src/rust/target" \
CARGO_HOME="$(abs_top_builddir)/src/rust" \
$(CARGO) build --release --quiet $(CARGO_ONLINE) )

FORCE:

13 changes: 13 additions & 0 deletions src/rust/external/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
authors = ["The Tor Project"]
version = "0.0.1"
name = "external"

[dependencies]
libc = "0.2.22"

[lib]
name = "external"
path = "lib.rs"
crate_type = ["rlib", "staticlib"]

Loading

0 comments on commit d1820c1

Please sign in to comment.