bootsnap 1.4.0 → 1.4.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +50 -0
  3. data/README.md +28 -4
  4. data/ext/bootsnap/bootsnap.c +122 -65
  5. data/ext/bootsnap/extconf.rb +1 -0
  6. data/lib/bootsnap/bundler.rb +1 -0
  7. data/lib/bootsnap/compile_cache/iseq.rb +8 -4
  8. data/lib/bootsnap/compile_cache/yaml.rb +74 -41
  9. data/lib/bootsnap/compile_cache.rb +15 -2
  10. data/lib/bootsnap/explicit_require.rb +1 -0
  11. data/lib/bootsnap/load_path_cache/cache.rb +25 -7
  12. data/lib/bootsnap/load_path_cache/change_observer.rb +3 -1
  13. data/lib/bootsnap/load_path_cache/core_ext/active_support.rb +14 -7
  14. data/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb +28 -5
  15. data/lib/bootsnap/load_path_cache/core_ext/loaded_features.rb +11 -0
  16. data/lib/bootsnap/load_path_cache/loaded_features_index.rb +42 -10
  17. data/lib/bootsnap/load_path_cache/path.rb +3 -2
  18. data/lib/bootsnap/load_path_cache/path_scanner.rb +42 -27
  19. data/lib/bootsnap/load_path_cache/realpath_cache.rb +5 -5
  20. data/lib/bootsnap/load_path_cache/store.rb +16 -10
  21. data/lib/bootsnap/load_path_cache.rb +8 -1
  22. data/lib/bootsnap/setup.rb +5 -1
  23. data/lib/bootsnap/version.rb +2 -1
  24. data/lib/bootsnap.rb +2 -0
  25. metadata +9 -24
  26. data/.gitignore +0 -17
  27. data/.rubocop.yml +0 -20
  28. data/.travis.yml +0 -24
  29. data/CODE_OF_CONDUCT.md +0 -74
  30. data/CONTRIBUTING.md +0 -21
  31. data/Gemfile +0 -8
  32. data/README.jp.md +0 -231
  33. data/Rakefile +0 -12
  34. data/bin/ci +0 -10
  35. data/bin/console +0 -14
  36. data/bin/setup +0 -8
  37. data/bin/test-minimal-support +0 -7
  38. data/bin/testunit +0 -8
  39. data/bootsnap.gemspec +0 -45
  40. data/dev.yml +0 -10
  41. data/shipit.rubygems.yml +0 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40694ca2512ff7861584a4f9cee05e424c3d9319f3b36c7f4f45f9196d2d9f19
4
- data.tar.gz: '087c4f2dafdd51a6a3d0a69d31aadabfe6ef90bf2ab12b861f2a288d7b16c04a'
3
+ metadata.gz: 76fd14d1b3f41092e29332553097e0642c3744870220459235a3cd572c4a3b7a
4
+ data.tar.gz: 8551da0ba4cef42ab3a2ee721a751f8113018315596b7dd8c54539c0247d6165
5
5
  SHA512:
