crossfire/agent-deployment/main_bc.c

136 lines
3.9 KiB
C

// crossfire: distributed brute force infrastructure
//
// Copyright (C) 2020 haskal
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#define MZ_PRECISE_GC
#include <racket/scheme.h>
#include <stdint.h>
#include <stdio.h>
#include <monocypher.h>
#define rkt_app_start _binary_app_zo_start
#define rkt_app_end _binary_app_zo_end
extern const char rkt_app_start;
extern const char rkt_app_end;
// ffi defs
typedef struct {
const char* name;
uintptr_t ptr;
} ffi_ent;
#define STR(x) #x
#define FFI_ENT(name) {STR(name), (uintptr_t) name}
// runtime hacks
void rktio_init_cpu(void* rktio);
int rktio_processor_count(void* rktio);
static const ffi_ent ffi_table[] = {
FFI_ENT(crypto_sign_public_key),
FFI_ENT(crypto_sign),
FFI_ENT(crypto_check),
FFI_ENT(crypto_key_exchange_public_key),
FFI_ENT(crypto_key_exchange),
FFI_ENT(crypto_lock),
FFI_ENT(crypto_unlock),
FFI_ENT(crypto_wipe),
FFI_ENT(crypto_blake2b_init),
FFI_ENT(crypto_blake2b_update),
FFI_ENT(crypto_blake2b_final),
FFI_ENT(rktio_init_cpu),
FFI_ENT(rktio_processor_count)
};
static const size_t ffi_table_size = sizeof(ffi_table)/sizeof(ffi_ent);
// setup ffi table to be passed to racket
static void bc_setup_ffi_table(Scheme_Env* parent) {
Scheme_Env* mod = NULL;
Scheme_Object* table = NULL;
Scheme_Object* table_size = NULL;
Scheme_Object* arch = NULL;
MZ_GC_DECL_REG(5);
MZ_GC_VAR_IN_REG(0, parent);
MZ_GC_VAR_IN_REG(1, mod);
MZ_GC_VAR_IN_REG(2, table);
MZ_GC_VAR_IN_REG(3, table_size);
MZ_GC_VAR_IN_REG(4, arch);
MZ_GC_REG();
mod = scheme_primitive_module(scheme_intern_symbol("#%static-ffi"), parent);
table = scheme_make_integer(&ffi_table[0]);
scheme_add_global("table", table, mod);
table_size = scheme_make_integer(ffi_table_size);
scheme_add_global("table-size", table_size, mod);
arch = scheme_make_utf8_string(APP_ARCH);
scheme_add_global("arch", arch, mod);
scheme_finish_primitive_module(mod);
MZ_GC_UNREG();
}
static int run_bc(Scheme_Env* e, int argc, char* argv[]) {
(void)argc;
(void)argv;
volatile mz_jmp_buf* save;
mz_jmp_buf fresh;
int rv = 0;
size_t load_size = ((uintptr_t) &rkt_app_end) - ((uintptr_t) &rkt_app_start);
Scheme_Object* l = NULL;
Scheme_Object* a[2] = { NULL, NULL };
// gc setup
MZ_GC_DECL_REG(5);
MZ_GC_VAR_IN_REG(0, e);
MZ_GC_VAR_IN_REG(1, l);
MZ_GC_ARRAY_VAR_IN_REG(2, a, 2);
MZ_GC_REG();
bc_setup_ffi_table(e);
scheme_register_embedded_load(load_size, &rkt_app_start);
scheme_embedded_load(load_size, &rkt_app_start, 1);
l = scheme_make_null();
l = scheme_make_pair(scheme_intern_symbol(APP_NAME), l);
l = scheme_make_pair(scheme_intern_symbol("quote"), l);
a[0] = l;
a[1] = scheme_false;
save = scheme_current_thread->error_buf;
scheme_current_thread->error_buf = &fresh;
if (scheme_setjmp(scheme_error_buf)) {
fprintf(stderr, "[WRAPPER] encountered top-level racket error. aborting\n");
rv = -1;
} else {
scheme_dynamic_require(2, a);
}
scheme_current_thread->error_buf = (mz_jmp_buf*) save;
MZ_GC_UNREG();
return rv;
}
int main(int argc, char *argv[]) {
return scheme_main_setup(1, run_bc, argc, argv);
}