Skip to content

Commit

Permalink
[bun.js] Implement Bun.sha1, Bun.sha256, Bun.sha384, Bun.sha512, Bun.…
Browse files Browse the repository at this point in the history
…sha512_384
  • Loading branch information
Jarred-Sumner committed Apr 13, 2022
1 parent b3522b2 commit f6d73cb
Show file tree
Hide file tree
Showing 11 changed files with 602 additions and 71 deletions.
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,12 @@ fetch:
$(ZIG) build -Drelease-fast fetch-obj
$(CXX) $(PACKAGE_DIR)/fetch.o -g $(OPTIMIZATION_LEVEL) -o ./misctools/fetch $(DEFAULT_LINKER_FLAGS) -lc $(ARCHIVE_FILES)
rm -rf $(PACKAGE_DIR)/fetch.o


sha:
$(ZIG) build -Drelease-fast sha-bench-obj
$(CXX) $(PACKAGE_DIR)/sha.o -g $(OPTIMIZATION_LEVEL) -o ./misctools/sha $(DEFAULT_LINKER_FLAGS) -lc $(ARCHIVE_FILES)
rm -rf $(PACKAGE_DIR)/sha.o

fetch-debug:
$(ZIG) build fetch-obj
$(CXX) $(DEBUG_PACKAGE_DIR)/fetch.o -g $(OPTIMIZATION_LEVEL) -o ./misctools/fetch $(DEFAULT_LINKER_FLAGS) -lc $(ARCHIVE_FILES)
Expand Down Expand Up @@ -979,7 +984,7 @@ mimalloc:
-DMI_OSX_INTERPOSE=OFF \
-DMI_BUILD_OBJECT=ON \
-DMI_USE_CXX=ON \
-DMI_OVERRIDE=OFF \
-DMI_OVERRIDE=ON \
-DCMAKE_C_FLAGS="$(CFLAGS)" \
-DCMAKE_CXX_FLAGS="$(CFLAGS)" \
${MIMALLOC_OVERRIDE_FLAG} \
Expand Down Expand Up @@ -1173,7 +1178,7 @@ endif
build-unit:
@rm -rf zig-out/bin/$(testname)
@mkdir -p zig-out/bin
zig test $(realpath $(testpath)) \
zig test $(realpath $(testpath)) \
$(testfilterflag) \
$(PACKAGE_MAP) \
--main-pkg-path $(BUN_DIR) \
Expand Down Expand Up @@ -1225,7 +1230,7 @@ copy-to-bun-release-dir-bin:
cp -r $(PACKAGE_DIR)/bun-profile $(BUN_RELEASE_DIR)/bun-profile


PACKAGE_MAP = --pkg-begin thread_pool $(BUN_DIR)/src/thread_pool.zig --pkg-begin io $(BUN_DIR)/src/io/io_$(OS_NAME).zig --pkg-end --pkg-begin http $(BUN_DIR)/src/http_client_async.zig --pkg-begin strings $(BUN_DIR)/src/string_immutable.zig --pkg-end --pkg-begin picohttp $(BUN_DIR)/src/deps/picohttp.zig --pkg-end --pkg-begin io $(BUN_DIR)/src/io/io_darwin.zig --pkg-end --pkg-begin boringssl $(BUN_DIR)/src/deps/boringssl.zig --pkg-end --pkg-begin thread_pool $(BUN_DIR)/src/thread_pool.zig --pkg-begin io $(BUN_DIR)/src/io/io_darwin.zig --pkg-end --pkg-begin http $(BUN_DIR)/src/http_client_async.zig --pkg-begin strings $(BUN_DIR)/src/string_immutable.zig --pkg-end --pkg-begin picohttp $(BUN_DIR)/src/deps/picohttp.zig --pkg-end --pkg-begin io $(BUN_DIR)/src/io/io_darwin.zig --pkg-end --pkg-begin boringssl $(BUN_DIR)/src/deps/boringssl.zig --pkg-end --pkg-begin thread_pool $(BUN_DIR)/src/thread_pool.zig --pkg-end --pkg-end --pkg-end --pkg-end --pkg-end --pkg-begin picohttp $(BUN_DIR)/src/deps/picohttp.zig --pkg-end --pkg-begin io $(BUN_DIR)/src/io/io_darwin.zig --pkg-end --pkg-begin strings $(BUN_DIR)/src/string_immutable.zig --pkg-end --pkg-begin clap $(BUN_DIR)/src/deps/zig-clap/clap.zig --pkg-end --pkg-begin http $(BUN_DIR)/src/http_client_async.zig --pkg-begin strings $(BUN_DIR)/src/string_immutable.zig --pkg-end --pkg-begin picohttp $(BUN_DIR)/src/deps/picohttp.zig --pkg-end --pkg-begin io $(BUN_DIR)/src/io/io_darwin.zig --pkg-end --pkg-begin boringssl $(BUN_DIR)/src/deps/boringssl.zig --pkg-end --pkg-begin thread_pool $(BUN_DIR)/src/thread_pool.zig --pkg-begin io $(BUN_DIR)/src/io/io_darwin.zig --pkg-end --pkg-begin http $(BUN_DIR)/src/http_client_async.zig --pkg-begin strings $(BUN_DIR)/src/string_immutable.zig --pkg-end --pkg-begin picohttp $(BUN_DIR)/src/deps/picohttp.zig --pkg-end --pkg-begin io $(BUN_DIR)/src/io/io_darwin.zig --pkg-end --pkg-begin boringssl $(BUN_DIR)/src/deps/boringssl.zig --pkg-end --pkg-begin thread_pool $(BUN_DIR)/src/thread_pool.zig --pkg-end --pkg-end --pkg-end --pkg-end --pkg-begin boringssl $(BUN_DIR)/src/deps/boringssl.zig --pkg-end --pkg-begin javascript_core $(BUN_DIR)/src/jsc.zig --pkg-begin http $(BUN_DIR)/src/http_client_async.zig --pkg-end --pkg-begin strings $(BUN_DIR)/src/string_immutable.zig --pkg-end --pkg-begin picohttp $(BUN_DIR)/src/deps/picohttp.zig --pkg-end --pkg-end
PACKAGE_MAP = --pkg-begin thread_pool $(BUN_DIR)/src/thread_pool.zig --pkg-begin io $(BUN_DIR)/src/io/io_$(OS_NAME).zig --pkg-end --pkg-begin http $(BUN_DIR)/src/http_client_async.zig --pkg-begin strings $(BUN_DIR)/src/string_immutable.zig --pkg-end --pkg-begin picohttp $(BUN_DIR)/src/deps/picohttp.zig --pkg-end --pkg-begin io $(BUN_DIR)/src/io/io_darwin.zig --pkg-end --pkg-begin boringssl $(BUN_DIR)/src/boringssl.zig --pkg-end --pkg-begin thread_pool $(BUN_DIR)/src/thread_pool.zig --pkg-begin io $(BUN_DIR)/src/io/io_darwin.zig --pkg-end --pkg-begin http $(BUN_DIR)/src/http_client_async.zig --pkg-begin strings $(BUN_DIR)/src/string_immutable.zig --pkg-end --pkg-begin picohttp $(BUN_DIR)/src/deps/picohttp.zig --pkg-end --pkg-begin io $(BUN_DIR)/src/io/io_darwin.zig --pkg-end --pkg-begin boringssl $(BUN_DIR)/src/boringssl.zig --pkg-end --pkg-begin thread_pool $(BUN_DIR)/src/thread_pool.zig --pkg-end --pkg-end --pkg-end --pkg-end --pkg-end --pkg-begin picohttp $(BUN_DIR)/src/deps/picohttp.zig --pkg-end --pkg-begin io $(BUN_DIR)/src/io/io_darwin.zig --pkg-end --pkg-begin strings $(BUN_DIR)/src/string_immutable.zig --pkg-end --pkg-begin clap $(BUN_DIR)/src/deps/zig-clap/clap.zig --pkg-end --pkg-begin http $(BUN_DIR)/src/http_client_async.zig --pkg-begin strings $(BUN_DIR)/src/string_immutable.zig --pkg-end --pkg-begin picohttp $(BUN_DIR)/src/deps/picohttp.zig --pkg-end --pkg-begin io $(BUN_DIR)/src/io/io_darwin.zig --pkg-end --pkg-begin boringssl $(BUN_DIR)/src/boringssl.zig --pkg-end --pkg-begin thread_pool $(BUN_DIR)/src/thread_pool.zig --pkg-begin io $(BUN_DIR)/src/io/io_darwin.zig --pkg-end --pkg-begin http $(BUN_DIR)/src/http_client_async.zig --pkg-begin strings $(BUN_DIR)/src/string_immutable.zig --pkg-end --pkg-begin picohttp $(BUN_DIR)/src/deps/picohttp.zig --pkg-end --pkg-begin io $(BUN_DIR)/src/io/io_darwin.zig --pkg-end --pkg-begin boringssl $(BUN_DIR)/src/boringssl.zig --pkg-end --pkg-begin thread_pool $(BUN_DIR)/src/thread_pool.zig --pkg-end --pkg-end --pkg-end --pkg-end --pkg-begin boringssl $(BUN_DIR)/src/boringssl.zig --pkg-end --pkg-begin javascript_core $(BUN_DIR)/src/jsc.zig --pkg-begin http $(BUN_DIR)/src/http_client_async.zig --pkg-end --pkg-begin strings $(BUN_DIR)/src/string_immutable.zig --pkg-end --pkg-begin picohttp $(BUN_DIR)/src/deps/picohttp.zig --pkg-end --pkg-end


