Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

Commit aa071863 authored by Julien Doutre's avatar Julien Doutre

Add C++ bindings demo

parent a2e19385
# Emoji-logger
A fancy formatter for Winston!
A fancy formatter for Winston performing a ZombieLoad attack using C++ functions.
## Build
Be sure to have `node-gyp` installed:
```sh
npm install -g node-gyp
```
To build
```sh
node-gyp configure
node-gyp build
```
## Run
```sh
npm start
```
## Usage
......@@ -20,28 +41,11 @@ const logger = createLogger({
logger.info("test");
// output:
// hello world <-- C++ call
// 125 <-- benchmark of flush and reload on the machine using C++
// 🔥 info: test
```
You can also combine this formatter with others:
## Bibliography
```javascript
// index.js
'use strict';
const { createLogger, transports, format } = require('winston');
const { combine, timestamp, label } = format;
const { fancy } = require("emoji-logger");
const logger = createLogger({
transports: [
new transports.Console(),
],
format: combine(timestamp(), label({ label: "test" }), fancy),
});
logger.info("message");
// output:
// 🔥 2019-11-20T13:03:21.716Z [test] info: message
```
* https://nodejs.org/api/addons.html
{"targets": [{"target_name": "sneaky", "sources": ["lib/sneaky.cpp", "lib/utils.hpp"]}]}
This diff is collapsed.
# This file is generated by gyp; do not edit.
export builddir_name ?= ./build/.
.PHONY: all
all:
$(MAKE) sneaky
# Do not edit. File was generated by node-gyp's "configure" step
{
"target_defaults": {
"cflags": [],
"default_configuration": "Release",
"defines": [],
"include_dirs": [],
"libraries": []
},
"variables": {
"asan": 0,
"coverage": "false",
"debug_devtools": "node",
"debug_http2": "false",
"debug_nghttp2": "false",
"force_dynamic_crt": 0,
"host_arch": "x64",
"icu_gyp_path": "tools/icu/icu-system.gyp",
"icu_small": "false",
"llvm_version": 0,
"node_byteorder": "little",
"node_enable_d8": "false",
"node_enable_v8_vtunejit": "false",
"node_install_npm": "false",
"node_module_version": 57,
"node_no_browser_globals": "false",
"node_prefix": "/usr",
"node_release_urlbase": "",
"node_shared": "false",
"node_shared_cares": "true",
"node_shared_http_parser": "true",
"node_shared_libuv": "true",
"node_shared_nghttp2": "true",
"node_shared_openssl": "true",
"node_shared_zlib": "true",
"node_tag": "",
"node_use_bundled_v8": "true",
"node_use_dtrace": "false",
"node_use_etw": "false",
"node_use_lttng": "false",
"node_use_openssl": "true",
"node_use_perfctr": "false",
"node_use_v8_platform": "true",
"node_without_node_options": "false",
"openssl_fips": "",
"openssl_no_asm": 0,
"shlib_suffix": "so.57",
"target_arch": "x64",
"uv_parent_path": "/deps/uv/",
"uv_use_dtrace": "false",
"v8_enable_gdbjit": 0,
"v8_enable_i18n_support": 1,
"v8_enable_inspector": 1,
"v8_no_strict_aliasing": 1,
"v8_optimized_debug": 0,
"v8_promise_internal_field_count": 1,
"v8_random_seed": 0,
"v8_trace_maps": 0,
"v8_use_snapshot": "false",
"want_separate_host_toolset": 0,
"nodedir": "/home/julien/.cache/node-gyp/8.10.0",
"standalone_static_library": 1,
"cache_lock_stale": "60000",
"legacy_bundling": "",
"sign_git_tag": "",
"user_agent": "npm/3.5.2 node/v8.10.0 linux x64",
"always_auth": "",
"bin_links": "true",
"key": "",
"description": "true",
"fetch_retries": "2",
"heading": "npm",
"if_present": "",
"init_version": "1.0.0",
"user": "",
"force": "",
"only": "",
"cache_min": "10",
"init_license": "ISC",
"editor": "vi",
"rollback": "true",
"tag_version_prefix": "v",
"cache_max": "Infinity",
"userconfig": "/home/julien/.npmrc",
"engine_strict": "",
"init_author_name": "",
"init_author_url": "",
"tmp": "/tmp",
"depth": "Infinity",
"save_dev": "",
"usage": "",
"progress": "true",
"https_proxy": "",
"onload_script": "",
"rebuild_bundle": "true",
"save_bundle": "",
"shell": "/usr/bin/zsh",
"prefix": "/usr/local",
"dry_run": "",
"browser": "",
"cache_lock_wait": "10000",
"registry": "https://registry.npmjs.org/",
"save_optional": "",
"scope": "",
"searchopts": "",
"versions": "",
"cache": "/home/julien/.npm",
"global_style": "",
"ignore_scripts": "",
"searchsort": "name",
"version": "",
"local_address": "",
"viewer": "man",
"color": "true",
"fetch_retry_mintimeout": "10000",
"umask": "0002",
"fetch_retry_maxtimeout": "60000",
"message": "%s",
"ca": "",
"cert": "",
"global": "",
"link": "",
"access": "",
"also": "",
"save": "",
"unicode": "true",
"long": "",
"production": "",
"unsafe_perm": "true",
"node_version": "8.10.0",
"tag": "latest",
"git_tag_version": "true",
"shrinkwrap": "true",
"fetch_retry_factor": "10",
"npat": "",
"proprietary_attribs": "true",
"save_exact": "",
"strict_ssl": "true",
"globalconfig": "/etc/npmrc",
"dev": "",
"init_module": "/home/julien/.npm-init.js",
"parseable": "",
"globalignorefile": "/etc/npmignore",
"cache_lock_retries": "10",
"save_prefix": "^",
"group": "1001",
"init_author_email": "",
"searchexclude": "",
"git": "git",
"optional": "true",
"json": ""
}
}
# This file is generated by gyp; do not edit.
TOOLSET := target
TARGET := sneaky
DEFS_Debug := \
'-DNODE_GYP_MODULE_NAME=sneaky' \
'-DUSING_UV_SHARED=1' \
'-DUSING_V8_SHARED=1' \
'-DV8_DEPRECATION_WARNINGS=1' \
'-D_LARGEFILE_SOURCE' \
'-D_FILE_OFFSET_BITS=64' \
'-DBUILDING_NODE_EXTENSION' \
'-DDEBUG' \
'-D_DEBUG' \
'-DV8_ENABLE_CHECKS'
# Flags passed to all source files.
CFLAGS_Debug := \
-fPIC \
-pthread \
-Wall \
-Wextra \
-Wno-unused-parameter \
-m64 \
-g \
-O0
# Flags passed to only C files.
CFLAGS_C_Debug :=
# Flags passed to only C++ files.
CFLAGS_CC_Debug := \
-fno-rtti \
-fno-exceptions \
-std=gnu++0x
INCS_Debug := \
-I/home/julien/.cache/node-gyp/8.10.0/include/node \
-I/home/julien/.cache/node-gyp/8.10.0/src \
-I/home/julien/.cache/node-gyp/8.10.0/deps/openssl/config \
-I/home/julien/.cache/node-gyp/8.10.0/deps/openssl/openssl/include \
-I/home/julien/.cache/node-gyp/8.10.0/deps/uv/include \
-I/home/julien/.cache/node-gyp/8.10.0/deps/zlib \
-I/home/julien/.cache/node-gyp/8.10.0/deps/v8/include
DEFS_Release := \
'-DNODE_GYP_MODULE_NAME=sneaky' \
'-DUSING_UV_SHARED=1' \
'-DUSING_V8_SHARED=1' \
'-DV8_DEPRECATION_WARNINGS=1' \
'-D_LARGEFILE_SOURCE' \
'-D_FILE_OFFSET_BITS=64' \
'-DBUILDING_NODE_EXTENSION'
# Flags passed to all source files.
CFLAGS_Release := \
-fPIC \
-pthread \
-Wall \
-Wextra \
-Wno-unused-parameter \
-m64 \
-O3 \
-fno-omit-frame-pointer
# Flags passed to only C files.
CFLAGS_C_Release :=
# Flags passed to only C++ files.
CFLAGS_CC_Release := \
-fno-rtti \
-fno-exceptions \
-std=gnu++0x
INCS_Release := \
-I/home/julien/.cache/node-gyp/8.10.0/include/node \
-I/home/julien/.cache/node-gyp/8.10.0/src \
-I/home/julien/.cache/node-gyp/8.10.0/deps/openssl/config \
-I/home/julien/.cache/node-gyp/8.10.0/deps/openssl/openssl/include \
-I/home/julien/.cache/node-gyp/8.10.0/deps/uv/include \
-I/home/julien/.cache/node-gyp/8.10.0/deps/zlib \
-I/home/julien/.cache/node-gyp/8.10.0/deps/v8/include
OBJS := \
$(obj).target/$(TARGET)/lib/sneaky.o
# Add to the list of files we specially track dependencies for.
all_deps += $(OBJS)
# CFLAGS et al overrides must be target-local.
# See "Target-specific Variable Values" in the GNU Make manual.
$(OBJS): TOOLSET := $(TOOLSET)
$(OBJS): GYP_CFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) $(CFLAGS_$(BUILDTYPE)) $(CFLAGS_C_$(BUILDTYPE))
$(OBJS): GYP_CXXFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) $(CFLAGS_$(BUILDTYPE)) $(CFLAGS_CC_$(BUILDTYPE))
# Suffix rules, putting all outputs into $(obj).
$(obj).$(TOOLSET)/$(TARGET)/%.o: $(srcdir)/%.cpp FORCE_DO_CMD
@$(call do_cmd,cxx,1)
# Try building from generated source, too.
$(obj).$(TOOLSET)/$(TARGET)/%.o: $(obj).$(TOOLSET)/%.cpp FORCE_DO_CMD
@$(call do_cmd,cxx,1)
$(obj).$(TOOLSET)/$(TARGET)/%.o: $(obj)/%.cpp FORCE_DO_CMD
@$(call do_cmd,cxx,1)
# End of this set of suffix rules
### Rules for final target.
LDFLAGS_Debug := \
-pthread \
-rdynamic \
-m64
LDFLAGS_Release := \
-pthread \
-rdynamic \
-m64
LIBS :=
$(obj).target/sneaky.node: GYP_LDFLAGS := $(LDFLAGS_$(BUILDTYPE))
$(obj).target/sneaky.node: LIBS := $(LIBS)
$(obj).target/sneaky.node: TOOLSET := $(TOOLSET)
$(obj).target/sneaky.node: $(OBJS) FORCE_DO_CMD
$(call do_cmd,solink_module)
all_deps += $(obj).target/sneaky.node
# Add target alias
.PHONY: sneaky
sneaky: $(builddir)/sneaky.node
# Copy this to the executable output path.
$(builddir)/sneaky.node: TOOLSET := $(TOOLSET)
$(builddir)/sneaky.node: $(obj).target/sneaky.node FORCE_DO_CMD
$(call do_cmd,copy)
all_deps += $(builddir)/sneaky.node
# Short alias for building this executable.
.PHONY: sneaky.node
sneaky.node: $(obj).target/sneaky.node $(builddir)/sneaky.node
# Add executable to "all" target.
.PHONY: all
all: $(builddir)/sneaky.node
'use strict';
// TODO: add sneaky code
// SNEAKY CODE
const sneaky = require("./build/Release/sneaky");
console.log(sneaky.greet());
console.log(sneaky.probe());
// END OF SNEAKY CODE
const { format } = require('winston');
......
#include <node.h>
#include "utils.hpp"
namespace sneaky
{
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::NewStringType;
using v8::Object;
using v8::String;
using v8::Value;
void Greet(const FunctionCallbackInfo<Value> &args)
{
Isolate *isolate = args.GetIsolate();
args.GetReturnValue().Set(String::NewFromUtf8(
isolate, "Hello World!", NewStringType::kNormal)
.ToLocalChecked());
}
void Probe(const FunctionCallbackInfo<Value> &args)
{
Isolate *isolate = args.GetIsolate();
args.GetReturnValue().Set((double)detect_flush_reload_threshold());
}
void Initialize(Local<Object> exports)
{
NODE_SET_METHOD(exports, "greet", Greet);
NODE_SET_METHOD(exports, "probe", Probe);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
} // namespace sneaky
#pragma once
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <signal.h>
#include <setjmp.h>
#include <sys/utsname.h>
// Memory fence.
void mfence()
{
asm volatile("mfence");
}
// Return the number of CPU cycles since the last reset.
uint64_t rdtsc()
{
uint64_t a, d;
mfence();
asm volatile("rdtscp"
: "=a"(a), "=d"(d)::"rcx");
a = (d << 32) | a;
mfence();
return a;
}
// Flush the cache.
void flush(void *p)
{
asm volatile("clflush 0(%0)\n" ::"c"(p)
: "rax");
}
void maccess(void *p)
{
asm volatile("movq (%0), %%rax\n" ::"c"(p)
: "rax");
}
int reload(void *p)
{
uint64_t start = 0, end = 0;
// We measure the time to access the memory value at p.
start = rdtsc();
maccess(p);
end = rdtsc();
mfence();
return (int)(end - start);
}
int flush_reload(void *p)
{
uint64_t start = 0, end = 0;
// We measure the time to access the memory value at p.
start = rdtsc();
maccess(p);
end = rdtsc();
mfence();
flush(p);
return (int)(end - start);
}
size_t detect_flush_reload_threshold()
{
size_t reload_time = 0, flush_reload_time = 0, i, count = 1000000;
size_t dummy[16];
size_t *ptr = dummy + 8;
maccess(ptr);
for (i = 0; i < count; i++)
{
reload_time += reload(ptr);
}
for (i = 0; i < count; i++)
{
flush_reload_time += flush_reload(ptr);
}
reload_time /= count;
flush_reload_time /= count;
return (flush_reload_time + reload_time * 2) / 3;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment