From 5da14042f70711ea5cf66e034699730335462f66 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 5 May 2024 14:08:03 +0200 Subject: Merging upstream version 1.45.3+dfsg. Signed-off-by: Daniel Baumann --- .../mrbgems/mruby-bin-strip/bintest/mruby-strip.rb | 73 ++++++++++ .../deps/mruby/mrbgems/mruby-bin-strip/mrbgem.rake | 6 + .../tools/mruby-strip/mruby-strip.c | 155 +++++++++++++++++++++ 3 files changed, 234 insertions(+) create mode 100644 src/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-bin-strip/bintest/mruby-strip.rb create mode 100644 src/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-bin-strip/mrbgem.rake create mode 100644 src/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c (limited to 'src/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-bin-strip') diff --git a/src/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-bin-strip/bintest/mruby-strip.rb b/src/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-bin-strip/bintest/mruby-strip.rb new file mode 100644 index 000000000..bb664a2b1 --- /dev/null +++ b/src/web/server/h2o/libh2o/deps/mruby/mrbgems/mruby-bin-strip/bintest/mruby-strip.rb @@ -0,0 +1,73 @@ +require 'tempfile' + +assert('no files') do + o = `#{cmd('mruby-strip')} 2>&1` + assert_equal 1, $?.exitstatus + assert_equal "no files to strip", o.split("\n")[0] +end + +assert('file not found') do + o = `#{cmd('mruby-strip')} not_found.mrb 2>&1` + assert_equal 1, $?.exitstatus + assert_equal "can't open file for reading not_found.mrb\n", o +end + +assert('not irep file') do + t = Tempfile.new('script.rb') + t.write 'p test\n' + t.flush + o = `#{cmd('mruby-strip')} #{t.path} 2>&1` + assert_equal 1, $?.exitstatus + assert_equal "can't read irep file #{t.path}\n", o +end + +assert('success') do + script_file, compiled1, compiled2 = + Tempfile.new('script.rb'), Tempfile.new('c1.mrb'), Tempfile.new('c2.mrb') + script_file.write "p 'test'\n" + script_file.flush + `#{cmd('mrbc')} -g -o #{compiled1.path} #{script_file.path}` + `#{cmd('mrbc')} -g -o #{compiled2.path} #{script_file.path}` + + o = `#{cmd('mruby-strip')} #{compiled1.path}` + assert_equal 0, $?.exitstatus + assert_equal "", o + assert_equal `#{cmd('mruby')} #{script_file.path}`, `#{cmd('mruby')} -b #{compiled1.path}` + + o = `#{cmd('mruby-strip')} #{compiled1.path} #{compiled2.path}` + assert_equal 0, $?.exitstatus + assert_equal "", o +end + +assert('check debug section') do + script_file, with_debug, without_debug = + Tempfile.new('script.rb'), Tempfile.new('c1.mrb'), Tempfile.new('c2.mrb') + script_file.write "p 'test'\n" + script_file.flush + `#{cmd('mrbc')} -o #{without_debug.path} #{script_file.path}` + `#{cmd('mrbc')} -g -o #{with_debug.path} #{script_file.path}` + + assert_true with_debug.size >= without_debug.size + + `#{cmd('mruby-strip')} #{with_debug.path}` + assert_equal without_debug.size, with_debug.size +end + +assert('check lv section') do + script_file, with_lv, without_lv = + Tempfile.new('script.rb'), Tempfile.new('c1.mrb'), Tempfile.new('c2.mrb') + script_file.write < +#include +#include +#include +#include +#include + +struct strip_args { + int argc_start; + int argc; + char **argv; + mrb_bool lvar; +}; + + +static void +irep_remove_lv(mrb_state *mrb, mrb_irep *irep) +{ + int i; + + if (irep->lv) { + mrb_free(mrb, irep->lv); + irep->lv = NULL; + } + + for (i = 0; i < irep->rlen; ++i) { + irep_remove_lv(mrb, irep->reps[i]); + } +} + +static void +print_usage(const char *f) +{ + printf("Usage: %s [switches] irepfiles\n", f); + printf("switches:\n"); + printf(" -l, --lvar remove LVAR section too.\n"); +} + +static int +parse_args(int argc, char **argv, struct strip_args *args) +{ + int i; + + args->argc_start = 0; + args->argc = argc; + args->argv = argv; + args->lvar = FALSE; + + for (i = 1; i < argc; ++i) { + const size_t len = strlen(argv[i]); + if (len >= 2 && argv[i][0] == '-') { + switch (argv[i][1]) { + case 'l': + args->lvar = TRUE; + break; + case '-': + if (strncmp((*argv) + 2, "lvar", len) == 0) { + args->lvar = TRUE; + break; + } + default: + return -1; + } + } + else { + break; + } + } + + args->argc_start = i; + return i; +} + +static int +strip(mrb_state *mrb, struct strip_args *args) +{ + int i; + + for (i = args->argc_start; i < args->argc; ++i) { + char *filename; + FILE *rfile; + mrb_irep *irep; + FILE *wfile; + int dump_result; + + filename = args->argv[i]; + rfile = fopen(filename, "rb"); + if (rfile == NULL) { + fprintf(stderr, "can't open file for reading %s\n", filename); + return EXIT_FAILURE; + } + + irep = mrb_read_irep_file(mrb, rfile); + fclose(rfile); + if (irep == NULL) { + fprintf(stderr, "can't read irep file %s\n", filename); + return EXIT_FAILURE; + } + + /* clear lv if --lvar is enabled */ + if (args->lvar) { + irep_remove_lv(mrb, irep); + } + + wfile = fopen(filename, "wb"); + if (wfile == NULL) { + fprintf(stderr, "can't open file for writing %s\n", filename); + mrb_irep_decref(mrb, irep); + return EXIT_FAILURE; + } + + /* debug flag must always be false */ + dump_result = mrb_dump_irep_binary(mrb, irep, FALSE, wfile); + + fclose(wfile); + mrb_irep_decref(mrb, irep); + + if (dump_result != MRB_DUMP_OK) { + fprintf(stderr, "error occurred during dumping %s\n", filename); + return EXIT_FAILURE; + } + } + return EXIT_SUCCESS; +} + +int +main(int argc, char **argv) +{ + struct strip_args args; + int args_result; + mrb_state *mrb; + int ret; + + if (argc <= 1) { + printf("no files to strip\n"); + print_usage(argv[0]); + return EXIT_FAILURE; + } + + args_result = parse_args(argc, argv, &args); + if (args_result < 0) { + print_usage(argv[0]); + return EXIT_FAILURE; + } + mrb = mrb_open_core(mrb_default_allocf, NULL); + if (mrb == NULL) { + fputs("Invalid mrb_state, exiting mruby-strip\n", stderr); + return EXIT_FAILURE; + } + + ret = strip(mrb, &args); + + mrb_close(mrb); + return ret; +} -- cgit v1.2.3