mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 13:52:28 +00:00
Make C memory safe like Rust
This change enables Address Sanitizer systemically w/ `make MODE=dbg`. Our version of Rust's `unsafe` keyword is named `noasan` which is used for two functions that do aligned memory chunking, like `strcpy.c` and we need to fix the tiny DEFLATE code, but that's it everything else is fabulous you can have all the fischer price security blankets you need Best of all is we're now able to use the ASAN data in Blinkenlights to colorize the memory dumps. See the screenshot below of a test program: https://justine.lol/blinkenlights/asan.png Which is operating on float arrays stored on the stack, with red areas indicating poisoned memory, and the green areas indicate valid memory.
This commit is contained in:
parent
fdc3fa9148
commit
1ff9ab95ac
153 changed files with 2545 additions and 2077 deletions
|
@ -16,6 +16,7 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
@ -24,16 +25,16 @@ TEST(GetDosEnviron, testOneVariable) {
|
|||
#define kEnv u"A=Und wird die Welt auch in Flammen stehen\0"
|
||||
size_t max = 2;
|
||||
size_t size = sizeof(kEnv) >> 1;
|
||||
char *block = tmalloc(size);
|
||||
char16_t *env = memcpy(tmalloc(sizeof(kEnv)), kEnv, sizeof(kEnv));
|
||||
char **envp = tmalloc(max * sizeof(char *));
|
||||
char *block = malloc(size);
|
||||
char16_t *env = memcpy(malloc(sizeof(kEnv)), kEnv, sizeof(kEnv));
|
||||
char **envp = malloc(max * sizeof(char *));
|
||||
EXPECT_EQ(1, GetDosEnviron(env, block, size, envp, max));
|
||||
EXPECT_STREQ("A=Und wird die Welt auch in Flammen stehen", envp[0]);
|
||||
EXPECT_EQ(NULL, envp[1]);
|
||||
ASSERT_BINEQ(u"A=Und wird die Welt auch in Flammen stehen ", block);
|
||||
tfree(envp);
|
||||
tfree(env);
|
||||
tfree(block);
|
||||
free(envp);
|
||||
free(env);
|
||||
free(block);
|
||||
#undef kEnv
|
||||
}
|
||||
|
||||
|
@ -43,16 +44,16 @@ TEST(GetDosEnviron, testTwoVariables) {
|
|||
u"𐌴𐌵𐌶𐌷=Wir werden wieder auferstehen\0")
|
||||
size_t max = 3;
|
||||
size_t size = 1024;
|
||||
char *block = tmalloc(size);
|
||||
char16_t *env = memcpy(tmalloc(sizeof(kEnv)), kEnv, sizeof(kEnv));
|
||||
char **envp = tmalloc(max * sizeof(char *));
|
||||
char *block = malloc(size);
|
||||
char16_t *env = memcpy(malloc(sizeof(kEnv)), kEnv, sizeof(kEnv));
|
||||
char **envp = malloc(max * sizeof(char *));
|
||||
EXPECT_EQ(2, GetDosEnviron(env, block, size, envp, max));
|
||||
EXPECT_STREQ("𐌰𐌱𐌲𐌳=Und wird die Welt auch in Flammen stehen", envp[0]);
|
||||
EXPECT_STREQ("𐌴𐌵𐌶𐌷=Wir werden wieder auferstehen", envp[1]);
|
||||
EXPECT_EQ(NULL, envp[2]);
|
||||
tfree(envp);
|
||||
tfree(env);
|
||||
tfree(block);
|
||||
free(envp);
|
||||
free(env);
|
||||
free(block);
|
||||
#undef kEnv
|
||||
}
|
||||
|
||||
|
@ -60,16 +61,16 @@ TEST(GetDosEnviron, testOverrun_truncatesWithGrace) {
|
|||
#define kEnv u"A=Und wird die Welt auch in Flammen stehen\0"
|
||||
size_t max = 2;
|
||||
size_t size = sizeof(kEnv) >> 2;
|
||||
char *block = tmalloc(size);
|
||||
char16_t *env = memcpy(tmalloc(sizeof(kEnv)), kEnv, sizeof(kEnv));
|
||||
char **envp = tmalloc(max * sizeof(char *));
|
||||
char *block = malloc(size);
|
||||
char16_t *env = memcpy(malloc(sizeof(kEnv)), kEnv, sizeof(kEnv));
|
||||
char **envp = malloc(max * sizeof(char *));
|
||||
EXPECT_EQ(1, GetDosEnviron(env, block, size, envp, max));
|
||||
EXPECT_STREQ("A=Und wird die Welt ", envp[0]);
|
||||
EXPECT_EQ(NULL, envp[1]);
|
||||
ASSERT_BINEQ(u"A=Und wird die Welt ", block);
|
||||
tfree(envp);
|
||||
tfree(env);
|
||||
tfree(block);
|
||||
free(envp);
|
||||
free(env);
|
||||
free(block);
|
||||
#undef kEnv
|
||||
}
|
||||
|
||||
|
@ -79,24 +80,24 @@ TEST(GetDosEnviron, testEmpty_doesntTouchMemory) {
|
|||
|
||||
TEST(GetDosEnviron, testEmpty_zeroTerminatesWheneverPossible_1) {
|
||||
size_t max = 1;
|
||||
char **envp = tmalloc(max * sizeof(char *));
|
||||
char **envp = malloc(max * sizeof(char *));
|
||||
EXPECT_EQ(0, GetDosEnviron(u"", NULL, 0, envp, max));
|
||||
EXPECT_EQ(NULL, envp[0]);
|
||||
tfree(envp);
|
||||
free(envp);
|
||||
}
|
||||
|
||||
TEST(GetDosEnviron, testEmpty_zeroTerminatesWheneverPossible_2) {
|
||||
size_t size = 1;
|
||||
char *block = tmalloc(size);
|
||||
char *block = malloc(size);
|
||||
EXPECT_EQ(0, GetDosEnviron(u"", block, size, NULL, 0));
|
||||
EXPECT_BINEQ(u" ", block);
|
||||
tfree(block);
|
||||
free(block);
|
||||
}
|
||||
|
||||
TEST(GetDosEnviron, testEmpty_zeroTerminatesWheneverPossible_3) {
|
||||
size_t size = 2;
|
||||
char *block = tmalloc(size);
|
||||
char *block = malloc(size);
|
||||
EXPECT_EQ(0, GetDosEnviron(u"", block, size, NULL, 0));
|
||||
EXPECT_BINEQ(u" ", block);
|
||||
tfree(block);
|
||||
free(block);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue