forked from torproject/tor
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5418aa8
commit d1820c1
Showing
29 changed files
with
1,913 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
|
Oops, something went wrong.