Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

common: (win) fix possible pool file coruption #3728

Merged
merged 3 commits into from
May 13, 2019

Conversation

marcinslusarz
Copy link
Contributor

@marcinslusarz marcinslusarz commented May 7, 2019

It turns out that _wopen on Windows can truncate a file by 1 byte when O_BINARY flag is not used and the last byte has a value of 0x1a. It doesn't always happen, but it does happen frequently enough. Currently this behavior is not mentioned in _wopen documentation (https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/open-wopen?view=vs-2019), but is in fopen documentation: https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=vs-2019. Specifically it says:

In text mode, CTRL+Z is interpreted as an EOF character on input. In files that are opened for reading/writing by using "a+", fopen checks for a CTRL+Z at the end of the file and removes it, if it is possible. This is done because using fseek and ftell to move within a file that ends with CTRL+Z may cause fseek to behave incorrectly near the end of the file.

Opening files without O_BINARY was never an immediate problem for us, because we are not reading anything from the opened files using "read", just mapping them.

This bug affects:

  • pmem_map_file
  • pmempool_create
  • pmempool_dump
  • pmempool_info
  • pmempool_transform

libpmemobj, libpmemblk, libpmemlog, librpmem, libvmem and libvmmalloc are not affected.

Ref: pmem/issues#972
Ref: pmem/issues#715
Ref: pmem/issues#603


This change is Reviewable

May affect:
- pmempool_transform
- pmempool_info
- pmempool_dump
- pmempool_create
May affect:
- pmempool_create
- pmempool_sync
- pmempool_rm
- pmempool_transform
@marcinslusarz marcinslusarz changed the base branch from master to stable-1.5 May 7, 2019 10:19
@pmem-bot
Copy link
Contributor

pmem-bot commented May 7, 2019

@pbalcer and @wlemkows please review this pull request

Copy link
Contributor

@kilobyte kilobyte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @marcinslusarz)


src/common/file.c, line 293 at r1 (raw file):

	int flags = O_RDWR;
#ifdef _WIN32
	flags |= O_BINARY;

I wonder if it would be more maintenable to #define O_BINARY 0 on non-Windows, then use it unconditionally everywhere. Less #ifdefs.

Affects:
- all external users of pmem_map_file
- pmempool_check (backups)
@marcinslusarz
Copy link
Contributor Author


src/common/file.c, line 293 at r1 (raw file):

Previously, kilobyte (Adam Borowski) wrote…

I wonder if it would be more maintenable to #define O_BINARY 0 on non-Windows, then use it unconditionally everywhere. Less #ifdefs.

Unfortunately it's not that simple. O_BINARY is in global/libc name space, so we can't just define it, because it may clash with future definitions of this constant. Using PMDK_O_BINARY with O_RDWR would look awkward. If I would start fixing this (and the whole mess of unicode support on Windows), it would be much longer effort. This is a minimal fix which works and will let us release 1.6.1 soon. It can be cleaned up later on master.

Copy link
Contributor

@kilobyte kilobyte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 2 files reviewed, all discussions resolved


src/common/file.c, line 293 at r1 (raw file):

Previously, marcinslusarz (Marcin Ślusarz) wrote…

Unfortunately it's not that simple. O_BINARY is in global/libc name space, so we can't just define it, because it may clash with future definitions of this constant. Using PMDK_O_BINARY with O_RDWR would look awkward. If I would start fixing this (and the whole mess of unicode support on Windows), it would be much longer effort. This is a minimal fix which works and will let us release 1.6.1 soon. It can be cleaned up later on master.

Right then.

@pbalcer
Copy link
Member

pbalcer commented May 8, 2019

Can we add a windows test for this specific case?

@marcinslusarz
Copy link
Contributor Author

Can we add a windows test for this specific case?

https://github.com/marcinslusarz/pmdk/commits/win-trunc-test

I'll submit it once this PR and necessary bug fixes for Python test framework will land on master.

Copy link
Contributor

@krzycz krzycz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm_strong:

Reviewed 1 of 2 files at r1, 1 of 1 files at r3.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved

@marcinslusarz
Copy link
Contributor Author

@pbalcer ?

@marcinslusarz marcinslusarz merged commit 1029741 into pmem:stable-1.5 May 13, 2019
@marcinslusarz marcinslusarz deleted the win-corruption-1.5 branch May 13, 2019 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants