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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ build/
# Cargo
target/
Cargo.lock
rules/rustls/rustls.h

# Python
*.pyc
Expand Down
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ add_custom_target("check"

set(RULES_IR_DIR "${CMAKE_BINARY_DIR}/rules")

set(RUSTLS_HEADER "${PROJECT_SOURCE_DIR}/rules/rustls/rustls.h")

file(GLOB rule_subdirs ${PROJECT_SOURCE_DIR}/rules/*)
set(cpp_rules_ir_outputs)
set(rust_rules_ir_outputs)
Expand All @@ -151,7 +153,7 @@ foreach(_rule_dir IN LISTS rule_subdirs)
OUTPUT ${_out}
COMMAND ${CMAKE_COMMAND} -E make_directory ${_out_dir}
COMMAND $<TARGET_FILE:cpp-rule-preprocessor> --dir ${_rule_dir} --out ${_out}
DEPENDS ${_srcs} ${PROJECT_SOURCE_DIR}/cpp2rust/cpp_rule_preprocessor.cpp
DEPENDS ${_srcs} ${PROJECT_SOURCE_DIR}/cpp2rust/cpp_rule_preprocessor.cpp ${RUSTLS_HEADER}
VERBATIM
)
list(APPEND cpp_rules_ir_outputs ${_out})
Expand All @@ -171,7 +173,7 @@ file(GLOB_RECURSE rule_preprocessor_sources
${PROJECT_SOURCE_DIR}/rule-preprocessor/src/*.rs)

add_custom_command(
OUTPUT ${rust_rules_ir_outputs}
OUTPUT ${rust_rules_ir_outputs} ${RUSTLS_HEADER}
COMMAND cargo +${RUST_STABLE_VERSION} build --release --manifest-path "${PROJECT_SOURCE_DIR}/rules/Cargo.toml"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change doesn't look intended

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that was a mistake. It was asymptomatic because I already had rustls.h generated on my machine

COMMAND ${CMAKE_COMMAND} -E env
CARGO_TARGET_DIR=${CMAKE_BINARY_DIR}/target_preprocessor
Expand Down
5 changes: 5 additions & 0 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1808,6 +1808,11 @@ std::string Converter::getIntegerLiteral(clang::IntegerLiteral *expr,
auto type_as_string = GetUnsafeTypeAsString(ty);

if (ty->isFloatingType() || incl_type) {
if (expr->getValue().isZero()) {
if (auto init = Mapper::MapInitializer(ty); !init.empty()) {
return init;
}
}
return std::format("{}_{}", num_as_string.c_str(), type_as_string);
}

Expand Down
18 changes: 10 additions & 8 deletions cpp2rust/cpp_rule_preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <string>

#include "compat/platform_flags.h"
#include "converter/converter_lib.h"
#include "converter/mapper.h"

namespace fs = std::filesystem;
Expand Down Expand Up @@ -109,14 +110,15 @@ class Callback : public clang::ast_matchers::MatchFinder::MatchCallback {
}
return;
}
if (auto var = R.Nodes.getNodeAs<clang::TypeAliasDecl>("tvar")) {
clang::QualType type;
if (auto *tdecl = var->getDescribedAliasTemplate()) {
type = lookupType(tdecl);
} else {
type = var->getUnderlyingType();
if (auto var = R.Nodes.getNodeAs<clang::TypedefNameDecl>("tvar")) {
clang::QualType type = var->getUnderlyingType();
if (auto *alias = llvm::dyn_cast<clang::TypeAliasDecl>(var)) {
if (auto *tdecl = alias->getDescribedAliasTemplate()) {
type = lookupType(tdecl);
}
}
out_.try_emplace(var->getQualifiedNameAsString(), Mapper::ToString(type));
auto src = Mapper::ToString(type, Mapper::ScalarSugar::kPreserve);
out_.try_emplace(var->getQualifiedNameAsString(), std::move(src));
return;
}

Expand Down Expand Up @@ -707,7 +709,7 @@ class ActionFactory : public clang::tooling::FrontendActionFactory {
&cb_);

finder_.addMatcher(
typeAliasDecl(matchesName("(^|::)t[0-9]+$"), isExpansionInMainFile())
typedefNameDecl(matchesName("(^|::)t[0-9]+$"), isExpansionInMainFile())
.bind("tvar"),
&cb_);

Expand Down
2 changes: 1 addition & 1 deletion rule-preprocessor/src/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn build_rustc_args(crate_root: &Path) -> Vec<String> {
args.push("-L".to_string());
args.push(format!("dependency={}", deps.display()));

for dep in &["libcc2rs", "libc", "brotli_sys"] {
for dep in &["libcc2rs", "libc", "brotli_sys", "rustls_ffi"] {
if let Some(rlib) = find_rlib(deps.as_path(), dep) {
args.push("--extern".to_string());
args.push(format!("{}={}", dep, rlib.display()));
Expand Down
8 changes: 8 additions & 0 deletions rules/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=Cargo.toml");

println!("cargo:rerun-if-env-changed=DEP_RUSTLS_FFI_INCLUDE");
let inc = env::var("DEP_RUSTLS_FFI_INCLUDE")
.expect("rustls-ffi did not export its include dir (DEP_RUSTLS_FFI_INCLUDE)");
let src = Path::new(&inc).join("rustls.h");
let dst = crate_root.join("rustls").join("rustls.h");
fs::copy(&src, &dst)
.unwrap_or_else(|e| panic!("failed to copy {} to {}: {e}", src.display(), dst.display()));

// Generate modules that include absolute paths
let mut buf = String::new();
for f in files {
Expand Down
248 changes: 248 additions & 0 deletions rules/rustls/src.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

#include "rustls.h"

typedef rustls_connection *t1;
typedef const rustls_connection *t2;
typedef const rustls_certificate *t3;
typedef rustls_str t4;
typedef rustls_result t5;
typedef rustls_io_result t6;
typedef rustls_tls_version t7;

rustls_result f1() { return RUSTLS_RESULT_OK; }
rustls_result f2() { return RUSTLS_RESULT_NULL_PARAMETER; }
rustls_result f3() { return RUSTLS_RESULT_PLAINTEXT_EMPTY; }
rustls_result f4() { return RUSTLS_RESULT_UNEXPECTED_EOF; }

rustls_tls_version f5() { return RUSTLS_TLS_VERSION_TLSV1_2; }
rustls_tls_version f6() { return RUSTLS_TLS_VERSION_TLSV1_3; }

rustls_result f7(rustls_connection *conn, uint8_t *buf, size_t count,
size_t *out_n) {
return rustls_connection_read(conn, buf, count, out_n);
}
rustls_result f8(rustls_connection *conn, const uint8_t *buf, size_t count,
size_t *out_n) {
return rustls_connection_write(conn, buf, count, out_n);
}
rustls_result f9(rustls_connection *conn) {
return rustls_connection_process_new_packets(conn);
}
bool f10(const rustls_connection *conn) {
return rustls_connection_wants_read(conn);
}
bool f11(const rustls_connection *conn) {
return rustls_connection_wants_write(conn);
}
bool f12(const rustls_connection *conn) {
return rustls_connection_is_handshaking(conn);
}
void f13(rustls_connection *conn) {
return rustls_connection_send_close_notify(conn);
}
void f14(rustls_connection *conn, void *userdata) {
return rustls_connection_set_userdata(conn, userdata);
}
void f15(const rustls_connection *conn, const uint8_t **protocol_out,
size_t *protocol_out_len) {
return rustls_connection_get_alpn_protocol(conn, protocol_out,
protocol_out_len);
}
uint16_t f16(const rustls_connection *conn) {
return rustls_connection_get_protocol_version(conn);
}
rustls_str f17(const rustls_connection *conn) {
return rustls_connection_get_negotiated_ciphersuite_name(conn);
}
rustls_str f18(const rustls_connection *conn) {
return rustls_connection_get_negotiated_key_exchange_group_name(conn);
}
const rustls_certificate *f19(const rustls_connection *conn, size_t i) {
return rustls_connection_get_peer_certificate(conn, i);
}
void f20(rustls_connection *conn) { return rustls_connection_free(conn); }

typedef rustls_client_config *t8;
typedef const rustls_client_config *t9;
typedef rustls_client_config_builder *t10;
typedef const rustls_client_config_builder *t11;
typedef rustls_certified_key *t12;
typedef const rustls_certified_key *t13;
typedef rustls_crypto_provider *t14;
typedef const rustls_crypto_provider *t15;
typedef rustls_crypto_provider_builder *t16;
typedef const rustls_crypto_provider_builder *t17;
typedef rustls_root_cert_store *t18;
typedef const rustls_root_cert_store *t19;
typedef rustls_root_cert_store_builder *t20;
typedef const rustls_root_cert_store_builder *t21;
typedef rustls_server_cert_verifier *t22;
typedef const rustls_server_cert_verifier *t23;
typedef rustls_web_pki_server_cert_verifier_builder *t24;
typedef const rustls_web_pki_server_cert_verifier_builder *t25;
typedef rustls_supported_ciphersuite *t26;
typedef const rustls_supported_ciphersuite *t27;
typedef rustls_slice_bytes t28;
typedef rustls_verify_server_cert_params t29;

rustls_result f21(rustls_client_config_builder *builder,
const rustls_client_config **config_out) {
return rustls_client_config_builder_build(builder, config_out);
}
void f22(rustls_client_config_builder *config) {
return rustls_client_config_builder_free(config);
}
rustls_result f23(const rustls_crypto_provider *provider,
const uint16_t *tls_versions, size_t tls_versions_len,
rustls_client_config_builder **builder_out) {
return rustls_client_config_builder_new_custom(provider, tls_versions,
tls_versions_len, builder_out);
}
rustls_result f24(rustls_client_config_builder *builder,
const rustls_slice_bytes *protocols, size_t len) {
return rustls_client_config_builder_set_alpn_protocols(builder, protocols,
len);
}
rustls_result f25(rustls_client_config_builder *builder,
const rustls_certified_key *const *certified_keys,
size_t certified_keys_len) {
return rustls_client_config_builder_set_certified_key(builder, certified_keys,
certified_keys_len);
}
void f26(rustls_client_config_builder *builder,
const rustls_server_cert_verifier *verifier) {
return rustls_client_config_builder_set_server_verifier(builder, verifier);
}
void f27(const rustls_client_config *config) {
return rustls_client_config_free(config);
}
rustls_result f28(const rustls_client_config *config, const char *server_name,
rustls_connection **conn_out) {
return rustls_client_connection_new(config, server_name, conn_out);
}

rustls_result f29(const rustls_certificate *cert, const uint8_t **out_der_data,
size_t *out_der_len) {
return rustls_certificate_get_der(cert, out_der_data, out_der_len);
}
rustls_result f30(const uint8_t *cert_chain, size_t cert_chain_len,
const uint8_t *private_key, size_t private_key_len,
const rustls_certified_key **certified_key_out) {
return rustls_certified_key_build(cert_chain, cert_chain_len, private_key,
private_key_len, certified_key_out);
}
void f31(const rustls_certified_key *key) {
return rustls_certified_key_free(key);
}
rustls_result f32(const rustls_certified_key *key) {
return rustls_certified_key_keys_match(key);
}
rustls_result f33(rustls_root_cert_store_builder *builder, const uint8_t *pem,
size_t pem_len, bool strict) {
return rustls_root_cert_store_builder_add_pem(builder, pem, pem_len, strict);
}
rustls_result f34(rustls_root_cert_store_builder *builder,
const rustls_root_cert_store **root_cert_store_out) {
return rustls_root_cert_store_builder_build(builder, root_cert_store_out);
}
void f35(rustls_root_cert_store_builder *builder) {
return rustls_root_cert_store_builder_free(builder);
}
rustls_result f36(rustls_root_cert_store_builder *builder, const char *filename,
bool strict) {
return rustls_root_cert_store_builder_load_roots_from_file(builder, filename,
strict);
}
rustls_root_cert_store_builder *f37() {
return rustls_root_cert_store_builder_new();
}
void f38(const rustls_root_cert_store *store) {
return rustls_root_cert_store_free(store);
}

rustls_result f39(rustls_crypto_provider_builder *builder,
const rustls_crypto_provider **provider_out) {
return rustls_crypto_provider_builder_build(builder, provider_out);
}
void f40(rustls_crypto_provider_builder *builder) {
return rustls_crypto_provider_builder_free(builder);
}
rustls_result f41(rustls_crypto_provider_builder **builder_out) {
return rustls_crypto_provider_builder_new_from_default(builder_out);
}
rustls_result f42(rustls_crypto_provider_builder *builder,
const rustls_supported_ciphersuite *const *cipher_suites,
size_t cipher_suites_len) {
return rustls_crypto_provider_builder_set_cipher_suites(builder, cipher_suites,
cipher_suites_len);
}
void f43(const rustls_crypto_provider *provider) {
return rustls_crypto_provider_free(provider);
}
const rustls_supported_ciphersuite *f44(size_t index) {
return rustls_default_crypto_provider_ciphersuites_get(index);
}
size_t f45() { return rustls_default_crypto_provider_ciphersuites_len(); }
rustls_result f46(uint8_t *buff, size_t len) {
return rustls_default_crypto_provider_random(buff, len);
}

rustls_result f47(rustls_server_cert_verifier **verifier_out) {
return rustls_platform_server_cert_verifier(verifier_out);
}
void f48(rustls_server_cert_verifier *verifier) {
return rustls_server_cert_verifier_free(verifier);
}
rustls_result f49(rustls_web_pki_server_cert_verifier_builder *builder,
const uint8_t *crl_pem, size_t crl_pem_len) {
return rustls_web_pki_server_cert_verifier_builder_add_crl(builder, crl_pem,
crl_pem_len);
}
rustls_result f50(rustls_web_pki_server_cert_verifier_builder *builder,
rustls_server_cert_verifier **verifier_out) {
return rustls_web_pki_server_cert_verifier_builder_build(builder,
verifier_out);
}
void f51(rustls_web_pki_server_cert_verifier_builder *builder) {
return rustls_web_pki_server_cert_verifier_builder_free(builder);
}
rustls_web_pki_server_cert_verifier_builder *
f52(const rustls_root_cert_store *store) {
return rustls_web_pki_server_cert_verifier_builder_new(store);
}

uint16_t f53(const rustls_supported_ciphersuite *supported_ciphersuite) {
return rustls_supported_ciphersuite_get_suite(supported_ciphersuite);
}
rustls_tls_version
f54(const rustls_supported_ciphersuite *supported_ciphersuite) {
return rustls_supported_ciphersuite_protocol_version(supported_ciphersuite);
}

rustls_str f55() { return rustls_version(); }

void f56(unsigned int result, char *buf, size_t len, size_t *out_n) {
return rustls_error(result, buf, len, out_n);
}
bool f57(unsigned int result) { return rustls_result_is_cert_error(result); }

rustls_io_result f58(rustls_connection *conn, rustls_read_callback callback,
void *userdata, size_t *out_n) {
return rustls_connection_read_tls(conn, callback, userdata, out_n);
}
rustls_io_result f59(rustls_connection *conn, rustls_write_callback callback,
void *userdata, size_t *out_n) {
return rustls_connection_write_tls(conn, callback, userdata, out_n);
}
rustls_result f60(rustls_client_config_builder *builder,
rustls_keylog_log_callback log_cb,
rustls_keylog_will_log_callback will_log_cb) {
return rustls_client_config_builder_set_key_log(builder, log_cb, will_log_cb);
}
rustls_result f61(rustls_client_config_builder *config_builder,
rustls_verify_server_cert_callback callback) {
return rustls_client_config_builder_dangerous_set_certificate_verifier(
config_builder, callback);
}
Loading
Loading