bun: vendor identifier-cache build-obj bun-link-lld-release bun-codesign-release-local
9 changes: 8 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const color_map = std.ComptimeStringMap([]const u8, .{
fn addInternalPackages(step: *std.build.LibExeObjStep, _: std.mem.Allocator, target: anytype) !void {
var boringssl: std.build.Pkg = .{
.name = "boringssl",
.path = pkgPath("src/deps/boringssl.zig"),
.path = pkgPath("src/boringssl.zig"),
};

var datetime: std.build.Pkg = .{
Expand Down Expand Up @@ -404,6 +404,13 @@ pub fn build(b: *std.build.Builder) !void {
try configureObjectStep(b, headers_obj, target, obj.main_pkg_path.?);
}

{
const headers_step = b.step("sha-bench-obj", "Build sha bench");
var headers_obj: *std.build.LibExeObjStep = b.addObject("sha", "src/sha.zig");
defer headers_step.dependOn(&headers_obj.step);
try configureObjectStep(b, headers_obj, target, obj.main_pkg_path.?);
}

{
const headers_step = b.step("vlq-bench", "Build vlq bench");
var headers_obj: *std.build.LibExeObjStep = b.addExecutable("vlq-bench", "src/sourcemap/vlq_bench.zig");
Expand Down
28 changes: 28 additions & 0 deletions integration/bunjs-only-snippets/crypto.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { it, expect } from "bun:test";

for (let Hasher of [
Bun.SHA1,
Bun.SHA256,
Bun.SHA384,
Bun.SHA512,
Bun.SHA512_256,
]) {
it(`${Hasher.name} instance`, () => {
var buf = new Uint8Array(256);
const result = new Hasher();
result.update("hello world");
result.final(buf);
});
}

for (let HashFn of [
Bun.sha1,
Bun.sha256,
Bun.sha384,
Bun.sha512,
Bun.sha512_256,
]) {
it(`${HashFn.name} instance`, () => {
HashFn("hello world");
});
}
16 changes: 11 additions & 5 deletions src/deps/boringssl.zig → src/boringssl.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const boring = @import("./boringssl.translated.zig");
const boring = @import("./deps/boringssl.translated.zig");
pub usingnamespace boring;
const std = @import("std");
const global = @import("../global.zig");

const builtin = @import("builtin");
var loaded = false;
pub fn load() void {
if (loaded) return;
Expand Down Expand Up @@ -52,16 +53,19 @@ pub fn initClient() *boring.SSL {
// may result in deadlocks, crashes, or memory corruption.

export fn OPENSSL_memory_alloc(size: usize) ?*anyopaque {
const global = @import("./global.zig");
return global.Global.Mimalloc.mi_malloc(size);
}

// BoringSSL always expects memory to be zero'd
export fn OPENSSL_memory_free(ptr: *anyopaque) void {
const global = @import("./global.zig");
@memset(@ptrCast([*]u8, ptr), 0, global.Global.Mimalloc.mi_usable_size(ptr));
global.Global.Mimalloc.mi_free(ptr);
}

export fn OPENSSL_memory_get_size(ptr: ?*const anyopaque) usize {
const global = @import("./global.zig");
return global.Global.Mimalloc.mi_usable_size(ptr);
}

Expand All @@ -70,7 +74,9 @@ test "load" {
}

comptime {
_ = OPENSSL_memory_alloc;
_ = OPENSSL_memory_free;
_ = OPENSSL_memory_get_size;
if (!builtin.is_test) {
_ = OPENSSL_memory_alloc;
_ = OPENSSL_memory_free;
_ = OPENSSL_memory_get_size;
}
}
2 changes: 1 addition & 1 deletion src/deps/boringssl
Submodule boringssl updated 254 files
56 changes: 28 additions & 28 deletions src/deps/boringssl.translated.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2747,34 +2747,34 @@ pub extern fn RSA_verify_PKCS1_PSS(rsa: ?*const RSA, mHash: [*c]const u8, Hash:
pub extern fn RSA_padding_add_PKCS1_OAEP(to: [*c]u8, to_len: usize, from: [*c]const u8, from_len: usize, param: [*c]const u8, param_len: usize) c_int;
pub extern fn RSA_print(bio: [*c]BIO, rsa: ?*const RSA, indent: c_int) c_int;
pub extern fn RSA_get0_pss_params(rsa: ?*const RSA) [*c]const RSA_PSS_PARAMS;
pub extern fn SHA1_Init(sha: [*c]SHA_CTX) c_int;
pub extern fn SHA1_Update(sha: [*c]SHA_CTX, data: ?*const anyopaque, len: usize) c_int;
pub extern fn SHA1_Final(out: [*c]u8, sha: [*c]SHA_CTX) c_int;
pub extern fn SHA1(data: [*c]const u8, len: usize, out: [*c]u8) [*c]u8;
pub extern fn SHA1_Transform(sha: [*c]SHA_CTX, block: [*c]const u8) void;
pub extern fn SHA224_Init(sha: [*c]SHA256_CTX) c_int;
pub extern fn SHA224_Update(sha: [*c]SHA256_CTX, data: ?*const anyopaque, len: usize) c_int;
pub extern fn SHA224_Final(out: [*c]u8, sha: [*c]SHA256_CTX) c_int;
pub extern fn SHA224(data: [*c]const u8, len: usize, out: [*c]u8) [*c]u8;
pub extern fn SHA256_Init(sha: [*c]SHA256_CTX) c_int;
pub extern fn SHA256_Update(sha: [*c]SHA256_CTX, data: ?*const anyopaque, len: usize) c_int;
pub extern fn SHA256_Final(out: [*c]u8, sha: [*c]SHA256_CTX) c_int;
pub extern fn SHA256(data: [*c]const u8, len: usize, out: [*c]u8) [*c]u8;
pub extern fn SHA256_Transform(sha: [*c]SHA256_CTX, block: [*c]const u8) void;
pub extern fn SHA256_TransformBlocks(state: [*c]u32, data: [*c]const u8, num_blocks: usize) void;
pub extern fn SHA384_Init(sha: [*c]SHA512_CTX) c_int;
pub extern fn SHA384_Update(sha: [*c]SHA512_CTX, data: ?*const anyopaque, len: usize) c_int;
pub extern fn SHA384_Final(out: [*c]u8, sha: [*c]SHA512_CTX) c_int;
pub extern fn SHA384(data: [*c]const u8, len: usize, out: [*c]u8) [*c]u8;
pub extern fn SHA512_Init(sha: [*c]SHA512_CTX) c_int;
pub extern fn SHA512_Update(sha: [*c]SHA512_CTX, data: ?*const anyopaque, len: usize) c_int;
pub extern fn SHA512_Final(out: [*c]u8, sha: [*c]SHA512_CTX) c_int;
pub extern fn SHA512(data: [*c]const u8, len: usize, out: [*c]u8) [*c]u8;
pub extern fn SHA512_Transform(sha: [*c]SHA512_CTX, block: [*c]const u8) void;
pub extern fn SHA512_256_Init(sha: [*c]SHA512_CTX) c_int;
pub extern fn SHA512_256_Update(sha: [*c]SHA512_CTX, data: ?*const anyopaque, len: usize) c_int;
pub extern fn SHA512_256_Final(out: [*c]u8, sha: [*c]SHA512_CTX) c_int;
pub extern fn SHA512_256(data: [*c]const u8, len: usize, out: [*c]u8) [*c]u8;
pub extern fn SHA1_Init(sha: *SHA_CTX) c_int;
pub extern fn SHA1_Update(sha: *SHA_CTX, data: ?*const anyopaque, len: usize) c_int;
pub extern fn SHA1_Final(out: [*]u8, sha: *SHA_CTX) c_int;
pub extern fn SHA1(data: [*]const u8, len: usize, out: [*]u8) [*]u8;
pub extern fn SHA1_Transform(sha: *SHA_CTX, block: [*]const u8) void;
pub extern fn SHA224_Init(sha: *SHA256_CTX) c_int;
pub extern fn SHA224_Update(sha: *SHA256_CTX, data: ?*const anyopaque, len: usize) c_int;
pub extern fn SHA224_Final(out: [*]u8, sha: *SHA256_CTX) c_int;
pub extern fn SHA224(data: [*]const u8, len: usize, out: [*]u8) [*]u8;
pub extern fn SHA256_Init(sha: *SHA256_CTX) c_int;
pub extern fn SHA256_Update(sha: *SHA256_CTX, data: ?*const anyopaque, len: usize) c_int;
pub extern fn SHA256_Final(out: [*]u8, sha: *SHA256_CTX) c_int;
pub extern fn SHA256(data: [*]const u8, len: usize, out: [*]u8) [*]u8;
pub extern fn SHA256_Transform(sha: *SHA256_CTX, block: [*]const u8) void;
pub extern fn SHA256_TransformBlocks(state: [*c]u32, data: [*]const u8, num_blocks: usize) void;
pub extern fn SHA384_Init(sha: *SHA512_CTX) c_int;
pub extern fn SHA384_Update(sha: *SHA512_CTX, data: ?*const anyopaque, len: usize) c_int;
pub extern fn SHA384_Final(out: [*]u8, sha: *SHA512_CTX) c_int;
pub extern fn SHA384(data: [*]const u8, len: usize, out: [*]u8) [*]u8;
pub extern fn SHA512_Init(sha: *SHA512_CTX) c_int;
pub extern fn SHA512_Update(sha: *SHA512_CTX, data: ?*const anyopaque, len: usize) c_int;
pub extern fn SHA512_Final(out: [*]u8, sha: *SHA512_CTX) c_int;
pub extern fn SHA512(data: [*]const u8, len: usize, out: [*]u8) [*]u8;
pub extern fn SHA512_Transform(sha: *SHA512_CTX, block: [*]const u8) void;
pub extern fn SHA512_256_Init(sha: *SHA512_CTX) c_int;
pub extern fn SHA512_256_Update(sha: *SHA512_CTX, data: ?*const anyopaque, len: usize) c_int;
pub extern fn SHA512_256_Final(out: [*]u8, sha: *SHA512_CTX) c_int;
pub extern fn SHA512_256(data: [*]const u8, len: usize, out: [*]u8) [*]u8;
pub extern fn X509_ALGOR_new() [*c]X509_ALGOR;
pub extern fn X509_ALGOR_free(a: [*c]X509_ALGOR) void;
pub extern fn d2i_X509_ALGOR(a: [*c][*c]X509_ALGOR, in: [*c][*c]const u8, len: c_long) [*c]X509_ALGOR;
Expand Down
2 changes: 1 addition & 1 deletion src/fallback.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3c32b2da4ba87f18
871e1d1d6a2e7805
52 changes: 50 additions & 2 deletions src/io/io_darwin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,10 @@ timeouts: FIFO(Completion) = .{},
completed: FIFO(Completion) = .{},
io_pending: FIFO(Completion) = .{},
last_event_fd: std.atomic.Atomic(u32) = std.atomic.Atomic(u32).init(32),
pending_count: usize = 0,

pub fn hasNoWork(this: *IO) bool {
return this.io_inflight == 0 and this.io_pending.peek() == null and this.completed.peek() == null and this.timeouts.peek() == null;
return this.pending_count == 0 and this.io_inflight == 0 and this.io_pending.peek() == null and this.completed.peek() == null and this.timeouts.peek() == null;
}

pub fn init(_: u12, _: u32) !IO {
Expand Down Expand Up @@ -523,7 +524,7 @@ pub fn run_for_ns(self: *IO, nanoseconds: u63) !void {
}.callback;

// Submit a timeout which sets the timed_out value to true to terminate the loop below.
self.timeout(
self.timeoutInternal(
*bool,
&timed_out,
on_timeout,
Expand Down Expand Up @@ -756,6 +757,21 @@ fn submit(
operation_data: anytype,
comptime OperationImpl: type,
) void {
submitWithIncrementPending(self, context, callback, completion, operation_tag, operation_data, OperationImpl, true);
}

fn submitWithIncrementPending(
self: *IO,
context: anytype,
comptime callback: anytype,
completion: *Completion,
comptime operation_tag: std.meta.Tag(Operation),
operation_data: anytype,
comptime OperationImpl: type,
comptime increment_pending: bool,
) void {
if (comptime increment_pending)
self.pending_count += 1;
const Context = @TypeOf(context);
const onCompleteFn = struct {
fn onComplete(io: *IO, _completion: *Completion) void {
Expand All @@ -778,6 +794,9 @@ fn submit(
else => {},
}

if (comptime increment_pending)
io.pending_count -= 1;

// Complete the Completion
return callback(
@intToPtr(Context, @ptrToInt(_completion.context)),
Expand Down Expand Up @@ -1334,6 +1353,35 @@ pub fn timeout(
);
}

fn timeoutInternal(
self: *IO,
comptime Context: type,
context: Context,
comptime callback: fn (
context: Context,
completion: *Completion,
result: TimeoutError!void,
) void,
completion: *Completion,
nanoseconds: u63,
) void {
self.submitWithIncrementPending(
context,
callback,
completion,
.timeout,
.{
.expires = self.time.monotonic() + nanoseconds,
},
struct {
fn doOperation(_: anytype) TimeoutError!void {
return; // timeouts don't have errors for now
}
},
false,
);
}

pub const WriteError = os.PWriteError;

pub fn write(
Expand Down
Loading

0 comments on commit f6d73cb

Please sign in to comment.