Add fmemopen_unlocked() and use unlocked ops in fuzzers and some other tests

This might make things marginially faster. I didn't benchmark though.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2019-04-04 12:24:38 +02:00
parent 2fe21124a6
commit 673a1e6fb9
14 changed files with 40 additions and 30 deletions

View file

@ -65,6 +65,16 @@ FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc) {
return f;
}
FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode) {
FILE *f = fmemopen(buf, size, mode);
if (!f)
return NULL;
(void) __fsetlocking(f, FSETLOCKING_BYCALLER);
return f;
}
int write_string_stream_ts(
FILE *f,
const char *line,

View file

@ -36,6 +36,7 @@ typedef enum {
int fopen_unlocked(const char *path, const char *options, FILE **ret);
int fdopen_unlocked(int fd, const char *options, FILE **ret);
FILE* open_memstream_unlocked(char **ptr, size_t *sizeloc);
FILE* fmemopen_unlocked(void *buf, size_t size, const char *mode);
int write_string_stream_ts(FILE *f, const char *line, WriteStringFileFlags flags, struct timespec *ts);
static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) {

View file

@ -1,13 +1,11 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <stdio.h>
#include "alloc-util.h"
#include "bus-dump.h"
#include "bus-message.h"
#include "env-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fuzz.h"
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
@ -36,7 +34,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
TAKE_PTR(buffer);
if (getenv_bool("SYSTEMD_FUZZ_OUTPUT") <= 0)
assert_se(g = open_memstream(&out, &out_size));
assert_se(g = open_memstream_unlocked(&out, &out_size));
bus_message_dump(m, g ?: stdout, BUS_MESSAGE_DUMP_WITH_HEADER);

View file

@ -1,9 +1,10 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <stdio.h>
#include "alloc-util.h"
#include "env-file.h"
#include "fileio.h"
#include "fd-util.h"
#include "fuzz.h"
#include "strv.h"
@ -15,7 +16,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (size == 0 || size > 65535)
return 0;
f = fmemopen((char*) data, size, "re");
f = fmemopen_unlocked((char*) data, size, "re");
assert_se(f);
/* We don't want to fill the logs with messages about parse errors.

View file

@ -1,9 +1,8 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fuzz.h"
#include "hostname-util.h"
@ -14,7 +13,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (size == 0)
return 0;
f = fmemopen((char*) data, size, "re");
f = fmemopen_unlocked((char*) data, size, "re");
assert_se(f);
/* We don't want to fill the logs with messages about parse errors.

View file

@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include "alloc-util.h"
#include "fileio.h"
#include "fd-util.h"
#include "fuzz.h"
#include "json.h"
@ -14,13 +15,13 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (size == 0)
return 0;
f = fmemopen((char*) data, size, "re");
f = fmemopen_unlocked((char*) data, size, "re");
assert_se(f);
if (json_parse_file(f, NULL, &v, NULL, NULL) < 0)
return 0;
g = open_memstream(&out, &out_size);
g = open_memstream_unlocked(&out, &out_size);
assert_se(g);
json_variant_dump(v, 0, g, NULL);

View file

@ -1,9 +1,8 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fuzz.h"
#include "nspawn-oci.h"
@ -14,7 +13,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (size == 0)
return 0;
f = fmemopen((char*) data, size, "re");
f = fmemopen_unlocked((char*) data, size, "re");
assert_se(f);
/* We don't want to fill the logs with messages about parse errors.

View file

@ -1,9 +1,8 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fuzz.h"
#include "nspawn-settings.h"
@ -14,7 +13,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (size == 0)
return 0;
f = fmemopen((char*) data, size, "re");
f = fmemopen_unlocked((char*) data, size, "re");
assert_se(f);
/* We don't want to fill the logs with messages about parse errors.

View file

@ -24,7 +24,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (size == 0)
return 0;
f = fmemopen((char*) data, size, "re");
f = fmemopen_unlocked((char*) data, size, "re");
assert_se(f);
if (read_line(f, LINE_MAX, &p) < 0)
@ -75,7 +75,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
config_item_perf_lookup, load_fragment_gperf_lookup,
CONFIG_PARSE_ALLOW_INCLUDE, u);
g = open_memstream(&out, &out_size);
g = open_memstream_unlocked(&out, &out_size);
assert_se(g);
unit_dump(u, g, "");

View file

@ -20,6 +20,7 @@
#include "bus-util.h"
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
#include "log.h"
#include "tests.h"
#include "util.h"
@ -189,7 +190,7 @@ int main(int argc, char *argv[]) {
bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
ms = open_memstream(&first, &first_size);
ms = open_memstream_unlocked(&first, &first_size);
bus_message_dump(m, ms, 0);
fflush(ms);
assert_se(!ferror(ms));
@ -245,7 +246,7 @@ int main(int argc, char *argv[]) {
bus_message_dump(m, stdout, BUS_MESSAGE_DUMP_WITH_HEADER);
fclose(ms);
ms = open_memstream(&second, &second_size);
ms = open_memstream_unlocked(&second, &second_size);
bus_message_dump(m, ms, 0);
fflush(ms);
assert_se(!ferror(ms));
@ -351,7 +352,7 @@ int main(int argc, char *argv[]) {
assert_se(r >= 0);
fclose(ms);
ms = open_memstream(&third, &third_size);
ms = open_memstream_unlocked(&third, &third_size);
bus_message_dump(copy, ms, 0);
fflush(ms);
assert_se(!ferror(ms));

View file

@ -12,6 +12,7 @@
#include "dirent-util.h"
#include "env-file.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-table.h"
#include "fs-util.h"
#include "locale-util.h"
@ -272,10 +273,9 @@ static int inspect_image(int argc, char *argv[], void *userdata) {
nl = true;
} else {
_cleanup_free_ char *pretty_portable = NULL, *pretty_os = NULL;
_cleanup_fclose_ FILE *f;
f = fmemopen((void*) data, sz, "re");
f = fmemopen_unlocked((void*) data, sz, "re");
if (!f)
return log_error_errno(errno, "Failed to open /etc/os-release buffer: %m");

View file

@ -298,7 +298,7 @@ static int boot_entry_load_unified(
if (!k)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Path is not below root: %s", path);
f = fmemopen((void*) osrelease, strlen(osrelease), "r");
f = fmemopen_unlocked((void*) osrelease, strlen(osrelease), "r");
if (!f)
return log_error_errno(errno, "Failed to open os-release buffer: %m");

View file

@ -637,7 +637,7 @@ static void test_fgetc(void) {
_cleanup_fclose_ FILE *f = NULL;
char c;
f = fmemopen((void*) chars, sizeof(chars), "re");
f = fmemopen_unlocked((void*) chars, sizeof(chars), "re");
assert_se(f);
for (unsigned i = 0; i < sizeof(chars); i++) {
@ -727,7 +727,7 @@ static void test_read_line_one_file(FILE *f) {
static void test_read_line(void) {
_cleanup_fclose_ FILE *f = NULL;
f = fmemopen((void*) buffer, sizeof(buffer), "re");
f = fmemopen_unlocked((void*) buffer, sizeof(buffer), "re");
assert_se(f);
test_read_line_one_file(f);
@ -792,7 +792,7 @@ static void test_read_line4(void) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *s = NULL;
assert_se(f = fmemopen((void*) eof_endings[i].string, eof_endings[i].length, "r"));
assert_se(f = fmemopen_unlocked((void*) eof_endings[i].string, eof_endings[i].length, "r"));
r = read_line(f, (size_t) -1, &s);
assert_se((size_t) r == eof_endings[i].length);
@ -813,7 +813,7 @@ static void test_read_nul_string(void) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *s = NULL;
assert_se(f = fmemopen((void*) test, sizeof(test)-1, "r"));
assert_se(f = fmemopen_unlocked((void*) test, sizeof(test)-1, "r"));
assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 13 && streq_ptr(s, "string nr. 1"));
s = mfree(s);

View file

@ -4,6 +4,7 @@
#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "json-internal.h"
#include "json.h"
#include "string-util.h"
@ -358,7 +359,7 @@ static void test_source(void) {
"%s"
"--- original end ---\n", data);
assert_se(f = fmemopen((void*) data, strlen(data), "r"));
assert_se(f = fmemopen_unlocked((void*) data, strlen(data), "r"));
assert_se(json_parse_file(f, "waldo", &v, NULL, NULL) >= 0);