6
- metadata.gz: ecbabde06c5f871702a000128e4dbc15d60fe0e16ab7adb4a8bb40b1c3b5b5f54b39c6927dc5aea9e49242d7e7f5deb596c42c44f8c2b8f998f835ec5b058b47
7
- data.tar.gz: d566ffe7c3048f57c3c4fcd925d5e8ff5a722fc2622628e26571361e7a0fbdb5cbe8c724d45f2fc8ba09d0de614c5fad87f1875ecc99a08a34aab3afa7665fe1
6
+ metadata.gz: 9491f0fd3650765fd49b8a3122abafb59ed387f9f34da52a902cf3f1c0542637f8ca744b2015ec1ab1b5117b37e548b8091e332166e1738e59b8b91c4578aaf3
7
+ data.tar.gz: ab23188bcb8af81af0bf51cb091fa420fb2fa8e19158a022a67d6ccd856120a12438949b57dfc3b1394f00153e22fec8fc9331531fc4ae99f205d1a8d5305291
data/CHANGELOG.md CHANGED
@@ -1,3 +1,53 @@
1
+ # 1.4.9
2
+
3
+ * [Windows support](https://github.com/Shopify/bootsnap/pull/319)
4
+ * [Fix potential crash](https://github.com/Shopify/bootsnap/pull/322)
5
+
6
+ # 1.4.8
7
+
8
+ * [Prevent FallbackScan from polluting exception cause](https://github.com/Shopify/bootsnap/pull/314)
9
+
10
+ # 1.4.7
11
+
12
+ * Various performance enhancements
13
+ * Fix race condition in heavy concurrent load scenarios that would cause bootsnap to raise
14
+
15
+ # 1.4.6
16
+
17
+ * Fix bug that was erroneously considering that files containing `.` in the names were being
18
+ required if a different file with the same name was already being required
19
+
20
+ Example:
21
+
22
+ require 'foo'
23
+ require 'foo.en'
24
+
25
+ Before bootsnap was considering `foo.en` to be the same file as `foo`
26
+
27
+ * Use glibc as part of the ruby_platform cache key
28
+
29
+ # 1.4.5
30
+
31
+ * MRI 2.7 support
32
+ * Fixed concurrency bugs
33
+
34
+ # 1.4.4
35
+
36
+ * Disable ISeq cache in `bootsnap/setup` by default in Ruby 2.5
37
+
38
+ # 1.4.3
39
+
40
+ * Fix some cache permissions and umask issues after switch to mkstemp
41
+
42
+ # 1.4.2
43
+
44
+ * Fix bug when removing features loaded by relative path from `$LOADED_FEATURES`
45
+ * Fix bug with propagation of `NameError` up from nested calls to `require`
46
+
47
+ # 1.4.1
48
+
49
+ * Don't register change observers to frozen objects.
50
+
1
51
  # 1.4.0
2
52
 
3
53
  * When running in development mode, always fall back to a full path scan on LoadError, making
data/README.md CHANGED
@@ -4,9 +4,14 @@ Bootsnap is a library that plugs into Ruby, with optional support for `ActiveSup
4
4
  to optimize and cache expensive computations. See [How Does This Work](#how-does-this-work).
5
5
 
6
6
  #### Performance
7
- - [Discourse](https://github.com/discourse/discourse) reports a boot time reduction of approximately 50%, from roughly 6 to 3 seconds on one machine;
7
+
8
+ - [Discourse](https://github.com/discourse/discourse) reports a boot time reduction of approximately
9
+ 50%, from roughly 6 to 3 seconds on one machine;
8
10
  - One of our smaller internal apps also sees a reduction of 50%, from 3.6 to 1.8 seconds;
9
- - The core Shopify platform -- a rather large monolithic application -- boots about 75% faster, dropping from around 25s to 6.5s.
11
+ - The core Shopify platform -- a rather large monolithic application -- boots about 75% faster,
12
+ dropping from around 25s to 6.5s.
13
+ * In Shopify core (a large app), about 25% of this gain can be attributed to `compile_cache_*`
14
+ features; 75% to path caching, and ~1% to `disable_trace`. This is fairly representative.
10
15
 
11
16
  ## Usage
12
17
 
@@ -24,6 +29,14 @@ If you are using Rails, add this to `config/boot.rb` immediately after `require
24
29
  require 'bootsnap/setup'
25
30
  ```
26
31
 
32
+ Note that bootsnap writes to `tmp/cache`, and that directory *must* be writable. Rails will fail to
33
+ boot if it is not. If this is unacceptable (e.g. you are running in a read-only container and
34
+ unwilling to mount in a writable tmpdir), you should remove this line or wrap it in a conditional.
35
+
36
+ **Note also that bootsnap will never clean up its own cache: this is left up to you. Depending on your
37
+ deployment strategy, you may need to periodically purge `tmp/cache/bootsnap*`. If you notice deploys
38
+ getting progressively slower, this is almost certainly the cause.**
39
+
27
40
  It's technically possible to simply specify `gem 'bootsnap', require: 'bootsnap/setup'`, but it's
28
41
  important to load Bootsnap as early as possible to get maximum performance improvement.
29
42
 
@@ -41,12 +54,14 @@ Bootsnap.setup(
41
54
  development_mode: env == 'development', # Current working environment, e.g. RACK_ENV, RAILS_ENV, etc
42
55
  load_path_cache: true, # Optimize the LOAD_PATH with a cache
43
56
  autoload_paths_cache: true, # Optimize ActiveSupport autoloads with cache
44
- disable_trace: true, # (Alpha) Set `RubyVM::InstructionSequence.compile_option = { trace_instruction: false }`
57
+ disable_trace: true, # Set `RubyVM::InstructionSequence.compile_option = { trace_instruction: false }`
45
58
  compile_cache_iseq: true, # Compile Ruby code into ISeq cache, breaks coverage reporting.
46
59
  compile_cache_yaml: true # Compile YAML into a cache
47
60
  )
48
61
  ```
49
62
 
63
+ **Note that `disable_trace` will break debuggers and tracing.**
64
+
50
65
  **Protip:** You can replace `require 'bootsnap'` with `BootLib::Require.from_gem('bootsnap',
51
66
  'bootsnap')` using [this trick](https://github.com/Shopify/bootsnap/wiki/Bootlib::Require). This
52
67
  will help optimize boot time further if you have an extremely large `$LOAD_PATH`.
@@ -199,7 +214,7 @@ Bootsnap writes a cache file containing a 64 byte header followed by the cache c
199
214
  is a cache key including several fields:
200
215
 
201
216
  * `version`, hardcoded in bootsnap. Essentially a schema version;
202
- * `os_version`, A hash of the current kernel version (on macOS, BSD) or glibc version (on Linux);
217
+ * `ruby_platform`, A hash of `RUBY_PLATFORM` (e.g. x86_64-linux-gnu) variable and glibc version (on Linux) or OS version (`uname -v` on BSD, macOS)
203
218
  * `compile_option`, which changes with `RubyVM::InstructionSequence.compile_option` does;
204
219
  * `ruby_revision`, the version of Ruby this was compiled with;
205
220
  * `size`, the size of the source file;
@@ -278,3 +293,12 @@ open /c/nope.bundle -> -1
278
293
  ```
279
294
  # (nothing!)
280
295
  ```
296
+
297
+ ## When not to use Bootsnap
298
+
299
+ *Alternative engines*: Bootsnap is pretty reliant on MRI features, and parts are disabled entirely on alternative ruby
300
+ engines.
301
+
302
+ *Non-local filesystems*: Bootsnap depends on `tmp/cache` (or whatever you set its cache directory
303
+ to) being on a relatively fast filesystem. If you put it on a network mount, bootsnap is very likely
304
+ to slow your application down quite a lot.
@@ -21,6 +21,9 @@
21
21
  #ifndef _WIN32
22
22
  #include <sys/utsname.h>
23
23
  #endif
24
+ #ifdef __GLIBC__
25
+ #include <gnu/libc-version.h>
26
+ #endif
24
27
 
25
28
  /* 1000 is an arbitrary limit; FNV64 plus some slashes brings the cap down to
26
29
  * 981 for the cache dir */
@@ -29,6 +32,8 @@
29
32
 
30
33
  #define KEY_SIZE 64
31
34
 
35
+ #define MAX_CREATE_TEMPFILE_ATTEMPT 3
36
+
32
37
  /*
33
38
  * An instance of this key is written as the first 64 bytes of each cache file.
34
39
  * The mtime and size members track whether the file contents have changed, and
@@ -74,6 +79,8 @@ static uint32_t current_ruby_platform;
74
79
  static uint32_t current_ruby_revision;
75
80
  /* Invalidates cache when RubyVM::InstructionSequence.compile_option changes */
76
81
  static uint32_t current_compile_option_crc32 = 0;
82
+ /* Current umask */
83
+ static mode_t current_umask;
77
84
 
78
85
  /* Bootsnap::CompileCache::{Native, Uncompilable} */
79
86
  static VALUE rb_mBootsnap;
@@ -84,28 +91,29 @@ static ID uncompilable;
84
91
 
85
92
  /* Functions exposed as module functions on Bootsnap::CompileCache::Native */
86
93
  static VALUE bs_compile_option_crc32_set(VALUE self, VALUE crc32_v);
87
- static VALUE bs_rb_fetch(VALUE self, VALUE cachedir_v, VALUE path_v, VALUE handler);
94
+ static VALUE bs_rb_fetch(VALUE self, VALUE cachedir_v, VALUE path_v, VALUE handler, VALUE args);
88
95
 
89
96
  /* Helpers */
90
97
  static uint64_t fnv1a_64(const char *str);
91
- static void bs_cache_path(const char * cachedir, const char * path, char ** cache_path);
98
+ static void bs_cache_path(const char * cachedir, const char * path, const char * extra, char (* cache_path)[MAX_CACHEPATH_SIZE]);
92
99
  static int bs_read_key(int fd, struct bs_cache_key * key);
93
100
  static int cache_key_equal(struct bs_cache_key * k1, struct bs_cache_key * k2);
94
- static VALUE bs_fetch(char * path, VALUE path_v, char * cache_path, VALUE handler);
95
- static int open_current_file(char * path, struct bs_cache_key * key, char ** errno_provenance);
96
- static int fetch_cached_data(int fd, ssize_t data_size, VALUE handler, VALUE * output_data, int * exception_tag, char ** errno_provenance);
101
+ static VALUE bs_fetch(char * path, VALUE path_v, char * cache_path, VALUE handler, VALUE args);
102
+ static int open_current_file(char * path, struct bs_cache_key * key, const char ** errno_provenance);
103
+ static int fetch_cached_data(int fd, ssize_t data_size, VALUE handler, VALUE args, VALUE * output_data, int * exception_tag, const char ** errno_provenance);
104
+ static uint32_t get_ruby_revision(void);
97
105
  static uint32_t get_ruby_platform(void);
98
106
 
99
107
  /*
100
108
  * Helper functions to call ruby methods on handler object without crashing on
101
109
  * exception.
102
110
  */
103
- static int bs_storage_to_output(VALUE handler, VALUE storage_data, VALUE * output_data);
111
+ static int bs_storage_to_output(VALUE handler, VALUE args, VALUE storage_data, VALUE * output_data);
104
112
  static VALUE prot_storage_to_output(VALUE arg);
105
113
  static VALUE prot_input_to_output(VALUE arg);
106
- static void bs_input_to_output(VALUE handler, VALUE input_data, VALUE * output_data, int * exception_tag);
114
+ static void bs_input_to_output(VALUE handler, VALUE args, VALUE input_data, VALUE * output_data, int * exception_tag);
107
115
  static VALUE prot_input_to_storage(VALUE arg);
108
- static int bs_input_to_storage(VALUE handler, VALUE input_data, VALUE pathval, VALUE * storage_data);
116
+ static int bs_input_to_storage(VALUE handler, VALUE args, VALUE input_data, VALUE pathval, VALUE * storage_data);
109
117
  struct s2o_data;
110
118
  struct i2o_data;
111
119
  struct i2s_data;
@@ -134,14 +142,17 @@ Init_bootsnap(void)
134
142
  rb_mBootsnap_CompileCache_Native = rb_define_module_under(rb_mBootsnap_CompileCache, "Native");
135
143
  rb_eBootsnap_CompileCache_Uncompilable = rb_define_class_under(rb_mBootsnap_CompileCache, "Uncompilable", rb_eStandardError);
136
144
 
137
- current_ruby_revision = FIX2INT(rb_const_get(rb_cObject, rb_intern("RUBY_REVISION")));
145
+ current_ruby_revision = get_ruby_revision();
138
146
  current_ruby_platform = get_ruby_platform();
139
147
 
140
148
  uncompilable = rb_intern("__bootsnap_uncompilable__");
141
149
 
142
150
  rb_define_module_function(rb_mBootsnap_CompileCache_Native, "coverage_running?", bs_rb_coverage_running, 0);
143
- rb_define_module_function(rb_mBootsnap_CompileCache_Native, "fetch", bs_rb_fetch, 3);
151
+ rb_define_module_function(rb_mBootsnap_CompileCache_Native, "fetch", bs_rb_fetch, 4);
144
152
  rb_define_module_function(rb_mBootsnap_CompileCache_Native, "compile_option_crc32=", bs_compile_option_crc32_set, 1);
153
+
154
+ current_umask = umask(0777);
155
+ umask(current_umask);
145
156
  }
146
157
 
147
158
  /*
@@ -191,6 +202,26 @@ fnv1a_64(const char *str)
191
202
  return fnv1a_64_iter(h, str);
192
203
  }
193
204
 
205
+ /*
206
+ * Ruby's revision may be Integer or String. CRuby 2.7 or later uses
207
+ * Git commit ID as revision. It's String.
208
+ */
209
+ static uint32_t
210
+ get_ruby_revision(void)
211
+ {
212
+ VALUE ruby_revision;
213
+
214
+ ruby_revision = rb_const_get(rb_cObject, rb_intern("RUBY_REVISION"));
215
+ if (RB_TYPE_P(ruby_revision, RUBY_T_FIXNUM)) {
216
+ return FIX2INT(ruby_revision);
217
+ } else {
218
+ uint64_t hash;
219
+
220
+ hash = fnv1a_64(StringValueCStr(ruby_revision));
221
+ return (uint32_t)(hash >> 32);
222
+ }
223
+ }
224
+
194
225
  /*
195
226
  * When ruby's version doesn't change, but it's recompiled on a different OS
196
227
  * (or OS version), we need to invalidate the cache.
@@ -210,6 +241,9 @@ get_ruby_platform(void)
210
241
 
211
242
  #ifdef _WIN32
212
243
  return (uint32_t)(hash >> 32) ^ (uint32_t)GetVersion();
244
+ #elif defined(__GLIBC__)
245
+ hash = fnv1a_64_iter(hash, gnu_get_libc_version());
246
+ return (uint32_t)(hash >> 32);
213
247
  #else
214
248
  struct utsname utsname;
215
249
 
@@ -230,9 +264,12 @@ get_ruby_platform(void)
230
264
  * The path will look something like: <cachedir>/12/34567890abcdef
231
265
  */
232
266
  static void
233
- bs_cache_path(const char * cachedir, const char * path, char ** cache_path)
267
+ bs_cache_path(const char * cachedir, const char * path, const char * extra, char (* cache_path)[MAX_CACHEPATH_SIZE])
234
268
  {
235
269
  uint64_t hash = fnv1a_64(path);
270
+ if (extra) {
271
+ hash ^= fnv1a_64(extra);
272
+ }
236
273
 
237
274
  uint8_t first_byte = (hash >> (64 - 8));
238
275
  uint64_t remainder = hash & 0x00ffffffffffffff;
@@ -267,7 +304,7 @@ cache_key_equal(struct bs_cache_key * k1, struct bs_cache_key * k2)
267
304
  * conversions on the ruby VALUE arguments before passing them along.
268
305
  */
269
306
  static VALUE
270
- bs_rb_fetch(VALUE self, VALUE cachedir_v, VALUE path_v, VALUE handler)
307
+ bs_rb_fetch(VALUE self, VALUE cachedir_v, VALUE path_v, VALUE handler, VALUE args)
271
308
  {
272
309
  FilePathValue(path_v);
273
310
 
@@ -281,13 +318,16 @@ bs_rb_fetch(VALUE self, VALUE cachedir_v, VALUE path_v, VALUE handler)
281
318
  char * cachedir = RSTRING_PTR(cachedir_v);
282
319
  char * path = RSTRING_PTR(path_v);
283
320
  char cache_path[MAX_CACHEPATH_SIZE];
284
-
285
- { /* generate cache path to cache_path */
286
- char * tmp = (char *)&cache_path;
287
- bs_cache_path(cachedir, path, &tmp);
321
+ char * extra = NULL;
322
+ if (!NIL_P(args)) {
323
+ VALUE args_serial = rb_marshal_dump(args, Qnil);
324
+ extra = RSTRING_PTR(args_serial);
288
325
  }
289
326
 
290
- return bs_fetch(path, path_v, cache_path, handler);
327
+ /* generate cache path to cache_path */
328
+ bs_cache_path(cachedir, path, extra, &cache_path);
329
+
330
+ return bs_fetch(path, path_v, cache_path, handler, args);
291
331
  }
292
332
 
293
333
  /*
@@ -295,14 +335,14 @@ bs_rb_fetch(VALUE self, VALUE cachedir_v, VALUE path_v, VALUE handler)
295
335
  * was loaded.
296
336
  */
297
337
  static int
298
- open_current_file(char * path, struct bs_cache_key * key, char ** errno_provenance)
338
+ open_current_file(char * path, struct bs_cache_key * key, const char ** errno_provenance)
299
339
  {
300
340
  struct stat statbuf;
301
341
  int fd;
302
342
 
303
343
  fd = open(path, O_RDONLY);
304
344
  if (fd < 0) {
305
- *errno_provenance = (char *)"bs_fetch:open_current_file:open";
345
+ *errno_provenance = "bs_fetch:open_current_file:open";
306
346
  return fd;
307
347
  }
308
348
  #ifdef _WIN32
@@ -310,7 +350,7 @@ open_current_file(char * path, struct bs_cache_key * key, char ** errno_provenan
310
350
  #endif
311
351
 
312
352
  if (fstat(fd, &statbuf) < 0) {
313
- *errno_provenance = (char *)"bs_fetch:open_current_file:fstat";
353
+ *errno_provenance = "bs_fetch:open_current_file:fstat";
314
354
  close(fd);
315
355
  return -1;
316
356
  }
@@ -356,13 +396,13 @@ bs_read_key(int fd, struct bs_cache_key * key)
356
396
  * - ERROR_WITH_ERRNO (-1, errno is set)
357
397
  */
358
398
  static int
359
- open_cache_file(const char * path, struct bs_cache_key * key, char ** errno_provenance)
399
+ open_cache_file(const char * path, struct bs_cache_key * key, const char ** errno_provenance)
360
400
  {
361
401
  int fd, res;
362
402
 
363
403
  fd = open(path, O_RDONLY);
364
404
  if (fd < 0) {
365
- *errno_provenance = (char *)"bs_fetch:open_cache_file:open";
405
+ *errno_provenance = "bs_fetch:open_cache_file:open";
366
406
  if (errno == ENOENT) return CACHE_MISSING_OR_INVALID;
367
407
  return ERROR_WITH_ERRNO;
368
408
  }
@@ -372,7 +412,7 @@ open_cache_file(const char * path, struct bs_cache_key * key, char ** errno_prov
372
412
 
373
413
  res = bs_read_key(fd, key);
374
414
  if (res < 0) {
375
- *errno_provenance = (char *)"bs_fetch:open_cache_file:read";
415
+ *errno_provenance = "bs_fetch:open_cache_file:read";
376
416
  close(fd);
377
417
  return res;
378
418
  }
@@ -396,7 +436,7 @@ open_cache_file(const char * path, struct bs_cache_key * key, char ** errno_prov
396
436
  * or exception, will be the final data returnable to the user.
397
437
  */
398
438
  static int
399
- fetch_cached_data(int fd, ssize_t data_size, VALUE handler, VALUE * output_data, int * exception_tag, char ** errno_provenance)
439
+ fetch_cached_data(int fd, ssize_t data_size, VALUE handler, VALUE args, VALUE * output_data, int * exception_tag, const char ** errno_provenance)
400
440
  {
401
441
  char * data = NULL;
402
442
  ssize_t nread;
@@ -405,7 +445,7 @@ fetch_cached_data(int fd, ssize_t data_size, VALUE handler, VALUE * output_data,
405
445
  VALUE storage_data;
406
446
 
407
447
  if (data_size > 100000000000) {
408
- *errno_provenance = (char *)"bs_fetch:fetch_cached_data:datasize";
448
+ *errno_provenance = "bs_fetch:fetch_cached_data:datasize";
409
449
  errno = EINVAL; /* because wtf? */
410
450
  ret = -1;
411
451
  goto done;
@@ -413,7 +453,7 @@ fetch_cached_data(int fd, ssize_t data_size, VALUE handler, VALUE * output_data,
413
453
  data = ALLOC_N(char, data_size);
414
454
  nread = read(fd, data, data_size);
415
455
  if (nread < 0) {
416
- *errno_provenance = (char *)"bs_fetch:fetch_cached_data:read";
456
+ *errno_provenance = "bs_fetch:fetch_cached_data:read";
417
457
  ret = -1;
418
458
  goto done;
419
459
  }
@@ -422,9 +462,9 @@ fetch_cached_data(int fd, ssize_t data_size, VALUE handler, VALUE * output_data,
422
462
  goto done;
423
463
  }
424
464
 
425
- storage_data = rb_str_new_static(data, data_size);
465
+ storage_data = rb_str_new(data, data_size);
426
466
 
427
- *exception_tag = bs_storage_to_output(handler, storage_data, output_data);
467
+ *exception_tag = bs_storage_to_output(handler, args, storage_data, output_data);
428
468
  ret = 0;
429
469
  done:
430
470
  if (data != NULL) xfree(data);
@@ -465,30 +505,36 @@ mkpath(char * file_path, mode_t mode)
465
505
  * path.
466
506
  */
467
507
  static int
468
- atomic_write_cache_file(char * path, struct bs_cache_key * key, VALUE data, char ** errno_provenance)
508
+ atomic_write_cache_file(char * path, struct bs_cache_key * key, VALUE data, const char ** errno_provenance)
469
509
  {
470
510
  char template[MAX_CACHEPATH_SIZE + 20];
471
- char * dest;
472
511
  char * tmp_path;
473
- int fd, ret;
512
+ int fd, ret, attempt;
474
513
  ssize_t nwrite;
475
514
 
476
- dest = strncpy(template, path, MAX_CACHEPATH_SIZE);
477
- strcat(dest, ".tmp.XXXXXX");
515
+ for (attempt = 0; attempt < MAX_CREATE_TEMPFILE_ATTEMPT; ++attempt) {
516
+ tmp_path = strncpy(template, path, MAX_CACHEPATH_SIZE);
517
+ strcat(tmp_path, ".tmp.XXXXXX");
478
518
 
479
- tmp_path = mktemp(template);
480
- fd = open(tmp_path, O_WRONLY | O_CREAT, 0664);
481
- if (fd < 0) {
482
- if (mkpath(path, 0775) < 0) {
483
- *errno_provenance = (char *)"bs_fetch:atomic_write_cache_file:mkpath";
484
- return -1;
485
- }
486
- fd = open(tmp_path, O_WRONLY | O_CREAT, 0664);
487
- if (fd < 0) {
488
- *errno_provenance = (char *)"bs_fetch:atomic_write_cache_file:open";
519
+ // mkstemp modifies the template to be the actual created path
520
+ fd = mkstemp(tmp_path);
521
+ if (fd > 0) break;
522
+
523
+ if (attempt == 0 && mkpath(tmp_path, 0775) < 0) {
524
+ *errno_provenance = "bs_fetch:atomic_write_cache_file:mkpath";
489
525
  return -1;
490
526
  }
491
527
  }
528
+ if (fd < 0) {
529
+ *errno_provenance = "bs_fetch:atomic_write_cache_file:mkstemp";
530
+ return -1;
531
+ }
532
+
533
+ if (chmod(tmp_path, 0644) < 0) {
534
+ *errno_provenance = "bs_fetch:atomic_write_cache_file:chmod";
535
+ return -1;
536
+ }
537
+
492
538
  #ifdef _WIN32
493
539
  setmode(fd, O_BINARY);
494
540
  #endif
@@ -496,11 +542,11 @@ atomic_write_cache_file(char * path, struct bs_cache_key * key, VALUE data, char
496
542
  key->data_size = RSTRING_LEN(data);
497
543
  nwrite = write(fd, key, KEY_SIZE);
498
544
  if (nwrite < 0) {
499
- *errno_provenance = (char *)"bs_fetch:atomic_write_cache_file:write";
545
+ *errno_provenance = "bs_fetch:atomic_write_cache_file:write";
500
546
  return -1;
501
547
  }
502
548
  if (nwrite != KEY_SIZE) {
503
- *errno_provenance = (char *)"bs_fetch:atomic_write_cache_file:keysize";
549
+ *errno_provenance = "bs_fetch:atomic_write_cache_file:keysize";
504
550
  errno = EIO; /* Lies but whatever */
505
551
  return -1;
506
552
  }
@@ -508,7 +554,7 @@ atomic_write_cache_file(char * path, struct bs_cache_key * key, VALUE data, char
508
554
  nwrite = write(fd, RSTRING_PTR(data), RSTRING_LEN(data));
509
555
  if (nwrite < 0) return -1;
510
556
  if (nwrite != RSTRING_LEN(data)) {
511
- *errno_provenance = (char *)"bs_fetch:atomic_write_cache_file:writelength";
557
+ *errno_provenance = "bs_fetch:atomic_write_cache_file:writelength";
512
558
  errno = EIO; /* Lies but whatever */
513
559
  return -1;
514
560
  }
@@ -516,7 +562,12 @@ atomic_write_cache_file(char * path, struct bs_cache_key * key, VALUE data, char
516
562
  close(fd);
517
563
  ret = rename(tmp_path, path);
518
564
  if (ret < 0) {
519
- *errno_provenance = (char *)"bs_fetch:atomic_write_cache_file:rename";
565
+ *errno_provenance = "bs_fetch:atomic_write_cache_file:rename";
566
+ return -1;
567
+ }
568
+ ret = chmod(path, 0664 & ~current_umask);
569
+ if (ret < 0) {
570
+ *errno_provenance = "bs_fetch:atomic_write_cache_file:chmod";
520
571
  }
521
572
  return ret;
522
573
  }
@@ -525,13 +576,13 @@ atomic_write_cache_file(char * path, struct bs_cache_key * key, VALUE data, char
525
576
  /* Read contents from an fd, whose contents are asserted to be +size+ bytes
526
577
  * long, into a buffer */
527
578
  static ssize_t
528
- bs_read_contents(int fd, size_t size, char ** contents, char ** errno_provenance)
579
+ bs_read_contents(int fd, size_t size, char ** contents, const char ** errno_provenance)
529
580
  {
530
581
  ssize_t nread;
531
582
  *contents = ALLOC_N(char, size);
532
583
  nread = read(fd, *contents, size);
533
584
  if (nread < 0) {
534
- *errno_provenance = (char *)"bs_fetch:bs_read_contents:read";
585
+ *errno_provenance = "bs_fetch:bs_read_contents:read";
535
586
  }
536
587
  return nread;
537
588
  }
@@ -581,13 +632,13 @@ bs_read_contents(int fd, size_t size, char ** contents, char ** errno_provenance
581
632
  * - Return storage_to_output(storage_data)
582
633
  */
583
634
  static VALUE
584
- bs_fetch(char * path, VALUE path_v, char * cache_path, VALUE handler)
635
+ bs_fetch(char * path, VALUE path_v, char * cache_path, VALUE handler, VALUE args)
585
636
  {
586
637
  struct bs_cache_key cached_key, current_key;
587
638
  char * contents = NULL;
588
639
  int cache_fd = -1, current_fd = -1;
589
640
  int res, valid_cache = 0, exception_tag = 0;
590
- char * errno_provenance = NULL;
641
+ const char * errno_provenance = NULL;
591
642
 
592
643
  VALUE input_data; /* data read from source file, e.g. YAML or ruby source */
593
644
  VALUE storage_data; /* compiled data, e.g. msgpack / binary iseq */
@@ -614,7 +665,7 @@ bs_fetch(char * path, VALUE path_v, char * cache_path, VALUE handler)
614
665
  if (valid_cache) {
615
666
  /* Fetch the cache data and return it if we're able to load it successfully */
616
667
  res = fetch_cached_data(
617
- cache_fd, (ssize_t)cached_key.data_size, handler,
668
+ cache_fd, (ssize_t)cached_key.data_size, handler, args,
618
669
  &output_data, &exception_tag, &errno_provenance
619
670
  );
620
671
  if (exception_tag != 0) goto raise;
@@ -628,15 +679,15 @@ bs_fetch(char * path, VALUE path_v, char * cache_path, VALUE handler)
628
679
 
629
680
  /* Read the contents of the source file into a buffer */
630
681
  if (bs_read_contents(current_fd, current_key.size, &contents, &errno_provenance) < 0) goto fail_errno;
631
- input_data = rb_str_new_static(contents, current_key.size);
682
+ input_data = rb_str_new(contents, current_key.size);
632
683
 
633
684
  /* Try to compile the input_data using input_to_storage(input_data) */
634
- exception_tag = bs_input_to_storage(handler, input_data, path_v, &storage_data);
685
+ exception_tag = bs_input_to_storage(handler, args, input_data, path_v, &storage_data);
635
686
  if (exception_tag != 0) goto raise;
636
687
  /* If input_to_storage raised Bootsnap::CompileCache::Uncompilable, don't try
637
688
  * to cache anything; just return input_to_output(input_data) */
638
689
  if (storage_data == uncompilable) {
639
- bs_input_to_output(handler, input_data, &output_data, &exception_tag);
690
+ bs_input_to_output(handler, args, input_data, &output_data, &exception_tag);
640
691
  if (exception_tag != 0) goto raise;
641
692
  goto succeed;
642
693
  }
@@ -648,17 +699,17 @@ bs_fetch(char * path, VALUE path_v, char * cache_path, VALUE handler)
648
699
  if (res < 0) goto fail_errno;
649
700
 
650
701
  /* Having written the cache, now convert storage_data to output_data */
651
- exception_tag = bs_storage_to_output(handler, storage_data, &output_data);
702
+ exception_tag = bs_storage_to_output(handler, args, storage_data, &output_data);
652
703
  if (exception_tag != 0) goto raise;
653
704
 
654
705
  /* If output_data is nil, delete the cache entry and generate the output
655
706
  * using input_to_output */
656
707
  if (NIL_P(output_data)) {
657
708
  if (unlink(cache_path) < 0) {
658
- errno_provenance = (char *)"bs_fetch:unlink";
709
+ errno_provenance = "bs_fetch:unlink";
659
710
  goto fail_errno;
660
711
  }
661
- bs_input_to_output(handler, input_data, &output_data, &exception_tag);
712
+ bs_input_to_output(handler, args, input_data, &output_data, &exception_tag);
662
713
  if (exception_tag != 0) goto raise;
663
714
  }
664
715
 
@@ -708,16 +759,19 @@ invalid_type_storage_data:
708
759
 
709
760
  struct s2o_data {
710
761
  VALUE handler;
762
+ VALUE args;
711
763
  VALUE storage_data;
712
764
  };
713
765
 
714
766
  struct i2o_data {
715
767
  VALUE handler;
768
+ VALUE args;
716
769
  VALUE input_data;
717
770
  };
718
771
 
719
772
  struct i2s_data {
720
773
  VALUE handler;
774
+ VALUE args;
721
775
  VALUE input_data;
722
776
  VALUE pathval;
723
777
  };
@@ -726,15 +780,16 @@ static VALUE
726
780
  prot_storage_to_output(VALUE arg)
727
781
  {
728
782
  struct s2o_data * data = (struct s2o_data *)arg;
729
- return rb_funcall(data->handler, rb_intern("storage_to_output"), 1, data->storage_data);
783
+ return rb_funcall(data->handler, rb_intern("storage_to_output"), 2, data->storage_data, data->args);
730
784
  }
731
785
 
732
786
  static int
733
- bs_storage_to_output(VALUE handler, VALUE storage_data, VALUE * output_data)
787
+ bs_storage_to_output(VALUE handler, VALUE args, VALUE storage_data, VALUE * output_data)
734
788
  {
735
789
  int state;
736
790
  struct s2o_data s2o_data = {
737
791
  .handler = handler,
792
+ .args = args,
738
793
  .storage_data = storage_data,
739
794
  };
740
795
  *output_data = rb_protect(prot_storage_to_output, (VALUE)&s2o_data, &state);
@@ -742,10 +797,11 @@ bs_storage_to_output(VALUE handler, VALUE storage_data, VALUE * output_data)
742
797
  }
743
798
 
744
799
  static void
745
- bs_input_to_output(VALUE handler, VALUE input_data, VALUE * output_data, int * exception_tag)
800
+ bs_input_to_output(VALUE handler, VALUE args, VALUE input_data, VALUE * output_data, int * exception_tag)
746
801
  {
747
802
  struct i2o_data i2o_data = {
748
803
  .handler = handler,
804
+ .args = args,
749
805
  .input_data = input_data,
750
806
  };
751
807
  *output_data = rb_protect(prot_input_to_output, (VALUE)&i2o_data, exception_tag);
@@ -755,18 +811,18 @@ static VALUE
755
811
  prot_input_to_output(VALUE arg)
756
812
  {
757
813
  struct i2o_data * data = (struct i2o_data *)arg;
758
- return rb_funcall(data->handler, rb_intern("input_to_output"), 1, data->input_data);
814
+ return rb_funcall(data->handler, rb_intern("input_to_output"), 2, data->input_data, data->args);
759
815
  }
760
816
 
761
817
  static VALUE
762
818
  try_input_to_storage(VALUE arg)
763
819
  {
764
820
  struct i2s_data * data = (struct i2s_data *)arg;
765
- return rb_funcall(data->handler, rb_intern("input_to_storage"), 2, data->input_data, data->pathval);
821
+ return rb_funcall(data->handler, rb_intern("input_to_storage"), 3, data->input_data, data->pathval, data->args);
766
822
  }
767
823
 
768
824
  static VALUE
769
- rescue_input_to_storage(VALUE arg)
825
+ rescue_input_to_storage(VALUE arg, VALUE e)
770
826
  {
771
827
  return uncompilable;
772
828
  }
@@ -782,11 +838,12 @@ prot_input_to_storage(VALUE arg)
782
838
  }
783
839
 
784
840
  static int
785
- bs_input_to_storage(VALUE handler, VALUE input_data, VALUE pathval, VALUE * storage_data)
841
+ bs_input_to_storage(VALUE handler, VALUE args, VALUE input_data, VALUE pathval, VALUE * storage_data)
786
842
  {
787
843
  int state;
788
844
  struct i2s_data i2s_data = {
789
845
  .handler = handler,
846
+ .args = args,
790
847
  .input_data = input_data,
791
848
  .pathval = pathval,
792
849
  };
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require("mkmf")
2
3
  $CFLAGS << ' -O3 '
3
4
  $CFLAGS << ' -std=c99'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Bootsnap
2
3
  extend(self)
3
4